Skip to content

Commit

Permalink
add permission checks for android M
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Nov 17, 2015
1 parent bc6fabf commit 8cd8e69
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/blitzortung/android/app/AppService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions app/src/main/java/org/blitzortung/android/app/Main.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
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;

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),
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;


}
}

Expand Down Expand Up @@ -281,4 +279,8 @@ public void updateProvider() {
enableProvider(provider);
}

public void update(SharedPreferences preferences) {
onSharedPreferenceChanged(preferences, PreferenceKey.LOCATION_MODE);
}

}

0 comments on commit 8cd8e69

Please sign in to comment.