WIP QuickConversationsService

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java      |  8 
src/quick/java/eu/siacs/conversations/services/QuickConversationsService.java | 50 
src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java        | 24 
src/quick/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java       |  7 
4 files changed, 81 insertions(+), 8 deletions(-)

Detailed changes

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

@@ -225,11 +225,11 @@ public class XmppConnectionService extends Service {
             mJingleConnectionManager.deliverPacket(account, packet);
         }
     };
-    private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(
-            this);
+    private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(this);
     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 ConversationsFileObserver fileObserver = new ConversationsFileObserver(
             Environment.getExternalStorageDirectory().getAbsolutePath()
     ) {
@@ -3682,6 +3682,10 @@ 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()) {

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

@@ -1,12 +1,60 @@
 package eu.siacs.conversations.services;
 
-import eu.siacs.conversations.services.XmppConnectionService;
+
+import android.util.Log;
+
+import java.util.Collections;
+import java.util.Set;
+import java.util.WeakHashMap;
+
+import eu.siacs.conversations.Config;
+import eu.siacs.conversations.entities.Account;
+import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
+import io.michaelrocks.libphonenumber.android.Phonenumber;
+import rocks.xmpp.addr.Jid;
 
 public class QuickConversationsService {
 
     private final XmppConnectionService service;
 
+    private final Set<OnVerificationRequested> mOnVerificationRequested = Collections.newSetFromMap(new WeakHashMap<>());
+    private final Set<OnVerified> mOnVerified = Collections.newSetFromMap(new WeakHashMap<>());
+
     QuickConversationsService(XmppConnectionService xmppConnectionService) {
         this.service = xmppConnectionService;
     }
+
+    public void addOnVerificationRequestedListener(OnVerificationRequested onVerificationRequested) {
+        synchronized (mOnVerificationRequested) {
+            mOnVerificationRequested.add(onVerificationRequested);
+        }
+    }
+
+    public void removeOnVerificationRequestedListener(OnVerificationRequested onVerificationRequested) {
+        synchronized (mOnVerificationRequested) {
+            mOnVerificationRequested.remove(onVerificationRequested);
+        }
+    }
+
+    public void requestVerification(Phonenumber.PhoneNumber phoneNumber) {
+        String local = PhoneNumberUtilWrapper.normalize(service, phoneNumber);
+        Log.d(Config.LOGTAG,"requesting verification for "+PhoneNumberUtilWrapper.normalize(service,phoneNumber));
+        Account account = new Account(Jid.of(local,"quick.conversations.im",null),"foo");
+        service.createAccount(account);
+        synchronized (mOnVerificationRequested) {
+            for(OnVerificationRequested onVerificationRequested : mOnVerificationRequested) {
+                onVerificationRequested.onVerificationRequested();
+            }
+        }
+    }
+
+    public interface OnVerificationRequested {
+        void onVerificationRequestFailed(int code);
+        void onVerificationRequested();
+    }
+
+    public interface OnVerified {
+        void onVerificationFailed();
+        void onVerificationSucceeded();
+    }
 }

src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java 🔗

@@ -18,13 +18,14 @@ import android.widget.TextView;
 import eu.siacs.conversations.Config;
 import eu.siacs.conversations.R;
 import eu.siacs.conversations.databinding.ActivityEnterNumberBinding;
+import eu.siacs.conversations.services.QuickConversationsService;
 import eu.siacs.conversations.ui.drawable.TextDrawable;
 import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
 import io.michaelrocks.libphonenumber.android.NumberParseException;
 import io.michaelrocks.libphonenumber.android.PhoneNumberUtil;
 import io.michaelrocks.libphonenumber.android.Phonenumber;
 
-public class EnterPhoneNumberActivity extends XmppActivity {
+public class EnterPhoneNumberActivity extends XmppActivity implements QuickConversationsService.OnVerificationRequested {
 
     private static final int REQUEST_CHOOSE_COUNTRY = 0x1234;
 
@@ -69,7 +70,7 @@ public class EnterPhoneNumberActivity extends XmppActivity {
 
     @Override
     void onBackendConnected() {
-
+        xmppConnectionService.getQuickConversationsService().addOnVerificationRequestedListener(this);
     }
 
     @Override
@@ -100,6 +101,14 @@ public class EnterPhoneNumberActivity extends XmppActivity {
         super.onSaveInstanceState(savedInstanceState);
     }
 
+    @Override
+    public void onStop() {
+        if (xmppConnectionService != null) {
+            xmppConnectionService.getQuickConversationsService().removeOnVerificationRequestedListener(this);
+        }
+        super.onStop();
+    }
+
     private void onNextClick(View v) {
         final AlertDialog.Builder builder = new AlertDialog.Builder(this);
         try {
@@ -133,7 +142,7 @@ public class EnterPhoneNumberActivity extends XmppActivity {
     }
 
     private void onPhoneNumberEntered(Phonenumber.PhoneNumber phoneNumber) {
-
+        xmppConnectionService.getQuickConversationsService().requestVerification(phoneNumber);
     }
 
     @Override
@@ -149,4 +158,13 @@ public class EnterPhoneNumberActivity extends XmppActivity {
         }
     }
 
+    @Override
+    public void onVerificationRequestFailed(int code) {
+
+    }
+
+    @Override
+    public void onVerificationRequested() {
+        Log.d(Config.LOGTAG,"requested");
+    }
 }

src/quick/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java 🔗

@@ -41,8 +41,11 @@ public class PhoneNumberUtilWrapper {
     }
 
     public static String normalize(Context context, String number) throws NumberParseException {
-        final PhoneNumberUtil instance = getInstance(context);
-        return instance.format(instance.parse(number, getUserCountry(context)), PhoneNumberUtil.PhoneNumberFormat.E164);
+        return normalize(context, getInstance(context).parse(number, getUserCountry(context)));
+    }
+
+    public static String normalize(Context context, Phonenumber.PhoneNumber phoneNumber) {
+        return getInstance(context).format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
     }
 
     public static PhoneNumberUtil getInstance(final Context context) {