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

Check if a browser supports Custom Tabs before trying to open an URL #2282

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -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,26 +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;
call.resolve();
} catch (ActivityNotFoundException ex) {
Logger.error(getLogTag(), ex.getLocalizedMessage(), null);
call.reject("Unable to display URL");
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();
} else {
call.reject("No application can handle the URL");
}
);
}
}

@PluginMethod
Expand Down