From 771d8a9415831b5d6fe9cebf574fbf7e5eacd373 Mon Sep 17 00:00:00 2001 From: Mohamed Zenadi Date: Sun, 19 Apr 2015 20:27:30 +0200 Subject: Add a Sync command that calls Pull then Push Fixes #78 --- .../java/com/zeapo/pwdstore/PasswordStore.java | 15 +++++-- .../java/com/zeapo/pwdstore/git/GitActivity.java | 47 +++++++++----------- .../java/com/zeapo/pwdstore/git/GitAsyncTask.java | 8 ++-- .../java/com/zeapo/pwdstore/git/GitOperation.java | 2 - .../java/com/zeapo/pwdstore/git/SyncOperation.java | 50 ++++++++++++++++++++++ app/src/main/res/menu/main_menu.xml | 36 ++++++++++++++++ app/src/main/res/menu/pwdstore.xml | 34 --------------- app/src/main/res/values-cs/strings.xml | 3 ++ app/src/main/res/values/strings.xml | 3 ++ 9 files changed, 128 insertions(+), 70 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java create mode 100644 app/src/main/res/menu/main_menu.xml delete mode 100644 app/src/main/res/menu/pwdstore.xml (limited to 'app/src') diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index 13ae41ae..b3d3beee 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -29,8 +29,6 @@ import org.eclipse.jgit.api.CommitCommand; import org.eclipse.jgit.api.Git; import java.io.File; -import java.io.IOException; -import java.util.Stack; public class PasswordStore extends ActionBarActivity { private static final String TAG = "PwdStrAct"; @@ -64,7 +62,7 @@ public class PasswordStore extends ActionBarActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.pwdstore, menu); + getMenuInflater().inflate(R.menu.main_menu, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); @@ -160,6 +158,17 @@ public class PasswordStore extends ActionBarActivity { startActivityForResult(intent, GitActivity.REQUEST_PULL); return true; + case R.id.git_sync: + if (!PasswordRepository.isInitialized()) { + initBefore.show(); + break; + } + + intent = new Intent(this, GitActivity.class); + intent.putExtra("Operation", GitActivity.REQUEST_SYNC); + startActivityForResult(intent, GitActivity.REQUEST_SYNC); + return true; + case R.id.refresh: updateListAdapter(); return true; diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java index 0db20811..76599c3b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java @@ -25,11 +25,7 @@ import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.UserPreference; import com.zeapo.pwdstore.utils.PasswordRepository; -import org.eclipse.jgit.api.CloneCommand; -import org.eclipse.jgit.api.Git; - import org.apache.commons.io.FileUtils; -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import java.io.File; import java.io.IOException; @@ -59,6 +55,7 @@ public class GitActivity extends ActionBarActivity { public static final int REQUEST_CLONE = 103; public static final int REQUEST_INIT = 104; public static final int EDIT_SERVER = 105; + public static final int REQUEST_SYNC = 106; @Override protected void onCreate(Bundle savedInstanceState) { @@ -260,11 +257,15 @@ public class GitActivity extends ActionBarActivity { break; case REQUEST_PULL: - pullFromRepository(); + syncRepository(REQUEST_PULL); break; case REQUEST_PUSH: - pushToRepository(); + syncRepository(REQUEST_PUSH); + break; + + case REQUEST_SYNC: + syncRepository(REQUEST_SYNC); break; } @@ -520,20 +521,6 @@ public class GitActivity extends ActionBarActivity { } } - /** - * Pull the latest changes from the remote repository and merges them locally - */ - public void pullFromRepository() { - syncRepository(REQUEST_PULL); - } - - /** - * Pushes the latest changes from the local repository to the remote one - */ - public void pushToRepository() { - syncRepository(REQUEST_PUSH); - } - /** * Syncs the local repository with the remote one (either pull or push) * @param operation the operation to execute can be REQUEST_PULL or REQUEST_PUSH @@ -566,13 +553,19 @@ public class GitActivity extends ActionBarActivity { PasswordRepository.addRemote("origin", hostname, false); GitOperation op; - if (operation == REQUEST_PULL) { - op = new PullOperation(localDir, activity).setCommand(); - } else if (operation == REQUEST_PUSH) { - op = new PushOperation(localDir, activity).setCommand(); - } else { - Log.e(TAG, "Sync operation not recognized : " + operation); - return; + switch (operation) { + case REQUEST_PULL: + op = new PullOperation(localDir, activity).setCommand(); + break; + case REQUEST_PUSH: + op = new PushOperation(localDir, activity).setCommand(); + break; + case REQUEST_SYNC: + op = new SyncOperation(localDir, activity).setCommands(); + break; + default: + Log.e(TAG, "Sync operation not recognized : " + operation); + return; } try { 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 add18278..1551012f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java @@ -39,10 +39,9 @@ public class GitAsyncTask extends AsyncTask { @Override protected String doInBackground(GitCommand... cmd) { - int count = cmd.length; - for (int i = 0; i < count; i++) { + for (GitCommand aCmd : cmd) { try { - cmd[i].call(); + aCmd.call(); } catch (Exception e) { e.printStackTrace(); return e.getMessage(); @@ -52,7 +51,8 @@ public class GitAsyncTask extends AsyncTask { } protected void onPostExecute(String result) { - this.dialog.dismiss(); + if (this.dialog != null) + this.dialog.dismiss(); if (result == null) result = "Unexpected error"; 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 60112c90..c7b70ca4 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java @@ -6,7 +6,6 @@ import android.content.DialogInterface; import android.content.Intent; import android.support.annotation.Nullable; import android.text.InputType; -import android.util.Log; import android.widget.EditText; import android.widget.LinearLayout; @@ -16,7 +15,6 @@ import com.zeapo.pwdstore.git.config.GitConfigSessionFactory; import com.zeapo.pwdstore.git.config.SshConfigSessionFactory; import com.zeapo.pwdstore.utils.PasswordRepository; -import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.JschConfigSessionFactory; diff --git a/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java new file mode 100644 index 00000000..9351cc1b --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java @@ -0,0 +1,50 @@ +package com.zeapo.pwdstore.git; + +import android.app.Activity; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.PullCommand; +import org.eclipse.jgit.api.PushCommand; + +import java.io.File; + +public class SyncOperation extends GitOperation { + protected PullCommand pullCommand; + protected PushCommand pushCommand; + + /** + * Creates a new git operation + * + * @param fileDir the git working tree directory + * @param callingActivity the calling activity + */ + public SyncOperation(File fileDir, Activity callingActivity) { + super(fileDir, callingActivity); + } + + /** + * Sets the command + * @return the current object + */ + public SyncOperation setCommands() { + this.pullCommand = new Git(repository) + .pull() + .setRebase(true) + .setRemote("origin"); + this.pushCommand = new Git(repository) + .push() + .setPushAll() + .setRemote("origin"); + return this; + } + + @Override + public void execute() throws Exception { + + 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); + } +} diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml new file mode 100644 index 00000000..6f391460 --- /dev/null +++ b/app/src/main/res/menu/main_menu.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/menu/pwdstore.xml b/app/src/main/res/menu/pwdstore.xml deleted file mode 100644 index 220bd49b..00000000 --- a/app/src/main/res/menu/pwdstore.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index c1dc6ae0..f789b8a9 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -121,5 +121,8 @@ Ne... později Ajaj... Zrušit + Syncronize repository + Pull from remote + Push to remote diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8cf8d140..b6793849 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -122,5 +122,8 @@ Nah... later Oops... Cancel + Syncronize repository + Pull from remote + Push to remote -- cgit v1.2.3