Skip to content

Commit

Permalink
Merge pull request #4 from mitchhentges/mock-location-permission
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
Mitchell Hentges committed Nov 8, 2015
2 parents af5f7df + aed6147 commit caf722a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ public void run() {
};

private Location current;
private boolean canMock = true;

public CurrentLocation(LocationManager locationManager) {
this.locationManager = locationManager;
}

public void init() {
if (!canMock) {
return;
}

locationManager.addTestProvider(PROVIDER, false, false, false, false, true, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
locationManager.setTestProviderEnabled(PROVIDER, true);
locationManager.setTestProviderStatus(PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
Expand All @@ -42,13 +47,29 @@ public void set(Position position) {
}

public void clean() {
if (!canMock) {
return;
}

locationManager.removeTestProvider(PROVIDER);
publishTask.cancel();
}

private void applyToSystem() {
if (!canMock) {
return;
}

if (current != null) {
locationManager.setTestProviderLocation(PROVIDER, current);
}
}

public void setCanMock(boolean canMock) {
this.canMock = canMock;
}

public boolean canMock() {
return canMock;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package ca.mitchhentges.positionmock;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -12,6 +18,8 @@
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;

import java.io.IOException;

/**
* Created by Mitch
* on 8/7/2015.
Expand All @@ -26,11 +34,47 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);

map = new Map();
currentLocation = new CurrentLocation((LocationManager) getSystemService(LOCATION_SERVICE));
currentLocation.init();
try {
map = new Map();
currentLocation = new CurrentLocation((LocationManager) getSystemService(LOCATION_SERVICE));
currentLocation.init();

((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
} catch (SecurityException e) {
currentLocation.setCanMock(false);
}
}

@Override
protected void onResume() {
super.onResume();
if (currentLocation.canMock()) {
return;
}

((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
Log.i("PositionMock", "Don't have mocking permission, can't run");
AlertDialog. Builder builder = new AlertDialog.Builder(this);
builder
.setTitle("Missing permissions")
.setMessage("(Android 6+) Set as \"mock location app\" in Developer Options\n" +
"(Android 5-) Enable \"Allow Mock Locations\"")
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
finish();
}
})
.create()
.show();
}

@Override
Expand Down

0 comments on commit caf722a

Please sign in to comment.