Skip to content

Commit

Permalink
Don't crash when "Allow mock locations" is not enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Vacek committed Feb 27, 2015
1 parent 0c39b80 commit b4a2460
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -61,9 +62,13 @@ protected void _init(Context context) {

protected void _register() {
// if the test provider already exists, android handles this fine
mLocationManager.addTestProvider(locationProviderName, false, false, false,
false, true, true, true, 0, accuracy);
mLocationManager.setTestProviderEnabled(locationProviderName, true);
try {
mLocationManager.addTestProvider(locationProviderName, false, false, false,
false, true, true, true, 0, accuracy);
mLocationManager.setTestProviderEnabled(locationProviderName, true);
} catch (IllegalArgumentException ex) {
Log.e(TAG, "IllegalArgumentException thrown in _register");
}
}

protected void _unregister() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -198,6 +199,10 @@ public void run() {
} catch (SecurityException ex) {
Log.e(TAG, "WakeLock not acquired - permission denied for WakeLock.");
}
if ( mContext.checkCallingOrSelfPermission("android.permission.ACCESS_MOCK_LOCATION")
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Permission denied for android.permission.ACCESS_MOCK_LOCATION");
}
MockLocationProvider.register();
mService.threadHasStartedSuccessfully();

Expand Down Expand Up @@ -231,6 +236,9 @@ public void run() {
}
} catch (IOException e) {
Log.e(TAG, e.toString() );
} catch (SecurityException e) {
Log.e(TAG, e.toString() );
mService.errorHasOccurred(mContext.getString(R.string.err_access_mock_location));
} finally {
if (mWakeLock.isHeld()) {
mWakeLock.release();
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
<string name="invalid_port">That\'s an invalid port number. Port must be a number between 0 and 65535.</string>
<string name="err_address_already_in_use">Binding error. Address/port already in use.</string>
<string name="err_socket_permission_denied">Binding error. Permission denied. (You should use port number >=1024.)</string>
<string name="err_access_mock_location">Mock Location Provider could not be created.
Is \"Allow mock locations\" enabled in \"Developer options\"?</string>

<!-- notes -->
<string name="note_needsreset">Note: This change won\'t take effect until you restart the service.</string>
Expand Down

0 comments on commit b4a2460

Please sign in to comment.