From 71e8e9ff6f6095f61a4d86c32d4405a119b88d6d Mon Sep 17 00:00:00 2001 From: andrekir Date: Tue, 25 Jan 2022 01:20:31 -0300 Subject: [PATCH] fix provideLocationCheckbox --- .../java/com/geeksville/mesh/MainActivity.kt | 2 +- .../geeksville/mesh/ui/SettingsFragment.kt | 47 +++++++++---------- 2 files changed, 23 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 5d7e432e1..be39e0713 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -814,7 +814,7 @@ class MainActivity : AppCompatActivity(), Logging, model.isConnected.value = oldConnection } // if provideLocation enabled: Start providing location (from phone GPS) to mesh - if (model.provideLocation.value == true && (oldConnection != connected)) + if (model.provideLocation.value == true) service.setupProvideLocation() } } else { diff --git a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt index 8567c653c..f8e4bbba8 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt @@ -655,34 +655,31 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { binding.provideLocationCheckbox.isEnabled = isGooglePlayAvailable(requireContext()) binding.provideLocationCheckbox.setOnCheckedChangeListener { view, isChecked -> - model.provideLocation.value = isChecked + if (view.isPressed && isChecked) { // We want to ignore changes caused by code (as opposed to the user) + // Don't check the box until the system setting changes + view.isChecked = myActivity.hasLocationPermission() && myActivity.hasBackgroundPermission() - if (view.isChecked) { - debug("User changed location tracking to $isChecked") - if (view.isPressed) { // We want to ignore changes caused by code (as opposed to the user) - // Don't check the box until the system setting changes - view.isChecked = myActivity.hasLocationPermission() && myActivity.hasBackgroundPermission() - - if (!myActivity.hasLocationPermission()) // Make sure we have location permission (prerequisite) - myActivity.requestLocationPermission() - else if (!myActivity.hasBackgroundPermission()) - MaterialAlertDialogBuilder(requireContext()) - .setTitle(R.string.background_required) - .setMessage(R.string.why_background_required) - .setNeutralButton(R.string.cancel) { _, _ -> - debug("User denied background permission") - } - .setPositiveButton(getString(R.string.accept)) { _, _ -> - myActivity.requestBackgroundPermission() - } - .show() - - if (view.isChecked) { - checkLocationEnabled(getString(R.string.location_disabled)) - model.meshService?.setupProvideLocation() - } + if (!myActivity.hasLocationPermission()) // Make sure we have location permission (prerequisite) + myActivity.requestLocationPermission() + else if (!myActivity.hasBackgroundPermission()) + MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.background_required) + .setMessage(R.string.why_background_required) + .setNeutralButton(R.string.cancel) { _, _ -> + debug("User denied background permission") + } + .setPositiveButton(getString(R.string.accept)) { _, _ -> + myActivity.requestBackgroundPermission() + } + .show() + if (view.isChecked) { + debug("User changed location tracking to $isChecked") + model.provideLocation.value = isChecked + checkLocationEnabled(getString(R.string.location_disabled)) + model.meshService?.setupProvideLocation() } } else { + model.provideLocation.value = isChecked model.meshService?.stopProvideLocation() } }