Search in sources :

Example 21 with AccountItem

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

the class ConnectionThread method connectAndLogin.

@SuppressWarnings("WeakerAccess")
void connectAndLogin() {
    AndroidLoggingHandler.reset(new AndroidLoggingHandler());
    java.util.logging.Logger.getLogger(XMPPTCPConnection.class.getName()).setLevel(Level.FINEST);
    java.util.logging.Logger.getLogger(AbstractDNSClient.class.getName()).setLevel(Level.FINEST);
    java.util.logging.Logger.getLogger(AbstractXMPPConnection.class.getName()).setLevel(Level.FINEST);
    java.util.logging.Logger.getLogger(DNSUtil.class.getName()).setLevel(Level.FINEST);
    if (connection.getConfiguration().getPassword().isEmpty()) {
        AccountErrorEvent accountErrorEvent = new AccountErrorEvent(connectionItem.getAccount(), AccountErrorEvent.Type.PASS_REQUIRED, "");
        // com.xabber.android.data.account.AccountManager.getInstance().addAccountError(accountErrorEvent);
        com.xabber.android.data.account.AccountManager.getInstance().setEnabled(connectionItem.getAccount(), false);
        EventBus.getDefault().postSticky(accountErrorEvent);
        return;
    }
    // switch (SettingsManager.connectionDnsResolver()) {
    // case dnsJavaResolver:
    // LogManager.i(this, "Use DNS Java resolver");
    // ExtDNSJavaResolver.setup();
    // break;
    // case miniDnsResolver:
    // LogManager.i(this, "Use Mini DNS resolver");
    // MiniDnsResolver.setup();
    // break;
    // }
    LogManager.i(this, "Use DNS Java resolver");
    ExtDNSJavaResolver.setup();
    ProviderManager.addExtensionProvider(DataForm.ELEMENT, DataForm.NAMESPACE, new CustomDataProvider());
    ProviderManager.addExtensionProvider(ForwardComment.ELEMENT, ForwardComment.NAMESPACE, new ForwardCommentProvider());
    ProviderManager.addExtensionProvider(ReferenceElement.ELEMENT, ReferenceElement.NAMESPACE, new ReferencesProvider());
    ProviderManager.addIQProvider(XTokenIQ.ELEMENT, XTokenIQ.NAMESPACE, new XTokenProvider());
    ProviderManager.addIQProvider(SessionsIQ.ELEMENT, SessionsIQ.NAMESPACE, new SessionsProvider());
    try {
        LogManager.i(this, "Trying to connect and login...");
        if (!connection.isConnected()) {
            connectionItem.updateState(ConnectionState.connecting);
            connection.connect();
        } else {
            LogManager.i(this, "Already connected");
        }
        if (!connection.isAuthenticated()) {
            ProviderManager.addIQProvider(HttpConfirmIq.ELEMENT, HttpConfirmIq.NAMESPACE, new HttpConfirmIqProvider());
            connection.login();
        } else {
            LogManager.i(this, "Already authenticated");
        }
    } catch (SASLErrorException e) {
        LogManager.exception(this, e);
        if (e.getMechanism().equals(SASLXTOKENMechanism.NAME)) {
            LogManager.d(this, "Authorization error with x-token: " + e.toString());
            AccountManager.getInstance().removeXToken(connectionItem.getAccount());
        }
        AccountErrorEvent accountErrorEvent = new AccountErrorEvent(connectionItem.getAccount(), AccountErrorEvent.Type.AUTHORIZATION, e.getMessage());
        // com.xabber.android.data.account.AccountManager.getInstance().addAccountError(accountErrorEvent);
        com.xabber.android.data.account.AccountManager.getInstance().setEnabled(connectionItem.getAccount(), false);
        EventBus.getDefault().postSticky(accountErrorEvent);
    // catching RuntimeExceptions seems to be strange, but we got a lot of error coming from
    // Smack or mini DSN client inside of Smack.
    } catch (XMPPException | SmackException | IOException | RuntimeException e) {
        LogManager.exception(this, e);
        if (!((AccountItem) connectionItem).isSuccessfulConnectionHappened()) {
            LogManager.i(this, "There was no successful connection, disabling account");
            AccountErrorEvent accountErrorEvent = new AccountErrorEvent(connectionItem.getAccount(), AccountErrorEvent.Type.CONNECTION, Log.getStackTraceString(e));
            com.xabber.android.data.account.AccountManager.getInstance().addAccountError(accountErrorEvent);
            com.xabber.android.data.account.AccountManager.getInstance().setEnabled(connectionItem.getAccount(), false);
            EventBus.getDefault().postSticky(accountErrorEvent);
        }
    } catch (InterruptedException e) {
        LogManager.exception(this, e);
    }
    LogManager.i(this, "Connection thread finished");
}
Also used : SessionsProvider(com.xabber.android.data.extension.xtoken.SessionsProvider) AndroidLoggingHandler(com.xabber.android.data.log.AndroidLoggingHandler) AccountItem(com.xabber.android.data.account.AccountItem) ReferencesProvider(com.xabber.android.data.extension.references.ReferencesProvider) SmackException(org.jivesoftware.smack.SmackException) HttpConfirmIqProvider(com.xabber.android.data.xaccount.HttpConfirmIqProvider) XTokenProvider(com.xabber.android.data.extension.xtoken.XTokenProvider) IOException(java.io.IOException) AccountErrorEvent(com.xabber.android.data.account.AccountErrorEvent) CustomDataProvider(com.xabber.android.data.extension.httpfileupload.CustomDataProvider) ForwardCommentProvider(com.xabber.android.data.extension.forward.ForwardCommentProvider) SASLErrorException(org.jivesoftware.smack.sasl.SASLErrorException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 22 with AccountItem

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

the class NextMamManager method onChatOpen.

public void onChatOpen(final AbstractChat chat) {
    final AccountItem accountItem = AccountManager.getInstance().getAccount(chat.getAccount());
    if (accountItem == null || accountItem.getLoadHistorySettings() == LoadHistorySettings.none || !isSupported(accountItem.getAccount()))
        return;
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
            // if history is empty - load last message
            MessageItem firstMessage = getFirstMessage(chat, realm);
            if (firstMessage == null)
                loadLastMessage(realm, accountItem, chat);
            synchronized (lock) {
                if (isRequested)
                    return;
                else
                    isRequested = true;
            }
            // load prev page if history is not enough
            if (historyIsNotEnough(realm, chat) && !chat.historyIsFull()) {
                EventBus.getDefault().post(new LastHistoryLoadStartedEvent(chat));
                loadNextHistory(realm, accountItem, chat);
                EventBus.getDefault().post(new LastHistoryLoadFinishedEvent(chat));
            }
            // load missed messages if need
            List<MessageItem> messages = findMissedMessages(realm, chat);
            if (messages != null && !messages.isEmpty() && accountItem != null) {
                for (MessageItem message : messages) {
                    loadMissedMessages(realm, accountItem, chat, message);
                }
            }
            synchronized (lock) {
                isRequested = false;
            }
            realm.close();
        }
    });
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) AccountItem(com.xabber.android.data.account.AccountItem) List(java.util.List) RealmList(io.realm.RealmList) ArrayList(java.util.ArrayList) Realm(io.realm.Realm)

