A brief look at Android 12L preliminary APIs
Tags: android, foldables, tablets
Published: 2021-12-05 15:27:47

Back in October 2021, Google made the **Android 12L Developer Preview** public. This feature drop is expected to become final for AOSP in Q1 2022. Starting in December, three beta versions are scheduled until then. Like in the past, this new version will get a new API level (probably 32). With Beta 2, system behaviours and APIs are finalized. Currently, you need to use `"Sv2"` as `compileSdkVersion`, `targetSdkVersion` and `minSdkVersion`. To get an idea of what will be new, and what will change, you should look at the [Android API Differences Report](https://developer.android.com/sdk/api_diff/sv2-dp1/changes). In this short port, I'll show you a few interesting changes and features.

### Spatial audio

Let's start with `android.media`. This packages sees one new class (`Spatializer`) and one accompanying interface (`Spatializer.OnSpatializerStateChangedListener`). According to the [documentation](https://developer.android.com/reference/android/media/Spatializer.html), `Spatializer`

> provides access to querying capabilities and behaviour of 
> sound spatialization on the device. Sound spatialization
> simulates sounds originating around the listener as if they
> were coming from virtual speakers placed around the listener.

As Android 12L is designed for large screens, it makes perfect sense to update the multimedia experience on these devices, as they are often used to enjoy audio and video content. Support for spatialization is optional, so you need to check if the feature is supported using [isAvailable()](https://developer.android.com/reference/android/media/Spatializer#isAvailable()).

`OnSpatializerStateChangedListener` allows your app to get notified of changes to the state of the spatializer effect. `AudioManager` has a new method [getSpatializer()](https://developer.android.com/reference/android/media/AudioManager.html#getSpatializer()). It returns a handle to the optional platform's `Spatializer`. The returned value is never `null`.

`AudioAttributes` has two new query methods, [getSpatializationBehavior()](https://developer.android.com/reference/android/media/AudioAttributes.html#getSpatializationBehavior()) and [isContentSpatialized()](https://developer.android.com/reference/android/media/AudioAttributes.html#isContentSpatialized()).

### Additions and changes to voice interaction session

Class `android.service.voice.VoiceInteractionSession` was added with API level 21. According to the [documentation](https://developer.android.com/reference/android/service/voice/VoiceInteractionSession), provides

> a facility for the implementation to interact with the user in
> the voice interaction layer. The user interface is initially
> shown by default, and can be created be overriding 
> `onCreateContentView()` in which the UI can be built.
>
> A voice interaction session can be self-contained, ultimately
> calling `finish()` when done. It can also initiate voice
> interactions with applications by calling
> `startVoiceActivity(Intent)`

The new method [registerVisibleActivityCallback](https://developer.android.com/reference/android/service/voice/VoiceInteractionSession#registerVisibleActivityCallback(java.util.concurrent.Executor,%20android.service.voice.VoiceInteractionSession.VisibleActivityCallback)) registers a callback that will be notified when visible activities have been changed.

The interface `VoiceInteractionSession.VisibleActivityCallback` defines two methods:

- `onInvisible(VoiceInteractionSession.ActivityId activityId)` is called to inform that a visible activity has gone
- `onVisible(VisibleActivityInfo activityInfo)` is called to inform that an activity has become visible

[unregisterVisibleActivityCallback()](https://developer.android.com/reference/android/service/voice/VoiceInteractionSession#unregisterVisibleActivityCallback(android.service.voice.VoiceInteractionSession.VisibleActivityCallback)) unregisters the callback.

As you have just seen, another new class related to this is `android.service.voice.VisibleActivityInfo`. According to the [documentation](https://developer.android.com/reference/android/service/voice/VisibleActivityInfo), it is

> used to represent a visible activity information. The system
> provides this to services that need to know
> `VoiceInteractionSession.ActivityId`.

To obtain the isnatce, just invoke `getActivityId()`, which returns a `VoiceInteractionSession.ActivityId`.

You will also get an instance of `VoiceInteractionSession.ActivityId`, for example, from `onInvisible()` of the `VoiceInteractionSession.VisibleActivityCallback` interface. 

Why `onVisible()` wraps `ActivityId` inside a `VisibleActivityInfo`, whereas `onInvisible()` receive it directly is, at the time of writing, not obvious.

You can pass `ActivityId` to, at least, `requestDirectActions()` of a `VoiceInteractionSession`.

### Random bits and pieces

There are some other changes and additions in various places. For example, class `android.os.Environment` seems to undeprecate `getExternalStorageDirectory()` and `getExternalStoragePublicDirectory(String)`. Both methods were deprecated with API level 29. At the time of writing, the implications of this are not obvious (at least to the author 🤣).

There also seems to be a new `Manifest.permission` on the way, as the `LAUNCH_TWO_PANE_SETTINGS_DEEP_LINK` constant suggests. The documentation [says](https://developer.android.com/reference/android/Manifest.permission#LAUNCH_TWO_PANE_SETTINGS_DEEP_LINK):

> An application needs this permission for
> `Settings.ACTION_SETTINGS_LARGE_SCREEN_DEEP_LINK` to show its
> Activity in 2-pane of Settings app.

[ACTION_SETTINGS_LARGE_SCREEN_DEEP_LINK](https://developer.android.com/reference/android/provider/Settings#ACTION_SETTINGS_LARGE_SCREEN_DEEP_LINK) currently reads:

> Activity Action: For system or preinstalled apps to show their
> `Activity` in 2-pane mode in Settings app on large screen
> devices.

Google will likely be detailing the workflow in later beta versions.

### Conclusion

As for most early preview versions, some API changes or additions are not immediately clear. Usually, Google explains them sooner or later. What do you think about the new feature drop? There surely are quite a few interesting things waiting for us in later betas, right? Please share your thoughts in the comments.

### Resources

[12L Developer Preview](https://developer.android.com/about/versions/12/12L/overview)