(Android) update: use PermissionChecker instead of cordova.hasPermission()
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.2.4-beta-20180207-1808" 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.2.4-beta-20180208-1503" 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 />
@@ -110,10 +110,6 @@ The native map view is faster, use less network usage, display 3D views, and eve
<platform name="android">
<preference name="API_KEY_FOR_ANDROID" />
- <js-module name="CordovaGoogleMaps" src="www/googlemaps-cdv-plugin.js">
- <clobbers target="plugin.google.maps" />
- </js-module>
-
<info>
Official document
https://github.com/mapsplugin/cordova-plugin-googlemaps-doc/blob/master/v2.0.0/README.md
@@ -204,16 +200,7 @@ The native map view is faster, use less network usage, display 3D views, and eve
<framework src="src/android/frameworks/tbxml-android.gradle" custom="true" type="gradleReference" />
<resource-file src="src/android/frameworks/tbxml-android.aar" target="libs/tbxml-android.aar" />
<resource-file src="src/android/frameworks/tbxml-android.aar" target="app/src/libs/tbxml-android.aar" />
-<!--
- <source-file src="src/android/tbxml-android/src/za/co/twyst/tbxml/TBXML.java" target-dir="src/za/co/twyst/tbxml" />
- <source-file src="src/android/tbxml-android/libs/arm64-v8a/libtbxml.so" target-dir="libs/arm64-v8a/" />
- <source-file src="src/android/tbxml-android/libs/armeabi/libtbxml.so" target-dir="libs/armeabi/" />
- <source-file src="src/android/tbxml-android/libs/armeabi-v7a/libtbxml.so" target-dir="libs/armeabi-v7a/" />
- <source-file src="src/android/tbxml-android/libs/mips/libtbxml.so" target-dir="libs/mips/" />
- <source-file src="src/android/tbxml-android/libs/mips64/libtbxml.so" target-dir="libs/mips64/" />
- <source-file src="src/android/tbxml-android/libs/x86/libtbxml.so" target-dir="libs/x86/" />
- <source-file src="src/android/tbxml-android/libs/x86_64/libtbxml.so" target-dir="libs/x86_64/" />
--->
+
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<meta-data
android:name="com.google.android.geo.API_KEY"
@@ -248,13 +235,18 @@ The native map view is faster, use less network usage, display 3D views, and eve
<preference name="PLAY_SERVICES_VERSION" default="11.8.0" />
<framework src="com.google.android.gms:play-services-maps:$PLAY_SERVICES_VERSION" />
<framework src="com.google.android.gms:play-services-location:$PLAY_SERVICES_VERSION" />
+
+ <!--
+ Minimum required version of Android Support library v4 for this plugin.
+ https://developers.google.com/android/guides/releases
+ -->
+ <preference name="ANDROID_SUPPORT_V4_VERSION" default="24.1.0" />
+ <framework src="com.android.support:support-core-utils:$ANDROID_SUPPORT_V4_VERSION" />
+
</platform>
<!-- ios -->
<platform name="ios">
- <js-module name="CordovaGoogleMaps" src="www/googlemaps-cdv-plugin.js">
- <clobbers target="plugin.google.maps" />
- </js-module>
<preference name="API_KEY_FOR_IOS" />
<preference name="LOCATION_WHEN_IN_USE_DESCRIPTION" default="Show your location on the map"/>

