Skip to content

Commit

Permalink
feat: Allows registering the onRequestPermissionsResult callback.
Browse files Browse the repository at this point in the history
Adds an interface in PythonActivity and a method to register a
Python function which will be called when the
onRequestPermissionsResult callback is received.
In android/permissions.py, a new function
'register_permissions_callback' is added to register a Python
function (that takes three arguments) which will receive the
three arguments of onRequestPermissionsResult.
  • Loading branch information
gbm001 committed Jul 15, 2019
1 parent 0415720 commit ecee0e2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,34 @@ public void onWindowFocusChanged(boolean hasFocus) {
// call native function (since it's not yet loaded)
}
considerLoadingScreenRemoval();
}
}

/**
* Used by android.permissions p4a module to register a call back after
* requesting runtime permissions
**/
public interface PermissionsCallback {
void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults);
}

private PermissionsCallback permissionCallback;
private boolean havePermissionsCallback = false;

public void addPermissionsCallback(PermissionsCallback callback) {
permissionCallback = callback;
havePermissionsCallback = true;
Log.v(TAG, "addPermissionsCallback(): Added callback for onRequestPermissionsResult");
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Log.v(TAG, "onRequestPermissionsResult()");
if (havePermissionsCallback) {
Log.v(TAG, "onRequestPermissionsResult passed to callback");
permissionCallback.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

/**
* Used by android.permissions p4a module to check a permission
Expand All @@ -592,9 +619,9 @@ public boolean checkCurrentPermission(String permission) {

try {
java.lang.reflect.Method methodCheckPermission =
Activity.class.getMethod("checkSelfPermission", java.lang.String.class);
Activity.class.getMethod("checkSelfPermission", java.lang.String.class);
Object resultObj = methodCheckPermission.invoke(this, permission);
int result = Integer.parseInt(resultObj.toString());
int result = Integer.parseInt(resultObj.toString());
if (result == PackageManager.PERMISSION_GRANTED)
return true;
} catch (IllegalAccessException | NoSuchMethodException |
Expand All @@ -612,7 +639,7 @@ public void requestPermissions(String[] permissions) {
try {
java.lang.reflect.Method methodRequestPermission =
Activity.class.getMethod("requestPermissions",
java.lang.String[].class, int.class);
java.lang.String[].class, int.class);
methodRequestPermission.invoke(this, permissions, 1);
} catch (IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
Expand Down
37 changes: 36 additions & 1 deletion pythonforandroid/recipes/android/src/android/permissions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

try:
from jnius import autoclass
from jnius import autoclass, PythonJavaClass, java_method
except ImportError:
# To allow importing by build/manifest-creating code without
# pyjnius being present:
Expand Down Expand Up @@ -422,6 +422,41 @@ class Permission:
)


class onRequestPermissionsCallback(PythonJavaClass):
"""Callback class for registering a Python callback from
onRequestPermissionsResult in PythonActivity.
"""
__javainterfaces__ = ['org.kivy.android.PythonActivity$PermissionsCallback']
__javacontext__ = 'app'
_callback = None # To avoid garbage collection

def __init__(self, func):
self.func = func
onRequestPermissionsCallback._callback = self
super().__init__()

@java_method('(I[Ljava/lang/String;[I)V')
def onRequestPermissionsResult(self, requestCode, permissions, grantResults):
self.func(requestCode, permissions, grantResults)


def register_permissions_callback(callback):
"""Register a callback. This will asynchronously receive arguments from
onRequestPermissionsResult on PythonActivity after request_permission(s)
is called.
The callback must accept three arguments: requestCode, permissions and
grantResults.
Note that calling request_permission on SDK_INT < 23 will return
immediately (as run-time permissions are not required), and so this
callback will never happen.
"""
java_callback = onRequestPermissionsCallback(callback)
python_activity = autoclass('org.kivy.android.PythonActivity')
python_activity.addPermissionsCallback(java_callback)


def request_permissions(permissions):
python_activity = autoclass('org.kivy.android.PythonActivity')
python_activity.requestPermissions(permissions)
Expand Down

0 comments on commit ecee0e2

Please sign in to comment.