blob: a49832314091fa4e7fb9d8ed2e145f44e27ae9c3 [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;
44import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070045import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080046import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070047
Thales Limab35faed2022-09-05 16:30:01 -030048import androidx.annotation.DimenRes;
Alex Chau1c883d82021-12-01 18:43:10 +000049import androidx.annotation.IntDef;
Thales Limab35faed2022-09-05 16:30:01 -030050import androidx.annotation.StyleRes;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070051import androidx.annotation.VisibleForTesting;
Thales Limaae0d7ef2022-11-16 15:53:43 +000052import androidx.annotation.XmlRes;
Thales Limab8c05952022-05-23 16:58:38 +010053import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070054
Sunny Goyal65190ae2022-08-02 14:16:26 -070055import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070056import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010057import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070058import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070059import com.android.launcher3.util.DisplayController;
60import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080061import com.android.launcher3.util.IntArray;
Stefan Andonian5bd9a222023-02-23 00:58:33 +000062import com.android.launcher3.util.LockedUserState;
Sunny Goyald0e360a2018-06-29 14:40:18 -070063import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal3e58eea2022-11-14 14:30:07 -080064import com.android.launcher3.util.Partner;
Sunny Goyal5bc18462019-01-07 15:13:39 -080065import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070066import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080067import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070068
Sunny Goyal819e1932016-07-07 16:43:58 -070069import org.xmlpull.v1.XmlPullParser;
70import org.xmlpull.v1.XmlPullParserException;
71
72import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000073import java.lang.annotation.Retention;
74import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070075import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070076import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080077import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070078import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070079
80public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070081
Hyunyoung Songc55a3502018-12-04 15:43:16 -080082 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070083 // We do not need any synchronization for this variable as its only written on UI thread.
84 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070085 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070086
Alex Chau1c883d82021-12-01 18:43:10 +000087 @Retention(RetentionPolicy.SOURCE)
88 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010089 public @interface DeviceType {}
90
Alex Chau1c883d82021-12-01 18:43:10 +000091 public static final int TYPE_PHONE = 0;
92 public static final int TYPE_MULTI_DISPLAY = 1;
93 public static final int TYPE_TABLET = 2;
94
Sunny Goyal53d7ee42015-05-22 12:25:45 -070095 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
96
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070097 // Constants that affects the interpolation curve between statically defined device profile
98 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080099 private static final float KNEARESTNEIGHBOR = 3;
100 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -0700101
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700102 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800103 private static final float WEIGHT_EFFICIENT = 100000f;
104
Thales Lima83bedbf2021-10-05 17:47:39 +0100105 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
106 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000107 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100108 static final int INDEX_DEFAULT = 0;
109 static final int INDEX_LANDSCAPE = 1;
110 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
111 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100112
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800113 /** These resources are used to override the device profile */
114 private static final String RES_GRID_NUM_ROWS = "grid_num_rows";
115 private static final String RES_GRID_NUM_COLUMNS = "grid_num_columns";
116 private static final String RES_GRID_ICON_SIZE_DP = "grid_icon_size_dp";
117
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700118 /**
119 * Number of icons per row and column in the workspace.
120 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700121 public int numRows;
122 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000123 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700124
125 /**
126 * Number of icons per row and column in the folder.
127 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700128 public int numFolderRows;
129 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100130 public float[] iconSize;
131 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700132 public int iconBitmapSize;
133 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000134 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700135
Thales Lima83bedbf2021-10-05 17:47:39 +0100136 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100137
Thales Lima83bedbf2021-10-05 17:47:39 +0100138 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300139 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100140
Thales Limab35faed2022-09-05 16:30:01 -0300141 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100142
Thales Limad852d652023-01-17 14:01:13 +0000143 public @StyleRes int cellStyle;
144
Thales Lima83bedbf2021-10-05 17:47:39 +0100145 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500146
Thales Limab7ef5692022-03-10 10:32:36 +0000147 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000148 public float[] allAppsIconSize;
149 public float[] allAppsIconTextSize;
150 public PointF[] allAppsBorderSpaces;
151
Sunny Goyal5bc18462019-01-07 15:13:39 -0800152 private SparseArray<TypedValue> mExtraAttrs;
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;
Adam Cohen27824492017-09-22 17:10:55 -0700186 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
Sunny Goyalae190ff2020-04-14 00:19:01 +0000361 mExtraAttrs = closestProfile.extraAttrs;
362
Thales Lima83bedbf2021-10-05 17:47:39 +0100363 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100364 float maxIconSize = iconSize[0];
365 for (int i = 1; i < iconSize.length; i++) {
366 maxIconSize = Math.max(maxIconSize, iconSize[i]);
367 }
368 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000369 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
370
Thales Lima83bedbf2021-10-05 17:47:39 +0100371 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700372
Thales Lima83bedbf2021-10-05 17:47:39 +0100373 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100374
Thales Lima83bedbf2021-10-05 17:47:39 +0100375 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100376
Thales Limad90faab2021-09-21 17:18:47 +0100377 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100378
Sunny Goyal19ff7282021-04-22 10:12:54 -0700379 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000380 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700381 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300382 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100383 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
384 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700385
Thales Limae9273ea2023-01-26 12:33:52 +0000386 allAppsStyle = closestProfile.allAppsStyle;
387
Sunny Goyal19ff7282021-04-22 10:12:54 -0700388 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000389 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700390 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500391
Thales Limab7ef5692022-03-10 10:32:36 +0000392 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000393 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
394 allAppsIconSize = displayOption.allAppsIconSizes;
395 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000396
Thales Lima11af7bc2022-08-12 15:24:54 +0100397 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000398
Sunny Goyalae190ff2020-04-14 00:19:01 +0000399 // If the partner customization apk contains any grid overrides, apply them
400 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700401 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700402
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000403 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700404 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700405 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700406 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000407 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chauab800f72022-12-13 17:46:06 +0000408 .setIsMultiDisplay(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000409 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700410 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000411 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700412
Sunny Goyal19ff7282021-04-22 10:12:54 -0700413 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
414 int displayWidth = bounds.bounds.width();
415 int displayHeight = bounds.bounds.height();
416 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700417
Sunny Goyal19ff7282021-04-22 10:12:54 -0700418 // We need to ensure that there is enough extra space in the wallpaper
419 // for the intended parallax effects
420 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300421 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
422 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700423 ? 2
424 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
425 defaultWallpaperSize.x =
426 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700427 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000428 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700429
Thales Lima6a590062022-11-22 15:52:49 +0000430 int numMinShownHotseatIconsForTablet = supportedProfiles
431 .stream()
432 .filter(deviceProfile -> deviceProfile.isTablet)
433 .mapToInt(deviceProfile -> deviceProfile.numShownHotseatIcons)
434 .min()
435 .orElse(0);
436
437 supportedProfiles
438 .stream()
439 .filter(deviceProfile -> deviceProfile.isTablet)
440 .forEach(deviceProfile -> {
441 deviceProfile.numShownHotseatIcons = numMinShownHotseatIconsForTablet;
442 deviceProfile.recalculateHotseatWidthAndBorderSpace();
443 });
444
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700445 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
446 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700447 }
448
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700449 public void addOnChangeListener(OnIDPChangeListener listener) {
450 mChangeListeners.add(listener);
451 }
452
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800453 public void removeOnChangeListener(OnIDPChangeListener listener) {
454 mChangeListeners.remove(listener);
455 }
456
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800457
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800458 public void setCurrentGrid(Context context, String gridName) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000459 LauncherPrefs.get(context).put(GRID_NAME, gridName);
460 MAIN_EXECUTOR.execute(() -> onConfigChanged(context.getApplicationContext()));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800461 }
462
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700463 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700464 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000465 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
466 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700467 }
468
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700469 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700470 Object[] oldState = toModelState();
471
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700472 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700473 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700474 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700475
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700476 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700477 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700478 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700479 }
480 }
481
Alex Chau1c883d82021-12-01 18:43:10 +0000482 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
483 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800484 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100485
Alex Chau1c883d82021-12-01 18:43:10 +0000486 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700487 final int depth = parser.getDepth();
488 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700489 while (((type = parser.next()) != XmlPullParser.END_TAG ||
490 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800491 if ((type == XmlPullParser.START_TAG)
492 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800493
Alex Chau1c883d82021-12-01 18:43:10 +0000494 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
495 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100496 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100497 final int displayDepth = parser.getDepth();
498 while (((type = parser.next()) != XmlPullParser.END_TAG
499 || parser.getDepth() > displayDepth)
500 && type != XmlPullParser.END_DOCUMENT) {
501 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
502 parser.getName())) {
503 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100504 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100505 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800506 }
507 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700508 }
509 }
sfufa@google.comde013292021-09-21 18:22:44 -0700510 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700511 throw new RuntimeException(e);
512 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800513
Sunny Goyalae190ff2020-04-14 00:19:01 +0000514 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800515 if (!TextUtils.isEmpty(gridName)) {
516 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100517 if (gridName.equals(option.grid.name)
518 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000519 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800520 }
521 }
522 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000523 if (filteredProfiles.isEmpty()) {
524 // No grid found, use the default options
525 for (DisplayOption option : profiles) {
526 if (option.canBeDefault) {
527 filteredProfiles.add(option);
528 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800529 }
530 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000531 if (filteredProfiles.isEmpty()) {
532 throw new RuntimeException("No display option with canBeDefault=true");
533 }
534 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700535 }
536
Thales Lima7ec83822021-08-05 13:32:10 +0100537 /**
538 * @return all the grid options that can be shown on the device
539 */
540 public List<GridOption> parseAllGridOptions(Context context) {
541 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100542
Alex Chau1c883d82021-12-01 18:43:10 +0000543 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100544 final int depth = parser.getDepth();
545 int type;
546 while (((type = parser.next()) != XmlPullParser.END_TAG
547 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
548 if ((type == XmlPullParser.START_TAG)
549 && GridOption.TAG_NAME.equals(parser.getName())) {
550 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000551 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100552 if (option.isEnabled) {
553 result.add(option);
554 }
555 }
556 }
557 } catch (IOException | XmlPullParserException e) {
558 Log.e(TAG, "Error parsing device profile", e);
559 return Collections.emptyList();
560 }
561 return result;
562 }
563
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700564 private int getLauncherIconDensity(int requiredSize) {
565 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700566 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700567 DisplayMetrics.DENSITY_LOW,
568 DisplayMetrics.DENSITY_MEDIUM,
569 DisplayMetrics.DENSITY_TV,
570 DisplayMetrics.DENSITY_HIGH,
571 DisplayMetrics.DENSITY_XHIGH,
572 DisplayMetrics.DENSITY_XXHIGH,
573 DisplayMetrics.DENSITY_XXXHIGH
574 };
575
576 int density = DisplayMetrics.DENSITY_XXXHIGH;
577 for (int i = densityBuckets.length - 1; i >= 0; i--) {
578 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
579 / DisplayMetrics.DENSITY_DEFAULT;
580 if (expectedSize >= requiredSize) {
581 density = densityBuckets[i];
582 }
583 }
584
585 return density;
586 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700587
Adam Cohen2e6da152015-05-06 11:42:25 -0700588 /**
589 * Apply any Partner customization grid overrides.
590 *
591 * Currently we support: all apps row / column count.
592 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700593 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
594 Partner p = Partner.get(context.getPackageManager());
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800595 if (p == null) {
596 return;
597 }
598 try {
599 int numRows = p.getIntValue(RES_GRID_NUM_ROWS, -1);
600 int numColumns = p.getIntValue(RES_GRID_NUM_COLUMNS, -1);
601 float iconSizePx = p.getDimenValue(RES_GRID_ICON_SIZE_DP, -1);
602
603 if (numRows > 0 && numColumns > 0) {
604 this.numRows = numRows;
605 this.numColumns = numColumns;
606 }
607 if (iconSizePx > 0) {
608 this.iconSize[InvariantDeviceProfile.INDEX_DEFAULT] =
609 Utilities.dpiFromPx(iconSizePx, dm.densityDpi);
610 }
611 } catch (Resources.NotFoundException ex) {
612 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700613 }
614 }
615
Sunny Goyal415f1732018-11-29 10:33:47 -0800616 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700617 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700618 }
619
Sunny Goyal19ff7282021-04-22 10:12:54 -0700620 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000621 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700622 int minWidthPx = Integer.MAX_VALUE;
623 int minHeightPx = Integer.MAX_VALUE;
624 for (WindowBounds bounds : displayInfo.supportedBounds) {
625 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000626 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700627 // For split displays, take half width per page
628 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
629 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700630
Sunny Goyal19ff7282021-04-22 10:12:54 -0700631 } else if (!isTablet && bounds.isLandscape()) {
632 // We will use transposed layout in this case
633 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
634 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
635 } else {
636 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
637 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
638 }
639 }
640
Thales Lima425f6822022-05-10 13:44:58 -0300641 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
642 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700643
644 // Sort the profiles based on the closeness to the device size
645 Collections.sort(points, (a, b) ->
646 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
647 dist(width, height, b.minWidthDps, b.minHeightDps)));
648
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700649 DisplayOption closestPoint = points.get(0);
650 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700651 float weights = 0;
652
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700653 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
654 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700655 }
656
657 DisplayOption out = new DisplayOption(closestOption);
658 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700659 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700660 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
661 weights += w;
662 out.add(new DisplayOption().add(p).multiply(w));
663 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700664 out.multiply(1.0f / weights);
665
Thales Lima6e0005a2021-10-27 15:53:41 +0100666 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700667 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100668 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
669 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700670 }
671
672 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700673 }
674
Sunny Goyal27835952017-01-13 12:15:53 -0800675 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700676 Resources res = context.getResources();
677 Configuration config = context.getResources().getConfiguration();
678
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400679 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
680 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100681 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
682
Alex Chaud3e8cc42022-05-03 17:45:34 +0100683 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400684 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700685
Sunny Goyal187b16c2022-03-01 16:53:23 -0800686 /**
687 * Returns the device profile matching the provided screen configuration
688 */
689 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700690 DeviceProfile bestMatch = supportedProfiles.get(0);
691 float minDiff = Float.MAX_VALUE;
692
693 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400694 float diff = Math.abs(profile.widthPx - screenWidth)
695 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700696 if (diff < minDiff) {
697 minDiff = diff;
698 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800699 } else if (diff == minDiff && profile.rotationHint == rotation) {
700 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700701 }
702 }
703 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800704 }
705
Sunny Goyal415f1732018-11-29 10:33:47 -0800706 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700707 float d = dist(x0, y0, x1, y1);
708 if (Float.compare(d, 0f) == 0) {
709 return Float.POSITIVE_INFINITY;
710 }
711 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
712 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700713
714 /**
715 * As a ratio of screen height, the total distance we want the parallax effect to span
716 * horizontally
717 */
718 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
719 float aspectRatio = width / (float) height;
720
721 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
722 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
723 // We will use these two data points to extrapolate how much the wallpaper parallax effect
724 // to span (ie travel) at any aspect ratio:
725
sfufa@google.comde013292021-09-21 18:22:44 -0700726 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
727 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700728 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
729 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
730
731 // To find out the desired width at different aspect ratios, we use the following two
732 // formulas, where the coefficient on x is the aspect ratio (width/height):
733 // (16/10)x + y = 1.5
734 // (10/16)x + y = 1.2
735 // We solve for x and y and end up with a final formula:
736 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700737 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
738 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700739 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
740 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
741 return x * aspectRatio + y;
742 }
743
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700744 public interface OnIDPChangeListener {
745
Sunny Goyalb47172b2021-05-03 19:59:51 -0700746 /**
747 * Called when the device provide changes
748 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700749 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700750 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800751
752
Sunny Goyaleff44f32019-01-09 17:29:49 -0800753 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800754
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800755 public static final String TAG_NAME = "grid-option";
756
Alex Chau1c883d82021-12-01 18:43:10 +0000757 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
758 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
759 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
760 private static final int DEVICE_CATEGORY_ALL =
761 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
762
Thales Lima11af7bc2022-08-12 15:24:54 +0100763 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
764 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
765 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
766 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
767 private static final int DONT_INLINE_QSB = 0;
768
Sunny Goyaleff44f32019-01-09 17:29:49 -0800769 public final String name;
770 public final int numRows;
771 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000772 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100773 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800774
775 private final int numFolderRows;
776 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300777 private final @StyleRes int folderStyle;
Thales Limad852d652023-01-17 14:01:13 +0000778 private final @StyleRes int cellStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800779
Thales Limae9273ea2023-01-26 12:33:52 +0000780 private final @StyleRes int allAppsStyle;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700781 private final int numAllAppsColumns;
782 private final int numDatabaseAllAppsColumns;
783 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700784 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100785
Thales Lima425f6822022-05-10 13:44:58 -0300786 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800787
Thales Lima11af7bc2022-08-12 15:24:54 +0100788 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
789
Thales Limab35faed2022-09-05 16:30:01 -0300790 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800791 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000792
Sunny Goyal415f1732018-11-29 10:33:47 -0800793 private final int defaultLayoutId;
794 private final int demoModeLayoutId;
795
Jon Mirandae126d722021-02-25 10:45:20 -0500796 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400797 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500798
Sunny Goyal5bc18462019-01-07 15:13:39 -0800799 private final SparseArray<TypedValue> extraAttrs;
800
Alex Chau1c883d82021-12-01 18:43:10 +0000801 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800802 TypedArray a = context.obtainStyledAttributes(
803 attrs, R.styleable.GridDisplayOption);
804 name = a.getString(R.styleable.GridDisplayOption_name);
805 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
806 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000807 numSearchContainerColumns = a.getInt(
808 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800809
Tracy Zhou7df93d22020-01-27 13:44:06 -0800810 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau766cd2b2022-07-06 17:15:21 +0100811 defaultLayoutId = a.getResourceId(
812 R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800813 demoModeLayoutId = a.getResourceId(
814 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700815
Thales Limae9273ea2023-01-26 12:33:52 +0000816 allAppsStyle = a.getResourceId(R.styleable.GridDisplayOption_allAppsStyle,
817 R.style.AllAppsStyleDefault);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700818 numAllAppsColumns = a.getInt(
819 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
820 numDatabaseAllAppsColumns = a.getInt(
821 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
822
823 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800824 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700825 numDatabaseHotseatIcons = a.getInt(
826 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100827
Thales Lima425f6822022-05-10 13:44:58 -0300828 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
829 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
830 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
831 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
832 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
833 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
834 numColumns);
835 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
836 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
837 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700838
Vinit Nayakc7293172022-07-18 16:41:50 -0700839 inlineNavButtonsEndSpacing =
840 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
841 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300842
Sunny Goyal415f1732018-11-29 10:33:47 -0800843 numFolderRows = a.getInt(
844 R.styleable.GridDisplayOption_numFolderRows, numRows);
845 numFolderColumns = a.getInt(
846 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700847
Thales Limab35faed2022-09-05 16:30:01 -0300848 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
849 INVALID_RESOURCE_HANDLE);
850
Thales Limad852d652023-01-17 14:01:13 +0000851 cellStyle = a.getResourceId(R.styleable.GridDisplayOption_cellStyle,
852 R.style.CellStyleDefault);
853
Jon Mirandae126d722021-02-25 10:45:20 -0500854 isScalable = a.getBoolean(
855 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400856 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000857 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Jon Mirandae126d722021-02-25 10:45:20 -0500858
Alex Chau1c883d82021-12-01 18:43:10 +0000859 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
860 DEVICE_CATEGORY_ALL);
861 isEnabled = (deviceType == TYPE_PHONE
862 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
863 || (deviceType == TYPE_TABLET
864 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
865 || (deviceType == TYPE_MULTI_DISPLAY
866 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
867 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100868
Thales Lima11af7bc2022-08-12 15:24:54 +0100869 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
870 DONT_INLINE_QSB);
871 inlineQsb[INDEX_DEFAULT] =
872 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
873 inlineQsb[INDEX_LANDSCAPE] =
874 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
875 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
876 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
877 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
878 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
879 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
880 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
881
Sunny Goyal415f1732018-11-29 10:33:47 -0800882 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800883 extraAttrs = Themes.createValueMap(context, attrs,
884 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800885 }
886 }
887
Sunny Goyal19ff7282021-04-22 10:12:54 -0700888 @VisibleForTesting
889 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700890 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800891
Sunny Goyal415f1732018-11-29 10:33:47 -0800892 private final float minWidthDps;
893 private final float minHeightDps;
894 private final boolean canBeDefault;
895
Thales Lima83bedbf2021-10-05 17:47:39 +0100896 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100897
Thales Lima78d00ad2021-09-30 11:29:06 +0100898 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100899 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100900 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
901 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100902
Thales Lima78d00ad2021-09-30 11:29:06 +0100903 private final float[] iconSizes = new float[COUNT_SIZES];
904 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800905
Thales Limab7ef5692022-03-10 10:32:36 +0000906 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000907 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
908 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
909 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
910
Thales Lima1de4d552021-10-13 16:13:25 +0100911 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800912 this.grid = grid;
913
Thales Lima1de4d552021-10-13 16:13:25 +0100914 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800915
Sunny Goyal415f1732018-11-29 10:33:47 -0800916 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
917 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700918
Thales Lima1de4d552021-10-13 16:13:25 +0100919 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800920
Thales Lima83bedbf2021-10-05 17:47:39 +0100921 float x;
922 float y;
923
Thales Lima85c942f2021-12-31 13:04:20 +0000924 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
925 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100926 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000927
928 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
929 minCellSize[INDEX_DEFAULT].x);
930 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
931 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100932 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100933
Thales Lima85c942f2021-12-31 13:04:20 +0000934 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100935 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000936 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100937 minCellSize[INDEX_DEFAULT].y);
938 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
939
Thales Lima85c942f2021-12-31 13:04:20 +0000940 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100941 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000942 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100943 minCellSize[INDEX_DEFAULT].y);
944 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100945
Thales Lima85c942f2021-12-31 13:04:20 +0000946 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000947 float borderSpaceLandscape = a.getFloat(
948 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000949 float borderSpaceTwoPanelPortrait = a.getFloat(
950 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
951 float borderSpaceTwoPanelLandscape = a.getFloat(
952 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100953
Thales Lima85c942f2021-12-31 13:04:20 +0000954 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
955 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100956 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000957
958 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
959 borderSpaceLandscape);
960 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
961 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100962 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
963
964 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000965 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
966 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100967 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000968 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
969 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100970 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
971
972 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000973 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
974 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100975 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000976 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
977 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100978 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
979
Thales Limab7ef5692022-03-10 10:32:36 +0000980 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
981 minCellSize[INDEX_DEFAULT].x);
982 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
983 minCellSize[INDEX_DEFAULT].y);
984 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
985
986 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
987 allAppsCellSize[INDEX_DEFAULT].x);
988 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
989 allAppsCellSize[INDEX_DEFAULT].y);
990 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
991
992 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
993 allAppsCellSize[INDEX_DEFAULT].x);
994 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
995 allAppsCellSize[INDEX_DEFAULT].y);
996 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
997
998 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
999 allAppsCellSize[INDEX_DEFAULT].x);
1000 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
1001 allAppsCellSize[INDEX_DEFAULT].y);
1002 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
1003
Thales Lima080d8902022-03-28 15:30:29 +01001004 float allAppsBorderSpace = a.getFloat(
1005 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
1006 float allAppsBorderSpaceLandscape = a.getFloat(
1007 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
1008 allAppsBorderSpace);
1009 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001010 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +01001011 allAppsBorderSpace);
1012 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001013 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +01001014 allAppsBorderSpace);
1015
1016 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
1017 allAppsBorderSpace);
1018 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
1019 allAppsBorderSpace);
1020 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
1021
1022 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
1023 allAppsBorderSpaceLandscape);
1024 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1025 allAppsBorderSpaceLandscape);
1026 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1027
1028 x = a.getFloat(
1029 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1030 allAppsBorderSpaceTwoPanelPortrait);
1031 y = a.getFloat(
1032 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1033 allAppsBorderSpaceTwoPanelPortrait);
1034 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1035
1036 x = a.getFloat(
1037 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1038 allAppsBorderSpaceTwoPanelLandscape);
1039 y = a.getFloat(
1040 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1041 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001042 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001043
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001044 iconSizes[INDEX_DEFAULT] =
1045 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1046 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001047 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001048 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001049 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001050 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001051 iconSizes[INDEX_DEFAULT]);
1052 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001053 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001054 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001055
Thales Limabb7d3882021-12-22 12:56:29 +00001056 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1057 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001058 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1059 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001060 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001061 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1062 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1063 allAppsIconSizes[INDEX_DEFAULT]);
1064 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1065 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1066 allAppsIconSizes[INDEX_DEFAULT]);
1067
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001068 textSizes[INDEX_DEFAULT] =
1069 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1070 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001071 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001072 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001073 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001074 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001075 textSizes[INDEX_DEFAULT]);
1076 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001077 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001078 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001079
Thales Limabb7d3882021-12-22 12:56:29 +00001080 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1081 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1082 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1083 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1084 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1085 allAppsIconTextSizes[INDEX_DEFAULT]);
1086 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1087 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1088 allAppsIconTextSizes[INDEX_DEFAULT]);
1089
Thales Lima83bedbf2021-10-05 17:47:39 +01001090 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1091 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001092 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1093 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1094 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001095 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001096 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001097 horizontalMargin[INDEX_DEFAULT]);
1098 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001099 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001100 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001101
Thales Limab8c05952022-05-23 16:58:38 +01001102 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1103 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1104 ResourcesCompat.getFloat(context.getResources(),
1105 R.dimen.hotseat_bar_bottom_space_default));
1106 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1107 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1108 hotseatBarBottomSpace[INDEX_DEFAULT]);
1109 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1110 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1111 hotseatBarBottomSpace[INDEX_DEFAULT]);
1112 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1113 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1114 hotseatBarBottomSpace[INDEX_DEFAULT]);
1115
1116 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1117 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1118 ResourcesCompat.getFloat(context.getResources(),
1119 R.dimen.hotseat_qsb_space_default));
1120 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1121 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1122 hotseatQsbSpace[INDEX_DEFAULT]);
1123 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1124 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1125 hotseatQsbSpace[INDEX_DEFAULT]);
1126 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1127 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1128 hotseatQsbSpace[INDEX_DEFAULT]);
1129
Sunny Goyal415f1732018-11-29 10:33:47 -08001130 a.recycle();
1131 }
1132
1133 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001134 this(null);
1135 }
1136
1137 DisplayOption(GridOption grid) {
1138 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001139 minWidthDps = 0;
1140 minHeightDps = 0;
1141 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001142 for (int i = 0; i < COUNT_SIZES; i++) {
1143 iconSizes[i] = 0;
1144 textSizes[i] = 0;
1145 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001146 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001147 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001148 allAppsIconSizes[i] = 0;
1149 allAppsIconTextSizes[i] = 0;
1150 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001151 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001152 }
1153
1154 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001155 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001156 iconSizes[i] *= w;
1157 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001158 borderSpaces[i].x *= w;
1159 borderSpaces[i].y *= w;
1160 minCellSize[i].x *= w;
1161 minCellSize[i].y *= w;
1162 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001163 hotseatBarBottomSpace[i] *= w;
1164 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001165 allAppsCellSize[i].x *= w;
1166 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001167 allAppsIconSizes[i] *= w;
1168 allAppsIconTextSizes[i] *= w;
1169 allAppsBorderSpaces[i].x *= w;
1170 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001171 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001172
Sunny Goyal415f1732018-11-29 10:33:47 -08001173 return this;
1174 }
1175
1176 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001177 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001178 iconSizes[i] += p.iconSizes[i];
1179 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001180 borderSpaces[i].x += p.borderSpaces[i].x;
1181 borderSpaces[i].y += p.borderSpaces[i].y;
1182 minCellSize[i].x += p.minCellSize[i].x;
1183 minCellSize[i].y += p.minCellSize[i].y;
1184 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001185 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1186 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001187 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1188 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001189 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1190 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1191 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1192 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001193 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001194
Sunny Goyal415f1732018-11-29 10:33:47 -08001195 return this;
1196 }
1197 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001198}