Skip to content

Commit

Permalink
Prevent camera permission from being requested twice
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed May 9, 2023
1 parent dabbf80 commit e38f72f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion debug/apps/arscene/src/main/java/arscene/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

public class MainActivity extends AppCompatActivity {
private static final int CAMERA_PERMISSION_CODE = 0;
private static boolean CAMERA_PERMISSION_REQUESTED = false;
private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;
private static final String CAMERA_PERMISSION_MESSAGE = "Camera permission is needed to use AR";

Expand Down Expand Up @@ -58,6 +59,8 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
if (sketch != null) {
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

CAMERA_PERMISSION_REQUESTED = false;
}

@Override
Expand Down Expand Up @@ -89,7 +92,10 @@ private boolean hasCameraPermission() {
}

private void requestCameraPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
if (!CAMERA_PERMISSION_REQUESTED) {
CAMERA_PERMISSION_REQUESTED = true;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
}
}

private boolean shouldShowRequestPermissionRationale() {
Expand Down
7 changes: 6 additions & 1 deletion mode/templates/ARActivity.java.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import processing.core.PApplet;

public class MainActivity extends AppCompatActivity {
private static final int CAMERA_PERMISSION_CODE = 0;
private static boolean CAMERA_PERMISSION_REQUESTED = false;
private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;
private static final String CAMERA_PERMISSION_MESSAGE = "Camera permission is needed to use AR";

Expand Down Expand Up @@ -57,6 +58,7 @@ public class MainActivity extends AppCompatActivity {
if (sketch != null) {
sketch.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
CAMERA_PERMISSION_REQUESTED = false;
}

@Override
Expand Down Expand Up @@ -86,7 +88,10 @@ public class MainActivity extends AppCompatActivity {
}

private void requestCameraPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
if (!CAMERA_PERMISSION_REQUESTED) {
CAMERA_PERMISSION_REQUESTED = true;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
}
}

private boolean shouldShowRequestPermissionRationale() {
Expand Down

0 comments on commit e38f72f

Please sign in to comment.