diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EmailEditText.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EmailEditText.java index e7ef788d8..1bdec7b84 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EmailEditText.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EmailEditText.java @@ -35,43 +35,33 @@ import org.sufficientlysecure.keychain.util.ContactHelper; import java.util.regex.Matcher; public class EmailEditText extends AutoCompleteTextView { - EmailEditText emailEditText; public EmailEditText(Context context) { super(context); - emailEditText = this; - this.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); - this.addTextChangedListener(textWatcher); - removeFlag(); - makeAdapter(); + init(); } public EmailEditText(Context context, AttributeSet attrs) { super(context, attrs); - emailEditText = this; - this.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); - this.addTextChangedListener(textWatcher); - removeFlag(); - makeAdapter(); + init(); } public EmailEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - emailEditText = this; - this.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); - this.addTextChangedListener(textWatcher); - removeFlag(); - makeAdapter(); + init(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public EmailEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - emailEditText = this; + init(); + } + + private void init() { this.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS); this.addTextChangedListener(textWatcher); removeFlag(); - makeAdapter(); + initAdapter(); } TextWatcher textWatcher = new TextWatcher() { @@ -91,25 +81,29 @@ public class EmailEditText extends AutoCompleteTextView { if (email.length() > 0) { Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email); if (emailMatcher.matches()) { - emailEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, + EmailEditText.this.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.uid_mail_ok, 0); } else { - emailEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, + EmailEditText.this.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.uid_mail_bad, 0); } } else { // remove drawable if email is empty - emailEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + EmailEditText.this.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } } }; - private void makeAdapter() { - this.setThreshold(1); // Start working from first character - this.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, + private void initAdapter() { + setThreshold(1); // Start working from first character + setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, ContactHelper.getPossibleUserEmails(getContext()))); } + /** + * Hack to re-enable keyboard auto correction in AutoCompleteTextView. + * From http://stackoverflow.com/a/22512858 + */ private void removeFlag() { int inputType = getInputType(); inputType &= ~EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/NameEditText.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/NameEditText.java index 895cd45ec..f086c5696 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/NameEditText.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/NameEditText.java @@ -30,39 +30,44 @@ import org.sufficientlysecure.keychain.util.ContactHelper; public class NameEditText extends AutoCompleteTextView { public NameEditText(Context context) { super(context); - removeFlag(); - makeAdapter(); + init(); } public NameEditText(Context context, AttributeSet attrs) { super(context, attrs); - removeFlag(); - makeAdapter(); + init(); } public NameEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - removeFlag(); - makeAdapter(); + init(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public NameEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - removeFlag(); - makeAdapter(); + init(); } + private void init() { + removeFlag(); + initAdapter(); + } + + private void initAdapter() { + setThreshold(1); // Start working from first character + setAdapter(new ArrayAdapter<>( + getContext(), android.R.layout.simple_spinner_dropdown_item, + ContactHelper.getPossibleUserNames(getContext()))); + } + + /** + * Hack to re-enable keyboard auto correction in AutoCompleteTextView. + * From http://stackoverflow.com/a/22512858 + */ private void removeFlag() { int inputType = getInputType(); inputType &= ~EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE; setRawInputType(inputType); } - - private void makeAdapter() { - this.setThreshold(1); // Start working from first character - this.setAdapter(new ArrayAdapter<>( - getContext(), android.R.layout.simple_spinner_dropdown_item, - ContactHelper.getPossibleUserNames(getContext()))); - } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PassphraseEditText.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PassphraseEditText.java index 11d9e8fd0..377f701d1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PassphraseEditText.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PassphraseEditText.java @@ -36,6 +36,10 @@ public class PassphraseEditText extends EditText { public PassphraseEditText(Context context, AttributeSet attrs) { super(context, attrs); + init(context, attrs); + } + + private void init(Context context, AttributeSet attrs) { mPasswordBarHeight = (int) (8 * getResources().getDisplayMetrics().density); mPasswordBarWidth = (int) (50 * getResources().getDisplayMetrics().density);