make port field disabled if hostname is empty

This commit is contained in:
Daniel Gultsch 2018-08-29 09:33:02 +02:00
parent eaa62f8840
commit af724a6697
2 changed files with 35 additions and 36 deletions

View File

@ -21,6 +21,7 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog.Builder; import android.support.v7.app.AlertDialog.Builder;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
@ -101,11 +102,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
private ImageButton mAxolotlFingerprintToClipboardButton; private ImageButton mAxolotlFingerprintToClipboardButton;
private ImageButton mPgpDeleteFingerprintButton; private ImageButton mPgpDeleteFingerprintButton;
private LinearLayout keys; private LinearLayout keys;
private LinearLayout mNamePort;
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;
@ -183,26 +180,26 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
String hostname = null; String hostname = null;
int numericPort = 5222; int numericPort = 5222;
if (mShowOptions) { if (mShowOptions) {
hostname = mHostname.getText().toString().replaceAll("\\s", ""); hostname = binding.hostname.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(" ")) {
mHostnameLayout.setError(getString(R.string.not_valid_hostname)); binding.hostnameLayout.setError(getString(R.string.not_valid_hostname));
mHostname.requestFocus(); binding.hostname.requestFocus();
removeErrorsOnAllBut(mHostnameLayout); removeErrorsOnAllBut(binding.hostnameLayout);
return; return;
} }
try { try {
numericPort = Integer.parseInt(port); numericPort = Integer.parseInt(port);
if (numericPort < 0 || numericPort > 65535) { if (numericPort < 0 || numericPort > 65535) {
mPortLayout.setError(getString(R.string.not_a_valid_port)); binding.portLayout.setError(getString(R.string.not_a_valid_port));
removeErrorsOnAllBut(mPortLayout); removeErrorsOnAllBut(binding.portLayout);
mPort.requestFocus(); mPort.requestFocus();
return; return;
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
mPortLayout.setError(getString(R.string.not_a_valid_port)); binding.portLayout.setError(getString(R.string.not_a_valid_port));
removeErrorsOnAllBut(mPortLayout); removeErrorsOnAllBut(binding.portLayout);
mPort.requestFocus(); mPort.requestFocus();
return; return;
} }
@ -247,8 +244,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);
} }
mHostnameLayout.setError(null); binding.hostnameLayout.setError(null);
mPortLayout.setError(null); binding.portLayout.setError(null);
if (mAccount.isEnabled() if (mAccount.isEnabled()
&& !registerNewAccount && !registerNewAccount
&& !mInitMode) { && !mInitMode) {
@ -346,6 +343,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
@Override @Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
updatePortLayout();
updateSaveButton(); updateSaveButton();
} }
@ -446,6 +444,11 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
} }
} }
private void updatePortLayout() {
String hostname = this.binding.hostname.getText().toString();
this.binding.portLayout.setEnabled(!TextUtils.isEmpty(hostname));
}
protected void updateSaveButton() { protected void updateSaveButton() {
boolean accountInfoEdited = accountInfoEdited(); boolean accountInfoEdited = accountInfoEdited();
@ -497,7 +500,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
} }
return jidEdited() || return jidEdited() ||
!this.mAccount.getPassword().equals(this.mPassword.getText().toString()) || !this.mAccount.getPassword().equals(this.mPassword.getText().toString()) ||
!this.mAccount.getHostname().equals(this.mHostname.getText().toString()) || !this.mAccount.getHostname().equals(this.binding.hostname.getText().toString()) ||
!String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString()); !String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString());
} }
@ -556,18 +559,14 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box); this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box);
this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard); this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard);
this.mOwnFingerprintDesc = (TextView) findViewById(R.id.own_fingerprint_desc); this.mOwnFingerprintDesc = (TextView) findViewById(R.id.own_fingerprint_desc);
this.keys = (LinearLayout) findViewById(R.id.other_device_keys); this.keys = findViewById(R.id.other_device_keys);
this.mNamePort = (LinearLayout) findViewById(R.id.name_port); this.binding.hostname.addTextChangedListener(mTextWatcher);
this.mHostname = (EditText) findViewById(R.id.hostname); this.binding.hostname.setOnFocusChangeListener(mEditTextFocusListener);
this.mHostname.addTextChangedListener(mTextWatcher);
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(v -> showWipePepDialog()); this.mClearDevicesButton.setOnClickListener(v -> showWipePepDialog());
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);
@ -676,7 +675,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
SharedPreferences preferences = getPreferences(); SharedPreferences preferences = getPreferences();
mUseTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false); mUseTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
this.mShowOptions = mUseTor || preferences.getBoolean("show_connection_options", false); this.mShowOptions = mUseTor || preferences.getBoolean("show_connection_options", false);
this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE); this.binding.namePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
} }
@Override @Override
@ -749,7 +748,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
processFingerprintVerification(pendingUri, false); processFingerprintVerification(pendingUri, false);
pendingUri = null; pendingUri = null;
} }
updatePortLayout();
updateSaveButton(); updateSaveButton();
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
@ -930,11 +929,11 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
} }
this.mPassword.getEditableText().clear(); this.mPassword.getEditableText().clear();
this.mPassword.getEditableText().append(this.mAccount.getPassword()); this.mPassword.getEditableText().append(this.mAccount.getPassword());
this.mHostname.setText(""); this.binding.hostname.setText("");
this.mHostname.getEditableText().append(this.mAccount.getHostname()); this.binding.hostname.getEditableText().append(this.mAccount.getHostname());
this.mPort.setText(""); this.mPort.setText("");
this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort())); this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort()));
this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE); this.binding.namePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
} }
@ -1094,8 +1093,8 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
errorLayout = this.mPasswordLayout; 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.binding.hostname.getText().length() > 0) {
errorLayout = this.mHostnameLayout; errorLayout = this.binding.hostnameLayout;
} else { } else {
errorLayout = this.mAccountJidLayout; errorLayout = this.mAccountJidLayout;
} }
@ -1121,13 +1120,13 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
this.mPasswordLayout.setErrorEnabled(false); this.mPasswordLayout.setErrorEnabled(false);
this.mPasswordLayout.setError(null); this.mPasswordLayout.setError(null);
} }
if (this.mHostnameLayout != exception) { if (this.binding.hostnameLayout != exception) {
this.mHostnameLayout.setErrorEnabled(false); this.binding.hostnameLayout.setErrorEnabled(false);
this.mHostnameLayout.setError(null); this.binding.hostnameLayout.setError(null);
} }
if (this.mPortLayout != exception) { if (this.binding.portLayout != exception) {
this.mPortLayout.setErrorEnabled(false); this.binding.portLayout.setErrorEnabled(false);
this.mPortLayout.setError(null); this.binding.portLayout.setError(null);
} }
} }

View File

@ -117,7 +117,7 @@
android:id="@+id/hostname" android:id="@+id/hostname"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="textNoSuggestions" android:inputType="textWebEmailAddress"
style="@style/Widget.Conversations.EditText"/> style="@style/Widget.Conversations.EditText"/>
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
</LinearLayout> </LinearLayout>