cloning and pulling are not well encapsulated via a common interface
This commit is contained in:
parent
42f1abfa76
commit
4002791b15
3 changed files with 102 additions and 49 deletions
|
@ -5,9 +5,12 @@ import android.app.AlertDialog;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.text.InputType;
|
import android.text.InputType;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
@ -39,6 +42,7 @@ import org.eclipse.jgit.util.FS;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
// TODO move the messages to strings.xml
|
// TODO move the messages to strings.xml
|
||||||
|
|
||||||
|
@ -54,6 +58,8 @@ public class GitHandler extends Activity {
|
||||||
private String hostname;
|
private String hostname;
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
|
private SharedPreferences settings;
|
||||||
|
|
||||||
public static final int REQUEST_PULL = 101;
|
public static final int REQUEST_PULL = 101;
|
||||||
public static final int REQUEST_PUSH = 102;
|
public static final int REQUEST_PUSH = 102;
|
||||||
public static final int REQUEST_CLONE = 103;
|
public static final int REQUEST_CLONE = 103;
|
||||||
|
@ -65,6 +71,11 @@ public class GitHandler extends Activity {
|
||||||
context = getApplicationContext();
|
context = getApplicationContext();
|
||||||
activity = this;
|
activity = this;
|
||||||
|
|
||||||
|
settings = PreferenceManager.getDefaultSharedPreferences(this.context);
|
||||||
|
|
||||||
|
protocol = settings.getString("git_remote_protocol", "ssh://");
|
||||||
|
connectionMode = settings.getString("git_remote_auth", "username/password");
|
||||||
|
|
||||||
switch (getIntent().getExtras().getInt("Operation")) {
|
switch (getIntent().getExtras().getInt("Operation")) {
|
||||||
case REQUEST_CLONE:
|
case REQUEST_CLONE:
|
||||||
setContentView(R.layout.activity_git_clone);
|
setContentView(R.layout.activity_git_clone);
|
||||||
|
@ -114,7 +125,7 @@ public class GitHandler extends Activity {
|
||||||
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
|
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
|
||||||
String selection = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString();
|
String selection = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString();
|
||||||
|
|
||||||
if (selection.equalsIgnoreCase("ssh-key")) {
|
if (selection.equalsIgnoreCase("ssh-key (not yet implemented)")) {
|
||||||
new AlertDialog.Builder(activity)
|
new AlertDialog.Builder(activity)
|
||||||
.setMessage("Authentication method not implemented yet")
|
.setMessage("Authentication method not implemented yet")
|
||||||
.setPositiveButton("OK",
|
.setPositiveButton("OK",
|
||||||
|
@ -138,13 +149,29 @@ public class GitHandler extends Activity {
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case REQUEST_PULL:
|
case REQUEST_PULL:
|
||||||
authenticateThenPull(this);
|
authenticateAndRun("pullOperation");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
|
||||||
|
if (findViewById(R.id.clone_uri) != null) {
|
||||||
|
((EditText) findViewById(R.id.clone_uri)).setText(
|
||||||
|
settings.getString("git_remote_username", "user")
|
||||||
|
+ "@" +
|
||||||
|
settings.getString("git_remote_server", "server.com")
|
||||||
|
+ ":" +
|
||||||
|
settings.getString("git_remote_location", "path/to/repository")
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
// Inflate the menu; this adds items to the action bar if it is present.
|
// Inflate the menu; this adds items to the action bar if it is present.
|
||||||
|
@ -213,6 +240,7 @@ public class GitHandler extends Activity {
|
||||||
|
|
||||||
}
|
}
|
||||||
}).show();
|
}).show();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
this.dialog.dismiss();
|
this.dialog.dismiss();
|
||||||
setResult(RESULT_OK);
|
setResult(RESULT_OK);
|
||||||
|
@ -220,6 +248,13 @@ public class GitHandler extends Activity {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.dialog.dismiss();
|
this.dialog.dismiss();
|
||||||
|
|
||||||
|
// if we were unable to finish the job
|
||||||
|
try {
|
||||||
|
FileUtils.deleteDirectory(localDir);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -230,13 +265,17 @@ public class GitHandler extends Activity {
|
||||||
try {
|
try {
|
||||||
cmd[i].call();
|
cmd[i].call();
|
||||||
} catch (JGitInternalException e) {
|
} catch (JGitInternalException e) {
|
||||||
|
e.printStackTrace();
|
||||||
return -99;
|
return -99;
|
||||||
} catch (InvalidRemoteException e) {
|
} catch (InvalidRemoteException e) {
|
||||||
|
e.printStackTrace();
|
||||||
return -1;
|
return -1;
|
||||||
} catch (TransportException e) {
|
} catch (TransportException e) {
|
||||||
|
e.printStackTrace();
|
||||||
return -2;
|
return -2;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
return -99;
|
||||||
}
|
}
|
||||||
totalSize++;
|
totalSize++;
|
||||||
}
|
}
|
||||||
|
@ -301,7 +340,7 @@ public class GitHandler extends Activity {
|
||||||
public void onClick(DialogInterface dialog, int id) {
|
public void onClick(DialogInterface dialog, int id) {
|
||||||
try {
|
try {
|
||||||
FileUtils.deleteDirectory(localDir);
|
FileUtils.deleteDirectory(localDir);
|
||||||
authenticateThenClone(localDir);
|
authenticateAndRun("cloneOperation");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
//TODO Handle the exception correctly if we are unable to delete the directory...
|
//TODO Handle the exception correctly if we are unable to delete the directory...
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -324,7 +363,7 @@ public class GitHandler extends Activity {
|
||||||
show();
|
show();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
authenticateThenClone(localDir);
|
authenticateAndRun("cloneOperation");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//This is what happens when jgit fails :(
|
//This is what happens when jgit fails :(
|
||||||
//TODO Handle the diffent cases of exceptions
|
//TODO Handle the diffent cases of exceptions
|
||||||
|
@ -333,15 +372,54 @@ public class GitHandler extends Activity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void cloneOperation(UsernamePasswordCredentialsProvider provider) {
|
||||||
|
|
||||||
private void authenticateThenClone(final File localDir) {
|
// remember the settings
|
||||||
String connectionMode = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString();
|
SharedPreferences.Editor editor = settings.edit();
|
||||||
|
|
||||||
|
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.commit();
|
||||||
|
|
||||||
|
CloneCommand cmd = Git.cloneRepository().
|
||||||
|
setCredentialsProvider(provider).
|
||||||
|
setCloneAllBranches(true).
|
||||||
|
setDirectory(localDir).
|
||||||
|
setURI(hostname);
|
||||||
|
|
||||||
|
new CloneTask(activity).execute(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pullOperation(UsernamePasswordCredentialsProvider provider) {
|
||||||
|
new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File("")))
|
||||||
|
.pull()
|
||||||
|
.setRebase(true)
|
||||||
|
.setCredentialsProvider(provider));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Finds the method and provides it with authentication paramters via invokeWithAuthentication */
|
||||||
|
private void authenticateAndRun(String operation) {
|
||||||
|
try {
|
||||||
|
invokeWithAuthentication(this, this.getClass().getMethod(operation, UsernamePasswordCredentialsProvider.class));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Calls a method encapsulating a GitCommand and providing it with authentication parameters
|
||||||
|
*
|
||||||
|
* @param activity
|
||||||
|
* @param method
|
||||||
|
*/
|
||||||
|
private void invokeWithAuthentication(final GitHandler activity, final Method method) {
|
||||||
|
|
||||||
if (connectionMode.equalsIgnoreCase("ssh-key")) {
|
if (connectionMode.equalsIgnoreCase("ssh-key")) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (protocol.equals("ssh://")) {
|
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);
|
||||||
|
@ -355,14 +433,16 @@ public class GitHandler extends Activity {
|
||||||
public void onClick(DialogInterface dialog, int whichButton) {
|
public void onClick(DialogInterface dialog, int whichButton) {
|
||||||
|
|
||||||
SshSessionFactory.setInstance(new GitConfigSessionFactory());
|
SshSessionFactory.setInstance(new GitConfigSessionFactory());
|
||||||
|
try {
|
||||||
|
method.invoke(activity,
|
||||||
|
new UsernamePasswordCredentialsProvider(
|
||||||
|
settings.getString("git_remote_username", "git"),
|
||||||
|
password.getText().toString())
|
||||||
|
);
|
||||||
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
CloneCommand cmd = Git.cloneRepository().
|
|
||||||
setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password.getText().toString())).
|
|
||||||
setCloneAllBranches(true).
|
|
||||||
setDirectory(localDir).
|
|
||||||
setURI(hostname);
|
|
||||||
|
|
||||||
new CloneTask(activity).execute(cmd);
|
|
||||||
}
|
}
|
||||||
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
|
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int whichButton) {
|
public void onClick(DialogInterface dialog, int whichButton) {
|
||||||
|
@ -382,36 +462,4 @@ public class GitHandler extends Activity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void authenticateThenPull(final Activity activity) {
|
|
||||||
//TODO recall the username
|
|
||||||
//TODO offer the choice ssh and user/pwd
|
|
||||||
final EditText password = new EditText(activity);
|
|
||||||
password.setHint("Password");
|
|
||||||
password.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
|
|
||||||
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
|
||||||
|
|
||||||
new AlertDialog.Builder(activity)
|
|
||||||
.setTitle("Authenticate")
|
|
||||||
.setMessage("Please provide the password for this repository")
|
|
||||||
.setView(password)
|
|
||||||
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
|
||||||
public void onClick(DialogInterface dialog, int whichButton) {
|
|
||||||
|
|
||||||
SshSessionFactory.setInstance(new GitConfigSessionFactory());
|
|
||||||
|
|
||||||
new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File("")))
|
|
||||||
.pull()
|
|
||||||
.setRebase(true)
|
|
||||||
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("git", password.getText().toString())));
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
|
|
||||||
public void onClick(DialogInterface dialog, int whichButton) {
|
|
||||||
// Do nothing.
|
|
||||||
}
|
|
||||||
}).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -316,7 +316,7 @@ public class PgpHandler extends Activity {
|
||||||
for (int i = 0; i < ids.length; i++) {
|
for (int i = 0; i < ids.length; i++) {
|
||||||
keyIDs += OpenPgpUtils.convertKeyIdToHex(ids[i]) + ", ";
|
keyIDs += OpenPgpUtils.convertKeyIdToHex(ids[i]) + ", ";
|
||||||
}
|
}
|
||||||
settings.edit().putString("openpgp_key_ids", keyIDs);
|
settings.edit().putString("openpgp_key_ids", keyIDs).commit();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,14 @@
|
||||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<PreferenceCategory android:title="Git">
|
<PreferenceCategory android:title="Git">
|
||||||
<EditTextPreference android:title="Server"
|
<EditTextPreference android:title="Server"
|
||||||
android:key="git_remote_server"/>
|
android:key="git_remote_server"
|
||||||
|
android:hint="server.com"/>
|
||||||
|
<EditTextPreference android:title="Remote location"
|
||||||
|
android:key="git_remote_location"
|
||||||
|
android:hint="path/to/repository"/>
|
||||||
<EditTextPreference android:title="Username"
|
<EditTextPreference android:title="Username"
|
||||||
android:key="git_remote_username"/>
|
android:key="git_remote_username"
|
||||||
|
android:hint="username"/>
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
<PreferenceCategory android:title="Crypto">
|
<PreferenceCategory android:title="Crypto">
|
||||||
|
|
Loading…
Reference in a new issue