diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/git')
6 files changed, 151 insertions, 67 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.java index dc94ce3b..d46513fd 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.java @@ -1,7 +1,13 @@ package com.zeapo.pwdstore.git; import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +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.Git; @@ -12,7 +18,8 @@ public class CloneOperation extends GitOperation { /** * Creates a new clone operation - * @param fileDir the git working tree directory + * + * @param fileDir the git working tree directory * @param callingActivity the calling activity */ public CloneOperation(File fileDir, Activity callingActivity) { @@ -21,6 +28,7 @@ public class CloneOperation extends GitOperation { /** * Sets the command using the repository uri + * * @param uri the uri of the repository * @return the current object */ @@ -34,6 +42,7 @@ public class CloneOperation extends GitOperation { /** * sets the authentication for user/pwd scheme + * * @param username the username * @param password the password * @return the current object @@ -46,6 +55,7 @@ public class CloneOperation extends GitOperation { /** * sets the authentication for the ssh-key scheme + * * @param sshKey the ssh-key file * @param username the username * @param passphrase the passphrase @@ -58,10 +68,31 @@ public class CloneOperation extends GitOperation { } @Override - public void execute() throws Exception { + public void execute() { if (this.provider != null) { ((CloneCommand) this.command).setCredentialsProvider(this.provider); } - new GitAsyncTask(callingActivity, true, false, CloneCommand.class).execute(this.command); + new GitAsyncTask(callingActivity, true, false, this).execute(this.command); + } + + @Override + public void onTaskEnded(String result) { + new AlertDialog.Builder(callingActivity). + setTitle(callingActivity.getResources().getString(R.string.jgit_error_dialog_title)). + setMessage("Error occured during the clone operation, " + + callingActivity.getResources().getString(R.string.jgit_error_dialog_text) + + result + + "\nPlease check the FAQ for possible reasons why this error might occur."). + setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + // if we were unable to finish the job + try { + FileUtils.deleteDirectory(PasswordRepository.getWorkTree()); + } catch (Exception e) { + e.printStackTrace(); + } + } + }).show(); } } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java index b5d1c907..70d3482f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java @@ -2,16 +2,11 @@ package com.zeapo.pwdstore.git; import android.app.Activity; import android.app.ProgressDialog; -import android.content.DialogInterface; import android.os.AsyncTask; -import android.support.v7.app.AlertDialog; 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; @@ -20,9 +15,9 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> { private boolean finishOnEnd; private boolean refreshListOnEnd; private ProgressDialog dialog; - private Class operation; + private GitOperation operation; - public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, Class operation) { + public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, GitOperation operation) { this.activity = activity; this.finishOnEnd = finishOnEnd; this.refreshListOnEnd = refreshListOnEnd; @@ -63,26 +58,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> { 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(); - + this.operation.onTaskEnded(result); } else { if (finishOnEnd) { this.activity.setResult(Activity.RESULT_OK); diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java index 8ec13593..f6b05f4c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java @@ -10,6 +10,7 @@ import android.widget.EditText; import android.widget.LinearLayout; import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; import com.jcraft.jsch.KeyPair; import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.UserPreference; @@ -75,10 +76,8 @@ public abstract class GitOperation { /** * Executes the GitCommand in an async task - * - * @throws Exception */ - public abstract void execute() throws Exception; + public abstract void execute(); /** * Executes the GitCommand in an async task after creating the authentication @@ -86,9 +85,8 @@ public abstract class GitOperation { * @param connectionMode the server-connection mode * @param username the username * @param sshKey the ssh-key file - * @throws Exception */ - public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey) throws Exception { + public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey) { executeAfterAuthentication(connectionMode, username, sshKey, false); } @@ -99,9 +97,8 @@ public abstract class GitOperation { * @param username the username * @param sshKey the ssh-key file * @param showError show the passphrase edit text in red - * @throws Exception */ - private void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, final boolean showError) throws Exception { + private void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, final boolean showError) { if (connectionMode.equalsIgnoreCase("ssh-key")) { if (sshKey == null || !sshKey.exists()) { new AlertDialog.Builder(callingActivity) @@ -152,15 +149,16 @@ public abstract class GitOperation { passphrase.setError("Wrong passphrase"); } JSch jsch = new JSch(); - final KeyPair keyPair = KeyPair.load(jsch, callingActivity.getFilesDir() + "/.ssh_key"); - if (keyPair.isEncrypted()) { - new AlertDialog.Builder(callingActivity) - .setTitle(callingActivity.getResources().getString(R.string.passphrase_dialog_title)) - .setMessage(callingActivity.getResources().getString(R.string.passphrase_dialog_text)) - .setView(passphrase) - .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - try { + try { + final KeyPair keyPair = KeyPair.load(jsch, callingActivity.getFilesDir() + "/.ssh_key"); + + if (keyPair.isEncrypted()) { + new AlertDialog.Builder(callingActivity) + .setTitle(callingActivity.getResources().getString(R.string.passphrase_dialog_title)) + .setMessage(callingActivity.getResources().getString(R.string.passphrase_dialog_text)) + .setView(passphrase) + .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { if (keyPair.decrypt(passphrase.getText().toString())) { // Authenticate using the ssh-key and then execute the command setAuthentication(sshKey, username, passphrase.getText().toString()).execute(); @@ -168,18 +166,25 @@ public abstract class GitOperation { // call back the method executeAfterAuthentication(connectionMode, username, sshKey, true); } - } catch (Exception e) { - e.printStackTrace(); } + }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + // Do nothing. + } + }).show(); + } else { + setAuthentication(sshKey, username, "").execute(); + } + } catch (JSchException e) { + new AlertDialog.Builder(callingActivity) + .setTitle("Unable to open the ssh-key") + .setMessage("Please check that it was imported.") + .setPositiveButton("Ok", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { } - }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - // Do nothing. - } - }).show(); - } else { - setAuthentication(sshKey, username, "").execute(); + }).show(); } } } else { @@ -195,11 +200,7 @@ public abstract class GitOperation { .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // authenticate using the user/pwd and then execute the command - try { - setAuthentication(username, password.getText().toString()).execute(); - } catch (Exception e) { - e.printStackTrace(); - } + setAuthentication(username, password.getText().toString()).execute(); } }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { @@ -209,4 +210,20 @@ public abstract class GitOperation { }).show(); } } + + public void onTaskEnded(String result) { + new AlertDialog.Builder(callingActivity). + setTitle(callingActivity.getResources().getString(R.string.jgit_error_dialog_title)). + setMessage("Error occurred during a Git operation, " + + callingActivity.getResources().getString(R.string.jgit_error_dialog_text) + + result + + "\nPlease check the FAQ for possible reasons why this error might occur."). + setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + callingActivity.setResult(Activity.RESULT_CANCELED); + callingActivity.finish(); + } + }).show(); + } } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.java index abf4bfe6..6ca8bf49 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.java @@ -1,6 +1,10 @@ package com.zeapo.pwdstore.git; import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; + +import com.zeapo.pwdstore.R; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PullCommand; @@ -32,10 +36,26 @@ public class PullOperation extends GitOperation { } @Override - public void execute() throws Exception { + public void execute() { if (this.provider != null) { ((PullCommand) this.command).setCredentialsProvider(this.provider); } - new GitAsyncTask(callingActivity, true, false, PullCommand.class).execute(this.command); + new GitAsyncTask(callingActivity, true, false, this).execute(this.command); + } + + @Override + public void onTaskEnded(String result) { + new AlertDialog.Builder(callingActivity). + setTitle(callingActivity.getResources().getString(R.string.jgit_error_dialog_title)). + setMessage("Error occured during the pull operation, " + + callingActivity.getResources().getString(R.string.jgit_error_dialog_text) + + result + + "\nPlease check the FAQ for possible reasons why this error might occur."). + setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + callingActivity.finish(); + } + }).show(); } } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.java index 73ed05ff..2774af90 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.java @@ -1,6 +1,10 @@ package com.zeapo.pwdstore.git; import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; + +import com.zeapo.pwdstore.R; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PushCommand; @@ -32,10 +36,27 @@ public class PushOperation extends GitOperation { } @Override - public void execute() throws Exception { + public void execute() { if (this.provider != null) { ((PushCommand) this.command).setCredentialsProvider(this.provider); } - new GitAsyncTask(callingActivity, true, false, PushCommand.class).execute(this.command); + new GitAsyncTask(callingActivity, true, false, this).execute(this.command); + } + + @Override + public void onTaskEnded(String result) { + // TODO handle the "Nothing to push" case + new AlertDialog.Builder(callingActivity). + setTitle(callingActivity.getResources().getString(R.string.jgit_error_dialog_title)). + setMessage("Error occured during the push operation, " + + callingActivity.getResources().getString(R.string.jgit_error_dialog_text) + + result + + "\nPlease check the FAQ for possible reasons why this error might occur."). + setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + callingActivity.finish(); + } + }).show(); } } diff --git a/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java index 9351cc1b..4b698075 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java @@ -1,6 +1,10 @@ package com.zeapo.pwdstore.git; import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; + +import com.zeapo.pwdstore.R; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PullCommand; @@ -39,12 +43,27 @@ public class SyncOperation extends GitOperation { } @Override - public void execute() throws Exception { - + public void execute() { if (this.provider != null) { this.pullCommand.setCredentialsProvider(this.provider); this.pushCommand.setCredentialsProvider(this.provider); } - new GitAsyncTask(callingActivity, true, false, PullCommand.class).execute(this.pullCommand, this.pushCommand); + new GitAsyncTask(callingActivity, true, false, this).execute(this.pullCommand, this.pushCommand); + } + + @Override + public void onTaskEnded(String result) { + new AlertDialog.Builder(callingActivity). + setTitle(callingActivity.getResources().getString(R.string.jgit_error_dialog_title)). + setMessage("Error occured during the sync operation, " + + callingActivity.getResources().getString(R.string.jgit_error_dialog_text) + + result + + "\nPlease check the FAQ for possible reasons why this error might occur."). + setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + callingActivity.finish(); + } + }).show(); } } |
