added count down latch to await account connect before going into publish profile activity

Daniel Gultsch created

Change summary

src/conversations/java/eu/siacs/conversations/services/QuickConversationsService.java |  5 
src/main/java/eu/siacs/conversations/services/AbstractQuickConversationsService.java  |  2 
src/main/java/eu/siacs/conversations/services/XmppConnectionService.java              |  5 
src/quicksy/java/eu/siacs/conversations/entities/Entry.java                           |  3 
src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java       | 67 
5 files changed, 53 insertions(+), 29 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -322,6 +322,11 @@ 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())) {

src/quicksy/java/eu/siacs/conversations/entities/Entry.java 🔗

@@ -1,7 +1,6 @@
 package eu.siacs.conversations.entities;
 
 import android.util.Base64;
-import android.util.Log;
 
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
@@ -10,7 +9,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
-import eu.siacs.conversations.Config;
 import eu.siacs.conversations.android.PhoneNumberContact;
 import eu.siacs.conversations.xml.Element;
 import rocks.xmpp.addr.Jid;
@@ -71,7 +69,6 @@ public class Entry implements Comparable<Entry> {
         } catch (NoSuchAlgorithmException e) {
             return "";
         }
-        Log.d(Config.LOGTAG,"status quo string: "+builder.toString());
         byte[] sha1 = md.digest(builder.toString().getBytes());
         return new String(Base64.encode(sha1, Base64.DEFAULT)).trim();
     }

src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java 🔗

@@ -24,6 +24,8 @@ import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 import java.util.WeakHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.net.ssl.SSLHandshakeException;
@@ -64,8 +66,9 @@ public class QuickConversationsService extends AbstractQuickConversationsService
 
     private final AtomicBoolean mVerificationInProgress = new AtomicBoolean(false);
     private final AtomicBoolean mVerificationRequestInProgress = new AtomicBoolean(false);
+    private CountDownLatch awaitingAccountStateChange;
 
-    private Attempt mLastSyncAttempt = new Attempt(0,0);
+    private Attempt mLastSyncAttempt = new Attempt(0, 0);
 
     QuickConversationsService(XmppConnectionService xmppConnectionService) {
         super(xmppConnectionService);
@@ -144,6 +147,13 @@ public class QuickConversationsService extends AbstractQuickConversationsService
 
     }
 
