Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.browser.customtabs.*;
import java.util.ArrayList;
import java.util.List;

/**
* The Browser class implements Custom Chrome Tabs. See
Expand Down Expand Up @@ -81,6 +85,24 @@ public BrowserEventListener getBrowserEventListenerListener() {
return browserEventListener;
}

/**
* Check if custom tabs are supported.
* @return boolean
*/
public boolean areCustomTabsSupported() {
PackageManager packageManager = context.getPackageManager();
Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(activityIntent, PackageManager.MATCH_ALL);

List<String> packageNames = new ArrayList<>();
for (ResolveInfo info : resolveInfos) {
packageNames.add(info.activityInfo.packageName);
}

String packageName = CustomTabsClient.getPackageName(context, packageNames, true);
return packageName != null;
}

/**
* Open the browser to the specified URL.
* @param url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,37 @@ public void open(PluginCall call) {
Logger.error(getLogTag(), "Invalid color provided for toolbarColor. Using default", null);
}

// open the browser and finish

Intent intent = new Intent(getContext(), BrowserControllerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);
if (implementation.areCustomTabsSupported()) {
// open the browser and finish
Intent intent = new Intent(getContext(), BrowserControllerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);

Integer finalToolbarColor = toolbarColor;
setBrowserControllerListener((activity) -> {
try {
activity.open(implementation, url, finalToolbarColor);
browserControllerActivityInstance = activity;
Integer finalToolbarColor = toolbarColor;
setBrowserControllerListener(
activity -> {
try {
activity.open(implementation, url, finalToolbarColor);
browserControllerActivityInstance = activity;
call.resolve();
} catch (ActivityNotFoundException ex) {
Logger.error(getLogTag(), ex.getLocalizedMessage(), null);
call.reject("Unable to display URL");
}
}
);
} else {
// fallback to opening the URL in the default browser
Intent browserIntent = new Intent(Intent.ACTION_VIEW, url);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (browserIntent.resolveActivity(getContext().getPackageManager()) != null) {
getContext().startActivity(browserIntent);
call.resolve();
} catch (ActivityNotFoundException ex) {
Logger.error(getLogTag(), ex.getLocalizedMessage(), null);
call.reject("Unable to display URL");
} else {
call.reject("No application can handle the URL");
}
});
}
}

@PluginMethod
Expand Down