Thanks to Marshmallow, we now have a bunch of new code to handle the permissions. This is a major step backwards.
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/commit/e713e7ab Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/tree/e713e7ab Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/diff/e713e7ab Branch: refs/heads/master Commit: e713e7abe980022e67f4ed1e967a00e74793dcd6 Parents: bf8a373 Author: Joe Bowser <[email protected]> Authored: Fri Sep 18 13:59:00 2015 -0700 Committer: Joe Bowser <[email protected]> Committed: Wed Oct 28 12:00:26 2015 -0700 ---------------------------------------------------------------------- plugin.xml | 13 +++++ src/android/Geolocation.java | 108 ++++++++++++++++++++++++++++++++++++++ www/android/geolocation.js | 58 ++++++++++++++++++++ 3 files changed, 179 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/blob/e713e7ab/plugin.xml ---------------------------------------------------------------------- diff --git a/plugin.xml b/plugin.xml index 60065a9..f73b424 100644 --- a/plugin.xml +++ b/plugin.xml @@ -39,6 +39,19 @@ xmlns:android="http://schemas.android.com/apk/res/android" <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </config-file> + <config-file target="res/xml/config.xml" parent="/*"> + <feature name="Geolocation"> + <param name="android-package" value="org.apache.cordova.geolocation.Geolocation" /> + </feature> + </config-file> + + <source-file src="src/android/Geolocation.java" target-dir="src/org/apache/cordova/geolocation/" /> + + <js-module src="www/android/geolocation.js" name="geolocation"> + <clobbers target="navigator.geolocation" /> + </js-module> + + </platform> <!-- amazon-fireos --> http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/blob/e713e7ab/src/android/Geolocation.java ---------------------------------------------------------------------- diff --git a/src/android/Geolocation.java b/src/android/Geolocation.java new file mode 100644 index 0000000..3eb9974 --- /dev/null +++ b/src/android/Geolocation.java @@ -0,0 +1,108 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + + +package org.apache.cordova.geolocation; + +import android.content.pm.PackageManager; +import android.Manifest; +import android.os.Build; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaArgs; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; +import org.apache.cordova.LOG; +import org.json.JSONArray; +import org.json.JSONException; + +import javax.security.auth.callback.Callback; + +public class Geolocation extends CordovaPlugin { + + String TAG = "GeolocationPlugin"; + CallbackContext context; + + String [] permissions; + + public Geolocation() + { + permissions = new String[2]; + permissions[0] = Manifest.permission.ACCESS_COARSE_LOCATION; + permissions[1] = Manifest.permission.ACCESS_FINE_LOCATION; + } + + + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + context = callbackContext; + if(action.equals("getPermission")) + { + if(hasPermisssion()) + { + PluginResult r = new PluginResult(PluginResult.Status.OK); + context.sendPluginResult(r); + return true; + } + else { + cordova.requestPermissions(this, 0); + } + return true; + } + return false; + } + + public String[] getPermissionRequest() { + return permissions; + } + + + public void onRequestPermissionResult(int requestCode, String[] permissions, + int[] grantResults) throws JSONException + { + PluginResult result; + for(int r:grantResults) + { + if(r == PackageManager.PERMISSION_DENIED) + { + LOG.d(TAG, "Permission Denied!"); + result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION); + context.sendPluginResult(result); + return; + } + } + result = new PluginResult(PluginResult.Status.OK); + context.sendPluginResult(result); + } + + public boolean hasPermisssion() { + if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) + { + return true; + } + for(String p : permissions) + { + if(PackageManager.PERMISSION_DENIED == cordova.getActivity().checkSelfPermission(p)) + { + return false; + } + } + + return true; + } + + +} http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/blob/e713e7ab/www/android/geolocation.js ---------------------------------------------------------------------- diff --git a/www/android/geolocation.js b/www/android/geolocation.js new file mode 100644 index 0000000..7aa36f4 --- /dev/null +++ b/www/android/geolocation.js @@ -0,0 +1,58 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + + +var exec = cordova.require('cordova/exec'); + +module.exports = { + + /*TODO: Fix scope issues with this cordova.require. I have no idea exec works, but geo doesn't work */ + + getCurrentPosition: function(success, error, args) { + var win = function() { + var geo = cordova.require('cordova/modulemapper').getOriginalSymbol(window, 'navigator.geolocation'); + geo.getCurrentPosition(success, error, { + enableHighAccuracy: args[0], + maximumAge: args[1] + }); + }; + exec(win, error, "Geolocation", "getPermission", []); + }, + + addWatch: function(success, error, args) { + var win = function() { + var geo = cordova.require('cordova/modulemapper').getOriginalSymbol(window, 'navigator.geolocation'); + geo.watchPosition(successCallback, error, { + enableHighAccuracy: args[1] + }); + }; + exec(win, error, "Geolocation", "getPermission", []); + }, + + clearWatch: function(success, error, args) { + var win = function() { + var geo = cordova.require('cordova/modulemapper').getOriginalSymbol(window, 'navigator.geolocation'); + geo.clearWatch(args[0]); + } + exec(win, error, "Geolocation", "getPermission", []); + } +}; + --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
