From d097131a3dcdf806608f43e49e0be96cf34f5c64 Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Tue, 14 Apr 2015 03:40:59 +0100 Subject: -Improved email sanity verifications when adding emails when creating a new Key -fixed a few NFC crashes -fixed a few instances when showing the keyboard would crash the Activity -fixed a case where adding new emails would crash the app if the user went back and forth between the Add Email fragment and the Add Name fragment. --- .../keychain/ui/CreateKeyEmailFragment.java | 95 ++++++++++++++++------ .../keychain/ui/base/BaseNfcActivity.java | 6 +- .../keychain/ui/dialog/AddEmailDialogFragment.java | 8 +- .../src/main/res/layout/add_email_dialog.xml | 1 + .../main/res/layout/create_key_email_fragment.xml | 1 + OpenKeychain/src/main/res/values/strings.xml | 1 + 6 files changed, 82 insertions(+), 30 deletions(-) (limited to 'OpenKeychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java index 85e2f8e9d..26c307957 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java @@ -18,7 +18,6 @@ package org.sufficientlysecure.keychain.ui; import android.app.Activity; -import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -27,6 +26,7 @@ import android.support.v4.app.Fragment; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Patterns; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -44,6 +44,7 @@ import org.sufficientlysecure.keychain.ui.widget.EmailEditText; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; public class CreateKeyEmailFragment extends Fragment { @@ -73,14 +74,13 @@ public class CreateKeyEmailFragment extends Fragment { * Checks if text of given EditText is not empty. If it is empty an error is * set and the EditText gets the focus. * - * @param context * @param editText * @return true if EditText is not empty */ - private static boolean isEditTextNotEmpty(Context context, EditText editText) { + private boolean isMainEmailValid(EditText editText) { boolean output = true; - if (editText.getText().length() == 0) { - editText.setError(context.getString(R.string.create_key_empty)); + if (!checkEmail(editText.getText().toString(), false)) { + editText.setError(getString(R.string.create_key_empty)); editText.requestFocus(); output = false; } else { @@ -125,7 +125,7 @@ public class CreateKeyEmailFragment extends Fragment { // initial values if (mAdditionalEmailModels == null) { mAdditionalEmailModels = new ArrayList<>(); - if (mCreateKeyActivity.mAdditionalEmails != null) { + if (mCreateKeyActivity.mAdditionalEmails != null && mEmailAdapter != null) { mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails); } } @@ -144,6 +144,65 @@ public class CreateKeyEmailFragment extends Fragment { return view; } + /** + * Checks if a given email is valid + * + * @param email + * @param additionalEmail + * @return + */ + private boolean checkEmail(String email, boolean additionalEmail) { + //check for email format or if the user did any input + if (!isEmailFormatValid(email)) { + Notify.create(getActivity(), + getString(R.string.create_key_email_invalid_email), + Notify.LENGTH_LONG, Notify.Style.ERROR).show(); + return false; + } + + //check for duplicated emails + if (!additionalEmail && isEmailDuplicatedInsideAdapter(email) || additionalEmail && + mEmailEdit.getText().length() > 0 && email.equals(mEmailEdit.getText().toString())) { + Notify.create(getActivity(), + getString(R.string.create_key_email_already_exists_text), + Notify.LENGTH_LONG, Notify.Style.ERROR).show(); + return false; + } + + return true; + } + + /** + * Checks the email format + * Uses the default Android Email Pattern + * + * @param email + * @return + */ + private boolean isEmailFormatValid(String email) { + Pattern emailPattern = Patterns.EMAIL_ADDRESS; + + //check for email format or if the user did any input + return !(email.length() == 0 || !emailPattern.matcher(email).matches()); + } + + /** + * Checks for duplicated emails inside the additional email adapter. + * + * @param email + * @return + */ + private boolean isEmailDuplicatedInsideAdapter(String email) { + //check for duplicated emails inside the adapter + for (EmailAdapter.ViewModel model : mAdditionalEmailModels) { + if (email.equals(model.email)) { + return true; + } + } + + return false; + } + private void addEmail() { Handler returnHandler = new Handler() { @Override @@ -152,26 +211,10 @@ public class CreateKeyEmailFragment extends Fragment { Bundle data = message.getData(); String email = data.getString(AddEmailDialogFragment.MESSAGE_DATA_EMAIL); - - if (email.length() > 0 && mEmailEdit.getText().length() > 0 && - email.equals(mEmailEdit.getText().toString())) { - Notify.create(getActivity(), - getString(R.string.create_key_email_already_exists_text), - Notify.LENGTH_LONG, Notify.Style.ERROR).show(); - return; - } - //check for duplicated emails inside the adapter - for (EmailAdapter.ViewModel model : mAdditionalEmailModels) { - if (email.equals(model.email)) { - Notify.create(getActivity(), - getString(R.string.create_key_email_already_exists_text), - Notify.LENGTH_LONG, Notify.Style.ERROR).show(); - return; - } + if (checkEmail(email, true)) { + // add new user id + mEmailAdapter.add(email); } - - // add new user id - mEmailAdapter.add(email); } } }; @@ -191,7 +234,7 @@ public class CreateKeyEmailFragment extends Fragment { } private void nextClicked() { - if (isEditTextNotEmpty(getActivity(), mEmailEdit)) { + if (isMainEmailValid(mEmailEdit)) { // save state mCreateKeyActivity.mEmail = mEmailEdit.getText().toString(); mCreateKeyActivity.mAdditionalEmails = getAdditionalEmails(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/BaseNfcActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/BaseNfcActivity.java index 9b10ccdb1..4f6d5807e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/BaseNfcActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/BaseNfcActivity.java @@ -451,6 +451,8 @@ public abstract class BaseNfcActivity extends BaseActivity { */ public void enableNfcForegroundDispatch() { mNfcAdapter = NfcAdapter.getDefaultAdapter(this); + if(mNfcAdapter == null) return; + Intent nfcI = new Intent(this, getClass()) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent nfcPendingIntent = PendingIntent.getActivity(this, 0, nfcI, PendingIntent.FLAG_CANCEL_CURRENT); @@ -472,7 +474,9 @@ public abstract class BaseNfcActivity extends BaseActivity { * Disable foreground dispatch in onPause! */ public void disableNfcForegroundDispatch() { - mNfcAdapter.disableForegroundDispatch(this); + if(mNfcAdapter != null) { + mNfcAdapter.disableForegroundDispatch(this); + } Log.d(Constants.TAG, "NfcForegroundDispatch has been disabled!"); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEmailDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEmailDialogFragment.java index 5d5ca533e..5b91b9d37 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEmailDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEmailDialogFragment.java @@ -112,9 +112,11 @@ public class AddEmailDialogFragment extends DialogFragment implements OnEditorAc mEmail.post(new Runnable() { @Override public void run() { - InputMethodManager imm = (InputMethodManager) getActivity() - .getSystemService(Context.INPUT_METHOD_SERVICE); - imm.showSoftInput(mEmail, InputMethodManager.SHOW_IMPLICIT); + if(getActivity() != null) { + InputMethodManager imm = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(mEmail, InputMethodManager.SHOW_IMPLICIT); + } } }); } diff --git a/OpenKeychain/src/main/res/layout/add_email_dialog.xml b/OpenKeychain/src/main/res/layout/add_email_dialog.xml index 68d895145..2de657a40 100644 --- a/OpenKeychain/src/main/res/layout/add_email_dialog.xml +++ b/OpenKeychain/src/main/res/layout/add_email_dialog.xml @@ -21,6 +21,7 @@ android:layout_marginTop="16dp" android:hint="@string/label_email" android:imeOptions="actionNext" + android:inputType="textEmailAddress" android:textAppearance="?android:attr/textAppearanceMedium" /> diff --git a/OpenKeychain/src/main/res/layout/create_key_email_fragment.xml b/OpenKeychain/src/main/res/layout/create_key_email_fragment.xml index 17cfe54ac..7b9cffec4 100644 --- a/OpenKeychain/src/main/res/layout/create_key_email_fragment.xml +++ b/OpenKeychain/src/main/res/layout/create_key_email_fragment.xml @@ -32,6 +32,7 @@ android:layout_marginTop="16dp" android:layout_marginBottom="8dp" android:imeOptions="actionNext" + android:inputType="textEmailAddress" android:hint="@string/label_email" android:ems="10" /> diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index c1e3b51f9..c1ff5a1d1 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -631,6 +631,7 @@ "Add email address" "Additional email addresses are also associated to this key and can be used for secure communication." "Email address has already been added" + "Email address format is invalid" "Revoked: Key must not be used anymore!" -- cgit v1.2.3 From d9cabf8dc4a4de0153ab6a07919ae8d7614cfc9d Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Fri, 24 Apr 2015 03:35:30 +0100 Subject: -Fixed a crash while recreating CreateKeyEmailFragment -Email data is properly restored while rotating the Activity. -Added necessary logic to guarantee that an email is actually valid before continuing, there are verifications for empty, duplicated and invalid formatted emails for both additional and main email. --- .../keychain/ui/CreateKeyEmailFragment.java | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'OpenKeychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java index 26c307957..8fdfb35cb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java @@ -47,16 +47,13 @@ import java.util.List; import java.util.regex.Pattern; public class CreateKeyEmailFragment extends Fragment { - - CreateKeyActivity mCreateKeyActivity; - EmailEditText mEmailEdit; - RecyclerView mEmailsRecyclerView; - View mBackButton; - View mNextButton; - - ArrayList mAdditionalEmailModels; - - EmailAdapter mEmailAdapter; + private CreateKeyActivity mCreateKeyActivity; + private EmailEditText mEmailEdit; + private RecyclerView mEmailsRecyclerView; + private View mBackButton; + private View mNextButton; + private ArrayList mAdditionalEmailModels; + private EmailAdapter mEmailAdapter; /** * Creates new instance of this fragment @@ -125,9 +122,6 @@ public class CreateKeyEmailFragment extends Fragment { // initial values if (mAdditionalEmailModels == null) { mAdditionalEmailModels = new ArrayList<>(); - if (mCreateKeyActivity.mAdditionalEmails != null && mEmailAdapter != null) { - mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails); - } } if (mEmailAdapter == null) { @@ -137,6 +131,10 @@ public class CreateKeyEmailFragment extends Fragment { addEmail(); } }); + + if (mCreateKeyActivity.mAdditionalEmails != null) { + mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails); + } } mEmailsRecyclerView.setAdapter(mEmailAdapter); @@ -203,6 +201,9 @@ public class CreateKeyEmailFragment extends Fragment { return false; } + /** + * Displays a dialog fragment for the user to input a valid email. + */ private void addEmail() { Handler returnHandler = new Handler() { @Override @@ -218,12 +219,11 @@ public class CreateKeyEmailFragment extends Fragment { } } }; - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(returnHandler); + Messenger messenger = new Messenger(returnHandler); AddEmailDialogFragment addEmailDialog = AddEmailDialogFragment.newInstance(messenger); - + addEmailDialog.setTargetFragment(this, -1); addEmailDialog.show(getActivity().getSupportFragmentManager(), "addEmailDialog"); } -- cgit v1.2.3 From 08d25f3685cffb7402bb6dd824bc0666bfe9a1c2 Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Sun, 26 Apr 2015 18:56:37 +0100 Subject: -removed unneeded code from previous commits. I tested on a KitKat(4.4.2) and Lollipop (5.1) phone and everything seems to be preserved as expected. --- .../org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java | 3 --- 1 file changed, 3 deletions(-) (limited to 'OpenKeychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java index ed4cf5b8a..473383e22 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java @@ -135,9 +135,6 @@ public class CreateKeyEmailFragment extends Fragment { mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails); } } - if (mAdditionalEmailModels.isEmpty() && mCreateKeyActivity.mAdditionalEmails != null) { - mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails); - } mEmailsRecyclerView.setAdapter(mEmailAdapter); -- cgit v1.2.3 From c0fbafde688f2f60f475121bf8c8b8ef2a59ce6d Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Sun, 26 Apr 2015 19:11:51 +0100 Subject: -Removed unneeded view references from the fragment class since they are not used outside of createView. -cleanup --- .../sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'OpenKeychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java index 473383e22..035945af3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java @@ -49,9 +49,6 @@ import java.util.regex.Pattern; public class CreateKeyEmailFragment extends Fragment { private CreateKeyActivity mCreateKeyActivity; private EmailEditText mEmailEdit; - private RecyclerView mEmailsRecyclerView; - private View mBackButton; - private View mNextButton; private ArrayList mAdditionalEmailModels; private EmailAdapter mEmailAdapter; @@ -92,9 +89,9 @@ public class CreateKeyEmailFragment extends Fragment { View view = inflater.inflate(R.layout.create_key_email_fragment, container, false); mEmailEdit = (EmailEditText) view.findViewById(R.id.create_key_email); - mBackButton = view.findViewById(R.id.create_key_back_button); - mNextButton = view.findViewById(R.id.create_key_next_button); - mEmailsRecyclerView = (RecyclerView) view.findViewById(R.id.create_key_emails); + View mBackButton = view.findViewById(R.id.create_key_back_button); + View mNextButton = view.findViewById(R.id.create_key_next_button); + RecyclerView mEmailsRecyclerView = (RecyclerView) view.findViewById(R.id.create_key_emails); // initial values mEmailEdit.setText(mCreateKeyActivity.mEmail); @@ -222,7 +219,6 @@ public class CreateKeyEmailFragment extends Fragment { Messenger messenger = new Messenger(returnHandler); AddEmailDialogFragment addEmailDialog = AddEmailDialogFragment.newInstance(messenger); - addEmailDialog.setTargetFragment(this, -1); addEmailDialog.show(getActivity().getSupportFragmentManager(), "addEmailDialog"); } -- cgit v1.2.3 From e159e619d299c0e91f3f9527186e50649657dd73 Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Thu, 30 Apr 2015 00:44:01 +0100 Subject: -removed unneeded inputType attribute from xml files (+1 squashed commits) Squashed commits: [ef54e68] -code style fixing --- .../keychain/ui/CreateKeyEmailFragment.java | 20 ++++++++++---------- .../src/main/res/layout/add_email_dialog.xml | 1 - .../main/res/layout/create_key_email_fragment.xml | 1 - 3 files changed, 10 insertions(+), 12 deletions(-) (limited to 'OpenKeychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java index 035945af3..078135772 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyEmailFragment.java @@ -89,9 +89,9 @@ public class CreateKeyEmailFragment extends Fragment { View view = inflater.inflate(R.layout.create_key_email_fragment, container, false); mEmailEdit = (EmailEditText) view.findViewById(R.id.create_key_email); - View mBackButton = view.findViewById(R.id.create_key_back_button); - View mNextButton = view.findViewById(R.id.create_key_next_button); - RecyclerView mEmailsRecyclerView = (RecyclerView) view.findViewById(R.id.create_key_emails); + View backButton = view.findViewById(R.id.create_key_back_button); + View nextButton = view.findViewById(R.id.create_key_next_button); + RecyclerView emailsRecyclerView = (RecyclerView) view.findViewById(R.id.create_key_emails); // initial values mEmailEdit.setText(mCreateKeyActivity.mEmail); @@ -100,21 +100,21 @@ public class CreateKeyEmailFragment extends Fragment { if (mCreateKeyActivity.mEmail == null) { mEmailEdit.requestFocus(); } - mBackButton.setOnClickListener(new View.OnClickListener() { + backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCreateKeyActivity.loadFragment(null, FragAction.TO_LEFT); } }); - mNextButton.setOnClickListener(new View.OnClickListener() { + nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { nextClicked(); } }); - mEmailsRecyclerView.setHasFixedSize(true); - mEmailsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); - mEmailsRecyclerView.setItemAnimator(new DefaultItemAnimator()); + emailsRecyclerView.setHasFixedSize(true); + emailsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); + emailsRecyclerView.setItemAnimator(new DefaultItemAnimator()); // initial values if (mAdditionalEmailModels == null) { @@ -133,7 +133,7 @@ public class CreateKeyEmailFragment extends Fragment { } } - mEmailsRecyclerView.setAdapter(mEmailAdapter); + emailsRecyclerView.setAdapter(mEmailAdapter); return view; } @@ -216,7 +216,7 @@ public class CreateKeyEmailFragment extends Fragment { } }; // Create a new Messenger for the communication back - Messenger messenger = new Messenger(returnHandler); + Messenger messenger = new Messenger(returnHandler); AddEmailDialogFragment addEmailDialog = AddEmailDialogFragment.newInstance(messenger); addEmailDialog.show(getActivity().getSupportFragmentManager(), "addEmailDialog"); diff --git a/OpenKeychain/src/main/res/layout/add_email_dialog.xml b/OpenKeychain/src/main/res/layout/add_email_dialog.xml index 2de657a40..68d895145 100644 --- a/OpenKeychain/src/main/res/layout/add_email_dialog.xml +++ b/OpenKeychain/src/main/res/layout/add_email_dialog.xml @@ -21,7 +21,6 @@ android:layout_marginTop="16dp" android:hint="@string/label_email" android:imeOptions="actionNext" - android:inputType="textEmailAddress" android:textAppearance="?android:attr/textAppearanceMedium" /> diff --git a/OpenKeychain/src/main/res/layout/create_key_email_fragment.xml b/OpenKeychain/src/main/res/layout/create_key_email_fragment.xml index 7b9cffec4..17cfe54ac 100644 --- a/OpenKeychain/src/main/res/layout/create_key_email_fragment.xml +++ b/OpenKeychain/src/main/res/layout/create_key_email_fragment.xml @@ -32,7 +32,6 @@ android:layout_marginTop="16dp" android:layout_marginBottom="8dp" android:imeOptions="actionNext" - android:inputType="textEmailAddress" android:hint="@string/label_email" android:ems="10" /> -- cgit v1.2.3