blob: 11dc0c47360cd01c01acdc230c4650de1b02b180 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
2 * Copyright (C) 2015 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
17package com.android.launcher3;
18
Hyunyoung Songc55a3502018-12-04 15:43:16 -080019import static com.android.launcher3.Utilities.getDevicePrefs;
Tracy Zhoued5f3082020-04-20 01:13:26 -070020import static com.android.launcher3.Utilities.getPointString;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010021import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Sunny Goyal35c7b192021-04-20 16:51:10 -070022import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
23import static com.android.launcher3.util.DisplayController.CHANGE_SIZE;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080024import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070025import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070026
Sunny Goyalc6205602015-05-21 20:46:33 -070027import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070028import android.appwidget.AppWidgetHostView;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070029import android.content.BroadcastReceiver;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070030import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070031import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070032import android.content.Intent;
Sunny Goyal27835952017-01-13 12:15:53 -080033import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080034import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070035import android.content.res.TypedArray;
36import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070037import android.graphics.Point;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070038import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080039import android.text.TextUtils;
40import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070041import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070042import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080043import android.util.SparseArray;
44import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070045import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080046import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070047
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070048import androidx.annotation.Nullable;
49import androidx.annotation.VisibleForTesting;
50
Sunny Goyal905262c2019-05-03 16:50:43 -070051import com.android.launcher3.graphics.IconShape;
Winson Chung8e64bba2021-04-20 13:26:26 -070052import com.android.launcher3.testing.TestProtocol;
Sunny Goyalfd58da62020-08-11 12:06:49 -070053import com.android.launcher3.util.DisplayController;
54import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080055import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070056import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080057import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070058
Sunny Goyal819e1932016-07-07 16:43:58 -070059import org.xmlpull.v1.XmlPullParser;
60import org.xmlpull.v1.XmlPullParserException;
61
62import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070063import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080064import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070065
66public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070067
Hyunyoung Songc55a3502018-12-04 15:43:16 -080068 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070069 // We do not need any synchronization for this variable as its only written on UI thread.
70 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070071 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070072
Tracy Zhoued5f3082020-04-20 01:13:26 -070073 public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size";
74 public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count";
75
Hyunyoung Songc55a3502018-12-04 15:43:16 -080076 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080077
Sunny Goyal53d7ee42015-05-22 12:25:45 -070078 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
79
Sunny Goyal87dc48b2018-10-12 11:42:33 -070080 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080081 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
82
83 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070084
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070085 // Constants that affects the interpolation curve between statically defined device profile
86 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080087 private static final float KNEARESTNEIGHBOR = 3;
88 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070089
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070090 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080091 private static final float WEIGHT_EFFICIENT = 100000f;
92
93 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
94 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070095
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070096 /**
97 * Number of icons per row and column in the workspace.
98 */
Adam Cohen2e6da152015-05-06 11:42:25 -070099 public int numRows;
100 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700101
102 /**
103 * Number of icons per row and column in the folder.
104 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700105 public int numFolderRows;
106 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700107 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800108 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700109 public float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200110 public float landscapeIconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700111 public int iconBitmapSize;
112 public int fillResIconDpi;
113 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000114 public float allAppsIconSize;
115 public float allAppsIconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700116
Jon Mirandae126d722021-02-25 10:45:20 -0500117 public float minCellHeight;
118 public float minCellWidth;
119 public float borderSpacing;
120
Sunny Goyal5bc18462019-01-07 15:13:39 -0800121 private SparseArray<TypedValue> mExtraAttrs;
122
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700123 /**
124 * Number of icons inside the hotseat area.
125 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700126 protected int numShownHotseatIcons;
127
128 /**
129 * Number of icons inside the hotseat area that is stored in the database. This is greater than
130 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
131 * sizes that share the same DB.
132 */
133 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700134
Jon Miranda6f7e9702019-09-16 14:44:14 -0700135 /**
136 * Number of columns in the all apps list.
137 */
138 public int numAllAppsColumns;
139
Jon Mirandae126d722021-02-25 10:45:20 -0500140 /**
141 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
142 */
143 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400144 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500145
Tracy Zhou7df93d22020-01-27 13:44:06 -0800146 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800147 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700148 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700149
cuijiaxingabda8d72017-03-20 09:51:36 -0700150 public DeviceProfile landscapeProfile;
151 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700152
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400153 @Nullable public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500154
Sunny Goyal6f866092016-03-17 17:04:15 -0700155 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700156 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700157
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700158 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700159 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700160
Sunny Goyalf633ef52018-03-13 09:57:05 -0700161 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700162 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700163
Sunny Goyalf633ef52018-03-13 09:57:05 -0700164 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800165 numRows = p.numRows;
166 numColumns = p.numColumns;
167 numFolderRows = p.numFolderRows;
168 numFolderColumns = p.numFolderColumns;
169 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800170 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800171 landscapeIconSize = p.landscapeIconSize;
Tony Wickham9459f312020-06-15 13:30:20 -0500172 iconBitmapSize = p.iconBitmapSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800173 iconTextSize = p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200174 landscapeIconTextSize = p.landscapeIconTextSize;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700175 numShownHotseatIcons = p.numShownHotseatIcons;
176 numDatabaseHotseatIcons = p.numDatabaseHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700177 numAllAppsColumns = p.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500178 isScalable = p.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400179 devicePaddingId = p.devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500180 minCellHeight = p.minCellHeight;
181 minCellWidth = p.minCellWidth;
182 borderSpacing = p.borderSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800183 dbFile = p.dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000184 allAppsIconSize = p.allAppsIconSize;
185 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800186 defaultLayoutId = p.defaultLayoutId;
187 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800188 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700189 mOverlayMonitor = p.mOverlayMonitor;
Jon Miranda228877d2021-02-09 11:05:00 -0500190 devicePaddings = p.devicePaddings;
Adam Cohen2e6da152015-05-06 11:42:25 -0700191 }
192
Sunny Goyalbbf01842015-10-08 07:41:15 -0700193 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700194 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700195 String gridName = getCurrentGridName(context);
196 String newGridName = initGrid(context, gridName);
197 if (!newGridName.equals(gridName)) {
198 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
199 }
Tracy Zhoued5f3082020-04-20 01:13:26 -0700200 Utilities.getPrefs(context).edit()
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700201 .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numDatabaseHotseatIcons)
Tracy Zhoued5f3082020-04-20 01:13:26 -0700202 .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows))
203 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700204
Sunny Goyal35c7b192021-04-20 16:51:10 -0700205 DisplayController.INSTANCE.get(context).addChangeListener(
206 (info, flags) -> {
207 if ((flags & (CHANGE_SIZE | CHANGE_DENSITY)) != 0) {
208 onConfigChanged(context);
209 }
210 });
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700211 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700212 }
213
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700214 /**
215 * This constructor should NOT have any monitors by design.
216 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800217 public InvariantDeviceProfile(Context context, String gridName) {
218 String newName = initGrid(context, gridName);
219 if (newName == null || !newName.equals(gridName)) {
220 throw new IllegalArgumentException("Unknown grid name");
221 }
222 }
223
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700224 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800225 * This constructor should NOT have any monitors by design.
226 */
227 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700228 // Ensure that the main device profile is initialized
229 InvariantDeviceProfile originalProfile = INSTANCE.get(context);
230 String gridName = getCurrentGridName(context);
231
232 // Get the display info based on default display and interpolate it to existing display
233 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700234 DisplayController.INSTANCE.get(context).getInfo(),
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235 getPredefinedDeviceProfiles(context, gridName));
236
Alex Chau417bd722021-01-26 15:22:18 +0000237 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700238 DisplayOption myDisplayOption = invDistWeightedInterpolate(
239 myInfo, getPredefinedDeviceProfiles(context, gridName));
240
241 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
242 .add(myDisplayOption);
243 result.iconSize = defaultDisplayOption.iconSize;
244 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700245 result.numShownHotseatIcons = myDisplayOption.numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000246 if (defaultDisplayOption.allAppsIconSize < myDisplayOption.allAppsIconSize) {
247 result.allAppsIconSize = defaultDisplayOption.allAppsIconSize;
248 result.numAllAppsColumns = defaultDisplayOption.numAllAppsColumns;
249 } else {
250 result.allAppsIconSize = myDisplayOption.allAppsIconSize;
251 result.numAllAppsColumns = myDisplayOption.numAllAppsColumns;
252 }
Jon Mirandae126d722021-02-25 10:45:20 -0500253 result.minCellHeight = defaultDisplayOption.minCellHeight;
254 result.minCellWidth = defaultDisplayOption.minCellWidth;
255 result.borderSpacing = defaultDisplayOption.borderSpacing;
Jon Miranda228877d2021-02-09 11:05:00 -0500256
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700257 initGrid(context, myInfo, result);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800258 }
259
Tracy Zhou42255d22020-03-13 00:38:11 -0700260 public static String getCurrentGridName(Context context) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100261 if (ENABLE_TWO_PANEL_HOME.get()) {
262 return ENABLE_TWO_PANEL_HOME.key;
263 }
Tracy Zhouc6060e62020-04-27 13:05:34 -0700264 return Utilities.isGridOptionsEnabled(context)
265 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700266 }
267
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800268 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800269 * Retrieve system defined or RRO overriden icon shape.
270 */
271 private static String getIconShapePath(Context context) {
272 if (CONFIG_ICON_MASK_RES_ID == 0) {
273 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
274 return "";
275 }
276 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
277 }
278
Sunny Goyaleff44f32019-01-09 17:29:49 -0800279 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700280 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700281 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
282
283 DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions);
284 initGrid(context, displayInfo, displayOption);
285 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800286 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700287
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700288 private void initGrid(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700289 Context context, Info displayInfo, DisplayOption displayOption) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700290 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700291 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000292 numRows = closestProfile.numRows;
293 numColumns = closestProfile.numColumns;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700294 numDatabaseHotseatIcons = closestProfile.numDatabaseHotseatIcons;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000295 dbFile = closestProfile.dbFile;
296 defaultLayoutId = closestProfile.defaultLayoutId;
297 demoModeLayoutId = closestProfile.demoModeLayoutId;
298 numFolderRows = closestProfile.numFolderRows;
299 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500300 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400301 devicePaddingId = closestProfile.devicePaddingId;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000302
303 mExtraAttrs = closestProfile.extraAttrs;
304
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700305 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000306 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700307 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700308 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700309 iconTextSize = displayOption.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200310 landscapeIconTextSize = displayOption.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000311 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
312
Jon Mirandae126d722021-02-25 10:45:20 -0500313 minCellHeight = displayOption.minCellHeight;
314 minCellWidth = displayOption.minCellWidth;
315 borderSpacing = displayOption.borderSpacing;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700316 numShownHotseatIcons = Math.round(displayOption.numShownHotseatIcons);
Alex Chau7c439722021-03-24 11:32:44 +0000317 numAllAppsColumns = Math.round(displayOption.numAllAppsColumns);
Jon Mirandae126d722021-02-25 10:45:20 -0500318
Tracy Zhouc6060e62020-04-27 13:05:34 -0700319 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700320 allAppsIconSize = displayOption.allAppsIconSize;
321 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000322 } else {
323 allAppsIconSize = iconSize;
324 allAppsIconTextSize = iconTextSize;
325 }
326
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400327 if (devicePaddingId != 0) {
328 devicePaddings = new DevicePaddings(context, devicePaddingId);
329 }
330
Sunny Goyalae190ff2020-04-14 00:19:01 +0000331 // If the partner customization apk contains any grid overrides, apply them
332 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700333 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700334
Winson Chung13c1c2c2019-09-06 11:46:19 -0700335 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700336 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700337 // same in any orientation.
338 int smallSide = Math.min(realSize.x, realSize.y);
339 int largeSide = Math.max(realSize.x, realSize.y);
340
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700341 DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo)
342 .setSizeRange(new Point(displayInfo.smallestSize),
343 new Point(displayInfo.largestSize));
Winson Chung8e64bba2021-04-20 13:26:26 -0700344 if (TestProtocol.sDebugTracing) {
345 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED,
346 "largeSide=" + largeSide + " smallSide=" + smallSide);
347 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700348
349 landscapeProfile = builder.setSize(largeSide, smallSide).build();
350 portraitProfile = builder.setSize(smallSide, largeSide).build();
Sunny Goyal6f866092016-03-17 17:04:15 -0700351
352 // We need to ensure that there is enough extra space in the wallpaper
353 // for the intended parallax effects
354 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
355 defaultWallpaperSize = new Point(
356 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
357 largeSide);
358 } else {
359 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
360 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700361
362 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
363 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700364 }
365
Sunny Goyal5bc18462019-01-07 15:13:39 -0800366 @Nullable
367 public TypedValue getAttrValue(int attr) {
368 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
369 }
370
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700371 public void addOnChangeListener(OnIDPChangeListener listener) {
372 mChangeListeners.add(listener);
373 }
374
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800375 public void removeOnChangeListener(OnIDPChangeListener listener) {
376 mChangeListeners.remove(listener);
377 }
378
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800379 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800380 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
381 // Good place to check if grid size changed in themepicker when launcher was dead.
382 if (savedIconMaskPath.isEmpty()) {
383 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
384 .apply();
385 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
386 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
387 .apply();
388 apply(context, CHANGE_FLAG_ICON_PARAMS);
389 }
390 }
391
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800392 public void setCurrentGrid(Context context, String gridName) {
393 Context appContext = context.getApplicationContext();
394 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700395 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800396 }
397
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700398 private void onConfigChanged(Context context) {
Winson Chungdc0f3972021-04-20 18:18:51 -0700399 if (TestProtocol.sDebugTracing) {
400 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED, "IDP.onConfigChanged");
401 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700402 // Config changes, what shall we do?
403 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
404
405 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700406 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700407 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700408
409 int changeFlags = 0;
410 if (numRows != oldProfile.numRows ||
411 numColumns != oldProfile.numColumns ||
412 numFolderColumns != oldProfile.numFolderColumns ||
413 numFolderRows != oldProfile.numFolderRows ||
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700414 numDatabaseHotseatIcons != oldProfile.numDatabaseHotseatIcons) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700415 changeFlags |= CHANGE_FLAG_GRID;
416 }
417
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800418 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
419 !iconShapePath.equals(oldProfile.iconShapePath)) {
420 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700421 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800422 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700423 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800424 }
425
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800426 apply(context, changeFlags);
427 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700428
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800429 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700430 for (OnIDPChangeListener listener : mChangeListeners) {
431 listener.onIdpChanged(changeFlags, this);
432 }
433 }
434
Sunny Goyalae190ff2020-04-14 00:19:01 +0000435 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800436 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700437 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
438 final int depth = parser.getDepth();
439 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700440 while (((type = parser.next()) != XmlPullParser.END_TAG ||
441 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800442 if ((type == XmlPullParser.START_TAG)
443 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800444
445 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
446 final int displayDepth = parser.getDepth();
447 while (((type = parser.next()) != XmlPullParser.END_TAG ||
448 parser.getDepth() > displayDepth)
449 && type != XmlPullParser.END_DOCUMENT) {
450 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
451 parser.getName())) {
452 profiles.add(new DisplayOption(
453 gridOption, context, Xml.asAttributeSet(parser)));
454 }
455 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700456 }
457 }
458 } catch (IOException|XmlPullParserException e) {
459 throw new RuntimeException(e);
460 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800461
Sunny Goyalae190ff2020-04-14 00:19:01 +0000462 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800463 if (!TextUtils.isEmpty(gridName)) {
464 for (DisplayOption option : profiles) {
465 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000466 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800467 }
468 }
469 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000470 if (filteredProfiles.isEmpty()) {
471 // No grid found, use the default options
472 for (DisplayOption option : profiles) {
473 if (option.canBeDefault) {
474 filteredProfiles.add(option);
475 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800476 }
477 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000478 if (filteredProfiles.isEmpty()) {
479 throw new RuntimeException("No display option with canBeDefault=true");
480 }
481 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700482 }
483
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700484 private int getLauncherIconDensity(int requiredSize) {
485 // Densities typically defined by an app.
486 int[] densityBuckets = new int[] {
487 DisplayMetrics.DENSITY_LOW,
488 DisplayMetrics.DENSITY_MEDIUM,
489 DisplayMetrics.DENSITY_TV,
490 DisplayMetrics.DENSITY_HIGH,
491 DisplayMetrics.DENSITY_XHIGH,
492 DisplayMetrics.DENSITY_XXHIGH,
493 DisplayMetrics.DENSITY_XXXHIGH
494 };
495
496 int density = DisplayMetrics.DENSITY_XXXHIGH;
497 for (int i = densityBuckets.length - 1; i >= 0; i--) {
498 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
499 / DisplayMetrics.DENSITY_DEFAULT;
500 if (expectedSize >= requiredSize) {
501 density = densityBuckets[i];
502 }
503 }
504
505 return density;
506 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700507
Adam Cohen2e6da152015-05-06 11:42:25 -0700508 /**
509 * Apply any Partner customization grid overrides.
510 *
511 * Currently we support: all apps row / column count.
512 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700513 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
514 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700515 if (p != null) {
516 p.applyInvariantDeviceProfileOverrides(this, dm);
517 }
518 }
519
Sunny Goyal415f1732018-11-29 10:33:47 -0800520 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700521 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700522 }
523
Sunny Goyal415f1732018-11-29 10:33:47 -0800524 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700525 static DisplayOption invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700526 Info displayInfo, ArrayList<DisplayOption> points) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700527 Point smallestSize = new Point(displayInfo.smallestSize);
528 Point largestSize = new Point(displayInfo.largestSize);
529
530 // This guarantees that width < height
Sunny Goyal35c7b192021-04-20 16:51:10 -0700531 float width = Utilities.dpiFromPx((float) Math.min(smallestSize.x, smallestSize.y),
532 displayInfo.densityDpi);
533 float height = Utilities.dpiFromPx((float) Math.min(largestSize.x, largestSize.y),
534 displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700535
536 // Sort the profiles based on the closeness to the device size
537 Collections.sort(points, (a, b) ->
538 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
539 dist(width, height, b.minWidthDps, b.minHeightDps)));
540
541 GridOption closestOption = points.get(0).grid;
542 float weights = 0;
543
544 DisplayOption p = points.get(0);
545 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
546 return p;
547 }
548
549 DisplayOption out = new DisplayOption(closestOption);
550 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
551 p = points.get(i);
552 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
553 weights += w;
554 out.add(new DisplayOption().add(p).multiply(w));
555 }
556 return out.multiply(1.0f / weights);
557 }
558
559 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800560 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000561 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700562 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700563
Sunny Goyal415f1732018-11-29 10:33:47 -0800564 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700565 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
566 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700567 }
568
Sunny Goyal415f1732018-11-29 10:33:47 -0800569 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700570 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800571 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700572 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
573 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800574 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700575 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800576 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700577 }
578
Sunny Goyal27835952017-01-13 12:15:53 -0800579 public DeviceProfile getDeviceProfile(Context context) {
Winson Chung8e64bba2021-04-20 13:26:26 -0700580 if (TestProtocol.sDebugTracing) {
581 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED, "getDeviceProfile: orientation="
Winson Chung0bba6a72021-04-20 15:41:51 -0700582 + context.getResources().getConfiguration().orientation);
Winson Chung8e64bba2021-04-20 13:26:26 -0700583 }
Sunny Goyal27835952017-01-13 12:15:53 -0800584 return context.getResources().getConfiguration().orientation
585 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
586 }
587
Sunny Goyal415f1732018-11-29 10:33:47 -0800588 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700589 float d = dist(x0, y0, x1, y1);
590 if (Float.compare(d, 0f) == 0) {
591 return Float.POSITIVE_INFINITY;
592 }
593 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
594 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700595
596 /**
597 * As a ratio of screen height, the total distance we want the parallax effect to span
598 * horizontally
599 */
600 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
601 float aspectRatio = width / (float) height;
602
603 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
604 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
605 // We will use these two data points to extrapolate how much the wallpaper parallax effect
606 // to span (ie travel) at any aspect ratio:
607
608 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
609 final float ASPECT_RATIO_PORTRAIT = 10/16f;
610 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
611 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
612
613 // To find out the desired width at different aspect ratios, we use the following two
614 // formulas, where the coefficient on x is the aspect ratio (width/height):
615 // (16/10)x + y = 1.5
616 // (10/16)x + y = 1.2
617 // We solve for x and y and end up with a final formula:
618 final float x =
619 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
620 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
621 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
622 return x * aspectRatio + y;
623 }
624
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700625 public interface OnIDPChangeListener {
626
627 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
628 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800629
630
Sunny Goyaleff44f32019-01-09 17:29:49 -0800631 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800632
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800633 public static final String TAG_NAME = "grid-option";
634
Sunny Goyaleff44f32019-01-09 17:29:49 -0800635 public final String name;
636 public final int numRows;
637 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800638
639 private final int numFolderRows;
640 private final int numFolderColumns;
641
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700642 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800643
Tracy Zhou7df93d22020-01-27 13:44:06 -0800644 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000645
Sunny Goyal415f1732018-11-29 10:33:47 -0800646 private final int defaultLayoutId;
647 private final int demoModeLayoutId;
648
Jon Mirandae126d722021-02-25 10:45:20 -0500649 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400650 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500651
Jon Mirandaec1277e2021-03-25 10:41:54 -0400652 public final boolean visible;
653
Sunny Goyal5bc18462019-01-07 15:13:39 -0800654 private final SparseArray<TypedValue> extraAttrs;
655
Sunny Goyaleff44f32019-01-09 17:29:49 -0800656 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800657 TypedArray a = context.obtainStyledAttributes(
658 attrs, R.styleable.GridDisplayOption);
659 name = a.getString(R.styleable.GridDisplayOption_name);
660 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
661 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
662
Tracy Zhou7df93d22020-01-27 13:44:06 -0800663 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800664 defaultLayoutId = a.getResourceId(
665 R.styleable.GridDisplayOption_defaultLayoutId, 0);
666 demoModeLayoutId = a.getResourceId(
667 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700668 numDatabaseHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800669 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
670 numFolderRows = a.getInt(
671 R.styleable.GridDisplayOption_numFolderRows, numRows);
672 numFolderColumns = a.getInt(
673 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700674
Jon Mirandae126d722021-02-25 10:45:20 -0500675 isScalable = a.getBoolean(
676 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400677 devicePaddingId = a.getResourceId(
678 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500679
Jon Mirandaec1277e2021-03-25 10:41:54 -0400680 visible = a.getBoolean(R.styleable.GridDisplayOption_visible, true);
681
Sunny Goyal415f1732018-11-29 10:33:47 -0800682 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800683
684 extraAttrs = Themes.createValueMap(context, attrs,
685 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800686 }
687 }
688
689 private static final class DisplayOption {
690 private final GridOption grid;
691
Sunny Goyal415f1732018-11-29 10:33:47 -0800692 private final float minWidthDps;
693 private final float minHeightDps;
694 private final boolean canBeDefault;
695
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700696 private float numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000697 private float numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500698 private float minCellHeight;
699 private float minCellWidth;
700 private float borderSpacing;
701
Sunny Goyal415f1732018-11-29 10:33:47 -0800702 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800703 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700704 private float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200705 private float landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000706 private float allAppsIconSize;
707 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800708
709 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
710 this.grid = grid;
711
712 TypedArray a = context.obtainStyledAttributes(
713 attrs, R.styleable.ProfileDisplayOption);
714
Sunny Goyal415f1732018-11-29 10:33:47 -0800715 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
716 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
717 canBeDefault = a.getBoolean(
718 R.styleable.ProfileDisplayOption_canBeDefault, false);
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700719 numShownHotseatIcons = a.getInt(R.styleable.ProfileDisplayOption_numShownHotseatIcons,
720 grid.numDatabaseHotseatIcons);
Alex Chau7c439722021-03-24 11:32:44 +0000721 numAllAppsColumns = a.getInt(R.styleable.ProfileDisplayOption_numAllAppsColumns,
722 grid.numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800723
Jon Mirandae126d722021-02-25 10:45:20 -0500724 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
725 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
726 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
727
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700728 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800729 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
730 iconSize);
731 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Andras Kloczl6057cb92021-04-07 13:45:17 +0200732 landscapeIconTextSize = a.getFloat(
733 R.styleable.ProfileDisplayOption_landscapeIconTextSize, iconTextSize);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700734
Sunny Goyalae190ff2020-04-14 00:19:01 +0000735 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
736 iconSize);
737 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
738 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800739 a.recycle();
740 }
741
742 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700743 this(null);
744 }
745
746 DisplayOption(GridOption grid) {
747 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800748 minWidthDps = 0;
749 minHeightDps = 0;
750 canBeDefault = false;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700751 numShownHotseatIcons = 0;
Alex Chau7c439722021-03-24 11:32:44 +0000752 numAllAppsColumns = 0;
Jon Mirandae126d722021-02-25 10:45:20 -0500753 minCellHeight = 0;
754 minCellWidth = 0;
755 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800756 }
757
758 private DisplayOption multiply(float w) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700759 numShownHotseatIcons *= w;
Alex Chau7c439722021-03-24 11:32:44 +0000760 numAllAppsColumns *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800761 iconSize *= w;
762 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000763 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800764 iconTextSize *= w;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200765 landscapeIconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000766 allAppsIconTextSize *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500767 minCellHeight *= w;
768 minCellWidth *= w;
769 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800770 return this;
771 }
772
773 private DisplayOption add(DisplayOption p) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700774 numShownHotseatIcons += p.numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000775 numAllAppsColumns += p.numAllAppsColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800776 iconSize += p.iconSize;
777 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000778 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800779 iconTextSize += p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200780 landscapeIconTextSize += p.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000781 allAppsIconTextSize += p.allAppsIconTextSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500782 minCellHeight += p.minCellHeight;
783 minCellWidth += p.minCellWidth;
784 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800785 return this;
786 }
787 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700788
789 private class OverlayMonitor extends BroadcastReceiver {
790
791 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
792
793 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700794 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700795 }
796
797 @Override
798 public void onReceive(Context context, Intent intent) {
799 onConfigChanged(context);
800 }
801 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000802}