Example 23 with AccountItem

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

the class NextMamManager method parseAndSaveMessageFromMamResult.

/**
 * PARSING
 */
private void parseAndSaveMessageFromMamResult(Realm realm, AccountJid account, Forwarded forwarded) {
    Stanza stanza = forwarded.getForwardedStanza();
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    Jid user = stanza.getFrom().asBareJid();
    if (user.equals(account.getFullJid().asBareJid()))
        user = stanza.getTo().asBareJid();
    try {
        AbstractChat chat = MessageManager.getInstance().getOrCreateChat(account, UserJid.from(user));
        MessageItem messageItem = parseMessage(accountItem, account, chat.getUser(), forwarded, null);
        if (messageItem != null) {
            saveOrUpdateMessages(realm, Collections.singletonList(messageItem), true);
            updateLastMessageId(chat, realm);
        }
    } catch (UserJid.UserJidCreateException e) {
        LogManager.d(LOG_TAG, e.toString());
    }
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) 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) AbstractChat(com.xabber.android.data.message.AbstractChat) Stanza(org.jivesoftware.smack.packet.Stanza) UserJid(com.xabber.android.data.entity.UserJid)

Example 24 with AccountItem

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

the class ChatStateManager method onDisconnect.

@Override
public void onDisconnect(ConnectionItem connection) {
    if (!(connection instanceof AccountItem))
        return;
    AccountJid account = ((AccountItem) connection).getAccount();
    chatStates.clear(account.toString());
    for (Map<Resourcepart, Runnable> map : stateCleaners.getNested(account.toString()).values()) {
        for (Runnable runnable : map.values()) {
            handler.removeCallbacks(runnable);
        }
    }
    stateCleaners.clear(account.toString());
    supports.clear(account.toString());
    sent.clear(account.toString());
    for (PendingIntent pendingIntent : pauseIntents.getNested(account.toString()).values()) {
        alarmManager.cancel(pendingIntent);
    }
    pauseIntents.clear(account.toString());
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) PendingIntent(android.app.PendingIntent) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 25 with AccountItem

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

