From 4b6e4c3d4d495431aaee8f83938bc0e6154c2c9a Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Sat, 8 Oct 2022 00:38:40 -0700 Subject: [PATCH] [android] Clean up CustomTabs stuff We should be fine using the default behaviour without all the package querying stuff (that apparently has been broken since Android 11?) --- src/android/OAuthPlugin.java | 144 +++-------------------------------- 1 file changed, 9 insertions(+), 135 deletions(-) diff --git a/src/android/OAuthPlugin.java b/src/android/OAuthPlugin.java index ce64e1e..0b05b88 100644 --- a/src/android/OAuthPlugin.java +++ b/src/android/OAuthPlugin.java @@ -16,18 +16,9 @@ package com.ayogo.cordova.oauth; -import android.app.Activity; -import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; import android.net.Uri; import androidx.browser.customtabs.CustomTabsIntent; -import android.text.TextUtils; - -import java.util.ArrayList; -import java.util.List; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaArgs; @@ -38,7 +29,6 @@ import org.apache.cordova.LOG; import org.apache.cordova.PluginResult; -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -46,22 +36,6 @@ public class OAuthPlugin extends CordovaPlugin { private final String TAG = "OAuthPlugin"; - // Taken from Google's CustomTabsHelper - // https://github.com/GoogleChrome/custom-tabs-client/blob/da65829d7d4b80c00809c6c4aa7f61f88fc7ca26/shared/src/main/java/org/chromium/customtabsclient/shared/CustomTabsHelper.java - static final String STABLE_PACKAGE = "com.android.chrome"; - static final String BETA_PACKAGE = "com.chrome.beta"; - static final String DEV_PACKAGE = "com.chrome.dev"; - static final String LOCAL_PACKAGE = "com.google.android.apps.chrome"; - private static final String EXTRA_CUSTOM_TABS_KEEP_ALIVE = "android.support.customtabs.extra.KEEP_ALIVE"; - private static final String ACTION_CUSTOM_TABS_CONNECTION = "android.support.customtabs.action.CustomTabsService"; - - - /** - * The name of the package to use for the custom tab service. - */ - private String tabProvider = null; - - /** * Executes the request. * @@ -122,15 +96,7 @@ public void onNewIntent(Intent intent) { jsobj.put(queryKey, uri.getQueryParameter(queryKey)); } - final String msg = jsobj.toString(); - CordovaWebViewEngine engine = this.webView.getEngine(); - final String jsCode = "window.dispatchEvent(new MessageEvent('message', { data: 'oauth::" + msg + "' }));"; - if (engine != null) { - engine.evaluateJavascript(jsCode, null); - } else { - this.webView.sendJavascript(jsCode); - } - + dispatchOAuthMessage(jsobj.toString()); } catch (JSONException e) { LOG.e(TAG, "JSON Serialization failed"); e.printStackTrace(); @@ -145,112 +111,20 @@ public void onNewIntent(Intent intent) { * @param url The URL of the OAuth endpoint. */ private void startOAuth(String url) { - String customTabsBrowser = findCustomTabProvider(); - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); - - String packageName = this.findCustomTabProvider(); - if (packageName != null) { - customTabsIntent.intent.setPackage(packageName); - } - customTabsIntent.launchUrl(this.cordova.getActivity(), Uri.parse(url)); } + @SuppressWarnings("deprecation") + private void dispatchOAuthMessage(final String msg) { + final String jsCode = "window.dispatchEvent(new MessageEvent('message', { data: 'oauth::" + msg + "' }));"; - /** - * Goes through all apps that handle VIEW intents and have a warmup service. - * - * Picks the one chosen by the user if there is one, otherwise makes a best - * effort to return a valid package name. - * - * This is not threadsafe. - * - * @return The package name recommended to use for connecting to custom - * tabs related components. - */ - private String findCustomTabProvider() { - if (this.tabProvider != null) { - return this.tabProvider; - } - - PackageManager pm = this.cordova.getActivity().getPackageManager(); - - // Get default VIEW intent handler. - Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")); - ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0); - String defaultViewHandlerPackageName = null; - - if (defaultViewHandlerInfo != null) { - defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName; + CordovaWebViewEngine engine = this.webView.getEngine(); + if (engine != null) { + engine.evaluateJavascript(jsCode, null); + } else { + this.webView.sendJavascript(jsCode); } - - - // Get all apps that can handle VIEW intents. - List resolvedActivityList = pm.queryIntentActivities(activityIntent, PackageManager.MATCH_ALL); - List packagesSupportingCustomTabs = new ArrayList<>(); - - for (ResolveInfo info : resolvedActivityList) { - Intent serviceIntent = new Intent(); - serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION); - serviceIntent.setPackage(info.activityInfo.packageName); - - if (pm.resolveService(serviceIntent, 0) != null) { - packagesSupportingCustomTabs.add(info.activityInfo.packageName); - } - } - - // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents - // and service calls. - if (packagesSupportingCustomTabs.isEmpty()) { - this.tabProvider = null; - } else if (packagesSupportingCustomTabs.size() == 1) { - this.tabProvider = packagesSupportingCustomTabs.get(0); - } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName) && !this.hasSpecializedHandlerIntents(activityIntent) && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) { - this.tabProvider = defaultViewHandlerPackageName; - } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) { - this.tabProvider = STABLE_PACKAGE; - } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) { - this.tabProvider = BETA_PACKAGE; - } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) { - this.tabProvider = DEV_PACKAGE; - } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) { - this.tabProvider = LOCAL_PACKAGE; - } - - return this.tabProvider; - } - - - /** - * Used to check whether there is a specialized handler for a given intent. - * - * @param intent The intent to check with. - * @return Whether there is a specialized handler for the given intent. - */ - private boolean hasSpecializedHandlerIntents(Intent intent) { - try { - PackageManager pm = this.cordova.getActivity().getPackageManager(); - List handlers = pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER); - - if (handlers == null || handlers.size() == 0) { - return false; - } - - for (ResolveInfo resolveInfo : handlers) { - IntentFilter filter = resolveInfo.filter; - - if (filter == null || filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0 || resolveInfo.activityInfo == null) { - continue; - } - - return true; - } - } catch (RuntimeException e) { - LOG.e(TAG, "Runtime exception while getting specialized handlers"); - } - - return false; } }