dynamically get known hosts in dialog

This commit is contained in:
Daniel Gultsch 2018-03-14 10:07:52 +01:00
parent 2af12afb64
commit acf0b995ab
7 changed files with 172 additions and 109 deletions

View file

@ -4,25 +4,18 @@ import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import rocks.xmpp.addr.Jid;
public class BlocklistActivity extends AbstractSearchableListItemActivity implements OnUpdateBlocklist {
private Collection<String> mKnownHosts = new ArrayList<>();
private Account account = null;
@ -45,7 +38,10 @@ public class BlocklistActivity extends AbstractSearchableListItemActivity implem
}
}
filterContacts();
this.mKnownHosts = xmppConnectionService.getKnownHosts();
Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
if (fragment != null && fragment instanceof OnBackendConnected) {
((OnBackendConnected) fragment).onBackendConnected();
}
}
@Override
@ -71,9 +67,12 @@ public class BlocklistActivity extends AbstractSearchableListItemActivity implem
}
ft.addToBackStack(null);
EnterJidDialog dialog = EnterJidDialog.newInstance(
mKnownHosts, null,
getString(R.string.block_jabber_id), getString(R.string.block),
null, account.getJid().asBareJid().toString(), true
null,
getString(R.string.block_jabber_id),
getString(R.string.block),
null,
account.getJid().asBareJid().toString(),
true
);
dialog.setOnEnterJidDialogPositiveListener((accountJid, contactJid) -> {

View file

@ -19,7 +19,6 @@ import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
@ -32,6 +31,7 @@ import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
import eu.siacs.conversations.ui.util.ActivityResult;
import eu.siacs.conversations.ui.util.PendingItem;
import eu.siacs.conversations.utils.XmppUri;
@ -40,7 +40,6 @@ import rocks.xmpp.addr.Jid;
public class ChooseContactActivity extends AbstractSearchableListItemActivity {
public static final String EXTRA_TITLE_RES_ID = "extra_title_res_id";
private List<String> mActivatedAccounts = new ArrayList<>();
private Collection<String> mKnownHosts;
private Set<Contact> selected;
private Set<String> filterContacts;
@ -240,7 +239,6 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity {
ft.addToBackStack(null);
Jid jid = uri == null ? null : uri.getJid();
EnterJidDialog dialog = EnterJidDialog.newInstance(
mKnownHosts,
mActivatedAccounts,
getString(R.string.enter_contact),
getString(R.string.select),
@ -301,11 +299,14 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity {
}
}
}
this.mKnownHosts = xmppConnectionService.getKnownHosts();
ActivityResult activityResult = this.postponedActivityResult.pop();
if (activityResult != null) {
handleActivityResult(activityResult);
}
Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
if (fragment != null && fragment instanceof OnBackendConnected) {
((OnBackendConnected) fragment).onBackendConnected();
}
}
@Override

View file

@ -1,11 +1,13 @@
package eu.siacs.conversations.ui;
import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
@ -13,6 +15,7 @@ import android.widget.Spinner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
@ -20,10 +23,11 @@ import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.EnterJidDialogBinding;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
import eu.siacs.conversations.ui.util.DelayedHintHelper;
import rocks.xmpp.addr.Jid;
public class EnterJidDialog extends DialogFragment{
public class EnterJidDialog extends DialogFragment implements OnBackendConnected {
private OnEnterJidDialogPositiveListener mListener = null;
@ -33,21 +37,20 @@ public class EnterJidDialog extends DialogFragment{
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 CONFERENCE_HOSTS_KEY = "known_conference_hosts";
public static EnterJidDialog newInstance(
Collection<String> knownHosts, final List<String> activatedAccounts,
final String title, final String positiveButton,
final String prefilledJid, final String account, boolean allowEditJid) {
private KnownHostsAdapter knownHostsAdapter;
public static EnterJidDialog newInstance(final List<String> activatedAccounts,
final String title, final String positiveButton,
final String prefilledJid, final String account, boolean allowEditJid) {
EnterJidDialog dialog = new EnterJidDialog();
Bundle bundle = new Bundle();
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.putSerializable(CONFERENCE_HOSTS_KEY, (HashSet) knownHosts);
dialog.setArguments(bundle);
return dialog;
}
@ -58,13 +61,23 @@ public class EnterJidDialog extends DialogFragment{
setRetainInstance(true);
}
@Override
public void onStart() {
super.onStart();
final Activity activity = getActivity();
if (activity instanceof XmppActivity && ((XmppActivity) activity).xmppConnectionService != null) {
refreshKnownHosts();
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getArguments().getString(TITLE_KEY));
EnterJidDialogBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.enter_jid_dialog, null, false);
binding.jid.setAdapter(new KnownHostsAdapter(getActivity(), R.layout.simple_list_item, (Collection<String>) getArguments().getSerializable(CONFERENCE_HOSTS_KEY)));
this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.simple_list_item);
binding.jid.setAdapter(this.knownHostsAdapter);
String prefilledJid = getArguments().getString(PREFILLED_JID_KEY);
if (prefilledJid != null) {
binding.jid.append(prefilledJid);
@ -84,7 +97,7 @@ public class EnterJidDialog extends DialogFragment{
} else {
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
R.layout.simple_list_item,
new String[] { account });
new String[]{account});
binding.account.setEnabled(false);
adapter.setDropDownViewResource(R.layout.simple_list_item);
binding.account.setAdapter(adapter);
@ -117,12 +130,12 @@ public class EnterJidDialog extends DialogFragment{
return;
}
if(mListener != null) {
if (mListener != null) {
try {
if(mListener.onEnterJidDialogPositive(accountJid, contactJid)) {
if (mListener.onEnterJidDialogPositive(accountJid, contactJid)) {
dialog.dismiss();
}
} catch(JidError error) {
} catch (JidError error) {
binding.jid.setError(error.toString());
}
}
@ -136,6 +149,19 @@ public class EnterJidDialog extends DialogFragment{
this.mListener = listener;
}
@Override
public void onBackendConnected() {
refreshKnownHosts();
}
private void refreshKnownHosts() {
Activity activity = getActivity();
if (activity instanceof XmppActivity) {
Collection<String> hosts = ((XmppActivity) activity).xmppConnectionService.getKnownHosts();
this.knownHostsAdapter.refresh(hosts);
}
}
public interface OnEnterJidDialogPositiveListener {
boolean onEnterJidDialogPositive(Jid account, Jid contact) throws EnterJidDialog.JidError;
}

View file

@ -1,5 +1,6 @@
package eu.siacs.conversations.ui;
import android.app.Activity;
import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
@ -22,79 +23,97 @@ import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.JoinConferenceDialogBinding;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
import eu.siacs.conversations.ui.util.DelayedHintHelper;
public class JoinConferenceDialog extends DialogFragment {
public class JoinConferenceDialog extends DialogFragment implements OnBackendConnected {
private static final String PREFILLED_JID_KEY = "prefilled_jid";
private static final String ACCOUNTS_LIST_KEY = "activated_accounts_list";
private static final String CONFERENCE_HOSTS_KEY = "known_conference_hosts";
private JoinConferenceDialogListener mListener;
private static final String PREFILLED_JID_KEY = "prefilled_jid";
private static final String ACCOUNTS_LIST_KEY = "activated_accounts_list";
private JoinConferenceDialogListener mListener;
private KnownHostsAdapter knownHostsAdapter;
public static JoinConferenceDialog newInstance(String prefilledJid, List<String> accounts, Collection<String> conferenceHosts) {
JoinConferenceDialog dialog = new JoinConferenceDialog();
Bundle bundle = new Bundle();
bundle.putString(PREFILLED_JID_KEY, prefilledJid);
bundle.putStringArrayList(ACCOUNTS_LIST_KEY, (ArrayList<String>) accounts);
bundle.putSerializable(CONFERENCE_HOSTS_KEY, (HashSet) conferenceHosts);
dialog.setArguments(bundle);
return dialog;
}
public static JoinConferenceDialog newInstance(String prefilledJid, List<String> accounts) {
JoinConferenceDialog dialog = new JoinConferenceDialog();
Bundle bundle = new Bundle();
bundle.putString(PREFILLED_JID_KEY, prefilledJid);
bundle.putStringArrayList(ACCOUNTS_LIST_KEY, (ArrayList<String>) accounts);
dialog.setArguments(bundle);
return dialog;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_title_join_conference);
JoinConferenceDialogBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.join_conference_dialog, null, false);
DelayedHintHelper.setHint(R.string.conference_address_example, binding.jid);
binding.jid.setAdapter(new KnownHostsAdapter(getActivity(), R.layout.simple_list_item, (Collection<String>) getArguments().getSerializable(CONFERENCE_HOSTS_KEY)));
String prefilledJid = getArguments().getString(PREFILLED_JID_KEY);
if (prefilledJid != null) {
binding.jid.append(prefilledJid);
}
StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), binding.account);
builder.setView(binding.getRoot());
builder.setPositiveButton(R.string.join, null);
builder.setNegativeButton(R.string.cancel, null);
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.onJoinDialogPositiveClick(dialog, binding.account, binding.jid, binding.bookmark.isChecked());
}
});
return dialog;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_title_join_conference);
JoinConferenceDialogBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.join_conference_dialog, null, false);
DelayedHintHelper.setHint(R.string.conference_address_example, binding.jid);
this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.simple_list_item);
binding.jid.setAdapter(knownHostsAdapter);
String prefilledJid = getArguments().getString(PREFILLED_JID_KEY);
if (prefilledJid != null) {
binding.jid.append(prefilledJid);
}
StartConversationActivity.populateAccountSpinner(getActivity(), getArguments().getStringArrayList(ACCOUNTS_LIST_KEY), binding.account);
builder.setView(binding.getRoot());
builder.setPositiveButton(R.string.join, null);
builder.setNegativeButton(R.string.cancel, null);
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(view -> mListener.onJoinDialogPositiveClick(dialog, binding.account, binding.jid, binding.bookmark.isChecked()));
return dialog;
}
public interface JoinConferenceDialogListener {
void onJoinDialogPositiveClick(Dialog dialog, Spinner spinner, AutoCompleteTextView jid, boolean isBookmarkChecked);
}
@Override
public void onBackendConnected() {
refreshKnownHosts();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (JoinConferenceDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement JoinConferenceDialogListener");
}
}
private void refreshKnownHosts() {
Activity activity = getActivity();
if (activity instanceof XmppActivity) {
Collection<String> hosts = ((XmppActivity) activity).xmppConnectionService.getKnownConferenceHosts();
this.knownHostsAdapter.refresh(hosts);
}
}
@Override
public void onDestroyView() {
Dialog dialog = getDialog();
if (dialog != null && getRetainInstance()) {
dialog.setDismissMessage(null);
}
super.onDestroyView();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (JoinConferenceDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement JoinConferenceDialogListener");
}
}
@Override
public void onDestroyView() {
Dialog dialog = getDialog();
if (dialog != null && getRetainInstance()) {
dialog.setDismissMessage(null);
}
super.onDestroyView();
}
@Override
public void onStart() {
super.onStart();
final Activity activity = getActivity();
if (activity instanceof XmppActivity && ((XmppActivity) activity).xmppConnectionService != null) {
refreshKnownHosts();
}
}
public interface JoinConferenceDialogListener {
void onJoinDialogPositiveClick(Dialog dialog, Spinner spinner, AutoCompleteTextView jid, boolean isBookmarkChecked);
}
}

