From 771d8a9415831b5d6fe9cebf574fbf7e5eacd373 Mon Sep 17 00:00:00 2001 From: Mohamed Zenadi Date: Sun, 19 Apr 2015 20:27:30 +0200 Subject: [PATCH] Add a Sync command that calls Pull then Push Fixes #78 --- app/build.gradle | 10 ++-- .../com/zeapo/pwdstore/PasswordStore.java | 15 ++++-- .../com/zeapo/pwdstore/git/GitActivity.java | 47 ++++++++--------- .../com/zeapo/pwdstore/git/GitAsyncTask.java | 8 +-- .../com/zeapo/pwdstore/git/GitOperation.java | 2 - .../com/zeapo/pwdstore/git/SyncOperation.java | 50 +++++++++++++++++++ .../res/menu/{pwdstore.xml => main_menu.xml} | 6 ++- app/src/main/res/values-cs/strings.xml | 3 ++ app/src/main/res/values/strings.xml | 3 ++ 9 files changed, 101 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java rename app/src/main/res/menu/{pwdstore.xml => main_menu.xml} (87%) diff --git a/app/build.gradle b/app/build.gradle index f8f43844..078dc444 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,12 +3,12 @@ apply from: 'copyLibs.gradle' // enable 'copyLibs.gradle' script plugin apply plugin: 'eclipse' android { - compileSdkVersion 21 - buildToolsVersion "21.1.2" + compileSdkVersion 22 + buildToolsVersion "22.0.0" defaultConfig { applicationId "com.zeapo.pwdstore" minSdkVersion 15 - targetSdkVersion 21 + targetSdkVersion 22 versionCode 35 versionName "1.2.0.15" } @@ -25,8 +25,8 @@ android { } dependencies { - compile 'com.android.support:appcompat-v7:21.0.3' - compile 'com.android.support:recyclerview-v7:21.0.3' + compile 'com.android.support:appcompat-v7:22.0.0' + compile 'com.android.support:recyclerview-v7:22.0.0' //compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':libraries:openpgp-api-lib') compile 'org.eclipse.jgit:org.eclipse.jgit:3.7.0.201502260915-r' 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/pwdstore.xml b/app/src/main/res/menu/main_menu.xml similarity index 87% rename from app/src/main/res/menu/pwdstore.xml rename to app/src/main/res/menu/main_menu.xml index 220bd49b..6f391460 100644 --- a/app/src/main/res/menu/pwdstore.xml +++ b/app/src/main/res/menu/main_menu.xml @@ -17,10 +17,12 @@ + + android:title="@string/git_pull"/> + android:title="@string/git_push"/> 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