1 /* <lambda>null2 * 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.wallpaper.picker.customization.data.repository 19 20 import android.graphics.Bitmap 21 import android.util.LruCache 22 import com.android.wallpaper.module.WallpaperPreferences 23 import com.android.wallpaper.picker.customization.data.content.WallpaperClient 24 import com.android.wallpaper.picker.customization.shared.model.WallpaperDestination 25 import com.android.wallpaper.picker.customization.shared.model.WallpaperModel 26 import kotlinx.coroutines.CoroutineDispatcher 27 import kotlinx.coroutines.CoroutineScope 28 import kotlinx.coroutines.flow.Flow 29 import kotlinx.coroutines.flow.MutableStateFlow 30 import kotlinx.coroutines.flow.SharingStarted 31 import kotlinx.coroutines.flow.StateFlow 32 import kotlinx.coroutines.flow.asStateFlow 33 import kotlinx.coroutines.flow.flowOn 34 import kotlinx.coroutines.flow.map 35 import kotlinx.coroutines.flow.stateIn 36 import kotlinx.coroutines.withContext 37 38 /** Encapsulates access to wallpaper-related data. */ 39 class WallpaperRepository( 40 private val scope: CoroutineScope, 41 private val client: WallpaperClient, 42 private val wallpaperPreferences: WallpaperPreferences, 43 private val backgroundDispatcher: CoroutineDispatcher, 44 ) { 45 val maxOptions = MAX_OPTIONS 46 47 private val thumbnailCache = LruCache<String, Bitmap>(maxOptions) 48 49 /** The ID of the currently-selected wallpaper. */ 50 fun selectedWallpaperId( 51 destination: WallpaperDestination, 52 ): StateFlow<String> { 53 return client 54 .recentWallpapers(destination = destination, limit = 1) 55 .map { previews -> currentWallpaperKey(destination, previews) } 56 .flowOn(backgroundDispatcher) 57 .stateIn( 58 scope = scope, 59 started = SharingStarted.WhileSubscribed(), 60 initialValue = currentWallpaperKey(destination, null) 61 ) 62 } 63 64 private fun currentWallpaperKey( 65 destination: WallpaperDestination, 66 previews: List<WallpaperModel>?, 67 ): String { 68 val key = 69 when (destination) { 70 WallpaperDestination.HOME -> wallpaperPreferences.homeWallpaperRecentsKey 71 WallpaperDestination.LOCK -> wallpaperPreferences.lockWallpaperRecentsKey 72 else -> error("Unsupported destination") 73 } 74 return key ?: previews?.firstOrNull()?.wallpaperId ?: DEFAULT_KEY 75 } 76 77 val areRecentsAvailable: Boolean by lazy { client.areRecentsAvailable() } 78 79 private val _selectingWallpaperId = 80 MutableStateFlow<Map<WallpaperDestination, String?>>(emptyMap()) 81 /** 82 * The ID of the wallpaper that is in the process of becoming the selected wallpaper or `null` 83 * if no such transaction is currently taking place. 84 */ 85 val selectingWallpaperId: StateFlow<Map<WallpaperDestination, String?>> = 86 _selectingWallpaperId.asStateFlow() 87 88 /** Lists the most recent wallpapers. The first one is the most recent (current) wallpaper. */ 89 fun recentWallpapers( 90 destination: WallpaperDestination, 91 limit: Int, 92 ): Flow<List<WallpaperModel>> { 93 return client 94 .recentWallpapers(destination = destination, limit = limit) 95 .flowOn(backgroundDispatcher) 96 } 97 98 /** Returns a thumbnail for the wallpaper with the given ID. */ 99 suspend fun loadThumbnail(wallpaperId: String, lastUpdatedTimestamp: Long): Bitmap? { 100 val cacheKey = "$wallpaperId-$lastUpdatedTimestamp" 101 return thumbnailCache[cacheKey] 102 ?: withContext(backgroundDispatcher) { 103 val thumbnail = client.loadThumbnail(wallpaperId) 104 if (thumbnail != null) { 105 thumbnailCache.put(cacheKey, thumbnail) 106 } 107 thumbnail 108 } 109 } 110 111 /** Sets the wallpaper to the one with the given ID. */ 112 suspend fun setWallpaper( 113 destination: WallpaperDestination, 114 wallpaperId: String, 115 ) { 116 _selectingWallpaperId.value = 117 _selectingWallpaperId.value.toMutableMap().apply { this[destination] = wallpaperId } 118 withContext(backgroundDispatcher) { 119 client.setWallpaper( 120 destination = destination, 121 wallpaperId = wallpaperId, 122 ) { 123 _selectingWallpaperId.value = 124 _selectingWallpaperId.value.toMutableMap().apply { this[destination] = null } 125 } 126 } 127 } 128 129 companion object { 130 private const val DEFAULT_KEY = "default_missing_key" 131 /** The maximum number of options to show, including the currently-selected one. */ 132 private const val MAX_OPTIONS = 5 133 } 134 } 135