1
1
package @@package_name@@;
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
+ private static final String CAMERA_PERMISSION_MESSAGE = "Camera permission is needed to use AR";
24
+
14
25
private PApplet sketch;
15
26
16
27
@Override
@@ -28,18 +39,63 @@ public class MainActivity extends AppCompatActivity {
28
39
}
29
40
30
41
@Override
31
- public void onRequestPermissionsResult(int requestCode,
32
- String permissions[],
33
- int[] grantResults) {
42
+ protected void onResume() {
43
+ super.onResume();
44
+ if (!hasCameraPermission()) requestCameraPermission();
45
+ }
46
+
47
+ @Override
48
+ public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
49
+ if (!hasCameraPermission()) {
50
+ Toast.makeText(this, CAMERA_PERMISSION_MESSAGE, Toast.LENGTH_LONG).show();
51
+ if (!shouldShowRequestPermissionRationale()) {
52
+ launchPermissionSettings();
53
+ }
54
+ finish();
55
+ }
34
56
if (sketch != null) {
35
57
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
36
58
}
37
- }
38
-
59
+ }
60
+
39
61
@Override
40
62
public void onNewIntent(Intent intent) {
41
63
if (sketch != null) {
42
64
sketch.onNewIntent(intent);
65
+ }
66
+ }
67
+
68
+ @Override
69
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
70
+ if (sketch != null) {
71
+ sketch.onActivityResult(requestCode, resultCode, data);
43
72
}
73
+ }
74
+
75
+ @Override
76
+ public void onBackPressed() {
77
+ if (sketch != null) {
78
+ sketch.onBackPressed();
79
+ }
80
+ }
81
+
82
+ private boolean hasCameraPermission() {
83
+ int res = ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION);
84
+ return res == PackageManager.PERMISSION_GRANTED;
85
+ }
86
+
87
+ private void requestCameraPermission() {
88
+ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
89
+ }
90
+
91
+ private boolean shouldShowRequestPermissionRationale() {
92
+ return ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA_PERMISSION);
44
93
}
94
+
95
+ private void launchPermissionSettings() {
96
+ Intent intent = new Intent();
97
+ intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
98
+ intent.setData(Uri.fromParts("package", this.getPackageName(), null));
99
+ this.startActivity(intent);
100
+ }
45
101
}
0 commit comments