Search in sources :

Example 1 with ResourceAdapter

use of com.xabber.android.ui.adapter.ResourceAdapter in project xabber-android by redsolution.

the class ChatFragment method showResourceChoiceAlert.

public void showResourceChoiceAlert(final AccountJid account, final UserJid user, final boolean restartSession) {
    final List<Presence> allPresences = RosterManager.getInstance().getPresences(account, user.getJid());
    final List<Map<String, String>> items = new ArrayList<>();
    for (Presence presence : allPresences) {
        Jid fromJid = presence.getFrom();
        ClientInfo clientInfo = CapabilitiesManager.getInstance().getCachedClientInfo(fromJid);
        String client = "";
        if (clientInfo == null) {
            CapabilitiesManager.getInstance().requestClientInfoByUser(account, fromJid);
        } else if (clientInfo == ClientInfo.INVALID_CLIENT_INFO) {
            client = getString(R.string.unknown);
        } else {
            String name = clientInfo.getName();
            if (name != null) {
                client = name;
            }
            String type = clientInfo.getType();
            if (type != null) {
                if (client.isEmpty()) {
                    client = type;
                } else {
                    client = client + "/" + type;
                }
            }
        }
        Map<String, String> map = new HashMap<>();
        if (!client.isEmpty()) {
            map.put(ResourceAdapter.KEY_CLIENT, client);
        }
        Resourcepart resourceOrNull = fromJid.getResourceOrNull();
        if (resourceOrNull != null) {
            map.put(ResourceAdapter.KEY_RESOURCE, resourceOrNull.toString());
            items.add(map);
        }
    }
    final ResourceAdapter adapter = new ResourceAdapter(getActivity(), items);
    adapter.setCheckedItem(checkedResource);
    if (items.size() > 0) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.otr_select_resource);
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                checkedResource = adapter.getCheckedItem();
            }
        });
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                checkedResource = adapter.getCheckedItem();
                try {
                    RegularChat chat = (RegularChat) getChat();
                    if (chat != null) {
                        chat.setOTRresource(Resourcepart.from(items.get(checkedResource).get(ResourceAdapter.KEY_RESOURCE)));
                        if (restartSession)
                            restartEncryption(account, user);
                        else
                            startEncryption(account, user);
                    } else {
                        Toast.makeText(getActivity(), R.string.otr_select_toast_error, Toast.LENGTH_SHORT).show();
                    }
                } catch (XmppStringprepException e) {
                    e.printStackTrace();
                    Toast.makeText(getActivity(), R.string.otr_select_toast_error, Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setSingleChoiceItems(adapter, checkedResource, null).show();
    } else {
        Toast.makeText(getActivity(), R.string.otr_select_toast_resources_not_found, Toast.LENGTH_SHORT).show();
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Jid(org.jxmpp.jid.Jid) AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid) HashMap(java.util.HashMap) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) RegularChat(com.xabber.android.data.message.RegularChat) Resourcepart(org.jxmpp.jid.parts.Resourcepart) Presence(org.jivesoftware.smack.packet.Presence) ResourceAdapter(com.xabber.android.ui.adapter.ResourceAdapter) ClientInfo(com.xabber.android.data.extension.capability.ClientInfo) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

DialogInterface (android.content.DialogInterface)1 AlertDialog (android.support.v7.app.AlertDialog)1 AccountJid (com.xabber.android.data.entity.AccountJid)1 UserJid (com.xabber.android.data.entity.UserJid)1 ClientInfo (com.xabber.android.data.extension.capability.ClientInfo)1 RegularChat (com.xabber.android.data.message.RegularChat)1 ResourceAdapter (com.xabber.android.ui.adapter.ResourceAdapter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Presence (org.jivesoftware.smack.packet.Presence)1 Jid (org.jxmpp.jid.Jid)1 Resourcepart (org.jxmpp.jid.parts.Resourcepart)1 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)1