Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default browser for custom tab if any #177

Merged
merged 2 commits into from
Oct 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentServices(serviceIntent, 0);
List<ResolveInfo> resolveInfos = getPreferedPackages(context);
promise.resolve(!(resolveInfos == null || resolveInfos.isEmpty()));
}

private List<ResolveInfo> getPreferedPackages(Context context){
Intent serviceIntent = new Intent(ACTION_CUSTOM_TABS_CONNECTION);
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentServices(serviceIntent, 0);
return resolveInfos;
}

private void setDefaultBrowser(Context context, Intent intent){
List<ResolveInfo> 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();
Expand Down