diff options
| author | zeapo <mohamed@zenadi.com> | 2014-12-16 23:25:55 +0100 |
|---|---|---|
| committer | zeapo <mohamed@zenadi.com> | 2014-12-16 23:27:30 +0100 |
| commit | 47bb25adcbded5e05d4be60bf4eba3bf380f559c (patch) | |
| tree | f80fc721969da486d6f7b7720296a18f3957b435 /app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java | |
| parent | f154dc30a4113665c2187be313477a236a790dc7 (diff) | |
| download | HardenedPass-47bb25adcbded5e05d4be60bf4eba3bf380f559c.tar.gz HardenedPass-47bb25adcbded5e05d4be60bf4eba3bf380f559c.zip | |
fix the the git fields and a small refactorv1.2.0.10
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java new file mode 100644 index 00000000..add18278 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java @@ -0,0 +1,96 @@ +package com.zeapo.pwdstore.git; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.os.AsyncTask; + +import com.zeapo.pwdstore.PasswordStore; +import com.zeapo.pwdstore.R; +import com.zeapo.pwdstore.utils.PasswordRepository; + +import org.apache.commons.io.FileUtils; +import org.eclipse.jgit.api.CloneCommand; +import org.eclipse.jgit.api.GitCommand; + + +public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> { + private Activity activity; + private boolean finishOnEnd; + private boolean refreshListOnEnd; + private ProgressDialog dialog; + private Class operation; + + public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, Class operation) { + this.activity = activity; + this.finishOnEnd = finishOnEnd; + this.refreshListOnEnd = refreshListOnEnd; + this.operation = operation; + + dialog = new ProgressDialog(this.activity); + } + + protected void onPreExecute() { + this.dialog.setMessage(activity.getResources().getString(R.string.running_dialog_text)); + this.dialog.setCancelable(false); + this.dialog.show(); + } + + @Override + protected String doInBackground(GitCommand... cmd) { + int count = cmd.length; + for (int i = 0; i < count; i++) { + try { + cmd[i].call(); + } catch (Exception e) { + e.printStackTrace(); + return e.getMessage(); + } + } + return ""; + } + + protected void onPostExecute(String result) { + this.dialog.dismiss(); + + if (result == null) + result = "Unexpected error"; + + if (!result.isEmpty()) { + new AlertDialog.Builder(activity). + setTitle(activity.getResources().getString(R.string.jgit_error_dialog_title)). + setMessage(activity.getResources().getString(R.string.jgit_error_dialog_text) + result). + setPositiveButton(activity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + if (operation.equals(CloneCommand.class)) { + // if we were unable to finish the job + try { + FileUtils.deleteDirectory(PasswordRepository.getWorkTree()); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + activity.setResult(Activity.RESULT_CANCELED); + activity.finish(); + } + } + }).show(); + + } else { + if (finishOnEnd) { + this.activity.setResult(Activity.RESULT_OK); + this.activity.finish(); + } + + if (refreshListOnEnd) { + try { + ((PasswordStore) this.activity).updateListAdapter(); + } catch (ClassCastException e) { + // oups, mistake + } + } + } + } +} |
