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