blob: 5e07a3c52f921dfc6d63f83b81674da28147b0fe [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;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070029import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070030import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070031import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080032import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080033import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070034import android.content.res.TypedArray;
35import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070036import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010037import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070038import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080039import android.text.TextUtils;
40import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070041import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010042import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080043import android.util.SparseArray;
Sunny Goyal819e1932016-07-07 16:43:58 -070044import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080045import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070046
Thales Limab35faed2022-09-05 16:30:01 -030047import androidx.annotation.DimenRes;
Alex Chau1c883d82021-12-01 18:43:10 +000048import androidx.annotation.IntDef;
Thales Limab35faed2022-09-05 16:30:01 -030049import androidx.annotation.StyleRes;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070050import androidx.annotation.VisibleForTesting;
Thales Limaae0d7ef2022-11-16 15:53:43 +000051import androidx.annotation.XmlRes;
Thales Limab8c05952022-05-23 16:58:38 +010052import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070053
Sunny Goyal65190ae2022-08-02 14:16:26 -070054import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070055import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010056import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070057import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070058import com.android.launcher3.util.DisplayController;
59import com.android.launcher3.util.DisplayController.Info;
Stefan Andonian5bd9a222023-02-23 00:58:33 +000060import com.android.launcher3.util.LockedUserState;
Sunny Goyald0e360a2018-06-29 14:40:18 -070061import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal3e58eea2022-11-14 14:30:07 -080062import com.android.launcher3.util.Partner;
Sunny Goyal19ff7282021-04-22 10:12:54 -070063import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080064import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070065
Sunny Goyal819e1932016-07-07 16:43:58 -070066import org.xmlpull.v1.XmlPullParser;
67import org.xmlpull.v1.XmlPullParserException;
68
69import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000070import java.lang.annotation.Retention;
71import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070072import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070073import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080074import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070075import java.util.List;
Sunny Goyal076e04b2023-04-03 12:38:30 -070076import java.util.stream.Collectors;
Adam Cohen2e6da152015-05-06 11:42:25 -070077
78public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070079
Hyunyoung Songc55a3502018-12-04 15:43:16 -080080 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070081 // We do not need any synchronization for this variable as its only written on UI thread.
82 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070083 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070084
Alex Chau1c883d82021-12-01 18:43:10 +000085 @Retention(RetentionPolicy.SOURCE)
86 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010087 public @interface DeviceType {}
88
Alex Chau1c883d82021-12-01 18:43:10 +000089 public static final int TYPE_PHONE = 0;
90 public static final int TYPE_MULTI_DISPLAY = 1;
91 public static final int TYPE_TABLET = 2;
92
Sunny Goyal53d7ee42015-05-22 12:25:45 -070093 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
94
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070095 // Constants that affects the interpolation curve between statically defined device profile
96 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080097 private static final float KNEARESTNEIGHBOR = 3;
98 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070099
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700100 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800101 private static final float WEIGHT_EFFICIENT = 100000f;
102
Thales Lima83bedbf2021-10-05 17:47:39 +0100103 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
104 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000105 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100106 static final int INDEX_DEFAULT = 0;
107 static final int INDEX_LANDSCAPE = 1;
108 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
109 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100110
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800111 /** These resources are used to override the device profile */
112 private static final String RES_GRID_NUM_ROWS = "grid_num_rows";
113 private static final String RES_GRID_NUM_COLUMNS = "grid_num_columns";
114 private static final String RES_GRID_ICON_SIZE_DP = "grid_icon_size_dp";
115
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700116 /**
117 * Number of icons per row and column in the workspace.
118 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700119 public int numRows;
120 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000121 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700122
123 /**
124 * Number of icons per row and column in the folder.
125 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700126 public int numFolderRows;
127 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100128 public float[] iconSize;
129 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700130 public int iconBitmapSize;
131 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000132 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700133
Thales Lima83bedbf2021-10-05 17:47:39 +0100134 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100135
Thales Lima83bedbf2021-10-05 17:47:39 +0100136 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300137 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100138
Thales Limab35faed2022-09-05 16:30:01 -0300139 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100140
Thales Limad852d652023-01-17 14:01:13 +0000141 public @StyleRes int cellStyle;
142
Thales Lima83bedbf2021-10-05 17:47:39 +0100143 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500144
Thales Limab7ef5692022-03-10 10:32:36 +0000145 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000146 public float[] allAppsIconSize;
147 public float[] allAppsIconTextSize;
148 public PointF[] allAppsBorderSpaces;
149
Jon Miranda9c478b62023-03-23 21:38:49 -0700150 public float[] transientTaskbarIconSize;
151
Jon Miranda04f05102023-04-04 12:16:31 -0700152 public boolean[] startAlignTaskbar;
153
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700154 /**
155 * Number of icons inside the hotseat area.
156 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800157 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700158
159 /**
160 * Number of icons inside the hotseat area that is stored in the database. This is greater than
161 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
162 * sizes that share the same DB.
163 */
164 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700165
Thales Lima425f6822022-05-10 13:44:58 -0300166 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100167 public float[] hotseatBarBottomSpace;
168 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300169
Jon Miranda6f7e9702019-09-16 14:44:14 -0700170 /**
171 * Number of columns in the all apps list.
172 */
173 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700174 public int numDatabaseAllAppsColumns;
Thales Limae9273ea2023-01-26 12:33:52 +0000175 public @StyleRes int allAppsStyle;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700176
Jon Mirandae126d722021-02-25 10:45:20 -0500177 /**
178 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
179 */
180 protected boolean isScalable;
Thales Limaae0d7ef2022-11-16 15:53:43 +0000181 @XmlRes
182 public int devicePaddingId = 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;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700195 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700196
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700197 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700198
Sunny Goyalf633ef52018-03-13 09:57:05 -0700199 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800200 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700201
Sunny Goyalbbf01842015-10-08 07:41:15 -0700202 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700203 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700204 String gridName = getCurrentGridName(context);
205 String newGridName = initGrid(context, gridName);
206 if (!newGridName.equals(gridName)) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000207 LauncherPrefs.get(context).put(GRID_NAME, newGridName);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700208 }
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000209 LockedUserState.get(context).runOnUserUnlocked(() -> {
210 new DeviceGridState(this).writeToPrefs(context);
211 });
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700212
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700213 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100214 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000215 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
216 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100217 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700218 }
219 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700220 }
221
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700222 /**
223 * This constructor should NOT have any monitors by design.
224 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800225 public InvariantDeviceProfile(Context context, String gridName) {
226 String newName = initGrid(context, gridName);
227 if (newName == null || !newName.equals(gridName)) {
228 throw new IllegalArgumentException("Unknown grid name");
229 }
230 }
231
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700232 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800233 * This constructor should NOT have any monitors by design.
234 */
235 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700236 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100237 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700238 String gridName = getCurrentGridName(context);
239
240 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000241 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
242 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700243 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000244 defaultInfo,
245 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
246 /*allowDisabledGrid=*/false),
247 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700248
Alex Chau661f02d2022-06-07 14:03:43 +0100249 Context displayContext = context.createDisplayContext(display);
250 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000251 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700252 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000253 myInfo,
254 getPredefinedDeviceProfiles(context, gridName, deviceType,
255 /*allowDisabledGrid=*/false),
256 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700257
258 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
259 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100260 result.iconSizes[INDEX_DEFAULT] =
261 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
262 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700263 result.iconSizes[i] = Math.min(
264 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000265 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700266
Thales Lima83bedbf2021-10-05 17:47:39 +0100267 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
268 COUNT_SIZES);
269 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
270 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500271
Alex Chau1c883d82021-12-01 18:43:10 +0000272 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800273 }
274
Alex Chau238aaee2021-10-06 16:15:24 +0100275 /**
276 * Reinitialize the current grid after a restore, where some grids might now be disabled.
277 */
278 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000279 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100280 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000281 String newGridName = initGrid(context, currentGridName);
282 String newDbFile = dbFile;
283 if (!newDbFile.equals(currentDbFile)) {
284 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100285 + ", migrating to: " + newGridName
286 + ", removing all other grid db files");
287 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
288 if (gridDbFile.equals(currentDbFile)) {
289 continue;
290 }
291 if (context.getDatabasePath(gridDbFile).delete()) {
292 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
293 }
294 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000295 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100296 }
297 }
298
Alex Chau1c883d82021-12-01 18:43:10 +0000299 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800300 int flagPhone = 1 << 0;
301 int flagTablet = 1 << 1;
302
303 int type = displayInfo.supportedBounds.stream()
304 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
305 .reduce(0, (a, b) -> a | b);
306 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
307 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000308 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800309 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000310 return TYPE_TABLET;
311 } else {
312 return TYPE_PHONE;
313 }
314 }
315
Tracy Zhou42255d22020-03-13 00:38:11 -0700316 public static String getCurrentGridName(Context context) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000317 return LauncherPrefs.get(context).get(GRID_NAME);
Tracy Zhou42255d22020-03-13 00:38:11 -0700318 }
319
Sunny Goyaleff44f32019-01-09 17:29:49 -0800320 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700321 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000322 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700323
324 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000325 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100326 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700327 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000328 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
329 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700330 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800331 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700332
Thales Limab67bfa72022-11-02 15:30:11 +0000333 @VisibleForTesting
334 public static String getDefaultGridName(Context context) {
335 return new InvariantDeviceProfile().initGrid(context, null);
336 }
337
Alex Chau1c883d82021-12-01 18:43:10 +0000338 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
339 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700340 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700341 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000342 numRows = closestProfile.numRows;
343 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000344 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000345 dbFile = closestProfile.dbFile;
346 defaultLayoutId = closestProfile.defaultLayoutId;
347 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300348
Sunny Goyalae190ff2020-04-14 00:19:01 +0000349 numFolderRows = closestProfile.numFolderRows;
350 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300351 folderStyle = closestProfile.folderStyle;
352
Thales Limad852d652023-01-17 14:01:13 +0000353 cellStyle = closestProfile.cellStyle;
354
Jon Mirandae126d722021-02-25 10:45:20 -0500355 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400356 devicePaddingId = closestProfile.devicePaddingId;
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 });
446
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700447 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
448 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700449 }
450
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700451 public void addOnChangeListener(OnIDPChangeListener listener) {
452 mChangeListeners.add(listener);
453 }
454
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800455 public void removeOnChangeListener(OnIDPChangeListener listener) {
456 mChangeListeners.remove(listener);
457 }
458
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800459
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800460 public void setCurrentGrid(Context context, String gridName) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000461 LauncherPrefs.get(context).put(GRID_NAME, gridName);
462 MAIN_EXECUTOR.execute(() -> onConfigChanged(context.getApplicationContext()));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800463 }
464
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700465 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700466 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000467 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
468 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700469 }
470
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700471 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700472 Object[] oldState = toModelState();
473
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700474 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700475 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700476 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700477
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700478 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700479 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700480 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700481 }
482 }
483
Alex Chau1c883d82021-12-01 18:43:10 +0000484 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
485 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800486 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100487
Alex Chau1c883d82021-12-01 18:43:10 +0000488 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700489 final int depth = parser.getDepth();
490 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700491 while (((type = parser.next()) != XmlPullParser.END_TAG ||
492 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800493 if ((type == XmlPullParser.START_TAG)
494 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800495
Sunny Goyal076e04b2023-04-03 12:38:30 -0700496 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
497 if (gridOption.isEnabled(deviceType) || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100498 final int displayDepth = parser.getDepth();
499 while (((type = parser.next()) != XmlPullParser.END_TAG
500 || parser.getDepth() > displayDepth)
501 && type != XmlPullParser.END_DOCUMENT) {
502 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
503 parser.getName())) {
504 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100505 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100506 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800507 }
508 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700509 }
510 }
sfufa@google.comde013292021-09-21 18:22:44 -0700511 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700512 throw new RuntimeException(e);
513 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800514
Sunny Goyalae190ff2020-04-14 00:19:01 +0000515 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800516 if (!TextUtils.isEmpty(gridName)) {
517 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100518 if (gridName.equals(option.grid.name)
Sunny Goyal076e04b2023-04-03 12:38:30 -0700519 && (option.grid.isEnabled(deviceType) || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000520 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800521 }
522 }
523 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000524 if (filteredProfiles.isEmpty()) {
525 // No grid found, use the default options
526 for (DisplayOption option : profiles) {
527 if (option.canBeDefault) {
528 filteredProfiles.add(option);
529 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800530 }
531 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000532 if (filteredProfiles.isEmpty()) {
533 throw new RuntimeException("No display option with canBeDefault=true");
534 }
535 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700536 }
537
Thales Lima7ec83822021-08-05 13:32:10 +0100538 /**
539 * @return all the grid options that can be shown on the device
540 */
541 public List<GridOption> parseAllGridOptions(Context context) {
Sunny Goyal076e04b2023-04-03 12:38:30 -0700542 return parseAllDefinedGridOptions(context)
543 .stream()
544 .filter(go -> go.isEnabled(deviceType))
545 .collect(Collectors.toList());
546 }
547
548 /**
549 * @return all the grid options that can be shown on the device
550 */
551 public static List<GridOption> parseAllDefinedGridOptions(Context context) {
Thales Lima7ec83822021-08-05 13:32:10 +0100552 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100553
Alex Chau1c883d82021-12-01 18:43:10 +0000554 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100555 final int depth = parser.getDepth();
556 int type;
557 while (((type = parser.next()) != XmlPullParser.END_TAG
558 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
559 if ((type == XmlPullParser.START_TAG)
560 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal076e04b2023-04-03 12:38:30 -0700561 result.add(new GridOption(context, Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100562 }
563 }
564 } catch (IOException | XmlPullParserException e) {
565 Log.e(TAG, "Error parsing device profile", e);
566 return Collections.emptyList();
567 }
568 return result;
569 }
570
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700571 private int getLauncherIconDensity(int requiredSize) {
572 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700573 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700574 DisplayMetrics.DENSITY_LOW,
575 DisplayMetrics.DENSITY_MEDIUM,
576 DisplayMetrics.DENSITY_TV,
577 DisplayMetrics.DENSITY_HIGH,
578 DisplayMetrics.DENSITY_XHIGH,
579 DisplayMetrics.DENSITY_XXHIGH,
580 DisplayMetrics.DENSITY_XXXHIGH
581 };
582
583 int density = DisplayMetrics.DENSITY_XXXHIGH;
584 for (int i = densityBuckets.length - 1; i >= 0; i--) {
585 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
586 / DisplayMetrics.DENSITY_DEFAULT;
587 if (expectedSize >= requiredSize) {
588 density = densityBuckets[i];
589 }
590 }
591
592 return density;
593 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700594
Adam Cohen2e6da152015-05-06 11:42:25 -0700595 /**
596 * Apply any Partner customization grid overrides.
597 *
598 * Currently we support: all apps row / column count.
599 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700600 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
601 Partner p = Partner.get(context.getPackageManager());
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800602 if (p == null) {
603 return;
604 }
605 try {
606 int numRows = p.getIntValue(RES_GRID_NUM_ROWS, -1);
607 int numColumns = p.getIntValue(RES_GRID_NUM_COLUMNS, -1);
608 float iconSizePx = p.getDimenValue(RES_GRID_ICON_SIZE_DP, -1);
609
610 if (numRows > 0 && numColumns > 0) {
611 this.numRows = numRows;
612 this.numColumns = numColumns;
613 }
614 if (iconSizePx > 0) {
615 this.iconSize[InvariantDeviceProfile.INDEX_DEFAULT] =
616 Utilities.dpiFromPx(iconSizePx, dm.densityDpi);
617 }
618 } catch (Resources.NotFoundException ex) {
619 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700620 }
621 }
622
Sunny Goyal415f1732018-11-29 10:33:47 -0800623 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700624 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700625 }
626
Sunny Goyal19ff7282021-04-22 10:12:54 -0700627 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000628 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700629 int minWidthPx = Integer.MAX_VALUE;
630 int minHeightPx = Integer.MAX_VALUE;
631 for (WindowBounds bounds : displayInfo.supportedBounds) {
632 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000633 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700634 // For split displays, take half width per page
635 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
636 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700637
Sunny Goyal19ff7282021-04-22 10:12:54 -0700638 } else if (!isTablet && bounds.isLandscape()) {
639 // We will use transposed layout in this case
640 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
641 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
642 } else {
643 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
644 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
645 }
646 }
647
Thales Lima425f6822022-05-10 13:44:58 -0300648 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
649 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700650
651 // Sort the profiles based on the closeness to the device size
652 Collections.sort(points, (a, b) ->
653 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
654 dist(width, height, b.minWidthDps, b.minHeightDps)));
655
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700656 DisplayOption closestPoint = points.get(0);
657 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700658 float weights = 0;
659
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700660 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
661 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700662 }
663
664 DisplayOption out = new DisplayOption(closestOption);
665 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700666 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700667 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
668 weights += w;
669 out.add(new DisplayOption().add(p).multiply(w));
670 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700671 out.multiply(1.0f / weights);
672
Thales Lima6e0005a2021-10-27 15:53:41 +0100673 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700674 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100675 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
676 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700677 }
678
679 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700680 }
681
Sunny Goyal27835952017-01-13 12:15:53 -0800682 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700683 Resources res = context.getResources();
684 Configuration config = context.getResources().getConfiguration();
685
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400686 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
687 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100688 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
689
Alex Chaud3e8cc42022-05-03 17:45:34 +0100690 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400691 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700692
Sunny Goyal187b16c2022-03-01 16:53:23 -0800693 /**
694 * Returns the device profile matching the provided screen configuration
695 */
696 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700697 DeviceProfile bestMatch = supportedProfiles.get(0);
698 float minDiff = Float.MAX_VALUE;
699
700 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400701 float diff = Math.abs(profile.widthPx - screenWidth)
702 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700703 if (diff < minDiff) {
704 minDiff = diff;
705 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800706 } else if (diff == minDiff && profile.rotationHint == rotation) {
707 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700708 }
709 }
710 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800711 }
712
Sunny Goyal415f1732018-11-29 10:33:47 -0800713 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700714 float d = dist(x0, y0, x1, y1);
715 if (Float.compare(d, 0f) == 0) {
716 return Float.POSITIVE_INFINITY;
717 }
718 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
719 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700720
721 /**
722 * As a ratio of screen height, the total distance we want the parallax effect to span
723 * horizontally
724 */
725 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
726 float aspectRatio = width / (float) height;
727
728 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
729 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
730 // We will use these two data points to extrapolate how much the wallpaper parallax effect
731 // to span (ie travel) at any aspect ratio:
732
sfufa@google.comde013292021-09-21 18:22:44 -0700733 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
734 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700735 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
736 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
737
738 // To find out the desired width at different aspect ratios, we use the following two
739 // formulas, where the coefficient on x is the aspect ratio (width/height):
740 // (16/10)x + y = 1.5
741 // (10/16)x + y = 1.2
742 // We solve for x and y and end up with a final formula:
743 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700744 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
745 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700746 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
747 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
748 return x * aspectRatio + y;
749 }
750
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700751 public interface OnIDPChangeListener {
752
Sunny Goyalb47172b2021-05-03 19:59:51 -0700753 /**
754 * Called when the device provide changes
755 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700756 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700757 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800758
759
Sunny Goyaleff44f32019-01-09 17:29:49 -0800760 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800761
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800762 public static final String TAG_NAME = "grid-option";
763
Alex Chau1c883d82021-12-01 18:43:10 +0000764 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
765 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
766 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
767 private static final int DEVICE_CATEGORY_ALL =
768 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
769
Thales Lima11af7bc2022-08-12 15:24:54 +0100770 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
771 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
772 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
773 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
774 private static final int DONT_INLINE_QSB = 0;
775
Sunny Goyaleff44f32019-01-09 17:29:49 -0800776 public final String name;
777 public final int numRows;
778 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000779 public final int numSearchContainerColumns;
Sunny Goyal076e04b2023-04-03 12:38:30 -0700780 public final int deviceCategory;
Sunny Goyal415f1732018-11-29 10:33:47 -0800781
782 private final int numFolderRows;
783 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300784 private final @StyleRes int folderStyle;
Thales Limad852d652023-01-17 14:01:13 +0000785 private final @StyleRes int cellStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800786
Thales Limae9273ea2023-01-26 12:33:52 +0000787 private final @StyleRes int allAppsStyle;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700788 private final int numAllAppsColumns;
789 private final int numDatabaseAllAppsColumns;
790 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700791 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100792
Thales Lima425f6822022-05-10 13:44:58 -0300793 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800794
Thales Lima11af7bc2022-08-12 15:24:54 +0100795 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
796
Thales Limab35faed2022-09-05 16:30:01 -0300797 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800798 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000799
Sunny Goyal415f1732018-11-29 10:33:47 -0800800 private final int defaultLayoutId;
801 private final int demoModeLayoutId;
802
Jon Mirandae126d722021-02-25 10:45:20 -0500803 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400804 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500805
Sunny Goyal076e04b2023-04-03 12:38:30 -0700806 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800807 TypedArray a = context.obtainStyledAttributes(
808 attrs, R.styleable.GridDisplayOption);
809 name = a.getString(R.styleable.GridDisplayOption_name);
810 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
811 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000812 numSearchContainerColumns = a.getInt(
813 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800814
Tracy Zhou7df93d22020-01-27 13:44:06 -0800815 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau766cd2b2022-07-06 17:15:21 +0100816 defaultLayoutId = a.getResourceId(
817 R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800818 demoModeLayoutId = a.getResourceId(
819 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700820
Thales Limae9273ea2023-01-26 12:33:52 +0000821 allAppsStyle = a.getResourceId(R.styleable.GridDisplayOption_allAppsStyle,
822 R.style.AllAppsStyleDefault);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700823 numAllAppsColumns = a.getInt(
824 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
825 numDatabaseAllAppsColumns = a.getInt(
826 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
827
828 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800829 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700830 numDatabaseHotseatIcons = a.getInt(
831 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100832
Thales Lima425f6822022-05-10 13:44:58 -0300833 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
834 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
835 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
836 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
837 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
838 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
839 numColumns);
840 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
841 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
842 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700843
Vinit Nayakc7293172022-07-18 16:41:50 -0700844 inlineNavButtonsEndSpacing =
845 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
846 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300847
Sunny Goyal415f1732018-11-29 10:33:47 -0800848 numFolderRows = a.getInt(
849 R.styleable.GridDisplayOption_numFolderRows, numRows);
850 numFolderColumns = a.getInt(
851 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700852
Thales Limab35faed2022-09-05 16:30:01 -0300853 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
854 INVALID_RESOURCE_HANDLE);
855
Thales Limad852d652023-01-17 14:01:13 +0000856 cellStyle = a.getResourceId(R.styleable.GridDisplayOption_cellStyle,
857 R.style.CellStyleDefault);
858
Jon Mirandae126d722021-02-25 10:45:20 -0500859 isScalable = a.getBoolean(
860 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400861 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000862 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Sunny Goyal076e04b2023-04-03 12:38:30 -0700863 deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
Alex Chau1c883d82021-12-01 18:43:10 +0000864 DEVICE_CATEGORY_ALL);
Thales Lima7ec83822021-08-05 13:32:10 +0100865
Thales Lima11af7bc2022-08-12 15:24:54 +0100866 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
867 DONT_INLINE_QSB);
868 inlineQsb[INDEX_DEFAULT] =
869 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
870 inlineQsb[INDEX_LANDSCAPE] =
871 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
872 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
873 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
874 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
875 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
876 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
877 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
878
Sunny Goyal415f1732018-11-29 10:33:47 -0800879 a.recycle();
Sunny Goyal076e04b2023-04-03 12:38:30 -0700880 }
881
882 public boolean isEnabled(@DeviceType int deviceType) {
883 switch (deviceType) {
884 case TYPE_PHONE:
885 return (deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE;
886 case TYPE_TABLET:
887 return (deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET;
888 case TYPE_MULTI_DISPLAY:
889 return (deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
890 == DEVICE_CATEGORY_MULTI_DISPLAY;
891 default:
892 return false;
893 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800894 }
895 }
896
Sunny Goyal19ff7282021-04-22 10:12:54 -0700897 @VisibleForTesting
898 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700899 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800900
Sunny Goyal415f1732018-11-29 10:33:47 -0800901 private final float minWidthDps;
902 private final float minHeightDps;
903 private final boolean canBeDefault;
904
Thales Lima83bedbf2021-10-05 17:47:39 +0100905 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100906
Thales Lima78d00ad2021-09-30 11:29:06 +0100907 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100908 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100909 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
910 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100911
Thales Lima78d00ad2021-09-30 11:29:06 +0100912 private final float[] iconSizes = new float[COUNT_SIZES];
913 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800914
Thales Limab7ef5692022-03-10 10:32:36 +0000915 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000916 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
917 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
918 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
919
Jon Miranda9c478b62023-03-23 21:38:49 -0700920 private final float[] transientTaskbarIconSize = new float[COUNT_SIZES];
921
Jon Miranda04f05102023-04-04 12:16:31 -0700922 private final boolean[] startAlignTaskbar = new boolean[COUNT_SIZES];
923
Thales Lima1de4d552021-10-13 16:13:25 +0100924 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800925 this.grid = grid;
926
Jon Miranda9c478b62023-03-23 21:38:49 -0700927 Resources res = context.getResources();
928
Thales Lima1de4d552021-10-13 16:13:25 +0100929 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800930
Sunny Goyal415f1732018-11-29 10:33:47 -0800931 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
932 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700933
Thales Lima1de4d552021-10-13 16:13:25 +0100934 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800935
Thales Lima83bedbf2021-10-05 17:47:39 +0100936 float x;
937 float y;
938
Thales Lima85c942f2021-12-31 13:04:20 +0000939 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
940 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100941 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000942
943 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
944 minCellSize[INDEX_DEFAULT].x);
945 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
946 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100947 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100948
Thales Lima85c942f2021-12-31 13:04:20 +0000949 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100950 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000951 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100952 minCellSize[INDEX_DEFAULT].y);
953 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
954
Thales Lima85c942f2021-12-31 13:04:20 +0000955 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100956 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000957 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100958 minCellSize[INDEX_DEFAULT].y);
959 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100960
Thales Lima85c942f2021-12-31 13:04:20 +0000961 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000962 float borderSpaceLandscape = a.getFloat(
963 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000964 float borderSpaceTwoPanelPortrait = a.getFloat(
965 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
966 float borderSpaceTwoPanelLandscape = a.getFloat(
967 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100968
Thales Lima85c942f2021-12-31 13:04:20 +0000969 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
970 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100971 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000972
973 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
974 borderSpaceLandscape);
975 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
976 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100977 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
978
979 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000980 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
981 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100982 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000983 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
984 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100985 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
986
987 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000988 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
989 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100990 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000991 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
992 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100993 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
994
Thales Limab7ef5692022-03-10 10:32:36 +0000995 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
996 minCellSize[INDEX_DEFAULT].x);
997 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
998 minCellSize[INDEX_DEFAULT].y);
999 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
1000
1001 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
1002 allAppsCellSize[INDEX_DEFAULT].x);
1003 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
1004 allAppsCellSize[INDEX_DEFAULT].y);
1005 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
1006
1007 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
1008 allAppsCellSize[INDEX_DEFAULT].x);
1009 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
1010 allAppsCellSize[INDEX_DEFAULT].y);
1011 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1012
1013 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
1014 allAppsCellSize[INDEX_DEFAULT].x);
1015 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
1016 allAppsCellSize[INDEX_DEFAULT].y);
1017 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
1018
Thales Lima080d8902022-03-28 15:30:29 +01001019 float allAppsBorderSpace = a.getFloat(
1020 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
1021 float allAppsBorderSpaceLandscape = a.getFloat(
1022 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
1023 allAppsBorderSpace);
1024 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001025 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +01001026 allAppsBorderSpace);
1027 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001028 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +01001029 allAppsBorderSpace);
1030
1031 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
1032 allAppsBorderSpace);
1033 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
1034 allAppsBorderSpace);
1035 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
1036
1037 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
1038 allAppsBorderSpaceLandscape);
1039 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1040 allAppsBorderSpaceLandscape);
1041 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1042
1043 x = a.getFloat(
1044 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1045 allAppsBorderSpaceTwoPanelPortrait);
1046 y = a.getFloat(
1047 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1048 allAppsBorderSpaceTwoPanelPortrait);
1049 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1050
1051 x = a.getFloat(
1052 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1053 allAppsBorderSpaceTwoPanelLandscape);
1054 y = a.getFloat(
1055 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1056 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001057 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001058
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001059 iconSizes[INDEX_DEFAULT] =
1060 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1061 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001062 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001063 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001064 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001065 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001066 iconSizes[INDEX_DEFAULT]);
1067 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001068 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001069 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001070
Thales Limabb7d3882021-12-22 12:56:29 +00001071 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1072 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001073 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1074 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001075 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001076 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1077 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1078 allAppsIconSizes[INDEX_DEFAULT]);
1079 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1080 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1081 allAppsIconSizes[INDEX_DEFAULT]);
1082
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001083 textSizes[INDEX_DEFAULT] =
1084 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1085 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001086 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001087 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001088 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001089 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001090 textSizes[INDEX_DEFAULT]);
1091 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001092 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001093 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001094
Thales Limabb7d3882021-12-22 12:56:29 +00001095 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1096 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1097 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1098 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1099 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1100 allAppsIconTextSizes[INDEX_DEFAULT]);
1101 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1102 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1103 allAppsIconTextSizes[INDEX_DEFAULT]);
1104
Thales Lima83bedbf2021-10-05 17:47:39 +01001105 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1106 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001107 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1108 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1109 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001110 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001111 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001112 horizontalMargin[INDEX_DEFAULT]);
1113 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001114 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001115 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001116
Thales Limab8c05952022-05-23 16:58:38 +01001117 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1118 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
Jon Miranda9c478b62023-03-23 21:38:49 -07001119 ResourcesCompat.getFloat(res, R.dimen.hotseat_bar_bottom_space_default));
Thales Limab8c05952022-05-23 16:58:38 +01001120 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1121 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1122 hotseatBarBottomSpace[INDEX_DEFAULT]);
1123 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1124 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1125 hotseatBarBottomSpace[INDEX_DEFAULT]);
1126 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1127 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1128 hotseatBarBottomSpace[INDEX_DEFAULT]);
1129
1130 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1131 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
Jon Miranda9c478b62023-03-23 21:38:49 -07001132 ResourcesCompat.getFloat(res, R.dimen.hotseat_qsb_space_default));
Thales Limab8c05952022-05-23 16:58:38 +01001133 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1134 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1135 hotseatQsbSpace[INDEX_DEFAULT]);
1136 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1137 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1138 hotseatQsbSpace[INDEX_DEFAULT]);
1139 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1140 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1141 hotseatQsbSpace[INDEX_DEFAULT]);
1142
Jon Miranda9c478b62023-03-23 21:38:49 -07001143 transientTaskbarIconSize[INDEX_DEFAULT] = a.getFloat(
1144 R.styleable.ProfileDisplayOption_transientTaskbarIconSize,
1145 ResourcesCompat.getFloat(res, R.dimen.taskbar_icon_size));
1146 transientTaskbarIconSize[INDEX_LANDSCAPE] = a.getFloat(
1147 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeLandscape,
1148 transientTaskbarIconSize[INDEX_DEFAULT]);
1149 transientTaskbarIconSize[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1150 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeTwoPanelLandscape,
1151 transientTaskbarIconSize[INDEX_DEFAULT]);
1152 transientTaskbarIconSize[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1153 R.styleable.ProfileDisplayOption_transientTaskbarIconSizeTwoPanelPortrait,
Jon Miranda4d74ad32023-03-27 16:31:11 -07001154 transientTaskbarIconSize[INDEX_DEFAULT]);
Jon Miranda9c478b62023-03-23 21:38:49 -07001155
Jon Miranda04f05102023-04-04 12:16:31 -07001156 startAlignTaskbar[INDEX_DEFAULT] = a.getBoolean(
1157 R.styleable.ProfileDisplayOption_startAlignTaskbar, false);
1158 startAlignTaskbar[INDEX_LANDSCAPE] = a.getBoolean(
1159 R.styleable.ProfileDisplayOption_startAlignTaskbarLandscape,
1160 startAlignTaskbar[INDEX_DEFAULT]);
1161 startAlignTaskbar[INDEX_TWO_PANEL_LANDSCAPE] = a.getBoolean(
1162 R.styleable.ProfileDisplayOption_startAlignTaskbarTwoPanelLandscape,
1163 startAlignTaskbar[INDEX_LANDSCAPE]);
1164 startAlignTaskbar[INDEX_TWO_PANEL_PORTRAIT] = a.getBoolean(
1165 R.styleable.ProfileDisplayOption_startAlignTaskbarTwoPanelPortrait,
1166 startAlignTaskbar[INDEX_DEFAULT]);
1167
Sunny Goyal415f1732018-11-29 10:33:47 -08001168 a.recycle();
1169 }
1170
1171 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001172 this(null);
1173 }
1174
1175 DisplayOption(GridOption grid) {
1176 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001177 minWidthDps = 0;
1178 minHeightDps = 0;
1179 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001180 for (int i = 0; i < COUNT_SIZES; i++) {
1181 iconSizes[i] = 0;
1182 textSizes[i] = 0;
1183 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001184 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001185 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001186 allAppsIconSizes[i] = 0;
1187 allAppsIconTextSizes[i] = 0;
1188 allAppsBorderSpaces[i] = new PointF();
Jon Miranda9c478b62023-03-23 21:38:49 -07001189 transientTaskbarIconSize[i] = 0;
Jon Miranda04f05102023-04-04 12:16:31 -07001190 startAlignTaskbar[i] = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001191 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001192 }
1193
1194 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001195 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001196 iconSizes[i] *= w;
1197 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001198 borderSpaces[i].x *= w;
1199 borderSpaces[i].y *= w;
1200 minCellSize[i].x *= w;
1201 minCellSize[i].y *= w;
1202 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001203 hotseatBarBottomSpace[i] *= w;
1204 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001205 allAppsCellSize[i].x *= w;
1206 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001207 allAppsIconSizes[i] *= w;
1208 allAppsIconTextSizes[i] *= w;
1209 allAppsBorderSpaces[i].x *= w;
1210 allAppsBorderSpaces[i].y *= w;
Jon Miranda9c478b62023-03-23 21:38:49 -07001211 transientTaskbarIconSize[i] *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001212 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001213
Sunny Goyal415f1732018-11-29 10:33:47 -08001214 return this;
1215 }
1216
1217 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001218 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001219 iconSizes[i] += p.iconSizes[i];
1220 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001221 borderSpaces[i].x += p.borderSpaces[i].x;
1222 borderSpaces[i].y += p.borderSpaces[i].y;
1223 minCellSize[i].x += p.minCellSize[i].x;
1224 minCellSize[i].y += p.minCellSize[i].y;
1225 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001226 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1227 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001228 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1229 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001230 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1231 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1232 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1233 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Jon Miranda9c478b62023-03-23 21:38:49 -07001234 transientTaskbarIconSize[i] += p.transientTaskbarIconSize[i];
Jon Miranda04f05102023-04-04 12:16:31 -07001235 startAlignTaskbar[i] |= p.startAlignTaskbar[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001236 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001237
Sunny Goyal415f1732018-11-29 10:33:47 -08001238 return this;
1239 }
1240 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001241}