From 8cd8e691176ea1e2ce84123c4be8705d309dc2de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20W=C3=BCrl?= Date: Tue, 17 Nov 2015 23:33:11 +0100 Subject: [PATCH] add permission checks for android M --- app/build.gradle | 4 +- .../blitzortung/android/app/AppService.java | 4 ++ .../org/blitzortung/android/app/Main.java | 54 +++++++++++++++++++ .../android/location/LocationHandler.java | 26 ++++----- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 6238f2e7..d1231592 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,12 +3,12 @@ apply plugin: 'me.tatarka.retrolambda' android { compileSdkVersion "Google Inc.:Google APIs:23" - buildToolsVersion "23.0.1" + buildToolsVersion "23.0.2" defaultConfig { applicationId "org.blitzortung.android.app" minSdkVersion 14 targetSdkVersion 23 - versionCode 140 + versionCode 141 versionName "1.3.6" multiDexEnabled true } diff --git a/app/src/main/java/org/blitzortung/android/app/AppService.java b/app/src/main/java/org/blitzortung/android/app/AppService.java index ee365fc2..1cd5fd4a 100644 --- a/app/src/main/java/org/blitzortung/android/app/AppService.java +++ b/app/src/main/java/org/blitzortung/android/app/AppService.java @@ -197,6 +197,10 @@ public AlertEvent getAlertEvent() { return alertHandler.getAlertEvent(); } + public void updateLocationHandler(SharedPreferences preferences) { + locationHandler.update(preferences); + } + public class DataServiceBinder extends Binder { AppService getService() { Log.d(Main.LOG_TAG, "DataServiceBinder.getService() " + AppService.this); diff --git a/app/src/main/java/org/blitzortung/android/app/Main.java b/app/src/main/java/org/blitzortung/android/app/Main.java index bd6ab6d1..10cfc23a 100644 --- a/app/src/main/java/org/blitzortung/android/app/Main.java +++ b/app/src/main/java/org/blitzortung/android/app/Main.java @@ -1,5 +1,7 @@ package org.blitzortung.android.app; +import android.Manifest; +import android.annotation.TargetApi; import android.app.ActionBar; import android.app.Dialog; import android.app.DialogFragment; @@ -13,6 +15,7 @@ import android.content.pm.PackageManager; import android.graphics.Color; import android.location.Location; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -55,6 +58,7 @@ import org.blitzortung.android.dialogs.AlertDialogColorHandler; import org.blitzortung.android.dialogs.InfoDialog; import org.blitzortung.android.dialogs.QuickSettingsDialog; +import org.blitzortung.android.location.LocationHandler; import org.blitzortung.android.map.OwnMapActivity; import org.blitzortung.android.map.OwnMapView; import org.blitzortung.android.map.overlay.FadeOverlay; @@ -72,6 +76,8 @@ public class Main extends OwnMapActivity implements OnSharedPreferenceChangeList public static final String LOG_TAG = "BO_ANDROID"; + public static final int REQUEST_GPS = 1; + protected StatusComponent statusComponent; private FadeOverlay fadeOverlay; @@ -161,6 +167,10 @@ public void onCreate(Bundle savedInstanceState) { onSharedPreferenceChanged(preferences, PreferenceKey.MAP_TYPE, PreferenceKey.MAP_FADE, PreferenceKey.SHOW_LOCATION, PreferenceKey.ALERT_NOTIFICATION_DISTANCE_LIMIT, PreferenceKey.ALERT_SIGNALING_DISTANCE_LIMIT, PreferenceKey.DO_NOT_SLEEP, PreferenceKey.SHOW_PARTICIPANTS); + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + requestPermissions(preferences); + } + createAndBindToDataService(); if (versionComponent.getState() == VersionComponent.State.FIRST_RUN) { @@ -513,6 +523,50 @@ protected Dialog onCreateDialog(int id) { return dialog; } + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + Log.v(LOG_TAG, "Main.onRequestPermissionsResult() " + requestCode + " - " + permissions + " - " + grantResults); + if (requestCode == REQUEST_GPS) { + // BEGIN_INCLUDE(permission_result) + // Received permission result for camera permission. + Log.i(LOG_TAG, "Received response for Camera permission request."); + + // Check if the only required permission has been granted + if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + // Camera permission has been granted, preview can be displayed + Log.i(LOG_TAG, "CAMERA permission has now been granted. Showing preview."); + } else { + Log.i(LOG_TAG, "CAMERA permission was NOT granted."); + + } + // END_INCLUDE(permission_result) + + } else { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + + if (appService != null) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + appService.updateLocationHandler(preferences); + } + } + + @TargetApi(Build.VERSION_CODES.M) + private void requestPermissions(SharedPreferences sharedPreferences) { + + LocationHandler.Provider newProvider = LocationHandler.Provider.fromString(sharedPreferences.getString(PreferenceKey.LOCATION_MODE.toString(), LocationHandler.Provider.PASSIVE.getType())); + if (newProvider == LocationHandler.Provider.PASSIVE || newProvider == LocationHandler.Provider.GPS) { + if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Main.REQUEST_GPS); + } + } + if (newProvider == LocationHandler.Provider.NETWORK) { + if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Main.REQUEST_GPS); + } + } + } + @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String keyString) { onSharedPreferenceChanged(sharedPreferences, PreferenceKey.fromString(keyString)); diff --git a/app/src/main/java/org/blitzortung/android/location/LocationHandler.java b/app/src/main/java/org/blitzortung/android/location/LocationHandler.java index 1099e52f..5e4d0cc5 100644 --- a/app/src/main/java/org/blitzortung/android/location/LocationHandler.java +++ b/app/src/main/java/org/blitzortung/android/location/LocationHandler.java @@ -1,23 +1,19 @@ package org.blitzortung.android.location; import android.Manifest; -import android.app.Activity; import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.location.GpsStatus; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; -import android.support.v4.content.ContextCompat; import android.util.Log; import android.widget.Toast; import com.annimon.stream.function.Consumer; import org.blitzortung.android.app.Main; -import org.blitzortung.android.app.Preferences; import org.blitzortung.android.app.R; import org.blitzortung.android.app.view.PreferenceKey; import org.blitzortung.android.protocol.ConsumerContainer; @@ -25,14 +21,12 @@ import java.util.HashMap; import java.util.Map; -import static android.support.v4.app.ActivityCompat.requestPermissions; import static android.support.v4.content.PermissionChecker.checkSelfPermission; public class LocationHandler implements SharedPreferences.OnSharedPreferenceChangeListener, android.location.LocationListener, GpsStatus.Listener { private final Context context; private boolean backgroundMode = true; - private int backgroundPeriod; public enum Provider { NETWORK(LocationManager.NETWORK_PROVIDER), @@ -127,6 +121,16 @@ private void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Pref switch (key) { case LOCATION_MODE: Provider newProvider = Provider.fromString(sharedPreferences.getString(key.toString(), Provider.PASSIVE.getType())); + if (newProvider == Provider.PASSIVE || newProvider == Provider.GPS) { + if (checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + newProvider = Provider.MANUAL; + } + } + if (newProvider == Provider.NETWORK) { + if (checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + newProvider = Provider.MANUAL; + } + } if (newProvider != provider) { updateProvider(newProvider, sharedPreferences); } @@ -139,12 +143,6 @@ private void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Pref case LOCATION_LATITUDE: updateManualLatitude(sharedPreferences); break; - - case BACKGROUND_QUERY_PERIOD: - backgroundPeriod = Integer.parseInt(sharedPreferences.getString(key.toString(), "0")); - break; - - } } @@ -281,4 +279,8 @@ public void updateProvider() { enableProvider(provider); } + public void update(SharedPreferences preferences) { + onSharedPreferenceChanged(preferences, PreferenceKey.LOCATION_MODE); + } + }