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);
}
}
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));
}
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();
}
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;
}
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;
}
}
Aggregations