Skip to content

Commit 0f3010e

Browse files
committed
fix android q background location access
1 parent bc5f233 commit 0f3010e

File tree

1 file changed

+12
-6
lines changed
  • app/src/main/java/org/blitzortung/android/app

1 file changed

+12
-6
lines changed

app/src/main/java/org/blitzortung/android/app/Main.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.blitzortung.android.app
2020

2121
import android.Manifest
22+
import android.Manifest.permission.*
2223
import android.annotation.TargetApi
2324
import android.content.Context
2425
import android.content.DialogInterface
@@ -28,6 +29,7 @@ import android.content.pm.ApplicationInfo
2829
import android.content.pm.PackageManager
2930
import android.graphics.Color
3031
import android.location.LocationManager
32+
import android.location.LocationManager.*
3133
import android.os.Build
3234
import android.os.Bundle
3335
import android.os.PowerManager
@@ -507,11 +509,15 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
507509

508510
@TargetApi(Build.VERSION_CODES.M)
509511
private fun requestLocationPermissions(sharedPreferences: SharedPreferences) {
510-
val locationProviderName = sharedPreferences.get(PreferenceKey.LOCATION_MODE, LocationManager.PASSIVE_PROVIDER)
511-
val permission = when (locationProviderName) {
512-
LocationManager.PASSIVE_PROVIDER, LocationManager.GPS_PROVIDER -> Manifest.permission.ACCESS_FINE_LOCATION
513-
LocationManager.NETWORK_PROVIDER -> Manifest.permission.ACCESS_COARSE_LOCATION
514-
else -> null
512+
val locationProviderName = sharedPreferences.get(PreferenceKey.LOCATION_MODE, PASSIVE_PROVIDER)
513+
val permission = if (isAtLeast(Build.VERSION_CODES.Q)) {
514+
ACCESS_BACKGROUND_LOCATION
515+
} else {
516+
when (locationProviderName) {
517+
PASSIVE_PROVIDER, GPS_PROVIDER -> ACCESS_FINE_LOCATION
518+
NETWORK_PROVIDER -> ACCESS_COARSE_LOCATION
519+
else -> null
520+
}
515521
}
516522

517523
if (permission is String && checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
@@ -583,7 +589,7 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
583589
}
584590

585591
private enum class LocationProviderRelation(val providerName: String) {
586-
GPS(LocationManager.GPS_PROVIDER), PASSIVE(LocationManager.PASSIVE_PROVIDER), NETWORK(LocationManager.NETWORK_PROVIDER);
592+
GPS(GPS_PROVIDER), PASSIVE(PASSIVE_PROVIDER), NETWORK(NETWORK_PROVIDER);
587593

588594
companion object {
589595
val byProviderName: Map<String, LocationProviderRelation> = values().groupBy { it.providerName }.mapValues { it.value.first() }

0 commit comments

Comments
 (0)