blob: 4ce5ed929cb7b22bdf09a108de7f9e22a3af0b89 [file] [log] [blame]
Catherine Liang6f794ed2023-01-05 16:12:24 +00001/*
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 */
17package com.android.customization.picker.color.data.repository
18
19import android.app.WallpaperColors
20import android.content.Context
21import android.util.Log
22import com.android.customization.model.CustomizationManager
23import com.android.customization.model.color.ColorBundle
24import com.android.customization.model.color.ColorCustomizationManager
25import com.android.customization.model.color.ColorOption
26import com.android.customization.model.color.ColorSeedOption
27import com.android.customization.model.theme.OverlayManagerCompat
28import com.android.customization.picker.color.shared.model.ColorOptionModel
29import com.android.customization.picker.color.shared.model.ColorType
30import com.android.wallpaper.model.WallpaperColorsViewModel
31import kotlinx.coroutines.flow.Flow
32import kotlinx.coroutines.flow.StateFlow
33import kotlinx.coroutines.flow.combine
Catherine Liang68e0a822023-01-26 20:22:58 +000034import kotlinx.coroutines.flow.map
35import kotlinx.coroutines.suspendCancellableCoroutine
Catherine Liang6f794ed2023-01-05 16:12:24 +000036
37// TODO (b/262924623): refactor to remove dependency on ColorCustomizationManager & ColorOption
Catherine Liang68e0a822023-01-26 20:22:58 +000038// TODO (b/268203200): Create test for ColorPickerRepositoryImpl
Catherine Liang6f794ed2023-01-05 16:12:24 +000039class ColorPickerRepositoryImpl(
40 context: Context,
41 wallpaperColorsViewModel: WallpaperColorsViewModel,
42) : ColorPickerRepository {
43
44 private val homeWallpaperColors: StateFlow<WallpaperColors?> =
45 wallpaperColorsViewModel.homeWallpaperColors
46 private val lockWallpaperColors: StateFlow<WallpaperColors?> =
47 wallpaperColorsViewModel.lockWallpaperColors
48 private val colorManager: ColorCustomizationManager =
49 ColorCustomizationManager.getInstance(context, OverlayManagerCompat(context))
50
51 /** List of wallpaper and preset color options on the device, categorized by Color Type */
52 override val colorOptions: Flow<Map<ColorType, List<ColorOptionModel>>> =
53 combine(homeWallpaperColors, lockWallpaperColors) { homeColors, lockColors ->
Catherine Liang68e0a822023-01-26 20:22:58 +000054 homeColors to lockColors
55 }
56 .map { (homeColors, lockColors) ->
57 suspendCancellableCoroutine { continuation ->
58 colorManager.setWallpaperColors(homeColors, lockColors)
59 colorManager.fetchOptions(
60 object : CustomizationManager.OptionsFetchedListener<ColorOption?> {
61 override fun onOptionsLoaded(options: MutableList<ColorOption?>?) {
62 val wallpaperColorOptions: MutableList<ColorOptionModel> =
63 mutableListOf()
64 val presetColorOptions: MutableList<ColorOptionModel> =
65 mutableListOf()
66 options?.forEach { option ->
67 when (option) {
68 is ColorSeedOption ->
69 wallpaperColorOptions.add(option.toModel())
70 is ColorBundle -> presetColorOptions.add(option.toModel())
71 }
72 }
73 continuation.resumeWith(
74 Result.success(
75 mapOf(
76 ColorType.WALLPAPER_COLOR to wallpaperColorOptions,
77 ColorType.BASIC_COLOR to presetColorOptions
78 )
79 )
80 )
Catherine Liang6f794ed2023-01-05 16:12:24 +000081 }
Catherine Liang6f794ed2023-01-05 16:12:24 +000082
Catherine Liang68e0a822023-01-26 20:22:58 +000083 override fun onError(throwable: Throwable?) {
84 Log.e(TAG, "Error loading theme bundles", throwable)
85 continuation.resumeWith(
86 Result.failure(
87 throwable ?: Throwable("Error loading theme bundles")
88 )
89 )
90 }
91 },
92 /* reload= */ false
93 )
94 }
95 }
Catherine Liang6f794ed2023-01-05 16:12:24 +000096
97 override fun select(colorOptionModel: ColorOptionModel) {
98 val colorOption: ColorOption = colorOptionModel.colorOption
99 colorManager.apply(
100 colorOption,
101 object : CustomizationManager.Callback {
102 override fun onSuccess() = Unit
103
104 override fun onError(throwable: Throwable?) {
Catherine Liang68e0a822023-01-26 20:22:58 +0000105 Log.w(TAG, "Apply theme with error", throwable)
Catherine Liang6f794ed2023-01-05 16:12:24 +0000106 }
107 }
108 )
109 }
110
111 private fun ColorOption.toModel(): ColorOptionModel {
112 return ColorOptionModel(
113 colorOption = this,
114 isSelected = isActive(colorManager),
115 )
116 }
Catherine Liang68e0a822023-01-26 20:22:58 +0000117
118 companion object {
119 private const val TAG = "ColorPickerRepositoryImpl"
120 }
Catherine Liang6f794ed2023-01-05 16:12:24 +0000121}