Skip to content

Commit

Permalink
fix(locationStatus): use LocationManager.isLocationEnabled on Andro…
Browse files Browse the repository at this point in the history
…id 12 onwards
  • Loading branch information
lemnik committed May 10, 2022
1 parent 93a224b commit 04eb1fc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* Small performance improvements to `Bugnag.start`
[#1680](https://github.com/bugsnag/bugsnag-android/pull/1680)

### Bug fixes

* Correctly report `device.locationStatus` on Android 12 onwards using `LocationManager.isLocationEnabled`
[1683](https://github.com/bugsnag/bugsnag-android/pull/1683)

## 5.22.2 (2022-05-04)

### Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.location.LocationManager
import android.net.ConnectivityManager
import android.os.RemoteException
import android.os.storage.StorageManager
Expand Down Expand Up @@ -69,3 +70,7 @@ internal fun Context.getConnectivityManager(): ConnectivityManager? =
@JvmName("getStorageManagerFrom")
internal fun Context.getStorageManager(): StorageManager? =
safeGetSystemService(Context.STORAGE_SERVICE)

@JvmName("getLocationManager")
internal fun Context.getLocationManager(): LocationManager? =
safeGetSystemService(Context.LOCATION_SERVICE)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.os.Build
import android.provider.Settings
import java.io.File
import java.util.Date
import java.util.HashMap
import java.util.Locale
import java.util.concurrent.Callable
import java.util.concurrent.Future
Expand Down Expand Up @@ -163,19 +162,24 @@ internal class DeviceDataCollector(
*/
private fun getLocationStatus(): String? {
try {
val cr = appContext.contentResolver
@Suppress("DEPRECATION") val providersAllowed =
Settings.Secure.getString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
return when {
providersAllowed != null && providersAllowed.isNotEmpty() -> "allowed"
else -> "disallowed"
}
return if (isLocationEnabled()) "allowed" else "disallowed"
} catch (exception: Exception) {
logger.w("Could not get locationStatus")
}
return null
}

private fun isLocationEnabled() = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ->
appContext.getLocationManager()?.isLocationEnabled == true
else -> {
val cr = appContext.contentResolver
@Suppress("DEPRECATION") val providersAllowed =
Settings.Secure.getString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED)
providersAllowed != null && providersAllowed.isNotEmpty()
}
}

/**
* Get the current status of network access, eg "cellular"
*/
Expand Down

0 comments on commit 04eb1fc

Please sign in to comment.