[android] #5045 - Adding Async Location Permissions Approval Check
This commit is contained in:
@@ -74,8 +74,7 @@ public class LocationServices implements com.mapzen.android.lost.api.LocationLis
|
||||
* @param enableGPS true if GPS is to be enabled, false if GPS is to be disabled
|
||||
*/
|
||||
public void toggleGPS(boolean enableGPS) {
|
||||
if ((ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) &&
|
||||
(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
|
||||
if (!areLocationPermissionsGranted()) {
|
||||
Log.w(TAG, "Location Permissions Not Granted Yet. Try again after requesting.");
|
||||
return;
|
||||
}
|
||||
@@ -176,4 +175,17 @@ public class LocationServices implements com.mapzen.android.lost.api.LocationLis
|
||||
public boolean removeLocationListener(@NonNull LocationListener locationListener) {
|
||||
return this.locationListeners.remove(locationListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check status of Location Permissions
|
||||
* @return True if granted to the app, False if not
|
||||
*/
|
||||
public boolean areLocationPermissionsGranted() {
|
||||
if ((ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) &&
|
||||
(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
|
||||
Log.w(TAG, "Location Permissions Not Granted Yet. Try again after requesting.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import android.net.wifi.WifiManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.TextUtils;
|
||||
@@ -244,8 +245,33 @@ public class MapboxEventManager {
|
||||
context.startService(new Intent(context, TelemetryService.class));
|
||||
|
||||
// Make sure Ambient Mode is started at a minimum
|
||||
if (LocationServices.getLocationServices(context).isGPSEnabled()) {
|
||||
LocationServices.getLocationServices(context).toggleGPS(false);
|
||||
if (LocationServices.getLocationServices(context).areLocationPermissionsGranted()) {
|
||||
Log.i(TAG, "Permissions are good, see if GPS is enabled and if not then setup Ambient.");
|
||||
if (LocationServices.getLocationServices(context).isGPSEnabled()) {
|
||||
LocationServices.getLocationServices(context).toggleGPS(false);
|
||||
}
|
||||
} else {
|
||||
// Start timer that checks for Permissions
|
||||
Log.i(TAG, "Permissions are not good. Need to do some looping to check on stuff.");
|
||||
|
||||
final Handler permsHandler = new Handler();
|
||||
Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (LocationServices.getLocationServices(context).areLocationPermissionsGranted()) {
|
||||
Log.i(TAG, "Permissions finally granted, so starting Ambient if GPS isn't already enabled");
|
||||
// Start Ambient
|
||||
if (LocationServices.getLocationServices(context).isGPSEnabled()) {
|
||||
LocationServices.getLocationServices(context).toggleGPS(false);
|
||||
}
|
||||
} else {
|
||||
// Restart Handler
|
||||
Log.i(TAG, "Permissions not granted yet... let's try again in 30 seconds");
|
||||
permsHandler.postDelayed(this, 1000 * 30);
|
||||
}
|
||||
}
|
||||
};
|
||||
permsHandler.postDelayed(runnable, 1000 * 10);
|
||||
}
|
||||
|
||||
// Manage Timer Flush
|
||||
|
||||
Reference in New Issue
Block a user