diff options
Diffstat (limited to 'android-app/app')
| -rw-r--r-- | android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt | 58 |
1 files changed, 45 insertions, 13 deletions
diff --git a/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt b/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt index ad9b2c8..9ffc02d 100644 --- a/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt +++ b/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt @@ -50,10 +50,25 @@ class MainActivity : AppCompatActivity() { private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> - if (permissions.all { it.value }) { + val fineLocationGranted = permissions[Manifest.permission.ACCESS_FINE_LOCATION] ?: false + val coarseLocationGranted = permissions[Manifest.permission.ACCESS_COARSE_LOCATION] ?: false + + if (fineLocationGranted || coarseLocationGranted) { + // Foreground location granted, now request background if needed + checkBackgroundPermission() startServices() } else { - Toast.makeText(this, "Location permissions denied", Toast.LENGTH_LONG).show() + Toast.makeText(this, "Location permissions denied. App needs location to function.", Toast.LENGTH_LONG).show() + } + } + + private val requestBackgroundPermissionLauncher = + registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> + if (isGranted) { + Log.d("MainActivity", "Background location permission granted") + } else { + Log.w("MainActivity", "Background location permission denied") + // We can still function, but anchor watch might be less reliable in background } } @@ -62,23 +77,35 @@ class MainActivity : AppCompatActivity() { MapLibre.getInstance(this) setContentView(R.layout.activity_main) - checkPermissions() + checkForegroundPermissions() initializeUI() } - private fun checkPermissions() { - val permissions = mutableListOf( - Manifest.permission.ACCESS_FINE_LOCATION, - Manifest.permission.ACCESS_COARSE_LOCATION - ) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - permissions.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION) - } + private fun checkForegroundPermissions() { + val fineLocationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + val coarseLocationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) - if (permissions.all { ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED }) { + if (fineLocationPermission == PackageManager.PERMISSION_GRANTED || coarseLocationPermission == PackageManager.PERMISSION_GRANTED) { startServices() + checkBackgroundPermission() } else { - requestPermissionLauncher.launch(permissions.toTypedArray()) + requestPermissionLauncher.launch(arrayOf( + Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION + )) + } + } + + private fun checkBackgroundPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val backgroundLocationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) + if (backgroundLocationPermission != PackageManager.PERMISSION_GRANTED) { + // On Android 11+, we SHOULD show a rationale before requesting background location + if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) { + Toast.makeText(this, "Background location is required for the anchor watch to work while the screen is off.", Toast.LENGTH_LONG).show() + } + requestBackgroundPermissionLauncher.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION) + } } } @@ -230,8 +257,13 @@ class MainActivity : AppCompatActivity() { } private fun observeDataSources() { + var firstFix = true lifecycleScope.launch { LocationService.locationFlow.distinctUntilChanged().collect { gpsData -> + if (firstFix) { + mapHandler?.centerOnLocation(gpsData.latitude, gpsData.longitude) + firstFix = false + } mobHandler?.updateMobUI(gpsData.latitude, gpsData.longitude) } } |
