From 1bcbd257c39a777fc6063093956dd00f7a57a8b2 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 29 Oct 2018 12:00:25 +0100 Subject: [PATCH] refresh contacts when opening StartConversationsActivity --- .../java/eu/siacs/conversations/Config.java | 2 ++ .../siacs/conversations/entities/Account.java | 8 +++++ .../services/NotificationService.java | 3 +- .../ui/StartConversationActivity.java | 1 + .../services/QuickConversationsService.java | 30 ++++++++++++++++++- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/Config.java b/src/main/java/eu/siacs/conversations/Config.java index 9add37561..0bb43be38 100644 --- a/src/main/java/eu/siacs/conversations/Config.java +++ b/src/main/java/eu/siacs/conversations/Config.java @@ -48,6 +48,8 @@ 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 * 5; + //Notification settings public static final boolean HIDE_MESSAGE_TEXT_IN_NOTIFICATION = false; diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 4fea4d6a1..55e330091 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -67,6 +67,7 @@ public class Account extends AbstractEntity { protected String password; protected int options = 0; protected State status = State.OFFLINE; + private State lastErrorStatus = State.OFFLINE; protected String resource; protected String avatar; protected String hostname = null; @@ -263,8 +264,15 @@ public class Account extends AbstractEntity { } } + public State getLastErrorStatus() { + return this.lastErrorStatus; + } + public void setStatus(final State status) { this.status = status; + if (status.isError) { + this.lastErrorStatus = status; + } } public State getTrueStatus() { diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index ad1a8f81a..c0ddb5c02 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -890,9 +890,10 @@ public class NotificationService { cancel(ERROR_NOTIFICATION_ID); return; } + final boolean showAllErrors = QuickConversationsService.isConversations(); final List errors = new ArrayList<>(); for (final Account account : mXmppConnectionService.getAccounts()) { - if (account.hasErrorStatus() && account.showErrorNotification()) { + if (account.hasErrorStatus() && account.showErrorNotification() && (showAllErrors || account.getLastErrorStatus() == Account.State.UNAUTHORIZED)) { errors.add(account); } } diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index ab498f934..56a38877c 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -726,6 +726,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne @Override protected void onBackendConnected() { + xmppConnectionService.getQuickConversationsService().considerSync(); if (mPostponedActivityResult != null) { onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second); this.mPostponedActivityResult = null; diff --git a/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java b/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java index cccd37872..b391361cb 100644 --- a/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java +++ b/src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java @@ -64,6 +64,8 @@ public class QuickConversationsService extends AbstractQuickConversationsService private final AtomicBoolean mVerificationInProgress = new AtomicBoolean(false); private final AtomicBoolean mVerificationRequestInProgress = new AtomicBoolean(false); + private Attempt mLastSyncAttempt = new Attempt(0,0); + QuickConversationsService(XmppConnectionService xmppConnectionService) { super(xmppConnectionService); } @@ -299,6 +301,12 @@ public class QuickConversationsService extends AbstractQuickConversationsService } private boolean considerSync(Account account, final Map contacts) { + final int hash = contacts.keySet().hashCode(); + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": consider sync of "+hash); + if (!mLastSyncAttempt.retry(hash)) { + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": do not attempt sync"); + return false; + } XmppConnection xmppConnection = account.getXmppConnection(); Jid syncServer = xmppConnection == null ? null : xmppConnection.findDiscoItemByFeature(Namespace.SYNCHRONIZATION); if (syncServer == null) { @@ -313,6 +321,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService IqPacket query = new IqPacket(IqPacket.TYPE.GET); query.setTo(syncServer); query.addChild(new Element("phone-book", Namespace.SYNCHRONIZATION).setChildren(entries)); + mLastSyncAttempt = Attempt.create(hash); service.sendIqPacket(account, query, (a, response) -> { if (response.getType() == IqPacket.TYPE.RESULT) { List withSystemAccounts = account.getRoster().getWithSystemAccounts(PhoneNumberContact.class); @@ -334,13 +343,14 @@ public class QuickConversationsService extends AbstractQuickConversationsService } } for (Contact contact : withSystemAccounts) { - boolean needsCacheClean = contact.unsetPhoneContact(JabberIdContact.class); + final boolean needsCacheClean = contact.unsetPhoneContact(PhoneNumberContact.class); if (needsCacheClean) { service.getAvatarService().clear(contact); } } } service.syncRoster(account); + service.updateRosterUi(); }); return true; } @@ -354,6 +364,24 @@ public class QuickConversationsService extends AbstractQuickConversationsService return null; } + private static class Attempt { + private final long timestamp; + private int hash; + + private Attempt(long timestamp, int hash) { + this.timestamp = timestamp; + this.hash = hash; + } + + public static Attempt create(int hash) { + return new Attempt(SystemClock.elapsedRealtime(), hash); + } + + public boolean retry(int hash) { + return hash != this.hash || SystemClock.elapsedRealtime() - timestamp >= Config.CONTACT_SYNC_RETRY_INTERVAL; + } + } + public static class Entry { private final List jids; private final String number;