diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c0d678f5c..6e1cfdf50 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -133,7 +133,7 @@ dependencies { implementation("com.google.android.flexbox:flexbox:3.0.0") // Andromeda - val andromedaVersion = "6e2386bc21" + val andromedaVersion = "c8b8baf887" implementation("com.github.kylecorry31.andromeda:core:$andromedaVersion") implementation("com.github.kylecorry31.andromeda:fragments:$andromedaVersion") implementation("com.github.kylecorry31.andromeda:forms:$andromedaVersion") @@ -166,7 +166,7 @@ dependencies { implementation("com.github.kylecorry31.andromeda:list:$andromedaVersion") // Ceres - val ceresVersion = "093e9f14ed" + val ceresVersion = "e45e6958fb" implementation("com.github.kylecorry31.ceres:list:$ceresVersion") implementation("com.github.kylecorry31.ceres:toolbar:$ceresVersion") implementation("com.github.kylecorry31.ceres:badge:$ceresVersion") diff --git a/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/BacktrackIsEnabled.kt b/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/BacktrackIsEnabled.kt index 3f319d920..5fe8d7a74 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/BacktrackIsEnabled.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/BacktrackIsEnabled.kt @@ -2,14 +2,10 @@ package com.kylecorry.trail_sense.navigation.paths.infrastructure import android.content.Context import com.kylecorry.andromeda.core.specifications.Specification -import com.kylecorry.andromeda.permissions.Permissions import com.kylecorry.trail_sense.shared.UserPreferences -import com.kylecorry.trail_sense.shared.permissions.canRunLocationForegroundService class BacktrackIsEnabled : Specification() { override fun isSatisfiedBy(value: Context): Boolean { - val prefs = UserPreferences(value) - val hasPermission = Permissions.canRunLocationForegroundService(value) - return hasPermission && prefs.backtrackEnabled && BacktrackIsAvailable().isSatisfiedBy(value) + return UserPreferences(value).backtrackEnabled } } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/BacktrackRequiresForeground.kt b/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/BacktrackRequiresForeground.kt deleted file mode 100644 index 8b0c92f5d..000000000 --- a/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/BacktrackRequiresForeground.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.kylecorry.trail_sense.navigation.paths.infrastructure - -import android.content.Context -import com.kylecorry.andromeda.core.specifications.Specification -import com.kylecorry.trail_sense.shared.UserPreferences -import java.time.Duration - -class BacktrackRequiresForeground : Specification() { - override fun isSatisfiedBy(value: Context): Boolean { - val prefs = UserPreferences(value) - return prefs.backtrackRecordFrequency >= Duration.ofMinutes(15) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/subsystem/BacktrackSubsystem.kt b/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/subsystem/BacktrackSubsystem.kt index 2ac487d40..4ff8b1d2f 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/subsystem/BacktrackSubsystem.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/navigation/paths/infrastructure/subsystem/BacktrackSubsystem.kt @@ -67,8 +67,8 @@ class BacktrackSubsystem private constructor(private val context: Context) { return frequency.getOrNull() ?: Duration.ofMinutes(30) } - suspend fun enable(startNewPath: Boolean, isInBackground: Boolean = false) { - if (isInBackground && !Permissions.canRunLocationForegroundService(context, true)) { + suspend fun enable(startNewPath: Boolean) { + if (!Permissions.canRunLocationForegroundService(context)) { ServiceRestartAlerter(context).alert() Log.d("BacktrackSubsystem", "Cannot start backtrack") return @@ -87,10 +87,10 @@ class BacktrackSubsystem private constructor(private val context: Context) { } private fun calculateBacktrackState(): FeatureState { - return if (BacktrackScheduler.isOn(context)) { - FeatureState.On - } else if (BacktrackScheduler.isDisabled(context)) { + return if (BacktrackScheduler.isDisabled(context)) { FeatureState.Unavailable + } else if (BacktrackScheduler.isOn(context)) { + FeatureState.On } else { FeatureState.Off } diff --git a/app/src/main/java/com/kylecorry/trail_sense/receivers/TrailSenseServiceUtils.kt b/app/src/main/java/com/kylecorry/trail_sense/receivers/TrailSenseServiceUtils.kt index 0cf77da78..d85b6cae9 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/receivers/TrailSenseServiceUtils.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/receivers/TrailSenseServiceUtils.kt @@ -28,7 +28,7 @@ object TrailSenseServiceUtils { } startWeatherMonitoring(context, isInBackground) - startBacktrack(context, isInBackground) + startBacktrack(context) startPedometer(context) startSunsetAlarm(context) startAstronomyAlerts(context) @@ -73,11 +73,11 @@ object TrailSenseServiceUtils { } } - private suspend fun startBacktrack(context: Context, isInBackground: Boolean) { + private suspend fun startBacktrack(context: Context) { val backtrack = BacktrackSubsystem.getInstance(context) if (backtrack.getState() == FeatureState.On) { if (!BacktrackService.isRunning) { - backtrack.enable(false, isInBackground) + backtrack.enable(false) } } else { backtrack.disable() diff --git a/app/src/main/java/com/kylecorry/trail_sense/shared/LowPowerMode.kt b/app/src/main/java/com/kylecorry/trail_sense/shared/LowPowerMode.kt index b3c34ee58..c4bc621f4 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/shared/LowPowerMode.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/shared/LowPowerMode.kt @@ -2,7 +2,6 @@ package com.kylecorry.trail_sense.shared import android.app.Activity import android.content.Context -import com.kylecorry.trail_sense.navigation.paths.infrastructure.BacktrackIsEnabled import com.kylecorry.trail_sense.navigation.paths.infrastructure.BacktrackScheduler import com.kylecorry.trail_sense.tools.pedometer.infrastructure.StepCounterService import com.kylecorry.trail_sense.weather.infrastructure.WeatherMonitorIsEnabled @@ -45,7 +44,7 @@ class LowPowerMode(val context: Context) { WeatherUpdateScheduler.start(context) } - if (BacktrackIsEnabled().isSatisfiedBy(context)) { + if (BacktrackScheduler.isOn(context)) { BacktrackScheduler.start(context, false) } diff --git a/app/src/main/java/com/kylecorry/trail_sense/shared/permissions/PermissionUtils.kt b/app/src/main/java/com/kylecorry/trail_sense/shared/permissions/PermissionUtils.kt index 8f390f560..554e2c712 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/shared/permissions/PermissionUtils.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/shared/permissions/PermissionUtils.kt @@ -3,6 +3,7 @@ package com.kylecorry.trail_sense.shared.permissions import android.Manifest import android.content.Context import android.os.Build +import android.util.Log import androidx.fragment.app.Fragment import com.kylecorry.andromeda.alerts.Alerts import com.kylecorry.andromeda.alerts.toast @@ -108,31 +109,14 @@ fun AndromedaFragment.requestCamera(action: (hasPermission: Boolean) -> Unit) { } } -fun Permissions.canRunLocationForegroundService(context: Context, isInBackground: Boolean = false): Boolean { +fun Permissions.canRunLocationForegroundService(context: Context): Boolean { // Older API versions don't need foreground permission if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { return true } - // If it is not in the background, just check if it has location permissions - if (!isInBackground){ - return canGetLocation(context) - } - - // The app is in the background, some restrictions apply - - // This is not a restriction, but appears to be a bug: https://issuetracker.google.com/issues/294408576 - if (!GPS.isAvailable(context)){ - return false - } - - // If background location is granted, we can start the service - if (isBackgroundLocationEnabled(context)){ - return true - } - - // Otherwise, we can only start the service if it is ignoring battery optimizations and has location permission - return isIgnoringBatteryOptimizations(context) && canGetLocation(context) + // The service can be started if it has background permission or if the system says it can get location + return isBackgroundLocationEnabled(context) || canGetLocation(context) } /** diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/battery/infrastructure/BatteryService.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/battery/infrastructure/BatteryService.kt index d887d90b3..279f8254a 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/battery/infrastructure/BatteryService.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/battery/infrastructure/BatteryService.kt @@ -38,7 +38,7 @@ class BatteryService { } // Backtrack - if (BacktrackIsEnabled().isSatisfiedBy(context)) { + if (BacktrackScheduler.isOn(context)) { services.add( RunningService( context.getString(R.string.backtrack), diff --git a/app/src/main/java/com/kylecorry/trail_sense/weather/infrastructure/WeatherUpdateScheduler.kt b/app/src/main/java/com/kylecorry/trail_sense/weather/infrastructure/WeatherUpdateScheduler.kt index 421d07f7d..c127d7850 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/weather/infrastructure/WeatherUpdateScheduler.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/weather/infrastructure/WeatherUpdateScheduler.kt @@ -31,12 +31,8 @@ object WeatherUpdateScheduler { } private fun canStartFromBackground(context: Context): Boolean { - if (Permissions.canRunLocationForegroundService(context, true)) { - return true - } - - // Location permission is not needed, so it is not restricted - return !Permissions.canGetLocation(context) + // TODO: If it was started without permission, it should be able to be restarted without permission - keep track of this + return Permissions.canRunLocationForegroundService(context) } fun stop(context: Context) {