From a81edbf646739dae501c737e2e9ab8fc754101d2 Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 12 Jun 2020 11:17:52 -0700 Subject: [PATCH] Fix #37, show a Toast every time the app launches about missing permissions --- .../java/com/geeksville/mesh/MainActivity.kt | 60 +++++++++++-------- app/src/main/res/values/strings.xml | 2 +- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index 5892d9fab..ca75c97fc 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -211,9 +211,7 @@ class MainActivity : AppCompatActivity(), Logging, ) != PackageManager.PERMISSION_GRANTED } - private fun requestPermission() { - debug("Checking permissions") - + private fun getMissingPermissions(): List { val perms = mutableListOf( Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, @@ -234,7 +232,13 @@ class MainActivity : AppCompatActivity(), Logging, perms.add(Manifest.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND) } - val missingPerms = getMissingPermissions(perms) + return getMissingPermissions(perms) + } + + private fun requestPermission() { + debug("Checking permissions") + + val missingPerms = getMissingPermissions() if (missingPerms.isNotEmpty()) { missingPerms.forEach { // Permission is not granted @@ -263,6 +267,31 @@ class MainActivity : AppCompatActivity(), Logging, } + /** + * Remind user he's disabled permissions we need + */ + private fun warnMissingPermissions() { + // Older versions of android don't know about these permissions - ignore failure to grant + val ignoredPermissions = setOf( + Manifest.permission.ACCESS_COARSE_LOCATION, + Manifest.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND, + Manifest.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND + ) + + val deniedPermissions = getMissingPermissions().filter { name -> + !ignoredPermissions.contains(name) + } + + if (deniedPermissions.isNotEmpty()) { + errormsg("Denied permissions: ${deniedPermissions.joinToString(",")}") + Toast.makeText( + this, + getString(R.string.permission_missing), + Toast.LENGTH_LONG + ).show() + } + } + override fun onRequestPermissionsResult( requestCode: Int, permissions: Array, @@ -270,26 +299,6 @@ class MainActivity : AppCompatActivity(), Logging, ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) - // Older versions of android don't know about these permissions - ignore failure to grant - val ignoredPermissions = arrayOf( - Manifest.permission.ACCESS_COARSE_LOCATION, - Manifest.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND, - Manifest.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND - ) - - val deniedPermissions = permissions.filterIndexed { index, name -> - grantResults[index] == PackageManager.PERMISSION_DENIED && - !ignoredPermissions.contains(name) - } - - if (deniedPermissions.isNotEmpty()) { - warn("Denied permissions: ${deniedPermissions.joinToString(",")}") - Toast.makeText( - this, - getString(R.string.permission_missing), - Toast.LENGTH_LONG - ).show() - } updateBluetoothEnabled() } @@ -420,7 +429,6 @@ class MainActivity : AppCompatActivity(), Logging, } - override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) handleIntent(intent) @@ -769,6 +777,8 @@ class MainActivity : AppCompatActivity(), Logging, override fun onStart() { super.onStart() + warnMissingPermissions() + // Ask to start bluetooth if no USB devices are visible val hasUSB = SerialInterface.findDrivers(this).isNotEmpty() if (!isInTestLab && !hasUSB) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7c3063cca..1140ba58e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -35,7 +35,7 @@ You have analytics disabled. Unfortunately our map provider (mapbox) requires analytics to be allowed for their \'free\' plan. So we have turned off the map view.\n\n If you would like to see the map, you\'ll need to turn on analytics in the Settings pane (also, for the time being you might need to force restart the application).\n\n If you are interested in us paying for mapbox (or switching to a different map provider), please post in meshtastic.discourse.group - A required permission is missing, Meshtastic won\'t be able to work properly. Please enable in application settings. + A required permission is missing, Meshtastic won\'t be able to work properly. Please enable in Android application settings. Radio was sleeping, could not change channel Report Bug Report a bug