Search in sources :

Example 61 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class TimeManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, final String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem))
        return;
    String account = ((AccountItem) connection).getAccount();
    if (!(packet instanceof Time))
        return;
    Time time = (Time) packet;
    if (time.getType() == Type.get) {
        Time result = new Time();
        result.setType(Type.result);
        result.setPacketID(time.getPacketID());
        result.setFrom(time.getTo());
        result.setTo(time.getFrom());
        Calendar calendar = Calendar.getInstance();
        result.setTzo((calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / 60000);
        result.setUtc(calendar.getTime());
        try {
            ConnectionManager.getInstance().sendStanza(account, result);
        } catch (NetworkException e) {
        }
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) Calendar(java.util.Calendar) Time(com.xabber.xmpp.time.Time) NetworkException(com.xabber.android.data.NetworkException)

Example 62 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class VCardManager method requestVCard.

private void requestVCard(final String account, final String srcUser) {
    final String userBareJid = srcUser;
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    ConnectionThread connectionThread = accountItem.getConnectionThread();
    if (!accountItem.getFactualStatusMode().isOnline() || connectionThread == null) {
        onVCardFailed(account, userBareJid);
        return;
    }
    final org.jivesoftware.smackx.vcardtemp.VCardManager vCardManager = org.jivesoftware.smackx.vcardtemp.VCardManager.getInstanceFor(connectionThread.getXMPPConnection());
    final Thread thread = new Thread("Get vCard user " + userBareJid + " for account " + account) {

        @Override
        public void run() {
            VCard vCard = null;
            vCardRequests.add(userBareJid);
            try {
                vCard = vCardManager.loadVCard(userBareJid);
            } catch (SmackException.NoResponseException | SmackException.NotConnectedException e) {
                LogManager.w(this, "Error getting vCard: " + e.getMessage());
            } catch (XMPPException.XMPPErrorException e) {
                LogManager.w(this, "XMPP error getting vCard: " + e.getMessage() + e.getXMPPError());
                if (e.getXMPPError().getCondition() == XMPPError.Condition.item_not_found) {
                    vCard = new VCard();
                }
            } catch (ClassCastException e) {
                // http://stackoverflow.com/questions/31498721/error-loading-vcard-information-using-smack-emptyresultiq-cannot-be-cast-to-or
                LogManager.w(this, "ClassCastException: " + e.getMessage());
                vCard = new VCard();
            }
            vCardRequests.remove(userBareJid);
            final VCard finalVCard = vCard;
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (finalVCard == null) {
                        onVCardFailed(account, userBareJid);
                    } else {
                        onVCardReceived(account, userBareJid, finalVCard);
                    }
                }
            });
        }
    };
    thread.start();
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) XMPPException(org.jivesoftware.smack.XMPPException) VCard(org.jivesoftware.smackx.vcardtemp.packet.VCard)

Example 63 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class MUCManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem)) {
        return;
    }
    String account = ((AccountItem) connection).getAccount();
    if (bareAddress == null || !(packet instanceof Message)) {
        return;
    }
    Message message = (Message) packet;
    if (message.getType() != Message.Type.normal && message.getType() != Message.Type.chat) {
        return;
    }
    MUCUser mucUser = MUC.getMUCUserExtension(packet);
    if (mucUser == null || mucUser.getInvite() == null) {
        return;
    }
    RoomChat roomChat = getRoomChat(account, bareAddress);
    if (roomChat == null || !roomChat.getState().inUse()) {
        String inviter = mucUser.getInvite().getFrom();
        if (inviter == null) {
            inviter = bareAddress;
        }
        inviteProvider.add(new RoomInvite(account, bareAddress, inviter, mucUser.getInvite().getReason(), mucUser.getPassword()), true);
    }
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) AccountItem(com.xabber.android.data.account.AccountItem)

Example 64 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class ReceiptManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem))
        return;
    String account = ((AccountItem) connection).getAccount();
    final String user = packet.getFrom();
    if (user == null)
        return;
    if (!(packet instanceof Message))
        return;
    final Message message = (Message) packet;
    if (message.getType() == Message.Type.error) {
        final MessageItem messageItem = sent.remove(account, message.getPacketID());
        if (messageItem != null && !messageItem.isError()) {
            messageItem.markAsError();
            Application.getInstance().runInBackground(new Runnable() {

                @Override
                public void run() {
                    if (messageItem.getId() != null)
                        MessageTable.getInstance().markAsError(messageItem.getId());
                }
            });
            MessageManager.getInstance().onChatChanged(messageItem.getChat().getAccount(), messageItem.getChat().getUser(), false);
        }
    } else {
        // TODO setDefaultAutoReceiptMode should be used
        for (ExtensionElement packetExtension : message.getExtensions()) if (packetExtension instanceof DeliveryReceiptRequest) {
            String id = message.getPacketID();
            if (id == null)
                continue;
            Message receipt = new Message(user);
            receipt.addExtension(new DeliveryReceipt(id));
            // the key problem is Thread - smack does not keep it in auto reply
            receipt.setThread(message.getThread());
            try {
                ConnectionManager.getInstance().sendStanza(account, receipt);
            } catch (NetworkException e) {
                LogManager.exception(this, e);
            }
        }
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) AccountItem(com.xabber.android.data.account.AccountItem) DeliveryReceiptRequest(org.jivesoftware.smackx.receipts.DeliveryReceiptRequest) DeliveryReceipt(org.jivesoftware.smackx.receipts.DeliveryReceipt) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) NetworkException(com.xabber.android.data.NetworkException)

Example 65 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class RosterManager method getRoster.

@Nullable
private Roster getRoster(String account) {
    final AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        return null;
    }
    final ConnectionThread connectionThread = accountItem.getConnectionThread();
    if (connectionThread == null) {
        return null;
    }
    final AbstractXMPPConnection xmppConnection = connectionThread.getXMPPConnection();
    if (xmppConnection == null) {
        return null;
    }
    return Roster.getInstanceFor(xmppConnection);
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection) Nullable(android.support.annotation.Nullable)

Aggregations

AccountItem (com.xabber.android.data.account.AccountItem)96 AccountJid (com.xabber.android.data.entity.AccountJid)24 UserJid (com.xabber.android.data.entity.UserJid)14 View (android.view.View)12 NetworkException (com.xabber.android.data.NetworkException)11 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)11 Message (org.jivesoftware.smack.packet.Message)11 Presence (org.jivesoftware.smack.packet.Presence)10 TextView (android.widget.TextView)9 XMPPException (org.jivesoftware.smack.XMPPException)9 Jid (org.jxmpp.jid.Jid)9 ImageView (android.widget.ImageView)8 StatusMode (com.xabber.android.data.account.StatusMode)6 ConnectionState (com.xabber.android.data.connection.ConnectionState)5 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)5 Realm (io.realm.Realm)5 ColorDrawable (android.graphics.drawable.ColorDrawable)4 AccountManager (com.xabber.android.data.account.AccountManager)4