Commit 96aa2fe3 authored by Ricki Hirner's avatar Ricki Hirner 🐑

Don't ask for active location services on Android <9 / location permissions on Android 8.0

parent ca9044cf
Pipeline #207302043 passed with stage
in 7 minutes and 31 seconds
......@@ -30,7 +30,7 @@ object PermissionUtils {
val WIFI_SSID_PERMISSIONS =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1)
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION)
else
arrayOf()
......@@ -38,22 +38,26 @@ object PermissionUtils {
/**
* Checks whether all conditions to access the current WiFi's SSID are met:
*
* 1. location permissions ([WIFI_SSID_PERMISSIONS]) granted
* 2. location enabled
* 1. location permissions ([WIFI_SSID_PERMISSIONS]) granted (Android 8.1+)
* 2. location enabled (Android 9+)
*
* @return *true* if SSID can be obtained; *false* if the SSID will be <unknown> or something like that
*/
fun canAccessWifiSsid(context: Context): Boolean {
// before Android 8, SSIDs are always readable
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
// before Android 8.1, SSIDs are always readable
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O_MR1)
return true
val locationEnabled = ContextCompat.getSystemService(context, LocationManager::class.java)?.let { locationManager ->
LocationManagerCompat.isLocationEnabled(locationManager)
} ?: /* location feature not available on this device */ false
val locationAvailable =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
true // Android <9 doesn't require active location services
else
ContextCompat.getSystemService(context, LocationManager::class.java)?.let { locationManager ->
LocationManagerCompat.isLocationEnabled(locationManager)
} ?: /* location feature not available on this device */ false
return havePermissions(context, WIFI_SSID_PERMISSIONS) &&
locationEnabled
locationAvailable
}
/**
......
......@@ -129,23 +129,28 @@ class WifiPermissionsActivity: AppCompatActivity() {
}
fun checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8.1+: location permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
val location = ContextCompat.checkSelfPermission(getApplication(), PERMISSION_LOCATION) == PackageManager.PERMISSION_GRANTED
haveLocation.value = location
needLocation.value = location
}
// Android 9+: location service
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getSystemService(getApplication(), LocationManager::class.java)?.let { locationManager ->
val locationEnabled = LocationManagerCompat.isLocationEnabled(locationManager)
isLocationEnabled.value = locationEnabled
needLocationEnabled.value = locationEnabled
}
}
// Android 10+: background location permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val backgroundLocation = ContextCompat.checkSelfPermission(getApplication(), Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED
haveBackgroundLocation.value = backgroundLocation
needBackgroundLocation.value = backgroundLocation
}
getSystemService(getApplication(), LocationManager::class.java)?.let { locationManager ->
val locationEnabled = LocationManagerCompat.isLocationEnabled(locationManager)
isLocationEnabled.value = locationEnabled
needLocationEnabled.value = locationEnabled
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment