From a8d1d3df9f151251518ff77ff3799a3003b548f9 Mon Sep 17 00:00:00 2001 From: Thuong Tran Date: Thu, 30 Jul 2020 11:12:11 +0700 Subject: [PATCH 1/2] add default browser for custom tab if any --- .../inappbrowser/RNInAppBrowser.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java b/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java index d42172f..10f2267 100644 --- a/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java +++ b/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java @@ -11,6 +11,7 @@ import android.graphics.BitmapFactory; import android.provider.Browser; import androidx.annotation.Nullable; +import androidx.browser.customtabs.CustomTabsClient; import androidx.browser.customtabs.CustomTabsIntent; import androidx.core.graphics.ColorUtils; @@ -25,6 +26,7 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import java.util.Arrays; import java.util.regex.Pattern; import java.util.List; @@ -44,6 +46,9 @@ public class RNInAppBrowser { private static final String KEY_ANIMATION_END_EXIT = "endExit"; private static final String HASBACKBUTTON = "hasBackButton"; + private static final String ACTION_CUSTOM_TABS_CONNECTION = "android.support.customtabs.action.CustomTabsService"; + private static final String CHROME_PACKAGE = "com.android.chrome"; + private @Nullable Promise mOpenBrowserPromise; private Boolean isLightTheme; private Activity currentActivity; @@ -136,8 +141,8 @@ public void open(Context context, final ReadableMap options, final Promise promi customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); } - Intent intent = customTabsIntent.intent; + setDefaultBrowser(currentActivity, intent); intent.setData(Uri.parse(url)); if (options.hasKey(KEY_SHOW_PAGE_TITLE)) { builder.setShowTitle(options.getBoolean(KEY_SHOW_PAGE_TITLE)); @@ -175,11 +180,30 @@ public void close() { } public void isAvailable(Context context, final Promise promise) { - Intent serviceIntent = new Intent("android.support.customtabs.action.CustomTabsService"); - List resolveInfos = context.getPackageManager().queryIntentServices(serviceIntent, 0); + List resolveInfos = getPreferedPackages(context); promise.resolve(!(resolveInfos == null || resolveInfos.isEmpty())); } + private List getPreferedPackages(Context context){ + Intent serviceIntent = new Intent(ACTION_CUSTOM_TABS_CONNECTION); + List resolveInfos = context.getPackageManager().queryIntentServices(serviceIntent, 0); + return resolveInfos; + } + + private void setDefaultBrowser(Context context, Intent intent){ + List resolveInfos = getPreferedPackages(context); + String packageName = CustomTabsClient.getPackageName(context, Arrays.asList(CHROME_PACKAGE)); + if(packageName == null && resolveInfos != null && resolveInfos.size() > 0){ + packageName = resolveInfos.get(0).serviceInfo.packageName; + } + + try{ + intent.setPackage(packageName); + }catch (Exception ex){ + ex.printStackTrace(); + } + } + @Subscribe public void onEvent(ChromeTabsDismissedEvent event) { unRegisterEventBus(); From fe2378f941d295c0e6f85e4b2a34670e70ce196a Mon Sep 17 00:00:00 2001 From: Thuong Tran Date: Mon, 10 Aug 2020 11:12:16 +0700 Subject: [PATCH 2/2] add more chrome packages and local package --- .../java/com/proyecto26/inappbrowser/RNInAppBrowser.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java b/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java index 10f2267..9648660 100644 --- a/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java +++ b/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java @@ -47,7 +47,10 @@ public class RNInAppBrowser { private static final String HASBACKBUTTON = "hasBackButton"; private static final String ACTION_CUSTOM_TABS_CONNECTION = "android.support.customtabs.action.CustomTabsService"; - private static final String CHROME_PACKAGE = "com.android.chrome"; + private static final String CHROME_PACKAGE_STABLE = "com.android.chrome"; + private static final String CHROME_PACKAGE_BETA = "com.chrome.beta"; + private static final String CHROME_PACKAGE_DEV = "com.chrome.dev"; + private static final String LOCAL_PACKAGE = "com.google.android.apps.chrome"; private @Nullable Promise mOpenBrowserPromise; private Boolean isLightTheme; @@ -192,7 +195,7 @@ private List getPreferedPackages(Context context){ private void setDefaultBrowser(Context context, Intent intent){ List resolveInfos = getPreferedPackages(context); - String packageName = CustomTabsClient.getPackageName(context, Arrays.asList(CHROME_PACKAGE)); + String packageName = CustomTabsClient.getPackageName(context, Arrays.asList(CHROME_PACKAGE_STABLE, CHROME_PACKAGE_BETA, CHROME_PACKAGE_DEV, LOCAL_PACKAGE)); if(packageName == null && resolveInfos != null && resolveInfos.size() > 0){ packageName = resolveInfos.get(0).serviceInfo.packageName; }