From d98a30ee4828eff9b1b8c28ca917322b502ac08c Mon Sep 17 00:00:00 2001 From: zeapo Date: Thu, 4 Dec 2014 21:43:27 +0100 Subject: Add the possibility to edit git server information Also uses jgit 3.6 --- app/build.gradle | 8 +-- .../main/java/com/zeapo/pwdstore/GitHandler.java | 43 +++++++++--- .../java/com/zeapo/pwdstore/UserPreference.java | 78 +++++++++++++++------- .../zeapo/pwdstore/utils/PasswordRepository.java | 27 +++++++- app/src/main/res/layout/activity_git_clone.xml | 6 ++ app/src/main/res/values/strings.xml | 3 +- app/src/main/res/xml/preference.xml | 10 +-- 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 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"/> +