Skip to content

Commit 80601c4

Browse files
committed
new permission handling api
1 parent 84e8b3f commit 80601c4

22 files changed

+331
-689
lines changed

core/src/processing/android/AppComponent.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,4 @@ abstract public interface AppComponent extends PConstants {
4646
public boolean canDraw();
4747

4848
public void dispose();
49-
public void onPermissionsGranted();
5049
}

core/src/processing/android/PFragment.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.support.v4.app.FragmentManager;
2929
import android.support.v4.app.FragmentTransaction;
3030
import android.util.DisplayMetrics;
31+
3132
import android.content.pm.ActivityInfo;
3233
import android.content.res.Configuration;
3334
import android.graphics.Point;
@@ -43,8 +44,8 @@
4344

4445
public class PFragment extends Fragment implements AppComponent {
4546
private DisplayMetrics metrics;
46-
protected Point size;
47-
protected PApplet sketch;
47+
private Point size;
48+
private PApplet sketch;
4849

4950
public PFragment() {
5051
}
@@ -113,6 +114,13 @@ public void setSketch(PApplet sketch, @IdRes int id, @LayoutRes int layout,
113114
transaction.commit();
114115
}
115116

117+
public void setSketch(PApplet sketch, View view, FragmentManager manager) {
118+
this.sketch = sketch;
119+
FragmentTransaction transaction = manager.beginTransaction();
120+
transaction.add(view.getId(), this);
121+
transaction.commit();
122+
}
123+
116124
public PApplet getSketch() {
117125
return sketch;
118126
}
@@ -124,6 +132,7 @@ public ServiceEngine getEngine() {
124132
public void dispose() {
125133
}
126134

135+
127136
@Override
128137
public View onCreateView(LayoutInflater inflater, ViewGroup container,
129138
Bundle savedInstanceState) {
@@ -177,16 +186,6 @@ public void onConfigurationChanged(Configuration newConfig) {
177186
}
178187

179188

180-
// public void onBackPressed() {
181-
// sketch.exit();
182-
// }
183-
184-
185-
public void onPermissionsGranted() {
186-
if (sketch != null) sketch.onPermissionsGranted();
187-
}
188-
189-
190189
public void setOrientation(int which) {
191190
if (which == PORTRAIT) {
192191
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
@@ -199,8 +198,14 @@ public void setOrientation(int which) {
199198
public void requestDraw() {
200199
}
201200

201+
202202
public boolean canDraw() {
203203
if (sketch == null) return false;
204204
return sketch.isLooping();
205205
}
206+
207+
208+
//public void onBackPressed() {
209+
// sketch.exit();
210+
//}
206211
}

core/src/processing/android/PWallpaper.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,6 @@ public void requestPermissions() {
122122
}
123123

124124

125-
public void onPermissionsGranted() {
126-
if (engine != null) engine.onPermissionsGranted();
127-
}
128-
129-
130125
@Override
131126
public Engine onCreateEngine() {
132127
engine = new WallpaperEngine();
@@ -159,13 +154,6 @@ public void onCreate(SurfaceHolder surfaceHolder) {
159154
}
160155

161156

162-
private void onPermissionsGranted() {
163-
if (sketch != null) {
164-
sketch.onPermissionsGranted();
165-
}
166-
}
167-
168-
169157
@Override
170158
public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
171159
super.onSurfaceCreated(surfaceHolder);
@@ -315,5 +303,14 @@ public boolean useLowBitAmbient() {
315303
public boolean requireBurnInProtection() {
316304
return false;
317305
}
306+
307+
@Override
308+
public void onRequestPermissionsResult(int requestCode,
309+
String permissions[],
310+
int[] grantResults) {
311+
if (sketch != null) {
312+
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
313+
}
314+
}
318315
}
319316
}

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ public void requestPermissions() {
135135
}
136136

137137

138-
public void onPermissionsGranted() {
139-
if (engine != null) engine.onPermissionsGranted();
140-
}
141-
142-
143138
@Override
144139
public Engine onCreateEngine() {
145140
engine = new CanvasEngine();
@@ -198,13 +193,6 @@ private void invalidateIfNecessary() {
198193
}
199194

200195

201-
private void onPermissionsGranted() {
202-
if (sketch != null) {
203-
sketch.onPermissionsGranted();
204-
}
205-
}
206-
207-
208196
@Override
209197
public void onAmbientModeChanged(boolean inAmbientMode) {
210198
super.onAmbientModeChanged(inAmbientMode);
@@ -433,5 +421,14 @@ public boolean useLowBitAmbient() {
433421
public boolean requireBurnInProtection() {
434422
return burnInProtection;
435423
}
424+
425+
@Override
426+
public void onRequestPermissionsResult(int requestCode,
427+
String permissions[],
428+
int[] grantResults) {
429+
if (sketch != null) {
430+
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
431+
}
432+
}
436433
}
437434
}

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ public void requestPermissions() {
133133
}
134134

135135

136-
public void onPermissionsGranted() {
137-
if (engine != null) engine.onPermissionsGranted();
138-
}
139-
140-
141136
@Override
142137
public Engine onCreateEngine() {
143138
engine = new GLES2Engine();
@@ -215,13 +210,6 @@ private void invalidateIfNecessary() {
215210
}
216211

217212

218-
public void onPermissionsGranted() {
219-
if (sketch != null) {
220-
sketch.onPermissionsGranted();
221-
}
222-
}
223-
224-
225213
@Override
226214
public void onAmbientModeChanged(boolean inAmbientMode) {
227215
super.onAmbientModeChanged(inAmbientMode);
@@ -434,5 +422,15 @@ public boolean useLowBitAmbient() {
434422
public boolean requireBurnInProtection() {
435423
return burnInProtection;
436424
}
425+
426+
427+
@Override
428+
public void onRequestPermissionsResult(int requestCode,
429+
String permissions[],
430+
int[] grantResults) {
431+
if (sketch != null) {
432+
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
433+
}
434+
}
437435
}
438436
}

core/src/processing/android/ServiceEngine.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ public interface ServiceEngine extends PConstants {
4141
public Rect getInsets();
4242
public boolean useLowBitAmbient();
4343
public boolean requireBurnInProtection();
44+
45+
// Service permissions
46+
public void onRequestPermissionsResult(int requestCode,
47+
String permissions[],
48+
int[] grantResults);
4449
}

core/src/processing/core/PApplet.java

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -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

core/src/processing/core/PSurface.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
* handling
4545
*/
4646
public interface PSurface {
47+
public static final int REQUEST_PERMISSIONS = 1;
48+
4749
public AppComponent getComponent();
4850
public Context getContext();
4951
public Activity getActivity();
@@ -95,4 +97,7 @@ public void initView(int sketchWidth, int sketchHeight, boolean parentSize,
9597
public void finish();
9698

9799
public void setFrameRate(float fps);
100+
101+
public boolean hasPermission(String permission);
102+
public void requestPermission(String permission);
98103
}

0 commit comments

Comments
 (0)