1 package com.joshuawise.dumload;
5 import android.content.SharedPreferences;
6 import android.os.AsyncTask;
7 import android.os.Bundle;
8 import android.preference.PreferenceActivity;
9 import android.preference.PreferenceManager;
10 import android.view.Menu;
11 import android.view.MenuItem;
12 import android.view.Window;
13 import android.widget.Toast;
15 import com.jcraft.jsch.JSch;
16 import com.jcraft.jsch.KeyPair;
18 public class Preferences extends PreferenceActivity {
20 private class KeygenTask extends AsyncTask<Void, Void, String> {
23 protected String doInBackground(Void... params) {
25 String homedir = getFilesDir().getAbsolutePath();
26 String filename = homedir + "/id_dsa_generated";
27 File file = new File(filename);
29 return getString(R.string.msgKeyExists);
31 JSch jsch = new JSch();
32 int type = KeyPair.DSA;
34 KeyPair kpair = KeyPair.genKeyPair(jsch, type);
35 // TODO: query for a passphrase
36 kpair.setPassphrase("");
37 kpair.writePrivateKey(filename);
38 kpair.writePublicKey(filename + ".pub", "generated by dumload");
39 ret = "Fingerprint: " + kpair.getFingerPrint();
41 } catch (Exception e) {
42 System.out.println(e);
49 protected void onPostExecute(String result) {
50 setProgressBarIndeterminateVisibility(false);
51 Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
52 super.onPostExecute(result);
58 public void onCreate(Bundle savedInstanceState) {
59 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
60 super.onCreate(savedInstanceState);
61 // Load the preferences from an XML resource
62 addPreferencesFromResource(R.xml.preferences);
66 public boolean onCreateOptionsMenu(Menu menu) {
67 super.onCreateOptionsMenu(menu);
69 menu.add(Menu.NONE, R.id.actionCreateSshKey, Menu.NONE,
70 getString(R.string.menuGenSshKey));
71 menu.add(Menu.NONE, R.id.actionRemoveSshKey, Menu.NONE,
72 getString(R.string.menuDelSshKey));
76 public boolean onOptionsItemSelected(MenuItem item) {
77 switch (item.getItemId()) {
78 case R.id.actionCreateSshKey:
81 case R.id.actionRemoveSshKey:
82 String homedir = getFilesDir().getAbsolutePath();
83 String filename = homedir + "/id_dsa_generated";
84 File file = new File(filename);
89 Toast.LENGTH_LONG).show();
91 Toast.makeText(this, getString(R.string.msgNoGenKeyFound), Toast.LENGTH_SHORT).show();
97 private void generateDSAKeyPair() {
98 setProgressBarIndeterminateVisibility(true);
99 KeygenTask t = new KeygenTask();