Search in sources :

Example 1 with Presences

use of de.pixart.messenger.entities.Presences in project Pix-Art-Messenger by kriztan.

the class XmppActivity method selectPresence.

public void selectPresence(final Conversation conversation, final PresenceSelector.OnPresenceSelected listener) {
    final Contact contact = conversation.getContact();
    if (conversation.hasValidOtrSession()) {
        SessionID id = conversation.getOtrSession().getSessionID();
        Jid jid;
        try {
            jid = Jid.fromString(id.getAccountID() + "/" + id.getUserID());
        } catch (InvalidJidException e) {
            jid = null;
        }
        conversation.setNextCounterpart(jid);
        listener.onPresenceSelected();
    } else if (!contact.showInRoster()) {
        showAddToRosterDialog(conversation);
    } else {
        final Presences presences = contact.getPresences();
        if (presences.size() == 0) {
            if (!contact.getOption(Contact.Options.TO) && !contact.getOption(Contact.Options.ASKING) && contact.getAccount().getStatus() == Account.State.ONLINE) {
                showAskForPresenceDialog(contact);
            } else if (!contact.getOption(Contact.Options.TO) || !contact.getOption(Contact.Options.FROM)) {
                PresenceSelector.warnMutualPresenceSubscription(this, conversation, listener);
            } else {
                conversation.setNextCounterpart(null);
                listener.onPresenceSelected();
            }
        } else if (presences.size() == 1) {
            String presence = presences.toResourceArray()[0];
            try {
                conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), presence));
            } catch (InvalidJidException e) {
                conversation.setNextCounterpart(null);
            }
            listener.onPresenceSelected();
        } else {
            PresenceSelector.showPresenceSelectionDialog(this, conversation, listener);
        }
    }
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) Presences(de.pixart.messenger.entities.Presences) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) SessionID(net.java.otr4j.session.SessionID) Contact(de.pixart.messenger.entities.Contact)

Example 2 with Presences

use of de.pixart.messenger.entities.Presences in project Pix-Art-Messenger by kriztan.

the class PresenceSelector method showPresenceSelectionDialog.

public static void showPresenceSelectionDialog(Activity activity, final Conversation conversation, final OnPresenceSelected listener) {
    final Contact contact = conversation.getContact();
    final Presences presences = contact.getPresences();
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(activity.getString(R.string.choose_presence));
    final String[] resourceArray = presences.toResourceArray();
    Pair<Map<String, String>, Map<String, String>> typeAndName = presences.toTypeAndNameMap();
    final Map<String, String> resourceTypeMap = typeAndName.first;
    final Map<String, String> resourceNameMap = typeAndName.second;
    final String[] readableIdentities = new String[resourceArray.length];
    final AtomicInteger selectedResource = new AtomicInteger(0);
    for (int i = 0; i < resourceArray.length; ++i) {
        String resource = resourceArray[i];
        if (resource.equals(contact.getLastResource())) {
            selectedResource.set(i);
        }
        String type = resourceTypeMap.get(resource);
        String name = resourceNameMap.get(resource);
        if (type != null) {
            if (Collections.frequency(resourceTypeMap.values(), type) == 1) {
                readableIdentities[i] = translateType(activity, type);
            } else if (name != null) {
                if (Collections.frequency(resourceNameMap.values(), name) == 1 || CryptoHelper.UUID_PATTERN.matcher(resource).matches()) {
                    readableIdentities[i] = translateType(activity, type) + "  (" + name + ")";
                } else {
                    readableIdentities[i] = translateType(activity, type) + " (" + name + " / " + resource + ")";
                }
            } else {
                readableIdentities[i] = translateType(activity, type) + " (" + resource + ")";
            }
        } else {
            readableIdentities[i] = resource;
        }
    }
    builder.setSingleChoiceItems(readableIdentities, selectedResource.get(), (dialog, which) -> selectedResource.set(which));
    builder.setNegativeButton(R.string.cancel, null);
    builder.setPositiveButton(R.string.ok, (dialog, which) -> {
        try {
            Jid next = Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), resourceArray[selectedResource.get()]);
            conversation.setNextCounterpart(next);
        } catch (InvalidJidException e) {
            conversation.setNextCounterpart(null);
        }
        listener.onPresenceSelected();
    });
    builder.create().show();
}
Also used : AlertDialog(android.app.AlertDialog) Jid(de.pixart.messenger.xmpp.jid.Jid) Presences(de.pixart.messenger.entities.Presences) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Contact(de.pixart.messenger.entities.Contact) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map)

Aggregations

Contact (de.pixart.messenger.entities.Contact)2 Presences (de.pixart.messenger.entities.Presences)2 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)2 Jid (de.pixart.messenger.xmpp.jid.Jid)2 AlertDialog (android.app.AlertDialog)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SessionID (net.java.otr4j.session.SessionID)1