Skip to content

Commit

Permalink
fix missing location permission on lower versions
Browse files Browse the repository at this point in the history
- discovering beacons always needs location permission
  • Loading branch information
ostrya authored and ostrya committed Jan 5, 2020
1 parent 93c535b commit b3c5b6f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
applicationId 'org.ostrya.presencepublisher'
minSdkVersion 14
targetSdkVersion 29
versionCode 25
versionCode 26
versionName '2.0.0'
}
signingConfigs {
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
android:required="false" />

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!-- actually only needed for SDK 28+ -->
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/org/ostrya/presencepublisher/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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 ...");
Expand Down Expand Up @@ -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 ...");
Expand All @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/play/listings/en-US/full-description.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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.

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 .

<b>Permissions</b>

• 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
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
<string name="message_schedule_dialog_text">Die Periode auf unter 15 Minuten zu setzen wird den Batterieverbrauch erhöhen. Fortfahren?</string>
<string name="permission_dialog_title">Standortberechtigung</string>
<string name="permission_dialog_message">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.
</string>
<string name="bluetooth_dialog_title">Bluetooth</string>
<string name="bluetooth_dialog_message">Bitte schalte Bluetooth an, damit diese App nach Beacons suchen kann. Andernfalls
Expand All @@ -20,8 +19,7 @@
<string name="autostart_title">Autostart</string>
<string name="location_dialog_title">Standortdienst</string>
<string name="location_dialog_message">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.
</string>
<string name="tab_connection_title">Verbindung</string>
<string name="tab_schedule_title">Zeitplan</string>
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
<string name="message_schedule_dialog_text">Setting the interval lower than 15 minutes will increase your battery usage. Do you want to continue?</string>
<string name="permission_dialog_title">Location permission</string>
<string name="permission_dialog_message">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.
</string>
<string name="bluetooth_dialog_title">Bluetooth</string>
<string name="bluetooth_dialog_message">Please enable Bluetooth so that this app can scan for beacons. Otherwise,
Expand All @@ -28,8 +27,7 @@
<string name="autostart_title">Autostart</string>
<string name="location_dialog_title">Location service</string>
<string name="location_dialog_message">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.
</string>
<string name="battery_optimization_dialog_title">Battery optimization</string>
<string name="battery_optimization_dialog_message">Please disable battery optimization. Otherwise, Android may doze this app and stop it from sending messages during standby.</string>
Expand Down

0 comments on commit b3c5b6f

Please sign in to comment.