Make email address less restrictive and add icon to show when it's correct

This commit is contained in:
uberspot 2014-03-13 23:52:21 +02:00
parent c737f01a72
commit b12392594f
2 changed files with 28 additions and 11 deletions

View File

@ -600,8 +600,6 @@ public class EditKeyActivity extends ActionBarActivity {
} catch (UserIdEditor.NoEmailException e) { } catch (UserIdEditor.NoEmailException e) {
throw new PgpGeneralException( throw new PgpGeneralException(
this.getString(R.string.error_user_id_needs_an_email_address)); this.getString(R.string.error_user_id_needs_an_email_address));
} catch (UserIdEditor.InvalidEmailException e) {
throw new PgpGeneralException(e.getMessage());
} }
if (userId.equals("")) { if (userId.equals("")) {

View File

@ -17,6 +17,8 @@
package org.sufficientlysecure.keychain.ui.widget; package org.sufficientlysecure.keychain.ui.widget;
import android.content.Context; import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Patterns; import android.util.Patterns;
import android.view.View; import android.view.View;
@ -102,6 +104,31 @@ public class UserIdEditor extends LinearLayout implements Editor, OnClickListene
(this.getContext(), android.R.layout.simple_dropdown_item_1line, (this.getContext(), android.R.layout.simple_dropdown_item_1line,
ContactHelper.getMailAccounts(getContext()) ContactHelper.getMailAccounts(getContext())
)); ));
mEmail.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
@Override
public void afterTextChanged(Editable editable) {
String email = editable.toString();
if (email.length() > 0) {
Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email);
if (emailMatcher.matches()) {
mEmail.setCompoundDrawablesWithIntrinsicBounds(0, 0,
android.R.drawable.presence_online, 0);
} else {
mEmail.setCompoundDrawablesWithIntrinsicBounds(0, 0,
android.R.drawable.presence_offline, 0);
}
} else {
// remove drawable if email is empty
mEmail.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
}
});
super.onFinishInflate(); super.onFinishInflate();
} }
@ -129,19 +156,11 @@ public class UserIdEditor extends LinearLayout implements Editor, OnClickListene
} }
} }
public String getValue() throws NoNameException, NoEmailException, InvalidEmailException { public String getValue() throws NoNameException, NoEmailException {
String name = ("" + mName.getText()).trim(); String name = ("" + mName.getText()).trim();
String email = ("" + mEmail.getText()).trim(); String email = ("" + mEmail.getText()).trim();
String comment = ("" + mComment.getText()).trim(); String comment = ("" + mComment.getText()).trim();
if (email.length() > 0) {
Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email);
if (!emailMatcher.matches()) {
throw new InvalidEmailException(getContext().getString(R.string.error_invalid_email,
email));
}
}
String userId = name; String userId = name;
if (comment.length() > 0) { if (comment.length() > 0) {
userId += " (" + comment + ")"; userId += " (" + comment + ")";