QR code scan: take disabled accounts into consideration

when making register or add contact decision
This commit is contained in:
Daniel Gultsch 2023-10-10 09:35:59 +02:00
parent c53e035935
commit 39194d111c
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -18,13 +18,6 @@ import androidx.databinding.DataBindingUtil;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.ActivityUriHandlerBinding; import eu.siacs.conversations.databinding.ActivityUriHandlerBinding;
@ -35,12 +28,20 @@ import eu.siacs.conversations.utils.ProvisioningUtils;
import eu.siacs.conversations.utils.SignupUtils; import eu.siacs.conversations.utils.SignupUtils;
import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.utils.XmppUri;
import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.Jid;
import okhttp3.Call; import okhttp3.Call;
import okhttp3.Callback; import okhttp3.Callback;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UriHandlerActivity extends AppCompatActivity { public class UriHandlerActivity extends AppCompatActivity {
public static final String ACTION_SCAN_QR_CODE = "scan_qr_code"; public static final String ACTION_SCAN_QR_CODE = "scan_qr_code";
@ -58,7 +59,9 @@ public class UriHandlerActivity extends AppCompatActivity {
} }
public static void scan(final Activity activity, final boolean provisioning) { public static void scan(final Activity activity, final boolean provisioning) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
|| ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
final Intent intent = new Intent(activity, UriHandlerActivity.class); final Intent intent = new Intent(activity, UriHandlerActivity.class);
intent.setAction(UriHandlerActivity.ACTION_SCAN_QR_CODE); intent.setAction(UriHandlerActivity.ACTION_SCAN_QR_CODE);
if (provisioning) { if (provisioning) {
@ -69,13 +72,16 @@ public class UriHandlerActivity extends AppCompatActivity {
} else { } else {
activity.requestPermissions( activity.requestPermissions(
new String[] {Manifest.permission.CAMERA}, new String[] {Manifest.permission.CAMERA},
provisioning ? REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION : REQUEST_CAMERA_PERMISSIONS_TO_SCAN provisioning
); ? REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION
: REQUEST_CAMERA_PERMISSIONS_TO_SCAN);
} }
} }
public static void onRequestPermissionResult(Activity activity, int requestCode, int[] grantResults) { public static void onRequestPermissionResult(
if (requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN && requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION) { Activity activity, int requestCode, int[] grantResults) {
if (requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN
&& requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION) {
return; return;
} }
if (grantResults.length > 0) { if (grantResults.length > 0) {
@ -86,7 +92,11 @@ public class UriHandlerActivity extends AppCompatActivity {
scan(activity); scan(activity);
} }
} else { } else {
Toast.makeText(activity, R.string.qr_code_scanner_needs_access_to_camera, Toast.LENGTH_SHORT).show(); Toast.makeText(
activity,
R.string.qr_code_scanner_needs_access_to_camera,
Toast.LENGTH_SHORT)
.show();
} }
} }
} }
@ -116,7 +126,7 @@ public class UriHandlerActivity extends AppCompatActivity {
private boolean handleUri(final Uri uri, final boolean scanned) { private boolean handleUri(final Uri uri, final boolean scanned) {
final Intent intent; final Intent intent;
final XmppUri xmppUri = new XmppUri(uri); final XmppUri xmppUri = new XmppUri(uri);
final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(true); final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(false);
if (SignupUtils.isSupportTokenRegistry() && xmppUri.isValidJid()) { if (SignupUtils.isSupportTokenRegistry() && xmppUri.isValidJid()) {
final String preAuth = xmppUri.getParameter(XmppUri.PARAMETER_PRE_AUTH); final String preAuth = xmppUri.getParameter(XmppUri.PARAMETER_PRE_AUTH);
@ -130,7 +140,12 @@ public class UriHandlerActivity extends AppCompatActivity {
startActivity(intent); startActivity(intent);
return true; return true;
} }
if (accounts.size() == 0 && xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParameter(XmppUri.PARAMETER_IBR))) { if (accounts.size() == 0
&& xmppUri.isAction(XmppUri.ACTION_ROSTER)
&& "y"
.equalsIgnoreCase(
Strings.nullToEmpty(xmppUri.getParameter(XmppUri.PARAMETER_IBR))
.trim())) {
intent = SignupUtils.getTokenRegistrationIntent(this, jid.getDomain(), preAuth); intent = SignupUtils.getTokenRegistrationIntent(this, jid.getDomain(), preAuth);
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString()); intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
startActivity(intent); startActivity(intent);
@ -197,11 +212,11 @@ public class UriHandlerActivity extends AppCompatActivity {
private void checkForLinkHeader(final HttpUrl url) { private void checkForLinkHeader(final HttpUrl url) {
Log.d(Config.LOGTAG, "checking for link header on " + url); Log.d(Config.LOGTAG, "checking for link header on " + url);
this.call = HttpConnectionManager.OK_HTTP_CLIENT.newCall(new Request.Builder() this.call =
.url(url) HttpConnectionManager.OK_HTTP_CLIENT.newCall(
.head() new Request.Builder().url(url).head().build());
.build()); this.call.enqueue(
this.call.enqueue(new Callback() { new Callback() {
@Override @Override
public void onFailure(@NotNull Call call, @NotNull IOException e) { public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.d(Config.LOGTAG, "unable to check HTTP url", e); Log.d(Config.LOGTAG, "unable to check HTTP url", e);
@ -219,7 +234,6 @@ public class UriHandlerActivity extends AppCompatActivity {
showError(R.string.no_xmpp_adddress_found); showError(R.string.no_xmpp_adddress_found);
} }
}); });
} }
private boolean processLinkHeader(final String header) { private boolean processLinkHeader(final String header) {
@ -256,7 +270,8 @@ public class UriHandlerActivity extends AppCompatActivity {
} }
switch (action) { switch (action) {
case Intent.ACTION_MAIN: case Intent.ACTION_MAIN:
binding.progress.setVisibility(call != null && !call.isCanceled() ? View.VISIBLE : View.INVISIBLE); binding.progress.setVisibility(
call != null && !call.isCanceled() ? View.VISIBLE : View.INVISIBLE);
break; break;
case Intent.ACTION_VIEW: case Intent.ACTION_VIEW:
case Intent.ACTION_SENDTO: case Intent.ACTION_SENDTO:
@ -280,7 +295,8 @@ public class UriHandlerActivity extends AppCompatActivity {
private boolean allowProvisioning() { private boolean allowProvisioning() {
final Intent launchIntent = getIntent(); final Intent launchIntent = getIntent();
return launchIntent != null && launchIntent.getBooleanExtra(EXTRA_ALLOW_PROVISIONING, false); return launchIntent != null
&& launchIntent.getBooleanExtra(EXTRA_ALLOW_PROVISIONING, false);
} }
@Override @Override
@ -303,13 +319,17 @@ public class UriHandlerActivity extends AppCompatActivity {
showError(R.string.no_xmpp_adddress_found); showError(R.string.no_xmpp_adddress_found);
} }
return; return;
} else if (QuickConversationsService.isConversations() && looksLikeJsonObject(result) && allowProvisioning) { } else if (QuickConversationsService.isConversations()
&& looksLikeJsonObject(result)
&& allowProvisioning) {
ProvisioningUtils.provision(this, result); ProvisioningUtils.provision(this, result);
finish(); finish();
return; return;
} }
final Uri uri = Uri.parse(result.trim()); final Uri uri = Uri.parse(result.trim());
if (allowProvisioning && "https".equalsIgnoreCase(uri.getScheme()) && !XmppUri.INVITE_DOMAIN.equalsIgnoreCase(uri.getHost())) { if (allowProvisioning
&& "https".equalsIgnoreCase(uri.getScheme())
&& !XmppUri.INVITE_DOMAIN.equalsIgnoreCase(uri.getHost())) {
final HttpUrl httpUrl = HttpUrl.parse(uri.toString()); final HttpUrl httpUrl = HttpUrl.parse(uri.toString());
if (httpUrl != null) { if (httpUrl != null) {
checkForLinkHeader(httpUrl); checkForLinkHeader(httpUrl);