Skip to content

Commit 65dfed2

Browse files
committed
register permission callback with registerWithArgs
1 parent 14ae2b4 commit 65dfed2

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

core/src/processing/core/PApplet.java

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public class PApplet extends Object implements PConstants {
252252
/**
253253
* Callback methods to handle permission requests
254254
*/
255-
protected HashMap<String, Method> permissionMethods = new HashMap<String, Method>();
255+
protected HashMap<String, String> permissionMethods = new HashMap<String, String>();
256256

257257

258258
/**
@@ -599,32 +599,21 @@ public void requestPermission(String permission) {
599599

600600

601601
public void requestPermission(String permission, String callback) {
602-
Method handleMethod = null;
603-
try {
604-
Class<?> callbackClass = this.getClass();
605-
handleMethod = callbackClass.getMethod(callback, new Class[] { boolean.class });
606-
} catch (NoSuchMethodException nsme) {
607-
System.err.println(callback + "() could not be found");
608-
}
609-
if (handleMethod != null) {
610-
if (hasPermission(permission)) {
611-
// If the app already has permission, still call the handle method as it
612-
// may be doing some initialization
613-
try {
614-
handleMethod.invoke(this, new Object[] { true });
615-
} catch (IllegalAccessException e) {
616-
e.printStackTrace();
617-
} catch (IllegalArgumentException e) {
618-
e.printStackTrace();
619-
} catch (InvocationTargetException e) {
620-
e.printStackTrace();
621-
}
622-
} else {
623-
permissionMethods.put(permission, handleMethod);
624-
// Accumulating permissions so they requested all at once at the end
625-
// of draw.
626-
reqPermissions.add(permission);
627-
}
602+
requestPermission(permission, callback, this);
603+
}
604+
605+
606+
public void requestPermission(String permission, String callback, Object target) {
607+
registerWithArgs(callback, target, new Class[] { boolean.class });
608+
if (hasPermission(permission)) {
609+
// If the app already has permission, still call the handle method as it
610+
// may be doing some initialization
611+
handleMethods(callback, new Object[] { true });
612+
} else {
613+
permissionMethods.put(permission, callback);
614+
// Accumulating permissions so they requested all at once at the end
615+
// of draw.
616+
reqPermissions.add(permission);
628617
}
629618
}
630619

@@ -642,17 +631,10 @@ public void onRequestPermissionsResult(int requestCode,
642631

643632

644633
private void handlePermissionsResult(String permission, boolean granted) {
645-
Method handleMethod = permissionMethods.get(permission);
646-
if (handleMethod != null) {
647-
try {
648-
handleMethod.invoke(this, new Object[] { granted });
649-
} catch (IllegalAccessException e) {
650-
e.printStackTrace();
651-
} catch (IllegalArgumentException e) {
652-
e.printStackTrace();
653-
} catch (InvocationTargetException e) {
654-
e.printStackTrace();
655-
}
634+
String methodName = permissionMethods.get(permission);
635+
RegisteredMethods meth = registerMap.get(methodName);
636+
if (meth != null) {
637+
meth.handle(new Object[] { granted });
656638
}
657639
}
658640

0 commit comments

Comments
 (0)