From e2ac781361a1c14e83ea294a00c80ad9ec89844c Mon Sep 17 00:00:00 2001 From: Wolfgang Wiedmeyer Date: Tue, 29 Dec 2015 12:45:56 +0100 Subject: The check for mock location doesn't work on Marshmallow, so add a function that works on Marshmallow and older Android versions code comes from here: https://stackoverflow.com/questions/33003553/how-to-read-selected-mock-location-app-in-android-m-api-23 Signed-off-by: Wolfgang Wiedmeyer --- .../bluetooth/provider/BlueetoothGpsManager.java | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index da76754..6460d35 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -39,6 +39,7 @@ import java.util.concurrent.TimeUnit; import org.broeuschmeul.android.gps.nmea.util.NmeaParser; import org.broeuschmeul.android.gps.sirf.util.SirfUtils; +import android.app.AppOpsManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -287,6 +288,30 @@ public class BlueetoothGpsManager { } /** + * @return true if mock locations are enabled, respectively if BlueGps is set as the mock location provider + */ + public boolean isMockLocationProviderEnabled() + { + boolean isMockLocation = false; + try { + //if marshmallow + if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { + AppOpsManager opsManager = (AppOpsManager) appContext.getSystemService(Context.APP_OPS_SERVICE); + isMockLocation = (opsManager.checkOp(AppOpsManager.OPSTR_MOCK_LOCATION, android.os.Process.myUid(), "org.broeuschmeul.android.gps.bluetooth.provider")== AppOpsManager.MODE_ALLOWED); + } + else { + // in marshmallow this will always return true + isMockLocation = !android.provider.Settings.Secure.getString(appContext.getContentResolver(), "mock_location").equals("0"); + } + } + catch (Exception e) { + return isMockLocation; + } + + return isMockLocation; + } + + /** * Enables the bluetooth GPS Provider. * @return */ @@ -304,7 +329,7 @@ public class BlueetoothGpsManager { // startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); Log.e(LOG_TAG, "Bluetooth is not enabled"); disable(R.string.msg_bluetooth_disabled); - } else if (Settings.Secure.getInt(callingService.getContentResolver(),Settings.Secure.ALLOW_MOCK_LOCATION, 0)==0){ + } else if (!isMockLocationProviderEnabled()){ Log.e(LOG_TAG, "Mock location provider OFF"); disable(R.string.msg_mock_location_disabled); // } else if ( (! locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) -- cgit v1.1