Search in sources :

Example 1 with ConnectionThread

use of com.xabber.android.data.connection.ConnectionThread in project xabber-android by redsolution.

the class AttentionManager method onSettingsChanged.

public void onSettingsChanged() {
    synchronized (enabledLock) {
        for (String account : AccountManager.getInstance().getAccounts()) {
            ConnectionThread connectionThread = AccountManager.getInstance().getAccount(account).getConnectionThread();
            if (connectionThread == null)
                continue;
            XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
            if (xmppConnection == null)
                continue;
            ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(xmppConnection);
            if (manager == null)
                continue;
            boolean contains = false;
            for (String feature : manager.getFeatures()) {
                if (Attention.NAMESPACE.equals(feature)) {
                    contains = true;
                }
            }
            if (SettingsManager.chatsAttention() == contains)
                continue;
            if (SettingsManager.chatsAttention())
                manager.addFeature(Attention.NAMESPACE);
            else
                manager.removeFeature(Attention.NAMESPACE);
        }
        AccountManager.getInstance().resendPresence();
    }
}
Also used : ConnectionThread(com.xabber.android.data.connection.ConnectionThread) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager)

Example 2 with ConnectionThread

use of com.xabber.android.data.connection.ConnectionThread in project xabber-android by redsolution.

the class ClientStateManager method sendClientState.

protected static void sendClientState(PlainStreamElement element) {
    AccountManager accountManager = AccountManager.getInstance();
    for (String accountName : accountManager.getAccounts()) {
        AccountItem account = accountManager.getAccount(accountName);
        if (account == null) {
            continue;
        }
        ConnectionThread connectionThread = account.getConnectionThread();
        if (connectionThread == null) {
            continue;
        }
        AbstractXMPPConnection xmppConnection = connectionThread.getXMPPConnection();
        if (xmppConnection == null) {
            continue;
        }
        if (xmppConnection.hasFeature("csi", ClientStateIndication.NAMESPACE))
            try {
                xmppConnection.send(element);
            } catch (SmackException.NotConnectedException e) {
            // not connected
            }
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) AccountManager(com.xabber.android.data.account.AccountManager) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection)

Example 3 with ConnectionThread

use of com.xabber.android.data.connection.ConnectionThread in project xabber-android by redsolution.

the class VCardManager method saveVCard.

public void saveVCard(final String account, final VCard vCard) {
    ConnectionThread connectionThread = AccountManager.getInstance().getAccount(account).getConnectionThread();
    if (connectionThread == null) {
        onVCardSaveFailed(account);
        return;
    }
    final AbstractXMPPConnection xmppConnection = connectionThread.getXMPPConnection();
    final org.jivesoftware.smackx.vcardtemp.VCardManager vCardManager = org.jivesoftware.smackx.vcardtemp.VCardManager.getInstanceFor(xmppConnection);
    final Thread thread = new Thread("Save vCard for account " + account) {

        @Override
        public void run() {
            boolean isSuccess = true;
            xmppConnection.setPacketReplyTimeout(120000);
            vCardSaveRequests.add(account);
            try {
                vCardManager.saveVCard(vCard);
                String avatarHash = vCard.getAvatarHash();
                if (avatarHash == null) {
                    avatarHash = AvatarManager.EMPTY_HASH;
                }
                PresenceManager.getInstance().sendVCardUpdatePresence(account, avatarHash);
            } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | NetworkException e) {
                LogManager.w(this, "Error saving vCard: " + e.getMessage());
                isSuccess = false;
            }
            vCardSaveRequests.remove(account);
            xmppConnection.setPacketReplyTimeout(ConnectionManager.PACKET_REPLY_TIMEOUT);
            final boolean finalIsSuccess = isSuccess;
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (finalIsSuccess) {
                        onVCardSaveSuccess(account);
                    } else {
                        onVCardSaveFailed(account);
                    }
                }
            });
        }
    };
    thread.start();
}
Also used : ConnectionThread(com.xabber.android.data.connection.ConnectionThread) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) NetworkException(com.xabber.android.data.NetworkException) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection)

Example 4 with ConnectionThread

