add location only logic

pull/323/head
andrekir 2021-12-15 09:04:44 -03:00
rodzic bb40672401
commit e21f3fdf93
3 zmienionych plików z 27 dodań i 4 usunięć

Wyświetl plik

@ -41,6 +41,7 @@ import com.geeksville.android.GeeksvilleApplication
import com.geeksville.android.Logging
import com.geeksville.android.ServiceClient
import com.geeksville.concurrent.handledLaunch
import com.geeksville.mesh.android.getLocationPermissions
import com.geeksville.mesh.android.getBackgroundPermissions
import com.geeksville.mesh.android.getCameraPermissions
import com.geeksville.mesh.android.getMissingPermissions
@ -251,6 +252,9 @@ class MainActivity : AppCompatActivity(), Logging,
/** Ask the user to grant camera permission */
fun requestCameraPermission() = requestPermission(getCameraPermissions(), false)
/** Ask the user to grant foreground location permission */
fun requestLocationPermission() = requestPermission(getLocationPermissions(), false)
/** Ask the user to grant background location permission */
fun requestBackgroundPermission() = requestPermission(getBackgroundPermissions(), false)

Wyświetl plik

@ -40,6 +40,18 @@ fun Context.getCameraPermissions(): List<String> {
/** @return true if the user already has camera permission */
fun Context.hasCameraPermission() = getCameraPermissions().isEmpty()
/**
* Camera permission (or empty if we already have what we need)
*/
fun Context.getLocationPermissions(): List<String> {
val perms = mutableListOf(Manifest.permission.ACCESS_FINE_LOCATION)
return getMissingPermissions(perms)
}
/** @return true if the user already has camera permission */
fun Context.hasLocationPermission() = getLocationPermissions().isEmpty()
/**
* A list of missing background location permissions (or empty if we already have what we need)
*/

Wyświetl plik

@ -35,6 +35,7 @@ import com.geeksville.mesh.MainActivity
import com.geeksville.mesh.R
import com.geeksville.mesh.RadioConfigProtos
import com.geeksville.mesh.android.bluetoothManager
import com.geeksville.mesh.android.hasLocationPermission
import com.geeksville.mesh.android.hasBackgroundPermission
import com.geeksville.mesh.android.usbManager
import com.geeksville.mesh.databinding.SettingsFragmentBinding
@ -656,14 +657,20 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
if (view.isPressed && isChecked) { // We want to ignore changes caused by code (as opposed to the user)
debug("User changed location tracking to $isChecked")
view.isChecked =
myActivity.hasBackgroundPermission() // Don't check the box until the system setting changes
if (!view.isChecked)
val hasLocationPermission = myActivity.hasLocationPermission()
val hasBackgroundPermission = myActivity.hasBackgroundPermission()
// Don't check the box until the system setting changes
view.isChecked = hasLocationPermission && hasBackgroundPermission
if (!hasLocationPermission) // Make sure we have location permission (prerequisite)
myActivity.requestLocationPermission()
if (hasLocationPermission && !hasBackgroundPermission)
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.background_required)
.setMessage(R.string.why_background_required)
.setNeutralButton(R.string.cancel) { _, _ ->
debug("Decided not to report a bug")
debug("User denied background permission")
}
.setPositiveButton(getString(R.string.accept)) { _, _ ->
myActivity.requestBackgroundPermission()