fix rare npe

This commit is contained in:
Daniel Gultsch 2022-02-23 09:40:47 +01:00
parent 0b470534f1
commit 4129ca6af8

View file

@ -22,6 +22,7 @@ import java.util.List;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.EnterJidDialogBinding; import eu.siacs.conversations.databinding.EnterJidDialogBinding;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter; import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.interfaces.OnBackendConnected; import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
import eu.siacs.conversations.ui.util.DelayedHintHelper; import eu.siacs.conversations.ui.util.DelayedHintHelper;
@ -29,234 +30,250 @@ import eu.siacs.conversations.xmpp.Jid;
public class EnterJidDialog extends DialogFragment implements OnBackendConnected, TextWatcher { public class EnterJidDialog extends DialogFragment implements OnBackendConnected, TextWatcher {
private static final List<String> SUSPICIOUS_DOMAINS =
Arrays.asList("conference", "muc", "room", "rooms", "chat");
private static final List<String> SUSPICIOUS_DOMAINS = Arrays.asList("conference","muc","room","rooms","chat"); private OnEnterJidDialogPositiveListener mListener = null;
private OnEnterJidDialogPositiveListener mListener = null; private static final String TITLE_KEY = "title";
private static final String POSITIVE_BUTTON_KEY = "positive_button";
private static final String PREFILLED_JID_KEY = "prefilled_jid";
private static final String ACCOUNT_KEY = "account";
private static final String ALLOW_EDIT_JID_KEY = "allow_edit_jid";
private static final String ACCOUNTS_LIST_KEY = "activated_accounts_list";
private static final String SANITY_CHECK_JID = "sanity_check_jid";
private static final String TITLE_KEY = "title"; private KnownHostsAdapter knownHostsAdapter;
private static final String POSITIVE_BUTTON_KEY = "positive_button"; private Collection<String> whitelistedDomains = Collections.emptyList();
private static final String PREFILLED_JID_KEY = "prefilled_jid";
private static final String ACCOUNT_KEY = "account";
private static final String ALLOW_EDIT_JID_KEY = "allow_edit_jid";
private static final String ACCOUNTS_LIST_KEY = "activated_accounts_list";
private static final String SANITY_CHECK_JID = "sanity_check_jid";
private KnownHostsAdapter knownHostsAdapter; private EnterJidDialogBinding binding;
private Collection<String> whitelistedDomains = Collections.emptyList(); private AlertDialog dialog;
private boolean sanityCheckJid = false;
private EnterJidDialogBinding binding; private boolean issuedWarning = false;
private AlertDialog dialog;
private boolean sanityCheckJid = false;
public static EnterJidDialog newInstance(
final List<String> activatedAccounts,
final String title,
final String positiveButton,
final String prefilledJid,
final String account,
boolean allowEditJid,
final boolean sanity_check_jid) {
EnterJidDialog dialog = new EnterJidDialog();
Bundle bundle = new Bundle();
bundle.putString(TITLE_KEY, title);
bundle.putString(POSITIVE_BUTTON_KEY, positiveButton);
bundle.putString(PREFILLED_JID_KEY, prefilledJid);
bundle.putString(ACCOUNT_KEY, account);
bundle.putBoolean(ALLOW_EDIT_JID_KEY, allowEditJid);
bundle.putStringArrayList(ACCOUNTS_LIST_KEY, (ArrayList<String>) activatedAccounts);
bundle.putBoolean(SANITY_CHECK_JID, sanity_check_jid);
dialog.setArguments(bundle);
return dialog;
}
private boolean issuedWarning = false; @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public static EnterJidDialog newInstance(final List<String> activatedAccounts, @Override
final String title, final String positiveButton, public void onStart() {
final String prefilledJid, final String account, super.onStart();
boolean allowEditJid, final boolean sanity_check_jid) { final Activity activity = getActivity();
EnterJidDialog dialog = new EnterJidDialog(); if (activity instanceof XmppActivity
Bundle bundle = new Bundle(); && ((XmppActivity) activity).xmppConnectionService != null) {
bundle.putString(TITLE_KEY, title); refreshKnownHosts();
bundle.putString(POSITIVE_BUTTON_KEY, positiveButton); }
bundle.putString(PREFILLED_JID_KEY, prefilledJid); }
bundle.putString(ACCOUNT_KEY, account);
bundle.putBoolean(ALLOW_EDIT_JID_KEY, allowEditJid);
bundle.putStringArrayList(ACCOUNTS_LIST_KEY, (ArrayList<String>) activatedAccounts);
bundle.putBoolean(SANITY_CHECK_JID, sanity_check_jid);
dialog.setArguments(bundle);
return dialog;
}
@Override @NonNull
public void onActivityCreated(Bundle savedInstanceState) { @Override
super.onActivityCreated(savedInstanceState); public Dialog onCreateDialog(Bundle savedInstanceState) {
setRetainInstance(true); final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
} builder.setTitle(getArguments().getString(TITLE_KEY));
binding =
DataBindingUtil.inflate(
getActivity().getLayoutInflater(), R.layout.enter_jid_dialog, null, false);
this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.simple_list_item);
binding.jid.setAdapter(this.knownHostsAdapter);
binding.jid.addTextChangedListener(this);
String prefilledJid = getArguments().getString(PREFILLED_JID_KEY);
if (prefilledJid != null) {
binding.jid.append(prefilledJid);
if (!getArguments().getBoolean(ALLOW_EDIT_JID_KEY)) {
binding.jid.setFocusable(false);
binding.jid.setFocusableInTouchMode(false);
binding.jid.setClickable(false);
binding.jid.setCursorVisible(false);
}
}
sanityCheckJid = getArguments().getBoolean(SANITY_CHECK_JID, false);
@Override DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id, binding.jid);
public void onStart() {
super.onStart();
final Activity activity = getActivity();
if (activity instanceof XmppActivity && ((XmppActivity) activity).xmppConnectionService != null) {
refreshKnownHosts();
}
}
@NonNull String account = getArguments().getString(ACCOUNT_KEY);
@Override if (account == null) {
public Dialog onCreateDialog(Bundle savedInstanceState) { StartConversationActivity.populateAccountSpinner(
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); getActivity(),
builder.setTitle(getArguments().getString(TITLE_KEY)); getArguments().getStringArrayList(ACCOUNTS_LIST_KEY),
binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.enter_jid_dialog, null, false); binding.account);
this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.simple_list_item); } else {
binding.jid.setAdapter(this.knownHostsAdapter); ArrayAdapter<String> adapter =
binding.jid.addTextChangedListener(this); new ArrayAdapter<>(
String prefilledJid = getArguments().getString(PREFILLED_JID_KEY); getActivity(), R.layout.simple_list_item, new String[] {account});
if (prefilledJid != null) { binding.account.setEnabled(false);
binding.jid.append(prefilledJid); adapter.setDropDownViewResource(R.layout.simple_list_item);
if (!getArguments().getBoolean(ALLOW_EDIT_JID_KEY)) { binding.account.setAdapter(adapter);
binding.jid.setFocusable(false); }
binding.jid.setFocusableInTouchMode(false);
binding.jid.setClickable(false);
binding.jid.setCursorVisible(false);
}
}
sanityCheckJid = getArguments().getBoolean(SANITY_CHECK_JID, false);
DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id, binding.jid); builder.setView(binding.getRoot());
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null);
this.dialog = builder.create();
String account = getArguments().getString(ACCOUNT_KEY); View.OnClickListener dialogOnClick =
if (account == null) { v -> {
StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), binding.account); handleEnter(binding, account);
} else { };
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
R.layout.simple_list_item,
new String[]{account});
binding.account.setEnabled(false);
adapter.setDropDownViewResource(R.layout.simple_list_item);
binding.account.setAdapter(adapter);
}
binding.jid.setOnEditorActionListener(
(v, actionId, event) -> {
handleEnter(binding, account);
return true;
});
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(dialogOnClick);
return dialog;
}
builder.setView(binding.getRoot()); private void handleEnter(EnterJidDialogBinding binding, String account) {
builder.setNegativeButton(R.string.cancel, null); final Jid accountJid;
builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null); if (!binding.account.isEnabled() && account == null) {
this.dialog = builder.create(); return;
}
try {
if (Config.DOMAIN_LOCK != null) {
accountJid =
Jid.ofEscaped(
(String) binding.account.getSelectedItem(),
Config.DOMAIN_LOCK,
null);
} else {
accountJid = Jid.ofEscaped((String) binding.account.getSelectedItem());
}
} catch (final IllegalArgumentException e) {
return;
}
final Jid contactJid;
try {
contactJid = Jid.ofEscaped(binding.jid.getText().toString());
} catch (final IllegalArgumentException e) {
binding.jidLayout.setError(getActivity().getString(R.string.invalid_jid));
return;
}
View.OnClickListener dialogOnClick = v -> { if (!issuedWarning && sanityCheckJid) {
handleEnter(binding, account); if (contactJid.isDomainJid()) {
}; 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().toEscapedString())) {
binding.jidLayout.setError(
getActivity().getString(R.string.this_looks_like_channel));
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway);
issuedWarning = true;
return;
}
}
binding.jid.setOnEditorActionListener((v, actionId, event) -> { if (mListener != null) {
handleEnter(binding, account); try {
return true; if (mListener.onEnterJidDialogPositive(accountJid, contactJid)) {
}); dialog.dismiss();
}
} catch (JidError error) {
binding.jidLayout.setError(error.toString());
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
issuedWarning = false;
}
}
}
dialog.show(); public void setOnEnterJidDialogPositiveListener(OnEnterJidDialogPositiveListener listener) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(dialogOnClick); this.mListener = listener;
return dialog; }
}
private void handleEnter(EnterJidDialogBinding binding, String account) { @Override
final Jid accountJid; public void onBackendConnected() {
if (!binding.account.isEnabled() && account == null) { refreshKnownHosts();
return; }
}
try {
if (Config.DOMAIN_LOCK != null) {
accountJid = Jid.ofEscaped((String) binding.account.getSelectedItem(), Config.DOMAIN_LOCK, null);
} else {
accountJid = Jid.ofEscaped((String) binding.account.getSelectedItem());
}
} catch (final IllegalArgumentException e) {
return;
}
final Jid contactJid;
try {
contactJid = Jid.ofEscaped(binding.jid.getText().toString());
} catch (final IllegalArgumentException e) {
binding.jidLayout.setError(getActivity().getString(R.string.invalid_jid));
return;
}
if (!issuedWarning && sanityCheckJid) { private void refreshKnownHosts() {
if (contactJid.isDomainJid()) { final Activity activity = getActivity();
binding.jidLayout.setError(getActivity().getString(R.string.this_looks_like_a_domain)); if (activity instanceof XmppActivity) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway); final XmppConnectionService service = ((XmppActivity) activity).xmppConnectionService;
issuedWarning = true; if (service == null) {
return; return;
} }
if (suspiciousSubDomain(contactJid.getDomain().toEscapedString())) { final Collection<String> hosts = service.getKnownHosts();
binding.jidLayout.setError(getActivity().getString(R.string.this_looks_like_channel)); this.knownHostsAdapter.refresh(hosts);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway); this.whitelistedDomains = hosts;
issuedWarning = true; }
return; }
}
}
if (mListener != null) { @Override
try { public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
if (mListener.onEnterJidDialogPositive(accountJid, contactJid)) {
dialog.dismiss();
}
} catch (JidError error) {
binding.jidLayout.setError(error.toString());
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
issuedWarning = false;
}
}
}
public void setOnEnterJidDialogPositiveListener(OnEnterJidDialogPositiveListener listener) { @Override
this.mListener = listener; public void onTextChanged(CharSequence s, int start, int before, int count) {}
}
@Override @Override
public void onBackendConnected() { public void afterTextChanged(Editable s) {
refreshKnownHosts(); if (issuedWarning) {
} dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
binding.jidLayout.setError(null);
issuedWarning = false;
}
}
private void refreshKnownHosts() { public interface OnEnterJidDialogPositiveListener {
Activity activity = getActivity(); boolean onEnterJidDialogPositive(Jid account, Jid contact) throws EnterJidDialog.JidError;
if (activity instanceof XmppActivity) { }
Collection<String> hosts = ((XmppActivity) activity).xmppConnectionService.getKnownHosts();
this.knownHostsAdapter.refresh(hosts);
this.whitelistedDomains = hosts;
}
}
@Override public static class JidError extends Exception {
public void beforeTextChanged(CharSequence s, int start, int count, int after) { final String msg;
} public JidError(final String msg) {
this.msg = msg;
}
@Override @NonNull
public void onTextChanged(CharSequence s, int start, int before, int count) { public String toString() {
return msg;
}
}
} @Override
public void onDestroyView() {
Dialog dialog = getDialog();
if (dialog != null && getRetainInstance()) {
dialog.setDismissMessage(null);
}
super.onDestroyView();
}
@Override private boolean suspiciousSubDomain(String domain) {
public void afterTextChanged(Editable s) { if (this.whitelistedDomains.contains(domain)) {
if (issuedWarning) { return false;
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add); }
binding.jidLayout.setError(null); final String[] parts = domain.split("\\.");
issuedWarning = false; return parts.length >= 3 && SUSPICIOUS_DOMAINS.contains(parts[0]);
} }
}
public interface OnEnterJidDialogPositiveListener {
boolean onEnterJidDialogPositive(Jid account, Jid contact) throws EnterJidDialog.JidError;
}
public static class JidError extends Exception {
final String msg;
public JidError(final String msg) {
this.msg = msg;
}
public String toString() {
return msg;
}
}
@Override
public void onDestroyView() {
Dialog dialog = getDialog();
if (dialog != null && getRetainInstance()) {
dialog.setDismissMessage(null);
}
super.onDestroyView();
}
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]);
}
} }