show error messages in text layout not edittext

This commit is contained in:
Daniel Gultsch 2019-05-22 17:31:11 +02:00
parent c965049605
commit 10653df164
2 changed files with 15 additions and 6 deletions

View File

@ -15,6 +15,7 @@ import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import eu.siacs.conversations.Config;
@ -41,6 +42,7 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
private static final String SANITY_CHECK_JID = "sanity_check_jid";
private KnownHostsAdapter knownHostsAdapter;
private Collection<String> whitelistedDomains = Collections.emptyList();
private EnterJidDialogBinding binding;
private AlertDialog dialog;
@ -155,19 +157,19 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
try {
contactJid = Jid.of(binding.jid.getText().toString());
} catch (final IllegalArgumentException e) {
binding.jid.setError(getActivity().getString(R.string.invalid_jid));
binding.jidLayout.setError(getActivity().getString(R.string.invalid_jid));
return;
}
if (!issuedWarning && sanityCheckJid) {
if (contactJid.isDomainJid()) {
binding.jid.setError(getActivity().getString(R.string.this_looks_like_a_domain));
binding.jidLayout.setError(getActivity().getString(R.string.this_looks_like_a_domain));
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway);
issuedWarning = true;
return;
}
if (suspiciousSubDomain(contactJid.getDomain())) {
binding.jid.setError(getActivity().getString(R.string.this_looks_like_channel));
binding.jidLayout.setError(getActivity().getString(R.string.this_looks_like_channel));
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway);
issuedWarning = true;
return;
@ -180,7 +182,9 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
dialog.dismiss();
}
} catch (JidError error) {
binding.jid.setError(error.toString());
binding.jidLayout.setError(error.toString());
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
issuedWarning = false;
}
}
}
@ -199,6 +203,7 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
if (activity instanceof XmppActivity) {
Collection<String> hosts = ((XmppActivity) activity).xmppConnectionService.getKnownHosts();
this.knownHostsAdapter.refresh(hosts);
this.whitelistedDomains = hosts;
}
}
@ -216,6 +221,7 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
public void afterTextChanged(Editable s) {
if (issuedWarning) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
binding.jidLayout.setError(null);
issuedWarning = false;
}
}
@ -245,7 +251,10 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
super.onDestroyView();
}
private static boolean suspiciousSubDomain(String domain) {
private boolean suspiciousSubDomain(String domain) {
if (this.whitelistedDomains.contains(domain)) {
return false;
}
final String[] parts = domain.split("\\.");
return parts.length >= 3 && SUSPICIOUS_DOMAINS.contains(parts[0]);
}

View File

@ -23,7 +23,7 @@
android:layout_height="wrap_content"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/account_jid_layout"
android:id="@+id/jid_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_settings_jabber_id"