Merge branch 'Hiperzone-development' into development

This commit is contained in:
Dominik Schürmann 2015-05-04 14:56:11 +02:00
commit 9698cb94e3
3 changed files with 94 additions and 50 deletions

View File

@ -18,7 +18,6 @@
package org.sufficientlysecure.keychain.ui; package org.sufficientlysecure.keychain.ui;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
@ -44,18 +43,17 @@ import org.sufficientlysecure.keychain.ui.widget.EmailEditText;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
public class CreateKeyEmailFragment extends Fragment { public class CreateKeyEmailFragment extends Fragment {
private CreateKeyActivity mCreateKeyActivity;
private EmailEditText mEmailEdit;
private ArrayList<EmailAdapter.ViewModel> mAdditionalEmailModels;
private EmailAdapter mEmailAdapter;
CreateKeyActivity mCreateKeyActivity; // NOTE: Do not use more complicated pattern like defined in android.util.Patterns.EMAIL_ADDRESS
EmailEditText mEmailEdit; // EMAIL_ADDRESS fails for mails with umlauts for example
RecyclerView mEmailsRecyclerView; private static final Pattern EMAIL_PATTERN = Pattern.compile(".[\\S]+@.[\\S]+\\.[a-z]+");
View mBackButton;
View mNextButton;
ArrayList<EmailAdapter.ViewModel> mAdditionalEmailModels;
EmailAdapter mEmailAdapter;
/** /**
* Creates new instance of this fragment * Creates new instance of this fragment
@ -73,14 +71,13 @@ public class CreateKeyEmailFragment extends Fragment {
* Checks if text of given EditText is not empty. If it is empty an error is * Checks if text of given EditText is not empty. If it is empty an error is
* set and the EditText gets the focus. * set and the EditText gets the focus.
* *
* @param context
* @param editText * @param editText
* @return true if EditText is not empty * @return true if EditText is not empty
*/ */
private static boolean isEditTextNotEmpty(Context context, EditText editText) { private boolean isMainEmailValid(EditText editText) {
boolean output = true; boolean output = true;
if (editText.getText().length() == 0) { if (!checkEmail(editText.getText().toString(), false)) {
editText.setError(context.getString(R.string.create_key_empty)); editText.setError(getString(R.string.create_key_empty));
editText.requestFocus(); editText.requestFocus();
output = false; output = false;
} else { } else {
@ -95,9 +92,9 @@ public class CreateKeyEmailFragment extends Fragment {
View view = inflater.inflate(R.layout.create_key_email_fragment, container, false); View view = inflater.inflate(R.layout.create_key_email_fragment, container, false);
mEmailEdit = (EmailEditText) view.findViewById(R.id.create_key_email); mEmailEdit = (EmailEditText) view.findViewById(R.id.create_key_email);
mBackButton = view.findViewById(R.id.create_key_back_button); View backButton = view.findViewById(R.id.create_key_back_button);
mNextButton = view.findViewById(R.id.create_key_next_button); View nextButton = view.findViewById(R.id.create_key_next_button);
mEmailsRecyclerView = (RecyclerView) view.findViewById(R.id.create_key_emails); RecyclerView emailsRecyclerView = (RecyclerView) view.findViewById(R.id.create_key_emails);
// initial values // initial values
mEmailEdit.setText(mCreateKeyActivity.mEmail); mEmailEdit.setText(mCreateKeyActivity.mEmail);
@ -106,21 +103,21 @@ public class CreateKeyEmailFragment extends Fragment {
if (mCreateKeyActivity.mEmail == null) { if (mCreateKeyActivity.mEmail == null) {
mEmailEdit.requestFocus(); mEmailEdit.requestFocus();
} }
mBackButton.setOnClickListener(new View.OnClickListener() { backButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
mCreateKeyActivity.loadFragment(null, FragAction.TO_LEFT); mCreateKeyActivity.loadFragment(null, FragAction.TO_LEFT);
} }
}); });
mNextButton.setOnClickListener(new View.OnClickListener() { nextButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
nextClicked(); nextClicked();
} }
}); });
mEmailsRecyclerView.setHasFixedSize(true); emailsRecyclerView.setHasFixedSize(true);
mEmailsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); emailsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mEmailsRecyclerView.setItemAnimator(new DefaultItemAnimator()); emailsRecyclerView.setItemAnimator(new DefaultItemAnimator());
// initial values // initial values
if (mAdditionalEmailModels == null) { if (mAdditionalEmailModels == null) {
@ -133,16 +130,77 @@ public class CreateKeyEmailFragment extends Fragment {
addEmail(); addEmail();
} }
}); });
}
if (mAdditionalEmailModels.isEmpty() && mCreateKeyActivity.mAdditionalEmails != null) { if (mCreateKeyActivity.mAdditionalEmails != null) {
mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails); mEmailAdapter.addAll(mCreateKeyActivity.mAdditionalEmails);
} }
}
mEmailsRecyclerView.setAdapter(mEmailAdapter); emailsRecyclerView.setAdapter(mEmailAdapter);
return view; 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) {
// check for email format or if the user did any input
return !(email.length() == 0 || !EMAIL_PATTERN.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;
}
/**
* Displays a dialog fragment for the user to input a valid email.
*/
private void addEmail() { private void addEmail() {
Handler returnHandler = new Handler() { Handler returnHandler = new Handler() {
@Override @Override
@ -152,34 +210,17 @@ public class CreateKeyEmailFragment extends Fragment {
String email = data.getString(AddEmailDialogFragment.MESSAGE_DATA_EMAIL); String email = data.getString(AddEmailDialogFragment.MESSAGE_DATA_EMAIL);
if (email.length() > 0 && mEmailEdit.getText().length() > 0 && if (checkEmail(email, true)) {
email.equals(mEmailEdit.getText().toString())) {
Notify.create(getActivity(),
getString(R.string.create_key_email_already_exists_text),
Notify.LENGTH_LONG, Notify.Style.ERROR).show(CreateKeyEmailFragment.this);
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(CreateKeyEmailFragment.this);
return;
}
}
// add new user id // add new user id
mEmailAdapter.add(email); mEmailAdapter.add(email);
} }
} }
}
}; };
// Create a new Messenger for the communication back // Create a new Messenger for the communication back
Messenger messenger = new Messenger(returnHandler); Messenger messenger = new Messenger(returnHandler);
AddEmailDialogFragment addEmailDialog = AddEmailDialogFragment.newInstance(messenger); AddEmailDialogFragment addEmailDialog = AddEmailDialogFragment.newInstance(messenger);
addEmailDialog.show(getActivity().getSupportFragmentManager(), "addEmailDialog"); addEmailDialog.show(getActivity().getSupportFragmentManager(), "addEmailDialog");
} }
@ -190,7 +231,7 @@ public class CreateKeyEmailFragment extends Fragment {
} }
private void nextClicked() { private void nextClicked() {
if (isEditTextNotEmpty(getActivity(), mEmailEdit)) { if (isMainEmailValid(mEmailEdit)) {
// save state // save state
mCreateKeyActivity.mEmail = mEmailEdit.getText().toString(); mCreateKeyActivity.mEmail = mEmailEdit.getText().toString();
mCreateKeyActivity.mAdditionalEmails = getAdditionalEmails(); mCreateKeyActivity.mAdditionalEmails = getAdditionalEmails();

View File

@ -112,10 +112,12 @@ public class AddEmailDialogFragment extends DialogFragment implements OnEditorAc
mEmail.post(new Runnable() { mEmail.post(new Runnable() {
@Override @Override
public void run() { public void run() {
if(getActivity() != null) {
InputMethodManager imm = (InputMethodManager) getActivity() InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE); .getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEmail, InputMethodManager.SHOW_IMPLICIT); imm.showSoftInput(mEmail, InputMethodManager.SHOW_IMPLICIT);
} }
}
}); });
} }
}); });

View File

@ -643,6 +643,7 @@
<string name="create_key_add_email">"Add email address"</string> <string name="create_key_add_email">"Add email address"</string>
<string name="create_key_add_email_text">"Additional email addresses are also associated to this key and can be used for secure communication."</string> <string name="create_key_add_email_text">"Additional email addresses are also associated to this key and can be used for secure communication."</string>
<string name="create_key_email_already_exists_text">"Email address has already been added"</string> <string name="create_key_email_already_exists_text">"Email address has already been added"</string>
<string name="create_key_email_invalid_email">"Email address format is invalid"</string>
<!-- View key --> <!-- View key -->
<string name="view_key_revoked">"Revoked: Key must not be used anymore!"</string> <string name="view_key_revoked">"Revoked: Key must not be used anymore!"</string>