TrustedWebActivityService


abstract class TrustedWebActivityService : Service


The TrustedWebActivityService lives in a client app and serves requests from a Trusted Web Activity provider. At present it only serves requests to do with notifications.

When the provider receives a notification from a scope that is associated with a Trusted Web Activity client app, it will attempt to connect to a TrustedWebActivityService and forward calls. This allows the client app to display the notifications itself, meaning it is attributable to the client app and is managed by notification permissions of the client app, not the provider.

TrustedWebActivityService is usable as it is, by adding the following to your AndroidManifest:

<service
    android:name="androidx.browser.trusted.TrustedWebActivityService"
    android:enabled="true"
    android:exported="true">

    <meta-data android:name="android.support.customtabs.trusted.SMALL_ICON"
        android:resource="@drawable/ic_notification_icon" />

    <intent-filter>
        <action android:name="android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</service>
The SMALL_ICON resource should point to a drawable to be used for the notification's small icon.

Alternatively for greater customization, TrustedWebActivityService can be extended and overridden. In this case the manifest entry should be updated to point to the extending class.

As this is an AIDL Service, calls may come in from different Binder threads, so overriding implementations need to be thread safe [1].

For security, the TrustedWebActivityService will check that whatever connects to it matches the Token stored in the TokenStore returned by getTokenStore. This is because we don't want to allow any app on the users device to connect to this Service be able to make it display notifications. [1]: https://developer.android.com/guide/components/aidl.html

Summary

Constants

const String!
ACTION_TRUSTED_WEB_ACTIVITY_SERVICE = "android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE"

An Intent Action used by the provider to find the TrustedWebActivityService or subclass.

const String!
KEY_SMALL_ICON_BITMAP = "android.support.customtabs.trusted.SMALL_ICON_BITMAP"

The key to use to store a Bitmap to return from the onGetSmallIconBitmap method.

const String!
KEY_SUCCESS = "androidx.browser.trusted.SUCCESS"

The key to use to store a boolean in the returns bundle of onExtraCommand method, to indicate whether the command is executed successfully.

const String!
META_DATA_NAME_SMALL_ICON = "android.support.customtabs.trusted.SMALL_ICON"

The Android Manifest meta-data name to specify a small icon id to use.

const Int

Used as a return value of onGetSmallIconId when the icon is not provided.

Public constructors

Public functions

abstract TokenStore

Returns a TokenStore that is used to determine whether the connecting package is allowed to connect to this service.

Boolean

Checks whether notifications are enabled.

IBinder?
Unit
@BinderThread
onCancelNotification(platformTag: String, platformId: Int)

Cancels a notification.

Unit

Called by the system when the service is first created.

Bundle?
@BinderThread
onExtraCommand(
    commandName: String,
    args: Bundle,
    callbackRemote: TrustedWebActivityCallbackRemote?
)

Contains a free form command from the browser.

Bundle

Returns a Bundle containing a bitmap to be use as the small icon for any notifications.

Int

Returns the Android resource id of a drawable to be used for the small icon of the notification.

Boolean
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)
@BinderThread
onNotifyNotificationWithChannel(
    platformTag: String,
    platformId: Int,
    notification: Notification,
    channelName: String
)

Displays a notification.

Boolean

Inherited Constants

