Search in sources :

Example 1 with OnImagePickerPermissionsCallback

use of com.imagepicker.permissions.OnImagePickerPermissionsCallback in project react-native-image-picker by marcshilling.

the class ImagePickerModule method permissionsCheck.

private boolean permissionsCheck(@NonNull final Activity activity, @NonNull final Callback callback, @NonNull final int requestCode) {
    final int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    final int cameraPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
    final boolean permissionsGrated = writePermission == PackageManager.PERMISSION_GRANTED && cameraPermission == PackageManager.PERMISSION_GRANTED;
    if (!permissionsGrated) {
        final Boolean dontAskAgain = ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) && ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA);
        if (dontAskAgain) {
            final AlertDialog dialog = PermissionUtils.explainingDialog(this, options, new PermissionUtils.OnExplainingPermissionCallback() {

                @Override
                public void onCancel(WeakReference<ImagePickerModule> moduleInstance, DialogInterface dialogInterface) {
                    final ImagePickerModule module = moduleInstance.get();
                    if (module == null) {
                        return;
                    }
                    module.doOnCancel();
                }

                @Override
                public void onReTry(WeakReference<ImagePickerModule> moduleInstance, DialogInterface dialogInterface) {
                    final ImagePickerModule module = moduleInstance.get();
                    if (module == null) {
                        return;
                    }
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", module.getContext().getPackageName(), null);
                    intent.setData(uri);
                    final Activity innerActivity = module.getActivity();
                    if (innerActivity == null) {
                        return;
                    }
                    innerActivity.startActivityForResult(intent, 1);
                }
            });
            dialog.show();
            //        responseHelper.invokeError(callback, "Permissions weren't granted");
            return false;
        } else {
            String[] PERMISSIONS = { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA };
            if (activity instanceof ReactActivity) {
                ((ReactActivity) activity).requestPermissions(PERMISSIONS, requestCode, listener);
            } else if (activity instanceof OnImagePickerPermissionsCallback) {
                ((OnImagePickerPermissionsCallback) activity).setPermissionListener(listener);
                ActivityCompat.requestPermissions(activity, PERMISSIONS, requestCode);
            } else {
                final String errorDescription = new StringBuilder(activity.getClass().getSimpleName()).append(" must implement ").append(OnImagePickerPermissionsCallback.class.getSimpleName()).toString();
                throw new UnsupportedOperationException(errorDescription);
            }
            return false;
        }
    }
    return true;
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) ReactActivity(com.facebook.react.ReactActivity) Activity(android.app.Activity) Intent(android.content.Intent) ReactActivity(com.facebook.react.ReactActivity) Uri(android.net.Uri) OnImagePickerPermissionsCallback(com.imagepicker.permissions.OnImagePickerPermissionsCallback) PermissionUtils(com.imagepicker.permissions.PermissionUtils)

Aggregations

Activity (android.app.Activity)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 AlertDialog (android.support.v7.app.AlertDialog)1 ReactActivity (com.facebook.react.ReactActivity)1 OnImagePickerPermissionsCallback (com.imagepicker.permissions.OnImagePickerPermissionsCallback)1 PermissionUtils (com.imagepicker.permissions.PermissionUtils)1