Skip to content

Commit 72413b5

Browse files
committed
aggregate permissions and request at the end of draw
1 parent 80601c4 commit 72413b5

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

core/src/processing/core/PApplet.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,20 @@ public class PApplet extends Object implements PConstants {
232232
*/
233233
public boolean focused = false;
234234

235+
///////////////////////////////////////////////////////////////
236+
// Permission handling
237+
235238
/**
236239
* Callback methods to handle permission requests
237240
*/
238241
protected HashMap<String, Method> permissionMethods = new HashMap<String, Method>();
239242

243+
244+
/**
245+
* Permissions requested during one frame
246+
*/
247+
protected ArrayList<String> reqPermissions = new ArrayList<String>();
248+
240249
///////////////////////////////////////////////////////////////
241250
// Wallpaper and watchface variables: these will go away soon...
242251
@Deprecated
@@ -579,7 +588,6 @@ public boolean hasPermission(String permission) {
579588

580589
public void requestPermission(String permission, String callback) {
581590
if (!hasPermission(permission)) {
582-
println("Requesting permission ", permission, " with callback ", callback);
583591
Method handleMethod = null;
584592
try {
585593
Class<?> callbackClass = this.getClass();
@@ -589,7 +597,9 @@ public void requestPermission(String permission, String callback) {
589597
}
590598
if (handleMethod != null) {
591599
permissionMethods.put(permission, handleMethod);
592-
surface.requestPermission(permission);
600+
// Accumulating permissions so they requested all at once at the end
601+
// of draw.
602+
reqPermissions.add(permission);
593603
}
594604
}
595605
}
@@ -623,6 +633,15 @@ private void handlePermissionsResult(String permission, boolean granted) {
623633
}
624634

625635

636+
private void handlePermissions() {
637+
if (0 < reqPermissions.size()) {
638+
String[] req = reqPermissions.toArray(new String[reqPermissions.size()]);
639+
surface.requestPermissions(req);
640+
reqPermissions.clear();
641+
}
642+
}
643+
644+
626645
/**
627646
* @param method "size" or "fullScreen"
628647
* @param args parameters passed to the function so we can show the user
@@ -1744,6 +1763,7 @@ public void handleDraw() {
17441763
dequeueEvents();
17451764

17461765
handleMethods("draw");
1766+
handlePermissions();
17471767

17481768
redraw = false; // unset 'redraw' flag in case it was set
17491769
// (only do this once draw() has run, not just setup())

core/src/processing/core/PSurface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,5 @@ public void initView(int sketchWidth, int sketchHeight, boolean parentSize,
9999
public void setFrameRate(float fps);
100100

101101
public boolean hasPermission(String permission);
102-
public void requestPermission(String permission);
102+
public void requestPermissions(String[] permissions);
103103
}

core/src/processing/core/PSurfaceNone.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
import android.widget.RelativeLayout;
4747
import android.support.v4.os.ResultReceiver;
4848

49-
import android.support.wearable.activity.WearableActivity;
50-
5149
import java.io.File;
5250
import java.io.FileNotFoundException;
5351
import java.io.InputStream;
@@ -522,16 +520,15 @@ public boolean hasPermission(String permission) {
522520
}
523521

524522

525-
public void requestPermission(String permission) {
523+
public void requestPermissions(String[] permissions) {
526524
int comp = component.getKind();
527525
if (comp == AppComponent.FRAGMENT) {
528526
// Requesting permissions from user when the app resumes.
529527
// Nice example on how to handle user response
530528
// http://stackoverflow.com/a/35495855
531529
// More on permission in Android 23:
532530
// https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en
533-
ActivityCompat.requestPermissions(activity, new String[] { permission },
534-
REQUEST_PERMISSIONS);
531+
ActivityCompat.requestPermissions(activity, permissions, REQUEST_PERMISSIONS);
535532
} else if (comp == AppComponent.WALLPAPER || comp == AppComponent.WATCHFACE) {
536533
// https://developer.android.com/training/articles/wear-permissions.html
537534
// Inspired by PermissionHelper.java from Michael von Glasow:
@@ -549,7 +546,7 @@ protected void onReceiveResult (int resultCode, Bundle resultData) {
549546
};
550547
final Intent permIntent = new Intent(getContext(), PermissionRequestActivity.class);
551548
permIntent.putExtra(KEY_RESULT_RECEIVER, resultReceiver);
552-
permIntent.putExtra(KEY_PERMISSIONS, new String[] { permission });
549+
permIntent.putExtra(KEY_PERMISSIONS, permissions);
553550
permIntent.putExtra(KEY_REQUEST_CODE, REQUEST_PERMISSIONS);
554551
// Show the dialog requesting the permissions
555552
permIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -558,7 +555,6 @@ protected void onReceiveResult (int resultCode, Bundle resultData) {
558555
}
559556
}
560557

561-
//WearableActivity
562558
public static class PermissionRequestActivity extends Activity {
563559
ResultReceiver resultReceiver;
564560
String[] permissions;

templates/FragmentActivity.java.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class MainActivity extends AppCompatActivity {
2929
public void onRequestPermissionsResult(int requestCode,
3030
String permissions[],
3131
int[] grantResults) {
32-
PApplet.println("-------> onRequestPermissionsResult", grantResults);
3332
if (sketch != null) {
3433
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
3534
}

0 commit comments

Comments
 (0)