Skip to content

Commit b620eb3

Browse files
committed
Support check if mock is enable for new devices
1 parent cb2e6b3 commit b620eb3

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

README.md.txt renamed to README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ This app allows you to change mock location from terminal using adb in order to
22

33
Usage:
44
```batch
5-
adb [-s <specific device>] shell am broadcast -a send.mock [-e lat<latitude>] [-e lon <longitude>]
5+
adb [-s <specific device>] shell am broadcast -a send.mockgi [-e lat<latitude>] [-e lon <longitude>]
66
[-e alt <altitude>] [-e accurate <accurate>]
77
```
88

app/src/main/java/amotz/example/com/mocklocationfordeveloper/MainActivity.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package amotz.example.com.mocklocationfordeveloper;
22

3+
import android.app.AppOpsManager;
34
import android.content.Context;
45
import android.content.DialogInterface;
56
import android.content.Intent;
67
import android.content.pm.PackageManager;
78
import android.location.Location;
9+
import android.os.Build;
810
import android.provider.Settings;
911
import android.support.annotation.NonNull;
1012
import android.support.annotation.Nullable;
@@ -44,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
4446
super.onCreate(savedInstanceState);
4547
setContentView(R.layout.activity_main);
4648

47-
if (!isMockSettingsON(this)) {
49+
if (!isMockSettingsON()) {
4850
AlertDialog.Builder builder = new AlertDialog.Builder(this);
4951
builder.setMessage("In order to use this app you must enable mock location do you want to enable it now?").setTitle("Mock location is not enable");
5052
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@@ -86,12 +88,28 @@ protected void onStop() {
8688
super.onStop();
8789
}
8890

89-
private boolean isMockSettingsON(Context context) {
90-
if (android.os.Build.VERSION.SDK_INT < 18) {
91-
String st = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION);
92-
return st.equals("1");
91+
private boolean isMockSettingsON() {
92+
boolean isMockLocation = false;
93+
try
94+
{
95+
//if marshmallow
96+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
97+
{
98+
AppOpsManager opsManager = (AppOpsManager) this.getSystemService(Context.APP_OPS_SERVICE);
99+
isMockLocation = (opsManager.checkOp(AppOpsManager.OPSTR_MOCK_LOCATION, android.os.Process.myUid(), BuildConfig.APPLICATION_ID)== AppOpsManager.MODE_ALLOWED);
100+
}
101+
else
102+
{
103+
// in marshmallow this will always return true
104+
isMockLocation = !android.provider.Settings.Secure.getString(this.getContentResolver(), "mock_location").equals("0");
105+
}
93106
}
94-
return true;
107+
catch (Exception e)
108+
{
109+
return isMockLocation;
110+
}
111+
112+
return isMockLocation;
95113

96114
}
97115

0 commit comments

Comments
 (0)