From a64bd352520a162b24ba6f6c7c011db32345ea46 Mon Sep 17 00:00:00 2001 From: whbosm Date: Sat, 22 Sep 2018 13:58:47 +0200 Subject: [PATCH] request location permission --- gradle/wrapper/gradle-wrapper.properties | 1 + src/dk/network42/osmfocus/MainActivity.java | 77 ++++++++++++++------- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 933b647..922c2d1 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +distributionSha256Sum=fa4873ae2c7f5e8c02ec6948ba95848cedced6134772a0169718eadcb39e0a2f distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip diff --git a/src/dk/network42/osmfocus/MainActivity.java b/src/dk/network42/osmfocus/MainActivity.java index 0b55671..946ed32 100644 --- a/src/dk/network42/osmfocus/MainActivity.java +++ b/src/dk/network42/osmfocus/MainActivity.java @@ -28,6 +28,7 @@ import android.os.Handler; import android.os.Looper; import android.os.Message; import android.preference.PreferenceManager; +import android.support.v4.app.ActivityCompat; import android.support.v4.app.DialogFragment; import android.support.v4.app.NotificationCompat; import android.support.v4.content.ContextCompat; @@ -51,6 +52,7 @@ public class MainActivity extends Activity implements public static final String userAgent = "OSMfocus"; public static final String PREFS_NAME = "OSMFocusPrefsFile"; static final int PREFERENCE_REQUEST = 9001; + static final int PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 9002; static final int INVALIDATE_VIEW = 1000; static final int POLL_NOTIFICATIONS = 1001; private static final int LOCATION_INTERVAL = 1000; //ms @@ -413,30 +415,7 @@ public class MainActivity extends Activity implements super.onResume(); //locationManager.requestLocationUpdates(mG.mLocProvider, 1000/*ms*/, 1/*meters*/, this); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); - if (mLocationManager != null) { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - == PackageManager.PERMISSION_GRANTED) { - mLocationManager.addGpsStatusListener(this); - } - - Location loc = getMostRecentKnownLocation(); - if (loc != null) - this.onLocationChanged(loc); - - if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - == PackageManager.PERMISSION_GRANTED) { - mLocationManager.requestLocationUpdates( - LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this); - } - } else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) - == PackageManager.PERMISSION_GRANTED) { - mLocationManager.requestLocationUpdates( - LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this); - } - } - } + checkAndRequestLocationUpdates(); if (mG.mUseCompass) { sensorManager.registerListener(this, @@ -488,6 +467,56 @@ public class MainActivity extends Activity implements return loc; } + private void checkAndRequestLocationUpdates() { + if (mLocationManager != null) { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + mLocationManager.addGpsStatusListener(this); + } else { + if (ActivityCompat.shouldShowRequestPermissionRationale(this, + Manifest.permission.ACCESS_FINE_LOCATION)) { + } else { + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, + PERMISSION_REQUEST_ACCESS_FINE_LOCATION); + } + } + + Location loc = getMostRecentKnownLocation(); + if (loc != null) + this.onLocationChanged(loc); + + if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + mLocationManager.requestLocationUpdates( + LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this); + } + } else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + mLocationManager.requestLocationUpdates( + LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this); + } + } + } + } + + @Override + public void onRequestPermissionsResult(int request, String permissions[], int[] granted) { + switch (request) { + case PERMISSION_REQUEST_ACCESS_FINE_LOCATION: { + if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED) { + Toast.makeText(this, "Location permission granted!", Toast.LENGTH_LONG).show(); + checkAndRequestLocationUpdates(); + } else { + Toast.makeText(this, "Location permission denied!", Toast.LENGTH_LONG).show(); + } + return; + } + } + } + @Override public void onLocationChanged(Location location) { mG.mPhyLocation = location;