• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.wm.shell.compatui.api
18 
19 /**
20  * Singleton which contains the global state of the compat ui system.
21  */
22 class CompatUIState {
23 
24     private val components = mutableMapOf<String, CompatUIComponent>()
25 
26     val sharedState = CompatUISharedState()
27 
28     val componentStates = mutableMapOf<String, CompatUIComponentState>()
29 
30     /**
31      * @return The CompatUIComponent for the given componentId if it exists.
32      */
getUIComponentnull33     fun getUIComponent(componentId: String): CompatUIComponent? =
34         components[componentId]
35 
36     /**
37      * Registers a component for a given componentId along with its optional state.
38      * <p/>
39      * @param componentId       The identifier for the component to register.
40      * @param comp              The {@link CompatUIComponent} instance to register.
41      * @param componentState    The optional state specific of the component. Not all components
42      *                          have a specific state so it can be null.
43      */
44     fun registerUIComponent(
45         componentId: String,
46         comp: CompatUIComponent,
47         componentState: CompatUIComponentState?
48     ) {
49         components[componentId] = comp
50         componentState?.let {
51             componentStates[componentId] = componentState
52         }
53     }
54 
55     /**
56      * Unregister a component for a given componentId.
57      * <p/>
58      * @param componentId       The identifier for the component to register.
59      */
unregisterUIComponentnull60     fun unregisterUIComponent(componentId: String) {
61         components.remove(componentId)
62         componentStates.remove(componentId)
63     }
64 
65     /**
66      * Get access to the specific {@link CompatUIComponentState} for a {@link CompatUIComponent}
67      * with a given identifier.
68      * <p/>
69      * @param componentId  The identifier of the {@link CompatUIComponent}.
70      * @return The optional state for the component of the provided id.
71      */
72     @Suppress("UNCHECKED_CAST")
stateForComponentnull73     fun <T : CompatUIComponentState> stateForComponent(componentId: String) =
74         componentStates[componentId] as? T
75 }
76