diff options
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/GitHandler.java | 43 | ||||
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/UserPreference.java | 78 | ||||
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java | 27 | ||||
-rw-r--r-- | app/src/main/res/layout/activity_git_clone.xml | 6 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 3 | ||||
-rw-r--r-- | app/src/main/res/xml/preference.xml | 10 |
6 files changed, 121 insertions, 46 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(); } } } diff --git a/app/src/main/res/layout/activity_git_clone.xml b/app/src/main/res/layout/activity_git_clone.xml index f26d6f8d..de3f0b0e 100644 --- a/app/src/main/res/layout/activity_git_clone.xml +++ b/app/src/main/res/layout/activity_git_clone.xml @@ -205,6 +205,12 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="cloneRepository"/> + <Button + android:id="@+id/save_button" + android:text="Save" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:onClick="saveConfiguration"/> </LinearLayout> </ScrollView> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 05b8bbd4..5f0db2a8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -93,7 +93,8 @@ <string name="pref_remote_hint">path/to/repository</string> <string name="pref_git_username_title">Username</string> <string name="pref_git_username_hint">username</string> - <string name="pref_ssh_title">SSH Key</string> + <string name="pref_edit_server_info">Edit git server settings</string> + <string name="pref_ssh_title">Import ssh-key</string> <string name="pref_crypto_title">Crypto</string> <string name="pref_provider_title">Select OpenPGP Provider!</string> <string name="pref_provider_account_title">Set your OpenPGP account</string> diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 7fe39daa..848a10ad 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -1,15 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/pref_git_title"> - <EditTextPreference android:title= "@string/pref_server_title" - android:key="git_remote_server" android:hint="@string/pref_server_hint" - android:inputType="textUri" /> - <EditTextPreference android:title="@string/pref_remote_title" - android:key="git_remote_location" android:hint="@string/pref_remote_hint" - android:inputType="textUri" /> - <EditTextPreference android:title="@string/pref_git_username_title" - android:key="git_remote_username" android:hint="@string/pref_git_username_hint" - android:inputType="textPersonName" /> + <Preference android:title="@string/pref_edit_server_info" android:key="git_server_info"/> <Preference android:title="@string/pref_ssh_title" android:key="ssh_key" /> </PreferenceCategory> |