Search in sources :

Example 56 with Presence

use of org.jivesoftware.smack.packet.Presence 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)

Example 57 with Presence

use of org.jivesoftware.smack.packet.Presence in project xabber-android by redsolution.

the class AttentionManager method sendAttention.

public void sendAttention(AccountJid account, UserJid user) throws NetworkException {
    AbstractChat chat = MessageManager.getInstance().getOrCreateChat(account, user);
    if (!(chat instanceof RegularChat)) {
        throw new NetworkException(R.string.ENTRY_IS_NOT_FOUND);
    }
    Jid to = chat.getTo();
    if (to.getResourceOrNull() == null || to.getResourceOrNull().equals(Resourcepart.EMPTY)) {
        final Presence presence = RosterManager.getInstance().getPresence(account, user);
        if (presence == null) {
            to = null;
        } else {
            to = presence.getFrom();
        }
    }
    if (to == null) {
        throw new NetworkException(R.string.ENTRY_IS_NOT_AVAILABLE);
    }
    if (!CapabilitiesManager.getInstance().isFeatureSupported(to, AttentionExtension.NAMESPACE)) {
        throw new NetworkException(R.string.ATTENTION_IS_NOT_SUPPORTED);
    }
    Message message = new Message();
    message.setTo(to);
    message.setType(Message.Type.headline);
    message.addExtension(new AttentionExtension());
    StanzaSender.sendStanza(account, message);
    chat.newAction(null, null, ChatAction.attention_called);
}
Also used : UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) Jid(org.jxmpp.jid.Jid) Message(org.jivesoftware.smack.packet.Message) AbstractChat(com.xabber.android.data.message.AbstractChat) Presence(org.jivesoftware.smack.packet.Presence) NetworkException(com.xabber.android.data.NetworkException) AttentionExtension(org.jivesoftware.smackx.attention.packet.AttentionExtension) RegularChat(com.xabber.android.data.message.RegularChat)

Example 58 with Presence

use of org.jivesoftware.smack.packet.Presence in project xabber-android by redsolution.

the class RoomChat method onPacket.

@Override
protected boolean onPacket(UserJid bareAddress, Stanza stanza, boolean isCarbons) {
    if (!super.onPacket(bareAddress, stanza, isCarbons)) {
        return false;
    }
    // MUCUser mucUserExtension = MUCUser.from(stanza);
    // if (mucUserExtension != null && mucUserExtension.getInvite() != null) {
    // return false;
    // }
    final org.jxmpp.jid.Jid from = stanza.getFrom();
    final Resourcepart resource = from.getResourceOrNull();
    if (stanza instanceof Message) {
        final Message message = (Message) stanza;
        if (message.getType() == Message.Type.error) {
            UserJid invite = invites.remove(message.getStanzaId());
            if (invite != null) {
                newAction(nickname, invite.toString(), ChatAction.invite_error);
            }
            return true;
        }
        MUCUser mucUser = MUCUser.from(stanza);
        if (mucUser != null && mucUser.getDecline() != null) {
            onInvitationDeclined(mucUser.getDecline().getFrom(), mucUser.getDecline().getReason());
            return true;
        }
        if (mucUser != null && mucUser.getStatus() != null && mucUser.getStatus().contains(MUCUser.Status.create("100")) && ChatManager.getInstance().isSuppress100(account, user)) {
            // 'This room is not anonymous'
            return true;
        }
        final String text = message.getBody();
        final String subject = message.getSubject();
        if (text == null && subject == null) {
            return true;
        }
        if (subject != null) {
            if (this.subject.equals(subject)) {
                return true;
            }
            this.subject = subject;
            RosterManager.onContactChanged(account, bareAddress);
            newAction(resource, subject, ChatAction.subject);
        } else {
            boolean notify = true;
            String stanzaId = message.getStanzaId();
            // disabling because new messages without stanza will be repeated
            // if (stanzaId == null) stanzaId = UUID.randomUUID().toString();
            DelayInformation delayInformation = DelayInformation.from(message);
            Date delay = null;
            if (delayInformation != null) {
                delay = delayInformation.getStamp();
            }
            if (delay != null) {
                notify = false;
            }
            final long startTime = System.currentTimeMillis();
            Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
            final MessageItem sameMessage = realm.where(MessageItem.class).equalTo(MessageItem.Fields.STANZA_ID, stanzaId).findFirst();
            // Server send our own message back
            if (sameMessage != null) {
                // realm.beginTransaction();
                // sameMessage.setDelivered(true);
                // realm.commitTransaction();
                LogManager.d("REALM", Thread.currentThread().getName() + " save message delivered: " + (System.currentTimeMillis() - startTime));
                return true;
            }
            if (isSelf(resource)) {
                // Own message from other client
                notify = false;
            }
            updateThreadId(message.getThread());
            createAndSaveNewMessage(resource, text, null, delay, true, notify, false, false, stanzaId);
        }
    } else if (stanza instanceof Presence) {
        Presence presence = (Presence) stanza;
        if (presence.getType() == Presence.Type.available) {
            Occupant oldOccupant = occupants.get(resource);
            Occupant newOccupant = createOccupant(resource, presence);
            newOccupant.setJid(from);
            occupants.put(resource, newOccupant);
            if (oldOccupant == null) {
                onAvailable(resource);
                RosterManager.onContactChanged(account, user);
            } else {
                boolean changed = false;
                if (oldOccupant.getAffiliation() != newOccupant.getAffiliation()) {
                    changed = true;
                    onAffiliationChanged(resource, newOccupant.getAffiliation());
                }
                if (oldOccupant.getRole() != newOccupant.getRole()) {
                    changed = true;
                    onRoleChanged(resource, newOccupant.getRole());
                }
                if (oldOccupant.getStatusMode() != newOccupant.getStatusMode() || !oldOccupant.getStatusText().equals(newOccupant.getStatusText())) {
                    changed = true;
                    onStatusChanged(resource, newOccupant.getStatusMode(), newOccupant.getStatusText());
                }
                if (changed) {
                    RosterManager.onContactChanged(account, user);
                }
            }
        } else if (presence.getType() == Presence.Type.unavailable && state == RoomState.available) {
            occupants.remove(resource);
            MUCUser mucUser = MUCUser.from(presence);
            if (mucUser != null && mucUser.getStatus() != null) {
                if (mucUser.getStatus().contains(MUCUser.Status.KICKED_307)) {
                    onKick(resource, mucUser.getItem().getActor());
                } else if (mucUser.getStatus().contains(MUCUser.Status.BANNED_301)) {
                    onBan(resource, mucUser.getItem().getActor());
                } else if (mucUser.getStatus().contains(MUCUser.Status.NEW_NICKNAME_303)) {
                    Resourcepart newNick = mucUser.getItem().getNick();
                    if (newNick == null) {
                        return true;
                    }
                    onRename(resource, newNick);
                    Occupant occupant = createOccupant(newNick, presence);
                    occupants.put(newNick, occupant);
                } else if (mucUser.getStatus().contains(MUCUser.Status.REMOVED_AFFIL_CHANGE_321)) {
                    onRevoke(resource, mucUser.getItem().getActor());
                }
            } else {
                onLeave(resource);
            }
            RosterManager.onContactChanged(account, user);
        }
    }
    return true;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) UserJid(com.xabber.android.data.entity.UserJid) Date(java.util.Date) Resourcepart(org.jxmpp.jid.parts.Resourcepart) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Presence(org.jivesoftware.smack.packet.Presence) Realm(io.realm.Realm)

