diff options
Diffstat (limited to 'app/src/main/java')
3 files changed, 112 insertions, 36 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java index 79dec904..98544621 100644 --- a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java @@ -12,8 +12,6 @@ import android.support.v7.app.ActionBarActivity; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; -import android.util.Log; -import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -37,7 +35,6 @@ import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.api.PullCommand; import org.eclipse.jgit.api.PushCommand; -import org.eclipse.jgit.diff.Edit; import org.eclipse.jgit.errors.UnsupportedCredentialItem; import org.eclipse.jgit.transport.CredentialItem; import org.eclipse.jgit.transport.CredentialsProvider; @@ -52,8 +49,6 @@ import org.eclipse.jgit.util.FS; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; -import java.net.URI; -import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -78,6 +73,7 @@ public class GitHandler extends ActionBarActivity { public static final int REQUEST_PUSH = 102; public static final int REQUEST_CLONE = 103; public static final int REQUEST_INIT = 104; + public static final int EDIT_SERVER = 105; private static final int GET_SSH_KEY_FROM_CLONE = 201; @@ -92,9 +88,13 @@ public class GitHandler extends ActionBarActivity { protocol = settings.getString("git_remote_protocol", "ssh://"); connectionMode = settings.getString("git_remote_auth", "ssh-key"); + int operationCode = getIntent().getExtras().getInt("Operation"); - switch (getIntent().getExtras().getInt("Operation")) { + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + switch (operationCode) { case REQUEST_CLONE: + case EDIT_SERVER: setContentView(R.layout.activity_git_clone); final Spinner protcol_spinner = (Spinner) findViewById(R.id.clone_protocol); @@ -245,6 +245,16 @@ public class GitHandler extends ActionBarActivity { } }); + if (operationCode == EDIT_SERVER) + { + findViewById(R.id.clone_button).setVisibility(View.INVISIBLE); + findViewById(R.id.save_button).setVisibility(View.VISIBLE); + } else { + findViewById(R.id.clone_button).setVisibility(View.VISIBLE); + findViewById(R.id.save_button).setVisibility(View.INVISIBLE); + } + + break; case REQUEST_PULL: authenticateAndRun("pullOperation"); @@ -453,6 +463,10 @@ public class GitHandler extends ActionBarActivity { } + /** + * Clones the repository, the directory exists, deletes it + * @param view + */ public void cloneRepository(View view) { localDir = new File(getApplicationContext().getFilesDir().getAbsoluteFile() + "/store"); @@ -532,8 +546,12 @@ public class GitHandler extends ActionBarActivity { } } - public void cloneOperation(UsernamePasswordCredentialsProvider provider) { + /** + * Save the repository information to the shared preferences settings + * @param view + */ + public void saveConfiguration(View view) { // remember the settings SharedPreferences.Editor editor = settings.edit(); @@ -545,6 +563,13 @@ public class GitHandler extends ActionBarActivity { editor.putString("git_remote_port", port); editor.commit(); + PasswordRepository.addRemote("origin", ((EditText) findViewById(R.id.clone_uri)).getText().toString(), true); + } + + public void cloneOperation(UsernamePasswordCredentialsProvider provider) { + + saveConfiguration(null); + CloneCommand cmd = Git.cloneRepository(). setCredentialsProvider(provider). setCloneAllBranches(true). @@ -584,7 +609,7 @@ public class GitHandler extends ActionBarActivity { + "@" + settings.getString("git_remote_server", "server.com").trim() + ":" + - settings.getString("git_remote_location", "path/to/repository")); + settings.getString("git_remote_location", "path/to/repository"), false); GitCommand cmd; if (provider != null) @@ -633,7 +658,7 @@ public class GitHandler extends ActionBarActivity { + "@" + settings.getString("git_remote_server", "server.com").trim() + ":" + - settings.getString("git_remote_location", "path/to/repository")); + settings.getString("git_remote_location", "path/to/repository"), false); GitCommand cmd; if (provider != null) diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java index ab3fc0e1..efd2f6a7 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java @@ -31,6 +31,9 @@ import java.io.InputStream; import java.net.URI; public class UserPreference extends ActionBarActivity implements Preference.OnPreferenceClickListener { + private final static int IMPORT_SSH_KEY = 1; + private final static int IMPORT_PGP_KEY = 2; + private final static int EDIT_GIT_INFO = 3; public static class PrefsFragment extends PreferenceFragment { @Override @@ -40,6 +43,7 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr addPreferencesFromResource(R.xml.preference); findPreference("openpgp_key_id").setOnPreferenceClickListener((UserPreference) getActivity()); findPreference("ssh_key").setOnPreferenceClickListener((UserPreference) getActivity()); + findPreference("git_server_info").setOnPreferenceClickListener((UserPreference) getActivity()); } } @@ -79,7 +83,7 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr public void getSshKey() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); - startActivityForResult(intent, 1); + startActivityForResult(intent, IMPORT_SSH_KEY); } @@ -92,12 +96,27 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr @Override public boolean onPreferenceClick(Preference pref) { - if (pref.getKey().equals("openpgp_key_id")) { - Intent intent = new Intent(this, PgpHandler.class); - intent.putExtra("Operation", "GET_KEY_ID"); - startActivityForResult(intent, 0); - } else if (pref.getKey().equals("ssh_key")) { - getSshKey(); + switch (pref.getKey()) + { + case "openpgp_key_id": + { + Intent intent = new Intent(this, PgpHandler.class); + intent.putExtra("Operation", "GET_KEY_ID"); + startActivityForResult(intent, IMPORT_PGP_KEY); + } + break; + case "ssh_key": + { + getSshKey(); + } + break; + case "git_server_info": + { + Intent intent = new Intent(this, GitHandler.class); + intent.putExtra("Operation", GitHandler.EDIT_SERVER); + startActivityForResult(intent, EDIT_GIT_INFO); + } + break; } return true; } @@ -105,25 +124,36 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { - if (requestCode == 1) { -// Uri sshFile = data.getData(); - try { - copySshKey(data.getData()); - Log.i("PREF", "Got key"); - setResult(RESULT_OK); - finish(); - } catch (IOException e) + switch (requestCode) + { + case IMPORT_SSH_KEY: { - new AlertDialog.Builder(this). - setTitle(this.getResources().getString(R.string.ssh_key_error_dialog_title)). - setMessage(this.getResources().getString(R.string.ssh_key_error_dialog_text) + e.getMessage()). - setPositiveButton(this.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - //pass - } - }).show(); + try { + copySshKey(data.getData()); + Log.i("PREF", "Got key"); + setResult(RESULT_OK); + finish(); + } catch (IOException e) + { + new AlertDialog.Builder(this). + setTitle(this.getResources().getString(R.string.ssh_key_error_dialog_title)). + setMessage(this.getResources().getString(R.string.ssh_key_error_dialog_text) + e.getMessage()). + setPositiveButton(this.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + //pass + } + }).show(); + } + } + break; + case EDIT_GIT_INFO: + { + } + break; + default: + break; } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java index 31dea887..6e7de7f1 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java @@ -63,16 +63,15 @@ public class PasswordRepository { getRepository(localDir); } - // TODO add remote edition later-on // TODO add multiple remotes support for pull/push - public static void addRemote(String name, String url) { + public static void addRemote(String name, String url, Boolean replace) { StoredConfig storedConfig = repository.getConfig(); Set<String> remotes = storedConfig.getSubsections("remote"); if (!remotes.contains(name)) { try { URIish uri = new URIish(url); - RefSpec refSpec = new RefSpec("+refs/head/*:refs/remotes/"+ name + "/*"); + RefSpec refSpec = new RefSpec("+refs/head/*:refs/remotes/" + name + "/*"); RemoteConfig remoteConfig = new RemoteConfig(storedConfig, name); remoteConfig.addFetchRefSpec(refSpec); @@ -84,7 +83,29 @@ public class PasswordRepository { storedConfig.save(); } catch (Exception e) { + e.printStackTrace(); + } + } else if (replace) { + try { + URIish uri = new URIish(url); + + RemoteConfig remoteConfig = new RemoteConfig(storedConfig, name); + // remove the first and eventually the only uri + if (remoteConfig.getURIs().size() > 0) { + remoteConfig.removeURI(remoteConfig.getURIs().get(0)); + } + if (remoteConfig.getPushURIs().size() > 0) { + remoteConfig.removePushURI(remoteConfig.getPushURIs().get(0)); + } + + remoteConfig.addURI(uri); + remoteConfig.addPushURI(uri); + + remoteConfig.update(storedConfig); + storedConfig.save(); + } catch (Exception e) { + e.printStackTrace(); } } } |