revert auto-sync and background git activity

too buggy, no time to work on it correclty
This commit is contained in:
Mohamed Zenadi 2017-06-24 12:24:28 +02:00
parent fca7f8267b
commit 7b8ad7ca0f
11 changed files with 34 additions and 98 deletions

View file

@ -23,19 +23,16 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.zeapo.pwdstore.crypto.PgpHandler; import com.zeapo.pwdstore.crypto.PgpHandler;
import com.zeapo.pwdstore.git.GitActivity; import com.zeapo.pwdstore.git.GitActivity;
import com.zeapo.pwdstore.git.GitAsyncTask; import com.zeapo.pwdstore.git.GitAsyncTask;
import com.zeapo.pwdstore.git.GitOperation; import com.zeapo.pwdstore.git.GitOperation;
import com.zeapo.pwdstore.git.SyncOperation;
import com.zeapo.pwdstore.pwgen.PRNGFixes; import com.zeapo.pwdstore.pwgen.PRNGFixes;
import com.zeapo.pwdstore.utils.PasswordItem; import com.zeapo.pwdstore.utils.PasswordItem;
import com.zeapo.pwdstore.utils.PasswordRecyclerAdapter; import com.zeapo.pwdstore.utils.PasswordRecyclerAdapter;
@ -46,13 +43,8 @@ import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import java.io.File; import java.io.File;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -347,20 +339,6 @@ public class PasswordStore extends AppCompatActivity {
startActivityForResult(intent, HOME); startActivityForResult(intent, HOME);
} else { } else {
checkLocalRepository(PasswordRepository.getRepositoryDirectory(getApplicationContext())); 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 + 3600) {
Toast.makeText(getApplicationContext(), "Running git auto sync", Toast.LENGTH_LONG).show();
Intent intent;
intent = new Intent(this, GitActivity.class);
intent.putExtra("Operation", GitActivity.REQUEST_SYNC);
startActivityForResult(intent, GitActivity.REQUEST_SYNC);
settings.edit().putInt("last_sync", currentTime).apply();
}
} }
} }
@ -567,7 +545,7 @@ public class PasswordStore extends AppCompatActivity {
private void commitChange(final String message) { private void commitChange(final String message) {
new GitOperation(PasswordRepository.getRepositoryDirectory(activity), activity) { new GitOperation(PasswordRepository.getRepositoryDirectory(activity), activity) {
@Override @Override
public void execute(boolean finishOnEnd) { public void execute() {
Log.d(TAG, "Commiting with message " + message); Log.d(TAG, "Commiting with message " + message);
Git git = new Git(this.repository); Git git = new Git(this.repository);
GitAsyncTask tasks = new GitAsyncTask(activity, finishOnEnd, true, this); GitAsyncTask tasks = new GitAsyncTask(activity, finishOnEnd, true, this);
@ -576,7 +554,7 @@ public class PasswordStore extends AppCompatActivity {
git.commit().setAll(true).setMessage(message) git.commit().setAll(true).setMessage(message)
); );
} }
}.execute(false); }.execute();
} }
protected void onActivityResult(int requestCode, int resultCode, protected void onActivityResult(int requestCode, int resultCode,

View file

@ -66,11 +66,11 @@ public class CloneOperation extends GitOperation {
} }
@Override @Override
public void execute(boolean finishOnEnd) { public void execute() {
if (this.provider != null) { if (this.provider != null) {
((CloneCommand) this.command).setCredentialsProvider(this.provider); ((CloneCommand) this.command).setCredentialsProvider(this.provider);
} }
new GitAsyncTask(callingActivity, finishOnEnd, false, this).execute(this.command); new GitAsyncTask(callingActivity, true, false, this).execute(this.command);
} }
@Override @Override

View file

@ -536,7 +536,7 @@ public class GitActivity extends AppCompatActivity {
try { try {
new CloneOperation(localDir, activity) new CloneOperation(localDir, activity)
.setCommand(hostname) .setCommand(hostname)
.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"), true); .executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"));
} catch (Exception e) { } catch (Exception e) {
//This is what happens when jgit fails :( //This is what happens when jgit fails :(
//TODO Handle the diffent cases of exceptions //TODO Handle the diffent cases of exceptions
@ -574,7 +574,7 @@ public class GitActivity extends AppCompatActivity {
} }
new CloneOperation(localDir, activity) new CloneOperation(localDir, activity)
.setCommand(hostname) .setCommand(hostname)
.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"), true); .executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"));
} catch (Exception e) { } catch (Exception e) {
//This is what happens when jgit fails :( //This is what happens when jgit fails :(
//TODO Handle the diffent cases of exceptions //TODO Handle the diffent cases of exceptions
@ -632,9 +632,7 @@ public class GitActivity extends AppCompatActivity {
} }
try { try {
op.executeAfterAuthentication(connectionMode, op.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"));
settings.getString("git_remote_username", "git"),
new File(getFilesDir() + "/.ssh_key"), false);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -676,7 +674,7 @@ public class GitActivity extends AppCompatActivity {
} }
try { try {
op.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"), true); op.executeAfterAuthentication(connectionMode, settings.getString("git_remote_username", "git"), new File(getFilesDir() + "/.ssh_key"));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -3,11 +3,7 @@ package com.zeapo.pwdstore.git;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.design.widget.Snackbar;
import android.text.Html;
import android.text.Spanned;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import com.zeapo.pwdstore.PasswordStore; import com.zeapo.pwdstore.PasswordStore;
import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.R;
@ -17,43 +13,26 @@ import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.StatusCommand; import org.eclipse.jgit.api.StatusCommand;
public class GitAsyncTask extends AsyncTask<GitCommand, String, String> { public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
private Activity activity; private Activity activity;
private boolean lockAndFinishActivity; private boolean finishOnEnd;
private boolean refreshListOnEnd; private boolean refreshListOnEnd;
private ProgressDialog dialog; private ProgressDialog dialog;
private GitOperation operation; private GitOperation operation;
private Snackbar snack;
public GitAsyncTask(Activity activity, boolean lockAndFinishActivity, boolean refreshListOnEnd, GitOperation operation) { public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, GitOperation operation) {
this.activity = activity; this.activity = activity;
this.lockAndFinishActivity = lockAndFinishActivity; this.finishOnEnd = finishOnEnd;
this.refreshListOnEnd = refreshListOnEnd; this.refreshListOnEnd = refreshListOnEnd;
this.operation = operation; this.operation = operation;
this.dialog = new ProgressDialog(this.activity); dialog = new ProgressDialog(this.activity);
} }
protected void onPreExecute() { protected void onPreExecute() {
if (lockAndFinishActivity) { this.dialog.setMessage(activity.getResources().getString(R.string.running_dialog_text));
final Spanned message = Html.fromHtml(String.format("Running %s", operation.getClass().getSimpleName())); this.dialog.setCancelable(false);
this.dialog.setMessage(message); this.dialog.show();
this.dialog.setCancelable(false);
this.dialog.show();
} else {
final Spanned message = Html.fromHtml(String.format("<font color=\"#ffffff\">Running %s</font>", operation.getClass().getSimpleName()));
Toast.makeText(activity.getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
}
protected void onProgressUpdate(String... progress) {
if (lockAndFinishActivity) {
final Spanned message = Html.fromHtml(String.format("Running jgit command: <strong>%s</strong>", progress[0]));
this.dialog.setMessage(message);
} else {
final Spanned message = Html.fromHtml(String.format("<font color=\"#ffffff\">Running jgit command: <strong>%s</strong></font>", progress[0]));
Toast.makeText(activity.getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
} }
@Override @Override
@ -73,8 +52,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, String, String> {
} else { } else {
command.call(); command.call();
} }
String opName = command.getClass().getSimpleName();
publishProgress(opName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return e.getMessage() + "\nCaused by:\n" + e.getCause(); return e.getMessage() + "\nCaused by:\n" + e.getCause();
@ -97,7 +75,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, String, String> {
if (!result.isEmpty()) { if (!result.isEmpty()) {
this.operation.onTaskEnded(result); this.operation.onTaskEnded(result);
} else { } else {
if (lockAndFinishActivity) { if (finishOnEnd) {
this.activity.setResult(Activity.RESULT_OK); this.activity.setResult(Activity.RESULT_OK);
this.activity.finish(); this.activity.finish();
} }

View file

@ -76,10 +76,8 @@ public abstract class GitOperation {
/** /**
* Executes the GitCommand in an async task * Executes the GitCommand in an async task
*
* @param finishOnEnd
*/ */
public abstract void execute(boolean finishOnEnd); public abstract void execute();
/** /**
* Executes the GitCommand in an async task after creating the authentication * Executes the GitCommand in an async task after creating the authentication
@ -87,10 +85,9 @@ public abstract class GitOperation {
* @param connectionMode the server-connection mode * @param connectionMode the server-connection mode
* @param username the username * @param username the username
* @param sshKey the ssh-key file * @param sshKey the ssh-key file
* @param finishOnEnd
*/ */
public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, boolean finishOnEnd) { public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey) {
executeAfterAuthentication(connectionMode, username, sshKey, false, finishOnEnd); executeAfterAuthentication(connectionMode, username, sshKey, false);
} }
/** /**
@ -100,14 +97,8 @@ public abstract class GitOperation {
* @param username the username * @param username the username
* @param sshKey the ssh-key file * @param sshKey the ssh-key file
* @param showError show the passphrase edit text in red * @param showError show the passphrase edit text in red
* @param finishOnEnd
*/ */
private void executeAfterAuthentication( private void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, final boolean showError) {
final String connectionMode,
final String username,
@Nullable final File sshKey,
final boolean showError,
final boolean finishOnEnd) {
if (connectionMode.equalsIgnoreCase("ssh-key")) { if (connectionMode.equalsIgnoreCase("ssh-key")) {
if (sshKey == null || !sshKey.exists()) { if (sshKey == null || !sshKey.exists()) {
new AlertDialog.Builder(callingActivity) new AlertDialog.Builder(callingActivity)
@ -170,21 +161,19 @@ public abstract class GitOperation {
public void onClick(DialogInterface dialog, int whichButton) { public void onClick(DialogInterface dialog, int whichButton) {
if (keyPair.decrypt(passphrase.getText().toString())) { if (keyPair.decrypt(passphrase.getText().toString())) {
// Authenticate using the ssh-key and then execute the command // Authenticate using the ssh-key and then execute the command
setAuthentication(sshKey, username, passphrase.getText().toString()).execute(finishOnEnd); setAuthentication(sshKey, username, passphrase.getText().toString()).execute();
callingActivity.finish();
} else { } else {
// call back the method // call back the method
executeAfterAuthentication(connectionMode, username, sshKey, true, finishOnEnd); executeAfterAuthentication(connectionMode, username, sshKey, true);
} }
} }
}).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { public void onClick(DialogInterface dialog, int whichButton) {
callingActivity.finish(); // Do nothing.
} }
}).show(); }).show();
} else { } else {
setAuthentication(sshKey, username, "").execute(finishOnEnd); setAuthentication(sshKey, username, "").execute();
callingActivity.finish();
} }
} catch (JSchException e) { } catch (JSchException e) {
new AlertDialog.Builder(callingActivity) new AlertDialog.Builder(callingActivity)
@ -211,7 +200,7 @@ public abstract class GitOperation {
.setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { public void onClick(DialogInterface dialog, int whichButton) {
// authenticate using the user/pwd and then execute the command // authenticate using the user/pwd and then execute the command
setAuthentication(username, password.getText().toString()).execute(finishOnEnd); setAuthentication(username, password.getText().toString()).execute();
} }
}).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {

View file

@ -36,11 +36,11 @@ public class PullOperation extends GitOperation {
} }
@Override @Override
public void execute(boolean finishOnEnd) { public void execute() {
if (this.provider != null) { if (this.provider != null) {
((PullCommand) this.command).setCredentialsProvider(this.provider); ((PullCommand) this.command).setCredentialsProvider(this.provider);
} }
new GitAsyncTask(callingActivity, finishOnEnd, false, this).execute(this.command); new GitAsyncTask(callingActivity, true, false, this).execute(this.command);
} }
@Override @Override

View file

@ -36,11 +36,11 @@ public class PushOperation extends GitOperation {
} }
@Override @Override
public void execute(boolean finishOnEnd) { public void execute() {
if (this.provider != null) { if (this.provider != null) {
((PushCommand) this.command).setCredentialsProvider(this.provider); ((PushCommand) this.command).setCredentialsProvider(this.provider);
} }
new GitAsyncTask(callingActivity, finishOnEnd, false, this).execute(this.command); new GitAsyncTask(callingActivity, true, false, this).execute(this.command);
} }
@Override @Override

View file

@ -48,12 +48,12 @@ public class SyncOperation extends GitOperation {
} }
@Override @Override
public void execute(boolean finishOnEnd) { public void execute() {
if (this.provider != null) { if (this.provider != null) {
this.pullCommand.setCredentialsProvider(this.provider); this.pullCommand.setCredentialsProvider(this.provider);
this.pushCommand.setCredentialsProvider(this.provider); this.pushCommand.setCredentialsProvider(this.provider);
} }
new GitAsyncTask(callingActivity, finishOnEnd, false, this).execute(this.addCommand, this.statusCommand, this.commitCommand, this.pullCommand, this.pushCommand); new GitAsyncTask(callingActivity, true, false, this).execute(this.addCommand, this.statusCommand, this.commitCommand, this.pullCommand, this.pushCommand);
} }
@Override @Override

View file

@ -3,8 +3,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".pwdstore" tools:context=".pwdstore"
android:orientation="vertical" android:orientation="vertical">
android:id="@+id/main_activity">
<LinearLayout <LinearLayout
android:id="@+id/main_layout" android:id="@+id/main_layout"

View file

@ -197,6 +197,4 @@
<string name="no_repo_selected2">No external repository selected</string> <string name="no_repo_selected2">No external repository selected</string>
<string name="send_plaintext_password_to">Send password as plaintext using…</string> <string name="send_plaintext_password_to">Send password as plaintext using…</string>
<string name="show_password">Show password</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> </resources>

View file

@ -16,10 +16,6 @@
<Preference <Preference
android:key="ssh_see_key" android:key="ssh_see_key"
android:title="@string/pref_ssh_see_key_title" /> 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 <Preference
android:key="git_delete_repo" android:key="git_delete_repo"
android:summary="@string/pref_git_delete_repo_summary" android:summary="@string/pref_git_delete_repo_summary"