Search in sources :

Example 31 with UserJid

use of com.xabber.android.data.entity.UserJid in project xabber-android by redsolution.

the class MessageManager method setVisibleChat.

/**
 * Sets currently visible chat.
 */
public void setVisibleChat(BaseEntity visibleChat) {
    AbstractChat chat = getChat(visibleChat.getAccount(), visibleChat.getUser());
    if (chat == null) {
        chat = createChat(visibleChat.getAccount(), visibleChat.getUser());
    } else {
        final AccountJid account = chat.getAccount();
        final UserJid user = chat.getUser();
        MessageDatabaseManager.getInstance().getRealmUiThread().executeTransactionAsync(new Realm.Transaction() {

            @Override
            public void execute(Realm realm) {
                RealmResults<MessageItem> unreadMessages = realm.where(MessageItem.class).equalTo(MessageItem.Fields.ACCOUNT, account.toString()).equalTo(MessageItem.Fields.USER, user.toString()).equalTo(MessageItem.Fields.READ, false).findAll();
                List<MessageItem> unreadMessagesList = new ArrayList<>(unreadMessages);
                for (MessageItem messageItem : unreadMessagesList) {
                    messageItem.setRead(true);
                }
            }
        });
    }
    this.visibleChat = chat;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid) List(java.util.List) ArrayList(java.util.ArrayList) Realm(io.realm.Realm) RealmResults(io.realm.RealmResults)

Example 32 with UserJid

use of com.xabber.android.data.entity.UserJid in project xabber-android by redsolution.

the class NotificationManager method onMessageNotification.

public void onMessageNotification(MessageItem messageItem) {
    MessageNotification messageNotification = getMessageNotification(messageItem.getAccount(), messageItem.getUser());
    if (messageNotification == null) {
        messageNotification = new MessageNotification(messageItem.getAccount(), messageItem.getUser(), null, null, 0);
    } else {
        messageNotifications.remove(messageNotification);
    }
    messageNotification.addMessage(messageItem.getText());
    messageNotifications.add(messageNotification);
    final AccountJid account = messageNotification.getAccount();
    final UserJid user = messageNotification.getUser();
    final String text = messageNotification.getText();
    final Date timestamp = messageNotification.getTimestamp();
    final int count = messageNotification.getCount();
    Application.getInstance().runInBackgroundUserRequest(new Runnable() {

        @Override
        public void run() {
            NotificationTable.getInstance().write(account.toString(), user.toString(), text, timestamp, count);
        }
    });
    updateMessageNotification(messageItem);
}
Also used : AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid) Date(java.util.Date) SuppressLint(android.annotation.SuppressLint)

Example 33 with UserJid

use of com.xabber.android.data.entity.UserJid in project xabber-android by redsolution.

the class PresenceManager method requestSubscription.

/**
 * Requests subscription to the contact.
 *
 * @throws NetworkException
 */
public void requestSubscription(AccountJid account, UserJid user) throws NetworkException {
    Presence packet = new Presence(Presence.Type.subscribe);
    packet.setTo(user.getJid());
    StanzaSender.sendStanza(account, packet);
    Set<UserJid> set = requestedSubscriptions.get(account);
    if (set == null) {
        set = new HashSet<>();
        requestedSubscriptions.put(account, set);
    }
    set.add(user);
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) UserJid(com.xabber.android.data.entity.UserJid)

Example 34 with UserJid

use of com.xabber.android.data.entity.UserJid in project xabber-android by redsolution.

