blob: 79a5b0a1ce52530d5a2d3cd95b525f2a05345609 [file] [log] [blame]
Fabian Kozynskif10b6ab2019-12-27 09:31:04 -05001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.controls.controller
18
19import android.content.ComponentName
20import android.service.controls.Control
Fabian Kozynski9c459e52020-02-12 09:08:15 -050021import android.service.controls.ControlsProviderService
Fabian Kozynskif10b6ab2019-12-27 09:31:04 -050022import android.service.controls.actions.ControlAction
23import com.android.systemui.controls.ControlStatus
Fabian Kozynski7988bd42020-01-30 12:21:52 -050024import com.android.systemui.controls.UserAwareController
Fabian Kozynski9c459e52020-02-12 09:08:15 -050025import com.android.systemui.controls.management.ControlsFavoritingActivity
Lucas Dupind60b3322020-04-15 18:06:47 -070026import com.android.systemui.controls.ui.ControlWithState
Fabian Kozynski9c459e52020-02-12 09:08:15 -050027import com.android.systemui.controls.ui.ControlsUiController
28import java.util.function.Consumer
Fabian Kozynskif10b6ab2019-12-27 09:31:04 -050029
Fabian Kozynski9c459e52020-02-12 09:08:15 -050030/**
31 * Controller to handle communication between different parts of the controls system.
32 *
33 * This controller is in charge of:
34 * * Keeping track of favorites
35 * * Determining and keeping track of whether controls are enabled
36 * * Listening for user change and propagating that message in the system
37 * * Communicate between the UI and the [ControlsBindingController]
38 *
39 * This controller being a [UserAwareController] means that all operations will be conducted on
40 * information for the current user only.
41 */
Fabian Kozynski7988bd42020-01-30 12:21:52 -050042interface ControlsController : UserAwareController {
Fabian Kozynski9c459e52020-02-12 09:08:15 -050043
44 /**
45 * Whether the controls system is available for the current user.
46 */
Fabian Kozynskif10b6ab2019-12-27 09:31:04 -050047 val available: Boolean
48
Fabian Kozynski9c459e52020-02-12 09:08:15 -050049 // SERVICE COMMUNICATION
50
51 /**
52 * Load all available [Control] for a given service.
53 *
54 * @param componentName the [ComponentName] of the [ControlsProviderService] to load from
55 * @param dataCallback a callback in which to retrieve the result.
56 */
Fabian Kozynski9aa23af2020-02-05 17:47:47 -050057 fun loadForComponent(
58 componentName: ComponentName,
Fabian Kozynski9c459e52020-02-12 09:08:15 -050059 dataCallback: Consumer<LoadData>
Fabian Kozynski9aa23af2020-02-05 17:47:47 -050060 )
61
Fabian Kozynski9c459e52020-02-12 09:08:15 -050062 /**
Fabian Kozynski1a6a7942020-03-10 11:19:04 -040063 * Cancels a pending load call
64 */
65 fun cancelLoad()
66
67 /**
Matt Pietal313f37d2020-02-24 11:27:22 -050068 * Request to subscribe for favorited controls per structure
Fabian Kozynski9c459e52020-02-12 09:08:15 -050069 *
Matt Pietal313f37d2020-02-24 11:27:22 -050070 * @param structureInfo structure to limit the subscription to
Fabian Kozynski9c459e52020-02-12 09:08:15 -050071 * @see [ControlsBindingController.subscribe]
72 */
Matt Pietal313f37d2020-02-24 11:27:22 -050073 fun subscribeToFavorites(structureInfo: StructureInfo)
Fabian Kozynski9aa23af2020-02-05 17:47:47 -050074
Fabian Kozynski9c459e52020-02-12 09:08:15 -050075 /**
Matt Pietal313f37d2020-02-24 11:27:22 -050076 * Request to unsubscribe to the current provider.
Fabian Kozynski9c459e52020-02-12 09:08:15 -050077 *
78 * @see [ControlsBindingController.unsubscribe]
79 */
Fabian Kozynskif10b6ab2019-12-27 09:31:04 -050080 fun unsubscribe()
Fabian Kozynski9c459e52020-02-12 09:08:15 -050081
82 /**
83 * Notify a [ControlsProviderService] that an action has been performed on a [Control].
84 *
Matt Pietal313f37d2020-02-24 11:27:22 -050085 * @param componentName the name of the service that provides the [Control]
Fabian Kozynski9c459e52020-02-12 09:08:15 -050086 * @param controlInfo information of the [Control] receiving the action
87 * @param action action performed on the [Control]
88 * @see [ControlsBindingController.action]
89 */
Matt Pietal313f37d2020-02-24 11:27:22 -050090 fun action(componentName: ComponentName, controlInfo: ControlInfo, action: ControlAction)
Fabian Kozynski9c459e52020-02-12 09:08:15 -050091
92 /**
93 * Refresh the status of a [Control] with information provided from the service.
94 *
95 * @param componentName the name of the service that provides the [Control]
96 * @param control a stateful [Control] with updated information
97 * @see [ControlsUiController.onRefreshState]
98 */
Fabian Kozynski1bb26b52020-01-08 18:20:36 -050099 fun refreshStatus(componentName: ComponentName, control: Control)
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500100
101 /**
102 * Indicate the result of a [ControlAction] performed on a [Control].
103 *
104 * @param componentName the name of the service that provides the [Control]
105 * @param controlId the id of the [Control] the actioned was performed on
106 * @param response the result of the action.
107 * @see [ControlsUiController.onActionResponse]
108 */
Fabian Kozynskif10b6ab2019-12-27 09:31:04 -0500109 fun onActionResponse(
110 componentName: ComponentName,
111 controlId: String,
112 @ControlAction.ResponseResult response: Int
113 )
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500114
Lucas Dupind60b3322020-04-15 18:06:47 -0700115 /**
116 * When a control should be highlighted, dimming down what's around it.
117 *
118 * @param cws focused control, or {@code null} if nothing should be highlighted.
119 */
120 fun onFocusChanged(cws: ControlWithState?)
121
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500122 // FAVORITE MANAGEMENT
123
124 /**
Matt Pietal61266442020-03-17 12:53:44 -0400125 * Send a request to seed favorites into the persisted XML file
126 *
127 * @param componentName the component to seed controls from
128 * @param callback true if the favorites were persisted
129 */
130 fun seedFavoritesForComponent(
131 componentName: ComponentName,
132 callback: Consumer<Boolean>
133 )
134
135 /**
136 * Callback to be informed when the seeding process has finished
137 *
138 * @param callback consumer accepts true if successful
139 * @return true if seeding is in progress and the callback was added
140 */
141 fun addSeedingFavoritesCallback(callback: Consumer<Boolean>): Boolean
142
143 /**
Matt Pietal313f37d2020-02-24 11:27:22 -0500144 * Get all the favorites.
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500145 *
Matt Pietal313f37d2020-02-24 11:27:22 -0500146 * @return a list of the structures that have at least one favorited control
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500147 */
Matt Pietal313f37d2020-02-24 11:27:22 -0500148 fun getFavorites(): List<StructureInfo>
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500149
150 /**
151 * Get all the favorites for a given component.
152 *
Matt Pietal313f37d2020-02-24 11:27:22 -0500153 * @param componentName the name of the service that provides the [Control]
154 * @return a list of the structures that have at least one favorited control
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500155 */
Matt Pietal313f37d2020-02-24 11:27:22 -0500156 fun getFavoritesForComponent(componentName: ComponentName): List<StructureInfo>
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500157
158 /**
Fabian Kozynski8765d352020-04-06 21:16:02 -0400159 * Get all the favorites for a given structure.
160 *
161 * @param componentName the name of the service that provides the [Control]
162 * @param structureName the name of the structure
163 * @return a list of the current favorites in that structure
164 */
165 fun getFavoritesForStructure(
166 componentName: ComponentName,
167 structureName: CharSequence
168 ): List<ControlInfo>
169
170 /**
Fabian Kozynski04e7bde2020-02-13 13:02:33 -0500171 * Adds a single favorite to a given component and structure
172 * @param componentName the name of the service that provides the [Control]
173 * @param structureName the name of the structure that holds the [Control]
174 * @param controlInfo persistent information about the [Control] to be added.
175 */
176 fun addFavorite(
177 componentName: ComponentName,
178 structureName: CharSequence,
179 controlInfo: ControlInfo
180 )
181
182 /**
Matt Pietal313f37d2020-02-24 11:27:22 -0500183 * Replaces the favorites for the given structure.
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500184 *
185 * Calling this method will eliminate the previous selection of favorites and replace it with a
186 * new one.
187 *
Matt Pietal313f37d2020-02-24 11:27:22 -0500188 * @param structureInfo common structure for all of the favorited controls
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500189 */
Matt Pietal313f37d2020-02-24 11:27:22 -0500190 fun replaceFavoritesForStructure(structureInfo: StructureInfo)
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500191
192 /**
193 * Return the number of favorites for a given component.
194 *
195 * This call returns the same as `getFavoritesForComponent(componentName).size`.
196 *
197 * @param componentName the name of the component
198 * @return the number of current favorites for the given component
199 */
200 fun countFavoritesForComponent(componentName: ComponentName): Int
201
202 /**
Matt Pietalcd757c82020-04-08 10:20:48 -0400203 * TEMPORARY for testing
204 */
205 fun resetFavorites()
206
207 /**
Fabian Kozynski9c459e52020-02-12 09:08:15 -0500208 * Interface for structure to pass data to [ControlsFavoritingActivity].
209 */
210 interface LoadData {
211 /**
212 * All of the available controls for the loaded [ControlsProviderService].
213 *
214 * This will indicate if they are currently a favorite and whether they were removed (a
215 * favorite but not retrieved on load).
216 */
217 val allControls: List<ControlStatus>
218
219 /**
220 * Ordered list of ids of favorite controls.
221 */
222 val favoritesIds: List<String>
223
224 /**
225 * Whether there was an error in loading.
226 *
227 * In this case, [allControls] will only contain those that were favorited and will not be
228 * marked as removed.
229 */
230 val errorOnLoad: Boolean
231 }
232}
233
234/**
235 * Creates a basic implementation of a [LoadData].
236 */
237fun createLoadDataObject(
238 allControls: List<ControlStatus>,
239 favorites: List<String>,
240 error: Boolean = false
241): ControlsController.LoadData {
242 return object : ControlsController.LoadData {
243 override val allControls = allControls
244 override val favoritesIds = favorites
245 override val errorOnLoad = error
246 }
Fabian Kozynski1bb26b52020-01-08 18:20:36 -0500247}