@@ -113,43 +113,12 @@ public class PApplet extends Object implements PConstants {
113113 static public final int DEFAULT_WIDTH = -1 ;
114114 static public final int DEFAULT_HEIGHT = -1 ;
115115
116- /**
117- * Minimum dimensions for the window holding an applet.
118- * This varies between platforms, Mac OS X 10.3 can do any height
119- * but requires at least 128 pixels width. Windows XP has another
120- * set of limitations. And for all I know, Linux probably lets you
121- * make windows with negative sizes.
122- */
123- // static public final int MIN_WINDOW_WIDTH = 128;
124- // static public final int MIN_WINDOW_HEIGHT = 128;
125-
126- /**
127- * Exception thrown when size() is called the first time.
128- * <P>
129- * This is used internally so that setup() is forced to run twice
130- * when the renderer is changed. This is the only way for us to handle
131- * invoking the new renderer while also in the midst of rendering.
132- */
133- // static public class RendererChangeException extends RuntimeException { }
134-
135- // protected boolean surfaceReady;
136-
137116 /**
138117 * Set true when the surface dimensions have changed, so that the PGraphics
139118 * object can be resized on the next trip through handleDraw().
140119 */
141120 protected boolean surfaceChanged ;
142121
143- /**
144- * true if no size() command has been executed. This is used to wait until
145- * a size has been set before placing in the window and showing it.
146- */
147- // public boolean defaultSize;
148-
149- // volatile boolean resizeRequest;
150- // volatile int resizeWidth;
151- // volatile int resizeHeight;
152-
153122 /**
154123 * Pixel buffer from this applet's PGraphics.
155124 * <P>
@@ -263,6 +232,11 @@ public class PApplet extends Object implements PConstants {
263232 */
264233 public boolean focused = false ;
265234
235+ /**
236+ * Callback methods to handle permission requests
237+ */
238+ protected HashMap <String , Method > permissionMethods = new HashMap <String , Method >();
239+
266240 ///////////////////////////////////////////////////////////////
267241 // Wallpaper and watchface variables: these will go away soon...
268242 @ Deprecated
@@ -311,12 +285,6 @@ public class PApplet extends Object implements PConstants {
311285 * As such, this value won't be valid until after 5-10 frames.
312286 */
313287 public float frameRate = 10 ;
314- /** Last time in nanoseconds that frameRate was checked */
315- // protected long frameRateLastNanos = 0;
316- //
317- // /** As of release 0116, frameRate(60) is called as a default */
318- // protected float frameRateTarget = 60;
319- // protected long frameRatePeriod = 1000000000L / 60L;
320288
321289 protected boolean looping ;
322290
@@ -339,18 +307,6 @@ public class PApplet extends Object implements PConstants {
339307 */
340308 public boolean finished ;
341309
342- /**
343- * For Android, true if the activity has been paused.
344- */
345- // protected boolean paused;
346-
347- // protected SurfaceView surfaceView;
348-
349- /**
350- * The Window object for Android.
351- */
352- // protected Window window;
353-
354310 /**
355311 * true if exit() has been called so that things shut down
356312 * once the main thread kicks off.
@@ -616,8 +572,54 @@ public void onStop() {
616572 }
617573
618574
619- public void onPermissionsGranted () {
575+ public boolean hasPermission (String permission ) {
576+ return surface .hasPermission (permission );
577+ }
578+
579+
580+ public void requestPermission (String permission , String callback ) {
581+ if (!hasPermission (permission )) {
582+ println ("Requesting permission " , permission , " with callback " , callback );
583+ Method handleMethod = null ;
584+ try {
585+ Class <?> callbackClass = this .getClass ();
586+ handleMethod = callbackClass .getMethod (callback , new Class [] { boolean .class });
587+ } catch (NoSuchMethodException nsme ) {
588+ System .err .println (callback + "() could not be found" );
589+ }
590+ if (handleMethod != null ) {
591+ permissionMethods .put (permission , handleMethod );
592+ surface .requestPermission (permission );
593+ }
594+ }
595+ }
596+
597+
598+ public void onRequestPermissionsResult (int requestCode ,
599+ String permissions [],
600+ int [] grantResults ) {
601+ if (requestCode == PSurface .REQUEST_PERMISSIONS ) {
602+ for (int i = 0 ; i < grantResults .length ; i ++) {
603+ boolean granted = grantResults [i ] == PackageManager .PERMISSION_GRANTED ;
604+ handlePermissionsResult (permissions [i ], granted );
605+ }
606+ }
607+ }
608+
620609
610+ private void handlePermissionsResult (String permission , boolean granted ) {
611+ Method handleMethod = permissionMethods .get (permission );
612+ if (handleMethod != null ) {
613+ try {
614+ handleMethod .invoke (this , new Object [] { granted });
615+ } catch (IllegalAccessException e ) {
616+ e .printStackTrace ();
617+ } catch (IllegalArgumentException e ) {
618+ e .printStackTrace ();
619+ } catch (InvocationTargetException e ) {
620+ e .printStackTrace ();
621+ }
622+ }
621623 }
622624
623625
0 commit comments