1 /*
2  * Copyright (C) 2019 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 package com.android.customization.model.theme.custom;
17 
18 import android.content.Context;
19 
20 import androidx.annotation.Nullable;
21 
22 import com.android.customization.model.CustomizationManager;
23 import com.android.customization.model.theme.ThemeBundle.PreviewInfo;
24 import com.android.customization.model.theme.ThemeManager;
25 import com.android.customization.model.theme.custom.CustomTheme.Builder;
26 
27 import java.util.Map;
28 
29 public class CustomThemeManager implements CustomizationManager<ThemeComponentOption> {
30 
31     private final CustomTheme mOriginalTheme;
32     private final CustomTheme.Builder mBuilder;
33 
CustomThemeManager(Map<String, String> overlayPackages, @Nullable CustomTheme originalTheme)34     private CustomThemeManager(Map<String, String> overlayPackages,
35             @Nullable CustomTheme originalTheme) {
36         mBuilder = new Builder();
37         overlayPackages.forEach(mBuilder::addOverlayPackage);
38         mOriginalTheme = originalTheme;
39     }
40 
41     @Override
isAvailable()42     public boolean isAvailable() {
43         return true;
44     }
45 
46     @Override
apply(ThemeComponentOption option, @Nullable Callback callback)47     public void apply(ThemeComponentOption option, @Nullable Callback callback) {
48         option.buildStep(mBuilder);
49         if (callback != null) {
50             callback.onSuccess();
51         }
52     }
53 
getOverlayPackages()54     public Map<String, String> getOverlayPackages() {
55         return mBuilder.getPackages();
56     }
57 
buildPartialCustomTheme(Context context, String id, String title)58     public CustomTheme buildPartialCustomTheme(Context context, String id, String title) {
59         return ((CustomTheme.Builder)mBuilder.setId(id).setTitle(title)).build(context);
60     }
61 
62     @Override
fetchOptions(OptionsFetchedListener<ThemeComponentOption> callback, boolean reload)63     public void fetchOptions(OptionsFetchedListener<ThemeComponentOption> callback, boolean reload) {
64         //Unused
65     }
66 
getOriginalTheme()67     public CustomTheme getOriginalTheme() {
68         return mOriginalTheme;
69     }
70 
buildCustomThemePreviewInfo(Context context)71     public PreviewInfo buildCustomThemePreviewInfo(Context context) {
72         return mBuilder.createPreviewInfo(context);
73     }
74 
create( @ullable CustomTheme customTheme, ThemeManager themeManager)75     public static CustomThemeManager create(
76             @Nullable CustomTheme customTheme, ThemeManager themeManager) {
77         if (customTheme != null && customTheme.isDefined()) {
78             return new CustomThemeManager(customTheme.getPackagesByCategory(), customTheme);
79         }
80         // Seed the first custom theme with the currently applied theme.
81         return new CustomThemeManager(themeManager.getCurrentOverlays(), customTheme);
82     }
83 
84 }
85