Skip to content

Commit

Permalink
Thanks to Marshmallow, we now have a bunch of new code to handle the …
Browse files Browse the repository at this point in the history
…permissions. This is a major step backwards.
  • Loading branch information
infil00p committed Oct 28, 2015
1 parent bf8a373 commit e713e7a
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
13 changes: 13 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
108 changes: 108 additions & 0 deletions src/android/Geolocation.java
Original file line number Diff line number Diff line change
@@ -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() {

This comment has been minimized.

Copy link
@colonelchlorine

colonelchlorine Oct 29, 2015

typo. extra "s"

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;
}


}
58 changes: 58 additions & 0 deletions www/android/geolocation.js
Original file line number Diff line number Diff line change
@@ -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", []);
}
};

0 comments on commit e713e7a

Please sign in to comment.