Skip to content

Commit

Permalink
Use new location permission check when in background
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Dec 11, 2023
1 parent 2f102ee commit 9d485e3
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 57 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context>() {
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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object TrailSenseServiceUtils {
}

startWeatherMonitoring(context, isInBackground)
startBacktrack(context, isInBackground)
startBacktrack(context)
startPedometer(context)
startSunsetAlarm(context)
startAstronomyAlerts(context)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,7 +44,7 @@ class LowPowerMode(val context: Context) {
WeatherUpdateScheduler.start(context)
}

if (BacktrackIsEnabled().isSatisfiedBy(context)) {
if (BacktrackScheduler.isOn(context)) {
BacktrackScheduler.start(context, false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BatteryService {
}

// Backtrack
if (BacktrackIsEnabled().isSatisfiedBy(context)) {
if (BacktrackScheduler.isOn(context)) {
services.add(
RunningService(
context.getString(R.string.backtrack),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9d485e3

Please sign in to comment.