diff --git a/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java b/android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java index d42172f..9648660 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,12 @@ 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_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; private Activity currentActivity; @@ -136,8 +144,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 +183,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_STABLE, CHROME_PACKAGE_BETA, CHROME_PACKAGE_DEV, LOCAL_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();