the class ChatStateManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (stanza.getFrom() == null) {
        return;
    }
    final Resourcepart resource = stanza.getFrom().getResourceOrNull();
    if (resource == null) {
        return;
    }
    final AccountJid account = ((AccountItem) connection).getAccount();
    final UserJid bareUserJid;
    try {
        bareUserJid = UserJid.from(stanza.getFrom()).getBareUserJid();
    } catch (UserJid.UserJidCreateException e) {
        return;
    }
    if (stanza instanceof Presence) {
        Presence presence = (Presence) stanza;
        if (presence.getType() != Type.unavailable) {
            return;
        }
        chatStates.remove(account.toString(), bareUserJid.toString(), resource);
        removeCallback(account, bareUserJid.getBareJid(), resource);
        supports.remove(account.toString(), bareUserJid.toString(), resource);
    } else if (stanza instanceof Message) {
        boolean support = false;
        for (ExtensionElement extension : stanza.getExtensions()) if (extension instanceof ChatStateExtension) {
            removeCallback(account, bareUserJid.getBareJid(), resource);
            ChatState chatState = ((ChatStateExtension) extension).getChatState();
            chatStates.put(account.toString(), bareUserJid.toString(), resource, chatState);
            if (chatState != ChatState.active) {
                Runnable runnable = new Runnable() {

                    @Override
                    public void run() {
                        if (this != stateCleaners.get(account.toString(), bareUserJid.toString(), resource)) {
                            return;
                        }
                        chatStates.remove(account.toString(), bareUserJid.toString(), resource);
                        removeCallback(account, bareUserJid.getBareJid(), resource);
                        RosterManager.onChatStateChanged(account, bareUserJid);
                    }
                };
                handler.postDelayed(runnable, REMOVE_STATE_DELAY);
                stateCleaners.put(account.toString(), bareUserJid.toString(), resource, runnable);
            }
            RosterManager.onChatStateChanged(account, bareUserJid);
            support = true;
            break;
        }
        Message message = (Message) stanza;
        if (message.getType() != Message.Type.chat && message.getType() != Message.Type.groupchat) {
            return;
        }
        if (support) {
            supports.put(account.toString(), bareUserJid.toString(), resource, true);
        } else if (supports.get(account.toString(), bareUserJid.toString(), resource) == null) {
            // Disable only if there no information about support.
            supports.put(account.toString(), bareUserJid.toString(), resource, false);
        }
    }
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState) Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension) AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) UserJid(com.xabber.android.data.entity.UserJid) Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

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