private void onUrisDecoded(@Nullable List<Upload> uploads) { if (uploads == null || uploads.isEmpty()) { boolean isEmpty = mAdapter == null || mAdapter.isEmpty(); mMultiView.setViewState( isEmpty ? MultiStateView.VIEW_STATE_EMPTY : MultiStateView.VIEW_STATE_CONTENT); } else { if (mAdapter == null) { mAdapter = new UploadPhotoAdapter(getActivity(), uploads, this); mRecyclerView.setAdapter(mAdapter); } else { mAdapter.addItems(uploads); } mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT); } mPhotoUris = null; if (mListener != null) { if (mAdapter == null || mAdapter.isEmpty()) { mListener.onPhotoRemoved(0); } else { mListener.onPhotoAdded(); } } }
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mAdapter != null && !mAdapter.isEmpty()) { outState.putParcelableArrayList(KEY_SAVED_ITEMS, mAdapter.retainItems()); } }
@Nullable public ArrayList<Upload> getPhotosForUpload() { if (mAdapter != null && !mAdapter.isEmpty()) { return mAdapter.retainItems(); } return null; }
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; } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case RequestCodes.TAKE_PHOTO: if (resultCode == Activity.RESULT_OK) { if (!FileUtil.isFileValid(mTempFile)) { Snackbar.make(mMultiView, R.string.upload_camera_error, Snackbar.LENGTH_LONG).show(); return; } FileUtil.scanFile(Uri.fromFile(mTempFile), getActivity()); String fileLocation = mTempFile.getAbsolutePath(); Upload upload = new Upload(fileLocation); if (mAdapter == null) { mAdapter = new UploadPhotoAdapter(getActivity(), upload, this); mRecyclerView.setAdapter(mAdapter); } else { mAdapter.addItem(upload); } mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT); mTempFile = null; } break; case RequestCodes.SELECT_PHOTO: if (resultCode == Activity.RESULT_OK && data != null) { ClipData clipData = data.getClipData(); List<Uri> uris = null; // Check if we have multiple images if (clipData != null) { int size = clipData.getItemCount(); uris = new ArrayList<>(size); for (int i = 0; i < size; i++) { uris.add(clipData.getItemAt(i).getUri()); } } else if (data.getData() != null) { // If not multiple images, then only one was selected uris = new ArrayList<>(1); uris.add(data.getData()); } if (uris != null && !uris.isEmpty()) { mMultiView.setViewState(MultiStateView.VIEW_STATE_LOADING); new DecodeImagesTask(this).execute(uris); } else { Snackbar.make(mMultiView, R.string.error_generic, Snackbar.LENGTH_LONG).show(); } } break; case RequestCodes.UPLOAD_EDIT: if (resultCode == Activity.RESULT_OK && data != null) { final Upload upload; if (data.hasExtra(UploadEditActivity.KEY_UPDATED_UPLOAD)) { upload = data.getParcelableExtra(UploadEditActivity.KEY_UPDATED_UPLOAD); if (upload != null) { mAdapter.updateItem(upload); } } else if (data.hasExtra(UploadEditActivity.KEY_UPDATED_DELETED)) { upload = data.getParcelableExtra(UploadEditActivity.KEY_UPDATED_DELETED); if (upload != null) { final int itemIndex = mAdapter.indexOf(upload); if (itemIndex > -1) { mAdapter.removeItem(itemIndex); if (mListener != null) mListener.onPhotoRemoved(mAdapter.getItemCount()); if (mAdapter.isEmpty()) mMultiView.setViewState(MultiStateView.VIEW_STATE_EMPTY); Snackbar.make(mMultiView, R.string.upload_photo_removed, Snackbar.LENGTH_LONG) .setAction( R.string.undo, new View.OnClickListener() { @Override public void onClick(View v) { mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT); mAdapter.addItem(upload, itemIndex); if (mListener != null) mListener.onPhotoAdded(); } }) .show(); } } } } break; } super.onActivityResult(requestCode, resultCode, data); }