diff options
Diffstat (limited to 'src/main/java/de/pixart/messenger/services/XmppConnectionService.java')
-rw-r--r-- | src/main/java/de/pixart/messenger/services/XmppConnectionService.java | 169 |
1 files changed, 102 insertions, 67 deletions
diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index bb704c6b6..7c5d2b64d 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -81,6 +81,7 @@ import java.util.concurrent.atomic.AtomicLong; import de.pixart.messenger.BuildConfig; import de.pixart.messenger.Config; import de.pixart.messenger.R; +import de.pixart.messenger.android.JabberIdContact; import de.pixart.messenger.crypto.OmemoSetting; import de.pixart.messenger.crypto.PgpDecryptionService; import de.pixart.messenger.crypto.PgpEngine; @@ -243,7 +244,7 @@ public class XmppConnectionService extends Service { } }; public HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(this); - private ReplacingSerialSingleThreadExecutor mContactMergerExecutor = new ReplacingSerialSingleThreadExecutor(true); + private ReplacingSerialSingleThreadExecutor mContactMergerExecutor = new ReplacingSerialSingleThreadExecutor("ContactMerger"); private long mLastActivity = 0; private MemorizingTrustManager mMemorizingTrustManager; private NotificationService mNotificationService = new NotificationService(this); @@ -286,6 +287,7 @@ public class XmppConnectionService extends Service { private AvatarService mAvatarService = new AvatarService(this); private MessageArchiveService mMessageArchiveService = new MessageArchiveService(this); private PushManagementService mPushManagementService = new PushManagementService(this); + private QuickConversationsService mQuickConversationsService = new QuickConversationsService(this); private final OnBindListener mOnBindListener = new OnBindListener() { @Override @@ -298,13 +300,22 @@ public class XmppConnectionService extends Service { } } } - boolean needsUpdating = account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, true); - needsUpdating |= account.setOption(Account.OPTION_HTTP_UPLOAD_AVAILABLE, account.getXmppConnection().getFeatures().httpUpload(0)); - if (needsUpdating) { + boolean loggedInSuccessfully = account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, true); + boolean gainedFeature = account.setOption(Account.OPTION_HTTP_UPLOAD_AVAILABLE, account.getXmppConnection().getFeatures().httpUpload(0)); + if (loggedInSuccessfully || gainedFeature) { databaseBackend.updateAccount(account); } + + if (loggedInSuccessfully) { + if (!TextUtils.isEmpty(account.getDisplayName())) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": display name wasn't empty on first log in. publishing"); + publishDisplayName(account); + } + } + account.getRoster().clearPresences(); mJingleConnectionManager.cancelInTransmission(); + mQuickConversationsService.considerSyncBackground(false); fetchRosterFromServer(account); if (!account.getXmppConnection().getFeatures().bookmarksConversion()) { fetchBookmarks(account); @@ -336,6 +347,9 @@ public class XmppConnectionService extends Service { public void onStatusChanged(final Account account) { XmppConnection connection = account.getXmppConnection(); updateAccountUi(); + if (account.getStatus() == Account.State.ONLINE || account.getStatus().isError()) { + mQuickConversationsService.signalAccountStateChange(); + } if (account.getStatus() == Account.State.ONLINE) { synchronized (mLowPingTimeoutMode) { if (mLowPingTimeoutMode.remove(account.getJid().asBareJid())) { @@ -567,6 +581,11 @@ public class XmppConnectionService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { final String action = intent == null ? null : intent.getAction(); + final boolean needsForegroundService = intent != null && intent.getBooleanExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, false); + if (needsForegroundService) { + Log.d(Config.LOGTAG, "toggle forced foreground service after receiving event"); + toggleForegroundService(true); + } String pushedAccountHash = null; boolean interactive = false; if (action != null) { @@ -938,15 +957,18 @@ public class XmppConnectionService extends Service { @SuppressLint("NewApi") @SuppressWarnings("deprecation") public boolean isInteractive() { - final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); - - final boolean isScreenOn; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - isScreenOn = pm.isScreenOn(); - } else { - isScreenOn = pm.isInteractive(); + try { + final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + final boolean isScreenOn; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + isScreenOn = pm.isScreenOn(); + } else { + isScreenOn = pm.isInteractive(); + } + return isScreenOn; + } catch (RuntimeException e) { + return false; } - return isScreenOn; } private boolean isPhoneSilenced() { @@ -1229,8 +1251,12 @@ public class XmppConnectionService extends Service { } public void toggleForegroundService() { + toggleForegroundService(false); + } + + private void toggleForegroundService(boolean force) { final boolean status; - if (mForceForegroundService.get() || (Compatibility.keepForegroundService(this)/* && hasEnabledAccounts()*/)) { + if (force || mForceForegroundService.get() || (Compatibility.keepForegroundService(this)/* && hasEnabledAccounts()*/)) { startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification()); status = true; } else { @@ -1241,6 +1267,10 @@ public class XmppConnectionService extends Service { Log.d(Config.LOGTAG, "ForegroundService: " + (status ? "on" : "off")); } + public boolean foregroundNotificationNeedsUpdatingWhenErrorStateChanges() { + return !mForceForegroundService.get() && Compatibility.keepForegroundService(this) && hasEnabledAccounts(); + } + @Override public void onTaskRemoved(final Intent rootIntent) { super.onTaskRemoved(rootIntent); @@ -1351,6 +1381,13 @@ public class XmppConnectionService extends Service { } final Conversation conversation = (Conversation) message.getConversation(); account.deactivateGracePeriod(); + if (QuickConversationsService.isQuicksy() && conversation.getMode() == Conversation.MODE_SINGLE) { + final Contact contact = conversation.getContact(); + if (!contact.showInRoster() && contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": adding " + contact.getJid() + " on sending message"); + createContact(contact, true); + } + } MessagePacket packet = null; final boolean addToConversation = (conversation.getMode() != Conversation.MODE_MULTI || !Patches.BAD_MUC_REFLECTION.contains(account.getServerIdentity())) @@ -1585,7 +1622,7 @@ public class XmppConnectionService extends Service { if (conversation != null) { bookmark.setConversation(conversation); } else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) { - conversation = findOrCreateConversation(account, bookmark.getJid(), true, true, false); + conversation = findOrCreateConversation(account, bookmark.getFullJid(), true, true, false); bookmark.setConversation(conversation); } } @@ -1720,45 +1757,31 @@ public class XmppConnectionService extends Service { } public void loadPhoneContacts() { - mContactMergerExecutor.execute(() -> PhoneHelper.loadPhoneContacts(XmppConnectionService.this, new OnPhoneContactsLoadedListener() { - @Override - public void onPhoneContactsLoaded(List<Bundle> phoneContacts) { - Log.d(Config.LOGTAG, "start merging phone contacts with roster"); - for (Account account : accounts) { - List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(); - for (Bundle phoneContact : phoneContacts) { - Jid jid; - try { - jid = Jid.of(phoneContact.getString("jid")); - } catch (final IllegalArgumentException e) { - continue; - } - final Contact contact = account.getRoster().getContact(jid); - String systemAccount = phoneContact.getInt("phoneid") - + "#" - + phoneContact.getString("lookup"); - contact.setSystemAccount(systemAccount); - boolean needsCacheClean = contact.setPhotoUri(phoneContact.getString("photouri")); - needsCacheClean |= contact.setSystemName(phoneContact.getString("displayname")); - if (needsCacheClean) { - getAvatarService().clear(contact); - } - withSystemAccounts.remove(contact); + mContactMergerExecutor.execute(() -> { + Map<Jid, JabberIdContact> contacts = JabberIdContact.load(this); + Log.d(Config.LOGTAG, "start merging phone contacts with roster"); + for (Account account : accounts) { + List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(JabberIdContact.class); + for (JabberIdContact jidContact : contacts.values()) { + final Contact contact = account.getRoster().getContact(jidContact.getJid()); + boolean needsCacheClean = contact.setPhoneContact(jidContact); + if (needsCacheClean) { + getAvatarService().clear(contact); } - for (Contact contact : withSystemAccounts) { - contact.setSystemAccount(null); - boolean needsCacheClean = contact.setPhotoUri(null); - needsCacheClean |= contact.setSystemName(null); - if (needsCacheClean) { - getAvatarService().clear(contact); - } + withSystemAccounts.remove(contact); + } + for (Contact contact : withSystemAccounts) { + boolean needsCacheClean = contact.unsetPhoneContact(JabberIdContact.class); + if (needsCacheClean) { + getAvatarService().clear(contact); } } - Log.d(Config.LOGTAG, "finished merging phone contacts"); - mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false, true)); - updateRosterUi(); } - })); + Log.d(Config.LOGTAG, "finished merging phone contacts"); + mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false, true)); + updateRosterUi(); + mQuickConversationsService.considerSync(); + }); } public void syncRoster(final Account account) { @@ -2072,11 +2095,7 @@ public class XmppConnectionService extends Service { } else { conversation.endOtrIfNeeded(); if (conversation.getContact().getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) { - Log.d(Config.LOGTAG, "Canceling presence request from " + conversation.getJid().toString()); - sendPresencePacket( - conversation.getAccount(), - mPresenceGenerator.stopPresenceUpdatesTo(conversation.getContact()) - ); + stopPresenceUpdatesTo(conversation.getContact()); } } updateConversation(conversation); @@ -2085,6 +2104,12 @@ public class XmppConnectionService extends Service { } } + public void stopPresenceUpdatesTo(Contact contact) { + Log.d(Config.LOGTAG, "Canceling presence request from " + contact.getJid().toString()); + sendPresencePacket(contact.getAccount(), mPresenceGenerator.stopPresenceUpdatesTo(contact)); + contact.resetOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST); + } + public void createAccount(final Account account) { account.initAccountServices(this); databaseBackend.createAccount(account); @@ -2695,15 +2720,17 @@ public class XmppConnectionService extends Service { public void persistSelfNick(MucOptions.User self) { final Conversation conversation = self.getConversation(); + final boolean tookProposedNickFromBookmark = conversation.getMucOptions().isTookProposedNickFromBookmark(); Jid full = self.getFullJid(); if (!full.equals(conversation.getJid())) { Log.d(Config.LOGTAG, "nick changed. updating"); conversation.setContactJid(full); databaseBackend.updateConversation(conversation); } - - Bookmark bookmark = conversation.getBookmark(); - if (bookmark != null && !full.getResource().equals(bookmark.getNick())) { + final Bookmark bookmark = conversation.getBookmark(); + final String bookmarkedNick = bookmark == null ? null : bookmark.getNick(); + if (bookmark != null && (tookProposedNickFromBookmark || TextUtils.isEmpty(bookmarkedNick)) && !full.getResource().equals(bookmarkedNick)) { + Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": persist nick '" + full.getResource() + "' into bookmark for " + conversation.getJid().asBareJid()); bookmark.setNick(full.getResource()); pushBookmarks(bookmark.getAccount()); } @@ -3683,11 +3710,11 @@ public class XmppConnectionService extends Service { } public boolean useTorToConnect() { - return Config.FORCE_ORBOT || getBooleanPreference("use_tor", R.bool.use_tor); + return QuickConversationsService.isConversations() && getBooleanPreference("use_tor", R.bool.use_tor); } public boolean showExtendedConnectionOptions() { - return getBooleanPreference("show_connection_options", R.bool.show_connection_options); + return QuickConversationsService.isConversations() && getBooleanPreference("show_connection_options", R.bool.show_connection_options); } public boolean warnUnecryptedChat() { @@ -4053,12 +4080,16 @@ public class XmppConnectionService extends Service { return this.mMessageArchiveService; } + public QuickConversationsService getQuickConversationsService() { + return this.mQuickConversationsService; + } + public List<Contact> findContacts(Jid jid, String accountJid) { ArrayList<Contact> contacts = new ArrayList<>(); for (Account account : getAccounts()) { if ((account.isEnabled() || accountJid != null) && (accountJid == null || accountJid.equals(account.getJid().asBareJid().toString()))) { - Contact contact = account.getRoster().getContactFromRoster(jid); + Contact contact = account.getRoster().getContactFromContactList(jid); if (contact != null) { contacts.add(contact); } @@ -4201,14 +4232,18 @@ public class XmppConnectionService extends Service { public void publishDisplayName(Account account) { String displayName = account.getDisplayName(); - if (displayName != null && !displayName.isEmpty()) { - IqPacket publish = mIqGenerator.publishNick(displayName); - sendIqPacket(account, publish, (account1, packet) -> { - if (packet.getType() == IqPacket.TYPE.ERROR) { - Log.d(Config.LOGTAG, account1.getJid().asBareJid() + ": could not publish nick"); - } - }); + final IqPacket request; + if (TextUtils.isEmpty(displayName)) { + request = mIqGenerator.deleteNode(Namespace.NICK); + } else { + request = mIqGenerator.publishNick(displayName); } + mAvatarService.clear(account); + sendIqPacket(account, request, (account1, packet) -> { + if (packet.getType() == IqPacket.TYPE.ERROR) { + Log.d(Config.LOGTAG, account1.getJid().asBareJid() + ": unable to modify nick name " + packet.toString()); + } + }); } public ServiceDiscoveryResult getCachedServiceDiscoveryResult(Pair<String, String> key) { |