Merge pull request #44 from zeapo/feature/https

Add https support
This commit is contained in:
Mohamed Zenadi 2014-11-26 20:50:44 +01:00
commit 244b719466
5 changed files with 120 additions and 91 deletions

Binary file not shown.

View file

@ -9,8 +9,8 @@ android {
applicationId "com.zeapo.pwdstore" applicationId "com.zeapo.pwdstore"
minSdkVersion 15 minSdkVersion 15
targetSdkVersion 21 targetSdkVersion 21
versionCode 24 versionCode 25
versionName "1.2-a5" versionName "1.2-a6"
} }
compileOptions { compileOptions {

View file

@ -97,43 +97,12 @@ public class GitHandler extends ActionBarActivity {
case REQUEST_CLONE: case REQUEST_CLONE:
setContentView(R.layout.activity_git_clone); setContentView(R.layout.activity_git_clone);
// init the spinner for protocols final Spinner protcol_spinner = (Spinner) findViewById(R.id.clone_protocol);
Spinner protcol_spinner = (Spinner) findViewById(R.id.clone_protocol); final Spinner connection_mode_spinner = (Spinner) findViewById(R.id.connection_mode);
ArrayAdapter<CharSequence> protocol_adapter = ArrayAdapter.createFromResource(this,
R.array.clone_protocols, android.R.layout.simple_spinner_item);
protocol_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
protcol_spinner.setAdapter(protocol_adapter);
protcol_spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
protocol = ((Spinner)findViewById(R.id.clone_protocol)).getSelectedItem().toString();
if (protocol.equals("ssh://")) {
((EditText)findViewById(R.id.clone_uri)).setHint("user@hostname:path");
} else {
((EditText)findViewById(R.id.clone_uri)).setHint("hostname/path");
new AlertDialog.Builder(activity).
setMessage(activity.getResources().getString(R.string.read_only_dialog_text)).
setCancelable(true).
setPositiveButton(activity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
);
// init the spinner for connection modes // init the spinner for connection modes
Spinner connection_mode_spinner = (Spinner) findViewById(R.id.connection_mode); final ArrayAdapter<CharSequence> connection_mode_adapter = ArrayAdapter.createFromResource(this,
ArrayAdapter<CharSequence> connection_mode_adapter = ArrayAdapter.createFromResource(this,
R.array.connection_modes, android.R.layout.simple_spinner_item); R.array.connection_modes, android.R.layout.simple_spinner_item);
connection_mode_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); connection_mode_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
connection_mode_spinner.setAdapter(connection_mode_adapter); connection_mode_spinner.setAdapter(connection_mode_adapter);
@ -151,6 +120,43 @@ public class GitHandler extends ActionBarActivity {
} }
}); });
// init the spinner for protocols
ArrayAdapter<CharSequence> protocol_adapter = ArrayAdapter.createFromResource(this,
R.array.clone_protocols, android.R.layout.simple_spinner_item);
protocol_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
protcol_spinner.setAdapter(protocol_adapter);
protcol_spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
protocol = ((Spinner)findViewById(R.id.clone_protocol)).getSelectedItem().toString();
if (protocol.equals("ssh://")) {
((EditText)findViewById(R.id.clone_uri)).setHint("user@hostname:path");
((EditText) findViewById(R.id.server_port)).setHint(R.string.default_ssh_port);
// select ssh-key auth mode as default and enable the spinner in case it was disabled
connection_mode_spinner.setSelection(0);
connection_mode_spinner.setEnabled(true);
} else {
((EditText)findViewById(R.id.clone_uri)).setHint("hostname/path");
((EditText) findViewById(R.id.server_port)).setHint(R.string.default_https_port);
// select user/pwd auth-mode and disable the spinner
connection_mode_spinner.setSelection(1);
connection_mode_spinner.setEnabled(false);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
);
// init the server information // init the server information
final EditText server_url = ((EditText) findViewById(R.id.server_url)); final EditText server_url = ((EditText) findViewById(R.id.server_url));
final EditText server_port = ((EditText) findViewById(R.id.server_port)); final EditText server_port = ((EditText) findViewById(R.id.server_port));
@ -259,30 +265,57 @@ public class GitHandler extends ActionBarActivity {
EditText server_port = ((EditText) findViewById(R.id.server_port)); EditText server_port = ((EditText) findViewById(R.id.server_port));
EditText server_path = ((EditText) findViewById(R.id.server_path)); EditText server_path = ((EditText) findViewById(R.id.server_path));
EditText server_user = ((EditText) findViewById(R.id.server_user)); EditText server_user = ((EditText) findViewById(R.id.server_user));
Log.i("GIT", "key entred");
if (uri != null) { if (uri != null) {
String hostname = switch (protocol)
server_user.getText() {
+ "@" + case "ssh://":
server_url.getText().toString().trim() {
+ ":"; String hostname =
if (server_port.getText().toString().equals("22")) { server_user.getText()
hostname += server_path.getText().toString(); + "@" +
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); ((TextView) findViewById(R.id.warn_url)).setVisibility(View.GONE);
} else { } else {
TextView warn_url = (TextView) findViewById(R.id.warn_url); TextView warn_url = (TextView) findViewById(R.id.warn_url);
if (!server_path.getText().toString().matches("/.*") && !server_port.getText().toString().isEmpty()) { if (!server_path.getText().toString().matches("/.*") && !server_port.getText().toString().isEmpty()) {
warn_url.setText(R.string.warn_malformed_url_port); warn_url.setText(R.string.warn_malformed_url_port);
warn_url.setVisibility(View.VISIBLE); warn_url.setVisibility(View.VISIBLE);
} else { } else {
warn_url.setVisibility(View.GONE); warn_url.setVisibility(View.GONE);
}
hostname += server_port.getText().toString() + server_path.getText().toString();
}
if (!hostname.equals("@:")) uri.setText(hostname);
} }
hostname += server_port.getText().toString() + server_path.getText().toString(); break;
case "https://":
{
StringBuilder hostname = new StringBuilder();
hostname.append(server_url.getText().toString().trim());
if (server_port.getText().toString().equals("443")) {
hostname.append(server_path.getText().toString());
((TextView) findViewById(R.id.warn_url)).setVisibility(View.GONE);
} else {
hostname.append("/");
hostname.append(server_port.getText().toString())
.append(server_path.getText().toString());
}
if (!hostname.toString().equals("@/")) uri.setText(hostname);
}
break;
default:
break;
} }
if (!hostname.equals("@:")) uri.setText(hostname);
} }
} }
@ -439,8 +472,6 @@ public class GitHandler extends ActionBarActivity {
if (!port.isEmpty()) if (!port.isEmpty())
hostname = protocol + hostname; hostname = protocol + hostname;
Log.i("GIT", "> " + port);
// did he forget the username? // did he forget the username?
if (!hostname.matches("^.+@.+")) { if (!hostname.matches("^.+@.+")) {
new AlertDialog.Builder(this). new AlertDialog.Builder(this).
@ -506,10 +537,9 @@ public class GitHandler extends ActionBarActivity {
// remember the settings // remember the settings
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
// TODO this is not pretty, use the information obtained earlier editor.putString("git_remote_server", ((EditText) findViewById(R.id.server_url)).getText().toString());
editor.putString("git_remote_server", hostname.split("@")[1].split(":")[0]); editor.putString("git_remote_location", ((EditText) findViewById(R.id.server_path)).getText().toString());
editor.putString("git_remote_location", hostname.split("@")[1].split(":")[1]); editor.putString("git_remote_username", ((EditText) findViewById(R.id.server_user)).getText().toString());
editor.putString("git_remote_username", hostname.split("@")[0]);
editor.putString("git_remote_protocol", protocol); editor.putString("git_remote_protocol", protocol);
editor.putString("git_remote_auth", connectionMode); editor.putString("git_remote_auth", connectionMode);
editor.putString("git_remote_port", port); editor.putString("git_remote_port", port);
@ -700,39 +730,35 @@ public class GitHandler extends ActionBarActivity {
}).show(); }).show();
} }
} else { } else {
if (protocol.equals("ssh://")) { final EditText password = new EditText(activity);
final EditText password = new EditText(activity); password.setHint("Password");
password.setHint("Password"); password.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
password.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
new AlertDialog.Builder(activity) new AlertDialog.Builder(activity)
.setTitle(activity.getResources().getString(R.string.passphrase_dialog_title)) .setTitle(activity.getResources().getString(R.string.passphrase_dialog_title))
.setMessage(activity.getResources().getString(R.string.password_dialog_text)) .setMessage(activity.getResources().getString(R.string.password_dialog_text))
.setView(password) .setView(password)
.setPositiveButton(activity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { .setPositiveButton(activity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { public void onClick(DialogInterface dialog, int whichButton) {
SshSessionFactory.setInstance(new GitConfigSessionFactory());
try {
method.invoke(activity,
new UsernamePasswordCredentialsProvider(
settings.getString("git_remote_username", "git"),
password.getText().toString())
);
} catch (Exception e){
e.printStackTrace();
}
SshSessionFactory.setInstance(new GitConfigSessionFactory());
try {
method.invoke(activity,
new UsernamePasswordCredentialsProvider(
settings.getString("git_remote_username", "git"),
password.getText().toString())
);
} catch (Exception e){
e.printStackTrace();
} }
}).setNegativeButton(activity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { }
// Do nothing. }).setNegativeButton(activity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
} public void onClick(DialogInterface dialog, int whichButton) {
}).show(); // Do nothing.
} else { }
// BUG: we do not support HTTP yet... }).show();
}
} }
} }

View file

@ -6,5 +6,6 @@
</string-array> </string-array>
<string-array name="clone_protocols"> <string-array name="clone_protocols">
<item>ssh://</item> <item>ssh://</item>
<item>https://</item>
</string-array> </string-array>
</resources> </resources>

View file

@ -63,6 +63,8 @@
<string name="server_url">Server URL</string> <string name="server_url">Server URL</string>
<string name="server_url_hint">server.com</string> <string name="server_url_hint">server.com</string>
<string name="server_port_hint">22</string> <string name="server_port_hint">22</string>
<string name="default_ssh_port">22</string>
<string name="default_https_port">443</string>
<string name="server_path">Repo path</string> <string name="server_path">Repo path</string>
<string name="server_path_hint">path/to/pass</string> <string name="server_path_hint">path/to/pass</string>
<string name="server_user">Username</string> <string name="server_user">Username</string>