From a0747aa960cd906beb3249e3d91443a0ea3e1578 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Fri, 28 Aug 2015 16:50:16 -0700 Subject: [PATCH] Refactoring based on feedback from Andrew --- .../org/apache/cordova/CordovaActivity.java | 3 +- .../org/apache/cordova/CordovaInterface.java | 3 +- .../apache/cordova/CordovaInterfaceImpl.java | 32 +++++++++++++++++-- .../src/org/apache/cordova/CordovaPlugin.java | 7 ---- .../src/org/apache/cordova/PluginManager.java | 22 ------------- 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java index 171e2a83..43e013bc 100755 --- a/framework/src/org/apache/cordova/CordovaActivity.java +++ b/framework/src/org/apache/cordova/CordovaActivity.java @@ -493,8 +493,7 @@ public class CordovaActivity extends Activity { @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { - PluginManager pm = this.appView.getPluginManager(); - pm.onRequestPermissionResult(requestCode, permissions, grantResults); + cordovaInterface.onRequestPermissionResult(requestCode, permissions, grantResults); } } diff --git a/framework/src/org/apache/cordova/CordovaInterface.java b/framework/src/org/apache/cordova/CordovaInterface.java index 3fa6e63b..2aa04526 100755 --- a/framework/src/org/apache/cordova/CordovaInterface.java +++ b/framework/src/org/apache/cordova/CordovaInterface.java @@ -70,5 +70,6 @@ public interface CordovaInterface { */ public ExecutorService getThreadPool(); - public void requestPermission(CordovaPlugin plugin); + public void requestPermission(CordovaPlugin plugin, String permission); + public void requestPermissions(CordovaPlugin plugin); } diff --git a/framework/src/org/apache/cordova/CordovaInterfaceImpl.java b/framework/src/org/apache/cordova/CordovaInterfaceImpl.java index 146bacb0..7b436af9 100644 --- a/framework/src/org/apache/cordova/CordovaInterfaceImpl.java +++ b/framework/src/org/apache/cordova/CordovaInterfaceImpl.java @@ -38,6 +38,7 @@ public class CordovaInterfaceImpl implements CordovaInterface { protected ActivityResultHolder savedResult; protected CordovaPlugin activityResultCallback; + protected CordovaPlugin permissionResultCallback; protected String initCallbackService; protected int activityResultRequestCode; @@ -162,9 +163,36 @@ public class CordovaInterfaceImpl implements CordovaInterface { } } - public void requestPermission(CordovaPlugin plugin) + /** + * Called by the system when the user grants permissions + * + * @param requestCode + * @param permissions + * @param grantResults + */ + public void onRequestPermissionResult(int requestCode, String[] permissions, + int[] grantResults) { + if(permissionResultCallback != null) + { + permissionResultCallback.onRequestPermissionResult(requestCode, permissions, grantResults); + permissionResultCallback = null; + } + } + + public void requestPermission(CordovaPlugin plugin, String permission) { + permissionResultCallback = plugin; + String[] permissions = new String [1]; + permissions[0] = permission; + int requestCode = 1; + getActivity().requestPermissions(permissions, requestCode); + } + + public void requestPermissions(CordovaPlugin plugin) { - pluginManager.requestPermission(plugin); + permissionResultCallback = plugin; + String[] permissions = plugin.getPermissionRequest(); + int requestCode = 1; + getActivity().requestPermissions(permissions, requestCode); } } diff --git a/framework/src/org/apache/cordova/CordovaPlugin.java b/framework/src/org/apache/cordova/CordovaPlugin.java index 6cb2a3c8..6e82027c 100644 --- a/framework/src/org/apache/cordova/CordovaPlugin.java +++ b/framework/src/org/apache/cordova/CordovaPlugin.java @@ -372,13 +372,6 @@ public class CordovaPlugin { return permissions; } - /** - * requestPermission - */ - public void requestPermission() { - cordova.requestPermission(this); - } - public boolean hasPermisssion() { for(String p : permissions) { diff --git a/framework/src/org/apache/cordova/PluginManager.java b/framework/src/org/apache/cordova/PluginManager.java index e543b04d..3afbc18d 100755 --- a/framework/src/org/apache/cordova/PluginManager.java +++ b/framework/src/org/apache/cordova/PluginManager.java @@ -511,26 +511,4 @@ public class PluginManager { } } - /** - * Called by the system when the user grants permissions - * - * @param requestCode - * @param permissions - * @param grantResults - */ - public void onRequestPermissionResult(int requestCode, String[] permissions, - int[] grantResults) { - if(permissionRequester != null) - { - permissionRequester.onRequestPermissionResult(requestCode, permissions, grantResults); - permissionRequester = null; - } - } - - public void requestPermission(CordovaPlugin plugin) { - permissionRequester = plugin; - String[] permissions = plugin.getPermissionRequest(); - int requestCode = 1; - ctx.getActivity().requestPermissions(permissions, requestCode); - } }