+    public void signalAccountStateChange() {
+        if (awaitingAccountStateChange != null && awaitingAccountStateChange.getCount() > 0) {
+            Log.d(Config.LOGTAG, "signaled state change");
+            awaitingAccountStateChange.countDown();
+        }
+    }
+
     private void createAccountAndWait(Phonenumber.PhoneNumber phoneNumber, final long timestamp) {
         String local = PhoneNumberUtilWrapper.normalize(service, phoneNumber);
         Log.d(Config.LOGTAG, "requesting verification for " + PhoneNumberUtilWrapper.normalize(service, phoneNumber));
@@ -194,7 +204,13 @@ public class QuickConversationsService extends AbstractQuickConversationsService
                     if (code == 200) {
                         account.setOption(Account.OPTION_UNVERIFIED, false);
                         account.setOption(Account.OPTION_DISABLED, false);
+                        awaitingAccountStateChange = new CountDownLatch(1);
                         service.updateAccount(account);
+                        try {
+                            awaitingAccountStateChange.await(5, TimeUnit.SECONDS);
+                        } catch (InterruptedException e) {
+                            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": timer expired while waiting for account to connect");
+                        }
                         synchronized (mOnVerification) {
                             for (OnVerification onVerification : mOnVerification) {
                                 onVerification.onVerificationSucceeded();
@@ -282,7 +298,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService
     }
 
     private void refresh(Account account, Collection<PhoneNumberContact> contacts) {
-        for(Contact contact : account.getRoster().getWithSystemAccounts(PhoneNumberContact.class)) {
+        for (Contact contact : account.getRoster().getWithSystemAccounts(PhoneNumberContact.class)) {
             final Uri uri = contact.getSystemAccount();
             if (uri == null) {
                 continue;
@@ -293,7 +309,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService
                 needsCacheClean = contact.setPhoneContact(phoneNumberContact);
             } else {
                 needsCacheClean = contact.unsetPhoneContact(PhoneNumberContact.class);
-                Log.d(Config.LOGTAG,uri.toString()+" vanished from address book");
+                Log.d(Config.LOGTAG, uri.toString() + " vanished from address book");
             }
             if (needsCacheClean) {
                 service.getAvatarService().clear(contact);
@@ -303,9 +319,9 @@ 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);
+        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");
+            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": do not attempt sync");
             return false;
         }
         XmppConnection xmppConnection = account.getXmppConnection();
@@ -323,8 +339,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService
         query.setTo(syncServer);
         Element book = new Element("phone-book", Namespace.SYNCHRONIZATION).setChildren(entries);
         String statusQuo = Entry.statusQuo(contacts.values(), account.getRoster().getWithSystemAccounts(PhoneNumberContact.class));
-        Log.d(Config.LOGTAG,"status quo="+statusQuo);
-        book.setAttribute("ver",statusQuo);
+        book.setAttribute("ver", statusQuo);
         query.addChild(book);
         mLastSyncAttempt = Attempt.create(hash);
         service.sendIqPacket(account, query, (a, response) -> {
@@ -332,12 +347,12 @@ public class QuickConversationsService extends AbstractQuickConversationsService
                 final Element phoneBook = response.findChild("phone-book", Namespace.SYNCHRONIZATION);
                 if (phoneBook != null) {
                     List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(PhoneNumberContact.class);
-                    for(Entry entry : Entry.ofPhoneBook(phoneBook)) {
+                    for (Entry entry : Entry.ofPhoneBook(phoneBook)) {
                         PhoneNumberContact phoneContact = contacts.get(entry.getNumber());
                         if (phoneContact == null) {
                             continue;
                         }
-                        for(Jid jid : entry.getJids()) {
+                        for (Jid jid : entry.getJids()) {
                             Contact contact = account.getRoster().getContact(jid);
                             final boolean needsCacheClean = contact.setPhoneContact(phoneContact);
                             if (needsCacheClean) {
@@ -353,7 +368,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService
                         }
                     }
                 } else {
-                    Log.d(Config.LOGTAG,account.getJid().asBareJid()+": phone number contact list remains unchanged");
+                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": phone number contact list remains unchanged");
                 }
             }
             service.syncRoster(account);
@@ -363,6 +378,22 @@ public class QuickConversationsService extends AbstractQuickConversationsService
     }
 
 
+    public interface OnVerificationRequested {
+        void onVerificationRequestFailed(int code);
+
+        void onVerificationRequested();
+
+        void onVerificationRequestedRetryAt(long timestamp);
+    }
+
+    public interface OnVerification {
+        void onVerificationFailed(int code);
+
+        void onVerificationSucceeded();
+
+        void onVerificationRetryAt(long timestamp);
+    }
+
     private static class Attempt {
         private final long timestamp;
         private int hash;
@@ -380,20 +411,4 @@ public class QuickConversationsService extends AbstractQuickConversationsService
             return hash != this.hash || SystemClock.elapsedRealtime() - timestamp >= Config.CONTACT_SYNC_RETRY_INTERVAL;
         }
     }
-
-    public interface OnVerificationRequested {
-        void onVerificationRequestFailed(int code);
-
-        void onVerificationRequested();
-
-        void onVerificationRequestedRetryAt(long timestamp);
-    }
-
-    public interface OnVerification {
-        void onVerificationFailed(int code);
-
-        void onVerificationSucceeded();
-
-        void onVerificationRetryAt(long timestamp);
-    }
 }