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.qs.pipeline.data.repository 18 19 import android.content.ComponentName 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.settings.UserFileManager 22 import javax.inject.Inject 23 24 /** 25 * Repository for keeping track of whether a given [CustomTile] [ComponentName] has been added to 26 * the set of current tiles for a user. This is used to determine when lifecycle methods in 27 * `TileService` about the tile being added/removed need to be called. 28 */ 29 interface CustomTileAddedRepository { 30 /** 31 * Check if a particular [CustomTile] associated with [componentName] has been added for 32 * [userId] and has not been removed since. 33 */ isTileAddednull34 fun isTileAdded(componentName: ComponentName, userId: Int): Boolean 35 36 /** 37 * Persists whether a particular [CustomTile] associated with [componentName] has been added and 38 * it's currently in the set of selected tiles for [userId]. 39 */ 40 fun setTileAdded(componentName: ComponentName, userId: Int, added: Boolean) 41 } 42 43 @SysUISingleton 44 class CustomTileAddedSharedPrefsRepository 45 @Inject 46 constructor(private val userFileManager: UserFileManager) : CustomTileAddedRepository { 47 48 override fun isTileAdded(componentName: ComponentName, userId: Int): Boolean { 49 return userFileManager 50 .getSharedPreferences(TILES, 0, userId) 51 .getBoolean(componentName.flattenToString(), false) 52 } 53 54 override fun setTileAdded(componentName: ComponentName, userId: Int, added: Boolean) { 55 userFileManager 56 .getSharedPreferences(TILES, 0, userId) 57 .edit() 58 .putBoolean(componentName.flattenToString(), added) 59 .apply() 60 } 61 62 companion object { 63 private const val TILES = "tiles_prefs" 64 } 65 } 66