Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 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.content.om.OverlayInfo; |
| 19 | import android.content.om.OverlayManager; |
| 20 | import android.os.UserHandle; |
| 21 | import android.util.ArrayMap; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import androidx.annotation.VisibleForTesting; |
| 25 | |
Amin Shaikh | b56b29c | 2019-04-11 07:26:03 -0400 | [diff] [blame] | 26 | import com.google.android.collect.Lists; |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 27 | import com.google.android.collect.Sets; |
| 28 | |
| 29 | import java.util.ArrayList; |
Amin Shaikh | b56b29c | 2019-04-11 07:26:03 -0400 | [diff] [blame] | 30 | import java.util.HashSet; |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
Amin Shaikh | 96e60d7 | 2019-04-24 13:47:57 -0400 | [diff] [blame] | 34 | import java.util.concurrent.Executor; |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 35 | import java.util.stream.Collectors; |
| 36 | |
| 37 | class ThemeOverlayManager { |
| 38 | private static final String TAG = "ThemeOverlayManager"; |
| 39 | private static final boolean DEBUG = false; |
| 40 | |
| 41 | @VisibleForTesting |
| 42 | static final String ANDROID_PACKAGE = "android"; |
| 43 | @VisibleForTesting |
| 44 | static final String SETTINGS_PACKAGE = "com.android.settings"; |
| 45 | @VisibleForTesting |
| 46 | static final String SYSUI_PACKAGE = "com.android.systemui"; |
| 47 | |
| 48 | @VisibleForTesting |
| 49 | static final String OVERLAY_CATEGORY_COLOR = "android.theme.customization.accent_color"; |
| 50 | @VisibleForTesting |
| 51 | static final String OVERLAY_CATEGORY_FONT = "android.theme.customization.font"; |
| 52 | @VisibleForTesting |
| 53 | static final String OVERLAY_CATEGORY_SHAPE = |
| 54 | "android.theme.customization.adaptive_icon_shape"; |
| 55 | @VisibleForTesting |
| 56 | static final String OVERLAY_CATEGORY_ICON_ANDROID = |
| 57 | "android.theme.customization.icon_pack.android"; |
| 58 | @VisibleForTesting |
| 59 | static final String OVERLAY_CATEGORY_ICON_SYSUI = |
| 60 | "android.theme.customization.icon_pack.systemui"; |
| 61 | @VisibleForTesting |
| 62 | static final String OVERLAY_CATEGORY_ICON_SETTINGS = |
| 63 | "android.theme.customization.icon_pack.settings"; |
| 64 | @VisibleForTesting |
| 65 | static final String OVERLAY_CATEGORY_ICON_LAUNCHER = |
| 66 | "android.theme.customization.icon_pack.launcher"; |
Amin Shaikh | 0974b88 | 2019-04-23 17:14:56 -0400 | [diff] [blame] | 67 | @VisibleForTesting |
| 68 | static final String OVERLAY_CATEGORY_ICON_THEME_PICKER = |
| 69 | "android.theme.customization.icon_pack.themepicker"; |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 70 | |
Amin Shaikh | b56b29c | 2019-04-11 07:26:03 -0400 | [diff] [blame] | 71 | /* |
| 72 | * All theme customization categories used by the system, in order that they should be applied, |
| 73 | * starts with launcher and grouped by target package. |
| 74 | */ |
| 75 | static final List<String> THEME_CATEGORIES = Lists.newArrayList( |
| 76 | OVERLAY_CATEGORY_ICON_LAUNCHER, |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 77 | OVERLAY_CATEGORY_SHAPE, |
Amin Shaikh | b56b29c | 2019-04-11 07:26:03 -0400 | [diff] [blame] | 78 | OVERLAY_CATEGORY_FONT, |
| 79 | OVERLAY_CATEGORY_COLOR, |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 80 | OVERLAY_CATEGORY_ICON_ANDROID, |
| 81 | OVERLAY_CATEGORY_ICON_SYSUI, |
Amin Shaikh | 0974b88 | 2019-04-23 17:14:56 -0400 | [diff] [blame] | 82 | OVERLAY_CATEGORY_ICON_SETTINGS, |
| 83 | OVERLAY_CATEGORY_ICON_THEME_PICKER); |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 84 | |
| 85 | /* Categories that need to applied to the current user as well as the system user. */ |
| 86 | @VisibleForTesting |
| 87 | static final Set<String> SYSTEM_USER_CATEGORIES = Sets.newHashSet( |
| 88 | OVERLAY_CATEGORY_COLOR, |
| 89 | OVERLAY_CATEGORY_FONT, |
| 90 | OVERLAY_CATEGORY_SHAPE, |
| 91 | OVERLAY_CATEGORY_ICON_ANDROID, |
| 92 | OVERLAY_CATEGORY_ICON_SYSUI); |
| 93 | |
| 94 | /* Allowed overlay categories for each target package. */ |
| 95 | private final Map<String, Set<String>> mTargetPackageToCategories = new ArrayMap<>(); |
| 96 | /* Target package for each overlay category. */ |
| 97 | private final Map<String, String> mCategoryToTargetPackage = new ArrayMap<>(); |
| 98 | private final OverlayManager mOverlayManager; |
Amin Shaikh | 96e60d7 | 2019-04-24 13:47:57 -0400 | [diff] [blame] | 99 | private final Executor mExecutor; |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 100 | private final String mLauncherPackage; |
Amin Shaikh | 0974b88 | 2019-04-23 17:14:56 -0400 | [diff] [blame] | 101 | private final String mThemePickerPackage; |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 102 | |
Amin Shaikh | 96e60d7 | 2019-04-24 13:47:57 -0400 | [diff] [blame] | 103 | ThemeOverlayManager(OverlayManager overlayManager, Executor executor, |
Amin Shaikh | 0974b88 | 2019-04-23 17:14:56 -0400 | [diff] [blame] | 104 | String launcherPackage, String themePickerPackage) { |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 105 | mOverlayManager = overlayManager; |
Amin Shaikh | 96e60d7 | 2019-04-24 13:47:57 -0400 | [diff] [blame] | 106 | mExecutor = executor; |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 107 | mLauncherPackage = launcherPackage; |
Amin Shaikh | 0974b88 | 2019-04-23 17:14:56 -0400 | [diff] [blame] | 108 | mThemePickerPackage = themePickerPackage; |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 109 | mTargetPackageToCategories.put(ANDROID_PACKAGE, Sets.newHashSet( |
| 110 | OVERLAY_CATEGORY_COLOR, OVERLAY_CATEGORY_FONT, |
| 111 | OVERLAY_CATEGORY_SHAPE, OVERLAY_CATEGORY_ICON_ANDROID)); |
| 112 | mTargetPackageToCategories.put(SYSUI_PACKAGE, |
| 113 | Sets.newHashSet(OVERLAY_CATEGORY_ICON_SYSUI)); |
| 114 | mTargetPackageToCategories.put(SETTINGS_PACKAGE, |
| 115 | Sets.newHashSet(OVERLAY_CATEGORY_ICON_SETTINGS)); |
| 116 | mTargetPackageToCategories.put(mLauncherPackage, |
| 117 | Sets.newHashSet(OVERLAY_CATEGORY_ICON_LAUNCHER)); |
Amin Shaikh | 0974b88 | 2019-04-23 17:14:56 -0400 | [diff] [blame] | 118 | mTargetPackageToCategories.put(mThemePickerPackage, |
| 119 | Sets.newHashSet(OVERLAY_CATEGORY_ICON_THEME_PICKER)); |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 120 | mCategoryToTargetPackage.put(OVERLAY_CATEGORY_COLOR, ANDROID_PACKAGE); |
| 121 | mCategoryToTargetPackage.put(OVERLAY_CATEGORY_FONT, ANDROID_PACKAGE); |
| 122 | mCategoryToTargetPackage.put(OVERLAY_CATEGORY_SHAPE, ANDROID_PACKAGE); |
| 123 | mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_ANDROID, ANDROID_PACKAGE); |
| 124 | mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_SYSUI, SYSUI_PACKAGE); |
| 125 | mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_SETTINGS, SETTINGS_PACKAGE); |
| 126 | mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_LAUNCHER, mLauncherPackage); |
Amin Shaikh | 0974b88 | 2019-04-23 17:14:56 -0400 | [diff] [blame] | 127 | mCategoryToTargetPackage.put(OVERLAY_CATEGORY_ICON_THEME_PICKER, mThemePickerPackage); |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Apply the set of overlay packages to the set of {@code UserHandle}s provided. Overlays that |
| 132 | * affect sysui will also be applied to the system user. |
| 133 | */ |
| 134 | void applyCurrentUserOverlays( |
| 135 | Map<String, String> categoryToPackage, Set<UserHandle> userHandles) { |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 136 | // Disable all overlays that have not been specified in the user setting. |
Amin Shaikh | b56b29c | 2019-04-11 07:26:03 -0400 | [diff] [blame] | 137 | final Set<String> overlayCategoriesToDisable = new HashSet<>(THEME_CATEGORIES); |
| 138 | overlayCategoriesToDisable.removeAll(categoryToPackage.keySet()); |
| 139 | final Set<String> targetPackagesToQuery = overlayCategoriesToDisable.stream() |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 140 | .map(category -> mCategoryToTargetPackage.get(category)) |
Amin Shaikh | b56b29c | 2019-04-11 07:26:03 -0400 | [diff] [blame] | 141 | .collect(Collectors.toSet()); |
| 142 | final List<OverlayInfo> overlays = new ArrayList<>(); |
| 143 | targetPackagesToQuery.forEach(targetPackage -> overlays.addAll(mOverlayManager |
| 144 | .getOverlayInfosForTarget(targetPackage, UserHandle.SYSTEM))); |
| 145 | final Map<String, String> overlaysToDisable = overlays.stream() |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 146 | .filter(o -> |
| 147 | mTargetPackageToCategories.get(o.targetPackageName).contains(o.category)) |
| 148 | .filter(o -> overlayCategoriesToDisable.contains(o.category)) |
| 149 | .filter(o -> o.isEnabled()) |
Amin Shaikh | b56b29c | 2019-04-11 07:26:03 -0400 | [diff] [blame] | 150 | .collect(Collectors.toMap((o) -> o.category, (o) -> o.packageName)); |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 151 | |
Amin Shaikh | b56b29c | 2019-04-11 07:26:03 -0400 | [diff] [blame] | 152 | // Toggle overlays in the order of THEME_CATEGORIES. |
| 153 | for (String category : THEME_CATEGORIES) { |
| 154 | if (categoryToPackage.containsKey(category)) { |
| 155 | setEnabled(categoryToPackage.get(category), category, userHandles, true); |
| 156 | } else if (overlaysToDisable.containsKey(category)) { |
| 157 | setEnabled(overlaysToDisable.get(category), category, userHandles, false); |
| 158 | } |
| 159 | } |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private void setEnabled( |
| 163 | String packageName, String category, Set<UserHandle> handles, boolean enabled) { |
| 164 | for (UserHandle userHandle : handles) { |
Amin Shaikh | 96e60d7 | 2019-04-24 13:47:57 -0400 | [diff] [blame] | 165 | setEnabledAsync(packageName, userHandle, enabled); |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 166 | } |
| 167 | if (!handles.contains(UserHandle.SYSTEM) && SYSTEM_USER_CATEGORIES.contains(category)) { |
Amin Shaikh | 96e60d7 | 2019-04-24 13:47:57 -0400 | [diff] [blame] | 168 | setEnabledAsync(packageName, UserHandle.SYSTEM, enabled); |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Amin Shaikh | 96e60d7 | 2019-04-24 13:47:57 -0400 | [diff] [blame] | 172 | private void setEnabledAsync(String pkg, UserHandle userHandle, boolean enabled) { |
| 173 | mExecutor.execute(() -> { |
| 174 | if (DEBUG) Log.d(TAG, String.format("setEnabled: %s %s %b", pkg, userHandle, enabled)); |
Amin Shaikh | 44a0e1c | 2019-06-24 13:36:45 -0400 | [diff] [blame] | 175 | try { |
| 176 | if (enabled) { |
| 177 | mOverlayManager.setEnabledExclusiveInCategory(pkg, userHandle); |
| 178 | } else { |
| 179 | mOverlayManager.setEnabled(pkg, false, userHandle); |
| 180 | } |
Winson | 792a0aa | 2020-01-15 17:16:14 -0800 | [diff] [blame] | 181 | } catch (SecurityException | IllegalStateException e) { |
Amin Shaikh | 44a0e1c | 2019-06-24 13:36:45 -0400 | [diff] [blame] | 182 | Log.e(TAG, |
| 183 | String.format("setEnabled failed: %s %s %b", pkg, userHandle, enabled), e); |
Amin Shaikh | 96e60d7 | 2019-04-24 13:47:57 -0400 | [diff] [blame] | 184 | } |
| 185 | }); |
Amin Shaikh | fd6402e | 2019-03-28 16:23:49 -0400 | [diff] [blame] | 186 | } |
| 187 | } |