# ProjectedContext

Artifact: [androidx.xr.projected:projected](https://developer.android.com/jetpack/androidx/releases/xr-projected) [View Source](https://cs.android.com/search?q=file:androidx/xr/projected/ProjectedContext.kt+class:androidx.xr.projected.ProjectedContext) Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

*** ** * ** ***


```
@ExperimentalProjectedApi
public static class ProjectedContext
```

<br />

*** ** * ** ***

Helper for accessing Projected device `https://developer.android.com/reference/android/content/Context.html` 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 fields |
|---|---|
| `static @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedContext` | `https://developer.android.com/reference/androidx/xr/projected/ProjectedContext#INSTANCE()` |

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/androidx/xr/projected/ProjectedContext#createHostDeviceContext(android.content.Context)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context)` Explicitly create the host device context from any context object. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/ActivityOptions.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 35) https://developer.android.com/reference/androidx/xr/projected/ProjectedContext#createProjectedActivityOptions(android.content.Context)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context)` Creates `https://developer.android.com/reference/android/app/ActivityOptions.html` that should be used to start an activity on the Projected device. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/androidx/xr/projected/ProjectedContext#createProjectedDeviceContext(android.content.Context)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context)` Explicitly create the Projected device context from any context object. |
| `static final https://developer.android.com/reference/java/lang/String.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/androidx/xr/projected/ProjectedContext#getProjectedDeviceName(android.content.Context)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html 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. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html>` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 36) https://developer.android.com/reference/androidx/xr/projected/ProjectedContext#isProjectedDeviceConnected(android.content.Context,kotlin.coroutines.CoroutineContext)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext )` Observe whether a Projected device is connected to the host. |
| `static final boolean` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 34) https://developer.android.com/reference/androidx/xr/projected/ProjectedContext#isProjectedDeviceContext(android.content.Context)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context)` Returns whether the provided context is the Projected device context. |

## Public fields

### INSTANCE

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public static @NonNull ProjectedContext INSTANCE
```

## Public methods

### createHostDeviceContext

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 34)
public static final @NonNull Context createHostDeviceContext(@NonNull 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-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 35)
public static final @NonNull ActivityOptions createProjectedActivityOptions(@NonNull Context context)
```

Creates `https://developer.android.com/reference/android/app/ActivityOptions.html` that should be used to start an activity on the Projected device.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | any `https://developer.android.com/reference/android/content/Context.html` object. If the provided context is not a Projected device context, a Projected device context will be created automatically and used to create Projected `https://developer.android.com/reference/android/app/ActivityOptions.html`. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the projected display was not found or if the Projected device doesn't have any displays. |

### createProjectedDeviceContext

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 34)
public static final @NonNull Context createProjectedDeviceContext(@NonNull Context context)
```

Explicitly create the Projected device context from any context object.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the projected device was not found. |

### getProjectedDeviceName

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 34)
public static final String getProjectedDeviceName(@NonNull 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.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | If another context is used (e.g. the host context). |

### isProjectedDeviceConnected

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 36)
public static final @NonNull Flow<@NonNull Boolean> isProjectedDeviceConnected(
    @NonNull Context context,
    @NonNull CoroutineContext coroutineContext
)
```

Observe whether a Projected device is connected to the host.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The context used to access the `https://developer.android.com/reference/android/companion/virtual/VirtualDeviceManager.html`. It can be any context object. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext` | The CoroutineContext that includes CoroutineDispatcher which is where the Projected device connectivity observer is executed on. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if provided coroutineContext doesn't include CoroutineDispatcher. |

### isProjectedDeviceContext

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
@RequiresApi(value = 34)
public static final boolean isProjectedDeviceContext(@NonNull Context context)
```

Returns whether the provided context is the Projected device context.