From 4dc656f8c27127252963d67df1f7d21b5a4d377d Mon Sep 17 00:00:00 2001 From: maiconcarraro Date: Sat, 13 Jan 2024 20:31:20 -0300 Subject: [PATCH] set preferred browsers over possible providers --- .../trusted/TwaProviderPicker.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/TwaProviderPicker.java b/androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/TwaProviderPicker.java index 7146a166..f1572a23 100644 --- a/androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/TwaProviderPicker.java +++ b/androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/TwaProviderPicker.java @@ -65,6 +65,26 @@ public class TwaProviderPicker { private static final String TAG = "TWAProviderPicker"; private static String sPackageNameForTesting; + private static String[] preferredProviders = { + "com.android.chrome", // CHROME + "com.chrome.beta", // CHROME_BETA + "com.chrome.dev", // CHROME_DEV + "com.chrome.canary", // CHROME_CANARY + "com.google.android.apps.chrome", // CHROME_LOCAL_BUILD + "org.chromium.chrome", // CHROMIUM_LOCAL_BUILD + "com.microsoft.emmx", // MICROSOFT_EDGE + "org.mozilla.firefox", // FIREFOX + "org.mozilla.firefox_beta", // FIREFOX_BETA + "org.mozilla.fennec_aurora", // FIREFOX_AURORA + "org.mozilla.fennec", // FIREFOX_FENNEC_NIGHTLY + "org.mozilla.fennec_fdroid", // FIREFOX_FDROID + "org.mozilla.rocket", // FIREFOX_LITE + "org.mozilla.fenix", // FIREFOX_NIGHTLY + "com.opera.browser", // OPERA + "com.opera.browser.beta", // OPERA_BETA + "com.opera.mini.native", // OPERA_MINI + "com.opera.mini.native.beta" // OPERA_MINI_BETA + }; @IntDef({LaunchMode.TRUSTED_WEB_ACTIVITY, LaunchMode.CUSTOM_TAB, LaunchMode.BROWSER}) @Retention(RetentionPolicy.SOURCE) @@ -139,10 +159,37 @@ public static Action pickProvider(PackageManager pm) { } Map customTabsServices = getLaunchModesForCustomTabsServices(pm); + String preferredProviderName = ""; + + // Loop preferred providers to use as preference order + for (String preferredProvider : preferredProviders) { + for (ResolveInfo possibleProvider : possibleProviders) { + String providerName = possibleProvider.activityInfo.packageName; + + if (providerName.equals(preferredProvider)) { + Log.d(TAG, "Found provider from preferred providers list: " + providerName); + preferredProviderName = preferredProvider; + break; + } + } + + if (!preferredProviderName.equals("")) { + break; + } + } + + + if (preferredProviderName.equals("")) { + Log.d(TAG, "Provider not in preferred providers list"); + } for (ResolveInfo possibleProvider : possibleProviders) { String providerName = possibleProvider.activityInfo.packageName; + if (!preferredProviderName.equals("") && !providerName.equals(preferredProviderName)) { + continue; + } + @LaunchMode int launchMode = customTabsServices.containsKey(providerName) ? customTabsServices.get(providerName) : LaunchMode.BROWSER;