blob: 7eb085a787f26ad94a522d8daefca75ecd5c8d62 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
Thales Lima1de4d552021-10-13 16:13:25 +01002 * Copyright (C) 2021 The Android Open Source Project
Adam Cohen2e6da152015-05-06 11:42:25 -07003 *
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
Stefan Andonian4c9612b2023-02-22 00:00:03 +000019import static com.android.launcher3.LauncherPrefs.GRID_NAME;
Sunny Goyal19ff7282021-04-22 10:12:54 -070020import static com.android.launcher3.Utilities.dpiFromPx;
Thales Limab35faed2022-09-05 16:30:01 -030021import static com.android.launcher3.testing.shared.ResourceUtils.INVALID_RESOURCE_HANDLE;
Sunny Goyal35c7b192021-04-20 16:51:10 -070022import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Alex Chau6ed408f2022-03-04 12:58:05 +000023import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE;
Sunny Goyal19ff7282021-04-22 10:12:54 -070024import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080025import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070026
Sunny Goyalc6205602015-05-21 20:46:33 -070027import android.annotation.TargetApi;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080029import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080030import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070031import android.content.res.TypedArray;
32import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070033import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010034import android.graphics.PointF;
Sunny Goyal415f1732018-11-29 10:33:47 -080035import android.text.TextUtils;
36import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070037import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010038import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080039import android.util.SparseArray;
Sunny Goyal819e1932016-07-07 16:43:58 -070040import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080041import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070042
Thales Limab35faed2022-09-05 16:30:01 -030043import androidx.annotation.DimenRes;
Alex Chau1c883d82021-12-01 18:43:10 +000044import androidx.annotation.IntDef;
Thales Limab35faed2022-09-05 16:30:01 -030045import androidx.annotation.StyleRes;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070046import androidx.annotation.VisibleForTesting;
Thales Limaae0d7ef2022-11-16 15:53:43 +000047import androidx.annotation.XmlRes;
Thales Limab8c05952022-05-23 16:58:38 +010048import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070049
Thales Limaec5abba2023-04-05 16:33:50 +010050import com.android.launcher3.config.FeatureFlags;
Sunny Goyal65190ae2022-08-02 14:16:26 -070051import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070052import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010053import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070054import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070055import com.android.launcher3.util.DisplayController;
56import com.android.launcher3.util.DisplayController.Info;
Stefan Andonian5bd9a222023-02-23 00:58:33 +000057import com.android.launcher3.util.LockedUserState;
Sunny Goyald0e360a2018-06-29 14:40:18 -070058import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal3e58eea2022-11-14 14:30:07 -080059import com.android.launcher3.util.Partner;
Sunny Goyal19ff7282021-04-22 10:12:54 -070060import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080061import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070062
Sunny Goyal819e1932016-07-07 16:43:58 -070063import org.xmlpull.v1.XmlPullParser;
64import org.xmlpull.v1.XmlPullParserException;
65
66import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000067import java.lang.annotation.Retention;
68import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070069import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070070import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080071import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070072import java.util.List;
Sunny Goyal076e04b2023-04-03 12:38:30 -070073import java.util.stream.Collectors;
Adam Cohen2e6da152015-05-06 11:42:25 -070074
75public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070076
Hyunyoung Songc55a3502018-12-04 15:43:16 -080077 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070078 // We do not need any synchronization for this variable as its only written on UI thread.
79 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070080 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070081
Alex Chau1c883d82021-12-01 18:43:10 +000082 @Retention(RetentionPolicy.SOURCE)
83 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010084 public @interface DeviceType {}
85
Alex Chau1c883d82021-12-01 18:43:10 +000086 public static final int TYPE_PHONE = 0;
87 public static final int TYPE_MULTI_DISPLAY = 1;
88 public static final int TYPE_TABLET = 2;
89
Sunny Goyal53d7ee42015-05-22 12:25:45 -070090 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
91
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070092 // Constants that affects the interpolation curve between statically defined device profile
93 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080094 private static final float KNEARESTNEIGHBOR = 3;
95 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070096
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070097 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080098 private static final float WEIGHT_EFFICIENT = 100000f;
99
Thales Lima83bedbf2021-10-05 17:47:39 +0100100 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
101 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000102 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100103 static final int INDEX_DEFAULT = 0;
104 static final int INDEX_LANDSCAPE = 1;
105 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
106 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100107
Thales Limaec5abba2023-04-05 16:33:50 +0100108 /** These resources are used to override the device profile */
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800109 private static final String RES_GRID_NUM_ROWS = "grid_num_rows";
110 private static final String RES_GRID_NUM_COLUMNS = "grid_num_columns";
111 private static final String RES_GRID_ICON_SIZE_DP = "grid_icon_size_dp";
112
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700113 /**
114 * Number of icons per row and column in the workspace.
115 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 public int numRows;
117 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000118 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700119
120 /**
121 * Number of icons per row and column in the folder.
122 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700123 public int numFolderRows;
124 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100125 public float[] iconSize;
126 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700127 public int iconBitmapSize;
128 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000129 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700130
Thales Lima83bedbf2021-10-05 17:47:39 +0100131 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100132
Thales Lima83bedbf2021-10-05 17:47:39 +0100133 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300134 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100135
Thales Limab35faed2022-09-05 16:30:01 -0300136 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100137
Thales Limad852d652023-01-17 14:01:13 +0000138 public @StyleRes int cellStyle;
139
Thales Lima83bedbf2021-10-05 17:47:39 +0100140 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500141
Thales Limab7ef5692022-03-10 10:32:36 +0000142 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000143 public float[] allAppsIconSize;
144 public float[] allAppsIconTextSize;
145 public PointF[] allAppsBorderSpaces;
146
Jon Miranda9c478b62023-03-23 21:38:49 -0700147 public float[] transientTaskbarIconSize;
148
Jon Miranda04f05102023-04-04 12:16:31 -0700149 public boolean[] startAlignTaskbar;
150
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700151 /**
152 * Number of icons inside the hotseat area.
153 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800154 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700155
156 /**
157 * Number of icons inside the hotseat area that is stored in the database. This is greater than
158 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
159 * sizes that share the same DB.
160 */
161 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700162
Thales Lima425f6822022-05-10 13:44:58 -0300163 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100164 public float[] hotseatBarBottomSpace;
165 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300166
Jon Miranda6f7e9702019-09-16 14:44:14 -0700167 /**
168 * Number of columns in the all apps list.
169 */
170 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700171 public int numDatabaseAllAppsColumns;
Thales Limae9273ea2023-01-26 12:33:52 +0000172 public @StyleRes int allAppsStyle;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700173
Jon Mirandae126d722021-02-25 10:45:20 -0500174 /**
175 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
176 */
177 protected boolean isScalable;
Thales Limaae0d7ef2022-11-16 15:53:43 +0000178 @XmlRes
179 public int devicePaddingId = INVALID_RESOURCE_HANDLE;
Thales Limaec5abba2023-04-05 16:33:50 +0100180 @XmlRes
181 public int workspaceSpecsId = INVALID_RESOURCE_HANDLE;
Jon Mirandae126d722021-02-25 10:45:20 -0500182
Tracy Zhou7df93d22020-01-27 13:44:06 -0800183 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800184 public int defaultLayoutId;
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700185 public int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100186 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700187
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000188 /**
189 * An immutable list of supported profiles.
190 */
191 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700192
Sunny Goyal6f866092016-03-17 17:04:15 -0700193 public Point defaultWallpaperSize;
194
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700195 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700196
Sunny Goyalf633ef52018-03-13 09:57:05 -0700197 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800198 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700199
Sunny Goyalbbf01842015-10-08 07:41:15 -0700200 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700201 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700202 String gridName = getCurrentGridName(context);
203 String newGridName = initGrid(context, gridName);
204 if (!newGridName.equals(gridName)) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000205 LauncherPrefs.get(context).put(GRID_NAME, newGridName);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700206 }
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000207 LockedUserState.get(context).runOnUserUnlocked(() -> {
208 new DeviceGridState(this).writeToPrefs(context);
209 });
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700210
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700211 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100212 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000213 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
214 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100215 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700216 }
217 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700218 }
219
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700220 /**
221 * This constructor should NOT have any monitors by design.
222 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800223 public InvariantDeviceProfile(Context context, String gridName) {
224 String newName = initGrid(context, gridName);
225 if (newName == null || !newName.equals(gridName)) {
226 throw new IllegalArgumentException("Unknown grid name");
227 }
228 }
229
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700230 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800231 * This constructor should NOT have any monitors by design.
232 */
233 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700234 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100235 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700236 String gridName = getCurrentGridName(context);
237
238 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000239 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
240 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700241 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000242 defaultInfo,
243 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
244 /*allowDisabledGrid=*/false),
245 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700246
Alex Chau661f02d2022-06-07 14:03:43 +0100247 Context displayContext = context.createDisplayContext(display);
248 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000249 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700250 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000251 myInfo,
252 getPredefinedDeviceProfiles(context, gridName, deviceType,
253 /*allowDisabledGrid=*/false),
254 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700255
256 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
257 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100258 result.iconSizes[INDEX_DEFAULT] =
259 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
260 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700261 result.iconSizes[i] = Math.min(
262 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000263 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700264
Thales Lima83bedbf2021-10-05 17:47:39 +0100265 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
266 COUNT_SIZES);
267 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
268 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500269
Alex Chau1c883d82021-12-01 18:43:10 +0000270 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800271 }
272
Alex Chau238aaee2021-10-06 16:15:24 +0100273 /**
274 * Reinitialize the current grid after a restore, where some grids might now be disabled.
275 */
276 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000277 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100278 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000279 String newGridName = initGrid(context, currentGridName);
280 String newDbFile = dbFile;
281 if (!newDbFile.equals(currentDbFile)) {
282 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100283 + ", migrating to: " + newGridName
284 + ", removing all other grid db files");
285 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
286 if (gridDbFile.equals(currentDbFile)) {
287 continue;
288 }
289 if (context.getDatabasePath(gridDbFile).delete()) {
290 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
291 }
292 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000293 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100294 }
295 }
296
Alex Chau1c883d82021-12-01 18:43:10 +0000297 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800298 int flagPhone = 1 << 0;
299 int flagTablet = 1 << 1;
300
301 int type = displayInfo.supportedBounds.stream()
302 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
303 .reduce(0, (a, b) -> a | b);
Sebastian Franco76a1ceb2023-06-08 13:26:20 -0700304 if ((type == (flagPhone | flagTablet))) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800305 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000306 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800307 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000308 return TYPE_TABLET;
309 } else {
310 return TYPE_PHONE;
311 }
312 }
313
Tracy Zhou42255d22020-03-13 00:38:11 -0700314 public static String getCurrentGridName(Context context) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000315 return LauncherPrefs.get(context).get(GRID_NAME);
Tracy Zhou42255d22020-03-13 00:38:11 -0700316 }
317
Sunny Goyaleff44f32019-01-09 17:29:49 -0800318 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700319 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000320 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700321
322 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000323 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100324 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700325 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000326 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
327 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700328 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800329 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700330
Thales Limab67bfa72022-11-02 15:30:11 +0000331 @VisibleForTesting
332 public static String getDefaultGridName(Context context) {
333 return new InvariantDeviceProfile().initGrid(context, null);
334 }
335
Alex Chau1c883d82021-12-01 18:43:10 +0000336 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
337 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700338 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700339 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000340 numRows = closestProfile.numRows;
341 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000342 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000343 dbFile = closestProfile.dbFile;
344 defaultLayoutId = closestProfile.defaultLayoutId;
345 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300346
Sunny Goyalae190ff2020-04-14 00:19:01 +0000347 numFolderRows = closestProfile.numFolderRows;
348 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300349 folderStyle = closestProfile.folderStyle;
350
Thales Limad852d652023-01-17 14:01:13 +0000351 cellStyle = closestProfile.cellStyle;
352
Jon Mirandae126d722021-02-25 10:45:20 -0500353 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400354 devicePaddingId = closestProfile.devicePaddingId;
Thales Limaec5abba2023-04-05 16:33:50 +0100355 workspaceSpecsId = closestProfile.mWorkspaceSpecsId;
Alex Chau1c883d82021-12-01 18:43:10 +0000356 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000357
Vinit Nayakc7293172022-07-18 16:41:50 -0700358 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
359
Thales Lima83bedbf2021-10-05 17:47:39 +0100360 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100361 float maxIconSize = iconSize[0];
362 for (int i = 1; i < iconSize.length; i++) {
363 maxIconSize = Math.max(maxIconSize, iconSize[i]);
364 }
365 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000366 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
367
Thales Lima83bedbf2021-10-05 17:47:39 +0100368 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700369
Thales Lima83bedbf2021-10-05 17:47:39 +0100370 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100371
Thales Lima83bedbf2021-10-05 17:47:39 +0100372 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100373
Thales Limad90faab2021-09-21 17:18:47 +0100374 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100375
Sunny Goyal19ff7282021-04-22 10:12:54 -0700376 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000377 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700378 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300379 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100380 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
381 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700382
Thales Limae9273ea2023-01-26 12:33:52 +0000383 allAppsStyle = closestProfile.allAppsStyle;
384
Sunny Goyal19ff7282021-04-22 10:12:54 -0700385 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000386 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700387 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500388
Thales Limab7ef5692022-03-10 10:32:36 +0000389 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000390 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
391 allAppsIconSize = displayOption.allAppsIconSizes;
392 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000393
Thales Lima11af7bc2022-08-12 15:24:54 +0100394 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000395
Jon Miranda9c478b62023-03-23 21:38:49 -0700396 transientTaskbarIconSize = displayOption.transientTaskbarIconSize;
397
Jon Miranda04f05102023-04-04 12:16:31 -0700398 startAlignTaskbar = displayOption.startAlignTaskbar;
399
Sunny Goyalae190ff2020-04-14 00:19:01 +0000400 // If the partner customization apk contains any grid overrides, apply them
401 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700402 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700403
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000404 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700405 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700406 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700407 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000408 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chauab800f72022-12-13 17:46:06 +0000409 .setIsMultiDisplay(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000410 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700411 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000412 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700413
Sunny Goyal19ff7282021-04-22 10:12:54 -0700414 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
415 int displayWidth = bounds.bounds.width();
416 int displayHeight = bounds.bounds.height();
417 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700418
Sunny Goyal19ff7282021-04-22 10:12:54 -0700419 // We need to ensure that there is enough extra space in the wallpaper
420 // for the intended parallax effects
421 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300422 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
423 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700424 ? 2
425 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
426 defaultWallpaperSize.x =
427 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700428 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000429 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700430
Thales Lima6a590062022-11-22 15:52:49 +0000431 int numMinShownHotseatIconsForTablet = supportedProfiles
432 .stream()
433 .filter(deviceProfile -> deviceProfile.isTablet)
434 .mapToInt(deviceProfile -> deviceProfile.numShownHotseatIcons)
435 .min()
436 .orElse(0);
437
438 supportedProfiles
439 .stream()
440 .filter(deviceProfile -> deviceProfile.isTablet)
441 .forEach(deviceProfile -> {
442 deviceProfile.numShownHotseatIcons = numMinShownHotseatIconsForTablet;
443 deviceProfile.recalculateHotseatWidthAndBorderSpace();
444 });
Adam Cohen2e6da152015-05-06 11:42:25 -0700445 }
446
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700447 public void addOnChangeListener(OnIDPChangeListener listener) {
448 mChangeListeners.add(listener);
449 }
450
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800451 public void removeOnChangeListener(OnIDPChangeListener listener) {
452 mChangeListeners.remove(listener);
453 }
454
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800455
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800456 public void setCurrentGrid(Context context, String gridName) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000457 LauncherPrefs.get(context).put(GRID_NAME, gridName);
458 MAIN_EXECUTOR.execute(() -> onConfigChanged(context.getApplicationContext()));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800459 }
460
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700461 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700462 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000463 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
464 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700465 }
466
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700467 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700468 Object[] oldState = toModelState();
469
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700470 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700471 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700472 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700473
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700474 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700475 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700476 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700477 }
478 }
479
Alex Chau1c883d82021-12-01 18:43:10 +0000480 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
481 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800482 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100483
Alex Chau1c883d82021-12-01 18:43:10 +0000484 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700485 final int depth = parser.getDepth();
486 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700487 while (((type = parser.next()) != XmlPullParser.END_TAG ||
488 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800489 if ((type == XmlPullParser.START_TAG)
490 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800491
Sunny Goyal076e04b2023-04-03 12:38:30 -0700492 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
493 if (gridOption.isEnabled(deviceType) || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100494 final int displayDepth = parser.getDepth();
495 while (((type = parser.next()) != XmlPullParser.END_TAG
496 || parser.getDepth() > displayDepth)
497 && type != XmlPullParser.END_DOCUMENT) {
498 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
499 parser.getName())) {
500 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100501 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100502 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800503 }
504 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700505 }
506 }
sfufa@google.comde013292021-09-21 18:22:44 -0700507 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700508 throw new RuntimeException(e);
509 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800510
Sunny Goyalae190ff2020-04-14 00:19:01 +0000511 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800512 if (!TextUtils.isEmpty(gridName)) {
513 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100514 if (gridName.equals(option.grid.name)
Sunny Goyal076e04b2023-04-03 12:38:30 -0700515 && (option.grid.isEnabled(deviceType) || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000516 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800517 }
518 }
519 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000520 if (filteredProfiles.isEmpty()) {
521 // No grid found, use the default options
522 for (DisplayOption option : profiles) {
523 if (option.canBeDefault) {
524 filteredProfiles.add(option);
525 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800526 }
527 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000528 if (filteredProfiles.isEmpty()) {
529 throw new RuntimeException("No display option with canBeDefault=true");
530 }
531 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700532 }
533
Thales Lima7ec83822021-08-05 13:32:10 +0100534 /**
535 * @return all the grid options that can be shown on the device
536 */
537 public List<GridOption> parseAllGridOptions(Context context) {
Sunny Goyal076e04b2023-04-03 12:38:30 -0700538 return parseAllDefinedGridOptions(context)
539 .stream()
540 .filter(go -> go.isEnabled(deviceType))
541 .collect(Collectors.toList());
542 }
543
544 /**
545 * @return all the grid options that can be shown on the device
546 */
547 public static List<GridOption> parseAllDefinedGridOptions(Context context) {
Thales Lima7ec83822021-08-05 13:32:10 +0100548 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100549
Alex Chau1c883d82021-12-01 18:43:10 +0000550 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100551 final int depth = parser.getDepth();
552 int type;
553 while (((type = parser.next()) != XmlPullParser.END_TAG
554 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
555 if ((type == XmlPullParser.START_TAG)
556 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal076e04b2023-04-03 12:38:30 -0700557 result.add(new GridOption(context, Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100558 }
559 }
560 } catch (IOException | XmlPullParserException e) {
561 Log.e(TAG, "Error parsing device profile", e);
562 return Collections.emptyList();
563 }
564 return result;
565 }
566
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700567 private int getLauncherIconDensity(int requiredSize) {
568 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700569 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700570 DisplayMetrics.DENSITY_LOW,
571 DisplayMetrics.DENSITY_MEDIUM,
572 DisplayMetrics.DENSITY_TV,
573 DisplayMetrics.DENSITY_HIGH,
574 DisplayMetrics.DENSITY_XHIGH,
575 DisplayMetrics.DENSITY_XXHIGH,
576 DisplayMetrics.DENSITY_XXXHIGH
577 };
578
579 int density = DisplayMetrics.DENSITY_XXXHIGH;
580 for (int i = densityBuckets.length - 1; i >= 0; i--) {
581 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
582 / DisplayMetrics.DENSITY_DEFAULT;
583 if (expectedSize >= requiredSize) {
584 density = densityBuckets[i];
585 }
586 }
587
588 return density;
589 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700590
Adam Cohen2e6da152015-05-06 11:42:25 -0700591 /**
592 * Apply any Partner customization grid overrides.
593 *
594 * Currently we support: all apps row / column count.
595 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700596 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
597 Partner p = Partner.get(context.getPackageManager());
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800598 if (p == null) {
599 return;
600 }
601 try {
602 int numRows = p.getIntValue(RES_GRID_NUM_ROWS, -1);
603 int numColumns = p.getIntValue(RES_GRID_NUM_COLUMNS, -1);
604 float iconSizePx = p.getDimenValue(RES_GRID_ICON_SIZE_DP, -1);
605
606 if (numRows > 0 && numColumns > 0) {
607 this.numRows = numRows;
608 this.numColumns = numColumns;
609 }
610 if (iconSizePx > 0) {
611 this.iconSize[InvariantDeviceProfile.INDEX_DEFAULT] =
612 Utilities.dpiFromPx(iconSizePx, dm.densityDpi);
613 }
614 } catch (Resources.NotFoundException ex) {
615 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700616 }
617 }
618
Sunny Goyal415f1732018-11-29 10:33:47 -0800619 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700620 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700621 }
622
Sunny Goyal19ff7282021-04-22 10:12:54 -0700623 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000624 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700625 int minWidthPx = Integer.MAX_VALUE;
626 int minHeightPx = Integer.MAX_VALUE;
627 for (WindowBounds bounds : displayInfo.supportedBounds) {
628 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000629 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700630 // For split displays, take half width per page
631 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
632 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700633
Sunny Goyal19ff7282021-04-22 10:12:54 -0700634 } else if (!isTablet && bounds.isLandscape()) {
635 // We will use transposed layout in this case
636 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
637 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
638 } else {
639 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
640 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
641 }
642 }
643
Thales Lima425f6822022-05-10 13:44:58 -0300644 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
645 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700646
647 // Sort the profiles based on the closeness to the device size
648 Collections.sort(points, (a, b) ->
649 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
650 dist(width, height, b.minWidthDps, b.minHeightDps)));
651
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700652 DisplayOption closestPoint = points.get(0);
653 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700654 float weights = 0;
655
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700656 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
657 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700658 }
659
660 DisplayOption out = new DisplayOption(closestOption);
661 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700662 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700663 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
664 weights += w;
665 out.add(new DisplayOption().add(p).multiply(w));
666 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700667 out.multiply(1.0f / weights);
668
Thales Lima6e0005a2021-10-27 15:53:41 +0100669 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700670 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100671 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
672 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700673 }
674
675 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700676 }
677
Sunny Goyal27835952017-01-13 12:15:53 -0800678 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700679 Resources res = context.getResources();
680 Configuration config = context.getResources().getConfiguration();
681
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400682 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
683 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100684 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
685
Alex Chaud3e8cc42022-05-03 17:45:34 +0100686 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400687 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700688
Sunny Goyal187b16c2022-03-01 16:53:23 -0800689 /**
690 * Returns the device profile matching the provided screen configuration
691 */
692 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700693 DeviceProfile bestMatch = supportedProfiles.get(0);
694 float minDiff = Float.MAX_VALUE;
695
696 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400697 float diff = Math.abs(profile.widthPx - screenWidth)
698 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700699 if (diff < minDiff) {
700 minDiff = diff;
701 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800702 } else if (diff == minDiff && profile.rotationHint == rotation) {
703 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700704 }
705 }
706 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800707 }
708
Sunny Goyal415f1732018-11-29 10:33:47 -0800709 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700710 float d = dist(x0, y0, x1, y1);
711 if (Float.compare(d, 0f) == 0) {
712 return Float.POSITIVE_INFINITY;
713 }
714 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
715 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700716
717 /**
718 * As a ratio of screen height, the total distance we want the parallax effect to span
719 * horizontally
720 */
721 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
722 float aspectRatio = width / (float) height;
723
724 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
725 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
726 // We will use these two data points to extrapolate how much the wallpaper parallax effect
727 // to span (ie travel) at any aspect ratio:
728
sfufa@google.comde013292021-09-21 18:22:44 -0700729 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
730 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700731 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
732 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
733
734 // To find out the desired width at different aspect ratios, we use the following two
735 // formulas, where the coefficient on x is the aspect ratio (width/height):
736 // (16/10)x + y = 1.5
737 // (10/16)x + y = 1.2
738 // We solve for x and y and end up with a final formula:
739 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700740 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
741 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700742 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
743 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
744 return x * aspectRatio + y;
745 }
746
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700747 public interface OnIDPChangeListener {
748
Sunny Goyalb47172b2021-05-03 19:59:51 -0700749 /**
750 * Called when the device provide changes
751 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700752 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700753 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800754
755
Sunny Goyaleff44f32019-01-09 17:29:49 -0800756 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800757
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800758 public static final String TAG_NAME = "grid-option";
759
Alex Chau1c883d82021-12-01 18:43:10 +0000760 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
761 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
762 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
763 private static final int DEVICE_CATEGORY_ALL =
764 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
765
Thales Lima11af7bc2022-08-12 15:24:54 +0100766 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
767 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
768 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
769 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
770 private static final int DONT_INLINE_QSB = 0;
771
Sunny Goyaleff44f32019-01-09 17:29:49 -0800772 public final String name;
773 public final int numRows;
774 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000775 public final int numSearchContainerColumns;
Sunny Goyal076e04b2023-04-03 12:38:30 -0700776 public final int deviceCategory;
Sunny Goyal415f1732018-11-29 10:33:47 -0800777
778 private final int numFolderRows;
779 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300780 private final @StyleRes int folderStyle;
Thales Limad852d652023-01-17 14:01:13 +0000781 private final @StyleRes int cellStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800782
Thales Limae9273ea2023-01-26 12:33:52 +0000783 private final @StyleRes int allAppsStyle;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700784 private final int numAllAppsColumns;
785 private final int numDatabaseAllAppsColumns;
786 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700787 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100788
Thales Lima425f6822022-05-10 13:44:58 -0300789 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800790
Thales Lima11af7bc2022-08-12 15:24:54 +0100791 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
792
Thales Limab35faed2022-09-05 16:30:01 -0300793 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800794 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000795
Sunny Goyal415f1732018-11-29 10:33:47 -0800796 private final int defaultLayoutId;
797 private final int demoModeLayoutId;
798
Jon Mirandae126d722021-02-25 10:45:20 -0500799 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400800 private final int devicePaddingId;
Thales Limaec5abba2023-04-05 16:33:50 +0100801 private final int mWorkspaceSpecsId;
Jon Mirandae126d722021-02-25 10:45:20 -0500802
Sunny Goyal076e04b2023-04-03 12:38:30 -0700803 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800804 TypedArray a = context.obtainStyledAttributes(
805 attrs, R.styleable.GridDisplayOption);
806 name = a.getString(R.styleable.GridDisplayOption_name);
807 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
808 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000809 numSearchContainerColumns = a.getInt(
810 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800811
Tracy Zhou7df93d22020-01-27 13:44:06 -0800812 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau766cd2b2022-07-06 17:15:21 +0100813 defaultLayoutId = a.getResourceId(
814 R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800815 demoModeLayoutId = a.getResourceId(
816 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700817
Thales Limae9273ea2023-01-26 12:33:52 +0000818 allAppsStyle = a.getResourceId(R.styleable.GridDisplayOption_allAppsStyle,
819 R.style.AllAppsStyleDefault);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700820 numAllAppsColumns = a.getInt(
821 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
822 numDatabaseAllAppsColumns = a.getInt(
823 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
824
825 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800826 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700827 numDatabaseHotseatIcons = a.getInt(
828 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100829
Thales Lima425f6822022-05-10 13:44:58 -0300830 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
831 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
832 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
833 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
834 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
835 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
836 numColumns);
837 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
838 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
839 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700840
Vinit Nayakc7293172022-07-18 16:41:50 -0700841 inlineNavButtonsEndSpacing =
842 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
Thales Limaec5abba2023-04-05 16:33:50 +0100843 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300844
Sunny Goyal415f1732018-11-29 10:33:47 -0800845 numFolderRows = a.getInt(
846 R.styleable.GridDisplayOption_numFolderRows, numRows);
847 numFolderColumns = a.getInt(
848 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700849
Thales Limab35faed2022-09-05 16:30:01 -0300850 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
851 INVALID_RESOURCE_HANDLE);
852
Thales Limad852d652023-01-17 14:01:13 +0000853 cellStyle = a.getResourceId(R.styleable.GridDisplayOption_cellStyle,
854 R.style.CellStyleDefault);
855
Jon Mirandae126d722021-02-25 10:45:20 -0500856 isScalable = a.getBoolean(
857 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400858 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000859 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Sunny Goyal076e04b2023-04-03 12:38:30 -0700860 deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
Alex Chau1c883d82021-12-01 18:43:10 +0000861 DEVICE_CATEGORY_ALL);
Thales Lima7ec83822021-08-05 13:32:10 +0100862
Thales Limaec5abba2023-04-05 16:33:50 +0100863 if (FeatureFlags.ENABLE_RESPONSIVE_WORKSPACE.get()) {
864 mWorkspaceSpecsId = a.getResourceId(
865 R.styleable.GridDisplayOption_workspaceSpecsId, INVALID_RESOURCE_HANDLE);
866 } else {
867 mWorkspaceSpecsId = INVALID_RESOURCE_HANDLE;
868 }
869
Thales Lima11af7bc2022-08-12 15:24:54 +0100870 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
871 DONT_INLINE_QSB);
872 inlineQsb[INDEX_DEFAULT] =
873 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
874 inlineQsb[INDEX_LANDSCAPE] =
875 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
876 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
877 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
878 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
879 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
880 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
881 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
882
Sunny Goyal415f1732018-11-29 10:33:47 -0800883 a.recycle();
Sunny Goyal076e04b2023-04-03 12:38:30 -0700884 }
885
886 public boolean isEnabled(@DeviceType int deviceType) {
887 switch (deviceType) {
888 case TYPE_PHONE:
889 return (deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE;
890 case TYPE_TABLET:
891 return (deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET;
892 case TYPE_MULTI_DISPLAY:
893 return (deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
894 == DEVICE_CATEGORY_MULTI_DISPLAY;
895 default:
896 return false;
897 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800898 }
899 }
900
Sunny Goyal19ff7282021-04-22 10:12:54 -0700901 @VisibleForTesting
902 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700903 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800904
Sunny Goyal415f1732018-11-29 10:33:47 -0800905 private final float minWidthDps;
906 private final float minHeightDps;
907 private final boolean canBeDefault;
908
Thales Lima83bedbf2021-10-05 17:47:39 +0100909 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100910
Thales Lima78d00ad2021-09-30 11:29:06 +0100911 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100912 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100913 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
914 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100915
Thales Lima78d00ad2021-09-30 11:29:06 +0100916 private final float[] iconSizes = new float[COUNT_SIZES];
917 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800918
Thales Limab7ef5692022-03-10 10:32:36 +0000919 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000920 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
921 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
922 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
923
Jon Miranda9c478b62023-03-23 21:38:49 -0700924 private final float[] transientTaskbarIconSize = new float[COUNT_SIZES];
925
Jon Miranda04f05102023-04-04 12:16:31 -0700926 private final boolean[] startAlignTaskbar = new boolean[COUNT_SIZES];
927
Thales Lima1de4d552021-10-13 16:13:25 +0100928 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800929 this.grid = grid;
930
Jon Miranda9c478b62023-03-23 21:38:49 -0700931 Resources res = context.getResources();
932
Thales Lima1de4d552021-10-13 16:13:25 +0100933 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800934
Sunny Goyal415f1732018-11-29 10:33:47 -0800935 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
936 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700937
Thales Lima1de4d552021-10-13 16:13:25 +0100938 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800939
Thales Lima83bedbf2021-10-05 17:47:39 +0100940 float x;
941 float y;
942
Thales Lima85c942f2021-12-31 13:04:20 +0000943 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
944 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100945 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000946
947 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
948 minCellSize[INDEX_DEFAULT].x);
949 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
950 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100951 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100952
Thales Lima85c942f2021-12-31 13:04:20 +0000953 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100954 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000955 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100956 minCellSize[INDEX_DEFAULT].y);
957 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
958
Thales Lima85c942f2021-12-31 13:04:20 +0000959 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100960 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000961 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100962 minCellSize[INDEX_DEFAULT].y);
963 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100964
Thales Lima85c942f2021-12-31 13:04:20 +0000965 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000966 float borderSpaceLandscape = a.getFloat(
967 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000968 float borderSpaceTwoPanelPortrait = a.getFloat(
969 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
970 float borderSpaceTwoPanelLandscape = a.getFloat(
971 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100972
Thales Lima85c942f2021-12-31 13:04:20 +0000973 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
974 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100975 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000976
977 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
978 borderSpaceLandscape);
979 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
980 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100981 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
982
983 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000984 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
985 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100986 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000987 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
988 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100989 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
990
991 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000992 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
993 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100994 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000995 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
996 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100997 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
998
Thales Limab7ef5692022-03-10 10:32:36 +0000999 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
1000 minCellSize[INDEX_DEFAULT].x);
1001 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
1002 minCellSize[INDEX_DEFAULT].y);
1003 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
1004
1005 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
1006 allAppsCellSize[INDEX_DEFAULT].x);
1007 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
1008 allAppsCellSize[INDEX_DEFAULT].y);
1009 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
1010
1011 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
1012 allAppsCellSize[INDEX_DEFAULT].x);
1013 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
1014 allAppsCellSize[INDEX_DEFAULT].y);
1015 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1016
1017 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
1018 allAppsCellSize[INDEX_DEFAULT].x);
1019 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
1020 allAppsCellSize[INDEX_DEFAULT].y);
1021 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
1022
Thales Lima080d8902022-03-28 15:30:29 +01001023 float allAppsBorderSpace = a.getFloat(
1024 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
1025 float allAppsBorderSpaceLandscape = a.getFloat(
1026 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
1027 allAppsBorderSpace);
1028 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001029 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +01001030 allAppsBorderSpace);
1031 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001032 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +01001033 allAppsBorderSpace);
1034
1035 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
1036 allAppsBorderSpace);
1037 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
1038 allAppsBorderSpace);
1039 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
1040
1041 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
1042 allAppsBorderSpaceLandscape);
1043 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1044 allAppsBorderSpaceLandscape);
1045 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1046
1047 x = a.getFloat(
1048 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1049 allAppsBorderSpaceTwoPanelPortrait);
1050 y = a.getFloat(
1051 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1052 allAppsBorderSpaceTwoPanelPortrait);
1053 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1054
1055 x = a.getFloat(
1056 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1057 allAppsBorderSpaceTwoPanelLandscape);
1058 y = a.getFloat(
1059 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1060 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001061 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001062
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001063 iconSizes[INDEX_DEFAULT] =
1064 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1065 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001066 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001067 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001068 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001069 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001070 iconSizes[INDEX_DEFAULT]);
1071 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001072 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001073 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001074
Thales Limabb7d3882021-12-22 12:56:29 +00001075 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1076 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001077 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1078 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001079 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001080 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1081 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1082 allAppsIconSizes[INDEX_DEFAULT]);
1083 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1084 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1085 allAppsIconSizes[INDEX_DEFAULT]);
1086
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001087 textSizes[INDEX_DEFAULT] =
1088 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1089 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001090 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001091 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001092 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001093 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001094 textSizes[INDEX_DEFAULT]);
1095 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001096 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001097 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001098
Thales Limabb7d3882021-12-22 12:56:29 +00001099 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1100 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1101 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1102 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1103 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1104 allAppsIconTextSizes[INDEX_DEFAULT]);
1105 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1106 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1107 allAppsIconTextSizes[INDEX_DEFAULT]);
1108
Thales Lima83bedbf2021-10-05 17:47:39 +01001109 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1110 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001111 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1112 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1113 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001114 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001115 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001116 horizontalMargin[INDEX_DEFAULT]);
1117 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001118 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001119 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001120
Thales Limab8c05952022-05-23 16:58:38 +01001121 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1122 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
Jon Miranda9c478b62023-03-23 21:38:49 -07001123 ResourcesCompat.getFloat(res, R.dimen.hotseat_bar_bottom_space_default));
Thales Limab8c05952022-05-23 16:58:38 +01001124 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1125 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1126 hotseatBarBottomSpace[INDEX_DEFAULT]);
1127 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1128 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1129 hotseatBarBottomSpace[INDEX_DEFAULT]);
1130 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1131 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1132 hotseatBarBottomSpace[INDEX_DEFAULT]);
1133
1134 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1135 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
Jon Miranda9c478b62023-03-23 21:38:49 -07001136 ResourcesCompat.getFloat(res, R.dimen.hotseat_qsb_space_default));
Thales Limab8c05952022-05-23 16:58:38 +01001137 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1138 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1139 hotseatQsbSpace[INDEX_DEFAULT]);
1140 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1141 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1142 hotseatQsbSpace[INDEX_DEFAULT]);
1143 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1144 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1145 hotseatQsbSpace[INDEX_DEFAULT]);
1146
Jon Miranda9c478b62023-03-23 21:38:49 -07001147 transientTaskbarIconSize[INDEX_DEFAULT] = a.getFloat(
1148 R.styleable.ProfileDisplayOption_transientTaskbarIconSize,
1149 ResourcesCompat.getFloat(res, R.dimen.taskbar_icon_size));
1150 transientTaskbarIconSize[INDEX_LANDSCAPE] = a.getFloat(
1151 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeLandscape,
1152 transientTaskbarIconSize[INDEX_DEFAULT]);
1153 transientTaskbarIconSize[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1154 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeTwoPanelLandscape,
1155 transientTaskbarIconSize[INDEX_DEFAULT]);
1156 transientTaskbarIconSize[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1157 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeTwoPanelPortrait,
Jon Miranda4d74ad32023-03-27 16:31:11 -07001158 transientTaskbarIconSize[INDEX_DEFAULT]);
Jon Miranda9c478b62023-03-23 21:38:49 -07001159
Jon Miranda04f05102023-04-04 12:16:31 -07001160 startAlignTaskbar[INDEX_DEFAULT] = a.getBoolean(
1161 R.styleable.ProfileDisplayOption_startAlignTaskbar, false);
1162 startAlignTaskbar[INDEX_LANDSCAPE] = a.getBoolean(
1163 R.styleable.ProfileDisplayOption_startAlignTaskbarLandscape,
1164 startAlignTaskbar[INDEX_DEFAULT]);
1165 startAlignTaskbar[INDEX_TWO_PANEL_LANDSCAPE] = a.getBoolean(
1166 R.styleable.ProfileDisplayOption_startAlignTaskbarTwoPanelLandscape,
1167 startAlignTaskbar[INDEX_LANDSCAPE]);
1168 startAlignTaskbar[INDEX_TWO_PANEL_PORTRAIT] = a.getBoolean(
1169 R.styleable.ProfileDisplayOption_startAlignTaskbarTwoPanelPortrait,
1170 startAlignTaskbar[INDEX_DEFAULT]);
1171
Sunny Goyal415f1732018-11-29 10:33:47 -08001172 a.recycle();
1173 }
1174
1175 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001176 this(null);
1177 }
1178
1179 DisplayOption(GridOption grid) {
1180 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001181 minWidthDps = 0;
1182 minHeightDps = 0;
1183 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001184 for (int i = 0; i < COUNT_SIZES; i++) {
1185 iconSizes[i] = 0;
1186 textSizes[i] = 0;
1187 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001188 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001189 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001190 allAppsIconSizes[i] = 0;
1191 allAppsIconTextSizes[i] = 0;
1192 allAppsBorderSpaces[i] = new PointF();
Jon Miranda9c478b62023-03-23 21:38:49 -07001193 transientTaskbarIconSize[i] = 0;
Jon Miranda04f05102023-04-04 12:16:31 -07001194 startAlignTaskbar[i] = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001195 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001196 }
1197
1198 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001199 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001200 iconSizes[i] *= w;
1201 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001202 borderSpaces[i].x *= w;
1203 borderSpaces[i].y *= w;
1204 minCellSize[i].x *= w;
1205 minCellSize[i].y *= w;
1206 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001207 hotseatBarBottomSpace[i] *= w;
1208 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001209 allAppsCellSize[i].x *= w;
1210 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001211 allAppsIconSizes[i] *= w;
1212 allAppsIconTextSizes[i] *= w;
1213 allAppsBorderSpaces[i].x *= w;
1214 allAppsBorderSpaces[i].y *= w;
Jon Miranda9c478b62023-03-23 21:38:49 -07001215 transientTaskbarIconSize[i] *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001216 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001217
Sunny Goyal415f1732018-11-29 10:33:47 -08001218 return this;
1219 }
1220
1221 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001222 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001223 iconSizes[i] += p.iconSizes[i];
1224 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001225 borderSpaces[i].x += p.borderSpaces[i].x;
1226 borderSpaces[i].y += p.borderSpaces[i].y;
1227 minCellSize[i].x += p.minCellSize[i].x;
1228 minCellSize[i].y += p.minCellSize[i].y;
1229 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001230 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1231 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001232 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1233 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001234 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1235 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1236 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1237 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Jon Miranda9c478b62023-03-23 21:38:49 -07001238 transientTaskbarIconSize[i] += p.transientTaskbarIconSize[i];
Jon Miranda04f05102023-04-04 12:16:31 -07001239 startAlignTaskbar[i] |= p.startAlignTaskbar[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001240 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001241
Sunny Goyal415f1732018-11-29 10:33:47 -08001242 return this;
1243 }
1244 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001245}