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.systemui.theme;
17 
18 import android.annotation.AnyThread;
19 import android.content.om.FabricatedOverlay;
20 import android.content.om.OverlayIdentifier;
21 import android.content.om.OverlayInfo;
22 import android.content.om.OverlayManager;
23 import android.content.om.OverlayManagerTransaction;
24 import android.os.UserHandle;
25 import android.util.ArrayMap;
26 import android.util.Log;
27 import android.util.Pair;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.VisibleForTesting;
31 
32 import com.android.systemui.Dumpable;
33 import com.android.systemui.dagger.SysUISingleton;
34 import com.android.systemui.dagger.qualifiers.Background;
35 import com.android.systemui.dagger.qualifiers.Main;
36 import com.android.systemui.dump.DumpManager;
37 
38 import com.google.android.collect.Lists;
39 import com.google.android.collect.Sets;
40 
41 import java.io.PrintWriter;
42 import java.util.ArrayList;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Set;
47 import java.util.concurrent.Executor;
48 import java.util.stream.Collectors;
49 
50 import javax.inject.Inject;
51 import javax.inject.Named;
52 
53 /**
54  * Responsible for orchestrating overlays, based on user preferences and other inputs from
55  * {@link ThemeOverlayController}.
56  */
57 @SysUISingleton
58 public class ThemeOverlayApplier implements Dumpable {
59     private static final String TAG = "ThemeOverlayApplier";
60     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
61 
62     @VisibleForTesting
63     static final String ANDROID_PACKAGE = "android";
64     @VisibleForTesting
65     static final String SETTINGS_PACKAGE = "com.android.settings";
66     @VisibleForTesting
67     static final String SYSUI_PACKAGE = "com.android.systemui";
68 
69     static final String OVERLAY_CATEGORY_DYNAMIC_COLOR =
70             "android.theme.customization.dynamic_color";
71     static final String OVERLAY_CATEGORY_ACCENT_COLOR =
72             "android.theme.customization.accent_color";
73     static final String OVERLAY_CATEGORY_SYSTEM_PALETTE =
74             "android.theme.customization.system_palette";
75     static final String OVERLAY_CATEGORY_THEME_STYLE =
76             "android.theme.customization.theme_style";
77 
78     static final String OVERLAY_COLOR_SOURCE = "android.theme.customization.color_source";
79 
80     static final String OVERLAY_COLOR_INDEX = "android.theme.customization.color_index";
81 
82     static final String OVERLAY_COLOR_BOTH = "android.theme.customization.color_both";
83 
84     static final String COLOR_SOURCE_PRESET = "preset";
85 
86     static final String COLOR_SOURCE_HOME = "home_wallpaper";
87 
88     static final String COLOR_SOURCE_LOCK = "lock_wallpaper";
89 
90     static final String TIMESTAMP_FIELD = "_applied_timestamp";
91 
92     @VisibleForTesting
93     static final String OVERLAY_CATEGORY_FONT = "android.theme.customization.font";
94     @VisibleForTesting
95     static final String OVERLAY_CATEGORY_SHAPE =
96             "android.theme.customization.adaptive_icon_shape";
97     @VisibleForTesting
98     static final String OVERLAY_CATEGORY_ICON_ANDROID =
99             "android.theme.customization.icon_pack.android";
100     @VisibleForTesting
101     static final String OVERLAY_CATEGORY_ICON_SYSUI =
102             "android.theme.customization.icon_pack.systemui";
103     @VisibleForTesting
104     static final String OVERLAY_CATEGORY_ICON_SETTINGS =
105             "android.theme.customization.icon_pack.settings";
106     @VisibleForTesting
107     static final String OVERLAY_CATEGORY_ICON_LAUNCHER =
108             "android.theme.customization.icon_pack.launcher";
109     @VisibleForTesting
110     static final String OVERLAY_CATEGORY_ICON_THEME_PICKER =
111             "android.theme.customization.icon_pack.themepicker";
112 
113     /*
114      * All theme customization categories used by the system, in order that they should be applied,
115      * starts with launcher and grouped by target package.
116      */
117     static final List<String> THEME_CATEGORIES = Lists.newArrayList(
118             OVERLAY_CATEGORY_SYSTEM_PALETTE,
119             OVERLAY_CATEGORY_ICON_LAUNCHER,
120             OVERLAY_CATEGORY_SHAPE,
121             OVERLAY_CATEGORY_FONT,
122             OVERLAY_CATEGORY_ACCENT_COLOR,
123             OVERLAY_CATEGORY_DYNAMIC_COLOR,
124             OVERLAY_CATEGORY_ICON_ANDROID,
125             OVERLAY_CATEGORY_ICON_SYSUI,
126             OVERLAY_CATEGORY_ICON_SETTINGS,
127             OVERLAY_CATEGORY_ICON_THEME_PICKER);
128 
129     /* Categories that need to be applied to the current user as well as the system user. */
130     @VisibleForTesting
131     static final Set<String> SYSTEM_USER_CATEGORIES = Sets.newHashSet(
132             OVERLAY_CATEGORY_SYSTEM_PALETTE,
133             OVERLAY_CATEGORY_ACCENT_COLOR,
134             OVERLAY_CATEGORY_DYNAMIC_COLOR,
135             OVERLAY_CATEGORY_FONT,
136             OVERLAY_CATEGORY_SHAPE,
137             OVERLAY_CATEGORY_ICON_ANDROID,
138             OVERLAY_CATEGORY_ICON_SYSUI);
139 
140     /* Allowed overlay categories for each target package. */
141     private final Map<String, Set<String>> mTargetPackageToCategories = new ArrayMap<>();
142     /* Target package for each overlay category. */
143     private final Map<String, String> mCategoryToTargetPackage = new ArrayMap<>();
144     private final OverlayManager mOverlayManager;
145     private final Executor mBgExecutor;
146     private final Executor mMainExecutor;
147     private final String mLauncherPackage;
148     private final String mThemePickerPackage;
149 
150     @Inject
ThemeOverlayApplier(OverlayManager overlayManager, @Background Executor bgExecutor, @Named(ThemeModule.LAUNCHER_PACKAGE) String launcherPackage, @Named(ThemeModule.THEME_PICKER_PACKAGE) String themePickerPackage, DumpManager dumpManager, @Main Executor mainExecutor)151     public ThemeOverlayApplier(OverlayManager overlayManager,
152             @Background Executor bgExecutor,
153             @Named(ThemeModule.LAUNCHER_PACKAGE) String launcherPackage,
154             @Named(ThemeModule.THEME_PICKER_PACKAGE) String themePickerPackage,
155             DumpManager dumpManager,
156             @Main Executor mainExecutor) {
157         mOverlayManager = overlayManager;
158         mBgExecutor = bgExecutor;
159         mMainExecutor = mainExecutor;
160         mLauncherPackage = launcherPackage;
161         mThemePickerPackage = themePickerPackage;
162         mTargetPackageToCategories.put(ANDROID_PACKAGE, Sets.newHashSet(
163                 OVERLAY_CATEGORY_SYSTEM_PALETTE, OVERLAY_CATEGORY_ACCENT_COLOR,
164                 OVERLAY_CATEGORY_DYNAMIC_COLOR,
165                 OVERLAY_CATEGORY_FONT, OVERLAY_CATEGORY_SHAPE,
166                 OVERLAY_CATEGORY_ICON_ANDROID));
167         mTargetPackageToCategories.put(SYSUI_PACKAGE,
168                 Sets.newHashSet(OVERLAY_CATEGORY_ICON_SYSUI));
169         mTargetPackageToCategories.put(SETTINGS_PACKAGE,
170                 Sets.newHashSet(OVERLAY_CATEGORY_ICON_SETTINGS));
171         mTargetPackageToCategories.put(mLauncherPackage,
172                 Sets.newHashSet(OVERLAY_CATEGORY_ICON_LAUNCHER));
173         mTargetPackageToCategories.put(mThemePickerPackage,
174                 Sets.newHashSet(OVERLAY_CATEGORY_ICON_THEME_PICKER));
175         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ACCENT_COLOR, ANDROID_PACKAGE);
176         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_DYNAMIC_COLOR, ANDROID_PACKAGE);
177         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_FONT, ANDROID_PACKAGE);
178         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_SHAPE, ANDROID_PACKAGE);
179         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_ANDROID, ANDROID_PACKAGE);
180         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_SYSUI, SYSUI_PACKAGE);
181         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_SETTINGS, SETTINGS_PACKAGE);
182         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_LAUNCHER, mLauncherPackage);
183         mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_THEME_PICKER, mThemePickerPackage);
184 
185         dumpManager.registerDumpable(TAG, this);
186     }
187 
188     /**
189      * Apply the set of overlay packages to the set of {@code UserHandle}s provided. Overlays that
190      * affect sysui will also be applied to the system user.
191      *
192      * @param categoryToPackage Overlay packages to be applied
193      * @param pendingCreation Overlays yet to be created
194      * @param currentUser Current User ID
195      * @param managedProfiles Profiles get overlays
196      * @param onComplete Callback for when resources are ready. Runs in the main thread.
197      */
applyCurrentUserOverlays( Map<String, OverlayIdentifier> categoryToPackage, FabricatedOverlay[] pendingCreation, int currentUser, Set<UserHandle> managedProfiles, Runnable onComplete )198     public void applyCurrentUserOverlays(
199             Map<String, OverlayIdentifier> categoryToPackage,
200             FabricatedOverlay[] pendingCreation,
201             int currentUser,
202             Set<UserHandle> managedProfiles,
203             Runnable onComplete
204     ) {
205 
206         mBgExecutor.execute(() -> {
207 
208             // Disable all overlays that have not been specified in the user setting.
209             final Set<String> overlayCategoriesToDisable = new HashSet<>(THEME_CATEGORIES);
210             final Set<String> targetPackagesToQuery = overlayCategoriesToDisable.stream()
211                     .map(category -> mCategoryToTargetPackage.get(category))
212                     .collect(Collectors.toSet());
213             final List<OverlayInfo> overlays = new ArrayList<>();
214             targetPackagesToQuery.forEach(targetPackage -> overlays.addAll(mOverlayManager
215                     .getOverlayInfosForTarget(targetPackage, UserHandle.SYSTEM)));
216             final List<Pair<String, String>> overlaysToDisable = overlays.stream()
217                     .filter(o ->
218                             mTargetPackageToCategories.get(o.targetPackageName).contains(
219                                     o.category))
220                     .filter(o -> overlayCategoriesToDisable.contains(o.category))
221                     .filter(o -> !categoryToPackage.containsValue(
222                             new OverlayIdentifier(o.packageName)))
223                     .filter(o -> o.isEnabled())
224                     .map(o -> new Pair<>(o.category, o.packageName))
225                     .collect(Collectors.toList());
226 
227             OverlayManagerTransaction.Builder transaction = getTransactionBuilder();
228             HashSet<OverlayIdentifier> identifiersPending = new HashSet<>();
229             if (pendingCreation != null) {
230                 for (FabricatedOverlay overlay : pendingCreation) {
231                     identifiersPending.add(overlay.getIdentifier());
232                     transaction.registerFabricatedOverlay(overlay);
233                 }
234             }
235 
236             for (Pair<String, String> packageToDisable : overlaysToDisable) {
237                 OverlayIdentifier overlayInfo = new OverlayIdentifier(packageToDisable.second);
238                 setEnabled(transaction, overlayInfo, packageToDisable.first, currentUser,
239                         managedProfiles, false, identifiersPending.contains(overlayInfo));
240             }
241 
242             for (String category : THEME_CATEGORIES) {
243                 if (categoryToPackage.containsKey(category)) {
244                     OverlayIdentifier overlayInfo = categoryToPackage.get(category);
245                     setEnabled(transaction, overlayInfo, category, currentUser, managedProfiles,
246                             true, identifiersPending.contains(overlayInfo));
247                 }
248             }
249 
250             try {
251                 mOverlayManager.commit(transaction.build());
252                 if (onComplete != null) {
253                     Log.d(TAG, "Executing onComplete runnable");
254                     mMainExecutor.execute(onComplete);
255                 }
256             } catch (SecurityException | IllegalStateException e) {
257                 Log.e(TAG, "setEnabled failed", e);
258             }
259         });
260     }
261 
262     @VisibleForTesting
getTransactionBuilder()263     protected OverlayManagerTransaction.Builder getTransactionBuilder() {
264         return new OverlayManagerTransaction.Builder();
265     }
266 
267     @AnyThread
setEnabled(OverlayManagerTransaction.Builder transaction, OverlayIdentifier identifier, String category, int currentUser, Set<UserHandle> managedProfiles, boolean enabled, boolean pendingCreation)268     private void setEnabled(OverlayManagerTransaction.Builder transaction,
269             OverlayIdentifier identifier, String category, int currentUser,
270             Set<UserHandle> managedProfiles, boolean enabled, boolean pendingCreation) {
271         if (DEBUG) {
272             Log.d(TAG, "setEnabled: " + identifier.getPackageName() + " category: "
273                     + category + ": " + enabled);
274         }
275 
276         OverlayInfo overlayInfo = mOverlayManager.getOverlayInfo(identifier,
277                 UserHandle.of(currentUser));
278         if (overlayInfo == null && !pendingCreation) {
279             Log.i(TAG, "Won't enable " + identifier + ", it doesn't exist for user"
280                     + currentUser);
281             return;
282         }
283 
284         transaction.setEnabled(identifier, enabled, currentUser);
285         if (currentUser != UserHandle.SYSTEM.getIdentifier()
286                 && SYSTEM_USER_CATEGORIES.contains(category)) {
287             transaction.setEnabled(identifier, enabled, UserHandle.SYSTEM.getIdentifier());
288         }
289 
290         // Do not apply Launcher or Theme picker overlays to managed users. Apps are not
291         // installed in there.
292         overlayInfo = mOverlayManager.getOverlayInfo(identifier, UserHandle.SYSTEM);
293         if (overlayInfo == null || overlayInfo.targetPackageName.equals(mLauncherPackage)
294                 || overlayInfo.targetPackageName.equals(mThemePickerPackage)) {
295             return;
296         }
297 
298         for (UserHandle userHandle : managedProfiles) {
299             transaction.setEnabled(identifier, enabled, userHandle.getIdentifier());
300         }
301     }
302 
303     /**
304      * @inherit
305      */
306     @Override
dump(@onNull PrintWriter pw, @NonNull String[] args)307     public void dump(@NonNull PrintWriter pw, @NonNull String[] args) {
308         pw.println("mTargetPackageToCategories=" + mTargetPackageToCategories);
309         pw.println("mCategoryToTargetPackage=" + mCategoryToTargetPackage);
310     }
311 }
312