From android.content.ComponentCallbacks2
From android.content.Context
const String!
ACCESSIBILITY_SERVICE = "accessibility"
const String!
ACCOUNT_SERVICE = "account"
const String!
ACTIVITY_SERVICE = "activity"
const String!
ALARM_SERVICE = "alarm"
const String!
APPWIDGET_SERVICE = "appwidget"
const String!
APP_OPS_SERVICE = "appops"
const String!
APP_SEARCH_SERVICE = "app_search"
const String!
AUDIO_SERVICE = "audio"
const String!
BATTERY_SERVICE = "batterymanager"
const Int
const Int
const Int
const Int
const Int
const Int
const Int
BIND_EXTERNAL_SERVICE = -2147483648
const Long
BIND_EXTERNAL_SERVICE_LONG = 4611686018427387904
const Int
const Int
const Int
const Int
const Int
const Int
const String!
BIOMETRIC_SERVICE = "biometric"
const String!
BLOB_STORE_SERVICE = "blob_store"
const String!
BLUETOOTH_SERVICE = "bluetooth"
const String!
BUGREPORT_SERVICE = "bugreport"
const String!
CAMERA_SERVICE = "camera"
const String!
CAPTIONING_SERVICE = "captioning"
const String!
CARRIER_CONFIG_SERVICE = "carrier_config"
const String!
CLIPBOARD_SERVICE = "clipboard"
const String!
COMPANION_DEVICE_SERVICE = "companiondevice"
const String!
CONNECTIVITY_DIAGNOSTICS_SERVICE = "connectivity_diagnostics"
const String!
CONNECTIVITY_SERVICE = "connectivity"
const String!
CONSUMER_IR_SERVICE = "consumer_ir"
const Int
const Int
const Int
const String!
CREDENTIAL_SERVICE = "credential"
const String!
CROSS_PROFILE_APPS_SERVICE = "crossprofileapps"
const Int
const Int
const String!
DEVICE_LOCK_SERVICE = "device_lock"
const String!
DEVICE_POLICY_SERVICE = "device_policy"
const String!
DISPLAY_HASH_SERVICE = "display_hash"
const String!
DISPLAY_SERVICE = "display"
const String!
DOMAIN_VERIFICATION_SERVICE = "domain_verification"
const String!
DOWNLOAD_SERVICE = "download"
const String!
DROPBOX_SERVICE = "dropbox"
const String!
EUICC_SERVICE = "euicc"
const String!
FILE_INTEGRITY_SERVICE = "file_integrity"
const String!
FINGERPRINT_SERVICE = "fingerprint"
const String!
GAME_SERVICE = "game"
const String!
GRAMMATICAL_INFLECTION_SERVICE = "grammatical_inflection"
const String!
HARDWARE_PROPERTIES_SERVICE = "hardware_properties"
const String!
HEALTHCONNECT_SERVICE = "healthconnect"
const String!
INPUT_METHOD_SERVICE = "input_method"
const String!
INPUT_SERVICE = "input"
const String!
IPSEC_SERVICE = "ipsec"
const String!
JOB_SCHEDULER_SERVICE = "jobscheduler"
const String!
KEYGUARD_SERVICE = "keyguard"
const String!
LAUNCHER_APPS_SERVICE = "launcherapps"
const String!
LAYOUT_INFLATER_SERVICE = "layout_inflater"
const String!
LOCALE_SERVICE = "locale"
const String!
LOCATION_SERVICE = "location"
const String!
MEDIA_COMMUNICATION_SERVICE = "media_communication"
const String!
MEDIA_METRICS_SERVICE = "media_metrics"
const String!
MEDIA_PROJECTION_SERVICE = "media_projection"
const String!
MEDIA_ROUTER_SERVICE = "media_router"
const String!
MEDIA_SESSION_SERVICE = "media_session"
const String!
MIDI_SERVICE = "midi"
const Int
MODE_APPEND = 32768
const Int
const Int

This property is deprecated.

const Int
const Int
const Int

This property is deprecated.

const Int

This property is deprecated.

const String!
const String!
NFC_SERVICE = "nfc"
const String!
NOTIFICATION_SERVICE = "notification"
const String!
NSD_SERVICE = "servicediscovery"
const String!
OVERLAY_SERVICE = "overlay"
const String!
PEOPLE_SERVICE = "people"
const String!
PERFORMANCE_HINT_SERVICE = "performance_hint"
const String!
POWER_SERVICE = "power"
const String!
PRINT_SERVICE = "print"
const Int
const Int
const Int
const String!
RESTRICTIONS_SERVICE = "restrictions"
const String!
ROLE_SERVICE = "role"
const String!
SEARCH_SERVICE = "search"
const String!
SENSOR_SERVICE = "sensor"
const String!
SHORTCUT_SERVICE = "shortcut"
const String!
STATUS_BAR_SERVICE = "statusbar"
const String!
STORAGE_SERVICE = "storage"
const String!
STORAGE_STATS_SERVICE = "storagestats"
const String!
SYSTEM_HEALTH_SERVICE = "systemhealth"
const String!
TELECOM_SERVICE = "telecom"
const String!
TELEPHONY_IMS_SERVICE = "telephony_ims"
const String!
const String!
TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service"
const String!
TEXT_CLASSIFICATION_SERVICE = "textclassification"
const String!
const String!
TV_INPUT_SERVICE = "tv_input"
const String!
TV_INTERACTIVE_APP_SERVICE = "tv_interactive_app"
const String!
UI_MODE_SERVICE = "uimode"
const String!
USAGE_STATS_SERVICE = "usagestats"
const String!
USB_SERVICE = "usb"
const String!
USER_SERVICE = "user"
const String!
VIBRATOR_MANAGER_SERVICE = "vibrator_manager"
const String!
VIBRATOR_SERVICE = "vibrator"

This property is deprecated.

const String!
VIRTUAL_DEVICE_SERVICE = "virtualdevice"
const String!
VPN_MANAGEMENT_SERVICE = "vpn_management"
const String!
WALLPAPER_SERVICE = "wallpaper"
const String!
WIFI_AWARE_SERVICE = "wifiaware"
const String!
WIFI_P2P_SERVICE = "wifip2p"
const String!
const String!
WIFI_SERVICE = "wifi"
const String!
WINDOW_SERVICE = "window"
From android.app.Service

Inherited functions

From android.content.Context
From android.content.ContextWrapper
Boolean
bindIsolatedService(
    service: Intent!,
    flags: Int,
    instanceName: String!,
    executor: Executor!,
    conn: ServiceConnection!
)
Boolean
bindService(service: Intent!, conn: ServiceConnection!, flags: Int)
Boolean
bindServiceAsUser(
    service: Intent!,
    conn: ServiceConnection!,
    flags: Int,
    user: UserHandle!
)
Int
Int
IntArray<Int>!
checkCallingOrSelfUriPermissions(
    uris: (Mutable)List<Uri!>!,
    modeFlags: Int
)
Int
Int
checkCallingUriPermission(uri: Uri!, modeFlags: Int)
IntArray<Int>!
checkCallingUriPermissions(uris: (Mutable)List<Uri!>!, modeFlags: Int)
Int
checkPermission(permission: String!, pid: Int, uid: Int)
Int
Int
checkUriPermission(uri: Uri!, pid: Int, uid: Int, modeFlags: Int)
IntArray<Int>!
checkUriPermissions(
    uris: (Mutable)List<Uri!>!,
    pid: Int,
    uid: Int,
    modeFlags: Int
)
Unit

This function is deprecated.

Context!
createAttributionContext(attributionTag: String!)
Context!
createConfigurationContext(overrideConfiguration: Configuration!)
Context!
createContext(contextParams: ContextParams!)
Context!
Context!
Context!
Context!
Context!
createPackageContext(packageName: String!, flags: Int)
Context!
createWindowContext(type: Int, options: Bundle!)
Array<String!>!
Boolean
Boolean
Boolean
Unit
enforceCallingOrSelfPermission(permission: String!, message: String!)
Unit
enforceCallingOrSelfUriPermission(
    uri: Uri!,
    modeFlags: Int,
    message: String!
)
Unit
enforceCallingPermission(permission: String!, message: String!)
Unit
enforceCallingUriPermission(uri: Uri!, modeFlags: Int, message: String!)
Unit
enforcePermission(permission: String!, pid: Int, uid: Int, message: String!)
Unit
enforceUriPermission(
    uri: Uri!,
    pid: Int,
    uid: Int,
    modeFlags: Int,
    message: String!
)
Array<String!>!
Context!
ApplicationInfo!
AssetManager!
AttributionSource!
String!
Context!
File!
ClassLoader!
File!
ContentResolver!
File!
File!
Int
File!
getDir(name: String!, mode: Int)
Display!
File!
Array<File!>!
File!
Array<File!>!
Array<File!>!

This function is deprecated.

File!
File!
Executor!
Looper!
File!
File!
Array<File!>!
String!
String!
PackageManager!
String!
String!
ContextParams!
Resources!
SharedPreferences!
getSharedPreferences(name: String!, mode: Int)
Any!
String!
getSystemServiceName(serviceClass: Class<Any!>!)
Resources.Theme!
Drawable!

This function is deprecated.

Int

This function is deprecated.

Int

This function is deprecated.

Unit
grantUriPermission(toPackage: String!, uri: Uri!, modeFlags: Int)
Boolean
Boolean
Boolean
Boolean
moveDatabaseFrom(sourceContext: Context!, name: String!)
Boolean
moveSharedPreferencesFrom(sourceContext: Context!, name: String!)
FileInputStream!
FileOutputStream!
openFileOutput(name: String!, mode: Int)
SQLiteDatabase!
openOrCreateDatabase(
    name: String!,
    mode: Int,
    factory: SQLiteDatabase.CursorFactory!
)
Drawable!

This function is deprecated.

Unit
Unit
Intent!
Unit

This function is deprecated.

Unit

This function is deprecated.

Unit
Unit
revokeUriPermission(uri: Uri!, modeFlags: Int)
Unit
Unit
Unit
sendOrderedBroadcast(intent: Intent!, receiverPermission: String!)
Unit
sendOrderedBroadcastAsUser(
    intent: Intent!,
    user: UserHandle!,
    receiverPermission: String!,
    resultReceiver: BroadcastReceiver!,
    scheduler: Handler!,
    initialCode: Int,
    initialData: String!,
    initialExtras: Bundle!
)
Unit

This function is deprecated.

Unit

This function is deprecated.

Unit
sendStickyOrderedBroadcast(
    intent: Intent!,
    resultReceiver: BroadcastReceiver!,
    scheduler: Handler!,
    initialCode: Int,
    initialData: String!,
    initialExtras: Bundle!
)

This function is deprecated.

Unit
sendStickyOrderedBroadcastAsUser(
    intent: Intent!,
    user: UserHandle!,
    resultReceiver: BroadcastReceiver!,
    scheduler: Handler!,
    initialCode: Int,
    initialData: String!,
    initialExtras: Bundle!
)

This function is deprecated.

Unit
setTheme(resid: Int)
Unit
setWallpaper(bitmap: Bitmap!)

This function is deprecated.

Unit
Unit
ComponentName!
Boolean
startInstrumentation(
    className: ComponentName!,
    profileFile: String!,
    arguments: Bundle!
)
Unit
startIntentSender(
    intent: IntentSender!,
    fillInIntent: Intent!,
    flagsMask: Int,
    flagsValues: Int,
    extraFlags: Int
)
ComponentName!
startService(service: Intent!)
Boolean
Unit
Unit
Unit
Unit
Unit
updateServiceGroup(conn: ServiceConnection!, group: Int, importance: Int)
From android.app.Service
Unit
Unit
dump(fd: FileDescriptor!, writer: PrintWriter!, args: Array<String!>!)
Application!
Int
Unit
Unit
Unit
Unit
onRebind(intent: Intent!)
Unit
onStart(intent: Intent!, startId: Int)

This function is deprecated.

Int
onStartCommand(intent: Intent!, flags: Int, startId: Int)
Unit
onTaskRemoved(rootIntent: Intent!)
Unit
onTimeout(startId: Int)
Unit
onTrimMemory(level: Int)
Unit
startForeground(id: Int, notification: Notification!)
Unit
stopForeground(removeNotification: Boolean)

This function is deprecated.

Unit
Boolean
stopSelfResult(startId: Int)

Constants

ACTION_TRUSTED_WEB_ACTIVITY_SERVICE

Added in 1.2.0
const val ACTION_TRUSTED_WEB_ACTIVITY_SERVICE = "android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE": String!

An Intent Action used by the provider to find the TrustedWebActivityService or subclass.

KEY_SMALL_ICON_BITMAP

Added in 1.2.0
const val KEY_SMALL_ICON_BITMAP = "android.support.customtabs.trusted.SMALL_ICON_BITMAP": String!

The key to use to store a Bitmap to return from the onGetSmallIconBitmap method.

KEY_SUCCESS

Added in 1.3.0
const val KEY_SUCCESS = "androidx.browser.trusted.SUCCESS": String!

The key to use to store a boolean in the returns bundle of onExtraCommand method, to indicate whether the command is executed successfully.

META_DATA_NAME_SMALL_ICON

Added in 1.2.0
const val META_DATA_NAME_SMALL_ICON = "android.support.customtabs.trusted.SMALL_ICON": String!

The Android Manifest meta-data name to specify a small icon id to use.

SMALL_ICON_NOT_SET

Added in 1.2.0
const val SMALL_ICON_NOT_SET = -1: Int

Used as a return value of onGetSmallIconId when the icon is not provided.

Public constructors

TrustedWebActivityService

Added in 1.2.0
TrustedWebActivityService()

Public functions

getTokenStore

Added in 1.2.0
@BinderThread
abstract fun getTokenStore(): TokenStore

Returns a TokenStore that is used to determine whether the connecting package is allowed to connect to this service.

Returns
TokenStore

An TokenStore containing the verified provider.

onAreNotificationsEnabled

Added in 1.2.0
@BinderThread
fun onAreNotificationsEnabled(channelName: String): Boolean

Checks whether notifications are enabled.

Parameters
channelName: String

The name of the notification channel to be used on Android O+.

Returns
Boolean

Whether notifications are enabled.

onBind

Added in 1.2.0
@MainThread
fun onBind(intent: Intent?): IBinder?

onCancelNotification

Added in 1.2.0
@BinderThread
fun onCancelNotification(platformTag: String, platformId: Int): Unit

Cancels a notification.

Parameters
platformTag: String

The notification tag, see cancel.

platformId: Int

The notification id, see cancel.

onCreate

@CallSuper
@MainThread
fun onCreate(): Unit

Called by the system when the service is first created. Do not call this method directly. Overrides must call super.onCreate().

onExtraCommand

Added in 1.3.0
@BinderThread
fun onExtraCommand(
    commandName: String,
    args: Bundle,
    callbackRemote: TrustedWebActivityCallbackRemote?
): Bundle?

Contains a free form command from the browser. The client and browser will need to agree on an additional API to use in advanced. This call can be used for testing or experimental purposes. A return value of null will be used to signify that the client does not know how to handle the request. As optional best practices, KEY_SUCCESS could be use to identify that command was *successfully* handled. For example, when returning a message with result:

    Bundle result = new Bundle();
    result.putString("message", message);
    if (success)
        result.putBoolean(KEY_SUCCESS, true);
    return result;
On the caller side:
    Bundle result = service.extraCommand(commandName, args);
    if (result.getBoolean(service.KEY_SUCCESS)) {
        // Command was successfully handled
    }
Parameters
commandName: String

Name of the command to execute.

args: Bundle

Arguments to the command.

callbackRemote: TrustedWebActivityCallbackRemote?

Contains the callback that passed with the command.

Returns
Bundle?

The result Bundle or null.

onGetSmallIconBitmap

Added in 1.2.0
@BinderThread
fun onGetSmallIconBitmap(): Bundle

Returns a Bundle containing a bitmap to be use as the small icon for any notifications.

Returns
Bundle

A Bundle that may contain a Bitmap contained with key KEY_SMALL_ICON_BITMAP. The bundle may be empty if the client app does not provide a small icon.

onGetSmallIconId

Added in 1.2.0
@BinderThread
fun onGetSmallIconId(): Int

Returns the Android resource id of a drawable to be used for the small icon of the notification. This is called by the provider as it is constructing the notification so a complete notification can be passed to the client. Default behaviour looks for meta-data with the name META_DATA_NAME_SMALL_ICON in service section of the manifest.

Returns
Int

A resource id for the small icon, or SMALL_ICON_NOT_SET if not found.

onNotifyNotificationWithChannel

Added in 1.2.0
@RequiresPermission(value = Manifest.permission.POST_NOTIFICATIONS)
@BinderThread
fun onNotifyNotificationWithChannel(
    platformTag: String,
    platformId: Int,
    notification: Notification,
    channelName: String
): Boolean

Displays a notification.

Parameters
platformTag: String

The notification tag, see notify.

platformId: Int

The notification id, see notify.

notification: Notification

The notification to be displayed, constructed by the provider.

channelName: String

The name of the notification channel that the notification should be displayed on. This method gets or creates a channel from the name and modifies the notification to use that channel.

Returns
Boolean

Whether the notification was successfully displayed (the channel/app may be blocked by the user).

onUnbind

Added in 1.2.0
@MainThread
fun onUnbind(intent: Intent?): Boolean