From 39194d111cdf2c9cd1a980ebac0626da33d03645 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 10 Oct 2023 09:35:59 +0200 Subject: [PATCH] QR code scan: take disabled accounts into consideration when making register or add contact decision --- .../conversations/ui/UriHandlerActivity.java | 102 +++++++++++------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java b/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java index 1e0cf41d3..de284264d 100644 --- a/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java @@ -18,13 +18,6 @@ import androidx.databinding.DataBindingUtil; 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.R; 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.XmppUri; import eu.siacs.conversations.xmpp.Jid; + import okhttp3.Call; import okhttp3.Callback; import okhttp3.HttpUrl; import okhttp3.Request; 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 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) { - 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); intent.setAction(UriHandlerActivity.ACTION_SCAN_QR_CODE); if (provisioning) { @@ -68,14 +71,17 @@ public class UriHandlerActivity extends AppCompatActivity { activity.startActivity(intent); } else { activity.requestPermissions( - new String[]{Manifest.permission.CAMERA}, - provisioning ? REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION : REQUEST_CAMERA_PERMISSIONS_TO_SCAN - ); + new String[] {Manifest.permission.CAMERA}, + provisioning + ? REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION + : REQUEST_CAMERA_PERMISSIONS_TO_SCAN); } } - public static void onRequestPermissionResult(Activity activity, int requestCode, int[] grantResults) { - if (requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN && requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION) { + public static void onRequestPermissionResult( + Activity activity, int requestCode, int[] grantResults) { + if (requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN + && requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION) { return; } if (grantResults.length > 0) { @@ -86,7 +92,11 @@ public class UriHandlerActivity extends AppCompatActivity { scan(activity); } } 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) { final Intent intent; final XmppUri xmppUri = new XmppUri(uri); - final List accounts = DatabaseBackend.getInstance(this).getAccountJids(true); + final List accounts = DatabaseBackend.getInstance(this).getAccountJids(false); if (SignupUtils.isSupportTokenRegistry() && xmppUri.isValidJid()) { final String preAuth = xmppUri.getParameter(XmppUri.PARAMETER_PRE_AUTH); @@ -130,7 +140,12 @@ public class UriHandlerActivity extends AppCompatActivity { startActivity(intent); 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.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString()); startActivity(intent); @@ -197,29 +212,28 @@ public class UriHandlerActivity extends AppCompatActivity { private void checkForLinkHeader(final HttpUrl url) { Log.d(Config.LOGTAG, "checking for link header on " + url); - this.call = HttpConnectionManager.OK_HTTP_CLIENT.newCall(new Request.Builder() - .url(url) - .head() - .build()); - this.call.enqueue(new Callback() { - @Override - public void onFailure(@NotNull Call call, @NotNull IOException e) { - Log.d(Config.LOGTAG, "unable to check HTTP url", e); - showError(R.string.no_xmpp_adddress_found); - } - - @Override - public void onResponse(@NotNull Call call, @NotNull Response response) { - if (response.isSuccessful()) { - final String linkHeader = response.header("Link"); - if (linkHeader != null && processLinkHeader(linkHeader)) { - return; + this.call = + HttpConnectionManager.OK_HTTP_CLIENT.newCall( + new Request.Builder().url(url).head().build()); + this.call.enqueue( + new Callback() { + @Override + public void onFailure(@NotNull Call call, @NotNull IOException e) { + Log.d(Config.LOGTAG, "unable to check HTTP url", e); + showError(R.string.no_xmpp_adddress_found); } - } - showError(R.string.no_xmpp_adddress_found); - } - }); + @Override + public void onResponse(@NotNull Call call, @NotNull Response response) { + if (response.isSuccessful()) { + final String linkHeader = response.header("Link"); + if (linkHeader != null && processLinkHeader(linkHeader)) { + return; + } + } + showError(R.string.no_xmpp_adddress_found); + } + }); } private boolean processLinkHeader(final String header) { @@ -256,7 +270,8 @@ public class UriHandlerActivity extends AppCompatActivity { } switch (action) { 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; case Intent.ACTION_VIEW: case Intent.ACTION_SENDTO: @@ -280,7 +295,8 @@ public class UriHandlerActivity extends AppCompatActivity { private boolean allowProvisioning() { final Intent launchIntent = getIntent(); - return launchIntent != null && launchIntent.getBooleanExtra(EXTRA_ALLOW_PROVISIONING, false); + return launchIntent != null + && launchIntent.getBooleanExtra(EXTRA_ALLOW_PROVISIONING, false); } @Override @@ -303,13 +319,17 @@ public class UriHandlerActivity extends AppCompatActivity { showError(R.string.no_xmpp_adddress_found); } return; - } else if (QuickConversationsService.isConversations() && looksLikeJsonObject(result) && allowProvisioning) { + } else if (QuickConversationsService.isConversations() + && looksLikeJsonObject(result) + && allowProvisioning) { ProvisioningUtils.provision(this, result); finish(); return; } 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()); if (httpUrl != null) { checkForLinkHeader(httpUrl);