From 2819ec5629b7c750b8bd540b490d7d7950178395 Mon Sep 17 00:00:00 2001 From: geeksville Date: Tue, 16 Jun 2020 15:53:18 -0700 Subject: [PATCH] 0.7.78 We do this painful process because LocationManager.isEnabled is only SDK28 or latet --- app/build.gradle | 4 +- .../geeksville/mesh/service/MeshService.kt | 1 - .../geeksville/mesh/ui/SettingsFragment.kt | 58 +++++++++++++++---- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index cb885cae9..2dd2397f3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,8 +17,8 @@ android { applicationId "com.geeksville.mesh" minSdkVersion 21 // The oldest emulator image I have tried is 22 (though 21 probably works) targetSdkVersion 29 - versionCode 10777 // format is Mmmss (where M is 1+the numeric major number - versionName "0.7.77" + versionCode 10778 // format is Mmmss (where M is 1+the numeric major number + versionName "0.7.78" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index a47b6d70f..5d9683ed5 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -262,7 +262,6 @@ class MeshService : Service(), Logging { locationSettingsResponse.addOnFailureListener { exception -> errormsg("Failed to listen to GPS") if (exception is ResolvableApiException) { - // Exceptions.report(exception) // FIXME, not yet implemented, report failure to mothership exceptionReporter { // Location settings are not satisfied, but this can be fixed // by showing the user a dialog. 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 cb5eedc6d..9b04376a4 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt @@ -14,7 +14,6 @@ import android.companion.CompanionDeviceManager import android.content.* import android.hardware.usb.UsbDevice import android.hardware.usb.UsbManager -import android.location.LocationManager import android.os.Bundle import android.os.ParcelUuid import android.view.LayoutInflater @@ -38,8 +37,13 @@ import com.geeksville.mesh.service.BluetoothInterface import com.geeksville.mesh.service.MeshService import com.geeksville.mesh.service.RadioInterfaceService import com.geeksville.mesh.service.SerialInterface +import com.geeksville.util.Exceptions import com.geeksville.util.anonymize import com.geeksville.util.exceptionReporter +import com.google.android.gms.common.api.ResolvableApiException +import com.google.android.gms.location.LocationRequest +import com.google.android.gms.location.LocationServices +import com.google.android.gms.location.LocationSettingsRequest import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.hoho.android.usbserial.driver.UsbSerialDriver import kotlinx.android.synthetic.main.settings_fragment.* @@ -753,12 +757,46 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { } /** - * Has the user _turned on_ the system setting for current location access. + * If the user has not turned on location access throw up a toast warning */ - private fun isLocationEnabled(): Boolean { - val locManager = - requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager - return locManager.isLocationEnabled + private fun checkLocationEnabled() { + // We do this painful process because LocationManager.isEnabled is only SDK28 or latet + val builder = LocationSettingsRequest.Builder() + builder.setNeedBle(true) + + val request = LocationRequest.create().apply { + priority = LocationRequest.PRIORITY_HIGH_ACCURACY + } + builder.addLocationRequest(request) // Make sure we are granted high accuracy permission + + val locationSettingsResponse = LocationServices.getSettingsClient(requireActivity()) + .checkLocationSettings(builder.build()) + + locationSettingsResponse.addOnSuccessListener { + debug("We have location access") + } + + locationSettingsResponse.addOnFailureListener { exception -> + errormsg("Failed to get location access") + if (exception is ResolvableApiException) { + exceptionReporter { + // Location settings are not satisfied, but this can be fixed + // by showing the user a dialog. + + // Show the dialog by calling startResolutionForResult(), + // and check the result in onActivityResult(). + // exception.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTINGS) + + // For now just punt and show a dialog + Toast.makeText( + requireContext(), + getString(R.string.location_disabled_warning), + Toast.LENGTH_SHORT + ).show() + } + } else + Exceptions.report(exception) + } } override fun onResume() { @@ -776,12 +814,8 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { R.string.error_bluetooth, Toast.LENGTH_SHORT ).show() - } else if (!isLocationEnabled()) { - Toast.makeText( - requireContext(), - getString(R.string.location_disabled_warning), - Toast.LENGTH_SHORT - ).show() + } else { + checkLocationEnabled() } } }