1# SysUI Media Controls Pipeline
2
3[TOC]
4
5## Purpose
6
7Describe how events flow through the media controls pipeline, and provide a high level overview of what the different components do.
8
9## Pipeline Diagram
10
11![media controls pipeline](media-controls-pipeline.png)
12
13* Orange: External inputs
14* Blue: Internal listeners; all except `MediaDataManager` and `ResumeMediaBrowser` implement [`MediaDataManager.Listener`](/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt#711) and receive `onMediaDataLoaded` and `onMediaDataRemoved` events
15
16## Classes
17
18Files under [`systemui/media/`](/packages/SystemUI/src/com/android/systemui/media/):
19
20* UI
21   * `dialog/`
22      * Output switcher dialog (maintained by Settings team)
23   * IlluminationDrawable.kt
24   * LightSourceDrawable.kt
25      * These create the glow animation when you tap on a button (see [`qs_media_light_source`](/packages/SystemUI/res/drawable/qs_media_light_source.xml)). Should be reusable in other layouts.
26   * Carousel:
27      * MediaCarouselController.kt
28         * Keeps the carousel view up to date and handles state changes (e.g. expansion)
29         * Handles settings gear and page indicator
30      * MediaCarouselScrollHandler.kt
31         * Handles scrolling between players in the carousel
32      * MediaScrollView.kt
33         * Scrollview used in the carousel layout, has some custom measurement code
34   * Individual players:
35      * KeyguardMediaController.kt
36         * Lockscreen media controls have a special wrapper in order to work with the existing lockscreen notification layout
37      * MediaControlPanel.java
38         * Main class for media control UI
39      * SeekBarObserver.kt
40         * Updates seekbar state
41      * SeekBarViewModel.kt
42         * Implements its own `computePosition()` for the seekbar (to avoid continually polling the `PlaybackState`, which involves binder calls)
43         * Does some touch falsing (ignore flings, require drags to start near the thumb - otherwise users would often accidentally trigger the seekbar when they meant to move the carousel or shade)
44      * MediaViewHolder.kt
45         * Holds references to the UI elements in the panel
46* Animation support:
47   * MediaHierarchyManager.kt
48      * Responsible for placement of media view and animation between hosts
49   * MediaHost.kt
50      * Every location that a media player could be located needs a `MediaHost`
51      * Tracks configuration (if it should show inactive media, needs falsing, etc.)
52   * MediaHostStatesManager.kt
53      * Manages the various media host states and coordinates heights between different players
54      * Has the most up to date state for any location
55   * MediaViewController.kt
56      * Controls a single instance of a media player, keeps the media view states up to date
57* Backend
58   * MediaData.kt
59      * Holds all the media data (track info, active/resume state, etc.)
60   * MediaDataCombineLatest.kt
61      * Combines update events from `MediaDataManager` and `MediaDeviceManager`, so that downstream listeners will have device info
62   * MediaDataFilter.kt
63      * Filters media data based on the current user
64      * Exit point for the pipeline: "external listeners" (currently `MediaHost` and `MediaCarouselController`), while they should be added via `MediaDataManager.addListener()`, will actually be listening to this output
65   * MediaDataManager.kt
66      * Entry point for the pipeline; initializes listener connections and assigns external listeners to the correct exit point
67      * Converts media notifications and resumable media info into `MediaData`
68   * MediaDeviceManager.kt
69      * Handles device updates
70   * MediaFeatureFlag.kt
71      * Utility to check whether media controls are enabled
72   * MediaSessionBasedFilter.kt
73      * Filters media events based on media session. This prevents duplicate controls in situations like casting where we might get both a local and remote object for the same media session.
74   * MediaTimeoutListener.kt
75      * Listens to `PlaybackState` and marks controls inactive after the media has been paused/stopped for 10 minutes (value can be adjusted locally with `adb shell setprop debug.sysui.media_timeout [ms]`)
76   * MediaResumeListener.kt
77      * Listens for new media data and attempts to find a valid `MediaBrowserService` for the app. If successful, sends the information back to the `MediaDataManager`
78      * Saves up to 5 valid `MediaBrowserService` components found this way, and queries them for recent media on boot or user change
79      * Note: the user can disable this feature completely (or block certain apps from being resumable) in [Settings](https://source.corp.google.com/android/packages/apps/Settings/src/com/android/settings/sound/ResumableMediaAppsController.java), in which case this listener will do nothing (or ignore updates from the blocked apps).
80   * ResumeMediaBrowser.java
81      * Connects to an app's [`MediaBrowser`](https://developer.android.com/reference/android/media/browse/MediaBrowser) to determine whether SystemUI is able to connect and find a recent [`MediaItem`](https://developer.android.com/reference/android/media/browse/MediaBrowser.MediaItem)
82* Factory classes (for unit testing):
83   * LocalMediaManagerFactory.kt
84   * MediaBrowserFactory.java
85   * MediaControllerFactory.java
86   * ResumeMediaBrowserFactory.java
87
88## Miscellaneous
89
90Other useful documents:
91
92* [go/sysui-media-resumption-requirements](https://goto.google.com/sysui-media-resumption-requirements) - Internal documentation for app developers about how to work with media resumption
93* [Playing nicely with media controls](https://android-developers.googleblog.com/2020/08/playing-nicely-with-media-controls.html) - blog post on the Android 11 updates
94* [Media Controls developer guide](https://developer.android.com/guide/topics/media/media-controls)
95