From 2c5a80a16f3699287971b3fcdf38fbb4c42b84ae Mon Sep 17 00:00:00 2001 From: Thialfihar Date: Sat, 15 May 2010 16:09:49 +0000 Subject: [PATCH] added pass phrase cache to preferences, not used yet, but preference and about menu item now show up in every activity --- res/layout/preferences.xml | 41 +++++++++- res/values/strings.xml | 8 ++ .../thialfihar/android/apg/BaseActivity.java | 75 +++++++++++++++++++ src/org/thialfihar/android/apg/Constants.java | 1 + .../android/apg/EditKeyActivity.java | 14 ++-- .../thialfihar/android/apg/MainActivity.java | 45 +---------- .../android/apg/PreferencesActivity.java | 53 +++++++++++-- .../android/apg/PublicKeyListActivity.java | 11 +-- .../android/apg/SecretKeyListActivity.java | 12 ++- 9 files changed, 193 insertions(+), 67 deletions(-) diff --git a/res/layout/preferences.xml b/res/layout/preferences.xml index 350e965b4..81690987a 100644 --- a/res/layout/preferences.xml +++ b/res/layout/preferences.xml @@ -27,11 +27,48 @@ android:paddingTop="5dip" android:layout_marginRight="?android:attr/scrollbarSize"> + + + + + + + + + + + + - + - diff --git a/res/values/strings.xml b/res/values/strings.xml index 5bb4dc2ba..aaeec61ab 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -42,6 +42,7 @@ User IDs Keys + General Defaults @@ -92,6 +93,7 @@ Hash Algorithm Public Key Pass Phrase + Pass Phrase Cache Select 1 Selected @@ -111,6 +113,12 @@ Sign only Encrypt only Sign and Encrypt + 15 secs + 1 min + 3 mins + 5 mins + 10 mins + until quit DSA ElGamal diff --git a/src/org/thialfihar/android/apg/BaseActivity.java b/src/org/thialfihar/android/apg/BaseActivity.java index 6ab2089f3..73d2d004f 100644 --- a/src/org/thialfihar/android/apg/BaseActivity.java +++ b/src/org/thialfihar/android/apg/BaseActivity.java @@ -25,12 +25,20 @@ import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.view.ContextMenu; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ContextMenu.ContextMenuInfo; +import android.widget.TextView; import android.widget.Toast; public class BaseActivity extends Activity @@ -61,6 +69,35 @@ public class BaseActivity extends Activity Apg.initialize(this); } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + menu.add(0, Id.menu.option.preferences, 0, R.string.menu_preferences) + .setIcon(android.R.drawable.ic_menu_preferences); + menu.add(0, Id.menu.option.about, 1, R.string.menu_about) + .setIcon(android.R.drawable.ic_menu_info_details); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case Id.menu.option.about: { + showDialog(Id.dialog.about); + return true; + } + + case Id.menu.option.preferences: { + startActivity(new Intent(this, PreferencesActivity.class)); + return true; + } + + default: { + break; + } + } + return false; + } + @Override protected Dialog onCreateDialog(int id) { @@ -101,6 +138,34 @@ public class BaseActivity extends Activity mProgressDialog = null; switch (id) { + case Id.dialog.about: { + AlertDialog.Builder alert = new AlertDialog.Builder(this); + + alert.setTitle("About " + Apg.FULL_VERSION); + + LayoutInflater inflater = + (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View layout = inflater.inflate(R.layout.info, null); + TextView message = (TextView) layout.findViewById(R.id.message); + message.setText("This is an attempt to bring OpenPGP to Android. " + + "It is far from complete, but more features are planned (see website).\n\n" + + "Feel free to send bug reports, suggestions, feature requests, feedback, " + + "photographs.\n\n" + + "mail: thi@thialfihar.org\n" + + "site: http://apg.thialfihar.org\n\n" + + "This software is provided \"as is\", without warranty of any kind."); + alert.setView(layout); + + alert.setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + BaseActivity.this.removeDialog(Id.dialog.about); + } + }); + + return alert.create(); + } + case Id.dialog.pass_phrase: { return AskForSecretKeyPassPhrase.createDialog(this, getSecretKeyId(), this); } @@ -291,6 +356,16 @@ public class BaseActivity extends Activity return mSecretKeyId; } + public int getPassPhraseCache() { + return mPreferences.getInt(Constants.pref.pass_phrase_cache_length, 300); + } + + public void setPassPhraseCache(int value) { + SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt(Constants.pref.pass_phrase_cache_length, value); + editor.commit(); + } + public int getDefaultEncryptionAlgorithm() { return mPreferences.getInt(Constants.pref.default_encryption_algorithm, PGPEncryptedData.AES_256); diff --git a/src/org/thialfihar/android/apg/Constants.java b/src/org/thialfihar/android/apg/Constants.java index 7ddc13985..40a77645d 100644 --- a/src/org/thialfihar/android/apg/Constants.java +++ b/src/org/thialfihar/android/apg/Constants.java @@ -28,5 +28,6 @@ public final class Constants { public static final String default_encryption_algorithm = "defaultEncryptionAlgorithm"; public static final String default_hash_algorithm = "defaultHashAlgorithm"; public static final String default_ascii_armour = "defaultAsciiArmour"; + public static final String pass_phrase_cache_length = "passPhraseCacheLength"; } } diff --git a/src/org/thialfihar/android/apg/EditKeyActivity.java b/src/org/thialfihar/android/apg/EditKeyActivity.java index eb462d24a..a1c74951e 100644 --- a/src/org/thialfihar/android/apg/EditKeyActivity.java +++ b/src/org/thialfihar/android/apg/EditKeyActivity.java @@ -128,8 +128,12 @@ public class EditKeyActivity extends BaseActivity implements OnClickListener { @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, Id.menu.option.new_pass_phrase, 0, - (havePassPhrase() ? R.string.menu_changePassPhrase : R.string.menu_setCachedPassPhrase)) + (havePassPhrase() ? R.string.menu_changePassPhrase : R.string.menu_setPassPhrase)) .setIcon(android.R.drawable.ic_menu_add); + menu.add(0, Id.menu.option.preferences, 1, R.string.menu_preferences) + .setIcon(android.R.drawable.ic_menu_preferences); + menu.add(0, Id.menu.option.about, 2, R.string.menu_about) + .setIcon(android.R.drawable.ic_menu_info_details); return true; } @@ -142,10 +146,9 @@ public class EditKeyActivity extends BaseActivity implements OnClickListener { } default: { - break; + return super.onOptionsItemSelected(item); } } - return false; } @Override @@ -157,7 +160,7 @@ public class EditKeyActivity extends BaseActivity implements OnClickListener { if (havePassPhrase()) { alert.setTitle(R.string.title_changePassPhrase); } else { - alert.setTitle(R.string.title_setCachedPassPhrase); + alert.setTitle(R.string.title_setPassPhrase); } alert.setMessage(R.string.enterPassPhraseTwice); @@ -201,10 +204,9 @@ public class EditKeyActivity extends BaseActivity implements OnClickListener { } default: { - break; + return super.onCreateDialog(id); } } - return super.onCreateDialog(id); } @Override diff --git a/src/org/thialfihar/android/apg/MainActivity.java b/src/org/thialfihar/android/apg/MainActivity.java index 4bf585df4..a4d584304 100644 --- a/src/org/thialfihar/android/apg/MainActivity.java +++ b/src/org/thialfihar/android/apg/MainActivity.java @@ -177,34 +177,6 @@ public class MainActivity extends BaseActivity { return alert.create(); } - case Id.dialog.about: { - AlertDialog.Builder alert = new AlertDialog.Builder(this); - - alert.setTitle("About " + Apg.FULL_VERSION); - - LayoutInflater inflater = - (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - View layout = inflater.inflate(R.layout.info, null); - TextView message = (TextView) layout.findViewById(R.id.message); - message.setText("This is an attempt to bring OpenPGP to Android. " + - "It is far from complete, but more features are planned (see website).\n\n" + - "Feel free to send bug reports, suggestions, feature requests, feedback, " + - "photographs.\n\n" + - "mail: thi@thialfihar.org\n" + - "site: http://apg.thialfihar.org\n\n" + - "This software is provided \"as is\", without warranty of any kind."); - alert.setView(layout); - - alert.setPositiveButton(android.R.string.ok, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - MainActivity.this.removeDialog(Id.dialog.about); - } - }); - - return alert.create(); - } - case Id.dialog.change_log: { AlertDialog.Builder alert = new AlertDialog.Builder(this); @@ -243,11 +215,9 @@ public class MainActivity extends BaseActivity { } default: { - break; + return super.onCreateDialog(id); } } - - return super.onCreateDialog(id); } @Override @@ -273,11 +243,6 @@ public class MainActivity extends BaseActivity { return true; } - case Id.menu.option.about: { - showDialog(Id.dialog.about); - return true; - } - case Id.menu.option.manage_public_keys: { startActivity(new Intent(this, PublicKeyListActivity.class)); return true; @@ -288,16 +253,10 @@ public class MainActivity extends BaseActivity { return true; } - case Id.menu.option.preferences: { - startActivity(new Intent(this, PreferencesActivity.class)); - return true; - } - default: { - break; + return super.onOptionsItemSelected(item); } } - return false; } @Override diff --git a/src/org/thialfihar/android/apg/PreferencesActivity.java b/src/org/thialfihar/android/apg/PreferencesActivity.java index 607bb9c8b..8a1c02475 100644 --- a/src/org/thialfihar/android/apg/PreferencesActivity.java +++ b/src/org/thialfihar/android/apg/PreferencesActivity.java @@ -30,6 +30,7 @@ import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class PreferencesActivity extends BaseActivity { + private Spinner mPassPhraseCache = null; private Spinner mEncryptionAlgorithm = null; private Spinner mHashAlgorithm = null; private CheckBox mAsciiArmour = null; @@ -39,13 +40,48 @@ public class PreferencesActivity extends BaseActivity { super.onCreate(savedInstanceState); setContentView(R.layout.preferences); + mPassPhraseCache = (Spinner) findViewById(R.id.passPhraseCache); + + Choice choices[] = { + new Choice(15, getString(R.string.choice_15secs)), + new Choice(60, getString(R.string.choice_1min)), + new Choice(180, getString(R.string.choice_3mins)), + new Choice(300, getString(R.string.choice_5mins)), + new Choice(600, getString(R.string.choice_10mins)), + new Choice(0, getString(R.string.choice_untilQuit)), + }; + ArrayAdapter adapter = + new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + mPassPhraseCache.setAdapter(adapter); + + int passPhraseCache = getPassPhraseCache(); + for (int i = 0; i < choices.length; ++i) { + if (choices[i].getId() == passPhraseCache) { + mPassPhraseCache.setSelection(i); + break; + } + } + + mPassPhraseCache.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapter, View view, int index, long id) { + setPassPhraseCache(((Choice) mPassPhraseCache.getSelectedItem()).getId()); + } + + @Override + public void onNothingSelected(AdapterView adapter) { + // nothing to do + } + }); + mEncryptionAlgorithm = (Spinner) findViewById(R.id.encryptionAlgorithm); mHashAlgorithm = (Spinner) findViewById(R.id.hashAlgorithm); mAsciiArmour = (CheckBox) findViewById(R.id.asciiArmour); mAsciiArmour.setChecked(getDefaultAsciiArmour()); - Choice choices[] = { + choices = new Choice[] { new Choice(PGPEncryptedData.AES_128, "AES 128"), new Choice(PGPEncryptedData.AES_192, "AES 192"), new Choice(PGPEncryptedData.AES_256, "AES 256"), @@ -56,13 +92,13 @@ public class PreferencesActivity extends BaseActivity { new Choice(PGPEncryptedData.TRIPLE_DES, "Triple DES"), new Choice(PGPEncryptedData.IDEA, "IDEA"), }; - ArrayAdapter adapter = - new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices); + adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mEncryptionAlgorithm.setAdapter(adapter); + int defaultEncryptionAlgorithm = getDefaultEncryptionAlgorithm(); for (int i = 0; i < choices.length; ++i) { - if (choices[i].getId() == getDefaultEncryptionAlgorithm()) { + if (choices[i].getId() == defaultEncryptionAlgorithm) { mEncryptionAlgorithm.setSelection(i); break; } @@ -80,7 +116,7 @@ public class PreferencesActivity extends BaseActivity { } }); - Choice choices2[] = { + choices = new Choice[] { new Choice(HashAlgorithmTags.MD5, "MD5"), new Choice(HashAlgorithmTags.RIPEMD160, "RIPEMD160"), new Choice(HashAlgorithmTags.SHA1, "SHA1"), @@ -89,12 +125,13 @@ public class PreferencesActivity extends BaseActivity { new Choice(HashAlgorithmTags.SHA384, "SHA384"), new Choice(HashAlgorithmTags.SHA512, "SHA512"), }; - adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices2); + adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, choices); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mHashAlgorithm.setAdapter(adapter); - for (int i = 0; i < choices2.length; ++i) { - if (choices2[i].getId() == getDefaultHashAlgorithm()) { + int defaultHashAlgorithm = getDefaultHashAlgorithm(); + for (int i = 0; i < choices.length; ++i) { + if (choices[i].getId() == defaultHashAlgorithm) { mHashAlgorithm.setSelection(i); break; } diff --git a/src/org/thialfihar/android/apg/PublicKeyListActivity.java b/src/org/thialfihar/android/apg/PublicKeyListActivity.java index caf28a391..67bc608ad 100644 --- a/src/org/thialfihar/android/apg/PublicKeyListActivity.java +++ b/src/org/thialfihar/android/apg/PublicKeyListActivity.java @@ -72,6 +72,10 @@ public class PublicKeyListActivity extends BaseActivity { .setIcon(android.R.drawable.ic_menu_add); menu.add(0, Id.menu.option.export_keys, 1, R.string.menu_exportKeys) .setIcon(android.R.drawable.ic_menu_save); + menu.add(1, Id.menu.option.preferences, 2, R.string.menu_preferences) + .setIcon(android.R.drawable.ic_menu_preferences); + menu.add(1, Id.menu.option.about, 3, R.string.menu_about) + .setIcon(android.R.drawable.ic_menu_info_details); return true; } @@ -89,10 +93,9 @@ public class PublicKeyListActivity extends BaseActivity { } default: { - break; + return super.onOptionsItemSelected(item); } } - return false; } @Override @@ -230,11 +233,9 @@ public class PublicKeyListActivity extends BaseActivity { } default: { - break; + return super.onCreateDialog(id); } } - - return super.onCreateDialog(id); } public void importKeys() { diff --git a/src/org/thialfihar/android/apg/SecretKeyListActivity.java b/src/org/thialfihar/android/apg/SecretKeyListActivity.java index 7b1ef4334..6272db9ce 100644 --- a/src/org/thialfihar/android/apg/SecretKeyListActivity.java +++ b/src/org/thialfihar/android/apg/SecretKeyListActivity.java @@ -76,6 +76,10 @@ public class SecretKeyListActivity extends BaseActivity implements OnChildClickL .setIcon(android.R.drawable.ic_menu_save); menu.add(1, Id.menu.option.create, 2, R.string.menu_createKey) .setIcon(android.R.drawable.ic_menu_add); + menu.add(2, Id.menu.option.preferences, 3, R.string.menu_preferences) + .setIcon(android.R.drawable.ic_menu_preferences); + menu.add(2, Id.menu.option.about, 4, R.string.menu_about) + .setIcon(android.R.drawable.ic_menu_info_details); return true; } @@ -98,10 +102,9 @@ public class SecretKeyListActivity extends BaseActivity implements OnChildClickL } default: { - break; + return super.onOptionsItemSelected(item); } } - return false; } @Override @@ -259,8 +262,11 @@ public class SecretKeyListActivity extends BaseActivity implements OnChildClickL long keyId = keyRing.getSecretKey().getKeyID(); return AskForSecretKeyPassPhrase.createDialog(this, keyId, this); } + + default: { + return super.onCreateDialog(id); + } } - return super.onCreateDialog(id); } @Override