mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
Refactor of the Cordova Plugin/Permissions API
This commit is contained in:
parent
2ceb8030ee
commit
013ad94af0
@ -70,6 +70,19 @@ public interface CordovaInterface {
|
|||||||
*/
|
*/
|
||||||
public ExecutorService getThreadPool();
|
public ExecutorService getThreadPool();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a permission request to the activity for one permission.
|
||||||
|
*/
|
||||||
public void requestPermission(CordovaPlugin plugin, int requestCode, String permission);
|
public void requestPermission(CordovaPlugin plugin, int requestCode, String permission);
|
||||||
public void requestPermissions(CordovaPlugin plugin, int requestCode);
|
|
||||||
|
/**
|
||||||
|
* Sends a permission request to the activity for a group of permissions
|
||||||
|
*/
|
||||||
|
public void requestPermissions(CordovaPlugin plugin, int requestCode, String [] permissions);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check for a permission. Returns true if the permission is granted, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean hasPermission(String permission);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,11 @@
|
|||||||
|
|
||||||
package org.apache.cordova;
|
package org.apache.cordova;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@ -188,11 +191,24 @@ public class CordovaInterfaceImpl implements CordovaInterface {
|
|||||||
getActivity().requestPermissions(permissions, requestCode);
|
getActivity().requestPermissions(permissions, requestCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestPermissions(CordovaPlugin plugin, int requestCode)
|
public void requestPermissions(CordovaPlugin plugin, int requestCode, String [] permissions)
|
||||||
{
|
{
|
||||||
permissionResultCallback = plugin;
|
permissionResultCallback = plugin;
|
||||||
String[] permissions = plugin.getPermissionRequest();
|
|
||||||
getActivity().requestPermissions(permissions, requestCode);
|
getActivity().requestPermissions(permissions, requestCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasPermission(String permission)
|
||||||
|
{
|
||||||
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
||||||
|
{
|
||||||
|
int result = activity.checkSelfPermission(permission);
|
||||||
|
return PackageManager.PERMISSION_GRANTED == result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ import android.content.Intent;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -365,14 +366,27 @@ public class CordovaPlugin {
|
|||||||
/**
|
/**
|
||||||
* Called by the Plugin Manager when we need to actually request permissions
|
* Called by the Plugin Manager when we need to actually request permissions
|
||||||
*
|
*
|
||||||
|
* @param requestCode Passed to the activity to track the request
|
||||||
|
*
|
||||||
* @return Returns the permission that was stored in the plugin
|
* @return Returns the permission that was stored in the plugin
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public String[] getPermissionRequest() {
|
public void requestPermissions(int requestCode) {
|
||||||
return permissions;
|
cordova.requestPermissions(this, requestCode, permissions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Called by the WebView implementation to check for geolocation permissions, can be used
|
||||||
|
* by other Java methods in the event that a plugin is using this as a dependency.
|
||||||
|
*
|
||||||
|
* @return Returns true if the plugin has all the permissions it needs to operate.
|
||||||
|
*/
|
||||||
|
|
||||||
public boolean hasPermisssion() {
|
public boolean hasPermisssion() {
|
||||||
|
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
for(String p : permissions)
|
for(String p : permissions)
|
||||||
{
|
{
|
||||||
if(PackageManager.PERMISSION_DENIED == cordova.getActivity().checkSelfPermission(p))
|
if(PackageManager.PERMISSION_DENIED == cordova.getActivity().checkSelfPermission(p))
|
||||||
|
@ -189,7 +189,7 @@ public class SystemWebChromeClient extends WebChromeClient {
|
|||||||
CordovaPlugin geolocation = parentEngine.pluginManager.getPlugin("Geolocation");
|
CordovaPlugin geolocation = parentEngine.pluginManager.getPlugin("Geolocation");
|
||||||
if(geolocation != null && !geolocation.hasPermisssion())
|
if(geolocation != null && !geolocation.hasPermisssion())
|
||||||
{
|
{
|
||||||
geolocation.cordova.requestPermissions(geolocation, 0);
|
geolocation.requestPermissions(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user