• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context
21 import android.content.SharedPreferences
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.flags.FeatureFlags
24 import com.android.systemui.flags.Flags
25 import com.android.systemui.settings.UserFileManager
26 import com.android.systemui.settings.UserTracker
27 import com.android.systemui.statusbar.policy.DeviceControlsControllerImpl
28 import javax.inject.Inject
29 
30 @SysUISingleton
31 class SelectedComponentRepositoryImpl
32 @Inject
33 constructor(
34     private val userFileManager: UserFileManager,
35     private val userTracker: UserTracker,
36     private val featureFlags: FeatureFlags,
37 ) : SelectedComponentRepository {
38 
39     private companion object {
40         const val PREF_COMPONENT = "controls_component"
41         const val PREF_STRUCTURE_OR_APP_NAME = "controls_structure"
42         const val PREF_IS_PANEL = "controls_is_panel"
43         const val SHOULD_ADD_DEFAULT_PANEL = "should_add_default_panel"
44     }
45 
46     private val sharedPreferences: SharedPreferences
47         get() =
48             userFileManager.getSharedPreferences(
49                 fileName = DeviceControlsControllerImpl.PREFS_CONTROLS_FILE,
50                 mode = Context.MODE_PRIVATE,
51                 userId = userTracker.userId
52             )
53 
getSelectedComponentnull54     override fun getSelectedComponent(): SelectedComponentRepository.SelectedComponent? {
55         with(sharedPreferences) {
56             val componentString = getString(PREF_COMPONENT, null) ?: return null
57             return SelectedComponentRepository.SelectedComponent(
58                 name = getString(PREF_STRUCTURE_OR_APP_NAME, "")!!,
59                 componentName = ComponentName.unflattenFromString(componentString),
60                 isPanel = getBoolean(PREF_IS_PANEL, false)
61             )
62         }
63     }
64 
setSelectedComponentnull65     override fun setSelectedComponent(
66         selectedComponent: SelectedComponentRepository.SelectedComponent
67     ) {
68         sharedPreferences
69             .edit()
70             .putString(PREF_COMPONENT, selectedComponent.componentName?.flattenToString())
71             .putString(PREF_STRUCTURE_OR_APP_NAME, selectedComponent.name)
72             .putBoolean(PREF_IS_PANEL, selectedComponent.isPanel)
73             .apply()
74     }
75 
removeSelectedComponentnull76     override fun removeSelectedComponent() {
77         sharedPreferences
78             .edit()
79             .remove(PREF_COMPONENT)
80             .remove(PREF_STRUCTURE_OR_APP_NAME)
81             .remove(PREF_IS_PANEL)
82             .apply()
83     }
84 
shouldAddDefaultComponentnull85     override fun shouldAddDefaultComponent(): Boolean =
86         if (featureFlags.isEnabled(Flags.APP_PANELS_REMOVE_APPS_ALLOWED)) {
87             sharedPreferences.getBoolean(SHOULD_ADD_DEFAULT_PANEL, true)
88         } else {
89             true
90         }
91 
setShouldAddDefaultComponentnull92     override fun setShouldAddDefaultComponent(shouldAdd: Boolean) {
93         sharedPreferences.edit().putBoolean(SHOULD_ADD_DEFAULT_PANEL, shouldAdd).apply()
94     }
95 }
96