Fix #37, show a Toast every time the app launches about missing permissions

pull/43/head
geeksville 2020-06-12 11:17:52 -07:00
rodzic dbcc84b5e2
commit a81edbf646
2 zmienionych plików z 36 dodań i 26 usunięć

Wyświetl plik

@ -211,9 +211,7 @@ class MainActivity : AppCompatActivity(), Logging,
) != PackageManager.PERMISSION_GRANTED
}
private fun requestPermission() {
debug("Checking permissions")
private fun getMissingPermissions(): List<String> {
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<out String>,
@ -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) {

Wyświetl plik

@ -35,7 +35,7 @@
<string name="map_not_allowed">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</string>
<string name="permission_missing">A required permission is missing, Meshtastic won\'t be able to work properly. Please enable in application settings.</string>
<string name="permission_missing">A required permission is missing, Meshtastic won\'t be able to work properly. Please enable in Android application settings.</string>
<string name="radio_sleeping">Radio was sleeping, could not change channel</string>
<string name="report_bug">Report Bug</string>
<string name="report_a_bug">Report a bug</string>