took off duplicate code CloneAsyncTask into GitAsyncTask
This commit is contained in:
parent
08ce874009
commit
83b893bc8c
4 changed files with 60 additions and 134 deletions
|
@ -1,10 +1,15 @@
|
||||||
package com.zeapo.pwdstore;
|
package com.zeapo.pwdstore;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.zeapo.pwdstore.utils.PasswordRepository;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.eclipse.jgit.api.CloneCommand;
|
import org.eclipse.jgit.api.CloneCommand;
|
||||||
import org.eclipse.jgit.api.Git;
|
import org.eclipse.jgit.api.Git;
|
||||||
import org.eclipse.jgit.api.GitCommand;
|
import org.eclipse.jgit.api.GitCommand;
|
||||||
|
@ -13,16 +18,18 @@ import org.eclipse.jgit.api.errors.JGitInternalException;
|
||||||
import org.eclipse.jgit.api.errors.TransportException;
|
import org.eclipse.jgit.api.errors.TransportException;
|
||||||
|
|
||||||
|
|
||||||
public class GitAsyncTask extends AsyncTask<GitCommand, Integer, Integer> {
|
public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
|
||||||
private Activity activity;
|
private Activity activity;
|
||||||
private boolean finishOnEnd;
|
private boolean finishOnEnd;
|
||||||
private boolean refreshListOnEnd;
|
private boolean refreshListOnEnd;
|
||||||
private ProgressDialog dialog;
|
private ProgressDialog dialog;
|
||||||
|
private Class operation;
|
||||||
|
|
||||||
public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd) {
|
public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, Class operation) {
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
this.finishOnEnd = finishOnEnd;
|
this.finishOnEnd = finishOnEnd;
|
||||||
this.refreshListOnEnd = refreshListOnEnd;
|
this.refreshListOnEnd = refreshListOnEnd;
|
||||||
|
this.operation = operation;
|
||||||
|
|
||||||
dialog = new ProgressDialog(this.activity);
|
dialog = new ProgressDialog(this.activity);
|
||||||
}
|
}
|
||||||
|
@ -34,43 +41,55 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, Integer> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Integer doInBackground(GitCommand... cmd) {
|
protected String doInBackground(GitCommand... cmd) {
|
||||||
int count = cmd.length;
|
int count = cmd.length;
|
||||||
Integer totalSize = 0;
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
try {
|
try {
|
||||||
cmd[i].call();
|
cmd[i].call();
|
||||||
} catch (JGitInternalException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return -99;
|
|
||||||
} catch (InvalidRemoteException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return -1;
|
|
||||||
} catch (TransportException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return -2;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return -98;
|
return e.getMessage();
|
||||||
}
|
}
|
||||||
totalSize++;
|
|
||||||
}
|
}
|
||||||
return totalSize;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onPostExecute(Integer result) {
|
protected void onPostExecute(String result) {
|
||||||
Log.i("GIT_ASYNC", result + "");
|
|
||||||
this.dialog.dismiss();
|
this.dialog.dismiss();
|
||||||
if (finishOnEnd) {
|
|
||||||
this.activity.setResult(Activity.RESULT_OK);
|
|
||||||
this.activity.finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (refreshListOnEnd) {
|
if (!result.isEmpty()) {
|
||||||
try {
|
new AlertDialog.Builder(activity).
|
||||||
((PasswordStore) this.activity).refreshListAdapter();
|
setTitle("Internal exception occurred").
|
||||||
} catch (ClassCastException e){
|
setMessage("Message from jgit:\n" + result).
|
||||||
// oups, mistake
|
setPositiveButton("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).refreshListAdapter();
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
// oups, mistake
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,8 @@ import org.eclipse.jgit.api.Git;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.eclipse.jgit.api.GitCommand;
|
import org.eclipse.jgit.api.GitCommand;
|
||||||
|
import org.eclipse.jgit.api.PullCommand;
|
||||||
|
import org.eclipse.jgit.api.PushCommand;
|
||||||
import org.eclipse.jgit.api.errors.InvalidRemoteException;
|
import org.eclipse.jgit.api.errors.InvalidRemoteException;
|
||||||
import org.eclipse.jgit.api.errors.JGitInternalException;
|
import org.eclipse.jgit.api.errors.JGitInternalException;
|
||||||
import org.eclipse.jgit.api.errors.TransportException;
|
import org.eclipse.jgit.api.errors.TransportException;
|
||||||
|
@ -200,99 +202,6 @@ public class GitHandler extends Activity {
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The clone process has to be on a different thread than the main one */
|
|
||||||
private class CloneTask extends AsyncTask<CloneCommand, Integer, Integer> {
|
|
||||||
private ProgressDialog dialog;
|
|
||||||
|
|
||||||
public CloneTask(Activity activity) {
|
|
||||||
context = activity;
|
|
||||||
dialog = new ProgressDialog(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void onPreExecute() {
|
|
||||||
this.dialog.setMessage("Cloning...");
|
|
||||||
this.dialog.setCancelable(false);
|
|
||||||
// TODO: Handle a dialog leak when there is no error
|
|
||||||
this.dialog.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void onPostExecute(Integer result) {
|
|
||||||
switch (result) {
|
|
||||||
case -1:
|
|
||||||
new AlertDialog.Builder(activity).
|
|
||||||
setTitle("Please check that the repository path is correct.").
|
|
||||||
setMessage("Did you forget to specify the path after the hostname?").
|
|
||||||
setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialogInterface, int i) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}).show();
|
|
||||||
break;
|
|
||||||
case -2:
|
|
||||||
new AlertDialog.Builder(activity).
|
|
||||||
setTitle("Communication error").
|
|
||||||
setMessage("JGit said that the server didn't like our request. Either an authentication issue or the host is not reachable. Check the debug messages.").
|
|
||||||
setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialogInterface, int i) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}).show();
|
|
||||||
break;
|
|
||||||
case -99:
|
|
||||||
new AlertDialog.Builder(activity).
|
|
||||||
setTitle("JGit raised an internal exception").
|
|
||||||
setMessage("OUPS, JGit didn't like what you did... Check that you provided it with a correct URI. Check also debug messages.").
|
|
||||||
setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialogInterface, int i) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}).show();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
this.dialog.dismiss();
|
|
||||||
setResult(RESULT_OK);
|
|
||||||
finish();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.dialog.dismiss();
|
|
||||||
|
|
||||||
// if we were unable to finish the job
|
|
||||||
try {
|
|
||||||
FileUtils.deleteDirectory(localDir);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected Integer doInBackground(CloneCommand... cmd) {
|
|
||||||
int count = cmd.length;
|
|
||||||
Integer totalSize = 0;
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
try {
|
|
||||||
cmd[i].call();
|
|
||||||
} catch (JGitInternalException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return -99;
|
|
||||||
} catch (InvalidRemoteException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return -1;
|
|
||||||
} catch (TransportException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return -2;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return -99;
|
|
||||||
}
|
|
||||||
totalSize++;
|
|
||||||
}
|
|
||||||
return totalSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected class GitConfigSessionFactory extends JschConfigSessionFactory {
|
protected class GitConfigSessionFactory extends JschConfigSessionFactory {
|
||||||
|
|
||||||
protected void configure(OpenSshConfig.Host hc, Session session) {
|
protected void configure(OpenSshConfig.Host hc, Session session) {
|
||||||
|
@ -452,7 +361,7 @@ public class GitHandler extends Activity {
|
||||||
setDirectory(localDir).
|
setDirectory(localDir).
|
||||||
setURI(hostname);
|
setURI(hostname);
|
||||||
|
|
||||||
new CloneTask(activity).execute(cmd);
|
new GitAsyncTask(activity, true, false, CloneCommand.class).execute(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pullOperation(UsernamePasswordCredentialsProvider provider) {
|
public void pullOperation(UsernamePasswordCredentialsProvider provider) {
|
||||||
|
@ -500,7 +409,7 @@ public class GitHandler extends Activity {
|
||||||
.setRebase(true)
|
.setRebase(true)
|
||||||
.setRemote("origin");
|
.setRemote("origin");
|
||||||
|
|
||||||
new GitAsyncTask(activity, true, false).execute(cmd);
|
new GitAsyncTask(activity, true, false, PullCommand.class).execute(cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -550,7 +459,7 @@ public class GitHandler extends Activity {
|
||||||
.setRemote("origin");
|
.setRemote("origin");
|
||||||
|
|
||||||
|
|
||||||
new GitAsyncTask(activity, true, false).execute(cmd);
|
new GitAsyncTask(activity, true, false, PushCommand.class).execute(cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -661,14 +570,7 @@ public class GitHandler extends Activity {
|
||||||
}
|
}
|
||||||
}).show();
|
}).show();
|
||||||
} else {
|
} else {
|
||||||
CloneCommand cmd = Git.cloneRepository()
|
// BUG: we do not support HTTP yet...
|
||||||
.setDirectory(localDir)
|
|
||||||
.setURI(hostname)
|
|
||||||
.setBare(false)
|
|
||||||
.setNoCheckout(false)
|
|
||||||
.setCloneAllBranches(true);
|
|
||||||
|
|
||||||
new CloneTask(activity).execute(cmd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ import com.zeapo.pwdstore.utils.PasswordItem;
|
||||||
import com.zeapo.pwdstore.utils.PasswordRepository;
|
import com.zeapo.pwdstore.utils.PasswordRepository;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.eclipse.jgit.api.CommitCommand;
|
||||||
import org.eclipse.jgit.api.Git;
|
import org.eclipse.jgit.api.Git;
|
||||||
import org.eclipse.jgit.revwalk.RevCommit;
|
import org.eclipse.jgit.revwalk.RevCommit;
|
||||||
import org.eclipse.jgit.transport.JschConfigSessionFactory;
|
import org.eclipse.jgit.transport.JschConfigSessionFactory;
|
||||||
|
@ -323,7 +324,7 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI
|
||||||
|
|
||||||
setResult(RESULT_CANCELED);
|
setResult(RESULT_CANCELED);
|
||||||
Git git = new Git(PasswordRepository.getRepository(new File("")));
|
Git git = new Git(PasswordRepository.getRepository(new File("")));
|
||||||
GitAsyncTask tasks = new GitAsyncTask(activity, false, true);
|
GitAsyncTask tasks = new GitAsyncTask(activity, false, true, CommitCommand.class);
|
||||||
System.out.println(tasks);
|
System.out.println(tasks);
|
||||||
tasks.execute(
|
tasks.execute(
|
||||||
git.rm().addFilepattern(path.replace(PasswordRepository.getWorkTree() + "/", "")),
|
git.rm().addFilepattern(path.replace(PasswordRepository.getWorkTree() + "/", "")),
|
||||||
|
@ -366,7 +367,7 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI
|
||||||
switch (requestCode) {
|
switch (requestCode) {
|
||||||
case PgpHandler.REQUEST_CODE_ENCRYPT :
|
case PgpHandler.REQUEST_CODE_ENCRYPT :
|
||||||
Git git = new Git(PasswordRepository.getRepository(new File("")));
|
Git git = new Git(PasswordRepository.getRepository(new File("")));
|
||||||
GitAsyncTask tasks = new GitAsyncTask(this, false, false);
|
GitAsyncTask tasks = new GitAsyncTask(this, false, false, CommitCommand.class);
|
||||||
tasks.execute(
|
tasks.execute(
|
||||||
git.add().addFilepattern("."),
|
git.add().addFilepattern("."),
|
||||||
git.commit().setMessage("[ANDROID PwdStore] Add " + data.getExtras().getString("NAME") + " from store.")
|
git.commit().setMessage("[ANDROID PwdStore] Add " + data.getExtras().getString("NAME") + " from store.")
|
||||||
|
|
|
@ -119,7 +119,11 @@ public class PgpHandler extends Activity implements OpenPgpServiceConnection.OnB
|
||||||
public void onStop(){
|
public void onStop(){
|
||||||
super.onStop();
|
super.onStop();
|
||||||
if (this.mServiceConnection.isBound())
|
if (this.mServiceConnection.isBound())
|
||||||
this.mServiceConnection.unbindFromService();
|
try {
|
||||||
|
this.mServiceConnection.unbindFromService();
|
||||||
|
} catch (Exception e){
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue