Catherine Liang | 6f794ed | 2023-01-05 16:12:24 +0000 | [diff] [blame] | 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.customization.picker.color.data.repository |
| 18 | |
| 19 | import android.app.WallpaperColors |
| 20 | import android.content.Context |
| 21 | import android.util.Log |
| 22 | import com.android.customization.model.CustomizationManager |
| 23 | import com.android.customization.model.color.ColorBundle |
| 24 | import com.android.customization.model.color.ColorCustomizationManager |
| 25 | import com.android.customization.model.color.ColorOption |
| 26 | import com.android.customization.model.color.ColorSeedOption |
| 27 | import com.android.customization.model.theme.OverlayManagerCompat |
| 28 | import com.android.customization.picker.color.shared.model.ColorOptionModel |
| 29 | import com.android.customization.picker.color.shared.model.ColorType |
| 30 | import com.android.wallpaper.model.WallpaperColorsViewModel |
| 31 | import kotlinx.coroutines.flow.Flow |
| 32 | import kotlinx.coroutines.flow.StateFlow |
| 33 | import kotlinx.coroutines.flow.combine |
Catherine Liang | 68e0a82 | 2023-01-26 20:22:58 +0000 | [diff] [blame^] | 34 | import kotlinx.coroutines.flow.map |
| 35 | import kotlinx.coroutines.suspendCancellableCoroutine |
Catherine Liang | 6f794ed | 2023-01-05 16:12:24 +0000 | [diff] [blame] | 36 | |
| 37 | // TODO (b/262924623): refactor to remove dependency on ColorCustomizationManager & ColorOption |
Catherine Liang | 68e0a82 | 2023-01-26 20:22:58 +0000 | [diff] [blame^] | 38 | // TODO (b/268203200): Create test for ColorPickerRepositoryImpl |
Catherine Liang | 6f794ed | 2023-01-05 16:12:24 +0000 | [diff] [blame] | 39 | class 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 Liang | 68e0a82 | 2023-01-26 20:22:58 +0000 | [diff] [blame^] | 54 | 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 Liang | 6f794ed | 2023-01-05 16:12:24 +0000 | [diff] [blame] | 81 | } |
Catherine Liang | 6f794ed | 2023-01-05 16:12:24 +0000 | [diff] [blame] | 82 | |
Catherine Liang | 68e0a82 | 2023-01-26 20:22:58 +0000 | [diff] [blame^] | 83 | 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 Liang | 6f794ed | 2023-01-05 16:12:24 +0000 | [diff] [blame] | 96 | |
| 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 Liang | 68e0a82 | 2023-01-26 20:22:58 +0000 | [diff] [blame^] | 105 | Log.w(TAG, "Apply theme with error", throwable) |
Catherine Liang | 6f794ed | 2023-01-05 16:12:24 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | ) |
| 109 | } |
| 110 | |
| 111 | private fun ColorOption.toModel(): ColorOptionModel { |
| 112 | return ColorOptionModel( |
| 113 | colorOption = this, |
| 114 | isSelected = isActive(colorManager), |
| 115 | ) |
| 116 | } |
Catherine Liang | 68e0a82 | 2023-01-26 20:22:58 +0000 | [diff] [blame^] | 117 | |
| 118 | companion object { |
| 119 | private const val TAG = "ColorPickerRepositoryImpl" |
| 120 | } |
Catherine Liang | 6f794ed | 2023-01-05 16:12:24 +0000 | [diff] [blame] | 121 | } |