TestCarContext

class TestCarContext : CarContext


The CarContext that is used for testing.

This class will return the test version of car services for tracking calls during testing.

It also allows retrieving the car services already cast to the testing class by calling getCarService with the class name of the test services:

testCarContext.getCarService(TestAppManager.class)
testCarContext.getCarService(TestNavigationManager.class)
testCarContext.getCarService(TestScreenManager.class)

Allows retrieving all Intents sent via startCarApp.

Summary

Nested types

A representation of a permission request including the permissions that were requested as well as the callback provided.

Public functions

java-static TestCarContext
createCarContext(testContext: Context)

Creates a TestCarContext to use for testing.

Unit

Requests to finish the car app.

Any

Provides a car service by name.

T
<T> getCarService(serviceClass: Class<T!>)

Returns a car service by class.

FakeHost

Retrieve the FakeHost being used.

TestCarContext.PermissionRequestInfo?

Returns a PermissionRequestInfo including the information with the last call made to requestPermissions, or null if no call was made.

(Mutable)List<Intent!>

Returns all Intents sent via startCarApp.

Boolean

Verifies if finishCarApp has been called.

Unit
requestPermissions(
    permissions: (Mutable)List<String!>,
    executor: Executor,
    listener: OnRequestPermissionsListener
)

Requests the provided permissions from the user.

Unit

Resets the values tracked by this TestCarContext.

Unit

Starts a car app on the car screen.

Inherited Constants

From androidx.car.app.CarContext
const String!
ACTION_NAVIGATE = "androidx.car.app.action.NAVIGATE"

Standard action for navigating to a location.

const String!
APP_SERVICE = "app"

Manages all app events such as invalidating the UI, showing a toast, etc.

const String!
CAR_SERVICE = "car"

Internal usage only.

const String!
@RequiresCarApi(value = 2)
CONSTRAINT_SERVICE = "constraints"

Manages constraints for the app as enforced by the connected host.

const String!
EXTRA_START_CAR_APP_BINDER_KEY = "androidx.car.app.extra.START_CAR_APP_BINDER_KEY"

Key for including a IStartCarApp in the notification Intent, for starting the app if it has not been opened yet.

const String!
@RequiresCarApi(value = 3)
HARDWARE_SERVICE = "hardware"

Manages access to androidx.car.app.hardware properties, sensors and actions.

const String!

Manages the media requests from 3p apps such as providing a media session token,

const String!
NAVIGATION_SERVICE = "navigation"

Manages all navigation events such as starting navigation when focus is granted, abandoning navigation when focus is lost, etc.

const String!
SCREEN_SERVICE = "screen"

Manages the screens of the app, including the screen stack.

const String!
SUGGESTION_SERVICE = "suggestion"

Manages posting suggestion events

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"

Inherited functions

From androidx.car.app.CarContext
ComponentName?

Return the component (service or activity) that invoked this car app.

Int

Retrieves the API level negotiated with the host.

String
getCarServiceName(serviceClass: Class<Any!>)

Gets the name of the car service that is represented by the specified class.

HostInfo?

Returns information about the host attached to this service.

OnBackPressedDispatcher

Returns the OnBackPressedDispatcher that will be triggered when the user clicks a back button.

Boolean

Returns true if the car is set to dark mode.

Unit
requestPermissions(
    permissions: (Mutable)List<String!>,
    listener: OnRequestPermissionsListener
)

Requests the provided permissions from the user, calling the provided listener in the main thread.

Unit
@RequiresCarApi(value = 2)
setCarAppResult(resultCode: Int, data: Intent?)

Sets the result of this car app.

java-static Unit
startCarApp(notificationIntent: Intent, appIntent: Intent)

This function is deprecated.

use getCarApp to create the pending intent for the notification action.

From android.content.Context
From android.content.ContextWrapper
Unit
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)

Public functions

createCarContext

Added in 1.1.0
java-static fun createCarContext(testContext: Context): TestCarContext

Creates a TestCarContext to use for testing.

Throws
java.lang.NullPointerException

if testContext is null

finishCarApp

Added in 1.4.0-rc02
fun finishCarApp(): Unit

Requests to finish the car app.

Call this when your app is done and should be closed. The Session corresponding to this CarContext will become State.DESTROYED.

At some point after this call, the OS will destroy your CarAppService.

This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.

getCarService

Added in 1.4.0-rc02
fun getCarService(name: String): Any

Provides a car service by name.

The class of the returned object varies by the requested name.

Currently supported car services, and their respective classes, are:

APP_SERVICE

An AppManager for communication between the app and the host.

NAVIGATION_SERVICE

A NavigationManager for management of navigation updates.

SCREEN_SERVICE

A ScreenManager for management of Screens.

CONSTRAINT_SERVICE

A ConstraintManager for management of content limits.

HARDWARE_SERVICE

A CarHardwareManager for interacting with car hardware (e.g. sensors) data.

SUGGESTION_SERVICE

A SuggestionManager for posting androidx.car.app.suggestion.model.Suggestions.

This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.

Parameters
name: String

The name of the car service requested. This should be one of APP_SERVICE, NAVIGATION_SERVICE or SCREEN_SERVICE

Returns
Any

The car service instance

Throws
java.lang.IllegalArgumentException

if name does not refer to a valid car service

java.lang.IllegalStateException

if the service referred by name can not be instantiated (e.g. missing library dependency)

java.lang.NullPointerException

if name is null

getCarService

Added in 1.4.0-rc02
fun <T> getCarService(serviceClass: Class<T!>): T

Returns a car service by class.

See getCarService for a list of the supported car services.

This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.

Parameters
serviceClass: Class<T!>

the class of the requested service

Throws
java.lang.IllegalArgumentException

if serviceClass is not the class of a supported car service

java.lang.IllegalStateException

if serviceClass can not be instantiated (e.g. missing library dependency)

java.lang.NullPointerException

if serviceClass is null

getFakeHost

Added in 1.1.0
fun getFakeHost(): FakeHost

Retrieve the FakeHost being used.

getLastPermissionRequestInfo

Added in 1.1.0
fun getLastPermissionRequestInfo(): TestCarContext.PermissionRequestInfo?

Returns a PermissionRequestInfo including the information with the last call made to requestPermissions, or null if no call was made.

getStartCarAppIntents

Added in 1.1.0
fun getStartCarAppIntents(): (Mutable)List<Intent!>

Returns all Intents sent via startCarApp.

The Intents are stored in the order of when they were sent, where the first intent in the list, is the first intent sent.

The results will be stored until reset is called.

hasCalledFinishCarApp

Added in 1.1.0
fun hasCalledFinishCarApp(): Boolean

Verifies if finishCarApp has been called.

requestPermissions

Added in 1.4.0-rc02
fun requestPermissions(
    permissions: (Mutable)List<String!>,
    executor: Executor,
    listener: OnRequestPermissionsListener
): Unit

Requests the provided permissions from the user.

When the result is available, the listener provided will be called using the Executor provided.

This method should be called using a androidx.car.app.model.ParkedOnlyOnClickListener.

If this method is called while the host deems it is unsafe (for example, when the user is driving), the permission(s) will not be requested from the user.

If the Session is destroyed before the user accepts or rejects the permissions, the callback will not be executed.

Platform Considerations Using this method allows the app to work across all platforms supported by the library with the same API (e.g. Android Auto on mobile devices and Android Automotive OS on native car heads unit). On a mobile platform, this method will start an activity that will display the platform's permissions UI over it. You can choose to not use this method and instead implement your own activity and code to request the permissions in that platform. On Automotive OS however, distraction-optimized activities other than CarAppActivity are not allowed and may be rejected during app submission. See CarAppActivity for more details.
Parameters
permissions: (Mutable)List<String!>

the runtime permissions to request from the user

executor: Executor

the executor that will be used for calling the callback provided

listener: OnRequestPermissionsListener

listener that will be notified when the user takes action on the permission request

Throws
java.lang.NullPointerException

if any of executor, permissions or callback are null

reset

Added in 1.1.0
fun reset(): Unit

Resets the values tracked by this TestCarContext.

startCarApp

Added in 1.4.0-rc02
fun startCarApp(intent: Intent): Unit

Starts a car app on the car screen.

The target application will get the Intent via onCreateScreen or onNewIntent.

Supported Intents:

An Intent to navigate.

The action must be ACTION_NAVIGATE.
The data URI scheme must be either a latitude,longitude pair, or a + separated string query as follows:
1) "geo:12.345,14.8767" for a latitude, longitude pair.
2) "geo:0,0?q=123+Main+St,+Seattle,+WA+98101" for an address.
3) "geo:0,0?q=a+place+name" for a place to search for.

An Intent to make a phone call.

The Intent must be created as defined here.

An Intent to start this app in the car.

The component name of the intent must be the one for the CarAppService that contains this CarContext. If the component name is for a different component, the method will throw a SecurityException.

This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.

Parameters
intent: Intent

the Intent to send to the target application

Throws
java.lang.SecurityException

if the app attempts to start a different app explicitly or does not have permissions for the requested action

androidx.car.app.HostException

if the remote call fails. For example, if the intent cannot be handled by the car host.

java.lang.NullPointerException

if intent is null