1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package com.zeapo.pwdstore.git;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.design.widget.Snackbar;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.widget.Toast;
import com.zeapo.pwdstore.PasswordStore;
import com.zeapo.pwdstore.R;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.StatusCommand;
public class GitAsyncTask extends AsyncTask<GitCommand, String, String> {
private Activity activity;
private boolean lockAndFinishActivity;
private boolean refreshListOnEnd;
private ProgressDialog dialog;
private GitOperation operation;
private Snackbar snack;
public GitAsyncTask(Activity activity, boolean lockAndFinishActivity, boolean refreshListOnEnd, GitOperation operation) {
this.activity = activity;
this.lockAndFinishActivity = lockAndFinishActivity;
this.refreshListOnEnd = refreshListOnEnd;
this.operation = operation;
this.dialog = new ProgressDialog(this.activity);
}
protected void onPreExecute() {
if (lockAndFinishActivity) {
final Spanned message = Html.fromHtml(String.format("Running %s", operation.getClass().getSimpleName()));
this.dialog.setMessage(message);
this.dialog.setCancelable(false);
this.dialog.show();
} else {
final Spanned message = Html.fromHtml(String.format("<font color=\"#ffffff\">Running %s</font>", operation.getClass().getSimpleName()));
Toast.makeText(activity.getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
}
protected void onProgressUpdate(String... progress) {
if (lockAndFinishActivity) {
final Spanned message = Html.fromHtml(String.format("Running jgit command: <strong>%s</strong>", progress[0]));
this.dialog.setMessage(message);
} else {
final Spanned message = Html.fromHtml(String.format("<font color=\"#ffffff\">Running jgit command: <strong>%s</strong></font>", progress[0]));
Toast.makeText(activity.getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
}
@Override
protected String doInBackground(GitCommand... commands) {
Integer nbChanges = null;
for (GitCommand command : commands) {
Log.d("doInBackground", "Executing the command <" + command.toString() + ">");
try {
if (command instanceof StatusCommand) {
// in case we have changes, we want to keep track of it
org.eclipse.jgit.api.Status status = ((StatusCommand) command).call();
nbChanges = status.getChanged().size() + status.getMissing().size();
} else if (command instanceof CommitCommand) {
// the previous status will eventually be used to avoid a commit
if (nbChanges == null || nbChanges > 0)
command.call();
} else {
command.call();
}
String opName = command.getClass().getSimpleName();
publishProgress(opName);
} catch (Exception e) {
e.printStackTrace();
return e.getMessage() + "\nCaused by:\n" + e.getCause();
}
}
return "";
}
protected void onPostExecute(String result) {
if (this.dialog != null)
try {
this.dialog.dismiss();
} catch (Exception e) {
// ignore
}
if (result == null)
result = "Unexpected error";
if (!result.isEmpty()) {
this.operation.onTaskEnded(result);
} else {
if (lockAndFinishActivity) {
this.activity.setResult(Activity.RESULT_OK);
this.activity.finish();
}
if (refreshListOnEnd) {
try {
((PasswordStore) this.activity).updateListAdapter();
} catch (ClassCastException e) {
// oups, mistake
}
}
}
}
}
|