Skip to content

Commit 41623cd

Browse files
committed
fixed permissions
1 parent d74637a commit 41623cd

File tree

10 files changed

+92
-65
lines changed

10 files changed

+92
-65
lines changed

core/src/processing/android/AppComponent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ abstract public interface AppComponent extends PConstants {
3838
public int getKind();
3939
public void setSketch(PApplet sketch);
4040
public PApplet getSketch();
41+
42+
public boolean isService();
4143
public ServiceEngine getEngine();
4244

4345
public void startActivity(Intent intent);

core/src/processing/android/PFragment.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,10 @@ public class PFragment extends Fragment implements AppComponent {
4747
private Point size;
4848
private PApplet sketch;
4949

50+
5051
public PFragment() {
5152
}
5253

53-
// public void setLayout(int layout, FragmentManager manager) {
54-
// this.layout = layout;
55-
// if (sketch != null) {
56-
// sketch.parentLayout = layout;
57-
// }
58-
// FragmentTransaction transaction = manager.beginTransaction();
59-
// transaction.add(layout, this);
60-
// transaction.commit();
61-
// }
6254

6355
public void initDimensions() {
6456
WindowManager wm = getActivity().getWindowManager();
@@ -83,28 +75,34 @@ public void initDimensions() {
8375
}
8476
}
8577

78+
8679
public int getDisplayWidth() {
8780
return size.x;
8881
// return metrics.widthPixels;
8982
}
9083

84+
9185
public int getDisplayHeight() {
9286
return size.y;
9387
// return metrics.heightPixels;
9488
}
9589

90+
9691
public float getDisplayDensity() {
9792
return metrics.density;
9893
}
9994

95+
10096
public int getKind() {
10197
return FRAGMENT;
10298
}
10399

100+
104101
public void setSketch(PApplet sketch) {
105102
this.sketch = sketch;
106103
}
107104

105+
108106
public void setSketch(PApplet sketch, @IdRes int id, @LayoutRes int layout,
109107
FragmentManager manager) {
110108
this.sketch = sketch;
@@ -114,21 +112,30 @@ public void setSketch(PApplet sketch, @IdRes int id, @LayoutRes int layout,
114112
transaction.commit();
115113
}
116114

115+
117116
public void setSketch(PApplet sketch, View view, FragmentManager manager) {
118117
this.sketch = sketch;
119118
FragmentTransaction transaction = manager.beginTransaction();
120119
transaction.add(view.getId(), this);
121120
transaction.commit();
122121
}
123122

123+
124124
public PApplet getSketch() {
125125
return sketch;
126126
}
127127

128+
129+
public boolean isService() {
130+
return false;
131+
}
132+
133+
128134
public ServiceEngine getEngine() {
129135
return null;
130136
}
131137

138+
132139
public void dispose() {
133140
}
134141

@@ -144,6 +151,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
144151
}
145152
}
146153

154+
147155
@Override
148156
public void onResume() {
149157
super.onResume();

core/src/processing/android/PWallpaper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public PApplet getSketch() {
100100
}
101101

102102

103+
public boolean isService() {
104+
return true;
105+
}
106+
107+
103108
public ServiceEngine getEngine() {
104109
return engine;
105110
}

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public PApplet getSketch() {
110110
}
111111

112112

113+
public boolean isService() {
114+
return true;
115+
}
116+
117+
113118
public ServiceEngine getEngine() {
114119
return engine;
115120
}

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public PApplet getSketch() {
108108
}
109109

110110

111+
public boolean isService() {
112+
return true;
113+
}
114+
115+
111116
public ServiceEngine getEngine() {
112117
return engine;
113118
}

core/src/processing/core/PApplet.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,13 @@ public boolean hasPermission(String permission) {
586586
}
587587

588588

589+
public void requestPermission(String permission) {
590+
if (!hasPermission(permission)) {
591+
reqPermissions.add(permission);
592+
}
593+
}
594+
595+
589596
public void requestPermission(String permission, String callback) {
590597
if (!hasPermission(permission)) {
591598
Method handleMethod = null;

core/src/processing/core/PSurfaceNone.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -517,37 +517,37 @@ public boolean hasPermission(String permission) {
517517

518518

519519
public void requestPermissions(String[] permissions) {
520-
int comp = component.getKind();
521-
if (comp == AppComponent.FRAGMENT) {
522-
// Requesting permissions from user when the app resumes.
523-
// Nice example on how to handle user response
524-
// http://stackoverflow.com/a/35495855
525-
// More on permission in Android 23:
526-
// https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en
527-
ActivityCompat.requestPermissions(activity, permissions, REQUEST_PERMISSIONS);
528-
} else if (comp == AppComponent.WALLPAPER || comp == AppComponent.WATCHFACE) {
520+
if (component.isService()) {
529521
// https://developer.android.com/training/articles/wear-permissions.html
530522
// Inspired by PermissionHelper.java from Michael von Glasow:
531523
// https://github.com/mvglasow/satstat/blob/master/src/com/vonglasow/michael/satstat/utils/PermissionHelper.java
532524
// Example of use:
533525
// https://github.com/mvglasow/satstat/blob/master/src/com/vonglasow/michael/satstat/PasvLocListenerService.java
534526
final ServiceEngine eng = getEngine();
535-
ResultReceiver resultReceiver = new ResultReceiver(new Handler(Looper.getMainLooper())) {
536-
@Override
537-
protected void onReceiveResult (int resultCode, Bundle resultData) {
538-
String[] outPermissions = resultData.getStringArray(PermissionRequestor.KEY_PERMISSIONS);
539-
int[] grantResults = resultData.getIntArray(PermissionRequestor.KEY_GRANT_RESULTS);
540-
eng.onRequestPermissionsResult(resultCode, outPermissions, grantResults);
541-
}
542-
};
543-
final Intent permIntent = new Intent(getContext(), PermissionRequestor.class);
544-
permIntent.putExtra(PermissionRequestor.KEY_RESULT_RECEIVER, resultReceiver);
545-
permIntent.putExtra(PermissionRequestor.KEY_PERMISSIONS, permissions);
546-
permIntent.putExtra(PermissionRequestor.KEY_REQUEST_CODE, REQUEST_PERMISSIONS);
547-
// Show the dialog requesting the permissions
548-
permIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
549-
550-
startActivity(permIntent);
527+
if (eng != null) { // A valid service should have a non-null engine at this point, but just in case
528+
ResultReceiver resultReceiver = new ResultReceiver(new Handler(Looper.getMainLooper())) {
529+
@Override
530+
protected void onReceiveResult (int resultCode, Bundle resultData) {
531+
String[] outPermissions = resultData.getStringArray(PermissionRequestor.KEY_PERMISSIONS);
532+
int[] grantResults = resultData.getIntArray(PermissionRequestor.KEY_GRANT_RESULTS);
533+
eng.onRequestPermissionsResult(resultCode, outPermissions, grantResults);
534+
}
535+
};
536+
final Intent permIntent = new Intent(getContext(), PermissionRequestor.class);
537+
permIntent.putExtra(PermissionRequestor.KEY_RESULT_RECEIVER, resultReceiver);
538+
permIntent.putExtra(PermissionRequestor.KEY_PERMISSIONS, permissions);
539+
permIntent.putExtra(PermissionRequestor.KEY_REQUEST_CODE, REQUEST_PERMISSIONS);
540+
// Show the dialog requesting the permissions
541+
permIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
542+
startActivity(permIntent);
543+
}
544+
} else if (activity != null) {
545+
// Requesting permissions from user when the app resumes.
546+
// Nice example on how to handle user response
547+
// http://stackoverflow.com/a/35495855
548+
// More on permission in Android 23:
549+
// https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en
550+
ActivityCompat.requestPermissions(activity, permissions, REQUEST_PERMISSIONS);
551551
}
552552
}
553553
}

libraries/vr/src/processing/vr/PSurfaceVR.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
public class PSurfaceVR extends PSurfaceGLES {
5555
protected GLVRSurfaceView glview;
56-
protected PGraphicsVR pgc;
56+
protected PGraphicsVR pvr;
5757

5858
protected GvrActivity vrActivity;
5959
protected AndroidVRStereoRenderer renderer;
@@ -65,7 +65,8 @@ public PSurfaceVR(PGraphics graphics, AppComponent component, SurfaceHolder hold
6565
this.pgl = (PGLES)((PGraphicsOpenGL)graphics).pgl;
6666

6767
vrActivity = (GvrActivity)component;
68-
pgc = (PGraphicsVR)graphics;
68+
this.activity = vrActivity;
69+
pvr = (PGraphicsVR)graphics;
6970

7071
glview = new GLVRSurfaceView(vrActivity);
7172
glview.setStereoModeEnabled(vr);
@@ -87,9 +88,6 @@ public PSurfaceVR(PGraphics graphics, AppComponent component, SurfaceHolder hold
8788
}
8889
vrActivity.setGvrView(glview);
8990

90-
// Required to read the paired viewer's distortion parameters.
91-
requestPermissions(new String[] {"android.permission.READ_EXTERNAL_STORAGE"});
92-
9391
surfaceView = null;
9492
}
9593

@@ -293,12 +291,12 @@ public AndroidVRStereoRenderer() {
293291
@Override
294292
public void onNewFrame(HeadTransform transform) {
295293
pgl.getGL(null);
296-
pgc.headTransform(transform);
294+
pvr.headTransform(transform);
297295
}
298296

299297
@Override
300298
public void onDrawEye(Eye eye) {
301-
pgc.eyeTransform(eye);
299+
pvr.eyeTransform(eye);
302300
sketch.handleDraw();
303301
}
304302

libraries/vr/src/processing/vr/PVR.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import processing.android.ServiceEngine;
3131
import processing.core.PApplet;
3232

33-
// http://pastebin.com/6wPgFYhq
3433
public class PVR extends GvrActivity implements AppComponent {
3534
public static final String STEREO = "processing.vr.PGraphicsVRStereo";
3635
public static final String MONO = "processing.vr.PGraphicsVRMono";
@@ -44,65 +43,70 @@ public class PVR extends GvrActivity implements AppComponent {
4443
private DisplayMetrics metrics;
4544
private PApplet sketch;
4645

46+
4747
public PVR() {
4848

4949
}
5050

51+
5152
public PVR(PApplet sketch) {
5253
this.sketch = sketch;
5354
}
5455

56+
5557
public void initDimensions() {
5658
metrics = getResources().getDisplayMetrics();
5759
}
5860

61+
5962
public int getDisplayWidth() {
6063
return metrics.widthPixels;
6164
}
6265

66+
6367
public int getDisplayHeight() {
6468
return metrics.heightPixels;
6569
}
6670

71+
6772
public float getDisplayDensity() {
6873
return metrics.density;
6974
}
7075

76+
7177
public int getKind() {
7278
return VR;
7379
}
7480

81+
7582
public void dispose() {
7683
}
7784

78-
public void onPermissionsGranted() {
79-
80-
}
8185

8286
public void setSketch(PApplet sketch) {
8387
this.sketch = sketch;
8488
if (sketch != null) {
8589
sketch.initSurface(PVR.this, null);
90+
// Required to read the paired viewer's distortion parameters.
91+
sketch.requestPermission("android.permission.READ_EXTERNAL_STORAGE");
8692
}
8793
}
8894

95+
8996
public PApplet getSketch() {
9097
return sketch;
9198
}
9299

100+
101+
public boolean isService() {
102+
return false;
103+
}
104+
105+
93106
public ServiceEngine getEngine() {
94107
return null;
95108
}
96109

97-
/*
98-
* Called with the activity is first created.
99-
*/
100-
// @SuppressWarnings("unchecked")
101-
// @Override
102-
// public void onCreate(Bundle savedInstanceState) {
103-
// super.onCreate(savedInstanceState);
104-
//
105-
// }
106110

107111
@Override
108112
public void onResume() {
@@ -138,18 +142,21 @@ public void onStop() {
138142
sketch.onStop();
139143
}
140144

145+
141146
public void requestDraw() {
142147
}
143148

149+
144150
public boolean canDraw() {
145151
return true;
146152
}
147153

154+
148155
@Override
149156
public void onRequestPermissionsResult(int requestCode,
150157
String permissions[],
151158
int[] grantResults) {
152-
if (sketch == null) {
159+
if (sketch != null) {
153160
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
154161
}
155162
}

templates/VRActivity.java.tmpl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,4 @@ public class MainActivity extends PVR {
1212
PApplet sketch = new @@sketch_class_name@@();
1313
setSketch(sketch);
1414
}
15-
16-
@Override
17-
public void onRequestPermissionsResult(int requestCode,
18-
String permissions[],
19-
int[] grantResults) {
20-
PApplet sketch = getSketch();
21-
if (sketch != null) {
22-
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
23-
}
24-
}
2515
}

0 commit comments

Comments
 (0)