diff options
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java | 121 |
1 files changed, 45 insertions, 76 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java index d92401ba..c1426080 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java @@ -455,7 +455,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } return START_STICKY; case Intent.ACTION_SHUTDOWN: - logoutAndSave(); + logoutAndSave(true); return START_NOT_STICKY; case ACTION_CLEAR_NOTIFICATION: mNotificationService.clear(); @@ -481,13 +481,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } break; case AudioManager.RINGER_MODE_CHANGED_ACTION: - if (xaOnSilentMode()) { + if (ConversationsPlusPreferences.xaOnSilentMode()) { refreshAllPresences(); } break; case Intent.ACTION_SCREEN_OFF: case Intent.ACTION_SCREEN_ON: - if (awayWhenScreenOff()) { + if (ConversationsPlusPreferences.awayWhenScreenOff()) { refreshAllPresences(); } break; @@ -572,22 +572,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa return START_STICKY; } - private boolean xaOnSilentMode() { - return getPreferences().getBoolean("xa_on_silent_mode", false); - } - - private boolean treatVibrateAsSilent() { - return getPreferences().getBoolean("treat_vibrate_as_silent", false); - } - - private boolean awayWhenScreenOff() { - return getPreferences().getBoolean("away_when_screen_off", false); - } - private Presence.Status getTargetPresence() { - if (xaOnSilentMode() && isPhoneSilenced()) { + if (ConversationsPlusPreferences.xaOnSilentMode() && isPhoneSilenced()) { return Presence.Status.XA; - } else if (awayWhenScreenOff() && !isInteractive()) { + } else if (ConversationsPlusPreferences.awayWhenScreenOff() && !isInteractive()) { return Presence.Status.AWAY; } else { return Presence.Status.ONLINE; @@ -610,7 +598,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private boolean isPhoneSilenced() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); - if (treatVibrateAsSilent()) { + if (ConversationsPlusPreferences.treatVibrateAsSilent()) { return audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL; } else { return audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT; @@ -725,7 +713,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } public void toggleScreenEventReceiver() { - if (awayWhenScreenOff()) { + if (ConversationsPlusPreferences.awayWhenScreenOff()) { final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(this.mEventReceiver, filter); @@ -750,12 +738,16 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public void onTaskRemoved(final Intent rootIntent) { super.onTaskRemoved(rootIntent); if (!ConversationsPlusPreferences.keepForegroundService()) { - this.logoutAndSave(); + this.logoutAndSave(false); } } - private void logoutAndSave() { + private void logoutAndSave(boolean stop) { + int activeAccounts = 0; for (final Account account : accounts) { + if (account.getStatus() != Account.State.DISABLED) { + activeAccounts++; + } databaseBackend.writeRoster(account.getRoster()); if (account.getXmppConnection() != null) { new Thread(new Runnable() { @@ -764,11 +756,12 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa disconnect(account, false); } }).start(); - cancelWakeUpCall(account.getUuid().hashCode()); } } - Logging.d(Config.LOGTAG, "good bye"); - stopSelf(); + if (stop || activeAccounts == 0) { + Logging.d(Config.LOGTAG, "good bye"); + stopSelf(); + } } private void cancelWakeUpCall(int requestCode) { @@ -801,6 +794,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa connection.setOnBindListener(this.mOnBindListener); connection.setOnMessageAcknowledgeListener(this.mOnMessageAcknowledgedListener); connection.addOnAdvancedStreamFeaturesAvailableListener(this.mMessageArchiveService); + connection.addOnAdvancedStreamFeaturesAvailableListener(AvatarService.getInstance()); AxolotlService axolotlService = account.getAxolotlService(); if (axolotlService != null) { connection.addOnAdvancedStreamFeaturesAvailableListener(axolotlService); @@ -967,7 +961,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (addToConversation) { conversation.add(message); } - if (message.getEncryption() == Message.ENCRYPTION_NONE || saveEncryptedMessages()) { + if (message.getEncryption() == Message.ENCRYPTION_NONE || !ConversationsPlusPreferences.dontSaveEncrypted()) { if (saveInDb) { databaseBackend.createMessage(message); } else if (message.edited()) { @@ -992,11 +986,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private void sendUnsentMessages(final Conversation conversation) { conversation.findWaitingMessages(new Conversation.OnMessageFound() { - @Override - public void onMessageFound(Message message) { - resendMessage(message, true); - } - }); + @Override + public void onMessageFound(Message message) { + resendMessage(message, true); + } + }); } public void resendMessage(final Message message, final boolean delay) { @@ -1027,7 +1021,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa final Element query = packet.query(); final HashMap<Jid, Bookmark> bookmarks = new HashMap<>(); final Element storage = query.findChild("storage", "storage:bookmarks"); - final boolean autojoin = respectAutojoin(); if (storage != null) { for (final Element item : storage.getChildren()) { if (item.getName().equals("conference")) { @@ -1039,7 +1032,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa Conversation conversation = find(bookmark); if (conversation != null) { conversation.setBookmark(bookmark); - } else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) { + } else if (bookmark.autojoin() && bookmark.getJid() != null && ConversationsPlusPreferences.autojoin()) { conversation = findOrCreateConversation( account, bookmark.getJid(), true); conversation.setBookmark(bookmark); @@ -1219,19 +1212,19 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } Collections.sort(list, new Comparator<Conversation>() { - @Override - public int compare(Conversation lhs, Conversation rhs) { - Message left = lhs.getLatestMessage(); - Message right = rhs.getLatestMessage(); - if (left.getTimeSent() > right.getTimeSent()) { - return -1; - } else if (left.getTimeSent() < right.getTimeSent()) { - return 1; - } else { - return 0; - } - } - }); + @Override + public int compare(Conversation lhs, Conversation rhs) { + Message left = lhs.getLatestMessage(); + Message right = rhs.getLatestMessage(); + if (left.getTimeSent() > right.getTimeSent()) { + return -1; + } else if (left.getTimeSent() < right.getTimeSent()) { + return 1; + } else { + return 0; + } + } + }); } public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) { @@ -1380,7 +1373,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getAccount().getStatus() == Account.State.ONLINE) { Bookmark bookmark = conversation.getBookmark(); - if (bookmark != null && bookmark.autojoin() && respectAutojoin()) { + if (bookmark != null && bookmark.autojoin() && ConversationsPlusPreferences.autojoin()) { bookmark.setAutojoin(false); pushBookmarks(bookmark.getAccount()); } @@ -1886,9 +1879,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (conversation.getMode() == Conversation.MODE_MULTI) { conversation.getMucOptions().setPassword(password); if (conversation.getBookmark() != null) { - if (respectAutojoin()) { - conversation.getBookmark().setAutojoin(true); - } + conversation.getBookmark().setAutojoin(ConversationsPlusPreferences.autojoin()); pushBookmarks(conversation.getAccount()); } databaseBackend.updateConversation(conversation); @@ -2240,9 +2231,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa final Account account = conversation.getAccount(); final Session otrSession = conversation.getOtrSession(); Logging.d(Config.LOGTAG, - account.getJid().toBareJid() + " otr session established with " - + conversation.getJid() + "/" - + otrSession.getSessionID().getUserID()); + account.getJid().toBareJid() + " otr session established with " + + conversation.getJid() + "/" + + otrSession.getSessionID().getUserID()); conversation.findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, new Conversation.OnMessageFound() { @Override @@ -2280,7 +2271,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa packet.setFrom(account.getJid()); MessageGenerator.addMessageHints(packet); packet.setAttribute("to", otrSession.getSessionID().getAccountID() + "/" - + otrSession.getSessionID().getUserID()); + + otrSession.getSessionID().getUserID()); try { packet.setBody(otrSession .transformSending(CryptoHelper.FILETRANSFER @@ -2346,12 +2337,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (!account.isOptionSet(Account.OPTION_DISABLED)) { if (!force) { disconnect(account, false); - try { - Logging.d(Config.LOGTAG, "wait for disconnect"); - Thread.sleep(500); //sleep wait for disconnect - } catch (InterruptedException e) { - //ignored - } } Thread thread = new Thread(connection); connection.setInteractive(interactive); @@ -2362,6 +2347,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa disconnect(account, force); account.getRoster().clearPresences(); connection.resetEverything(); + account.getAxolotlService().resetBrokenness(); } } } @@ -2441,23 +2427,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa updateConversationUi(); } - public SharedPreferences getPreferences() { - return PreferenceManager - .getDefaultSharedPreferences(getApplicationContext()); - } - - public boolean saveEncryptedMessages() { - return !getPreferences().getBoolean("dont_save_encrypted", false); - } - - private boolean respectAutojoin() { - return getPreferences().getBoolean("autojoin", true); - } - - public boolean showExtendedConnectionOptions() { - return getPreferences().getBoolean("show_connection_options", false); - } - public int unreadCount() { int count = 0; for (Conversation conversation : getConversations()) { |