diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/GitHandler.java')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/GitHandler.java | 127 |
1 files changed, 117 insertions, 10 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java index c487ce0f..651b23d7 100644 --- a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java @@ -9,6 +9,8 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.InputType; +import android.util.Log; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -32,6 +34,7 @@ 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; @@ -46,6 +49,10 @@ 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; // TODO move the messages to strings.xml @@ -60,6 +67,7 @@ public class GitHandler extends Activity { private File localDir; private String hostname; private String username; + private String port; private SharedPreferences settings; @@ -80,7 +88,7 @@ public class GitHandler extends Activity { settings = PreferenceManager.getDefaultSharedPreferences(this.context); protocol = settings.getString("git_remote_protocol", "ssh://"); - connectionMode = settings.getString("git_remote_auth", "username/password"); + connectionMode = settings.getString("git_remote_auth", "ssh-key"); switch (getIntent().getExtras().getInt("Operation")) { case REQUEST_CLONE: @@ -139,6 +147,40 @@ public class GitHandler extends Activity { } }); + + // init the server information + EditText server_url = ((EditText) findViewById(R.id.server_url)); + EditText server_port = ((EditText) findViewById(R.id.server_port)); + EditText server_path = ((EditText) findViewById(R.id.server_path)); + EditText server_user = ((EditText) findViewById(R.id.server_user)); + final EditText server_uri = ((EditText)findViewById(R.id.clone_uri)); + + View.OnKeyListener updateListener = new View.OnKeyListener() { + @Override + public boolean onKey(View view, int i, KeyEvent keyEvent) { + updateURI(); + return false; + } + }; + + server_url.setText(settings.getString("git_remote_server", "")); + server_port.setText(settings.getString("git_remote_server_port", "")); + server_user.setText(settings.getString("git_remote_username", "")); + server_path.setText(settings.getString("git_remote_location", "")); + + server_url.setOnKeyListener(updateListener); + server_port.setOnKeyListener(updateListener); + server_user.setOnKeyListener(updateListener); + server_path.setOnKeyListener(updateListener); + + server_uri.setOnKeyListener(new View.OnKeyListener() { + @Override + public boolean onKey(View view, int i, KeyEvent keyEvent) { + splitURI(); + return false; + } + }); + break; case REQUEST_PULL: authenticateAndRun("pullOperation"); @@ -152,23 +194,78 @@ public class GitHandler extends Activity { } - @Override - public void onResume() { - super.onResume(); - + /** Fills in the server_uri field with the information coming from other fields */ + private void updateURI() { EditText uri = (EditText) findViewById(R.id.clone_uri); + EditText server_url = ((EditText) findViewById(R.id.server_url)); + EditText server_port = ((EditText) findViewById(R.id.server_port)); + EditText server_path = ((EditText) findViewById(R.id.server_path)); + EditText server_user = ((EditText) findViewById(R.id.server_user)); + if (uri != null) { String hostname = - settings.getString("git_remote_username", "") + server_user.getText() + "@" + - settings.getString("git_remote_server", "").trim() - + ":" + - settings.getString("git_remote_location", ""); + server_url.getText().toString().trim() + + ":"; + if (server_port.getText().toString().equals("22")) { + hostname += server_path.getText().toString(); + + ((TextView) findViewById(R.id.warn_url)).setVisibility(View.GONE); + } else { + TextView warn_url = (TextView) findViewById(R.id.warn_url); + if (!server_path.getText().toString().matches("/.*") && !server_port.getText().toString().isEmpty()) { + warn_url.setText(R.string.warn_malformed_url_port); + warn_url.setVisibility(View.VISIBLE); + } else { + warn_url.setVisibility(View.GONE); + } + hostname += server_port.getText().toString() + server_path.getText().toString(); + } if (!hostname.equals("@:")) uri.setText(hostname); } } + /** Splits the information in server_uri into the other fields */ + private void splitURI() { + EditText server_uri = (EditText) findViewById(R.id.clone_uri); + EditText server_url = ((EditText) findViewById(R.id.server_url)); + EditText server_port = ((EditText) findViewById(R.id.server_port)); + EditText server_path = ((EditText) findViewById(R.id.server_path)); + EditText server_user = ((EditText) findViewById(R.id.server_user)); + + String uri = server_uri.getText().toString(); + Pattern pattern = Pattern.compile("(.+)@([\\w\\d\\.]+):([\\d]+)*(.*)"); + Matcher matcher = pattern.matcher(uri); + if (matcher.find()) { + int count = matcher.groupCount(); + Log.i("GIT", ">> " + count); + if (count > 1) { + server_user.setText(matcher.group(1)); + server_url.setText(matcher.group(2)); + } + if (count == 4) { + server_port.setText(matcher.group(3)); + server_path.setText(matcher.group(4)); + + TextView warn_url = (TextView) findViewById(R.id.warn_url); + if (!server_path.getText().toString().matches("/.*") && !server_port.getText().toString().isEmpty()) { + warn_url.setText(R.string.warn_malformed_url_port); + warn_url.setVisibility(View.VISIBLE); + } else { + warn_url.setVisibility(View.GONE); + } + } + } + } + + @Override + public void onResume() { + super.onResume(); + updateURI(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. @@ -268,7 +365,8 @@ public class GitHandler extends Activity { public void cloneRepository(View view) { localDir = new File(getApplicationContext().getFilesDir().getAbsoluteFile() + "/store"); - hostname = ((TextView) findViewById(R.id.clone_uri)).getText().toString(); + hostname = ((EditText) findViewById(R.id.clone_uri)).getText().toString(); + port = ((EditText) findViewById(R.id.server_port)).getText().toString(); // don't ask the user, take off the protocol that he puts in hostname = hostname.replaceFirst("^.+://", ""); ((TextView) findViewById(R.id.clone_uri)).setText(hostname); @@ -278,6 +376,13 @@ public class GitHandler extends Activity { if (!protocol.equals("ssh://")) { hostname = protocol + hostname; } else { + + // if the port is explicitly given, jgit requires the ssh:// + if (!port.isEmpty()) + hostname = protocol + hostname; + + Log.i("GIT", "> " + port); + // did he forget the username? if (!hostname.matches("^.+@.+")) { new AlertDialog.Builder(this). @@ -343,11 +448,13 @@ public class GitHandler extends Activity { // remember the settings SharedPreferences.Editor editor = settings.edit(); + // TODO this is not pretty, use the information obtained earlier editor.putString("git_remote_server", hostname.split("@")[1].split(":")[0]); editor.putString("git_remote_location", hostname.split("@")[1].split(":")[1]); editor.putString("git_remote_username", hostname.split("@")[0]); editor.putString("git_remote_protocol", protocol); editor.putString("git_remote_auth", connectionMode); + editor.putString("git_remote_port", port); editor.commit(); CloneCommand cmd = Git.cloneRepository(). |
