• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.wallpaper.util.converter
18 
19 import android.app.WallpaperManager
20 import android.content.ComponentName
21 import android.content.Context
22 import android.util.Log
23 import com.android.wallpaper.effects.EffectsController
24 import com.android.wallpaper.model.CreativeWallpaperInfo
25 import com.android.wallpaper.model.CurrentWallpaperInfo
26 import com.android.wallpaper.model.ImageWallpaperInfo
27 import com.android.wallpaper.model.LiveWallpaperInfo
28 import com.android.wallpaper.model.WallpaperInfo
29 import com.android.wallpaper.picker.data.ColorInfo
30 import com.android.wallpaper.picker.data.CommonWallpaperData
31 import com.android.wallpaper.picker.data.CreativeWallpaperData
32 import com.android.wallpaper.picker.data.CreativeWallpaperEffectsData
33 import com.android.wallpaper.picker.data.Destination
34 import com.android.wallpaper.picker.data.ImageWallpaperData
35 import com.android.wallpaper.picker.data.LiveWallpaperData
36 import com.android.wallpaper.picker.data.WallpaperId
37 import com.android.wallpaper.picker.data.WallpaperModel
38 import com.android.wallpaper.util.wallpaperconnection.WallpaperConnectionUtils.Companion.isExtendedEffectWallpaper
39 
40 /** This class creates an instance of [WallpaperModel] from a given [WallpaperInfo] object. */
41 interface WallpaperModelFactory {
42 
getWallpaperModelnull43     fun getWallpaperModel(context: Context, wallpaperInfo: WallpaperInfo): WallpaperModel
44 
45     companion object {
46 
47         const val STATIC_WALLPAPER_PACKAGE = "StaticWallpaperPackage"
48         const val STATIC_WALLPAPER_CLASS = "StaticWallpaperClass"
49 
50         private const val TAG = "WallpaperModelFactory"
51         private const val UNKNOWN_COLLECTION_ID = "unknown_collection_id"
52 
53         fun WallpaperInfo.getCommonWallpaperData(context: Context): CommonWallpaperData {
54             var wallpaperDestination = Destination.NOT_APPLIED
55             if (this is CurrentWallpaperInfo) {
56                 wallpaperDestination =
57                     when (wallpaperManagerFlag) {
58                         WallpaperManager.FLAG_SYSTEM -> Destination.APPLIED_TO_SYSTEM
59                         WallpaperManager.FLAG_LOCK -> Destination.APPLIED_TO_LOCK
60                         WallpaperManager.FLAG_LOCK and WallpaperManager.FLAG_SYSTEM ->
61                             Destination.APPLIED_TO_SYSTEM_LOCK
62                         else -> {
63                             Log.w(
64                                 TAG,
65                                 "Invalid value for wallpaperManagerFlag: $wallpaperManagerFlag",
66                             )
67                             Destination.NOT_APPLIED
68                         }
69                     }
70             }
71 
72             // componentName is a valid value for liveWallpapers, for other types of wallpapers
73             // (which are static) we can have a constant value
74             val componentName =
75                 if (this is LiveWallpaperInfo) {
76                     wallpaperComponent.component
77                 } else {
78                     ComponentName(STATIC_WALLPAPER_PACKAGE, STATIC_WALLPAPER_CLASS)
79                 }
80 
81             val wallpaperId =
82                 WallpaperId(
83                     componentName = componentName,
84                     uniqueId =
85                         if (this is ImageWallpaperInfo && getWallpaperId() == null)
86                             "${imageWallpaperUri.hashCode()}"
87                         else wallpaperId,
88                     // TODO(b/308800470): Figure out the use of collection ID
89                     collectionId = getCollectionId(context) ?: UNKNOWN_COLLECTION_ID,
90                 )
91 
92             val colorInfoOfWallpaper =
93                 ColorInfo(colorInfo.wallpaperColors, colorInfo.placeholderColor)
94 
95             return CommonWallpaperData(
96                 id = wallpaperId,
97                 title = getTitle(context),
98                 attributions = getAttributions(context).map { it ?: "" },
99                 exploreActionUrl = getActionUrl(context),
100                 thumbAsset = getThumbAsset(context),
101                 placeholderColorInfo = colorInfoOfWallpaper,
102                 destination = wallpaperDestination,
103             )
104         }
105 
106         fun LiveWallpaperInfo.getLiveWallpaperData(
107             context: Context,
108             effectsController: EffectsController? = null,
109         ): LiveWallpaperData {
110             val groupNameOfWallpaper = (this as? CreativeWallpaperInfo)?.groupName ?: ""
111             val wallpaperManager = WallpaperManager.getInstance(context)
112             val currentHomeWallpaper =
113                 wallpaperManager.getWallpaperInfo(WallpaperManager.FLAG_SYSTEM)
114             val currentLockWallpaper = wallpaperManager.getWallpaperInfo(WallpaperManager.FLAG_LOCK)
115             val contextDescription: CharSequence? = this.getActionDescription(context)
116             return LiveWallpaperData(
117                 groupName = groupNameOfWallpaper,
118                 systemWallpaperInfo = info,
119                 isTitleVisible = isVisibleTitle,
120                 isApplied = isApplied(currentHomeWallpaper, currentLockWallpaper),
121                 // TODO (331227828): don't relay on effectNames to determine if this is an effect
122                 // live wallpaper
123                 isEffectWallpaper =
124                     isExtendedEffectWallpaper(context, info.component) ||
125                         effectsController?.isEffectsWallpaper(info) ?: (effectNames != null),
126                 effectNames = effectNames,
127                 contextDescription = contextDescription,
128                 description = wallpaperDescription,
129             )
130         }
131 
132         fun CreativeWallpaperInfo.getCreativeWallpaperData(): CreativeWallpaperData {
133             return CreativeWallpaperData(
134                 configPreviewUri = configPreviewUri,
135                 cleanPreviewUri = cleanPreviewUri,
136                 deleteUri = deleteUri,
137                 thumbnailUri = thumbnailUri,
138                 shareUri = shareUri,
139                 author = author ?: "",
140                 description = description ?: "",
141                 contentDescription = contentDescription,
142                 isCurrent = isCurrent,
143                 creativeWallpaperEffectsData = getCreativeWallpaperEffectData(),
144                 isNewCreativeWallpaper = isNewCreativeWallpaper,
145             )
146         }
147 
148         private fun CreativeWallpaperInfo.getCreativeWallpaperEffectData():
149             CreativeWallpaperEffectsData? {
150             val effectsBottomSheetTitle =
151                 effectsBottomSheetTitle.takeUnless { it.isNullOrEmpty() } ?: return null
152             val effectsBottomSheetSubtitle =
153                 effectsBottomSheetSubtitle.takeUnless { it.isNullOrEmpty() } ?: return null
154             val clearActionsUri =
155                 clearActionsUri.takeUnless { it?.scheme.isNullOrEmpty() } ?: return null
156             val effectsUri = effectsUri.takeUnless { it?.scheme.isNullOrEmpty() } ?: return null
157             return CreativeWallpaperEffectsData(
158                 effectsBottomSheetTitle = effectsBottomSheetTitle,
159                 effectsBottomSheetSubtitle = effectsBottomSheetSubtitle,
160                 currentEffectId = currentlyAppliedEffectId ?: "",
161                 clearActionUri = clearActionsUri,
162                 effectsUri = effectsUri,
163             )
164         }
165 
166         fun WallpaperInfo.getImageWallpaperData(): ImageWallpaperData? =
167             imageWallpaperUri?.let { ImageWallpaperData(it) }
168     }
169 }
170