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