From 5be43e36e43303904d80cde9171b45ed5960ddf7 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 7 Nov 2018 15:44:39 +0100 Subject: [PATCH] always do contact sync in background --- .../services/QuickConversationsService.java | 2 +- .../java/eu/siacs/conversations/Config.java | 2 +- .../AbstractQuickConversationsService.java | 2 +- .../services/XmppConnectionService.java | 2 +- .../ui/StartConversationActivity.java | 4 ++-- .../services/QuickConversationsService.java | 24 +++++++++++++++---- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/conversations/java/eu/siacs/conversations/services/QuickConversationsService.java b/src/conversations/java/eu/siacs/conversations/services/QuickConversationsService.java index 7632206fe..1d83a5cb5 100644 --- a/src/conversations/java/eu/siacs/conversations/services/QuickConversationsService.java +++ b/src/conversations/java/eu/siacs/conversations/services/QuickConversationsService.java @@ -22,7 +22,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService } @Override - public void considerSync(boolean force) { + public void considerSyncBackground(boolean force) { } } \ No newline at end of file diff --git a/src/main/java/eu/siacs/conversations/Config.java b/src/main/java/eu/siacs/conversations/Config.java index f28ab354b..6ea05a375 100644 --- a/src/main/java/eu/siacs/conversations/Config.java +++ b/src/main/java/eu/siacs/conversations/Config.java @@ -48,7 +48,7 @@ public final class Config { public static final boolean ALLOW_NON_TLS_CONNECTIONS = false; //very dangerous. you should have a good reason to set this to true - public static final long CONTACT_SYNC_RETRY_INTERVAL = 1000L * 60 * 1; + public static final long CONTACT_SYNC_RETRY_INTERVAL = 1000L * 60 * 5; //Notification settings diff --git a/src/main/java/eu/siacs/conversations/services/AbstractQuickConversationsService.java b/src/main/java/eu/siacs/conversations/services/AbstractQuickConversationsService.java index b7550c47c..dabd36290 100644 --- a/src/main/java/eu/siacs/conversations/services/AbstractQuickConversationsService.java +++ b/src/main/java/eu/siacs/conversations/services/AbstractQuickConversationsService.java @@ -24,5 +24,5 @@ public abstract class AbstractQuickConversationsService { public abstract boolean isSynchronizing(); - public abstract void considerSync(boolean force); + public abstract void considerSyncBackground(boolean force); } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 27d477c6a..4927a6f35 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -292,7 +292,7 @@ public class XmppConnectionService extends Service { } account.getRoster().clearPresences(); mJingleConnectionManager.cancelInTransmission(); - mQuickConversationsService.considerSync(); + mQuickConversationsService.considerSyncBackground(false); fetchRosterFromServer(account); if (!account.getXmppConnection().getFeatures().bookmarksConversion()) { fetchBookmarks(account); diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index 98c7a582f..bcbb2980c 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -728,7 +728,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne @Override protected void onBackendConnected() { - xmppConnectionService.getQuickConversationsService().considerSync(); + xmppConnectionService.getQuickConversationsService().considerSyncBackground(false); if (mPostponedActivityResult != null) { onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second); this.mPostponedActivityResult = null; @@ -1008,7 +1008,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne public void onRefresh() { Log.d(Config.LOGTAG,"user requested to refresh"); if (QuickConversationsService.isQuicksy() && xmppConnectionService != null) { - xmppConnectionService.getQuickConversationsService().considerSync(true); + xmppConnectionService.getQuickConversationsService().considerSyncBackground(true); } } diff --git a/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java b/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java index f777d768b..595f88a6c 100644 --- a/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java +++ b/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java @@ -41,6 +41,7 @@ import eu.siacs.conversations.entities.Entry; import eu.siacs.conversations.utils.AccountUtils; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.PhoneNumberUtilWrapper; +import eu.siacs.conversations.utils.SerialSingleThreadExecutor; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.OnIqPacketReceived; @@ -58,9 +59,9 @@ public class QuickConversationsService extends AbstractQuickConversationsService public static final int API_ERROR_SSL_HANDSHAKE = -4; public static final int API_ERROR_AIRPLANE_MODE = -5; - private static final String API_DOMAIN = "api."+Config.QUICKSY_DOMAIN; + private static final String API_DOMAIN = "api." + Config.QUICKSY_DOMAIN; - private static final String BASE_URL = "https://"+API_DOMAIN; + private static final String BASE_URL = "https://" + API_DOMAIN; private static final String INSTALLATION_ID = "eu.siacs.conversations.installation-id"; @@ -74,6 +75,8 @@ public class QuickConversationsService extends AbstractQuickConversationsService private Attempt mLastSyncAttempt = Attempt.NULL; + private final SerialSingleThreadExecutor mSerialSingleThreadExecutor = new SerialSingleThreadExecutor(QuickConversationsService.class.getSimpleName()); + QuickConversationsService(XmppConnectionService xmppConnectionService) { super(xmppConnectionService); } @@ -296,7 +299,18 @@ public class QuickConversationsService extends AbstractQuickConversationsService } @Override - public void considerSync(boolean forced) { + public void considerSyncBackground(final boolean forced) { + mRunningSyncJobs.incrementAndGet(); + mSerialSingleThreadExecutor.execute(() -> { + considerSync(forced); + if (mRunningSyncJobs.decrementAndGet() == 0) { + service.updateRosterUi(); + } + }); + } + + + private void considerSync(boolean forced) { Map contacts = PhoneNumberContact.load(service); for (Account account : service.getAccounts()) { refresh(account, contacts.values()); @@ -378,7 +392,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService } else if (response.getType() == IqPacket.TYPE.TIMEOUT) { mLastSyncAttempt = Attempt.NULL; } else { - Log.d(Config.LOGTAG,account.getJid().asBareJid()+": failed to sync contact list with api server"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": failed to sync contact list with api server"); } mRunningSyncJobs.decrementAndGet(); service.syncRoster(account); @@ -408,7 +422,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService private final long timestamp; private int hash; - private static final Attempt NULL = new Attempt(0,0); + private static final Attempt NULL = new Attempt(0, 0); private Attempt(long timestamp, int hash) { this.timestamp = timestamp;