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.ui
18 
19 import android.content.ComponentName
20 import android.content.Context
21 import android.service.controls.Control
22 import android.service.controls.actions.ControlAction
23 import android.view.ViewGroup
24 import com.android.systemui.controls.controller.StructureInfo
25 
26 interface ControlsUiController {
27     companion object {
28         public const val TAG = "ControlsUiController"
29         public const val EXTRA_ANIMATE = "extra_animate"
30         public const val EXIT_TO_DREAM = "extra_exit_to_dream"
31     }
32 
shownull33     fun show(parent: ViewGroup, onDismiss: Runnable, activityContext: Context)
34 
35     /**
36      * Hide the controls content if it's attached to this parent.
37      */
38     fun hide(parent: ViewGroup)
39 
40     val isShowing: Boolean
41 
42     /**
43      * Returns the preferred activity to start, depending on if the user has favorited any
44      * controls or whether there are any app providing panels.
45      */
46     fun resolveActivity(): Class<*>
47 
48     /**
49      * Request all open dialogs be closed. Set [immediately] to true to dismiss without
50      * animations.
51      */
52     fun closeDialogs(immediately: Boolean)
53 
54     fun onRefreshState(componentName: ComponentName, controls: List<Control>)
55     fun onActionResponse(
56         componentName: ComponentName,
57         controlId: String,
58         @ControlAction.ResponseResult response: Int
59     )
60 
61     /**
62      * Returns the element that is currently preferred by the user.
63      *
64      * This element will be the one that appears when the user first opens the controls activity.
65      */
66     fun getPreferredSelectedItem(structures: List<StructureInfo>): SelectedItem
67 
68     fun onSizeChange()
69 }
70 
71 sealed class SelectedItem {
72 
73     abstract val name: CharSequence
74     abstract val hasControls: Boolean
75     abstract val componentName: ComponentName
76 
77     /**
78      * Represents the currently selected item for a structure.
79      */
80     data class StructureItem(val structure: StructureInfo) : SelectedItem() {
81         override val name: CharSequence = structure.structure
82         override val hasControls: Boolean = structure.controls.isNotEmpty()
83         override val componentName: ComponentName = structure.componentName
84     }
85 
86     /**
87      * Represents the currently selected item for a service that provides a panel activity.
88      *
89      * The [componentName] is that of the service, as that is the expected identifier that should
90      * not change (to always provide proper migration).
91      */
92     data class PanelItem(
93             val appName: CharSequence,
94             override val componentName:
95             ComponentName
96     ) : SelectedItem() {
97         override val name: CharSequence = appName
98         override val hasControls: Boolean = true
99     }
100     companion object {
101         val EMPTY_SELECTION: SelectedItem = StructureItem(StructureInfo.EMPTY_STRUCTURE)
102     }
103 }