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,8 +30,8 @@ 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 =
private static final List<String> SUSPICIOUS_DOMAINS = Arrays.asList("conference","muc","room","rooms","chat"); Arrays.asList("conference", "muc", "room", "rooms", "chat");
private OnEnterJidDialogPositiveListener mListener = null; private OnEnterJidDialogPositiveListener mListener = null;
@ -49,13 +50,16 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
private AlertDialog dialog; private AlertDialog dialog;
private boolean sanityCheckJid = false; private boolean sanityCheckJid = false;
private boolean issuedWarning = false; private boolean issuedWarning = false;
public static EnterJidDialog newInstance(final List<String> activatedAccounts, public static EnterJidDialog newInstance(
final String title, final String positiveButton, final List<String> activatedAccounts,
final String prefilledJid, final String account, final String title,
boolean allowEditJid, final boolean sanity_check_jid) { final String positiveButton,
final String prefilledJid,
final String account,
boolean allowEditJid,
final boolean sanity_check_jid) {
EnterJidDialog dialog = new EnterJidDialog(); EnterJidDialog dialog = new EnterJidDialog();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(TITLE_KEY, title); bundle.putString(TITLE_KEY, title);
@ -79,7 +83,8 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
final Activity activity = getActivity(); final Activity activity = getActivity();
if (activity instanceof XmppActivity && ((XmppActivity) activity).xmppConnectionService != null) { if (activity instanceof XmppActivity
&& ((XmppActivity) activity).xmppConnectionService != null) {
refreshKnownHosts(); refreshKnownHosts();
} }
} }
@ -89,7 +94,9 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getArguments().getString(TITLE_KEY)); builder.setTitle(getArguments().getString(TITLE_KEY));
binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.enter_jid_dialog, null, false); binding =
DataBindingUtil.inflate(
getActivity().getLayoutInflater(), R.layout.enter_jid_dialog, null, false);
this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.simple_list_item); this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.simple_list_item);
binding.jid.setAdapter(this.knownHostsAdapter); binding.jid.setAdapter(this.knownHostsAdapter);
binding.jid.addTextChangedListener(this); binding.jid.addTextChangedListener(this);
@ -109,28 +116,31 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
String account = getArguments().getString(ACCOUNT_KEY); String account = getArguments().getString(ACCOUNT_KEY);
if (account == null) { if (account == null) {
StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), binding.account); StartConversationActivity.populateAccountSpinner(
getActivity(),
getArguments().getStringArrayList(ACCOUNTS_LIST_KEY),
binding.account);
} else { } else {
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), ArrayAdapter<String> adapter =
R.layout.simple_list_item, new ArrayAdapter<>(
new String[]{account}); getActivity(), R.layout.simple_list_item, new String[] {account});
binding.account.setEnabled(false); binding.account.setEnabled(false);
adapter.setDropDownViewResource(R.layout.simple_list_item); adapter.setDropDownViewResource(R.layout.simple_list_item);
binding.account.setAdapter(adapter); binding.account.setAdapter(adapter);
} }
builder.setView(binding.getRoot()); builder.setView(binding.getRoot());
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null); builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null);
this.dialog = builder.create(); this.dialog = builder.create();
View.OnClickListener dialogOnClick = v -> { View.OnClickListener dialogOnClick =
v -> {
handleEnter(binding, account); handleEnter(binding, account);
}; };
binding.jid.setOnEditorActionListener((v, actionId, event) -> { binding.jid.setOnEditorActionListener(
(v, actionId, event) -> {
handleEnter(binding, account); handleEnter(binding, account);
return true; return true;
}); });
@ -147,7 +157,11 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
} }
try { try {
if (Config.DOMAIN_LOCK != null) { if (Config.DOMAIN_LOCK != null) {
accountJid = Jid.ofEscaped((String) binding.account.getSelectedItem(), Config.DOMAIN_LOCK, null); accountJid =
Jid.ofEscaped(
(String) binding.account.getSelectedItem(),
Config.DOMAIN_LOCK,
null);
} else { } else {
accountJid = Jid.ofEscaped((String) binding.account.getSelectedItem()); accountJid = Jid.ofEscaped((String) binding.account.getSelectedItem());
} }
@ -164,13 +178,15 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
if (!issuedWarning && sanityCheckJid) { if (!issuedWarning && sanityCheckJid) {
if (contactJid.isDomainJid()) { if (contactJid.isDomainJid()) {
binding.jidLayout.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); dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway);
issuedWarning = true; issuedWarning = true;
return; return;
} }
if (suspiciousSubDomain(contactJid.getDomain().toEscapedString())) { if (suspiciousSubDomain(contactJid.getDomain().toEscapedString())) {
binding.jidLayout.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); dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway);
issuedWarning = true; issuedWarning = true;
return; return;
@ -200,23 +216,23 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
} }
private void refreshKnownHosts() { private void refreshKnownHosts() {
Activity activity = getActivity(); final Activity activity = getActivity();
if (activity instanceof XmppActivity) { if (activity instanceof XmppActivity) {
Collection<String> hosts = ((XmppActivity) activity).xmppConnectionService.getKnownHosts(); final XmppConnectionService service = ((XmppActivity) activity).xmppConnectionService;
if (service == null) {
return;
}
final Collection<String> hosts = service.getKnownHosts();
this.knownHostsAdapter.refresh(hosts); this.knownHostsAdapter.refresh(hosts);
this.whitelistedDomains = hosts; this.whitelistedDomains = hosts;
} }
} }
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
}
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {}
}
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
@ -238,6 +254,7 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected
this.msg = msg; this.msg = msg;
} }
@NonNull
public String toString() { public String toString() {
return msg; return msg;
} }