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