Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.controls.controller |
| 18 | |
| 19 | import android.content.ComponentName |
| 20 | import android.service.controls.Control |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 21 | import android.service.controls.ControlsProviderService |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 22 | import android.service.controls.actions.ControlAction |
| 23 | import com.android.systemui.controls.ControlStatus |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 24 | import com.android.systemui.controls.UserAwareController |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 25 | import com.android.systemui.controls.management.ControlsFavoritingActivity |
Lucas Dupin | d60b332 | 2020-04-15 18:06:47 -0700 | [diff] [blame] | 26 | import com.android.systemui.controls.ui.ControlWithState |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 27 | import com.android.systemui.controls.ui.ControlsUiController |
| 28 | import java.util.function.Consumer |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 29 | |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 30 | /** |
| 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 Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 42 | interface ControlsController : UserAwareController { |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Whether the controls system is available for the current user. |
| 46 | */ |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 47 | val available: Boolean |
| 48 | |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 49 | // 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 Kozynski | 9aa23af | 2020-02-05 17:47:47 -0500 | [diff] [blame] | 57 | fun loadForComponent( |
| 58 | componentName: ComponentName, |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 59 | dataCallback: Consumer<LoadData> |
Fabian Kozynski | 9aa23af | 2020-02-05 17:47:47 -0500 | [diff] [blame] | 60 | ) |
| 61 | |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 62 | /** |
Fabian Kozynski | 1a6a794 | 2020-03-10 11:19:04 -0400 | [diff] [blame] | 63 | * Cancels a pending load call |
| 64 | */ |
| 65 | fun cancelLoad() |
| 66 | |
| 67 | /** |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 68 | * Request to subscribe for favorited controls per structure |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 69 | * |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 70 | * @param structureInfo structure to limit the subscription to |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 71 | * @see [ControlsBindingController.subscribe] |
| 72 | */ |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 73 | fun subscribeToFavorites(structureInfo: StructureInfo) |
Fabian Kozynski | 9aa23af | 2020-02-05 17:47:47 -0500 | [diff] [blame] | 74 | |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 75 | /** |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 76 | * Request to unsubscribe to the current provider. |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 77 | * |
| 78 | * @see [ControlsBindingController.unsubscribe] |
| 79 | */ |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 80 | fun unsubscribe() |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Notify a [ControlsProviderService] that an action has been performed on a [Control]. |
| 84 | * |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 85 | * @param componentName the name of the service that provides the [Control] |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 86 | * @param controlInfo information of the [Control] receiving the action |
| 87 | * @param action action performed on the [Control] |
| 88 | * @see [ControlsBindingController.action] |
| 89 | */ |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 90 | fun action(componentName: ComponentName, controlInfo: ControlInfo, action: ControlAction) |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 91 | |
| 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 Kozynski | 1bb26b5 | 2020-01-08 18:20:36 -0500 | [diff] [blame] | 99 | fun refreshStatus(componentName: ComponentName, control: Control) |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 100 | |
| 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 Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 109 | fun onActionResponse( |
| 110 | componentName: ComponentName, |
| 111 | controlId: String, |
| 112 | @ControlAction.ResponseResult response: Int |
| 113 | ) |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 114 | |
Lucas Dupin | d60b332 | 2020-04-15 18:06:47 -0700 | [diff] [blame] | 115 | /** |
| 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 Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 122 | // FAVORITE MANAGEMENT |
| 123 | |
| 124 | /** |
Matt Pietal | 6126644 | 2020-03-17 12:53:44 -0400 | [diff] [blame] | 125 | * 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 Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 144 | * Get all the favorites. |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 145 | * |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 146 | * @return a list of the structures that have at least one favorited control |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 147 | */ |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 148 | fun getFavorites(): List<StructureInfo> |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * Get all the favorites for a given component. |
| 152 | * |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 153 | * @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 Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 155 | */ |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 156 | fun getFavoritesForComponent(componentName: ComponentName): List<StructureInfo> |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 157 | |
| 158 | /** |
Fabian Kozynski | 8765d35 | 2020-04-06 21:16:02 -0400 | [diff] [blame] | 159 | * 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 Kozynski | 04e7bde | 2020-02-13 13:02:33 -0500 | [diff] [blame] | 171 | * 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 Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 183 | * Replaces the favorites for the given structure. |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 184 | * |
| 185 | * Calling this method will eliminate the previous selection of favorites and replace it with a |
| 186 | * new one. |
| 187 | * |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 188 | * @param structureInfo common structure for all of the favorited controls |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 189 | */ |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 190 | fun replaceFavoritesForStructure(structureInfo: StructureInfo) |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 191 | |
| 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 Pietal | cd757c8 | 2020-04-08 10:20:48 -0400 | [diff] [blame] | 203 | * TEMPORARY for testing |
| 204 | */ |
| 205 | fun resetFavorites() |
| 206 | |
| 207 | /** |
Fabian Kozynski | 9c459e5 | 2020-02-12 09:08:15 -0500 | [diff] [blame] | 208 | * 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 | */ |
| 237 | fun 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 Kozynski | 1bb26b5 | 2020-01-08 18:20:36 -0500 | [diff] [blame] | 247 | } |