Search in sources :

Example 16 with AccountItem

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

the class ConnectionManager method sendStanza.

/**
     * Send stanza to authenticated connection.
     *
     * @param account
     * @param stanza
     */
public void sendStanza(String account, Stanza stanza) throws NetworkException {
    ConnectionThread connectionThread = null;
    for (ConnectionThread check : managedConnections) {
        if (check.getConnectionItem() instanceof AccountItem && ((AccountItem) check.getConnectionItem()).getAccount().equals(account)) {
            connectionThread = check;
            break;
        }
    }
    if (connectionThread == null || !connectionThread.getConnectionItem().getState().isConnected()) {
        throw new NetworkException(R.string.NOT_CONNECTED);
    }
    XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
    try {
        xmppConnection.sendStanza(stanza);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
        throw new NetworkException(R.string.XMPP_EXCEPTION);
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) SmackException(org.jivesoftware.smack.SmackException) XMPPConnection(org.jivesoftware.smack.XMPPConnection) NetworkException(com.xabber.android.data.NetworkException)

Example 17 with AccountItem

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

the class MamManager method getLastHistory.

@SuppressWarnings("WeakerAccess")
void getLastHistory(AbstractChat chat, boolean ignoreTime) {
    if (chat == null) {
        return;
    }
    if (!ignoreTime) {
        if (isTimeToRefreshHistory(chat)) {
            return;
        }
    }
    final AccountItem accountItem = AccountManager.getInstance().getAccount(chat.getAccount());
    if (accountItem == null) {
        return;
    }
    XMPPTCPConnection connection = accountItem.getConnection();
    if (!connection.isAuthenticated()) {
        return;
    }
    if (!checkSupport(accountItem)) {
        return;
    }
    EventBus.getDefault().post(new LastHistoryLoadStartedEvent(chat));
    org.jivesoftware.smackx.mam.MamManager mamManager = org.jivesoftware.smackx.mam.MamManager.getInstanceFor(connection);
    String lastMessageMamId;
    int receivedMessagesCount;
    do {
        Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
        lastMessageMamId = getSyncInfo(realm, chat.getAccount(), chat.getUser()).getLastMessageMamId();
        realm.close();
        receivedMessagesCount = requestLastHistoryPage(mamManager, chat, lastMessageMamId);
    // if it was NOT the first time, and we got exactly one page,
    // it means that there should be more unloaded recent history
    } while (lastMessageMamId != null && receivedMessagesCount == PAGE_SIZE);
    // it mean that all previous history loaded
    if (lastMessageMamId == null && receivedMessagesCount >= 0 && receivedMessagesCount < PAGE_SIZE) {
        setRemoteHistoryCompletelyLoaded(chat);
    }
    EventBus.getDefault().post(new LastHistoryLoadFinishedEvent(chat));
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) AccountItem(com.xabber.android.data.account.AccountItem) Realm(io.realm.Realm)

Example 18 with AccountItem

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

the class MamManager method requestFullChatHistory.

/**
 * Only for debugging
 * Call only from background thread
 * @param chat
 */
public void requestFullChatHistory(final AbstractChat chat) {
    if (chat == null || chat.isRemotePreviousHistoryCompletelyLoaded()) {
        return;
    }
    final AccountItem accountItem = AccountManager.getInstance().getAccount(chat.getAccount());
    if (accountItem == null || !accountItem.getFactualStatusMode().isOnline()) {
        return;
    }
    if (!checkSupport(accountItem)) {
        return;
    }
    String firstMamMessageMamId;
    boolean remoteHistoryCompletelyLoaded;
    {
        Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
        SyncInfo syncInfo = getSyncInfo(realm, chat.getAccount(), chat.getUser());
        firstMamMessageMamId = syncInfo.getFirstMamMessageMamId();
        remoteHistoryCompletelyLoaded = syncInfo.isRemoteHistoryCompletelyLoaded();
        realm.close();
    }
    if (remoteHistoryCompletelyLoaded) {
        chat.setRemotePreviousHistoryCompletelyLoaded(true);
    }
    if (firstMamMessageMamId == null || remoteHistoryCompletelyLoaded) {
        return;
    }
    org.jivesoftware.smackx.mam.MamManager mamManager = org.jivesoftware.smackx.mam.MamManager.getInstanceFor(accountItem.getConnection());
    final org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult;
    try {
        LogManager.i("MAM", "Loading previous history");
        mamQueryResult = mamManager.queryArchive(chat.getUser().getJid());
    } catch (SmackException.NotLoggedInException | SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
        LogManager.exception(this, e);
        return;
    }
    LogManager.i("MAM", "queryArchive finished. fin count expected: " + mamQueryResult.mamFin.getRSMSet().getCount() + " real: " + mamQueryResult.forwardedMessages.size());
    Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
    List<MessageItem> messageItems = getMessageItems(mamQueryResult, chat);
    syncMessages(realm, chat, messageItems);
    updatePreviousHistorySyncInfo(realm, chat, mamQueryResult, messageItems);
    realm.close();
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) AccountItem(com.xabber.android.data.account.AccountItem) SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo) Realm(io.realm.Realm)

Example 19 with AccountItem

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

the class ChatMessageAdapter method getHint.

/**
 * @return New hint.
 */
private String getHint() {
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    boolean online = accountItem != null && accountItem.getState().isConnected();
    final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
    if (!online) {
        if (abstractContact instanceof RoomContact) {
            return context.getString(R.string.muc_is_unavailable);
        } else {
            return context.getString(R.string.account_is_offline);
        }
    } else if (!abstractContact.getStatusMode().isOnline()) {
        if (abstractContact instanceof RoomContact) {
            return context.getString(R.string.muc_is_unavailable);
        } else {
            return context.getString(R.string.contact_is_offline);
        }
    }
    return null;
}
Also used : RoomContact(com.xabber.android.data.extension.muc.RoomContact) AccountItem(com.xabber.android.data.account.AccountItem) AbstractContact(com.xabber.android.data.roster.AbstractContact)

Example 20 with AccountItem

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

the class ChatFragment method loadHistoryIfNeeded.

private void loadHistoryIfNeeded() {
    AccountItem accountItem = AccountManager.getInstance().getAccount(this.account);
    if (accountItem == null) {
        return;
    }
    LoadHistorySettings loadHistorySettings = accountItem.getLoadHistorySettings();
    if (loadHistorySettings != LoadHistorySettings.current && loadHistorySettings != LoadHistorySettings.all) {
        return;
    }
    if (isRemoteHistoryRequested) {
        return;
    }
    int visibleItemCount = layoutManager.getChildCount();
    if (visibleItemCount == 0) {
        return;
    }
    int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
    if (firstVisibleItemPosition / visibleItemCount <= 2) {
        requestRemoteHistoryLoad();
        return;
    }
    if (firstVisibleItemPosition < firstRemoteSyncedItemPosition) {
        requestRemoteHistoryLoad();
        return;
    }
    if (firstVisibleItemPosition - firstRemoteSyncedItemPosition < visibleItemCount * 2) {
        requestRemoteHistoryLoad();
        return;
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) LoadHistorySettings(com.xabber.android.data.extension.mam.LoadHistorySettings)

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