1 /* <lambda>null2 * Copyright (C) 2025 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.wallpaper.util 18 19 import android.content.Context 20 import android.net.Uri 21 import com.android.wallpaper.model.CurrentWallpaperInfo 22 import com.android.wallpaper.model.Screen 23 import com.android.wallpaper.model.WallpaperInfo 24 import com.android.wallpaper.module.InjectorProvider 25 import kotlin.coroutines.resume 26 import kotlin.coroutines.suspendCoroutine 27 28 /** Utils for [CurrentWallpaperInfo]. */ 29 object CurrentWallpaperInfoUtils { 30 31 /** 32 * Determines WallpaperInfo objects representing the currently set wallpaper(s), retrieving the 33 * wallpaper id from local metadata if necessary. Updates the current recents key(s) and returns 34 * the WallpaperInfo pair, one for the lock screen and one for the home screen. 35 */ 36 suspend fun getCurrentWallpapers( 37 context: Context, 38 updateRecents: Boolean, 39 forceRefresh: Boolean, 40 onFetchUri: (CurrentWallpaperInfo, Screen) -> Uri?, 41 ): Pair<WallpaperInfo, WallpaperInfo> = suspendCoroutine { continuation -> 42 val injector = InjectorProvider.getInjector() 43 val currentWallpaperFactory = injector.getCurrentWallpaperInfoFactory(context) 44 currentWallpaperFactory.createCurrentWallpaperInfos(context, forceRefresh) { 45 homeWallpaper, 46 lockWallpaper, 47 _ -> 48 val preferences = injector.getPreferences(context) 49 val hw = 50 if (homeWallpaper is CurrentWallpaperInfo) { 51 homeWallpaper.augmentByRecent( 52 context, 53 Screen.HOME_SCREEN, 54 preferences.getHomeWallpaperRecentsKey(), 55 updateRecents, 56 onFetchUri.invoke(homeWallpaper, Screen.HOME_SCREEN), 57 ) 58 } else { 59 homeWallpaper 60 } 61 val lw = 62 when (lockWallpaper) { 63 null -> { 64 hw 65 } 66 is CurrentWallpaperInfo -> { 67 lockWallpaper.augmentByRecent( 68 context, 69 Screen.LOCK_SCREEN, 70 preferences.getLockWallpaperRecentsKey(), 71 updateRecents, 72 onFetchUri.invoke(lockWallpaper, Screen.LOCK_SCREEN), 73 ) 74 } 75 else -> { 76 lockWallpaper 77 } 78 } 79 80 if (updateRecents) { 81 preferences.setHomeWallpaperRecentsKey(hw.wallpaperId) 82 preferences.setLockWallpaperRecentsKey(lw.wallpaperId) 83 } 84 85 continuation.resume(Pair(hw, lw)) 86 } 87 } 88 89 /** Augments the current wallpaper info by its recent wallpaper data. */ 90 private fun CurrentWallpaperInfo.augmentByRecent( 91 context: Context, 92 screen: Screen, 93 recentKey: String?, 94 updateRecents: Boolean, 95 sharableUri: Uri?, 96 ): CurrentWallpaperInfo { 97 val cropHints = 98 if (InjectorProvider.getInjector().getFlags().isMultiCropEnabled()) { 99 wallpaperCropHints 100 } else { 101 null 102 } 103 104 return object : 105 CurrentWallpaperInfo( 106 getAttributions(context), 107 getActionUrl(context), 108 getCollectionId(context), 109 screen.toFlag(), 110 sharableUri, 111 ) { 112 override fun getWallpaperId(): String { 113 if (!updateRecents) { 114 return super.getWallpaperId() 115 } 116 117 return recentKey ?: super.getWallpaperId() 118 } 119 } 120 .apply { wallpaperCropHints = cropHints } 121 } 122 } 123