Example 59 with Presence

use of org.jivesoftware.smack.packet.Presence in project xabber-android by redsolution.

the class ContactVcardViewerFragment method fillResourceList.

private void fillResourceList(AccountJid account, Jid bareAddress, List<View> resourcesList) {
    final List<Presence> allPresences = RosterManager.getInstance().getPresences(account, bareAddress);
    boolean isAccount = account.getFullJid().asBareJid().equals(user.getBareJid());
    Resourcepart accountResource = null;
    if (isAccount) {
        // TODO: probably not the best way to get own resource
        AccountItem accountItem = AccountManager.getInstance().getAccount(account);
        if (accountItem != null) {
            accountResource = accountItem.getConnection().getConfiguration().getResource();
        }
    }
    PresenceManager.sortPresencesByPriority(allPresences);
    for (Presence presence : allPresences) {
        Jid fromJid = presence.getFrom();
        ClientInfo clientInfo = CapabilitiesManager.getInstance().getCachedClientInfo(fromJid);
        String client = "";
        if (clientInfo == null) {
            client = getString(R.string.please_wait);
            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;
                }
            }
        }
        int priorityValue = presence.getPriority();
        String priorityString;
        if (priorityValue == Integer.MIN_VALUE) {
            priorityString = getString(R.string.account_priority) + ": " + getString(R.string.unknown);
        } else {
            priorityString = getString(R.string.account_priority) + ": " + priorityValue;
        }
        if (!client.isEmpty()) {
            client = getString(R.string.contact_viewer_client) + ": " + client;
        }
        Resourcepart resourceOrNull = fromJid.getResourceOrNull();
        String resource = getString(R.string.account_resource) + ": " + resourceOrNull;
        final StatusMode statusMode = StatusMode.createStatusMode(presence);
        String status = presence.getStatus();
        if (TextUtils.isEmpty(status)) {
            status = getString(statusMode.getStringID());
        }
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View resourceView = inflater.inflate(R.layout.item_contact_info, xmppItems, false);
        resourceView.findViewById(R.id.contact_info_item_secondary);
        ((TextView) resourceView.findViewById(R.id.contact_info_item_secondary)).setText(client);
        ((TextView) resourceView.findViewById(R.id.contact_info_item_main)).setText(status);
        ((TextView) resourceView.findViewById(R.id.contact_info_item_secondary_second_line)).setText(resource);
        resourceView.findViewById(R.id.contact_info_item_secondary_second_line).setVisibility(View.VISIBLE);
        ((TextView) resourceView.findViewById(R.id.contact_info_item_secondary_third_line)).setText(priorityString);
        resourceView.findViewById(R.id.contact_info_item_secondary_third_line).setVisibility(View.VISIBLE);
        if (isAccount && resourceOrNull != null && resourceOrNull.equals(accountResource)) {
            TextView thisDeviceIndicatorTextView = (TextView) resourceView.findViewById(R.id.contact_info_item_secondary_forth_line);
            thisDeviceIndicatorTextView.setTextColor(ColorManager.getInstance().getAccountPainter().getAccountSendButtonColor(account));
            thisDeviceIndicatorTextView.setText(R.string.contact_viewer_this_device);
            thisDeviceIndicatorTextView.setVisibility(View.VISIBLE);
        }
        ImageView statusIcon = (ImageView) resourceView.findViewById(R.id.contact_info_right_icon);
        statusIcon.setVisibility(View.VISIBLE);
        statusIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_status));
        statusIcon.setImageLevel(statusMode.getStatusLevel());
        resourcesList.add(resourceView);
    }
}
Also used : StatusMode(com.xabber.android.data.account.StatusMode) AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid) Jid(org.jxmpp.jid.Jid) AccountItem(com.xabber.android.data.account.AccountItem) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Resourcepart(org.jxmpp.jid.parts.Resourcepart) LayoutInflater(android.view.LayoutInflater) Presence(org.jivesoftware.smack.packet.Presence) TextView(android.widget.TextView) ClientInfo(com.xabber.android.data.extension.capability.ClientInfo) ImageView(android.widget.ImageView)

Example 60 with Presence

use of org.jivesoftware.smack.packet.Presence in project xabber-android by redsolution.

the class RegularChat method onPacket.

@Override
protected boolean onPacket(UserJid bareAddress, Stanza packet, boolean isCarbons) {
    if (!super.onPacket(bareAddress, packet, isCarbons))
        return false;
    final Resourcepart resource = packet.getFrom().getResourceOrNull();
    if (packet instanceof Presence) {
        final Presence presence = (Presence) packet;
        if (this.resource != null && presence.getType() == Presence.Type.unavailable && resource != null && this.resource.equals(resource)) {
            this.resource = null;
        }
    // if (presence.getType() == Presence.Type.unavailable) {
    // OTRManager.getInstance().onContactUnAvailable(account, user);
    // }
    } else if (packet instanceof Message) {
        final Message message = (Message) packet;
        if (message.getType() == Message.Type.error)
            return true;
        MUCUser mucUser = MUCUser.from(message);
        if (mucUser != null && mucUser.getInvite() != null)
            return true;
        String text = message.getBody();
        if (text == null)
            return true;
        String thread = message.getThread();
        updateThreadId(thread);
        if (resource != null && !resource.equals(Resourcepart.EMPTY)) {
            this.resource = resource;
        }
        boolean encrypted = OTRManager.getInstance().isEncrypted(text);
        if (!isCarbons) {
            try {
                text = OTRManager.getInstance().transformReceiving(account, user, text);
            } catch (OtrException e) {
                if (e.getCause() instanceof OTRUnencryptedException) {
                    text = ((OTRUnencryptedException) e.getCause()).getText();
                    encrypted = false;
                } else {
                    LogManager.exception(this, e);
                    // Invalid message received.
                    return true;
                }
            }
        }
        // System message received.
        if (text == null || text.trim().equals(""))
            return true;
        createAndSaveNewMessage(resource, text, null, getDelayStamp(message), true, true, encrypted, isOfflineMessage(account.getFullJid().getDomain(), packet), packet.getStanzaId());
        EventBus.getDefault().post(new NewIncomingMessageEvent(account, user));
    }
    return true;
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence) OtrException(net.java.otr4j.OtrException) OTRUnencryptedException(com.xabber.android.data.extension.otr.OTRUnencryptedException) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)103 Message (org.jivesoftware.smack.packet.Message)21 Resourcepart (org.jxmpp.jid.parts.Resourcepart)12 Jid (org.jxmpp.jid.Jid)11 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)10 AccountItem (com.xabber.android.data.account.AccountItem)9 UserJid (com.xabber.android.data.entity.UserJid)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 StanzaCollector (org.jivesoftware.smack.StanzaCollector)7 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 UserPresence (jetbrains.communicator.core.users.UserPresence)6 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)6 BareJid (org.jxmpp.jid.BareJid)6 ClientInfo (com.xabber.android.data.extension.capability.ClientInfo)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)5 Test (org.junit.Test)5 EntityFullJid (org.jxmpp.jid.EntityFullJid)5