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

Commit

Permalink
Fix crash when creating dropbox directory
Browse files Browse the repository at this point in the history
Also improves loading screen usage for directory creation.

Fixes #76
  • Loading branch information
spacecowboy committed Feb 24, 2016
1 parent 45b73f4 commit 0a511ac
Showing 1 changed file with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v7.util.SortedList;
Expand Down Expand Up @@ -146,12 +147,7 @@ public void onLoaderReset(Loader<SortedList<DropboxAPI.Entry>> loader) {
@Override
public void onNewFolder(final String name) {
File folder = new File(mCurrentPath.path, name);

if (folderCreator == null) {
folderCreator = new FolderCreator();
}

folderCreator.execute(folder.getPath());
new FolderCreator().execute(folder.getPath());
}

@Override
Expand Down Expand Up @@ -305,21 +301,43 @@ protected void onReset() {
};
}

private class FolderCreator extends AsyncTask<String, Void, Void> {
/**
* Dropbox requires stuff to be done in a background thread. Refreshing has to be done on the
* UI thread however (it restarts the loader so actual work is done in the background).
*/
private class FolderCreator extends AsyncTask<String, Void, DropboxAPI.Entry> {
@Override
protected void onPreExecute() {
// Switch to progress bar before starting work
progressBar.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.INVISIBLE);
}

@Override
protected Void doInBackground(final String... paths) {
for (String path : paths) {
try {
dbApi.createFolder(path);
mCurrentPath = dbApi.metadata(path, 1, null, false, null);
refresh();
} catch (DropboxException e) {
Toast.makeText(getActivity(), R.string.nnf_create_folder_error,
Toast.LENGTH_SHORT).show();
}
protected DropboxAPI.Entry doInBackground(final String... paths) {
if (paths.length == 0) {
return null;
}

String path = paths[0];
try {
dbApi.createFolder(path);
return dbApi.metadata(path, 1, null, false, null);
} catch (DropboxException e) {
return null;
}
}

@Override
protected void onPostExecute(@Nullable DropboxAPI.Entry path) {
if (path != null) {
goToDir(path);
} else {
progressBar.setVisibility(View.INVISIBLE);
recyclerView.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), R.string.nnf_create_folder_error,
Toast.LENGTH_SHORT).show();
}
return null;
}
}
}
}

0 comments on commit 0a511ac

Please sign in to comment.