]>
Commit | Line | Data |
---|---|---|
42aa7dfd | 1 | package com.joshuawise.dumload; |
2 | ||
3 | import java.io.File; | |
4 | ||
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; | |
14 | ||
15 | import com.jcraft.jsch.JSch; | |
16 | import com.jcraft.jsch.KeyPair; | |
17 | ||
18 | public class Preferences extends PreferenceActivity { | |
19 | ||
20 | private class KeygenTask extends AsyncTask<Void, Void, String> { | |
21 | ||
22 | @Override | |
23 | protected String doInBackground(Void... params) { | |
24 | String ret = null; | |
25 | String homedir = getFilesDir().getAbsolutePath(); | |
26 | String filename = homedir + "/id_dsa_generated"; | |
27 | File file = new File(filename); | |
28 | if (file.exists()) { | |
29 | return getString(R.string.msgKeyExists); | |
30 | } else { | |
31 | JSch jsch = new JSch(); | |
32 | int type = KeyPair.DSA; | |
33 | try { | |
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(); | |
40 | kpair.dispose(); | |
41 | } catch (Exception e) { | |
42 | System.out.println(e); | |
43 | } | |
44 | } | |
45 | return ret; | |
46 | } | |
47 | ||
48 | @Override | |
49 | protected void onPostExecute(String result) { | |
50 | setProgressBarIndeterminateVisibility(false); | |
51 | Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); | |
52 | super.onPostExecute(result); | |
53 | } | |
54 | ||
55 | } | |
56 | ||
57 | @Override | |
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); | |
63 | } | |
64 | ||
65 | @Override | |
66 | public boolean onCreateOptionsMenu(Menu menu) { | |
67 | super.onCreateOptionsMenu(menu); | |
68 | ||
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)); | |
73 | return true; | |
74 | } | |
75 | ||
76 | public boolean onOptionsItemSelected(MenuItem item) { | |
77 | switch (item.getItemId()) { | |
78 | case R.id.actionCreateSshKey: | |
79 | generateDSAKeyPair(); | |
80 | break; | |
81 | case R.id.actionRemoveSshKey: | |
82 | String homedir = getFilesDir().getAbsolutePath(); | |
83 | String filename = homedir + "/id_dsa_generated"; | |
84 | File file = new File(filename); | |
85 | if (file.exists()) { | |
86 | file.delete(); | |
87 | Toast.makeText(this, | |
88 | "Deleted", | |
89 | Toast.LENGTH_LONG).show(); | |
90 | } else { | |
91 | Toast.makeText(this, getString(R.string.msgNoGenKeyFound), Toast.LENGTH_SHORT).show(); | |
92 | } | |
93 | } | |
94 | return false; | |
95 | } | |
96 | ||
97 | private void generateDSAKeyPair() { | |
98 | setProgressBarIndeterminateVisibility(true); | |
99 | KeygenTask t = new KeygenTask(); | |
100 | t.execute(); | |
101 | } | |
102 | } |