From 13ed3bbd8a99b8031dc959f2cb367f5fcc6daba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jerzy=20Ko=C5=82osowski?= Date: Mon, 13 Oct 2014 04:47:48 +0200 Subject: [PATCH] Added option for num keypad for YubiKey --- .../sufficientlysecure/keychain/Constants.java | 1 + .../keychain/ui/PassphraseDialogActivity.java | 14 +++++++++++++- .../keychain/ui/PreferencesActivity.java | 16 ++++++++++++++++ .../ui/dialog/PassphraseDialogFragment.java | 13 ++++++++++++- .../keychain/util/Preferences.java | 10 ++++++++++ .../src/main/res/layout/passphrase_dialog.xml | 1 - OpenKeychain/src/main/res/values/strings.xml | 1 + .../src/main/res/xml/adv_preferences.xml | 6 +++++- 8 files changed, 58 insertions(+), 4 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index a32c14ca0..04a932658 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -83,6 +83,7 @@ public final class Constants { public static final String SEARCH_KEYSERVER = "search_keyserver_pref"; public static final String SEARCH_KEYBASE = "search_keybase_pref"; public static final String USE_DEFAULT_YUBIKEY_PIN = "useDefaultYubikeyPin"; + public static final String USE_NUMKEYPAD_FOR_YUBIKEY_PIN="useNumKeypadForYubikeyPin"; } public static final class Defaults { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java index b9761fbf3..deff648ba 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java @@ -27,6 +27,8 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; +import android.text.InputType; +import android.text.method.PasswordTransformationMethod; import android.view.ContextThemeWrapper; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -52,6 +54,7 @@ import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Preferences; /** * We can not directly create a dialog on the application context. @@ -138,6 +141,7 @@ public class PassphraseDialogActivity extends FragmentActivity { alert.setTitle(R.string.title_authentication); String userId; + CanonicalizedSecretKey.SecretKeyType keyType = CanonicalizedSecretKey.SecretKeyType.PASSPHRASE; if (mSubKeyId == Constants.key.symmetric || mSubKeyId == Constants.key.none) { alert.setMessage(R.string.passphrase_for_symmetric_encryption); @@ -161,7 +165,7 @@ public class PassphraseDialogActivity extends FragmentActivity { long masterKeyId = new ProviderHelper(activity).getMasterKeyId(mSubKeyId); CachedPublicKeyRing keyRing = new ProviderHelper(activity).getCachedPublicKeyRing(masterKeyId); // get the type of key (from the database) - CanonicalizedSecretKey.SecretKeyType keyType = keyRing.getSecretKeyType(mSubKeyId); + keyType = keyRing.getSecretKeyType(mSubKeyId); switch (keyType) { case PASSPHRASE: message = getString(R.string.passphrase_for, userId); @@ -231,6 +235,14 @@ public class PassphraseDialogActivity extends FragmentActivity { mPassphraseEditText.setImeActionLabel(getString(android.R.string.ok), EditorInfo.IME_ACTION_DONE); mPassphraseEditText.setOnEditorActionListener(this); + if (keyType == CanonicalizedSecretKey.SecretKeyType.DIVERT_TO_CARD && Preferences.getPreferences(activity).useNumKeypadForYubikeyPin()) { + mPassphraseEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_PASSWORD); + } else { + mPassphraseEditText.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + } + + mPassphraseEditText.setTransformationMethod(PasswordTransformationMethod.getInstance()); + AlertDialog dialog = alert.create(); dialog.setButton(DialogInterface.BUTTON_POSITIVE, activity.getString(android.R.string.ok), (DialogInterface.OnClickListener) null); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PreferencesActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PreferencesActivity.java index 9d3175d15..51fac4779 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PreferencesActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PreferencesActivity.java @@ -127,6 +127,9 @@ public class PreferencesActivity extends PreferenceActivity { initializeUseDefaultYubikeyPin( (CheckBoxPreference) findPreference(Constants.Pref.USE_DEFAULT_YUBIKEY_PIN)); + initializeUseNumKeypadForYubikeyPin( + (CheckBoxPreference) findPreference(Constants.Pref.USE_NUMKEYPAD_FOR_YUBIKEY_PIN)); + } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { // Load the legacy preferences headers addPreferencesFromResource(R.xml.preference_headers_legacy); @@ -278,6 +281,9 @@ public class PreferencesActivity extends PreferenceActivity { initializeUseDefaultYubikeyPin( (CheckBoxPreference) findPreference(Constants.Pref.USE_DEFAULT_YUBIKEY_PIN)); + + initializeUseNumKeypadForYubikeyPin( + (CheckBoxPreference) findPreference(Constants.Pref.USE_NUMKEYPAD_FOR_YUBIKEY_PIN)); } } @@ -463,4 +469,14 @@ public class PreferencesActivity extends PreferenceActivity { }); } + private static void initializeUseNumKeypadForYubikeyPin(final CheckBoxPreference mUseNumKeypadForYubikeyPin) { + mUseNumKeypadForYubikeyPin.setChecked(sPreferences.useNumKeypadForYubikeyPin()); + mUseNumKeypadForYubikeyPin.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + public boolean onPreferenceChange(Preference preference, Object newValue) { + mUseNumKeypadForYubikeyPin.setChecked((Boolean) newValue); + sPreferences.setUseNumKeypadForYubikeyPin((Boolean) newValue); + return false; + } + }); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index 8560bccc0..40ee8e0e9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -31,6 +31,8 @@ import android.os.Messenger; import android.os.RemoteException; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; +import android.text.InputType; +import android.text.method.PasswordTransformationMethod; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -54,6 +56,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Preferences; public class PassphraseDialogFragment extends DialogFragment implements OnEditorActionListener { private static final String ARG_MESSENGER = "messenger"; @@ -132,6 +135,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor alert.setTitle(R.string.title_authentication); String userId; + CanonicalizedSecretKey.SecretKeyType keyType = CanonicalizedSecretKey.SecretKeyType.PASSPHRASE; if (mSubKeyId == Constants.key.symmetric || mSubKeyId == Constants.key.none) { alert.setMessage(R.string.passphrase_for_symmetric_encryption); @@ -155,7 +159,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor long masterKeyId = new ProviderHelper(getActivity()).getMasterKeyId(mSubKeyId); CachedPublicKeyRing keyRing = new ProviderHelper(getActivity()).getCachedPublicKeyRing(masterKeyId); // get the type of key (from the database) - CanonicalizedSecretKey.SecretKeyType keyType = keyRing.getSecretKeyType(mSubKeyId); + keyType = keyRing.getSecretKeyType(mSubKeyId); switch (keyType) { case PASSPHRASE: message = getString(R.string.passphrase_for, userId); @@ -226,6 +230,13 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor mPassphraseEditText.setImeActionLabel(getString(android.R.string.ok), EditorInfo.IME_ACTION_DONE); mPassphraseEditText.setOnEditorActionListener(this); + if (keyType == CanonicalizedSecretKey.SecretKeyType.DIVERT_TO_CARD && Preferences.getPreferences(activity).useNumKeypadForYubikeyPin()) { + mPassphraseEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_PASSWORD); + } else { + mPassphraseEditText.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); + } + mPassphraseEditText.setTransformationMethod(PasswordTransformationMethod.getInstance()); + AlertDialog dialog = alert.create(); dialog.setButton(DialogInterface.BUTTON_POSITIVE, activity.getString(android.R.string.ok), (DialogInterface.OnClickListener) null); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index bdb7c65fc..52e083fd4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -191,6 +191,16 @@ public class Preferences { editor.commit(); } + public boolean useNumKeypadForYubikeyPin() { + return mSharedPreferences.getBoolean(Pref.USE_NUMKEYPAD_FOR_YUBIKEY_PIN, false); + } + + public void setUseNumKeypadForYubikeyPin(boolean useNumKeypadForYubikeyPin) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putBoolean(Pref.USE_NUMKEYPAD_FOR_YUBIKEY_PIN, useNumKeypadForYubikeyPin); + editor.commit(); + } + public void setFirstTime(boolean value) { SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putBoolean(Constants.Pref.FIRST_TIME, value); diff --git a/OpenKeychain/src/main/res/layout/passphrase_dialog.xml b/OpenKeychain/src/main/res/layout/passphrase_dialog.xml index f003e6994..2ca5199fd 100644 --- a/OpenKeychain/src/main/res/layout/passphrase_dialog.xml +++ b/OpenKeychain/src/main/res/layout/passphrase_dialog.xml @@ -19,7 +19,6 @@ android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:imeOptions="actionDone" - android:inputType="textPassword" android:hint="@string/label_passphrase" android:ems="10" android:layout_gravity="center_horizontal" /> diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 0b0f0b4fd..3720826fe 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -112,6 +112,7 @@ "Let others know that you're using OpenKeychain" "Writes 'OpenKeychain v2.7' to OpenPGP signatures, ciphertext, and exported keys" "Use default Yubikey PIN" + Use number keypad for Yubikey PIN "Uses default PIN (123456) to access Yubikeys over NFC" "Signed by:" "Encrypt to:" diff --git a/OpenKeychain/src/main/res/xml/adv_preferences.xml b/OpenKeychain/src/main/res/xml/adv_preferences.xml index 92e0f2ffc..fcca59abe 100644 --- a/OpenKeychain/src/main/res/xml/adv_preferences.xml +++ b/OpenKeychain/src/main/res/xml/adv_preferences.xml @@ -44,5 +44,9 @@ android:defaultValue="true" android:title="@string/label_use_default_yubikey_pin" android:summary="@string/label_label_use_default_yubikey_pin_summary" /> - +