Skip to content

Commit

Permalink
Merge pull request #122 from Semper-Viventem/show-online-status-on-th…
Browse files Browse the repository at this point in the history
…e-device-details

Show online status for selected device
  • Loading branch information
Semper-Viventem authored Feb 28, 2024
2 parents e077beb + fa23257 commit 8919078
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 153 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {
minSdk = 29
targetSdk = 34

versionCode = 1708536347
versionName = "0.23.0-beta"
versionCode = 1708536350
versionName = "0.24.0-beta"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/f/cking/software/domain/model/DeviceData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ data class DeviceData(
return (System.currentTimeMillis() - lastDetectTimeMs).getTimePeriodStr(context)
}

fun hasBeenSeenTimeAgo(): Long {
return System.currentTimeMillis() - lastDetectTimeMs
}

fun distance(): Float? {
return if (rssi != null) {
val txPower = -59 //hard coded power value. Usually ranges between -59 to -65
Expand Down
40 changes: 35 additions & 5 deletions app/src/main/java/f/cking/software/service/BgScanService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.android.ext.android.inject
Expand Down Expand Up @@ -65,7 +68,7 @@ class BgScanService : Service() {

override fun onCreate() {
super.onCreate()
isActive.tryEmit(true)
updateState(ScannerState.IDLING)
}

private fun handleError(exception: Throwable) {
Expand Down Expand Up @@ -118,7 +121,7 @@ class BgScanService : Service() {
super.onDestroy()
Timber.d("Background service destroyed")
scope.cancel()
isActive.tryEmit(false)
updateState(ScannerState.DISABLED)
bleScannerHelper.stopScanning()
locationProvider.stopLocationListening()
handler.removeCallbacks(nextScanRunnable)
Expand All @@ -132,6 +135,7 @@ class BgScanService : Service() {
private fun scan() {
scope.launch {
try {
updateState(ScannerState.SCANNING)
bleScannerHelper.scan(scanListener = bleListener)
} catch (e: BleScannerHelper.BluetoothIsNotInitialized) {
handleBleIsTurnedOffError()
Expand Down Expand Up @@ -183,6 +187,7 @@ class BgScanService : Service() {
bluetoothDisabledWasReported = false

return try {
updateState(ScannerState.ANALYZING)
val analyseResult = analyseScanBatchInteractor.execute(batch)
withContext(Dispatchers.Default) {
saveScanBatchInteractor.execute(batch)
Expand Down Expand Up @@ -217,6 +222,7 @@ class BgScanService : Service() {
}

private fun scheduleNextScan() {
updateState(ScannerState.IDLING)
val interval = powerModeHelper.powerMode().scanInterval
handler.postDelayed(nextScanRunnable, interval)
}
Expand All @@ -232,13 +238,37 @@ class BgScanService : Service() {
}
}

enum class ScannerState {
DISABLED, SCANNING, ANALYZING, IDLING;

fun isActive(): Boolean {
return this != DISABLED
}

fun isProcessing(): Boolean {
return this == SCANNING || this == ANALYZING
}
}

companion object {
private const val MAX_FAILURE_SCANS_TO_CLOSE = 10

private const val ACTION_STOP_SERVICE = "stop_ble_scan_service"
private const val ACTION_SCAN_NOW = "ble_scan_now"

var isActive = MutableStateFlow(false)
var state = MutableStateFlow(ScannerState.DISABLED)
private set
val isActive: Boolean get() = state.value.isActive()

private fun updateState(newState: ScannerState) {
Timber.i("Scanner state: $newState")
state.tryEmit(newState)
}

fun observeIsActive(): Flow<Boolean> {
return state.map { it.isActive() }
.distinctUntilChanged()
}

private fun createCloseServiceIntent(context: Context): Intent {
return Intent(context, BgScanService::class.java).apply {
Expand All @@ -252,13 +282,13 @@ class BgScanService : Service() {
}

fun stop(context: Context) {
if (isActive.value) {
if (isActive) {
context.startService(createCloseServiceIntent(context))
}
}

fun scan(context: Context) {
if (isActive.value) {
if (isActive) {
val intent = Intent(context, BgScanService::class.java).apply {
action = ACTION_SCAN_NOW
}
Expand Down
Loading

0 comments on commit 8919078

Please sign in to comment.