Skip to content

Commit

Permalink
better dialog for background location
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksville committed Jun 23, 2021
1 parent c5fc6bc commit 74f7281
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 6 additions & 5 deletions app/src/main/java/com/geeksville/mesh/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class MainActivity : AppCompatActivity(), Logging,


/** Ask the user to grant background location permission */
fun requestBackgroundPermission() = requestPermission(getBackgroundPermissions())
fun requestBackgroundPermission() = requestPermission(getBackgroundPermissions(), false)

/**
* @return a localized string warning user about missing permissions. Or null if everything is find
Expand Down Expand Up @@ -280,10 +280,11 @@ class MainActivity : AppCompatActivity(), Logging,
}

/** Possibly prompt user to grant permissions
* @param shouldShowDialog usually true, but in cases where we've already shown a dialog elsewhere we skip it.
*
* @return true if we already have the needed permissions
*/
private fun requestPermission(missingPerms: List<String> = getMinimumPermissions()): Boolean =
private fun requestPermission(missingPerms: List<String> = getMinimumPermissions(), shouldShowDialog: Boolean = true): Boolean =
if (missingPerms.isNotEmpty()) {
val shouldShow = missingPerms.filter {
ActivityCompat.shouldShowRequestPermissionRationale(this, it)
Expand All @@ -299,7 +300,7 @@ class MainActivity : AppCompatActivity(), Logging,
)
}

if (shouldShow.isNotEmpty()) {
if (shouldShow.isNotEmpty() && shouldShowDialog) {
// DID_REQUEST_PERM is an
// app-defined int constant. The callback method gets the
// result of the request.
Expand All @@ -308,10 +309,10 @@ class MainActivity : AppCompatActivity(), Logging,
MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.required_permissions))
.setMessage(getMissingMessage())
.setNeutralButton("Cancel (no radio access)") { _, _ ->
.setNeutralButton(R.string.cancel_no_radio) { _, _ ->
error("User bailed due to permissions")
}
.setPositiveButton("Allow (will show dialog)") { _, _ ->
.setPositiveButton(R.string.allow_will_show) { _, _ ->
doRequest()
}
.show()
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@
<string name="choose_theme_title">Choose theme</string>
<string name="background_required">Background location access required</string>
<string name="show_system_settings">Show system settings</string>
<string name="why_background_required">In order to enable this feature, you must grant this application "allow location access all the time" permission. This allows meshtastic to read your location while the application is in the background, so that it can send your location to other members of your mesh.</string>
<string name="why_background_required">In order to enable this feature, you must grant "allow location access all the time" permission.\n\nThis allows meshtastic to read your location while the application is in the background, so that it can send your location to other members of your mesh.</string>
<string name="required_permissions">Required permissions</string>
<string name="location">location</string>
<string name="cancel_no_radio">Cancel (no radio access)</string>
<string name="allow_will_show">Allow (will show dialog)</string>
</resources>

0 comments on commit 74f7281

Please sign in to comment.