1 /*
2  * Copyright (C) 2023 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.panels
18 
19 import android.content.ComponentName
20 import android.os.UserHandle
21 import com.android.systemui.controls.ui.ControlsUiController
22 import com.android.systemui.controls.ui.SelectedItem
23 import com.android.systemui.flags.Flags
24 import kotlinx.coroutines.flow.Flow
25 
26 /** Stores user-selected preferred component. */
27 interface SelectedComponentRepository {
28 
29     /** Returns current set preferred component for the specified user. */
selectedComponentFlownull30     fun selectedComponentFlow(userHandle: UserHandle): Flow<SelectedComponent?>
31 
32     /**
33      * Returns the current set preferred component for the specified user, or null when nothing is
34      * set. If no user is specified, the current user's preference is used. This method by default
35      * operates in the context of the current user unless another user is explicitly specified.
36      * Consider using [ControlsUiController.getPreferredSelectedItem] to get domain specific data.
37      */
38     fun getSelectedComponent(userHandle: UserHandle = UserHandle.CURRENT): SelectedComponent?
39 
40     /**
41      * Sets the preferred component for the current user. Use [getSelectedComponent] to retrieve the
42      * currently set preferred component. This method applies to the current user's settings.
43      */
44     fun setSelectedComponent(selectedComponent: SelectedComponent)
45 
46     /**
47      * Clears the current user's preferred component. After this operation, [getSelectedComponent]
48      * will return null for the current user.
49      */
50     fun removeSelectedComponent()
51 
52     /**
53      * Return true when default preferred component should be set up and false the otherwise. This
54      * is always true when [Flags.APP_PANELS_REMOVE_APPS_ALLOWED] is disabled
55      */
56     fun shouldAddDefaultComponent(): Boolean
57 
58     /**
59      * Sets if default component should be added. This is ignored when
60      * [Flags.APP_PANELS_REMOVE_APPS_ALLOWED] is disabled
61      */
62     fun setShouldAddDefaultComponent(shouldAdd: Boolean)
63 
64     data class SelectedComponent(
65         val name: String,
66         val componentName: ComponentName?,
67         val isPanel: Boolean,
68     ) {
69         constructor(
70             selectedItem: SelectedItem
71         ) : this(
72             name = selectedItem.name.toString(),
73             componentName = selectedItem.componentName,
74             isPanel = selectedItem is SelectedItem.PanelItem,
75         )
76     }
77 }
78