Skip to content

Instantly share code, notes, and snippets.

@ashishkudale
Created April 11, 2020 17:28
Show Gist options
  • Save ashishkudale/e6b62e9467664ffabc6840b09c3450a7 to your computer and use it in GitHub Desktop.
Save ashishkudale/e6b62e9467664ffabc6840b09c3450a7 to your computer and use it in GitHub Desktop.
import static android.Manifest.permission.CAMERA;
public class Camera extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_permission);
if (!checkPermission()) {
requestPermissionDialog();
} else {
openCamera();
}
}
//shouldShowRequestPermissionRationale method helps to determin that user had selected "Never ask again"
private void requestPermissionDialog() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !shouldShowRequestPermissionRationale(CAMERA)) {
openSettingsDialog();
} else {
showAlertDialog("Attention",
"This app requires camera permission to take photos and videos",
"Continue", "Not Now", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
requestPermission();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
}
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, PERMISSION_REQUEST_CODE);
}
private void showAlertDialog(String title, String message, String positiveButtonText, String negativeButtonText, DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener) {
AlertDialog dialog = new AlertDialog.Builder(CameraPermission.this)
.setPositiveButton(positiveButtonText, positiveListener)
.setNegativeButton(negativeButtonText, negativeListener)
.create();
if (title != null || !title.isEmpty()) {
dialog.setTitle(title);
}
dialog.setMessage(message);
dialog.show();
}
private boolean checkPermission() {
return ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED;
}
private void openCamera() {
//logic for showing camera.
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
finish();
}
}
}
private void openSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
private void openSettingsDialog() {
showAlertDialog("Attention", "It seems that user has denied camera permissions. Please click on settings and then go to Permissions > Camera and enable it. ", "Settings", "Not Now", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
openSettings();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment