int REQUEST_SHOWCAMERA = 0;
private static final String[] PERMISSION_SHOWCAMERA = new String[] {"android.permission.CAMERA"};
private MainActivityPermissionsDispatcher() {
}
static void showCameraWithCheck(MainActivity target) {
if (PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCAMERA)) {
target.showCamera();
} else {
if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SHOWCAMERA)) {
target.showRationaleForCamera(new ShowCameraPermissionRequest(target));
} else {
ActivityCompat.requestPermissions(target, PERMISSION_SHOWCAMERA, REQUEST_SHOWCAMERA);
}
}
}
static void onRequestPermissionsResult(MainActivity target, int requestCode, int[] grantResults) {
switch (requestCode) {
case REQUEST_SHOWCAMERA:
if (PermissionUtils.verifyPermissions(grantResults)) {
target.showCamera();
} else {
target.onCameraDenied();
}
break;
default:
break;
}
}
private static final class ShowCameraPermissionRequest implements PermissionRequest {
private final WeakReference<MainActivity> weakTarget;
private ShowCameraPermissionRequest(MainActivity target) {
this.weakTarget = new WeakReference<>(target);
}
@Override
public void proceed() {
MainActivity target = weakTarget.get();
if (target == null) return;
ActivityCompat.requestPermissions(target, PERMISSION_SHOWCAMERA, REQUEST_SHOWCAMERA);
}
@Override
public void cancel() {
MainActivity target = weakTarget.get();
if (target == null) return;
target.onCameraDenied();
}
}
} Generated code