made qr code scanner work in choose contact

This commit is contained in:
Daniel Gultsch 2018-03-07 12:56:35 +01:00
parent 69374f3e5f
commit 7e963820a5
1 changed files with 47 additions and 5 deletions

View File

@ -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<Contact> selected;
private Set<String> filterContacts;
private PendingItem<ActivityResult> postponedActivityResult = new PendingItem<>();
public static Intent create(Activity activity, Conversation conversation) {
final Intent intent = new Intent(activity, ChooseContactActivity.class);
List<String> 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);
}
}