View file

@ -35,6 +35,7 @@ import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.TypefaceSpan;
import android.util.Log;
import android.util.Pair;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
@ -76,6 +77,7 @@ import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.adapter.ListItemAdapter;
import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
import eu.siacs.conversations.ui.service.EmojiService;
import eu.siacs.conversations.ui.util.DelayedHintHelper;
import eu.siacs.conversations.utils.XmppUri;
@ -95,8 +97,6 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
private List<ListItem> conferences = new ArrayList<>();
private ListItemAdapter mConferenceAdapter;
private List<String> mActivatedAccounts = new ArrayList<>();
private Collection<String> mKnownHosts;
private Collection<String> mKnownConferenceHosts;
private Invite mPendingInvite = null;
private EditText mSearchEditText;
private AtomicBoolean mRequestedContactsPermission = new AtomicBoolean(false);
@ -441,15 +441,18 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
@SuppressLint("InflateParams")
protected void showCreateContactDialog(final String prefilledJid, final Invite invite) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
Fragment prev = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
EnterJidDialog dialog = EnterJidDialog.newInstance(
mKnownHosts, mActivatedAccounts,
getString(R.string.dialog_title_create_contact), getString(R.string.create),
prefilledJid, null, invite == null || !invite.hasFingerprints()
mActivatedAccounts,
getString(R.string.dialog_title_create_contact),
getString(R.string.create),
prefilledJid,
null,
invite == null || !invite.hasFingerprints()
);
dialog.setOnEnterJidDialogPositiveListener((accountJid, contactJid) -> {
@ -480,30 +483,30 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
return true;
}
});
dialog.show(ft, "dialog");
dialog.show(ft, FRAGMENT_TAG_DIALOG);
}
@SuppressLint("InflateParams")
protected void showJoinConferenceDialog(final String prefilledJid) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
Fragment prev = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
JoinConferenceDialog joinConferenceFragment = JoinConferenceDialog.newInstance(prefilledJid, mActivatedAccounts, mKnownConferenceHosts);
joinConferenceFragment.show(ft, "dialog");
JoinConferenceDialog joinConferenceFragment = JoinConferenceDialog.newInstance(prefilledJid, mActivatedAccounts);
joinConferenceFragment.show(ft, FRAGMENT_TAG_DIALOG);
}
private void showCreateConferenceDialog() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
Fragment prev = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
CreateConferenceDialog createConferenceFragment = CreateConferenceDialog.newInstance(mActivatedAccounts);
createConferenceFragment.show(ft, "dialog");
createConferenceFragment.show(ft, FRAGMENT_TAG_DIALOG);
}
private Account getSelectedAccount(Spinner spinner) {
@ -707,8 +710,6 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
ab.setDisplayHomeAsUpEnabled(false);
ab.setHomeButtonEnabled(false);
}
this.mKnownHosts = xmppConnectionService.getKnownHosts();
this.mKnownConferenceHosts = xmppConnectionService.getKnownConferenceHosts();
if (this.mPendingInvite != null) {
mPendingInvite.invite();
this.mPendingInvite = null;
@ -723,6 +724,11 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
filter(null);
}
setIntent(null);
Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
if (fragment != null && fragment instanceof OnBackendConnected) {
Log.d(Config.LOGTAG,"calling on backend connected on dialog");
((OnBackendConnected) fragment).onBackendConnected();
}
}
protected boolean handleIntent(Intent intent) {

View file

@ -87,6 +87,8 @@ public abstract class XmppActivity extends AppCompatActivity {
protected int mColorOrange;
protected int mColorGreen;
protected static final String FRAGMENT_TAG_DIALOG = "dialog";
private boolean isCameraFeatureAvailable = false;
protected boolean mUseSubject = true;

View file

@ -64,6 +64,16 @@ public class KnownHostsAdapter extends ArrayAdapter<String> {
domains = new ArrayList<>(mKnownHosts);
}
public KnownHostsAdapter(Context context, int viewResourceId) {
super(context, viewResourceId, new ArrayList<>());
domains = new ArrayList<>();
}
public void refresh(Collection<String> knownHosts) {
domains = new ArrayList<>(knownHosts);
notifyDataSetChanged();
}
@Override
@NonNull
public Filter getFilter() {