refresh contacts when opening StartConversationsActivity

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/Config.java                                |  2 
src/main/java/eu/siacs/conversations/entities/Account.java                      |  8 
src/main/java/eu/siacs/conversations/services/NotificationService.java          |  3 
src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java          |  1 
src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java | 30 
5 files changed, 42 insertions(+), 2 deletions(-)

Detailed changes

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;

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() {

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<Account> 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);
             }
         }

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;

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<String, PhoneNumberContact> 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<Contact> 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<Jid> jids;
         private final String number;