use of com.xabber.android.data.connection.ConnectionThread 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 5 with ConnectionThread

use of com.xabber.android.data.connection.ConnectionThread in project xabber-android by redsolution.

the class MUCManager method joinRoom.

/**
     * Requests to join to the room.
     *
     * @param requested Whether user request to join the room.
     */
public void joinRoom(final String account, final String room, boolean requested) {
    final XMPPConnection xmppConnection;
    final RoomChat roomChat;
    final String nickname;
    final String password;
    final Thread thread;
    roomChat = getRoomChat(account, room);
    if (roomChat == null) {
        Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
        return;
    }
    RoomState state = roomChat.getState();
    if (state == RoomState.available || state == RoomState.occupation) {
        Application.getInstance().onError(R.string.ALREADY_JOINED);
        return;
    }
    if (state == RoomState.creating || state == RoomState.joining) {
        Application.getInstance().onError(R.string.ALREADY_IN_PROGRESS);
        return;
    }
    nickname = roomChat.getNickname();
    password = roomChat.getPassword();
    requestToWriteRoom(account, room, nickname, password, true);
    ConnectionThread connectionThread = AccountManager.getInstance().getAccount(account).getConnectionThread();
    if (connectionThread == null) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    xmppConnection = connectionThread.getXMPPConnection();
    if (xmppConnection == null) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    final MultiUserChat multiUserChat;
    try {
        multiUserChat = MultiUserChatManager.getInstanceFor(xmppConnection).getMultiUserChat(room);
    } catch (IllegalStateException e) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    roomChat.setState(RoomState.joining);
    roomChat.setMultiUserChat(multiUserChat);
    roomChat.setRequested(requested);
    thread = new Thread("Join to room " + room + " from " + account) {

        @Override
        public void run() {
            try {
                if (roomChat.getMultiUserChat() != multiUserChat) {
                    return;
                }
                multiUserChat.join(nickname, password);
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        if (roomChat.getState() == RoomState.joining) {
                            roomChat.setState(RoomState.occupation);
                        }
                        removeAuthorizationError(account, room);
                        RosterManager.getInstance().onContactChanged(account, room);
                    }
                });
                return;
            } catch (final XMPPException.XMPPErrorException e) {
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        roomChat.setState(RoomState.error);
                        addAuthorizationError(account, room);
                        XMPPError xmppError = e.getXMPPError();
                        if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.conflict) {
                            Application.getInstance().onError(R.string.NICK_ALREADY_USED);
                        } else if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.not_authorized) {
                            Application.getInstance().onError(R.string.AUTHENTICATION_FAILED);
                        } else {
                            Application.getInstance().onError(R.string.NOT_CONNECTED);
                        }
                        RosterManager.getInstance().onContactChanged(account, room);
                    }
                });
                return;
            } catch (IllegalStateException e) {
            } catch (Exception e) {
                LogManager.exception(this, e);
            }
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (roomChat.getMultiUserChat() != multiUserChat) {
                        return;
                    }
                    roomChat.setState(RoomState.waiting);
                    Application.getInstance().onError(R.string.NOT_CONNECTED);
                    RosterManager.getInstance().onContactChanged(account, room);
                }
            });
        }
    };
    thread.setDaemon(true);
    thread.start();
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPError(org.jivesoftware.smack.packet.XMPPError) SmackException(org.jivesoftware.smack.SmackException) NetworkException(com.xabber.android.data.NetworkException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) ConnectionThread(com.xabber.android.data.connection.ConnectionThread)

Aggregations

ConnectionThread (com.xabber.android.data.connection.ConnectionThread)6 AccountItem (com.xabber.android.data.account.AccountItem)3 AbstractXMPPConnection (org.jivesoftware.smack.AbstractXMPPConnection)3 NetworkException (com.xabber.android.data.NetworkException)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 XMPPException (org.jivesoftware.smack.XMPPException)2 Nullable (android.support.annotation.Nullable)1 AccountManager (com.xabber.android.data.account.AccountManager)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPError (org.jivesoftware.smack.packet.XMPPError)1 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)1 MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)1 VCard (org.jivesoftware.smackx.vcardtemp.packet.VCard)1