Android Browser Helper

repository·main·Indexed 21 days ago

https://github.com/googlechrome/android-browser-helper

A library that simplifies the implementation of Custom Tabs and Trusted Web Activities (TWA) on Android. It provides components for launching browsers, managing site settings, implementing ephemeral browsing with fallbacks, adding custom headers via Digital Asset Links, and handling TWA features such as location and notification delegation, multi-domain support, offline-first experiences, and Google Play Billing integration.

Tokens
4.6K
Snippets
10
Records
26
Agent score
74%

What's inside Android Browser Helper

  1. What is Android Browser Helper

    main

    Android Browser Helper is a library designed to help developers implement Custom Tabs and Trusted Web Activities (TWA) using the AndroidX browser support library. It provides pre-built implementations for common tasks, such as:

    • Creating a Launcher Activity specifically for launching a Trusted Web Activity.
    • Logic for selecting the most appropriate Custom Tabs provider on a device.
    • Creating an Activity that launches the browser's site settings for a specific TWA.
  2. Explore the Custom Tabs Navigation Callbacks Demo

    main

    The Custom Tabs Navigation Callbacks Demo demonstrates how to launch a Chrome Custom Tab and intercept navigation events occurring within that tab.

    Key features of this demo include:

    • Launching a Custom Tab via the Android Browser Helper library.
    • Receiving and processing navigation callbacks.
    • Reporting navigation events to the Android log and displaying them on the demo's launch screen.

    Note on Privacy: The navigation information reported by this demo does not include actual URLs; it is designed to facilitate the collection of performance statistics without compromising user privacy.

  3. Understand the TWA WebView Fallback demo

    main

    The TWA WebView Fallback demo provides an alternative to the default fallback behavior when a Trusted Web Activity (TWA) fails to start or when a TWA-compatible browser is unavailable.

    Instead of falling back to a Custom Tab or the default browser (which breaks the full-screen experience), this demo opens the content in a full-screen WebView. It is designed to handle TWA-specific requirements, including:

    • Navigation: Correctly handling users leaving the verified origin and returning via the back button or links.
    • Customization: Applying TWA-specific visual customizations, such as status bar and toolbar colors, to the fallback WebView experience.

    Warning: This demo is experimental and under development. It does not yet cover all user journeys required for a production-ready WebView fallback.

  4. Explore the TWA Basic demo

    main

    The twa-basic demo is a minimal Android application designed to demonstrate how to launch a Trusted Web Activity (TWA) using only existing components from androidx.browser and android-browser-helper.

    Key characteristics:

    • No custom Java/Kotlin code: It relies entirely on configuring and wiring together existing library components.
    • Purpose: Primarily intended for developers of android-browser-helper to test changes in a real-world app context.
    • Functionality: Launches https://airhorner.com as a TWA.
    • Similarity: It is architecturally similar to projects generated by svgomg-twa.
  5. Understand Trusted Web Activity (TWA) Notification Delegation

    main

    The Notification Delegation demo demonstrates how to override the standard web-based notification prompt using native Android code. This allows developers to leverage native Android notification features that are not available through standard web APIs, such as using custom notification sounds.

    The core logic for implementing this behavior is located in the NotificationDelegationService class.

  6. Use Play Billing from a website

    main

    To access Google Play Billing capabilities (like querying purchase history, initializing payments, or querying product details) from your website within a TWA, use the window.getDigitalGoodsService API.

    You must provide the payment method URL https://play.google.com/billing and an array of product item IDs.

    const PAYMENT_METHOD = 'https://play.google.com/billing'; 
    const ITEM_IDS = [  
         'android.test.purchased',  
         'android.test.canceled', 
    ] 
    
    const service = await window.getDigitalGoodsService(PAYMENT_METHOD); 
    const details = await service.getDetails(ITEM_IDS);
    console.log(details);
  7. Implement Custom Tabs with Ephemeral Browsing and Fallback

    main

    This demo demonstrates how to launch a Custom Tab using ephemeral browsing mode. Ephemeral browsing ensures that the session is private and does not persist cookies, cache, or history.

    Because ephemeral browsing support varies by device, the implementation includes a fallback mechanism: if ephemeral Custom Tabs are unavailable, the app falls back to opening a web view that manually clears cookies, cache, history, and stored file data to achieve a similar privacy effect.