From 954acb377e17a258d8081b4d85c637e39cac2213 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 19 Oct 2018 23:29:17 +0200 Subject: [PATCH] WIP QuickConversationsService --- .../services/XmppConnectionService.java | 8 ++- .../services/QuickConversationsService.java | 50 ++++++++++++++++++- .../ui/EnterPhoneNumberActivity.java | 24 +++++++-- .../utils/PhoneNumberUtilWrapper.java | 7 ++- 4 files changed, 81 insertions(+), 8 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index e08b3b428..8ea0582d1 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/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 findContacts(Jid jid, String accountJid) { ArrayList contacts = new ArrayList<>(); for (Account account : getAccounts()) { diff --git a/src/quick/java/eu/siacs/conversations/services/QuickConversationsService.java b/src/quick/java/eu/siacs/conversations/services/QuickConversationsService.java index 896c07c53..36f45f28f 100644 --- a/src/quick/java/eu/siacs/conversations/services/QuickConversationsService.java +++ b/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 mOnVerificationRequested = Collections.newSetFromMap(new WeakHashMap<>()); + private final Set 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(); + } } \ No newline at end of file diff --git a/src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java b/src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java index 5195f0f1b..77c5ff423 100644 --- a/src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java +++ b/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"); + } } diff --git a/src/quick/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java b/src/quick/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java index 03c53097a..c25424277 100644 --- a/src/quick/java/eu/siacs/conversations/utils/PhoneNumberUtilWrapper.java +++ b/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) {