Search in sources :

Example 6 with MultiUserChat

use of org.jivesoftware.smackx.muc.MultiUserChat in project xabber-android by redsolution.

the class MUCManager method leaveRoom.

public void leaveRoom(String account, String room) {
    final MultiUserChat multiUserChat;
    RoomChat roomChat = getRoomChat(account, room);
    if (roomChat == null) {
        return;
    }
    multiUserChat = roomChat.getMultiUserChat();
    roomChat.setState(RoomState.unavailable);
    roomChat.setRequested(false);
    roomChat.newAction(roomChat.getNickname(), null, ChatAction.leave);
    requestToWriteRoom(account, room, roomChat.getNickname(), roomChat.getPassword(), false);
    if (multiUserChat != null) {
        Thread thread = new Thread("Leave to room " + room + " from " + account) {

            @Override
            public void run() {
                try {
                    multiUserChat.leave();
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.setDaemon(true);
        thread.start();
    }
    RosterManager.getInstance().onContactChanged(account, room);
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) SmackException(org.jivesoftware.smack.SmackException) ConnectionThread(com.xabber.android.data.connection.ConnectionThread)

Example 7 with MultiUserChat

use of org.jivesoftware.smackx.muc.MultiUserChat 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

MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)7 SmackException (org.jivesoftware.smack.SmackException)3 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)3 XMPPException (org.jivesoftware.smack.XMPPException)3 ConnectionThread (com.xabber.android.data.connection.ConnectionThread)2 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 DiscussionHistory (org.jivesoftware.smackx.muc.DiscussionHistory)2 NetworkException (com.xabber.android.data.NetworkException)1 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)1 AndFilter (org.jivesoftware.smack.filter.AndFilter)1 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)1 OrFilter (org.jivesoftware.smack.filter.OrFilter)1 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)1 Presence (org.jivesoftware.smack.packet.Presence)1 XMPPError (org.jivesoftware.smack.packet.XMPPError)1 BookmarkedConference (org.jivesoftware.smackx.bookmarks.BookmarkedConference)1 MucCreateConfigFormHandle (org.jivesoftware.smackx.muc.MultiUserChat.MucCreateConfigFormHandle)1 NotAMucServiceException (org.jivesoftware.smackx.muc.MultiUserChatException.NotAMucServiceException)1 Resourcepart (org.jxmpp.jid.parts.Resourcepart)1