aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeapo <mohamed@zenadi.com>2017-06-05 14:30:19 +0200
committerzeapo <mohamed@zenadi.com>2017-06-05 14:30:19 +0200
commitbbd7f661918fa2a96c7c2f2d954c0a10dc24ce9b (patch)
treedde5a4a11e76893a2675c6e95c41724504bb51e2
parent3a5a322bfa54d2b520cfb2ff9dfc03bdea55c23b (diff)
add autoSync on startup
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/PasswordStore.java29
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.java2
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java8
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java30
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java25
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/PullOperation.java2
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/PushOperation.java2
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java4
-rw-r--r--app/src/main/res/layout/activity_pwdstore.xml3
-rw-r--r--app/src/main/res/values/strings.xml2
-rw-r--r--app/src/main/res/xml/preference.xml4
11 files changed, 78 insertions, 33 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
index cfb1f269..028d8263 100644
--- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
+++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java
@@ -23,16 +23,19 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
+import android.text.format.DateUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
+import android.widget.Toast;
import com.zeapo.pwdstore.crypto.PgpHandler;
import com.zeapo.pwdstore.git.GitActivity;
import com.zeapo.pwdstore.git.GitAsyncTask;
import com.zeapo.pwdstore.git.GitOperation;
+import com.zeapo.pwdstore.git.SyncOperation;
import com.zeapo.pwdstore.pwgen.PRNGFixes;
import com.zeapo.pwdstore.utils.PasswordItem;
import com.zeapo.pwdstore.utils.PasswordRecyclerAdapter;
@@ -43,8 +46,13 @@ import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import java.io.File;
+import java.sql.Timestamp;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Calendar;
import java.util.Collections;
+import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -339,6 +347,23 @@ public class PasswordStore extends AppCompatActivity {
startActivityForResult(intent, HOME);
} else {
checkLocalRepository(PasswordRepository.getRepositoryDirectory(getApplicationContext()));
+ File localDir = PasswordRepository.getRepositoryDirectory(getApplicationContext());
+ checkLocalRepository(localDir);
+
+ int lastSync = settings.getInt("last_sync", -1);
+ int currentTime = (int) Calendar.getInstance().getTimeInMillis() / 1000;
+ if (settings.getBoolean("git_auto_sync", false) && currentTime > lastSync + 10) {
+ Toast.makeText(getApplicationContext(), "Running git auto sync", Toast.LENGTH_LONG).show();
+ SyncOperation op = new SyncOperation(localDir.getAbsoluteFile(), activity).setCommands();
+
+ try {
+ String connectionMode = settings.getString("git_remote_auth", "ssh-key");
+ op.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"), false);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ settings.edit().putInt("last_sync", currentTime).apply();
+ }
}
}
@@ -545,7 +570,7 @@ public class PasswordStore extends AppCompatActivity {
private void commitChange(final String message) {
new GitOperation(PasswordRepository.getRepositoryDirectory(activity), activity) {
@Override
- public void execute() {
+ public void execute(boolean finishOnEnd) {
Log.d(TAG, "Commiting with message " + message);
Git git = new Git(this.repository);
GitAsyncTask tasks = new GitAsyncTask(activity, false, true, this);
@@ -554,7 +579,7 @@ public class PasswordStore extends AppCompatActivity {
git.commit().setMessage(message)
);
}
- }.execute();
+ }.execute(true);
}
protected void onActivityResult(int requestCode, int resultCode,
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 25dba1fc..03c35c23 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.java
@@ -66,7 +66,7 @@ public class CloneOperation extends GitOperation {
}
@Override
- public void execute() {
+ public void execute(boolean finishOnEnd) {
if (this.provider != null) {
((CloneCommand) this.command).setCredentialsProvider(this.provider);
}
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 66e308b9..5d72cd34 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java
@@ -536,7 +536,7 @@ public class GitActivity extends AppCompatActivity {
try {
new CloneOperation(localDir, activity)
.setCommand(hostname)
- .executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"));
+ .executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"), true);
} catch (Exception e) {
//This is what happens when jgit fails :(
//TODO Handle the diffent cases of exceptions
@@ -574,7 +574,7 @@ public class GitActivity extends AppCompatActivity {
}
new CloneOperation(localDir, activity)
.setCommand(hostname)
- .executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"));
+ .executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"), true);
} catch (Exception e) {
//This is what happens when jgit fails :(
//TODO Handle the diffent cases of exceptions
@@ -632,7 +632,7 @@ public class GitActivity extends AppCompatActivity {
}
try {
- op.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"));
+ op.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"), true);
} catch (Exception e) {
e.printStackTrace();
}
@@ -674,7 +674,7 @@ public class GitActivity extends AppCompatActivity {
}
try {
- op.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"));
+ op.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"), true);
} catch (Exception e) {
e.printStackTrace();
}
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 6b0edb7a..644d3c6b 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java
@@ -3,7 +3,10 @@ package com.zeapo.pwdstore.git;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
+import android.support.design.widget.Snackbar;
+import android.text.Html;
import android.util.Log;
+import android.widget.Toast;
import com.zeapo.pwdstore.PasswordStore;
import com.zeapo.pwdstore.R;
@@ -13,26 +16,35 @@ import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.StatusCommand;
-public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
+public class GitAsyncTask extends AsyncTask<GitCommand, String, String> {
private Activity activity;
private boolean finishOnEnd;
private boolean refreshListOnEnd;
private ProgressDialog dialog;
private GitOperation operation;
+ private Snackbar snack;
public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, GitOperation operation) {
this.activity = activity;
this.finishOnEnd = finishOnEnd;
this.refreshListOnEnd = refreshListOnEnd;
this.operation = operation;
-
- dialog = new ProgressDialog(this.activity);
}
protected void onPreExecute() {
- this.dialog.setMessage(activity.getResources().getString(R.string.running_dialog_text));
- this.dialog.setCancelable(false);
- this.dialog.show();
+// Toast.makeText(activity.getApplicationContext(), String.format("Running %s", operation.toString()), Toast.LENGTH_LONG).show();
+ snack = Snackbar.make(activity.findViewById(R.id.main_layout),
+ Html.fromHtml(String.format("<font color=\"#ffffff\">Running the Git operation %s</font>", operation.toString())),
+ Snackbar.LENGTH_INDEFINITE);
+ snack.show();
+ }
+
+ protected void onProgressUpdate(String... progress) {
+ if (this.snack != null) snack.dismiss();
+ snack = Snackbar.make(activity.findViewById(R.id.main_layout),
+ Html.fromHtml(String.format("<font color=\"#ffffff\">Running: <strong>%s</strong></font>", progress[0])),
+ Snackbar.LENGTH_INDEFINITE);
+ snack.show();
}
@Override
@@ -51,7 +63,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
} else {
command.call();
}
-
+ publishProgress(command.getClass().getName());
} catch (Exception e) {
e.printStackTrace();
return e.getMessage() + "\nCaused by:\n" + e.getCause();
@@ -61,9 +73,9 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
}
protected void onPostExecute(String result) {
- if (this.dialog != null)
+ if (this.snack != null)
try {
- this.dialog.dismiss();
+ this.snack.dismiss();
} catch (Exception e) {
// ignore
}
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 f6b05f4c..5a206f0c 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java
@@ -76,29 +76,30 @@ public abstract class GitOperation {
/**
* Executes the GitCommand in an async task
+ * @param finishOnEnd
*/
- public abstract void execute();
+ public abstract void execute(boolean finishOnEnd);
/**
* Executes the GitCommand in an async task after creating the authentication
- *
- * @param connectionMode the server-connection mode
+ * @param connectionMode the server-connection mode
* @param username the username
* @param sshKey the ssh-key file
+ * @param finishOnEnd
*/
- public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey) {
- executeAfterAuthentication(connectionMode, username, sshKey, false);
+ public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, boolean finishOnEnd) {
+ executeAfterAuthentication(connectionMode, username, sshKey, false, finishOnEnd);
}
/**
* Executes the GitCommand in an async task after creating the authentication
- *
- * @param connectionMode the server-connection mode
+ * @param connectionMode the server-connection mode
* @param username the username
* @param sshKey the ssh-key file
* @param showError show the passphrase edit text in red
+ * @param finishOnEnd
*/
- private void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, final boolean showError) {
+ private void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, final boolean showError, final boolean finishOnEnd) {
if (connectionMode.equalsIgnoreCase("ssh-key")) {
if (sshKey == null || !sshKey.exists()) {
new AlertDialog.Builder(callingActivity)
@@ -161,10 +162,10 @@ public abstract class GitOperation {
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();
+ setAuthentication(sshKey, username, passphrase.getText().toString()).execute(finishOnEnd);
} else {
// call back the method
- executeAfterAuthentication(connectionMode, username, sshKey, true);
+ executeAfterAuthentication(connectionMode, username, sshKey, true, finishOnEnd);
}
}
}).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
@@ -173,7 +174,7 @@ public abstract class GitOperation {
}
}).show();
} else {
- setAuthentication(sshKey, username, "").execute();
+ setAuthentication(sshKey, username, "").execute(finishOnEnd);
}
} catch (JSchException e) {
new AlertDialog.Builder(callingActivity)
@@ -200,7 +201,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
- setAuthentication(username, password.getText().toString()).execute();
+ setAuthentication(username, password.getText().toString()).execute(finishOnEnd);
}
}).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
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 6ca8bf49..a5422cb4 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.java
@@ -36,7 +36,7 @@ public class PullOperation extends GitOperation {
}
@Override
- public void execute() {
+ public void execute(boolean finishOnEnd) {
if (this.provider != null) {
((PullCommand) this.command).setCredentialsProvider(this.provider);
}
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 2774af90..afe058f0 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.java
@@ -36,7 +36,7 @@ public class PushOperation extends GitOperation {
}
@Override
- public void execute() {
+ public void execute(boolean finishOnEnd) {
if (this.provider != null) {
((PushCommand) this.command).setCredentialsProvider(this.provider);
}
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 cc82b9b3..128713e9 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java
@@ -48,12 +48,12 @@ public class SyncOperation extends GitOperation {
}
@Override
- public void execute() {
+ public void execute(boolean finishOnEnd) {
if (this.provider != null) {
this.pullCommand.setCredentialsProvider(this.provider);
this.pushCommand.setCredentialsProvider(this.provider);
}
- new GitAsyncTask(callingActivity, true, false, this).execute(this.addCommand, this.statusCommand, this.commitCommand, this.pullCommand, this.pushCommand);
+ new GitAsyncTask(callingActivity, finishOnEnd, false, this).execute(this.addCommand, this.statusCommand, this.commitCommand, this.pullCommand, this.pushCommand);
}
@Override
diff --git a/app/src/main/res/layout/activity_pwdstore.xml b/app/src/main/res/layout/activity_pwdstore.xml
index 84c7af06..1b7ccac6 100644
--- a/app/src/main/res/layout/activity_pwdstore.xml
+++ b/app/src/main/res/layout/activity_pwdstore.xml
@@ -3,7 +3,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pwdstore"
- android:orientation="vertical">
+ android:orientation="vertical"
+ android:id="@+id/main_activity">
<LinearLayout
android:id="@+id/main_layout"
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 5823c016..175a20c6 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -197,4 +197,6 @@
<string name="no_repo_selected2">No external repository selected</string>
<string name="send_plaintext_password_to">Send password as plaintext using…</string>
<string name="show_password">Show password</string>
+ <string name="git_auto_sync_title">Auto Sync on start</string>
+ <string name="git_auto_sync_summary">Synchronize the repository when starting the application or when resuming.</string>
</resources>
diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml
index 67c33f8c..c4a2398c 100644
--- a/app/src/main/res/xml/preference.xml
+++ b/app/src/main/res/xml/preference.xml
@@ -16,6 +16,10 @@
<Preference
android:key="ssh_see_key"
android:title="@string/pref_ssh_see_key_title" />
+ <CheckBoxPreference
+ android:key="git_auto_sync"
+ android:summary="@string/git_auto_sync_summary"
+ android:title="@string/git_auto_sync_title" />
<Preference
android:key="git_delete_repo"
android:summary="@string/pref_git_delete_repo_summary"