update permissions

This commit is contained in:
andrekir
2022-01-25 15:59:45 -03:00
parent d9e1f17418
commit 8aa2f546b0
3 changed files with 12 additions and 13 deletions

View File

@@ -251,12 +251,9 @@ class MainActivity : AppCompatActivity(), Logging,
val requiredPerms: MutableList<String> = mutableListOf()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
requiredPerms.add(Manifest.permission.BLUETOOTH_SCAN)
requiredPerms.add(Manifest.permission.BLUETOOTH_CONNECT)
} else {
requiredPerms.add(Manifest.permission.ACCESS_FINE_LOCATION)
requiredPerms.add(Manifest.permission.BLUETOOTH)
requiredPerms.add(Manifest.permission.BLUETOOTH_ADMIN)
}
if (getMissingPermissions(requiredPerms).isEmpty()) {
@@ -275,8 +272,6 @@ class MainActivity : AppCompatActivity(), Logging,
*/
private fun getMinimumPermissions(): List<String> {
val perms = mutableListOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WAKE_LOCK
// We only need this for logging to capture files for the simulator - turn off for most users
@@ -284,11 +279,9 @@ class MainActivity : AppCompatActivity(), Logging,
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
perms.add(Manifest.permission.BLUETOOTH_SCAN)
perms.add(Manifest.permission.BLUETOOTH_CONNECT)
} else {
perms.add(Manifest.permission.BLUETOOTH)
perms.add(Manifest.permission.BLUETOOTH_ADMIN)
}
// Some old phones complain about requesting perms they don't understand
@@ -312,16 +305,19 @@ class MainActivity : AppCompatActivity(), Logging,
/**
* @return a localized string warning user about missing permissions. Or null if everything is find
*/
fun getMissingMessage(): String? {
fun getMissingMessage(
missingPerms: List<String> = getMinimumPermissions()
): String? {
val renamedPermissions = mapOf(
// Older versions of android don't know about these permissions - ignore failure to grant
Manifest.permission.ACCESS_COARSE_LOCATION to null,
Manifest.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND to null,
Manifest.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND to null,
Manifest.permission.ACCESS_FINE_LOCATION to getString(R.string.location)
Manifest.permission.ACCESS_FINE_LOCATION to getString(R.string.location),
Manifest.permission.BLUETOOTH_CONNECT to "Bluetooth"
)
val deniedPermissions = getMinimumPermissions().mapNotNull {
val deniedPermissions = missingPerms.mapNotNull {
if (renamedPermissions.containsKey(it))
renamedPermissions[it]
else // No localization found - just show the nasty android string
@@ -369,7 +365,7 @@ class MainActivity : AppCompatActivity(), Logging,
MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.required_permissions))
.setMessage(getMissingMessage())
.setMessage(getMissingMessage(missingPerms))
.setNeutralButton(R.string.cancel) { _, _ ->
warn("User bailed due to permissions")
}
@@ -550,6 +546,9 @@ class MainActivity : AppCompatActivity(), Logging,
handleIntent(intent)
askToRate()
// if (!isInTestLab) - very important - even in test lab we must request permissions because we need location perms for some of our tests to pass
requestPermission()
}
private fun initToolbar() {

View File

@@ -41,7 +41,7 @@ fun Context.getCameraPermissions(): List<String> {
fun Context.hasCameraPermission() = getCameraPermissions().isEmpty()
/**
* Camera permission (or empty if we already have what we need)
* Location permission (or empty if we already have what we need)
*/
fun Context.getLocationPermissions(): List<String> {
val perms = mutableListOf(Manifest.permission.ACCESS_FINE_LOCATION)

View File

@@ -560,7 +560,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
val statusText = binding.scanStatusText
val permissionsWarning = myActivity.getMissingMessage()
when {
(!hasCompanionDeviceApi && permissionsWarning != null) ->
(permissionsWarning != null) ->
statusText.text = permissionsWarning
region == RadioConfigProtos.RegionCode.Unset ->