From bab6dc3ec06217b45675c15edf1ed7d9c9d431f6 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Tue, 3 Apr 2018 21:19:15 +0200 Subject: made qr code scanner work in choose contact --- .../pixart/messenger/ui/ChooseContactActivity.java | 53 ++++++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to 'src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java') diff --git a/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java b/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java index 647330c71..a6203215a 100644 --- a/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java +++ b/src/main/java/de/pixart/messenger/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 de.pixart.messenger.entities.Contact; import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.entities.ListItem; import de.pixart.messenger.entities.MucOptions; +import de.pixart.messenger.ui.util.ActivityResult; +import de.pixart.messenger.ui.util.PendingItem; +import de.pixart.messenger.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<>(); @@ -149,7 +155,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); } @@ -223,11 +229,18 @@ 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, xmppConnectionService.multipleAccounts() + this, + mKnownHosts, + mActivatedAccounts, + getString(R.string.enter_contact), + getString(R.string.select), + jid == null ? null : jid.asBareJid().toString(), + getIntent().getStringExtra(EXTRA_ACCOUNT), + true, + xmppConnectionService.multipleAccounts() ); dialog.setOnEnterJidDialogPositiveListener((accountJid, contactJid) -> { @@ -248,6 +261,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(); @@ -263,5 +297,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); } } -- cgit v1.2.3