@Override protected List<Upload> doInBackground(List<Uri>... params) { if (params != null && params[0] != null && !params[0].isEmpty()) { Activity activity = mFragment.get().getActivity(); ContentResolver resolver = activity.getContentResolver(); photoUris = params[0]; List<Upload> uploads = new ArrayList<>(photoUris.size()); boolean hasReadPermission = PermissionUtils.hasPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE); for (Uri uri : photoUris) { try { // Check if the URI is a file as we will need read permissions for it if (!hasReadPermission && "file".equals(uri.getScheme())) { LogUtil.v("DecodeImageTask", "Received a File URI and don't have permissions"); needsPermission = true; return null; } File file = FileUtil.createFile(uri, resolver); if (FileUtil.isFileValid(file)) uploads.add(new Upload(file.getAbsolutePath())); } catch (Throwable ex) { LogUtil.e("DecodeImageTask", "Unable to decode image", ex); } } return uploads; } return null; }
/** * Checks if the needed permissions are available * * @return If the permissions are available. If false is returned, the necessary prompts will be * shown */ private boolean checkWritePermissions() { @PermissionUtils.PermissionLevel int permissionLevel = PermissionUtils.getPermissionLevel(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); switch (permissionLevel) { case PermissionUtils.PERMISSION_AVAILABLE: LogUtil.v(TAG, "Permissions available"); return true; case PermissionUtils.PERMISSION_DENIED: new AlertDialog.Builder(getActivity(), app.getImgurTheme().getAlertDialogTheme()) .setTitle(R.string.permission_title) .setMessage(R.string.permission_rationale_upload) .setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Snackbar.make(mMultiView, R.string.permission_denied, Snackbar.LENGTH_LONG) .show(); } }) .setPositiveButton( R.string.okay, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ActivityCompat.requestPermissions( getActivity(), new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, RequestCodes.REQUEST_PERMISSION_WRITE); } }) .setOnCancelListener( new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialogInterface) { Snackbar.make(mMultiView, R.string.permission_denied, Snackbar.LENGTH_LONG) .show(); } }) .show(); break; case PermissionUtils.PERMISSION_NEVER_ASKED: default: LogUtil.v(TAG, "Prompting for permissions"); ActivityCompat.requestPermissions( getActivity(), new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, RequestCodes.REQUEST_PERMISSION_WRITE); break; } return false; }
@Override public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case RequestCodes.REQUEST_PERMISSION_WRITE: if (PermissionUtils.verifyPermissions(grantResults)) { startCamera(); } else { Snackbar.make(mMultiView, R.string.permission_denied, Snackbar.LENGTH_LONG).show(); } break; case RequestCodes.REQUEST_PERMISSION_READ: if (PermissionUtils.verifyPermissions(grantResults)) { if (mPhotoUris != null) new DecodeImagesTask(this).execute(mPhotoUris); } else { Snackbar.make(mMultiView, R.string.permission_denied, Snackbar.LENGTH_LONG).show(); } break; } }
private void onNeedsReadPermission(List<Uri> photoUris) { mMultiView.setViewState( mAdapter != null && !mAdapter.isEmpty() ? MultiStateView.VIEW_STATE_CONTENT : MultiStateView.VIEW_STATE_EMPTY); mPhotoUris = photoUris; int level = PermissionUtils.getPermissionLevel(this, Manifest.permission.READ_EXTERNAL_STORAGE); switch (level) { // Should never have gotten here if its available case PermissionUtils.PERMISSION_AVAILABLE: new DecodeImagesTask(this).doInBackground(mPhotoUris); break; case PermissionUtils.PERMISSION_DENIED: int uriSize = photoUris != null ? photoUris.size() : 0; new AlertDialog.Builder(getActivity(), theme.getAlertDialogTheme()) .setTitle(R.string.permission_title) .setMessage( getResources() .getQuantityString(R.plurals.permission_rational_file_access, uriSize, uriSize)) .setNegativeButton(R.string.cancel, null) .setPositiveButton( R.string.okay, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions( getActivity(), new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, RequestCodes.REQUEST_PERMISSION_READ); } }) .show(); break; case PermissionUtils.PERMISSION_NEVER_ASKED: ActivityCompat.requestPermissions( getActivity(), new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, RequestCodes.REQUEST_PERMISSION_READ); break; } }