the class NextMamManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza packet) {
    if (packet instanceof Message) {
        for (ExtensionElement packetExtension : packet.getExtensions()) {
            if (packetExtension instanceof MamElements.MamResultExtension) {
                MamElements.MamResultExtension resultExtension = (MamElements.MamResultExtension) packetExtension;
                String resultID = resultExtension.getQueryId();
                if (waitingRequests.containsKey(resultID)) {
                    Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
                    parseAndSaveMessageFromMamResult(realm, connection.getAccount(), resultExtension.getForwarded());
                    UserJid userJid = waitingRequests.get(resultID);
                    AbstractChat chat = MessageManager.getInstance().getChat(connection.getAccount(), userJid);
                    if (chat != null && !chat.isHistoryRequestedAtStart())
                        chat.setHistoryRequestedAtStart(true);
                    waitingRequests.remove(resultID);
                }
            }
        }
    }
    if (packet instanceof MamFinIQ) {
        MamFinIQ finIQ = (MamFinIQ) packet;
        if (finIQ.isComplete() && waitingRequests.containsKey(finIQ.getQueryId())) {
            UserJid userJid = waitingRequests.get(finIQ.getQueryId());
            AbstractChat chat = MessageManager.getInstance().getChat(connection.getAccount(), userJid);
            if (chat != null) {
                if (!chat.isHistoryRequestedAtStart())
                    chat.setHistoryRequestedAtStart(true);
            }
        }
    }
}
Also used : PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Message(org.jivesoftware.smack.packet.Message) AbstractChat(com.xabber.android.data.message.AbstractChat) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) MamFinIQ(org.jivesoftware.smackx.mam.element.MamFinIQ) UserJid(com.xabber.android.data.entity.UserJid) MamElements(org.jivesoftware.smackx.mam.element.MamElements) Realm(io.realm.Realm)

Example 35 with UserJid

use of com.xabber.android.data.entity.UserJid 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, true);
            }
            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;
        }
        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, true);
        } else {
            boolean notify = true;
            Date delay = getDelayStamp(message);
            if (delay != null) {
                notify = false;
            }
            // forward comment (to support previous forwarded xep)
            String forwardComment = ForwardManager.parseForwardComment(stanza);
            if (forwardComment != null)
                text = forwardComment;
            // modify body with references
            Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
            text = bodies.first;
            String markupText = bodies.second;
            String originalFrom = stanza.getFrom().toString();
            String messageUId = getMessageIdIfInHistory(getStanzaId(message), text);
            if (messageUId != null) {
                if (isSelf(resource)) {
                    markMessageAsDelivered(messageUId, originalFrom);
                }
                return true;
            }
            if (isSelf(resource)) {
                notify = false;
            }
            updateThreadId(message.getThread());
            RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(stanza);
            String uid = UUID.randomUUID().toString();
            RealmList<ForwardId> forwardIds = parseForwardedMessage(true, stanza, uid);
            String originalStanza = stanza.toXML().toString();
            // create message with file-attachments
            if (attachments.size() > 0)
                createAndSaveFileMessage(true, uid, resource, text, markupText, null, null, delay, true, notify, false, false, getStanzaId(message), attachments, originalStanza, null, originalFrom, true, false, null);
            else
                // create message without attachments
                createAndSaveNewMessage(true, uid, resource, text, markupText, null, null, delay, true, notify, false, false, getStanzaId(message), originalStanza, null, originalFrom, forwardIds, true, false, null);
            EventBus.getDefault().post(new NewIncomingMessageEvent(account, user));
        }
    } 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 : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) NewIncomingMessageEvent(com.xabber.android.data.message.NewIncomingMessageEvent) Message(org.jivesoftware.smack.packet.Message) UserJid(com.xabber.android.data.entity.UserJid) Attachment(com.xabber.android.data.database.messagerealm.Attachment) Date(java.util.Date) Resourcepart(org.jxmpp.jid.parts.Resourcepart) Presence(org.jivesoftware.smack.packet.Presence) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId)

Aggregations

UserJid (com.xabber.android.data.entity.UserJid)67 AccountJid (com.xabber.android.data.entity.AccountJid)43 AbstractChat (com.xabber.android.data.message.AbstractChat)15 ArrayList (java.util.ArrayList)15 Intent (android.content.Intent)9 Jid (org.jxmpp.jid.Jid)9 Presence (org.jivesoftware.smack.packet.Presence)8 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)8 AccountItem (com.xabber.android.data.account.AccountItem)7 AbstractContact (com.xabber.android.data.roster.AbstractContact)6 Message (org.jivesoftware.smack.packet.Message)6 Resourcepart (org.jxmpp.jid.parts.Resourcepart)6 NetworkException (com.xabber.android.data.NetworkException)5 IFlexible (eu.davidea.flexibleadapter.items.IFlexible)5 SmackException (org.jivesoftware.smack.SmackException)5 Uri (android.net.Uri)4 ContactVO (com.xabber.android.presentation.ui.contactlist.viewobjects.ContactVO)4 DomainBareJid (org.jxmpp.jid.DomainBareJid)4 TextView (android.widget.TextView)3 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)3