Moved the error message to TextInputLayout

This commit is contained in:
SoyaLeaf 2018-02-18 11:20:02 +08:00 committed by Daniel Gultsch
parent 22a722c06d
commit f334349cd6
2 changed files with 34 additions and 23 deletions

View File

@ -12,6 +12,7 @@ import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
import android.security.KeyChain; import android.security.KeyChain;
import android.security.KeyChainAliasCallback; import android.security.KeyChainAliasCallback;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -74,7 +75,9 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
private static final int REQUEST_DATA_SAVER = 0x37af244; private static final int REQUEST_DATA_SAVER = 0x37af244;
private AutoCompleteTextView mAccountJid; private AutoCompleteTextView mAccountJid;
private TextInputLayout mAccountJidLayout;
private EditText mPassword; private EditText mPassword;
private TextInputLayout mPasswordLayout;
private CheckBox mRegisterNew; private CheckBox mRegisterNew;
private Button mCancelButton; private Button mCancelButton;
private Button mSaveButton; private Button mSaveButton;
@ -101,7 +104,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
private TextView mOwnFingerprintDesc; private TextView mOwnFingerprintDesc;
private TextView mOtrFingerprintDesc; private TextView mOtrFingerprintDesc;
private TextView getmPgpFingerprintDesc; private TextView getmPgpFingerprintDesc;
private TextView mAccountJidLabel;
private ImageView mAvatar; private ImageView mAvatar;
private RelativeLayout mOtrFingerprintBox; private RelativeLayout mOtrFingerprintBox;
private RelativeLayout mAxolotlFingerprintBox; private RelativeLayout mAxolotlFingerprintBox;
@ -113,7 +115,9 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
private LinearLayout keysCard; private LinearLayout keysCard;
private LinearLayout mNamePort; private LinearLayout mNamePort;
private EditText mHostname; private EditText mHostname;
private TextInputLayout mHostnameLayout;
private EditText mPort; private EditText mPort;
private TextInputLayout mPortLayout;
private AlertDialog mCaptchaDialog = null; private AlertDialog mCaptchaDialog = null;
private Jid jidToEdit; private Jid jidToEdit;
@ -148,7 +152,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
} }
final boolean registerNewAccount = mRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI; final boolean registerNewAccount = mRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI;
if (mUsernameMode && mAccountJid.getText().toString().contains("@")) { if (mUsernameMode && mAccountJid.getText().toString().contains("@")) {
mAccountJid.setError(getString(R.string.invalid_username)); mAccountJidLayout.setError(getString(R.string.invalid_username));
mAccountJid.requestFocus(); mAccountJid.requestFocus();
return; return;
} }
@ -177,9 +181,9 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
} }
} catch (final InvalidJidException e) { } catch (final InvalidJidException e) {
if (mUsernameMode) { if (mUsernameMode) {
mAccountJid.setError(getString(R.string.invalid_username)); mAccountJidLayout.setError(getString(R.string.invalid_username));
} else { } else {
mAccountJid.setError(getString(R.string.invalid_jid)); mAccountJidLayout.setError(getString(R.string.invalid_jid));
} }
mAccountJid.requestFocus(); mAccountJid.requestFocus();
return; return;
@ -190,20 +194,20 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
hostname = mHostname.getText().toString().replaceAll("\\s",""); hostname = mHostname.getText().toString().replaceAll("\\s","");
final String port = mPort.getText().toString().replaceAll("\\s",""); final String port = mPort.getText().toString().replaceAll("\\s","");
if (hostname.contains(" ")) { if (hostname.contains(" ")) {
mHostname.setError(getString(R.string.not_valid_hostname)); mHostnameLayout.setError(getString(R.string.not_valid_hostname));
mHostname.requestFocus(); mHostname.requestFocus();
return; return;
} }
try { try {
numericPort = Integer.parseInt(port); numericPort = Integer.parseInt(port);
if (numericPort < 0 || numericPort > 65535) { if (numericPort < 0 || numericPort > 65535) {
mPort.setError(getString(R.string.not_a_valid_port)); mPortLayout.setError(getString(R.string.not_a_valid_port));
mPort.requestFocus(); mPort.requestFocus();
return; return;
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
mPort.setError(getString(R.string.not_a_valid_port)); mPortLayout.setError(getString(R.string.not_a_valid_port));
mPort.requestFocus(); mPort.requestFocus();
return; return;
} }
@ -211,9 +215,9 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
if (jid.isDomainJid()) { if (jid.isDomainJid()) {
if (mUsernameMode) { if (mUsernameMode) {
mAccountJid.setError(getString(R.string.invalid_username)); mAccountJidLayout.setError(getString(R.string.invalid_username));
} else { } else {
mAccountJid.setError(getString(R.string.invalid_jid)); mAccountJidLayout.setError(getString(R.string.invalid_jid));
} }
mAccountJid.requestFocus(); mAccountJid.requestFocus();
return; return;
@ -225,7 +229,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
mAccount.setJid(jid); mAccount.setJid(jid);
mAccount.setPort(numericPort); mAccount.setPort(numericPort);
mAccount.setHostname(hostname); mAccount.setHostname(hostname);
mAccountJid.setError(null); mAccountJidLayout.setError(null);
mAccount.setPassword(password); mAccount.setPassword(password);
mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount); mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
if (!xmppConnectionService.updateAccount(mAccount)) { if (!xmppConnectionService.updateAccount(mAccount)) {
@ -234,7 +238,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
} }
} else { } else {
if (xmppConnectionService.findAccountByJid(jid) != null) { if (xmppConnectionService.findAccountByJid(jid) != null) {
mAccountJid.setError(getString(R.string.account_already_exists)); mAccountJidLayout.setError(getString(R.string.account_already_exists));
mAccountJid.requestFocus(); mAccountJid.requestFocus();
return; return;
} }
@ -246,8 +250,8 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount); mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
xmppConnectionService.createAccount(mAccount); xmppConnectionService.createAccount(mAccount);
} }
mHostname.setError(null); mHostnameLayout.setError(null);
mPort.setError(null); mPortLayout.setError(null);
if (mAccount.isEnabled() if (mAccount.isEnabled()
&& !registerNewAccount && !registerNewAccount
&& !mInitMode) { && !mInitMode) {
@ -535,8 +539,10 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid); this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
this.mAccountJid.addTextChangedListener(this.mTextWatcher); this.mAccountJid.addTextChangedListener(this.mTextWatcher);
this.mAccountJid.setOnFocusChangeListener(this.mEditTextFocusListener); this.mAccountJid.setOnFocusChangeListener(this.mEditTextFocusListener);
this.mAccountJidLayout = (TextInputLayout) findViewById(R.id.account_jid_layout);
this.mPassword = (EditText) findViewById(R.id.account_password); this.mPassword = (EditText) findViewById(R.id.account_password);
this.mPassword.addTextChangedListener(this.mTextWatcher); this.mPassword.addTextChangedListener(this.mTextWatcher);
this.mPasswordLayout = (TextInputLayout) findViewById(R.id.account_password_layout);
this.mAvatar = (ImageView) findViewById(R.id.avater); this.mAvatar = (ImageView) findViewById(R.id.avater);
this.mAvatar.setOnClickListener(this.mAvatarClickListener); this.mAvatar.setOnClickListener(this.mAvatarClickListener);
this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new); this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
@ -574,6 +580,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
this.mHostname = (EditText) findViewById(R.id.hostname); this.mHostname = (EditText) findViewById(R.id.hostname);
this.mHostname.addTextChangedListener(mTextWatcher); this.mHostname.addTextChangedListener(mTextWatcher);
this.mHostname.setOnFocusChangeListener(mEditTextFocusListener); this.mHostname.setOnFocusChangeListener(mEditTextFocusListener);
this.mHostnameLayout = (TextInputLayout)findViewById(R.id.hostname_layout);
this.mClearDevicesButton = (Button) findViewById(R.id.clear_devices); this.mClearDevicesButton = (Button) findViewById(R.id.clear_devices);
this.mClearDevicesButton.setOnClickListener(new OnClickListener() { this.mClearDevicesButton.setOnClickListener(new OnClickListener() {
@Override @Override
@ -584,6 +591,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
this.mPort = (EditText) findViewById(R.id.port); this.mPort = (EditText) findViewById(R.id.port);
this.mPort.setText("5222"); this.mPort.setText("5222");
this.mPort.addTextChangedListener(mTextWatcher); this.mPort.addTextChangedListener(mTextWatcher);
this.mPortLayout = (TextInputLayout)findViewById(R.id.port_layout);
this.mSaveButton = (Button) findViewById(R.id.save_button); this.mSaveButton = (Button) findViewById(R.id.save_button);
this.mCancelButton = (Button) findViewById(R.id.cancel_button); this.mCancelButton = (Button) findViewById(R.id.cancel_button);
this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener); this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
@ -753,7 +761,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
this.mCancelButton.setTextColor(getSecondaryTextColor()); this.mCancelButton.setTextColor(getSecondaryTextColor());
} }
if (mUsernameMode) { if (mUsernameMode) {
this.mAccountJidLabel.setText(R.string.username);
this.mAccountJid.setHint(R.string.username_hint); this.mAccountJid.setHint(R.string.username_hint);
} else { } else {
final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this, final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
@ -1055,24 +1062,24 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
} }
} else { } else {
if (this.mAccount.errorStatus()) { if (this.mAccount.errorStatus()) {
final EditText errorTextField; final TextInputLayout errorLayout;
if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) { if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
errorTextField = this.mPassword; errorLayout = this.mPasswordLayout;
} else if (mShowOptions } else if (mShowOptions
&& this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND && this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
&& this.mHostname.getText().length() > 0) { && this.mHostname.getText().length() > 0) {
errorTextField = this.mHostname; errorLayout = this.mHostnameLayout;
} else { } else {
errorTextField = this.mAccountJid; errorLayout = this.mAccountJidLayout;
} }
errorTextField.setError(getString(this.mAccount.getStatus().getReadableId())); errorLayout.setError(getString(this.mAccount.getStatus().getReadableId()));
if (init || !accountInfoEdited()) { if (init || !accountInfoEdited()) {
errorTextField.requestFocus(); errorLayout.requestFocus();
} }
} else { } else {
this.mAccountJid.setError(null); this.mAccountJidLayout.setError(null);
this.mPassword.setError(null); this.mPasswordLayout.setError(null);
this.mHostname.setError(null); this.mHostnameLayout.setError(null);
} }
this.mStats.setVisibility(View.GONE); this.mStats.setVisibility(View.GONE);
} }

View File

@ -46,6 +46,7 @@
android:orientation="vertical"> android:orientation="vertical">
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:id="@+id/account_jid_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/account_settings_jabber_id"> android:hint="@string/account_settings_jabber_id">
@ -63,6 +64,7 @@
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:id="@+id/account_password_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:passwordToggleDrawable="@drawable/visibility_toggle_drawable" app:passwordToggleDrawable="@drawable/visibility_toggle_drawable"
@ -98,6 +100,7 @@
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/hostname_layout"
android:hint="@string/account_settings_hostname"> android:hint="@string/account_settings_hostname">
<EditText <EditText
@ -120,6 +123,7 @@
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/port_layout"
android:hint="@string/account_settings_port"> android:hint="@string/account_settings_port">
<EditText <EditText