1 /*
<lambda>null2  * Copyright (C) 2024 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.category
18 
19 import android.content.Context
20 import android.util.Log
21 import com.android.wallpaper.model.Category
22 import com.android.wallpaper.model.ImageCategory
23 import com.android.wallpaper.model.ThirdPartyAppCategory
24 import com.android.wallpaper.model.WallpaperCategory
25 import com.android.wallpaper.picker.data.category.CategoryModel
26 import com.android.wallpaper.picker.data.category.CollectionCategoryData
27 import com.android.wallpaper.picker.data.category.CommonCategoryData
28 import com.android.wallpaper.picker.data.category.ImageCategoryData
29 import com.android.wallpaper.picker.data.category.ThirdPartyCategoryData
30 import com.android.wallpaper.util.converter.WallpaperModelFactory
31 import dagger.hilt.android.qualifiers.ApplicationContext
32 import javax.inject.Inject
33 import javax.inject.Singleton
34 
35 /** This class creates an instance of [CategoryModel] from an instance of [Category] object. */
36 @Singleton
37 class DefaultCategoryFactory
38 @Inject
39 constructor(
40     @ApplicationContext private val context: Context,
41     private val wallpaperModelFactory: WallpaperModelFactory,
42 ) : CategoryFactory {
43 
44     override fun getCategoryModel(category: Category): CategoryModel {
45         return CategoryModel(
46             commonCategoryData = getCommonCategoryData(category),
47             collectionCategoryData = (category as? WallpaperCategory)?.getCollectionsCategoryData(),
48             imageCategoryData = getImageCategoryData(category),
49             thirdPartyCategoryData = getThirdPartyCategoryData(category),
50         )
51     }
52 
53     private fun getCommonCategoryData(category: Category): CommonCategoryData {
54         return CommonCategoryData(
55             title = category.title,
56             collectionId = category.collectionId,
57             priority = category.priority,
58         )
59     }
60 
61     private fun WallpaperCategory.getCollectionsCategoryData(): CollectionCategoryData {
62         val wallpaperModelList =
63             wallpapers
64                 .map { wallpaperInfo ->
65                     wallpaperModelFactory.getWallpaperModel(context, wallpaperInfo)
66                 }
67                 .toMutableList()
68         return CollectionCategoryData(
69             wallpaperModels = wallpaperModelList,
70             thumbAsset = getThumbnail(context),
71             featuredThumbnailIndex = featuredThumbnailIndex,
72             isSingleWallpaperCategory = isSingleWallpaperCategory,
73         )
74     }
75 
76     private fun getImageCategoryData(category: Category): ImageCategoryData? {
77         return if (category is ImageCategory) {
78             ImageCategoryData(
79                 thumbnailAsset = category.getThumbnail(context),
80                 defaultDrawable = category.getOverlayIcon(context),
81             )
82         } else {
83             Log.w(TAG, "Passed category is not of type ImageCategory")
84             null
85         }
86     }
87 
88     private fun getThirdPartyCategoryData(category: Category): ThirdPartyCategoryData? {
89         return if (category is ThirdPartyAppCategory) {
90             ThirdPartyCategoryData(
91                 resolveInfo = category.resolveInfo,
92                 defaultDrawable = category.getOverlayIcon(context),
93             )
94         } else {
95             Log.w(TAG, "Passed category is not of type ThirdPartyAppCategory")
96             null
97         }
98     }
99 
100     companion object {
101         private const val TAG = "DefaultCategoryFactory"
102     }
103 }
104