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 
18 package com.android.systemui.controls.panels
19 
20 import android.content.Context
21 import android.content.SharedPreferences
22 import android.os.UserHandle
23 import com.android.systemui.res.R
24 import com.android.systemui.settings.UserFileManager
25 import com.android.systemui.settings.UserTracker
26 import com.android.systemui.statusbar.policy.DeviceControlsControllerImpl
27 import com.android.systemui.util.kotlin.SharedPreferencesExt.observe
28 import com.android.systemui.util.kotlin.emitOnStart
29 import javax.inject.Inject
30 import kotlinx.coroutines.flow.Flow
31 import kotlinx.coroutines.flow.map
32 
33 class AuthorizedPanelsRepositoryImpl
34 @Inject
35 constructor(
36     private val context: Context,
37     private val userFileManager: UserFileManager,
38     private val userTracker: UserTracker,
39 ) : AuthorizedPanelsRepository {
40 
observeAuthorizedPanelsnull41     override fun observeAuthorizedPanels(user: UserHandle): Flow<Set<String>> {
42         val prefs = instantiateSharedPrefs(user)
43         return prefs.observe().emitOnStart().map { getAuthorizedPanelsInternal(prefs) }
44     }
45 
getAuthorizedPanelsnull46     override fun getAuthorizedPanels(): Set<String> {
47         return getAuthorizedPanelsInternal(instantiateSharedPrefs(userTracker.userHandle))
48     }
49 
getPreferredPackagesnull50     override fun getPreferredPackages(): Set<String> =
51         context.resources.getStringArray(R.array.config_controlsPreferredPackages).toSet()
52 
53     override fun addAuthorizedPanels(packageNames: Set<String>) {
54         addAuthorizedPanelsInternal(instantiateSharedPrefs(userTracker.userHandle), packageNames)
55     }
56 
removeAuthorizedPanelsnull57     override fun removeAuthorizedPanels(packageNames: Set<String>) {
58         with(instantiateSharedPrefs(userTracker.userHandle)) {
59             val currentSet = getAuthorizedPanelsInternal(this)
60             edit().putStringSet(KEY, currentSet - packageNames).apply()
61         }
62     }
63 
getAuthorizedPanelsInternalnull64     private fun getAuthorizedPanelsInternal(sharedPreferences: SharedPreferences): Set<String> {
65         return sharedPreferences.getStringSet(KEY, emptySet())!!
66     }
67 
addAuthorizedPanelsInternalnull68     private fun addAuthorizedPanelsInternal(
69         sharedPreferences: SharedPreferences,
70         packageNames: Set<String>
71     ) {
72         val currentSet = getAuthorizedPanelsInternal(sharedPreferences)
73         sharedPreferences.edit().putStringSet(KEY, currentSet + packageNames).apply()
74     }
75 
instantiateSharedPrefsnull76     private fun instantiateSharedPrefs(user: UserHandle): SharedPreferences {
77         val sharedPref =
78             userFileManager.getSharedPreferences(
79                 DeviceControlsControllerImpl.PREFS_CONTROLS_FILE,
80                 Context.MODE_PRIVATE,
81                 user.identifier,
82             )
83 
84         // We should add default packages when we've never run this
85         val needToSetup = sharedPref.getStringSet(KEY, null) == null
86         if (needToSetup) {
87             sharedPref.edit().putStringSet(KEY, getPreferredPackages()).apply()
88         }
89         return sharedPref
90     }
91 
92     companion object {
93         private const val KEY = "authorized_panels"
94     }
95 }
96