Bug fix: getMyLocation crashes app when permission denied
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta3-20170824-1832" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
+<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta3-20170825-1109" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>cordova-plugin-googlemaps</name>
<js-module name="BaseClass" src="www/BaseClass.js">
<runs />

MODIFY
src/android/plugin/google/maps/CordovaGoogleMaps.java
src/android/plugin/google/maps/CordovaGoogleMaps.java
@@ -330,32 +330,8 @@ public class CordovaGoogleMaps extends CordovaPlugin implements ViewTreeObserver
}
- public void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) {
-
- try {
- Method requestPermission = CordovaInterface.class.getDeclaredMethod(
- "requestPermissions", CordovaPlugin.class, int.class, String[].class);
-
- // If there is no exception, then this is cordova-android 5.0.0+
- requestPermission.invoke(plugin.cordova, plugin, requestCode, permissions);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {
- boolean didPermissionGrant = true;
- for (int r : grantResults) {
- if (r == PackageManager.PERMISSION_DENIED) {
- didPermissionGrant = false;
- break;
- }
- }
- synchronized (semaphore) {
- semaphore.put("result_" + requestCode, didPermissionGrant ? "grant" : "denied");
- }
-
synchronized (CordovaGoogleMaps.semaphore) {
semaphore.notify();
}
@@ -419,32 +395,20 @@ public class CordovaGoogleMaps extends CordovaPlugin implements ViewTreeObserver
final boolean isHigh = isHighLocal;
// Request geolocation permission.
- boolean locationPermission;
- try {
- Method hasPermission = CordovaInterface.class.getDeclaredMethod("hasPermission", String.class);
-
- String permission = "android.permission.ACCESS_COARSE_LOCATION";
- locationPermission = (Boolean) hasPermission.invoke(cordova, permission);
- } catch (Exception e) {
- PluginResult result;
- result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION);
- callbackContext.sendPluginResult(result);
- return;
- }
+ boolean locationPermission = cordova.hasPermission("android.permission.ACCESS_COARSE_LOCATION");
- Log.d(TAG, "---> getMyLocation, hasPermission = " + locationPermission);
if (!locationPermission) {
//_saveArgs = args;
//_saveCallbackContext = callbackContext;
- requestPermissions(CordovaGoogleMaps.this, callbackContext.hashCode(), new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
synchronized (semaphore) {
+ cordova.requestPermissions(this, callbackContext.hashCode(), new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
try {
semaphore.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- locationPermission = "grant".equals(CordovaGoogleMaps.semaphore.remove("result_" + callbackContext.hashCode()));
+ locationPermission = cordova.hasPermission("android.permission.ACCESS_COARSE_LOCATION");
if (!locationPermission) {
callbackContext.error("Geolocation permission request was denied.");

MODIFY
src/android/plugin/google/maps/PluginMap.java
src/android/plugin/google/maps/PluginMap.java
@@ -1568,17 +1568,6 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {
- boolean didPermissionGrant = true;
- for (int r : grantResults) {
- if (r == PackageManager.PERMISSION_DENIED) {
- didPermissionGrant = false;
- break;
- }
- }
- synchronized (CordovaGoogleMaps.semaphore) {
- CordovaGoogleMaps.semaphore.put("result_" + requestCode, didPermissionGrant ? "grant" : "denied");
- }
-
synchronized (CordovaGoogleMaps.semaphore) {
CordovaGoogleMaps.semaphore.notify();
}
@@ -1593,25 +1582,13 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
public void setMyLocationEnabled(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
// Request geolocation permission.
- boolean locationPermission = false;
- try {
- Method hasPermission = CordovaInterface.class.getDeclaredMethod("hasPermission", String.class);
-
- String permission = "android.permission.ACCESS_COARSE_LOCATION";
- locationPermission = (Boolean) hasPermission.invoke(cordova, permission);
- } catch (Exception e) {
- e.printStackTrace();
- PluginResult result;
- result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION);
- callbackContext.sendPluginResult(result);
- return;
- }
+ boolean locationPermission = cordova.hasPermission("android.permission.ACCESS_COARSE_LOCATION");
Log.d(TAG, "---> setMyLocationEnabled, hasPermission = " + locationPermission);
if (!locationPermission) {
//_saveArgs = args;
//_saveCallbackContext = callbackContext;
- mapCtrl.requestPermissions(this, callbackContext.hashCode(), new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
+ cordova.requestPermissions(this, callbackContext.hashCode(), new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
synchronized (CordovaGoogleMaps.semaphore) {
try {
CordovaGoogleMaps.semaphore.wait();
@@ -1619,9 +1596,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
e.printStackTrace();
}
}
- locationPermission = "grant".equals(CordovaGoogleMaps.semaphore.remove("result_" + callbackContext.hashCode()));
-
- if (!locationPermission) {
+ if (!cordova.hasPermission("android.permission.ACCESS_COARSE_LOCATION")) {
callbackContext.error("Geolocation permission request was denied.");
return;
}

Bug fix: getMyLocation crashes app when permission denied (#1672)