From 7e963820a5559812983d2b737d7862d122f5c9de Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 7 Mar 2018 12:56:35 +0100 Subject: [PATCH] made qr code scanner work in choose contact --- .../ui/ChooseContactActivity.java | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ChooseContactActivity.java b/src/main/java/eu/siacs/conversations/ui/ChooseContactActivity.java index 44802fa33..4d26370b2 100644 --- a/src/main/java/eu/siacs/conversations/ui/ChooseContactActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ChooseContactActivity.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.StringRes; import android.support.v7.app.ActionBar; import android.view.ActionMode; @@ -28,6 +29,9 @@ import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.entities.MucOptions; +import eu.siacs.conversations.ui.util.ActivityResult; +import eu.siacs.conversations.ui.util.PendingItem; +import eu.siacs.conversations.utils.XmppUri; import rocks.xmpp.addr.Jid; public class ChooseContactActivity extends AbstractSearchableListItemActivity { @@ -37,6 +41,8 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity { private Set selected; private Set filterContacts; + private PendingItem postponedActivityResult = new PendingItem<>(); + public static Intent create(Activity activity, Conversation conversation) { final Intent intent = new Intent(activity, ChooseContactActivity.class); List contacts = new ArrayList<>(); @@ -148,7 +154,7 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity { final Intent i = getIntent(); boolean showEnterJid = i != null && i.getBooleanExtra("show_enter_jid", false); if (showEnterJid) { - this.binding.fab.setOnClickListener((v) -> showEnterJidDialog()); + this.binding.fab.setOnClickListener((v) -> showEnterJidDialog(null)); } else { this.binding.fab.setVisibility(View.GONE); } @@ -222,11 +228,17 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity { return super.onOptionsItemSelected(item); } - protected void showEnterJidDialog() { + protected void showEnterJidDialog(XmppUri uri) { + Jid jid = uri == null ? null : uri.getJid(); EnterJidDialog dialog = new EnterJidDialog( - this, mKnownHosts, mActivatedAccounts, - getString(R.string.enter_contact), getString(R.string.select), - null, getIntent().getStringExtra(EXTRA_ACCOUNT), true + this, + mKnownHosts, + mActivatedAccounts, + getString(R.string.enter_contact), + getString(R.string.select), + jid == null ? null : jid.asBareJid().toString(), + getIntent().getStringExtra(EXTRA_ACCOUNT), + true ); dialog.setOnEnterJidDialogPositiveListener((accountJid, contactJid) -> { @@ -247,6 +259,27 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity { dialog.show(); } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + super.onActivityResult(requestCode, requestCode, intent); + ActivityResult activityResult = ActivityResult.of(requestCode, resultCode, intent); + if (xmppConnectionService != null) { + handleActivityResult(activityResult); + } else { + this.postponedActivityResult.push(activityResult); + } + } + + private void handleActivityResult(ActivityResult activityResult) { + if (activityResult.resultCode == RESULT_OK && activityResult.requestCode == ScanActivity.REQUEST_SCAN_QR_CODE) { + String result = activityResult.data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT); + XmppUri uri = new XmppUri(result == null ? "" : result); + if (uri.isJidValid()) { + showEnterJidDialog(uri); + } + } + } + @Override void onBackendConnected() { filterContacts(); @@ -261,5 +294,14 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity { } } this.mKnownHosts = xmppConnectionService.getKnownHosts(); + ActivityResult activityResult = this.postponedActivityResult.pop(); + if (activityResult != null) { + handleActivityResult(activityResult); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { + ScanActivity.onRequestPermissionResult(this, requestCode, grantResults); } }