aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohamed Zenadi <mohamed@zenadi.com>2015-04-19 20:27:30 +0200
committerMohamed Zenadi <mohamed@zenadi.com>2015-04-19 20:27:30 +0200
commit771d8a9415831b5d6fe9cebf574fbf7e5eacd373 (patch)
tree3c74a84dce73e1e19b93839b803f509feabe36df
parent6983ef4f82a85cae1e7f83638ce2f5ec37e96ef0 (diff)
Add a Sync command that calls Pull then Push
Fixes #78
-rw-r--r--app/build.gradle10
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/PasswordStore.java15
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java47
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java8
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java2
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java50
-rw-r--r--app/src/main/res/menu/main_menu.xml (renamed from app/src/main/res/menu/pwdstore.xml)6
-rw-r--r--app/src/main/res/values-cs/strings.xml3
-rw-r--r--app/src/main/res/values/strings.xml3
9 files changed, 101 insertions, 43 deletions
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;
}
@@ -521,20 +522,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<GitCommand, Integer, String> {
@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<GitCommand, Integer, String> {
}
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
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 @@
<!--<item android:id="@+id/menu_add_category"-->
<!--android:title="New category"/>-->
+ <item android:id="@+id/git_sync"
+ android:title="@string/git_sync"/>
<item android:id="@+id/git_pull"
- android:title="Pull from remote"/>
+ android:title="@string/git_pull"/>
<item android:id="@+id/git_push"
- android:title="Push to remote"/>
+ android:title="@string/git_push"/>
<item android:id="@+id/refresh"
android:title="Refresh list"
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 @@
<string name="dialog_negative">Ne... později</string>
<string name="dialog_oops">Ajaj...</string>
<string name="dialog_cancel">Zrušit</string>
+ <string name="git_sync">Syncronize repository</string>
+ <string name="git_pull">Pull from remote</string>
+ <string name="git_push">Push to remote</string>
</resources>
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 @@
<string name="dialog_negative">Nah... later</string>
<string name="dialog_oops">Oops...</string>
<string name="dialog_cancel">Cancel</string>
+ <string name="git_sync">Syncronize repository</string>
+ <string name="git_pull">Pull from remote</string>
+ <string name="git_push">Push to remote</string>
</resources>