Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Pass path into permission and refresh handlers
Browse files Browse the repository at this point in the history
This allows for better handling in case of denied/missing permissions,
as well the ability to request more fine-grained permissions.

Fixes #85
Fixes $84
  • Loading branch information
spacecowboy committed May 29, 2016
1 parent 4080ad6 commit c9d7035
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,13 @@ public void onActivityCreated(Bundle savedInstanceState) {
}
}
}

// If still null
if (mCurrentPath == null) {
mCurrentPath = getRoot();
}
}

refresh();
// If still null
if (mCurrentPath == null) {
mCurrentPath = getRoot();
}
refresh(mCurrentPath);
}

@Override
Expand Down Expand Up @@ -372,33 +371,39 @@ public void onDetach() {
* if permissions are granted and requests them if necessary. See hasPermission()
* and handlePermission(). By default, these methods do nothing. Override them if
* you need to request permissions at runtime.
*
* @param nextPath path to list files for
*/
protected void refresh() {
if (hasPermission()) {
protected void refresh(@NonNull T nextPath) {
if (hasPermission(nextPath)) {
mCurrentPath = nextPath;
isLoading = true;
getLoaderManager()
.restartLoader(0, null, AbstractFilePickerFragment.this);
} else {
handlePermission();
handlePermission(nextPath);
}
}

/**
* If permission has not been granted yet, this method should request it.
* <p/>
* Override only if you need to request a permission.
*
* @param path The path for which permission should be requested
*/
protected void handlePermission() {
protected void handlePermission(@NonNull T path) {
// Nothing to do by default
}

/**
* If your implementation needs to request a specific permission to function, check if it
* has been granted here. You should probably also override handlePermission() to request it.
*
* @param path the path for which permissions should be checked
* @return true if permission has been granted, false otherwise.
*/
protected boolean hasPermission() {
protected boolean hasPermission(@NonNull T path) {
// Nothing to request by default
return true;
}
Expand Down Expand Up @@ -573,10 +578,9 @@ public void onClickDir(@NonNull View view, @NonNull DirViewHolder viewHolder) {
*/
public void goToDir(@NonNull T file) {
if (!isLoading) {
mCurrentPath = file;
mCheckedItems.clear();
mCheckedVisibleViewHolders.clear();
refresh();
refresh(file);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class FilePickerFragment extends AbstractFilePickerFragment<File> {

protected static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
protected boolean showHiddenItems = false;
private File mRequestedPath = null;

public FilePickerFragment() {
}
Expand Down Expand Up @@ -55,7 +56,7 @@ public boolean areHiddenItemsShown(){
* @return true if app has been granted permission to write to the SD-card.
*/
@Override
protected boolean hasPermission() {
protected boolean hasPermission(@NonNull File path) {
return PackageManager.PERMISSION_GRANTED ==
ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE);
Expand All @@ -65,13 +66,14 @@ protected boolean hasPermission() {
* Request permission to write to the SD-card.
*/
@Override
protected void handlePermission() {
protected void handlePermission(@NonNull File path) {
// Should we show an explanation?
// if (shouldShowRequestPermissionRationale(
// Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Explain to the user why we need permission
// }

mRequestedPath = path;
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
Expand All @@ -97,7 +99,9 @@ public void onRequestPermissionsResult(int requestCode,
} else { // if (requestCode == PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
// Do refresh
refresh();
if (mRequestedPath != null) {
refresh(mRequestedPath);
}
} else {
Toast.makeText(getContext(), R.string.nnf_permission_external_write_denied,
Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -303,8 +307,7 @@ public void onNewFolder(@NonNull final String name) {
File folder = new File(mCurrentPath, name);

if (folder.mkdir()) {
mCurrentPath = folder;
refresh();
refresh(folder);
} else {
Toast.makeText(getActivity(), R.string.nnf_create_folder_error,
Toast.LENGTH_SHORT).show();
Expand All @@ -322,7 +325,7 @@ public void onNewFolder(@NonNull final String name) {
* @return True if item should be added to the list, false otherwise
*/
protected boolean isItemVisible(final File file) {
if(!showHiddenItems && file.isHidden()){
if (!showHiddenItems && file.isHidden()) {
return false;
}
return (isDir(file) || (mode == MODE_FILE || mode == MODE_FILE_AND_DIR));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class DropboxFilePickerFragment
extends AbstractFilePickerFragment<DropboxAPI.Entry> {

private final DropboxAPI<AndroidAuthSession> dbApi;
private FolderCreator folderCreator;
private ProgressBar progressBar;
private RecyclerView recyclerView;

Expand Down Expand Up @@ -114,10 +113,12 @@ public void onClick(final View v) {

/**
* If we are loading, then hide the list and show the progress bar instead.
*
* @param nextPath path to list files for
*/
@Override
protected void refresh() {
super.refresh();
protected void refresh(DropboxAPI.Entry nextPath) {
super.refresh(nextPath);
if (isLoading) {
progressBar.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.INVISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ protected FtpFile doInBackground(String... names) {
@Override
protected void onPostExecute(FtpFile folder) {
if (folder != null) {
mCurrentPath = folder;
refresh();
refresh(folder);
} else {
Toast.makeText(getContext(), R.string.nnf_create_folder_error, Toast.LENGTH_SHORT).show();
}
Expand Down

0 comments on commit c9d7035

Please sign in to comment.