Trouble warming up Chrome CustomTabs with latest androidx browser dependency

Viewed 535

I'm looking for an example of how to use the CustomTabs "warm up" functionality to preload Chrome when the host application is initialized. Documentation around using Chrome CustomTabs for loading URLs is woefully incomplete, and all the examples out there are from 5 years ago and use ugly callback mechanisms to accomplish this.

In the latest version (2.2.0), I was able to find this method that looked promising:

     * Connects to the Custom Tabs warmup service, and initializes the browser.
     *
     * This convenience method connects to the service, and immediately warms up the Custom Tabs
     * implementation. Since service connection is asynchronous, the return code is not the return
     * code of warmup.
     * This call is optional, and clients are encouraged to connect to the service, call
     * <code>warmup()</code> and create a session. In this case, calling this method is not
     * necessary.
     *
     * @param context     {@link Context} to use to connect to the remote service.
     * @param packageName Package name of the target implementation.
     * @return Whether the binding was successful.
     */
    public static boolean connectAndInitialize(@NonNull Context context,
            @NonNull String packageName) {
        if (packageName == null) return false;
        final Context applicationContext = context.getApplicationContext();
        CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
            @Override
            public final void onCustomTabsServiceConnected(
                    @NonNull ComponentName name, @NonNull CustomTabsClient client) {
                client.warmup(0);
                // Unbinding immediately makes the target process "Empty", provided that it is
                // not used by anyone else, and doesn't contain any Activity. This makes it
                // likely to get killed, but is preferable to keeping the connection around.
                applicationContext.unbindService(this);
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) { }
        };
        try {
            return bindCustomTabsService(applicationContext, packageName, connection);
        } catch (SecurityException e) {
            return false;
        }
    }

But the warmup method doesn't ever get called and performance doesn't seem to be improved. I tried calling this in the Application class as well as the Activity classes that I'd be loading URLs from.

I'm loading URLs using CustomTabsClient.Builder()..build().launchUrl(url)

0 Answers
Related