1
1
package arscene ;
2
2
3
+ import android .Manifest ;
4
+ import android .content .pm .PackageManager ;
5
+ import android .net .Uri ;
3
6
import android .os .Bundle ;
4
7
import android .content .Intent ;
8
+ import android .provider .Settings ;
9
+ import android .support .v4 .app .ActivityCompat ;
10
+ import android .support .v4 .content .ContextCompat ;
5
11
import android .view .ViewGroup ;
6
12
import android .widget .FrameLayout ;
7
13
import android .support .v7 .app .AppCompatActivity ;
14
+ import android .widget .Toast ;
8
15
9
16
import processing .android .PFragment ;
10
17
import processing .android .CompatUtils ;
11
18
import processing .core .PApplet ;
12
19
13
20
public class MainActivity extends AppCompatActivity {
21
+ private static final int CAMERA_PERMISSION_CODE = 0 ;
22
+ private static final String CAMERA_PERMISSION = Manifest .permission .CAMERA ;
23
+
14
24
private PApplet sketch ;
15
25
16
26
@ Override
@@ -19,17 +29,31 @@ protected void onCreate(Bundle savedInstanceState) {
19
29
FrameLayout frame = new FrameLayout (this );
20
30
frame .setId (CompatUtils .getUniqueViewId ());
21
31
setContentView (frame , new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT ,
22
- ViewGroup .LayoutParams .MATCH_PARENT ));
32
+ ViewGroup .LayoutParams .MATCH_PARENT ));
23
33
24
34
sketch = new Sketch ();
25
35
PFragment fragment = new PFragment (sketch );
26
36
fragment .setView (frame , this );
27
37
}
28
38
29
39
@ Override
30
- public void onRequestPermissionsResult (int requestCode ,
31
- String permissions [],
32
- int [] grantResults ) {
40
+ protected void onResume () {
41
+ super .onResume ();
42
+ if (!hasCameraPermission ()) requestCameraPermission ();
43
+ }
44
+
45
+ @ Override
46
+ public void onRequestPermissionsResult (int requestCode , String permissions [], int [] grantResults ) {
47
+ if (!hasCameraPermission ()) {
48
+ Toast .makeText (this , "Camera permission is needed to use AR" , Toast .LENGTH_LONG )
49
+ .show ();
50
+ if (!shouldShowRequestPermissionRationale ()) {
51
+ // Permission denied with checking "Do not ask again".
52
+ launchPermissionSettings ();
53
+ }
54
+ finish ();
55
+ }
56
+
33
57
if (sketch != null ) {
34
58
sketch .onRequestPermissionsResult (requestCode , permissions , grantResults );
35
59
}
@@ -41,4 +65,41 @@ public void onNewIntent(Intent intent) {
41
65
sketch .onNewIntent (intent );
42
66
}
43
67
}
68
+
69
+ @ Override
70
+ public void onActivityResult (int requestCode , int resultCode , Intent data ) {
71
+ if (sketch != null ) {
72
+ sketch .onActivityResult (requestCode , resultCode , data );
73
+ }
74
+ }
75
+
76
+ @ Override
77
+ public void onBackPressed () {
78
+ if (sketch != null ) {
79
+ sketch .onBackPressed ();
80
+ }
81
+ }
82
+
83
+ private boolean hasCameraPermission () {
84
+ int res = ContextCompat .checkSelfPermission (this , CAMERA_PERMISSION );
85
+ return res == PackageManager .PERMISSION_GRANTED ;
86
+ }
87
+
88
+ private void requestCameraPermission () {
89
+ ActivityCompat .requestPermissions (this , new String []{Manifest .permission .CAMERA }, CAMERA_PERMISSION_CODE );
90
+ }
91
+
92
+ /** Check to see if we need to show the rationale for this permission. */
93
+ private boolean shouldShowRequestPermissionRationale () {
94
+ return ActivityCompat .shouldShowRequestPermissionRationale (this , CAMERA_PERMISSION );
95
+ }
96
+
97
+ /** Launch Application Setting to grant permission. */
98
+ private void launchPermissionSettings () {
99
+ Intent intent = new Intent ();
100
+ intent .setAction (Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
101
+ intent .setData (Uri .fromParts ("package" , this .getPackageName (), null ));
102
+ this .startActivity (intent );
103
+ }
104
+
44
105
}
0 commit comments