blob: 376f54dc4cf41c8029f8fd5717f6fdf70b17777e [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;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010021import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Thales Limab35faed2022-09-05 16:30:01 -030022import static com.android.launcher3.testing.shared.ResourceUtils.INVALID_RESOURCE_HANDLE;
Sunny Goyal35c7b192021-04-20 16:51:10 -070023import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Alex Chau6ed408f2022-03-04 12:58:05 +000024import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE;
Sunny Goyal19ff7282021-04-22 10:12:54 -070025import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080026import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070027
Sunny Goyalc6205602015-05-21 20:46:33 -070028import android.annotation.TargetApi;
Adam Cohen2e6da152015-05-06 11:42:25 -070029import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080030import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080031import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070032import android.content.res.TypedArray;
33import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010035import android.graphics.PointF;
Sunny Goyal415f1732018-11-29 10:33:47 -080036import android.text.TextUtils;
37import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070038import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010039import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080040import android.util.SparseArray;
Sunny Goyal819e1932016-07-07 16:43:58 -070041import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080042import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070043
Thales Limab35faed2022-09-05 16:30:01 -030044import androidx.annotation.DimenRes;
Alex Chau1c883d82021-12-01 18:43:10 +000045import androidx.annotation.IntDef;
Thales Limab35faed2022-09-05 16:30:01 -030046import androidx.annotation.StyleRes;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070047import androidx.annotation.VisibleForTesting;
Thales Limaae0d7ef2022-11-16 15:53:43 +000048import androidx.annotation.XmlRes;
Thales Limab8c05952022-05-23 16:58:38 +010049import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070050
Thales Limaec5abba2023-04-05 16:33:50 +010051import com.android.launcher3.config.FeatureFlags;
Sunny Goyal65190ae2022-08-02 14:16:26 -070052import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070053import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010054import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070055import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070056import com.android.launcher3.util.DisplayController;
57import com.android.launcher3.util.DisplayController.Info;
Stefan Andonian5bd9a222023-02-23 00:58:33 +000058import com.android.launcher3.util.LockedUserState;
Sunny Goyald0e360a2018-06-29 14:40:18 -070059import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal3e58eea2022-11-14 14:30:07 -080060import com.android.launcher3.util.Partner;
Sunny Goyal19ff7282021-04-22 10:12:54 -070061import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080062import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070063
Sunny Goyal819e1932016-07-07 16:43:58 -070064import org.xmlpull.v1.XmlPullParser;
65import org.xmlpull.v1.XmlPullParserException;
66
67import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000068import java.lang.annotation.Retention;
69import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070070import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070071import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080072import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070073import java.util.List;
Sunny Goyal076e04b2023-04-03 12:38:30 -070074import java.util.stream.Collectors;
Adam Cohen2e6da152015-05-06 11:42:25 -070075
76public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070077
Hyunyoung Songc55a3502018-12-04 15:43:16 -080078 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070079 // We do not need any synchronization for this variable as its only written on UI thread.
80 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070081 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070082
Alex Chau1c883d82021-12-01 18:43:10 +000083 @Retention(RetentionPolicy.SOURCE)
84 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010085 public @interface DeviceType {}
86
Alex Chau1c883d82021-12-01 18:43:10 +000087 public static final int TYPE_PHONE = 0;
88 public static final int TYPE_MULTI_DISPLAY = 1;
89 public static final int TYPE_TABLET = 2;
90
Sunny Goyal53d7ee42015-05-22 12:25:45 -070091 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
92
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070093 // Constants that affects the interpolation curve between statically defined device profile
94 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080095 private static final float KNEARESTNEIGHBOR = 3;
96 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070097
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080099 private static final float WEIGHT_EFFICIENT = 100000f;
100
Thales Lima83bedbf2021-10-05 17:47:39 +0100101 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
102 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000103 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100104 static final int INDEX_DEFAULT = 0;
105 static final int INDEX_LANDSCAPE = 1;
106 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
107 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100108
Thales Limaec5abba2023-04-05 16:33:50 +0100109 /** These resources are used to override the device profile */
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800110 private static final String RES_GRID_NUM_ROWS = "grid_num_rows";
111 private static final String RES_GRID_NUM_COLUMNS = "grid_num_columns";
112 private static final String RES_GRID_ICON_SIZE_DP = "grid_icon_size_dp";
113
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700114 /**
115 * Number of icons per row and column in the workspace.
116 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700117 public int numRows;
118 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000119 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700120
121 /**
122 * Number of icons per row and column in the folder.
123 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700124 public int numFolderRows;
125 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100126 public float[] iconSize;
127 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700128 public int iconBitmapSize;
129 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000130 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700131
Thales Lima83bedbf2021-10-05 17:47:39 +0100132 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100133
Thales Lima83bedbf2021-10-05 17:47:39 +0100134 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300135 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100136
Thales Limab35faed2022-09-05 16:30:01 -0300137 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100138
Thales Limad852d652023-01-17 14:01:13 +0000139 public @StyleRes int cellStyle;
140
Thales Lima83bedbf2021-10-05 17:47:39 +0100141 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500142
Thales Limab7ef5692022-03-10 10:32:36 +0000143 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000144 public float[] allAppsIconSize;
145 public float[] allAppsIconTextSize;
146 public PointF[] allAppsBorderSpaces;
147
Jon Miranda9c478b62023-03-23 21:38:49 -0700148 public float[] transientTaskbarIconSize;
149
Jon Miranda04f05102023-04-04 12:16:31 -0700150 public boolean[] startAlignTaskbar;
151
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700152 /**
153 * Number of icons inside the hotseat area.
154 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800155 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700156
157 /**
158 * Number of icons inside the hotseat area that is stored in the database. This is greater than
159 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
160 * sizes that share the same DB.
161 */
162 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700163
Thales Lima425f6822022-05-10 13:44:58 -0300164 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100165 public float[] hotseatBarBottomSpace;
166 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300167
Jon Miranda6f7e9702019-09-16 14:44:14 -0700168 /**
169 * Number of columns in the all apps list.
170 */
171 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700172 public int numDatabaseAllAppsColumns;
Thales Limae9273ea2023-01-26 12:33:52 +0000173 public @StyleRes int allAppsStyle;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700174
Jon Mirandae126d722021-02-25 10:45:20 -0500175 /**
176 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
177 */
178 protected boolean isScalable;
Thales Limaae0d7ef2022-11-16 15:53:43 +0000179 @XmlRes
180 public int devicePaddingId = INVALID_RESOURCE_HANDLE;
Thales Limaec5abba2023-04-05 16:33:50 +0100181 @XmlRes
182 public int workspaceSpecsId = INVALID_RESOURCE_HANDLE;
Jon Mirandae126d722021-02-25 10:45:20 -0500183
Tracy Zhou7df93d22020-01-27 13:44:06 -0800184 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800185 public int defaultLayoutId;
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700186 public int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100187 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700188
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000189 /**
190 * An immutable list of supported profiles.
191 */
192 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700193
Sunny Goyal6f866092016-03-17 17:04:15 -0700194 public Point defaultWallpaperSize;
195
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700196 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700197
Sunny Goyalf633ef52018-03-13 09:57:05 -0700198 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800199 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700200
Sunny Goyalbbf01842015-10-08 07:41:15 -0700201 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700202 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700203 String gridName = getCurrentGridName(context);
204 String newGridName = initGrid(context, gridName);
205 if (!newGridName.equals(gridName)) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000206 LauncherPrefs.get(context).put(GRID_NAME, newGridName);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700207 }
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000208 LockedUserState.get(context).runOnUserUnlocked(() -> {
209 new DeviceGridState(this).writeToPrefs(context);
210 });
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700211
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700212 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100213 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000214 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
215 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100216 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700217 }
218 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700219 }
220
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700221 /**
222 * This constructor should NOT have any monitors by design.
223 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800224 public InvariantDeviceProfile(Context context, String gridName) {
225 String newName = initGrid(context, gridName);
226 if (newName == null || !newName.equals(gridName)) {
227 throw new IllegalArgumentException("Unknown grid name");
228 }
229 }
230
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700231 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800232 * This constructor should NOT have any monitors by design.
233 */
234 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100236 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700237 String gridName = getCurrentGridName(context);
238
239 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000240 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
241 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700242 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000243 defaultInfo,
244 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
245 /*allowDisabledGrid=*/false),
246 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700247
Alex Chau661f02d2022-06-07 14:03:43 +0100248 Context displayContext = context.createDisplayContext(display);
249 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000250 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700251 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000252 myInfo,
253 getPredefinedDeviceProfiles(context, gridName, deviceType,
254 /*allowDisabledGrid=*/false),
255 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700256
257 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
258 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100259 result.iconSizes[INDEX_DEFAULT] =
260 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
261 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700262 result.iconSizes[i] = Math.min(
263 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000264 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700265
Thales Lima83bedbf2021-10-05 17:47:39 +0100266 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
267 COUNT_SIZES);
268 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
269 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500270
Alex Chau1c883d82021-12-01 18:43:10 +0000271 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800272 }
273
Alex Chau238aaee2021-10-06 16:15:24 +0100274 /**
275 * Reinitialize the current grid after a restore, where some grids might now be disabled.
276 */
277 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000278 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100279 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000280 String newGridName = initGrid(context, currentGridName);
281 String newDbFile = dbFile;
282 if (!newDbFile.equals(currentDbFile)) {
283 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100284 + ", migrating to: " + newGridName
285 + ", removing all other grid db files");
286 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
287 if (gridDbFile.equals(currentDbFile)) {
288 continue;
289 }
290 if (context.getDatabasePath(gridDbFile).delete()) {
291 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
292 }
293 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000294 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100295 }
296 }
297
Alex Chau1c883d82021-12-01 18:43:10 +0000298 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800299 int flagPhone = 1 << 0;
300 int flagTablet = 1 << 1;
301
302 int type = displayInfo.supportedBounds.stream()
303 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
304 .reduce(0, (a, b) -> a | b);
305 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
306 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000307 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800308 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000309 return TYPE_TABLET;
310 } else {
311 return TYPE_PHONE;
312 }
313 }
314
Tracy Zhou42255d22020-03-13 00:38:11 -0700315 public static String getCurrentGridName(Context context) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000316 return LauncherPrefs.get(context).get(GRID_NAME);
Tracy Zhou42255d22020-03-13 00:38:11 -0700317 }
318
Sunny Goyaleff44f32019-01-09 17:29:49 -0800319 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700320 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000321 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700322
323 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000324 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100325 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700326 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000327 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
328 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700329 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800330 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700331
Thales Limab67bfa72022-11-02 15:30:11 +0000332 @VisibleForTesting
333 public static String getDefaultGridName(Context context) {
334 return new InvariantDeviceProfile().initGrid(context, null);
335 }
336
Alex Chau1c883d82021-12-01 18:43:10 +0000337 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
338 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700339 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700340 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000341 numRows = closestProfile.numRows;
342 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000343 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000344 dbFile = closestProfile.dbFile;
345 defaultLayoutId = closestProfile.defaultLayoutId;
346 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300347
Sunny Goyalae190ff2020-04-14 00:19:01 +0000348 numFolderRows = closestProfile.numFolderRows;
349 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300350 folderStyle = closestProfile.folderStyle;
351
Thales Limad852d652023-01-17 14:01:13 +0000352 cellStyle = closestProfile.cellStyle;
353
Jon Mirandae126d722021-02-25 10:45:20 -0500354 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400355 devicePaddingId = closestProfile.devicePaddingId;
Thales Limaec5abba2023-04-05 16:33:50 +0100356 workspaceSpecsId = closestProfile.mWorkspaceSpecsId;
Alex Chau1c883d82021-12-01 18:43:10 +0000357 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000358
Vinit Nayakc7293172022-07-18 16:41:50 -0700359 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
360
Thales Lima83bedbf2021-10-05 17:47:39 +0100361 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100362 float maxIconSize = iconSize[0];
363 for (int i = 1; i < iconSize.length; i++) {
364 maxIconSize = Math.max(maxIconSize, iconSize[i]);
365 }
366 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000367 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
368
Thales Lima83bedbf2021-10-05 17:47:39 +0100369 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700370
Thales Lima83bedbf2021-10-05 17:47:39 +0100371 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100372
Thales Lima83bedbf2021-10-05 17:47:39 +0100373 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100374
Thales Limad90faab2021-09-21 17:18:47 +0100375 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100376
Sunny Goyal19ff7282021-04-22 10:12:54 -0700377 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000378 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700379 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300380 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100381 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
382 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700383
Thales Limae9273ea2023-01-26 12:33:52 +0000384 allAppsStyle = closestProfile.allAppsStyle;
385
Sunny Goyal19ff7282021-04-22 10:12:54 -0700386 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000387 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700388 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500389
Thales Limab7ef5692022-03-10 10:32:36 +0000390 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000391 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
392 allAppsIconSize = displayOption.allAppsIconSizes;
393 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000394
Thales Lima11af7bc2022-08-12 15:24:54 +0100395 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000396
Jon Miranda9c478b62023-03-23 21:38:49 -0700397 transientTaskbarIconSize = displayOption.transientTaskbarIconSize;
398
Jon Miranda04f05102023-04-04 12:16:31 -0700399 startAlignTaskbar = displayOption.startAlignTaskbar;
400
Sunny Goyalae190ff2020-04-14 00:19:01 +0000401 // If the partner customization apk contains any grid overrides, apply them
402 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700403 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700404
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000405 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700406 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700407 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700408 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000409 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chauab800f72022-12-13 17:46:06 +0000410 .setIsMultiDisplay(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000411 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700412 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000413 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700414
Sunny Goyal19ff7282021-04-22 10:12:54 -0700415 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
416 int displayWidth = bounds.bounds.width();
417 int displayHeight = bounds.bounds.height();
418 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700419
Sunny Goyal19ff7282021-04-22 10:12:54 -0700420 // We need to ensure that there is enough extra space in the wallpaper
421 // for the intended parallax effects
422 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300423 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
424 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700425 ? 2
426 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
427 defaultWallpaperSize.x =
428 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700429 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000430 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700431
Thales Lima6a590062022-11-22 15:52:49 +0000432 int numMinShownHotseatIconsForTablet = supportedProfiles
433 .stream()
434 .filter(deviceProfile -> deviceProfile.isTablet)
435 .mapToInt(deviceProfile -> deviceProfile.numShownHotseatIcons)
436 .min()
437 .orElse(0);
438
439 supportedProfiles
440 .stream()
441 .filter(deviceProfile -> deviceProfile.isTablet)
442 .forEach(deviceProfile -> {
443 deviceProfile.numShownHotseatIcons = numMinShownHotseatIconsForTablet;
444 deviceProfile.recalculateHotseatWidthAndBorderSpace();
445 });
Adam Cohen2e6da152015-05-06 11:42:25 -0700446 }
447
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700448 public void addOnChangeListener(OnIDPChangeListener listener) {
449 mChangeListeners.add(listener);
450 }
451
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800452 public void removeOnChangeListener(OnIDPChangeListener listener) {
453 mChangeListeners.remove(listener);
454 }
455
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800456
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800457 public void setCurrentGrid(Context context, String gridName) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000458 LauncherPrefs.get(context).put(GRID_NAME, gridName);
459 MAIN_EXECUTOR.execute(() -> onConfigChanged(context.getApplicationContext()));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800460 }
461
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700462 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700463 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000464 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
465 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700466 }
467
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700468 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700469 Object[] oldState = toModelState();
470
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700471 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700472 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700473 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700474
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700475 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700476 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700477 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700478 }
479 }
480
Alex Chau1c883d82021-12-01 18:43:10 +0000481 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
482 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800483 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100484
Alex Chau1c883d82021-12-01 18:43:10 +0000485 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700486 final int depth = parser.getDepth();
487 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700488 while (((type = parser.next()) != XmlPullParser.END_TAG ||
489 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800490 if ((type == XmlPullParser.START_TAG)
491 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800492
Sunny Goyal076e04b2023-04-03 12:38:30 -0700493 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
494 if (gridOption.isEnabled(deviceType) || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100495 final int displayDepth = parser.getDepth();
496 while (((type = parser.next()) != XmlPullParser.END_TAG
497 || parser.getDepth() > displayDepth)
498 && type != XmlPullParser.END_DOCUMENT) {
499 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
500 parser.getName())) {
501 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100502 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100503 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800504 }
505 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700506 }
507 }
sfufa@google.comde013292021-09-21 18:22:44 -0700508 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700509 throw new RuntimeException(e);
510 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800511
Sunny Goyalae190ff2020-04-14 00:19:01 +0000512 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800513 if (!TextUtils.isEmpty(gridName)) {
514 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100515 if (gridName.equals(option.grid.name)
Sunny Goyal076e04b2023-04-03 12:38:30 -0700516 && (option.grid.isEnabled(deviceType) || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000517 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800518 }
519 }
520 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000521 if (filteredProfiles.isEmpty()) {
522 // No grid found, use the default options
523 for (DisplayOption option : profiles) {
524 if (option.canBeDefault) {
525 filteredProfiles.add(option);
526 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800527 }
528 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000529 if (filteredProfiles.isEmpty()) {
530 throw new RuntimeException("No display option with canBeDefault=true");
531 }
532 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700533 }
534
Thales Lima7ec83822021-08-05 13:32:10 +0100535 /**
536 * @return all the grid options that can be shown on the device
537 */
538 public List<GridOption> parseAllGridOptions(Context context) {
Sunny Goyal076e04b2023-04-03 12:38:30 -0700539 return parseAllDefinedGridOptions(context)
540 .stream()
541 .filter(go -> go.isEnabled(deviceType))
542 .collect(Collectors.toList());
543 }
544
545 /**
546 * @return all the grid options that can be shown on the device
547 */
548 public static List<GridOption> parseAllDefinedGridOptions(Context context) {
Thales Lima7ec83822021-08-05 13:32:10 +0100549 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100550
Alex Chau1c883d82021-12-01 18:43:10 +0000551 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100552 final int depth = parser.getDepth();
553 int type;
554 while (((type = parser.next()) != XmlPullParser.END_TAG
555 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
556 if ((type == XmlPullParser.START_TAG)
557 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal076e04b2023-04-03 12:38:30 -0700558 result.add(new GridOption(context, Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100559 }
560 }
561 } catch (IOException | XmlPullParserException e) {
562 Log.e(TAG, "Error parsing device profile", e);
563 return Collections.emptyList();
564 }
565 return result;
566 }
567
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700568 private int getLauncherIconDensity(int requiredSize) {
569 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700570 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700571 DisplayMetrics.DENSITY_LOW,
572 DisplayMetrics.DENSITY_MEDIUM,
573 DisplayMetrics.DENSITY_TV,
574 DisplayMetrics.DENSITY_HIGH,
575 DisplayMetrics.DENSITY_XHIGH,
576 DisplayMetrics.DENSITY_XXHIGH,
577 DisplayMetrics.DENSITY_XXXHIGH
578 };
579
580 int density = DisplayMetrics.DENSITY_XXXHIGH;
581 for (int i = densityBuckets.length - 1; i >= 0; i--) {
582 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
583 / DisplayMetrics.DENSITY_DEFAULT;
584 if (expectedSize >= requiredSize) {
585 density = densityBuckets[i];
586 }
587 }
588
589 return density;
590 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700591
Adam Cohen2e6da152015-05-06 11:42:25 -0700592 /**
593 * Apply any Partner customization grid overrides.
594 *
595 * Currently we support: all apps row / column count.
596 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700597 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
598 Partner p = Partner.get(context.getPackageManager());
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800599 if (p == null) {
600 return;
601 }
602 try {
603 int numRows = p.getIntValue(RES_GRID_NUM_ROWS, -1);
604 int numColumns = p.getIntValue(RES_GRID_NUM_COLUMNS, -1);
605 float iconSizePx = p.getDimenValue(RES_GRID_ICON_SIZE_DP, -1);
606
607 if (numRows > 0 && numColumns > 0) {
608 this.numRows = numRows;
609 this.numColumns = numColumns;
610 }
611 if (iconSizePx > 0) {
612 this.iconSize[InvariantDeviceProfile.INDEX_DEFAULT] =
613 Utilities.dpiFromPx(iconSizePx, dm.densityDpi);
614 }
615 } catch (Resources.NotFoundException ex) {
616 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700617 }
618 }
619
Sunny Goyal415f1732018-11-29 10:33:47 -0800620 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700621 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700622 }
623
Sunny Goyal19ff7282021-04-22 10:12:54 -0700624 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000625 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700626 int minWidthPx = Integer.MAX_VALUE;
627 int minHeightPx = Integer.MAX_VALUE;
628 for (WindowBounds bounds : displayInfo.supportedBounds) {
629 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000630 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700631 // For split displays, take half width per page
632 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
633 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700634
Sunny Goyal19ff7282021-04-22 10:12:54 -0700635 } else if (!isTablet && bounds.isLandscape()) {
636 // We will use transposed layout in this case
637 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
638 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
639 } else {
640 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
641 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
642 }
643 }
644
Thales Lima425f6822022-05-10 13:44:58 -0300645 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
646 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700647
648 // Sort the profiles based on the closeness to the device size
649 Collections.sort(points, (a, b) ->
650 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
651 dist(width, height, b.minWidthDps, b.minHeightDps)));
652
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700653 DisplayOption closestPoint = points.get(0);
654 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700655 float weights = 0;
656
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700657 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
658 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700659 }
660
661 DisplayOption out = new DisplayOption(closestOption);
662 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700663 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700664 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
665 weights += w;
666 out.add(new DisplayOption().add(p).multiply(w));
667 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700668 out.multiply(1.0f / weights);
669
Thales Lima6e0005a2021-10-27 15:53:41 +0100670 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700671 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100672 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
673 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700674 }
675
676 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700677 }
678
Sunny Goyal27835952017-01-13 12:15:53 -0800679 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700680 Resources res = context.getResources();
681 Configuration config = context.getResources().getConfiguration();
682
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400683 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
684 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100685 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
686
Alex Chaud3e8cc42022-05-03 17:45:34 +0100687 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400688 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700689
Sunny Goyal187b16c2022-03-01 16:53:23 -0800690 /**
691 * Returns the device profile matching the provided screen configuration
692 */
693 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700694 DeviceProfile bestMatch = supportedProfiles.get(0);
695 float minDiff = Float.MAX_VALUE;
696
697 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400698 float diff = Math.abs(profile.widthPx - screenWidth)
699 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700700 if (diff < minDiff) {
701 minDiff = diff;
702 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800703 } else if (diff == minDiff && profile.rotationHint == rotation) {
704 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700705 }
706 }
707 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800708 }
709
Sunny Goyal415f1732018-11-29 10:33:47 -0800710 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700711 float d = dist(x0, y0, x1, y1);
712 if (Float.compare(d, 0f) == 0) {
713 return Float.POSITIVE_INFINITY;
714 }
715 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
716 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700717
718 /**
719 * As a ratio of screen height, the total distance we want the parallax effect to span
720 * horizontally
721 */
722 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
723 float aspectRatio = width / (float) height;
724
725 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
726 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
727 // We will use these two data points to extrapolate how much the wallpaper parallax effect
728 // to span (ie travel) at any aspect ratio:
729
sfufa@google.comde013292021-09-21 18:22:44 -0700730 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
731 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700732 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
733 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
734
735 // To find out the desired width at different aspect ratios, we use the following two
736 // formulas, where the coefficient on x is the aspect ratio (width/height):
737 // (16/10)x + y = 1.5
738 // (10/16)x + y = 1.2
739 // We solve for x and y and end up with a final formula:
740 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700741 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
742 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700743 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
744 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
745 return x * aspectRatio + y;
746 }
747
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700748 public interface OnIDPChangeListener {
749
Sunny Goyalb47172b2021-05-03 19:59:51 -0700750 /**
751 * Called when the device provide changes
752 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700753 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700754 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800755
756
Sunny Goyaleff44f32019-01-09 17:29:49 -0800757 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800758
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800759 public static final String TAG_NAME = "grid-option";
760
Alex Chau1c883d82021-12-01 18:43:10 +0000761 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
762 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
763 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
764 private static final int DEVICE_CATEGORY_ALL =
765 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
766
Thales Lima11af7bc2022-08-12 15:24:54 +0100767 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
768 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
769 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
770 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
771 private static final int DONT_INLINE_QSB = 0;
772
Sunny Goyaleff44f32019-01-09 17:29:49 -0800773 public final String name;
774 public final int numRows;
775 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000776 public final int numSearchContainerColumns;
Sunny Goyal076e04b2023-04-03 12:38:30 -0700777 public final int deviceCategory;
Sunny Goyal415f1732018-11-29 10:33:47 -0800778
779 private final int numFolderRows;
780 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300781 private final @StyleRes int folderStyle;
Thales Limad852d652023-01-17 14:01:13 +0000782 private final @StyleRes int cellStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800783
Thales Limae9273ea2023-01-26 12:33:52 +0000784 private final @StyleRes int allAppsStyle;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700785 private final int numAllAppsColumns;
786 private final int numDatabaseAllAppsColumns;
787 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700788 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100789
Thales Lima425f6822022-05-10 13:44:58 -0300790 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800791
Thales Lima11af7bc2022-08-12 15:24:54 +0100792 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
793
Thales Limab35faed2022-09-05 16:30:01 -0300794 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800795 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000796
Sunny Goyal415f1732018-11-29 10:33:47 -0800797 private final int defaultLayoutId;
798 private final int demoModeLayoutId;
799
Jon Mirandae126d722021-02-25 10:45:20 -0500800 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400801 private final int devicePaddingId;
Thales Limaec5abba2023-04-05 16:33:50 +0100802 private final int mWorkspaceSpecsId;
Jon Mirandae126d722021-02-25 10:45:20 -0500803
Sunny Goyal076e04b2023-04-03 12:38:30 -0700804 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800805 TypedArray a = context.obtainStyledAttributes(
806 attrs, R.styleable.GridDisplayOption);
807 name = a.getString(R.styleable.GridDisplayOption_name);
808 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
809 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000810 numSearchContainerColumns = a.getInt(
811 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800812
Tracy Zhou7df93d22020-01-27 13:44:06 -0800813 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau766cd2b2022-07-06 17:15:21 +0100814 defaultLayoutId = a.getResourceId(
815 R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800816 demoModeLayoutId = a.getResourceId(
817 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700818
Thales Limae9273ea2023-01-26 12:33:52 +0000819 allAppsStyle = a.getResourceId(R.styleable.GridDisplayOption_allAppsStyle,
820 R.style.AllAppsStyleDefault);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700821 numAllAppsColumns = a.getInt(
822 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
823 numDatabaseAllAppsColumns = a.getInt(
824 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
825
826 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800827 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700828 numDatabaseHotseatIcons = a.getInt(
829 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100830
Thales Lima425f6822022-05-10 13:44:58 -0300831 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
832 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
833 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
834 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
835 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
836 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
837 numColumns);
838 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
839 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
840 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700841
Vinit Nayakc7293172022-07-18 16:41:50 -0700842 inlineNavButtonsEndSpacing =
843 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
Thales Limaec5abba2023-04-05 16:33:50 +0100844 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300845
Sunny Goyal415f1732018-11-29 10:33:47 -0800846 numFolderRows = a.getInt(
847 R.styleable.GridDisplayOption_numFolderRows, numRows);
848 numFolderColumns = a.getInt(
849 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700850
Thales Limab35faed2022-09-05 16:30:01 -0300851 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
852 INVALID_RESOURCE_HANDLE);
853
Thales Limad852d652023-01-17 14:01:13 +0000854 cellStyle = a.getResourceId(R.styleable.GridDisplayOption_cellStyle,
855 R.style.CellStyleDefault);
856
Jon Mirandae126d722021-02-25 10:45:20 -0500857 isScalable = a.getBoolean(
858 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400859 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000860 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Sunny Goyal076e04b2023-04-03 12:38:30 -0700861 deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
Alex Chau1c883d82021-12-01 18:43:10 +0000862 DEVICE_CATEGORY_ALL);
Thales Lima7ec83822021-08-05 13:32:10 +0100863
Thales Limaec5abba2023-04-05 16:33:50 +0100864 if (FeatureFlags.ENABLE_RESPONSIVE_WORKSPACE.get()) {
865 mWorkspaceSpecsId = a.getResourceId(
866 R.styleable.GridDisplayOption_workspaceSpecsId, INVALID_RESOURCE_HANDLE);
867 } else {
868 mWorkspaceSpecsId = INVALID_RESOURCE_HANDLE;
869 }
870
Thales Lima11af7bc2022-08-12 15:24:54 +0100871 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
872 DONT_INLINE_QSB);
873 inlineQsb[INDEX_DEFAULT] =
874 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
875 inlineQsb[INDEX_LANDSCAPE] =
876 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
877 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
878 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
879 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
880 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
881 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
882 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
883
Sunny Goyal415f1732018-11-29 10:33:47 -0800884 a.recycle();
Sunny Goyal076e04b2023-04-03 12:38:30 -0700885 }
886
887 public boolean isEnabled(@DeviceType int deviceType) {
888 switch (deviceType) {
889 case TYPE_PHONE:
890 return (deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE;
891 case TYPE_TABLET:
892 return (deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET;
893 case TYPE_MULTI_DISPLAY:
894 return (deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
895 == DEVICE_CATEGORY_MULTI_DISPLAY;
896 default:
897 return false;
898 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800899 }
900 }
901
Sunny Goyal19ff7282021-04-22 10:12:54 -0700902 @VisibleForTesting
903 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700904 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800905
Sunny Goyal415f1732018-11-29 10:33:47 -0800906 private final float minWidthDps;
907 private final float minHeightDps;
908 private final boolean canBeDefault;
909
Thales Lima83bedbf2021-10-05 17:47:39 +0100910 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100911
Thales Lima78d00ad2021-09-30 11:29:06 +0100912 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100913 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100914 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
915 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100916
Thales Lima78d00ad2021-09-30 11:29:06 +0100917 private final float[] iconSizes = new float[COUNT_SIZES];
918 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800919
Thales Limab7ef5692022-03-10 10:32:36 +0000920 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000921 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
922 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
923 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
924
Jon Miranda9c478b62023-03-23 21:38:49 -0700925 private final float[] transientTaskbarIconSize = new float[COUNT_SIZES];
926
Jon Miranda04f05102023-04-04 12:16:31 -0700927 private final boolean[] startAlignTaskbar = new boolean[COUNT_SIZES];
928
Thales Lima1de4d552021-10-13 16:13:25 +0100929 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800930 this.grid = grid;
931
Jon Miranda9c478b62023-03-23 21:38:49 -0700932 Resources res = context.getResources();
933
Thales Lima1de4d552021-10-13 16:13:25 +0100934 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800935
Sunny Goyal415f1732018-11-29 10:33:47 -0800936 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
937 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700938
Thales Lima1de4d552021-10-13 16:13:25 +0100939 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800940
Thales Lima83bedbf2021-10-05 17:47:39 +0100941 float x;
942 float y;
943
Thales Lima85c942f2021-12-31 13:04:20 +0000944 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
945 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100946 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000947
948 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
949 minCellSize[INDEX_DEFAULT].x);
950 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
951 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100952 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100953
Thales Lima85c942f2021-12-31 13:04:20 +0000954 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100955 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000956 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100957 minCellSize[INDEX_DEFAULT].y);
958 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
959
Thales Lima85c942f2021-12-31 13:04:20 +0000960 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100961 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000962 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100963 minCellSize[INDEX_DEFAULT].y);
964 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100965
Thales Lima85c942f2021-12-31 13:04:20 +0000966 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000967 float borderSpaceLandscape = a.getFloat(
968 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000969 float borderSpaceTwoPanelPortrait = a.getFloat(
970 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
971 float borderSpaceTwoPanelLandscape = a.getFloat(
972 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100973
Thales Lima85c942f2021-12-31 13:04:20 +0000974 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
975 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100976 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000977
978 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
979 borderSpaceLandscape);
980 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
981 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100982 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
983
984 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000985 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
986 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100987 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000988 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
989 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100990 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
991
992 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000993 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
994 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100995 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000996 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
997 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100998 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
999
Thales Limab7ef5692022-03-10 10:32:36 +00001000 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
1001 minCellSize[INDEX_DEFAULT].x);
1002 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
1003 minCellSize[INDEX_DEFAULT].y);
1004 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
1005
1006 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
1007 allAppsCellSize[INDEX_DEFAULT].x);
1008 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
1009 allAppsCellSize[INDEX_DEFAULT].y);
1010 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
1011
1012 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
1013 allAppsCellSize[INDEX_DEFAULT].x);
1014 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
1015 allAppsCellSize[INDEX_DEFAULT].y);
1016 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1017
1018 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
1019 allAppsCellSize[INDEX_DEFAULT].x);
1020 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
1021 allAppsCellSize[INDEX_DEFAULT].y);
1022 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
1023
Thales Lima080d8902022-03-28 15:30:29 +01001024 float allAppsBorderSpace = a.getFloat(
1025 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
1026 float allAppsBorderSpaceLandscape = a.getFloat(
1027 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
1028 allAppsBorderSpace);
1029 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001030 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +01001031 allAppsBorderSpace);
1032 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001033 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +01001034 allAppsBorderSpace);
1035
1036 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
1037 allAppsBorderSpace);
1038 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
1039 allAppsBorderSpace);
1040 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
1041
1042 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
1043 allAppsBorderSpaceLandscape);
1044 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1045 allAppsBorderSpaceLandscape);
1046 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1047
1048 x = a.getFloat(
1049 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1050 allAppsBorderSpaceTwoPanelPortrait);
1051 y = a.getFloat(
1052 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1053 allAppsBorderSpaceTwoPanelPortrait);
1054 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1055
1056 x = a.getFloat(
1057 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1058 allAppsBorderSpaceTwoPanelLandscape);
1059 y = a.getFloat(
1060 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1061 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001062 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001063
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001064 iconSizes[INDEX_DEFAULT] =
1065 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1066 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001067 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001068 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001069 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001070 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001071 iconSizes[INDEX_DEFAULT]);
1072 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001073 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001074 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001075
Thales Limabb7d3882021-12-22 12:56:29 +00001076 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1077 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001078 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1079 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001080 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001081 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1082 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1083 allAppsIconSizes[INDEX_DEFAULT]);
1084 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1085 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1086 allAppsIconSizes[INDEX_DEFAULT]);
1087
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001088 textSizes[INDEX_DEFAULT] =
1089 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1090 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001091 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001092 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001093 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001094 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001095 textSizes[INDEX_DEFAULT]);
1096 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001097 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001098 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001099
Thales Limabb7d3882021-12-22 12:56:29 +00001100 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1101 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1102 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1103 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1104 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1105 allAppsIconTextSizes[INDEX_DEFAULT]);
1106 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1107 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1108 allAppsIconTextSizes[INDEX_DEFAULT]);
1109
Thales Lima83bedbf2021-10-05 17:47:39 +01001110 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1111 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001112 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1113 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1114 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001115 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001116 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001117 horizontalMargin[INDEX_DEFAULT]);
1118 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001119 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001120 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001121
Thales Limab8c05952022-05-23 16:58:38 +01001122 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1123 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
Jon Miranda9c478b62023-03-23 21:38:49 -07001124 ResourcesCompat.getFloat(res, R.dimen.hotseat_bar_bottom_space_default));
Thales Limab8c05952022-05-23 16:58:38 +01001125 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1126 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1127 hotseatBarBottomSpace[INDEX_DEFAULT]);
1128 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1129 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1130 hotseatBarBottomSpace[INDEX_DEFAULT]);
1131 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1132 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1133 hotseatBarBottomSpace[INDEX_DEFAULT]);
1134
1135 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1136 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
Jon Miranda9c478b62023-03-23 21:38:49 -07001137 ResourcesCompat.getFloat(res, R.dimen.hotseat_qsb_space_default));
Thales Limab8c05952022-05-23 16:58:38 +01001138 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1139 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1140 hotseatQsbSpace[INDEX_DEFAULT]);
1141 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1142 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1143 hotseatQsbSpace[INDEX_DEFAULT]);
1144 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1145 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1146 hotseatQsbSpace[INDEX_DEFAULT]);
1147
Jon Miranda9c478b62023-03-23 21:38:49 -07001148 transientTaskbarIconSize[INDEX_DEFAULT] = a.getFloat(
1149 R.styleable.ProfileDisplayOption_transientTaskbarIconSize,
1150 ResourcesCompat.getFloat(res, R.dimen.taskbar_icon_size));
1151 transientTaskbarIconSize[INDEX_LANDSCAPE] = a.getFloat(
1152 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeLandscape,
1153 transientTaskbarIconSize[INDEX_DEFAULT]);
1154 transientTaskbarIconSize[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1155 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeTwoPanelLandscape,
1156 transientTaskbarIconSize[INDEX_DEFAULT]);
1157 transientTaskbarIconSize[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1158 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeTwoPanelPortrait,
Jon Miranda4d74ad32023-03-27 16:31:11 -07001159 transientTaskbarIconSize[INDEX_DEFAULT]);
Jon Miranda9c478b62023-03-23 21:38:49 -07001160
Jon Miranda04f05102023-04-04 12:16:31 -07001161 startAlignTaskbar[INDEX_DEFAULT] = a.getBoolean(
1162 R.styleable.ProfileDisplayOption_startAlignTaskbar, false);
1163 startAlignTaskbar[INDEX_LANDSCAPE] = a.getBoolean(
1164 R.styleable.ProfileDisplayOption_startAlignTaskbarLandscape,
1165 startAlignTaskbar[INDEX_DEFAULT]);
1166 startAlignTaskbar[INDEX_TWO_PANEL_LANDSCAPE] = a.getBoolean(
1167 R.styleable.ProfileDisplayOption_startAlignTaskbarTwoPanelLandscape,
1168 startAlignTaskbar[INDEX_LANDSCAPE]);
1169 startAlignTaskbar[INDEX_TWO_PANEL_PORTRAIT] = a.getBoolean(
1170 R.styleable.ProfileDisplayOption_startAlignTaskbarTwoPanelPortrait,
1171 startAlignTaskbar[INDEX_DEFAULT]);
1172
Sunny Goyal415f1732018-11-29 10:33:47 -08001173 a.recycle();
1174 }
1175
1176 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001177 this(null);
1178 }
1179
1180 DisplayOption(GridOption grid) {
1181 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001182 minWidthDps = 0;
1183 minHeightDps = 0;
1184 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001185 for (int i = 0; i < COUNT_SIZES; i++) {
1186 iconSizes[i] = 0;
1187 textSizes[i] = 0;
1188 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001189 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001190 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001191 allAppsIconSizes[i] = 0;
1192 allAppsIconTextSizes[i] = 0;
1193 allAppsBorderSpaces[i] = new PointF();
Jon Miranda9c478b62023-03-23 21:38:49 -07001194 transientTaskbarIconSize[i] = 0;
Jon Miranda04f05102023-04-04 12:16:31 -07001195 startAlignTaskbar[i] = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001196 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001197 }
1198
1199 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001200 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001201 iconSizes[i] *= w;
1202 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001203 borderSpaces[i].x *= w;
1204 borderSpaces[i].y *= w;
1205 minCellSize[i].x *= w;
1206 minCellSize[i].y *= w;
1207 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001208 hotseatBarBottomSpace[i] *= w;
1209 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001210 allAppsCellSize[i].x *= w;
1211 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001212 allAppsIconSizes[i] *= w;
1213 allAppsIconTextSizes[i] *= w;
1214 allAppsBorderSpaces[i].x *= w;
1215 allAppsBorderSpaces[i].y *= w;
Jon Miranda9c478b62023-03-23 21:38:49 -07001216 transientTaskbarIconSize[i] *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001217 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001218
Sunny Goyal415f1732018-11-29 10:33:47 -08001219 return this;
1220 }
1221
1222 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001223 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001224 iconSizes[i] += p.iconSizes[i];
1225 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001226 borderSpaces[i].x += p.borderSpaces[i].x;
1227 borderSpaces[i].y += p.borderSpaces[i].y;
1228 minCellSize[i].x += p.minCellSize[i].x;
1229 minCellSize[i].y += p.minCellSize[i].y;
1230 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001231 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1232 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001233 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1234 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001235 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1236 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1237 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1238 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Jon Miranda9c478b62023-03-23 21:38:49 -07001239 transientTaskbarIconSize[i] += p.transientTaskbarIconSize[i];
Jon Miranda04f05102023-04-04 12:16:31 -07001240 startAlignTaskbar[i] |= p.startAlignTaskbar[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001241 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001242
Sunny Goyal415f1732018-11-29 10:33:47 -08001243 return this;
1244 }
1245 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001246}