Skip to content

Commit

Permalink
Fix start on system startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Semper-Viventem committed Jan 25, 2025
1 parent 36b4b7e commit 8304418
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
minSdk = 29
targetSdk = 35

versionCode = 1708536364
versionCode = 1708536365
versionName = "0.27.2-beta"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PermissionHelper(
private var permissionRequestTime: Long? = null
private val backgroundPermissionState = MutableStateFlow(backgroundLocationAllowed())

fun checkBlePermissions(
fun checkOrRequestPermission(
onRequestPermissions: (permissions: Array<String>, permissionRequestCode: Int, pendingFun: () -> Unit) -> Unit = ::requestPermissions,
permissions: Array<String> = BLE_PERMISSIONS,
permissionRequestCode: Int = PERMISSIONS_REQUEST_CODE,
Expand Down Expand Up @@ -82,6 +82,10 @@ class PermissionHelper(
)
}

fun blePermissionsAllowed(): Boolean {
return BLE_PERMISSIONS.all { checkPermission(it) }
}

fun checkAllPermissions(): Boolean {
return (BLE_PERMISSIONS + BACKGROUND_LOCATION).all { checkPermission(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class BgScanService : Service() {
ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE or ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION,
)

permissionHelper.checkBlePermissions(
permissionHelper.checkOrRequestPermission(
onRequestPermissions = { _, _, _ ->
reportError(IllegalStateException("BLE Service is started but permissins are not granted"))
stopSelf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BootBroadcastReceiver : BroadcastReceiver() {

private fun tryToRunService(context: Context) {
if (settingsRepository.getRunOnStartup()) {
if (permissionHelper.checkAllPermissions()) {
if (permissionHelper.blePermissionsAllowed()) {
try {
BgScanService.start(context)
} catch (error: Exception) {
Expand All @@ -38,13 +38,23 @@ class BootBroadcastReceiver : BroadcastReceiver() {
title = "[Launch on system startup error]: ${error.message ?: error::class.java}",
stackTrace = error.stackTraceToString(),
)
scope.launch {
saveReportInteractor.execute(report)
}
report(report)
}
} else {
Timber.e("Not all permissions granted, can't start service from the boot receiver")
report(
JournalEntry.Report.Error(
title = "[Launch on system startup error]: Not all permissions granted",
stackTrace = IllegalStateException("Not all permissions granted").stackTraceToString()
)
)
}
}
}

private fun report(report: JournalEntry.Report.Error) {
Timber.e(report.stackTrace)
scope.launch {
saveReportInteractor.execute(report)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class BackgroundLocationRequestViewModel(
}

fun grantPermission() {
permissionHelper.checkBlePermissions {
permissionHelper.checkBlePermissions(permissions = PermissionHelper.BACKGROUND_LOCATION) {
permissionHelper.checkOrRequestPermission {
permissionHelper.checkOrRequestPermission(permissions = PermissionHelper.BACKGROUND_LOCATION) {
onBack()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class DeviceDetailsViewModel(
}

private fun observeLocation() {
permissionHelper.checkBlePermissions {
permissionHelper.checkOrRequestPermission {
viewModelScope.launch {
locationProvider.fetchOnce()
locationProvider.observeLocation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class MainViewModel(
}

private fun checkPermissions(granted: () -> Unit) {
permissionHelper.checkBlePermissions {
permissionHelper.checkOrRequestPermission {
permissionHelper.checkDozeModePermission()
granted.invoke()
}
Expand Down

0 comments on commit 8304418

Please sign in to comment.