+package com.joshuawise.dumload;
+
+import java.io.File;
+
+import android.content.SharedPreferences;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceManager;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.Window;
+import android.widget.Toast;
+
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.KeyPair;
+
+public class Preferences extends PreferenceActivity {
+
+ private class KeygenTask extends AsyncTask<Void, Void, String> {
+
+ @Override
+ protected String doInBackground(Void... params) {
+ String ret = null;
+ String homedir = getFilesDir().getAbsolutePath();
+ String filename = homedir + "/id_dsa_generated";
+ File file = new File(filename);
+ if (file.exists()) {
+ return getString(R.string.msgKeyExists);
+ } else {
+ JSch jsch = new JSch();
+ int type = KeyPair.DSA;
+ try {
+ KeyPair kpair = KeyPair.genKeyPair(jsch, type);
+ // TODO: query for a passphrase
+ kpair.setPassphrase("");
+ kpair.writePrivateKey(filename);
+ kpair.writePublicKey(filename + ".pub", "generated by dumload");
+ ret = "Fingerprint: " + kpair.getFingerPrint();
+ kpair.dispose();
+ } catch (Exception e) {
+ System.out.println(e);
+ }
+ }
+ return ret;
+ }
+
+ @Override
+ protected void onPostExecute(String result) {
+ setProgressBarIndeterminateVisibility(false);
+ Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
+ super.onPostExecute(result);
+ }
+
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
+ super.onCreate(savedInstanceState);
+ // Load the preferences from an XML resource
+ addPreferencesFromResource(R.xml.preferences);
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ super.onCreateOptionsMenu(menu);
+
+ menu.add(Menu.NONE, R.id.actionCreateSshKey, Menu.NONE,
+ getString(R.string.menuGenSshKey));
+ menu.add(Menu.NONE, R.id.actionRemoveSshKey, Menu.NONE,
+ getString(R.string.menuDelSshKey));
+ return true;
+ }
+
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case R.id.actionCreateSshKey:
+ generateDSAKeyPair();
+ break;
+ case R.id.actionRemoveSshKey:
+ String homedir = getFilesDir().getAbsolutePath();
+ String filename = homedir + "/id_dsa_generated";
+ File file = new File(filename);
+ if (file.exists()) {
+ file.delete();
+ Toast.makeText(this,
+ "Deleted",
+ Toast.LENGTH_LONG).show();
+ } else {
+ Toast.makeText(this, getString(R.string.msgNoGenKeyFound), Toast.LENGTH_SHORT).show();
+ }
+ }
+ return false;
+ }
+
+ private void generateDSAKeyPair() {
+ setProgressBarIndeterminateVisibility(true);
+ KeygenTask t = new KeygenTask();
+ t.execute();
+ }
+}