From b3c5b6f1b72f4f3b71cddf34f2c7b13e10665b43 Mon Sep 17 00:00:00 2001 From: ostrya Date: Sun, 5 Jan 2020 11:43:37 +0100 Subject: [PATCH] fix missing location permission on lower versions - discovering beacons always needs location permission --- README.md | 3 +-- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 4 +--- .../org/ostrya/presencepublisher/MainActivity.java | 14 ++++++++++++-- .../main/play/listings/en-US/full-description.txt | 9 +++++---- app/src/main/res/values-de/strings.xml | 6 ++---- app/src/main/res/values/strings.xml | 6 ++---- 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 4854957..1fc7169 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,7 @@ Make sure your PKCS#12 keystore file has the `.pfx` extension, otherwise Android ## Permissions * ACCESS_BACKGROUND_LOCATION: on Android 10+, necessary to retrieve SSID of connected WiFi while running in background -* ACCESS_FINE_LOCATION: on Android 9+, necessary to retrieve SSID of connected WiFi (you do not need to grant - the permission in Android 6.0 - 8.1 for the app to work) +* ACCESS_FINE_LOCATION: necessary to discover beacons; on Android 9+, necessary to retrieve SSID of connected WiFi * ACCESS_NETWORK_STATE: necessary to register network change listener * ACCESS_WIFI_STATE: necessary to retrieve SSID of connected WiFi * BLUETOOTH: necessary to communicate with beacons diff --git a/app/build.gradle b/app/build.gradle index 4504e2d..01ceccf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,7 +38,7 @@ android { applicationId 'org.ostrya.presencepublisher' minSdkVersion 14 targetSdkVersion 29 - versionCode 25 + versionCode 26 versionName '2.0.0' } signingConfigs { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9687ec6..936c5eb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,9 +12,7 @@ android:required="false" /> - - - + diff --git a/app/src/main/java/org/ostrya/presencepublisher/MainActivity.java b/app/src/main/java/org/ostrya/presencepublisher/MainActivity.java index 1927738..a395ea5 100644 --- a/app/src/main/java/org/ostrya/presencepublisher/MainActivity.java +++ b/app/src/main/java/org/ostrya/presencepublisher/MainActivity.java @@ -55,6 +55,7 @@ public class MainActivity extends FragmentActivity { private static final String TAG = "MainActivity"; private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceListener = this::onSharedPreferenceChanged; + private boolean needsLocationService; private SharedPreferences sharedPreferences; @Override @@ -67,6 +68,9 @@ public void onCreate(Bundle savedInstanceState) { ViewPager viewPager = findViewById(R.id.pager); viewPager.setAdapter(mainPagerAdapter); + needsLocationService = ((Application) getApplication()).supportsBeacons() + // for WiFi name resolution + || Build.VERSION.SDK_INT >= Build.VERSION_CODES.P; sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceListener); @@ -149,7 +153,7 @@ private void onSharedPreferenceChanged(SharedPreferences preferences, String key } private void checkLocationPermissionAndAccessAndBluetoothAndBatteryOptimizationAndStartWorker() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P + if (needsLocationService && ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { HyperLog.d(TAG, "Location permission not yet granted, asking user ..."); @@ -177,7 +181,9 @@ private void checkLocationPermissionAndAccessAndBluetoothAndBatteryOptimizationA private void checkLocationAccessAndBluetoothAndBatteryOptimizationAndStartWorker() { LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P + if (needsLocationService + && ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED && (locationManager == null || !(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)))) { HyperLog.d(TAG, "Location service not yet enabled, asking user ..."); @@ -196,6 +202,10 @@ private void checkLocationAccessAndBluetoothAndBatteryOptimizationAndStartWorker private void checkBluetoothAndBatteryOptimizationAndStartWorker() { if (((Application) getApplication()).supportsBeacons() + // make linter happy + && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 + && ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED && !sharedPreferences.getStringSet(BEACON_LIST, Collections.emptySet()).isEmpty()) { BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager == null) { diff --git a/app/src/main/play/listings/en-US/full-description.txt b/app/src/main/play/listings/en-US/full-description.txt index 88d25f0..fa57c26 100644 --- a/app/src/main/play/listings/en-US/full-description.txt +++ b/app/src/main/play/listings/en-US/full-description.txt @@ -1,4 +1,4 @@ -This app regularly publishes to a configurable MQTT topic whenever +This free open-source app regularly publishes to a configurable MQTT topic whenever connected to a given WiFi network or in proximity to a Bluetooth beacon. It can be used to integrate the presence of your phone in home automation. @@ -6,14 +6,15 @@ The app uses the built-in Android alarm manager, so notifications are sent even if the phone is in stand-by. In addition to regularly scheduled checks, the app also reacts to changes in the network connection. -In addition to anonymous and username / password authentication, client certificate -authentication is also supported. For more details, please have a look at +The MQTT client supports anonymous and username / password authentication +as well ass client certificate authentication. For more details on how to +configure certificate-based authentication, please have a look at https://github.com/ostrya/PresencePublisher/blob/master/README.md . Permissions • ACCESS_BACKGROUND_LOCATION: on Android 10+, necessary to retrieve SSID of connected WiFi while running in background -• ACCESS_FINE_LOCATION: on Android 9+, necessary to retrieve SSID of connected WiFi (you do not need to grant the permission in Android 6.0 - 8.1 for the app to work) +• ACCESS_FINE_LOCATION: necessary to discover beacons; on Android 9+, necessary to retrieve SSID of connected WiFi • ACCESS_NETWORK_STATE: necessary to register network change listener • ACCESS_WIFI_STATE: necessary to retrieve SSID of connected WiFi • BLUETOOTH: necessary to communicate with beacons diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 6f82bed..61e16c4 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -10,8 +10,7 @@ Die Periode auf unter 15 Minuten zu setzen wird den Batterieverbrauch erhöhen. Fortfahren? Standortberechtigung Bitte erlaube den dauerhaften Zugriff auf genaue Position. Andernfalls kann diese App den - Namen des aktuell verbundenen WLANs nicht bestimmen. Du kannst das Setzen der Berechtigung überspringen, aber - in diesem Fall wird die App keine Benachrichtigungen senden. + Namen des aktuell verbundenen WLANs nicht bestimmen und Bluetooth-Beacons nicht finden. Bluetooth Bitte schalte Bluetooth an, damit diese App nach Beacons suchen kann. Andernfalls @@ -20,8 +19,7 @@ Autostart Standortdienst Bitte schalte den Standortdienst (zumindest Energiesparmodus) dauerhaft an. - Andernfalls kann diese App den Namen des aktuell verbundenen WLANs nicht bestimmen. Du kannst das Einschalten - überspringen, aber in diesem Fall wird die App keine Benachrichtigungen senden. + Andernfalls kann diese App den Namen des aktuell verbundenen WLANs nicht bestimmen und Bluetooth-Beacons nicht finden. Verbindung Zeitplan diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 151efd9..6c03b7b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,8 +18,7 @@ Setting the interval lower than 15 minutes will increase your battery usage. Do you want to continue? Location permission Please allow access to fine location all the time. Otherwise, this app will not be able to - retrieve the currently connected WiFi name. You can skip granting the permission, but then the app will not send - any notifications. + retrieve the currently connected WiFi name or find Bluetooth beacons. Bluetooth Please enable Bluetooth so that this app can scan for beacons. Otherwise, @@ -28,8 +27,7 @@ Autostart Location service Please enable location services (at least network-based) and keep them enabled. - Otherwise, this app will not be able to retrieve the currently connected WiFi name. You can skip enabling it, but - then the app will not send any notifications. + Otherwise, this app will not be able to retrieve the currently connected WiFi name or find Bluetooth beacons. Battery optimization Please disable battery optimization. Otherwise, Android may doze this app and stop it from sending messages during standby.