diff options
author | zeapo <mohamed@zenadi.com> | 2014-12-04 21:43:27 +0100 |
---|---|---|
committer | zeapo <mohamed@zenadi.com> | 2014-12-04 21:43:27 +0100 |
commit | d98a30ee4828eff9b1b8c28ca917322b502ac08c (patch) | |
tree | c72d9cbd2de773579498af1c287ed87c577edbe6 | |
parent | 173248e1bb96fd971027ac8a36fdc7e12825fe23 (diff) |
Add the possibility to edit git server information
Also uses jgit 3.6
-rw-r--r-- | app/build.gradle | 8 | ||||
-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 | ||||
-rw-r--r-- | gradle/wrapper/gradle-wrapper.properties | 4 |
8 files changed, 127 insertions, 52 deletions
diff --git a/app/build.gradle b/app/build.gradle index 0662e235..0de0ef2a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,7 +4,7 @@ apply plugin: 'eclipse' android { compileSdkVersion 21 - buildToolsVersion "21.0.1" + buildToolsVersion "21.1.1" defaultConfig { applicationId "com.zeapo.pwdstore" minSdkVersion 15 @@ -25,12 +25,12 @@ android { } dependencies { - compile "com.android.support:appcompat-v7:21.+" - compile "com.android.support:recyclerview-v7:21.+" + compile "com.android.support:appcompat-v7:21.0.2" + compile "com.android.support:recyclerview-v7:21.0.2" //compile fileTree(dir: 'libs', include: ['*.jar']) compile project(':libraries:openpgp-api-lib') - compile 'org.eclipse.jgit:org.eclipse.jgit:3.5.+' + compile 'org.eclipse.jgit:org.eclipse.jgit:3.6.0.201411121045-m1' compile 'org.apache.commons:commons-io:1.3.2' } tasks.findAll { // make all tasks whose name starts with 'assemble'... 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> diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1a7c53be..68e02ebb 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Nov 25 21:49:03 CET 2014 +#Thu Dec 04 19:46:18 CET 2014 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip |