ProjectedContext


object ProjectedContext


Helper for accessing Projected device Context and its features.

Projected device is an XR device connected to an Android device (host). Host can project the application content to the Projected device and let users interact with it.

The Projected device context will ensure Projected device system services are returned, when queried for system services from this object.

Note: The application context's deviceId can switch between the Projected and host deviceId depending on which activity was most recently in the foreground. Prefer using the Activity context to minimize the risk of running into this problem.

Summary

Public functions

Context

Explicitly create the host device context from any context object.

ActivityOptions

Creates ActivityOptions that should be used to start an activity on the Projected device.

Context

Explicitly create the Projected device context from any context object.

String?
@RequiresApi(value = 34)
getProjectedDeviceName(context: Context)

Returns the name of the Projected device or null if either virtual device wasn't found or the name of the virtual device wasn't set.

Flow<Boolean>
@RequiresApi(value = 35)
isProjectedDeviceConnected(
    context: Context,
    coroutineContext: CoroutineContext
)

Observe whether a Projected device is connected to the host.

Boolean

Returns whether the provided context is the Projected device context.

Public functions

createHostDeviceContext

Added in 1.0.0-alpha01
@RequiresApi(value = 34)
fun createHostDeviceContext(context: Context): Context

Explicitly create the host device context from any context object. The host is the device that connects to a Projected device.

If an application is using a Projected device context and it wants to use system services from the host (e.g. phone), it needs to use the host device context.

createProjectedActivityOptions

Added in 1.0.0-alpha01
@RequiresApi(value = 35)
fun createProjectedActivityOptions(context: Context): ActivityOptions

Creates ActivityOptions that should be used to start an activity on the Projected device.

Parameters
context: Context

any Context object. If the provided context is not a Projected device context, a Projected device context will be created automatically and used to create Projected ActivityOptions.

Throws
kotlin.IllegalStateException

if the projected display was not found or if the Projected device doesn't have any displays.

createProjectedDeviceContext

Added in 1.0.0-alpha01
@RequiresApi(value = 34)
fun createProjectedDeviceContext(context: Context): Context

Explicitly create the Projected device context from any context object.

Throws
kotlin.IllegalStateException

if the projected device was not found.

getProjectedDeviceName

Added in 1.0.0-alpha01
@RequiresApi(value = 34)
fun getProjectedDeviceName(context: Context): String?

Returns the name of the Projected device or null if either virtual device wasn't found or the name of the virtual device wasn't set.

Throws
kotlin.IllegalArgumentException

If another context is used (e.g. the host context).

isProjectedDeviceConnected

Added in 1.0.0-alpha01
@RequiresApi(value = 35)
fun isProjectedDeviceConnected(
    context: Context,
    coroutineContext: CoroutineContext
): Flow<Boolean>

Observe whether a Projected device is connected to the host.

Parameters
context: Context

The context used to access the VirtualDeviceManager. It can be any context object.

coroutineContext: CoroutineContext

The CoroutineContext that includes CoroutineDispatcher which is where the Projected device connectivity observer is executed on.

Throws
kotlin.IllegalArgumentException

if provided coroutineContext doesn't include CoroutineDispatcher.

isProjectedDeviceContext

Added in 1.0.0-alpha01
@RequiresApi(value = 34)
fun isProjectedDeviceContext(context: Context): Boolean

Returns whether the provided context is the Projected device context.