MODIFY
src/android/plugin/google/maps/PluginLocationService.java
src/android/plugin/google/maps/PluginLocationService.java
package plugin.google.maps;
+import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
@@ -9,6 +10,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
+import android.support.v4.content.PermissionChecker;
import android.util.Log;
import android.widget.Toast;
@@ -82,11 +84,57 @@ public class PluginLocationService extends CordovaPlugin {
@SuppressWarnings("unused")
public void getMyLocation(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
synchronized (semaphore) {
+
+
+
+ LocationManager locationManager = (LocationManager) this.activity.getSystemService(Context.LOCATION_SERVICE);
+ List<String> providers = locationManager.getAllProviders();
+ int availableProviders = 0;
+ //if (mPluginLayout != null && mPluginLayout.isDebug) {
+ Log.d(TAG, "---debug at getMyLocation(available providers)--");
+ //}
+ Iterator<String> iterator = providers.iterator();
+ String provider;
+ boolean isAvailable;
+ while(iterator.hasNext()) {
+ provider = iterator.next();
+ if ("passive".equals(provider)) {
+ continue;
+ }
+ isAvailable = locationManager.isProviderEnabled(provider);
+ if (isAvailable) {
+ availableProviders++;
+ }
+ //if (mPluginLayout != null && mPluginLayout.isDebug) {
+ Log.d(TAG, " " + provider + " = " + (isAvailable ? "" : "not ") + "available");
+ //}
+ }
+ if (availableProviders == 0) {
+ JSONObject result = new JSONObject();
+ try {
+ result.put("status", false);
+ result.put("error_code", "not_available");
+ result.put("error_message", "Since this device does not have any location provider, this app can not detect your location.");
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ callbackContext.error(result);
+ return;
+ }
+
+
+
+
+
JSONObject params = args.getJSONObject(0);
boolean requestHighAccuracy = false;
if (params.has("enableHighAccuracy")) {
requestHighAccuracy = params.getBoolean("enableHighAccuracy");
}
+ if (requestHighAccuracy && !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
+ callbackContext.error("Location manager is disabled.");
+ return;
+ }
// enableHighAccuracy = true -> PRIORITY_HIGH_ACCURACY
// enableHighAccuracy = false -> PRIORITY_BALANCED_POWER_ACCURACY
@@ -102,20 +150,22 @@ public class PluginLocationService extends CordovaPlugin {
}
// Request geolocation permission.
- boolean locationPermission = cordova.hasPermission("android.permission.ACCESS_COARSE_LOCATION");
+ boolean locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
if (!locationPermission) {
//_saveArgs = args;
//_saveCallbackContext = callbackContext;
synchronized (semaphore) {
- cordova.requestPermissions(this, callbackContext.hashCode(), new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
+ cordova.requestPermissions(this, callbackContext.hashCode(), new String[]{
+ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
+ });
try {
semaphore.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- locationPermission = cordova.hasPermission("android.permission.ACCESS_COARSE_LOCATION");
+ locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
if (!locationPermission) {
@@ -462,6 +512,9 @@ public class PluginLocationService extends CordovaPlugin {
boolean isAvailable;
while(iterator.hasNext()) {
provider = iterator.next();
+ if ("passive".equals(provider)) {
+ continue;
+ }
isAvailable = locationManager.isProviderEnabled(provider);
if (isAvailable) {
availableProviders++;
@@ -546,9 +599,4 @@ public class PluginLocationService extends CordovaPlugin {
}
}
-// protected void sendNoResult(CallbackContext callbackContext) {
-// PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
-// pluginResult.setKeepCallback(true);
-// callbackContext.sendPluginResult(pluginResult);
-// }
}

MODIFY
src/android/plugin/google/maps/PluginMap.java
src/android/plugin/google/maps/PluginMap.java
package plugin.google.maps;
+import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
@@ -16,6 +17,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
+import android.support.v4.content.PermissionChecker;
import android.util.Base64;
import android.util.Log;
import android.view.Gravity;
@@ -1697,13 +1699,16 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
public void setMyLocationEnabled(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
// Request geolocation permission.
- boolean locationPermission = cordova.hasPermission("android.permission.ACCESS_COARSE_LOCATION");
+
+ boolean locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
Log.d(TAG, "---> setMyLocationEnabled, hasPermission = " + locationPermission);
if (!locationPermission) {
//_saveArgs = args;
//_saveCallbackContext = callbackContext;
- cordova.requestPermissions(this, callbackContext.hashCode(), new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
+ cordova.requestPermissions(this, callbackContext.hashCode(), new String[]{
+ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
+ });
synchronized (PluginLocationService.semaphore) {
try {
PluginLocationService.semaphore.wait();
@@ -1711,7 +1716,9 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
e.printStackTrace();
}
}
- if (!cordova.hasPermission("android.permission.ACCESS_COARSE_LOCATION")) {
+ locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
+
+ if (!locationPermission) {
callbackContext.error("Geolocation permission request was denied.");
return;
}

(Android) update: use PermissionChecker instead of cordova.hasPermission() #2063