blob: 9c5ec388660a2d94cb31fe2fdbba878bea65e7ae [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
Sunny Goyal19ff7282021-04-22 10:12:54 -070019import static com.android.launcher3.Utilities.dpiFromPx;
Alex Chau24265142022-11-14 21:19:44 +000020import static com.android.launcher3.config.FeatureFlags.ENABLE_DEVICE_PROFILE_LOGGING;
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;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070050import androidx.annotation.Nullable;
Thales Limab35faed2022-09-05 16:30:01 -030051import androidx.annotation.StyleRes;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070052import androidx.annotation.VisibleForTesting;
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;
Sunny Goyald0e360a2018-06-29 14:40:18 -070062import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080063import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070064import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080065import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070066
Sunny Goyal819e1932016-07-07 16:43:58 -070067import org.xmlpull.v1.XmlPullParser;
68import org.xmlpull.v1.XmlPullParserException;
69
70import java.io.IOException;
Alex Chau5dfd9392022-11-02 17:39:40 +000071import java.io.PrintWriter;
72import java.io.StringWriter;
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
Hyunyoung Songc55a3502018-12-04 15:43:16 -080095 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080096
Sunny Goyal53d7ee42015-05-22 12:25:45 -070097 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
98
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099 // Constants that affects the interpolation curve between statically defined device profile
100 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800101 private static final float KNEARESTNEIGHBOR = 3;
102 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -0700103
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700104 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800105 private static final float WEIGHT_EFFICIENT = 100000f;
106
Thales Lima83bedbf2021-10-05 17:47:39 +0100107 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
108 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000109 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100110 static final int INDEX_DEFAULT = 0;
111 static final int INDEX_LANDSCAPE = 1;
112 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
113 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100114
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700115 /**
116 * Number of icons per row and column in the workspace.
117 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700118 public int numRows;
119 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000120 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700121
122 /**
123 * Number of icons per row and column in the folder.
124 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700125 public int numFolderRows;
126 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100127 public float[] iconSize;
128 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700129 public int iconBitmapSize;
130 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000131 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700132
Thales Lima83bedbf2021-10-05 17:47:39 +0100133 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100134
Thales Lima83bedbf2021-10-05 17:47:39 +0100135 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300136 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100137
Thales Limab35faed2022-09-05 16:30:01 -0300138 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100139
Thales Lima83bedbf2021-10-05 17:47:39 +0100140 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500141
Thales Limab7ef5692022-03-10 10:32:36 +0000142 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000143 public float[] allAppsIconSize;
144 public float[] allAppsIconTextSize;
145 public PointF[] allAppsBorderSpaces;
146
Sunny Goyal5bc18462019-01-07 15:13:39 -0800147 private SparseArray<TypedValue> mExtraAttrs;
148
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700149 /**
150 * Number of icons inside the hotseat area.
151 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800152 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700153
154 /**
155 * Number of icons inside the hotseat area that is stored in the database. This is greater than
156 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
157 * sizes that share the same DB.
158 */
159 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700160
Thales Lima425f6822022-05-10 13:44:58 -0300161 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100162 public float[] hotseatBarBottomSpace;
163 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300164
Jon Miranda6f7e9702019-09-16 14:44:14 -0700165 /**
166 * Number of columns in the all apps list.
167 */
168 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700169 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700170
Jon Mirandae126d722021-02-25 10:45:20 -0500171 /**
172 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
173 */
174 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400175 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500176
Tracy Zhou7df93d22020-01-27 13:44:06 -0800177 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800178 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700179 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100180 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700181
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000182 /**
183 * An immutable list of supported profiles.
184 */
185 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700186
sfufa@google.comde013292021-09-21 18:22:44 -0700187 @Nullable
188 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500189
Sunny Goyal6f866092016-03-17 17:04:15 -0700190 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700191 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700192
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700193 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700194
Sunny Goyalf633ef52018-03-13 09:57:05 -0700195 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800196 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700197
Sunny Goyalbbf01842015-10-08 07:41:15 -0700198 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700199 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700200 String gridName = getCurrentGridName(context);
201 String newGridName = initGrid(context, gridName);
202 if (!newGridName.equals(gridName)) {
203 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
204 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700205 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700206
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700207 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100208 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000209 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
210 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100211 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700212 }
213 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700214 }
215
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700216 /**
217 * This constructor should NOT have any monitors by design.
218 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800219 public InvariantDeviceProfile(Context context, String gridName) {
220 String newName = initGrid(context, gridName);
221 if (newName == null || !newName.equals(gridName)) {
222 throw new IllegalArgumentException("Unknown grid name");
223 }
224 }
225
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700226 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800227 * This constructor should NOT have any monitors by design.
228 */
229 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700230 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100231 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700232 String gridName = getCurrentGridName(context);
233
234 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000235 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
236 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700237 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000238 defaultInfo,
239 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
240 /*allowDisabledGrid=*/false),
241 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700242
Alex Chau661f02d2022-06-07 14:03:43 +0100243 Context displayContext = context.createDisplayContext(display);
244 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000245 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700246 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000247 myInfo,
248 getPredefinedDeviceProfiles(context, gridName, deviceType,
249 /*allowDisabledGrid=*/false),
250 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700251
252 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
253 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100254 result.iconSizes[INDEX_DEFAULT] =
255 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
256 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700257 result.iconSizes[i] = Math.min(
258 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000259 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700260
Thales Lima83bedbf2021-10-05 17:47:39 +0100261 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
262 COUNT_SIZES);
263 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
264 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500265
Alex Chau1c883d82021-12-01 18:43:10 +0000266 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800267 }
268
Alex Chau238aaee2021-10-06 16:15:24 +0100269 /**
270 * Reinitialize the current grid after a restore, where some grids might now be disabled.
271 */
272 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000273 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100274 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000275 String newGridName = initGrid(context, currentGridName);
276 String newDbFile = dbFile;
277 if (!newDbFile.equals(currentDbFile)) {
278 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100279 + ", migrating to: " + newGridName
280 + ", removing all other grid db files");
281 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
282 if (gridDbFile.equals(currentDbFile)) {
283 continue;
284 }
285 if (context.getDatabasePath(gridDbFile).delete()) {
286 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
287 }
288 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000289 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100290 }
291 }
292
Alex Chau1c883d82021-12-01 18:43:10 +0000293 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800294 int flagPhone = 1 << 0;
295 int flagTablet = 1 << 1;
296
297 int type = displayInfo.supportedBounds.stream()
298 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
299 .reduce(0, (a, b) -> a | b);
300 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
301 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000302 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800303 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000304 return TYPE_TABLET;
305 } else {
306 return TYPE_PHONE;
307 }
308 }
309
Tracy Zhou42255d22020-03-13 00:38:11 -0700310 public static String getCurrentGridName(Context context) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700311 return Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null);
Tracy Zhou42255d22020-03-13 00:38:11 -0700312 }
313
Sunny Goyaleff44f32019-01-09 17:29:49 -0800314 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700315 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000316 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700317
318 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000319 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100320 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700321 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000322 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
323 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700324 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800325 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700326
Thales Limab67bfa72022-11-02 15:30:11 +0000327 @VisibleForTesting
328 public static String getDefaultGridName(Context context) {
329 return new InvariantDeviceProfile().initGrid(context, null);
330 }
331
Alex Chau1c883d82021-12-01 18:43:10 +0000332 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
333 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700334 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700335 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000336 numRows = closestProfile.numRows;
337 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000338 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000339 dbFile = closestProfile.dbFile;
340 defaultLayoutId = closestProfile.defaultLayoutId;
341 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300342
Sunny Goyalae190ff2020-04-14 00:19:01 +0000343 numFolderRows = closestProfile.numFolderRows;
344 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300345 folderStyle = closestProfile.folderStyle;
346
Jon Mirandae126d722021-02-25 10:45:20 -0500347 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400348 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000349 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000350
Vinit Nayakc7293172022-07-18 16:41:50 -0700351 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
352
Sunny Goyalae190ff2020-04-14 00:19:01 +0000353 mExtraAttrs = closestProfile.extraAttrs;
354
Thales Lima83bedbf2021-10-05 17:47:39 +0100355 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100356 float maxIconSize = iconSize[0];
357 for (int i = 1; i < iconSize.length; i++) {
358 maxIconSize = Math.max(maxIconSize, iconSize[i]);
359 }
360 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000361 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
362
Thales Lima83bedbf2021-10-05 17:47:39 +0100363 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700364
Thales Lima83bedbf2021-10-05 17:47:39 +0100365 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100366
Thales Lima83bedbf2021-10-05 17:47:39 +0100367 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100368
Thales Limad90faab2021-09-21 17:18:47 +0100369 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100370
Sunny Goyal19ff7282021-04-22 10:12:54 -0700371 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000372 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700373 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300374 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100375 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
376 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700377
378 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000379 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700380 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500381
Thales Limab7ef5692022-03-10 10:32:36 +0000382 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000383 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
384 allAppsIconSize = displayOption.allAppsIconSizes;
385 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000386
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400387 if (devicePaddingId != 0) {
388 devicePaddings = new DevicePaddings(context, devicePaddingId);
389 }
390
Thales Lima11af7bc2022-08-12 15:24:54 +0100391 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000392
Sunny Goyalae190ff2020-04-14 00:19:01 +0000393 // If the partner customization apk contains any grid overrides, apply them
394 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700395 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700396
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000397 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700398 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700399 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700400 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000401 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000402 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000403 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700404 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000405 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700406
Sunny Goyal19ff7282021-04-22 10:12:54 -0700407 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
408 int displayWidth = bounds.bounds.width();
409 int displayHeight = bounds.bounds.height();
410 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700411
Sunny Goyal19ff7282021-04-22 10:12:54 -0700412 // We need to ensure that there is enough extra space in the wallpaper
413 // for the intended parallax effects
414 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300415 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
416 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700417 ? 2
418 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
419 defaultWallpaperSize.x =
420 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700421 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000422 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700423
424 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
425 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700426 }
427
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700428 public void addOnChangeListener(OnIDPChangeListener listener) {
429 mChangeListeners.add(listener);
430 }
431
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800432 public void removeOnChangeListener(OnIDPChangeListener listener) {
433 mChangeListeners.remove(listener);
434 }
435
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800436
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800437 public void setCurrentGrid(Context context, String gridName) {
438 Context appContext = context.getApplicationContext();
439 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700440 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800441 }
442
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700443 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700444 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000445 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
446 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700447 }
448
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700449 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700450 Object[] oldState = toModelState();
451
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700452 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700453 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700454 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700455
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700456 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700457 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700458 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700459 }
460 }
461
Alex Chau1c883d82021-12-01 18:43:10 +0000462 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
463 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800464 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100465
Alex Chau1c883d82021-12-01 18:43:10 +0000466 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700467 final int depth = parser.getDepth();
468 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700469 while (((type = parser.next()) != XmlPullParser.END_TAG ||
470 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800471 if ((type == XmlPullParser.START_TAG)
472 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800473
Alex Chau1c883d82021-12-01 18:43:10 +0000474 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
475 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100476 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100477 final int displayDepth = parser.getDepth();
478 while (((type = parser.next()) != XmlPullParser.END_TAG
479 || parser.getDepth() > displayDepth)
480 && type != XmlPullParser.END_DOCUMENT) {
481 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
482 parser.getName())) {
483 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100484 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100485 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800486 }
487 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700488 }
489 }
sfufa@google.comde013292021-09-21 18:22:44 -0700490 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700491 throw new RuntimeException(e);
492 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800493
Sunny Goyalae190ff2020-04-14 00:19:01 +0000494 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800495 if (!TextUtils.isEmpty(gridName)) {
496 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100497 if (gridName.equals(option.grid.name)
498 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000499 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800500 }
501 }
502 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000503 if (filteredProfiles.isEmpty()) {
504 // No grid found, use the default options
505 for (DisplayOption option : profiles) {
506 if (option.canBeDefault) {
507 filteredProfiles.add(option);
508 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800509 }
510 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000511 if (filteredProfiles.isEmpty()) {
512 throw new RuntimeException("No display option with canBeDefault=true");
513 }
514 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700515 }
516
Thales Lima7ec83822021-08-05 13:32:10 +0100517 /**
518 * @return all the grid options that can be shown on the device
519 */
520 public List<GridOption> parseAllGridOptions(Context context) {
521 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100522
Alex Chau1c883d82021-12-01 18:43:10 +0000523 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100524 final int depth = parser.getDepth();
525 int type;
526 while (((type = parser.next()) != XmlPullParser.END_TAG
527 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
528 if ((type == XmlPullParser.START_TAG)
529 && GridOption.TAG_NAME.equals(parser.getName())) {
530 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000531 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100532 if (option.isEnabled) {
533 result.add(option);
534 }
535 }
536 }
537 } catch (IOException | XmlPullParserException e) {
538 Log.e(TAG, "Error parsing device profile", e);
539 return Collections.emptyList();
540 }
541 return result;
542 }
543
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700544 private int getLauncherIconDensity(int requiredSize) {
545 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700546 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700547 DisplayMetrics.DENSITY_LOW,
548 DisplayMetrics.DENSITY_MEDIUM,
549 DisplayMetrics.DENSITY_TV,
550 DisplayMetrics.DENSITY_HIGH,
551 DisplayMetrics.DENSITY_XHIGH,
552 DisplayMetrics.DENSITY_XXHIGH,
553 DisplayMetrics.DENSITY_XXXHIGH
554 };
555
556 int density = DisplayMetrics.DENSITY_XXXHIGH;
557 for (int i = densityBuckets.length - 1; i >= 0; i--) {
558 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
559 / DisplayMetrics.DENSITY_DEFAULT;
560 if (expectedSize >= requiredSize) {
561 density = densityBuckets[i];
562 }
563 }
564
565 return density;
566 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700567
Adam Cohen2e6da152015-05-06 11:42:25 -0700568 /**
569 * Apply any Partner customization grid overrides.
570 *
571 * Currently we support: all apps row / column count.
572 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700573 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
574 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700575 if (p != null) {
576 p.applyInvariantDeviceProfileOverrides(this, dm);
577 }
578 }
579
Sunny Goyal415f1732018-11-29 10:33:47 -0800580 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700581 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700582 }
583
Sunny Goyal19ff7282021-04-22 10:12:54 -0700584 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000585 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700586 int minWidthPx = Integer.MAX_VALUE;
587 int minHeightPx = Integer.MAX_VALUE;
588 for (WindowBounds bounds : displayInfo.supportedBounds) {
589 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000590 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700591 // For split displays, take half width per page
592 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
593 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700594
Sunny Goyal19ff7282021-04-22 10:12:54 -0700595 } else if (!isTablet && bounds.isLandscape()) {
596 // We will use transposed layout in this case
597 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
598 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
599 } else {
600 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
601 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
602 }
603 }
604
Thales Lima425f6822022-05-10 13:44:58 -0300605 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
606 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700607
608 // Sort the profiles based on the closeness to the device size
609 Collections.sort(points, (a, b) ->
610 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
611 dist(width, height, b.minWidthDps, b.minHeightDps)));
612
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700613 DisplayOption closestPoint = points.get(0);
614 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700615 float weights = 0;
616
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700617 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
618 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700619 }
620
621 DisplayOption out = new DisplayOption(closestOption);
622 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700623 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700624 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
625 weights += w;
626 out.add(new DisplayOption().add(p).multiply(w));
627 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700628 out.multiply(1.0f / weights);
629
Thales Lima6e0005a2021-10-27 15:53:41 +0100630 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700631 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100632 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
633 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700634 }
635
636 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700637 }
638
Sunny Goyal27835952017-01-13 12:15:53 -0800639 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700640 Resources res = context.getResources();
641 Configuration config = context.getResources().getConfiguration();
642
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400643 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
644 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100645 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
646
Alex Chau24265142022-11-14 21:19:44 +0000647 if (Utilities.IS_DEBUG_DEVICE && ENABLE_DEVICE_PROFILE_LOGGING.get()) {
Alex Chau5dfd9392022-11-02 17:39:40 +0000648 StringWriter stringWriter = new StringWriter();
649 PrintWriter printWriter = new PrintWriter(stringWriter);
650 DisplayController.INSTANCE.get(context).dump(printWriter);
651 printWriter.flush();
652 Log.d("b/253338238", "getDeviceProfile -"
653 + "\nconfig: " + config
654 + "\ndisplayMetrics: " + res.getDisplayMetrics()
655 + "\nrotation: " + rotation
656 + "\n" + stringWriter,
657 new Exception());
658 }
Alex Chaud3e8cc42022-05-03 17:45:34 +0100659 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400660 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700661
Sunny Goyal187b16c2022-03-01 16:53:23 -0800662 /**
663 * Returns the device profile matching the provided screen configuration
664 */
665 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700666 DeviceProfile bestMatch = supportedProfiles.get(0);
667 float minDiff = Float.MAX_VALUE;
668
669 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400670 float diff = Math.abs(profile.widthPx - screenWidth)
671 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700672 if (diff < minDiff) {
673 minDiff = diff;
674 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800675 } else if (diff == minDiff && profile.rotationHint == rotation) {
676 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700677 }
678 }
679 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800680 }
681
Sunny Goyal415f1732018-11-29 10:33:47 -0800682 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700683 float d = dist(x0, y0, x1, y1);
684 if (Float.compare(d, 0f) == 0) {
685 return Float.POSITIVE_INFINITY;
686 }
687 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
688 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700689
690 /**
691 * As a ratio of screen height, the total distance we want the parallax effect to span
692 * horizontally
693 */
694 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
695 float aspectRatio = width / (float) height;
696
697 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
698 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
699 // We will use these two data points to extrapolate how much the wallpaper parallax effect
700 // to span (ie travel) at any aspect ratio:
701
sfufa@google.comde013292021-09-21 18:22:44 -0700702 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
703 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700704 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
705 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
706
707 // To find out the desired width at different aspect ratios, we use the following two
708 // formulas, where the coefficient on x is the aspect ratio (width/height):
709 // (16/10)x + y = 1.5
710 // (10/16)x + y = 1.2
711 // We solve for x and y and end up with a final formula:
712 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700713 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
714 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700715 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
716 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
717 return x * aspectRatio + y;
718 }
719
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700720 public interface OnIDPChangeListener {
721
Sunny Goyalb47172b2021-05-03 19:59:51 -0700722 /**
723 * Called when the device provide changes
724 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700725 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700726 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800727
728
Sunny Goyaleff44f32019-01-09 17:29:49 -0800729 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800730
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800731 public static final String TAG_NAME = "grid-option";
732
Alex Chau1c883d82021-12-01 18:43:10 +0000733 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
734 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
735 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
736 private static final int DEVICE_CATEGORY_ALL =
737 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
738
Thales Lima11af7bc2022-08-12 15:24:54 +0100739 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
740 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
741 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
742 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
743 private static final int DONT_INLINE_QSB = 0;
744
Sunny Goyaleff44f32019-01-09 17:29:49 -0800745 public final String name;
746 public final int numRows;
747 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000748 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100749 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800750
751 private final int numFolderRows;
752 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300753 private final @StyleRes int folderStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800754
Sunny Goyal19ff7282021-04-22 10:12:54 -0700755 private final int numAllAppsColumns;
756 private final int numDatabaseAllAppsColumns;
757 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700758 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100759
Thales Lima425f6822022-05-10 13:44:58 -0300760 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800761
Thales Lima11af7bc2022-08-12 15:24:54 +0100762 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
763
Thales Limab35faed2022-09-05 16:30:01 -0300764 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800765 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000766
Sunny Goyal415f1732018-11-29 10:33:47 -0800767 private final int defaultLayoutId;
768 private final int demoModeLayoutId;
769
Jon Mirandae126d722021-02-25 10:45:20 -0500770 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400771 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500772
Sunny Goyal5bc18462019-01-07 15:13:39 -0800773 private final SparseArray<TypedValue> extraAttrs;
774
Alex Chau1c883d82021-12-01 18:43:10 +0000775 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800776 TypedArray a = context.obtainStyledAttributes(
777 attrs, R.styleable.GridDisplayOption);
778 name = a.getString(R.styleable.GridDisplayOption_name);
779 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
780 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000781 numSearchContainerColumns = a.getInt(
782 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800783
Tracy Zhou7df93d22020-01-27 13:44:06 -0800784 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000785 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100786 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
787 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
788 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800789 demoModeLayoutId = a.getResourceId(
790 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700791
792 numAllAppsColumns = a.getInt(
793 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
794 numDatabaseAllAppsColumns = a.getInt(
795 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
796
797 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800798 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700799 numDatabaseHotseatIcons = a.getInt(
800 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100801
Thales Lima425f6822022-05-10 13:44:58 -0300802 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
803 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
804 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
805 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
806 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
807 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
808 numColumns);
809 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
810 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
811 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700812
Vinit Nayakc7293172022-07-18 16:41:50 -0700813 inlineNavButtonsEndSpacing =
814 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
815 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300816
Sunny Goyal415f1732018-11-29 10:33:47 -0800817 numFolderRows = a.getInt(
818 R.styleable.GridDisplayOption_numFolderRows, numRows);
819 numFolderColumns = a.getInt(
820 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700821
Thales Limab35faed2022-09-05 16:30:01 -0300822 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
823 INVALID_RESOURCE_HANDLE);
824
Jon Mirandae126d722021-02-25 10:45:20 -0500825 isScalable = a.getBoolean(
826 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400827 devicePaddingId = a.getResourceId(
828 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500829
Alex Chau1c883d82021-12-01 18:43:10 +0000830 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
831 DEVICE_CATEGORY_ALL);
832 isEnabled = (deviceType == TYPE_PHONE
833 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
834 || (deviceType == TYPE_TABLET
835 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
836 || (deviceType == TYPE_MULTI_DISPLAY
837 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
838 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100839
Thales Lima11af7bc2022-08-12 15:24:54 +0100840 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
841 DONT_INLINE_QSB);
842 inlineQsb[INDEX_DEFAULT] =
843 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
844 inlineQsb[INDEX_LANDSCAPE] =
845 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
846 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
847 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
848 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
849 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
850 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
851 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
852
Sunny Goyal415f1732018-11-29 10:33:47 -0800853 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800854 extraAttrs = Themes.createValueMap(context, attrs,
855 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800856 }
857 }
858
Sunny Goyal19ff7282021-04-22 10:12:54 -0700859 @VisibleForTesting
860 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700861 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800862
Sunny Goyal415f1732018-11-29 10:33:47 -0800863 private final float minWidthDps;
864 private final float minHeightDps;
865 private final boolean canBeDefault;
866
Thales Lima83bedbf2021-10-05 17:47:39 +0100867 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100868
Thales Lima78d00ad2021-09-30 11:29:06 +0100869 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100870 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100871 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
872 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100873
Thales Lima78d00ad2021-09-30 11:29:06 +0100874 private final float[] iconSizes = new float[COUNT_SIZES];
875 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800876
Thales Limab7ef5692022-03-10 10:32:36 +0000877 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000878 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
879 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
880 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
881
Thales Lima1de4d552021-10-13 16:13:25 +0100882 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800883 this.grid = grid;
884
Thales Lima1de4d552021-10-13 16:13:25 +0100885 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800886
Sunny Goyal415f1732018-11-29 10:33:47 -0800887 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
888 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700889
Thales Lima1de4d552021-10-13 16:13:25 +0100890 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800891
Thales Lima83bedbf2021-10-05 17:47:39 +0100892 float x;
893 float y;
894
Thales Lima85c942f2021-12-31 13:04:20 +0000895 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
896 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100897 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000898
899 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
900 minCellSize[INDEX_DEFAULT].x);
901 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
902 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100903 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100904
Thales Lima85c942f2021-12-31 13:04:20 +0000905 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100906 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000907 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100908 minCellSize[INDEX_DEFAULT].y);
909 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
910
Thales Lima85c942f2021-12-31 13:04:20 +0000911 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100912 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000913 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100914 minCellSize[INDEX_DEFAULT].y);
915 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100916
Thales Lima85c942f2021-12-31 13:04:20 +0000917 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000918 float borderSpaceLandscape = a.getFloat(
919 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000920 float borderSpaceTwoPanelPortrait = a.getFloat(
921 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
922 float borderSpaceTwoPanelLandscape = a.getFloat(
923 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100924
Thales Lima85c942f2021-12-31 13:04:20 +0000925 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
926 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100927 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000928
929 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
930 borderSpaceLandscape);
931 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
932 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100933 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
934
935 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000936 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
937 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100938 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000939 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
940 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100941 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
942
943 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000944 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
945 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100946 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000947 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
948 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100949 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
950
Thales Limab7ef5692022-03-10 10:32:36 +0000951 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
952 minCellSize[INDEX_DEFAULT].x);
953 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
954 minCellSize[INDEX_DEFAULT].y);
955 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
956
957 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
958 allAppsCellSize[INDEX_DEFAULT].x);
959 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
960 allAppsCellSize[INDEX_DEFAULT].y);
961 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
962
963 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
964 allAppsCellSize[INDEX_DEFAULT].x);
965 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
966 allAppsCellSize[INDEX_DEFAULT].y);
967 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
968
969 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
970 allAppsCellSize[INDEX_DEFAULT].x);
971 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
972 allAppsCellSize[INDEX_DEFAULT].y);
973 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
974
Thales Lima080d8902022-03-28 15:30:29 +0100975 float allAppsBorderSpace = a.getFloat(
976 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
977 float allAppsBorderSpaceLandscape = a.getFloat(
978 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
979 allAppsBorderSpace);
980 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000981 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100982 allAppsBorderSpace);
983 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000984 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100985 allAppsBorderSpace);
986
987 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
988 allAppsBorderSpace);
989 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
990 allAppsBorderSpace);
991 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
992
993 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
994 allAppsBorderSpaceLandscape);
995 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
996 allAppsBorderSpaceLandscape);
997 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
998
999 x = a.getFloat(
1000 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1001 allAppsBorderSpaceTwoPanelPortrait);
1002 y = a.getFloat(
1003 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1004 allAppsBorderSpaceTwoPanelPortrait);
1005 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1006
1007 x = a.getFloat(
1008 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1009 allAppsBorderSpaceTwoPanelLandscape);
1010 y = a.getFloat(
1011 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1012 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001013 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001014
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001015 iconSizes[INDEX_DEFAULT] =
1016 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1017 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001018 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001019 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001020 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001021 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001022 iconSizes[INDEX_DEFAULT]);
1023 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001024 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001025 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001026
Thales Limabb7d3882021-12-22 12:56:29 +00001027 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1028 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001029 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1030 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001031 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001032 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1033 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1034 allAppsIconSizes[INDEX_DEFAULT]);
1035 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1036 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1037 allAppsIconSizes[INDEX_DEFAULT]);
1038
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001039 textSizes[INDEX_DEFAULT] =
1040 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1041 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001042 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001043 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001044 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001045 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001046 textSizes[INDEX_DEFAULT]);
1047 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001048 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001049 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001050
Thales Limabb7d3882021-12-22 12:56:29 +00001051 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1052 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1053 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1054 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1055 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1056 allAppsIconTextSizes[INDEX_DEFAULT]);
1057 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1058 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1059 allAppsIconTextSizes[INDEX_DEFAULT]);
1060
Thales Lima83bedbf2021-10-05 17:47:39 +01001061 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1062 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001063 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1064 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1065 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001066 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001067 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001068 horizontalMargin[INDEX_DEFAULT]);
1069 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001070 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001071 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001072
Thales Limab8c05952022-05-23 16:58:38 +01001073 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1074 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1075 ResourcesCompat.getFloat(context.getResources(),
1076 R.dimen.hotseat_bar_bottom_space_default));
1077 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1078 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1079 hotseatBarBottomSpace[INDEX_DEFAULT]);
1080 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1081 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1082 hotseatBarBottomSpace[INDEX_DEFAULT]);
1083 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1084 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1085 hotseatBarBottomSpace[INDEX_DEFAULT]);
1086
1087 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1088 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1089 ResourcesCompat.getFloat(context.getResources(),
1090 R.dimen.hotseat_qsb_space_default));
1091 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1092 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1093 hotseatQsbSpace[INDEX_DEFAULT]);
1094 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1095 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1096 hotseatQsbSpace[INDEX_DEFAULT]);
1097 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1098 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1099 hotseatQsbSpace[INDEX_DEFAULT]);
1100
Sunny Goyal415f1732018-11-29 10:33:47 -08001101 a.recycle();
1102 }
1103
1104 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001105 this(null);
1106 }
1107
1108 DisplayOption(GridOption grid) {
1109 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001110 minWidthDps = 0;
1111 minHeightDps = 0;
1112 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001113 for (int i = 0; i < COUNT_SIZES; i++) {
1114 iconSizes[i] = 0;
1115 textSizes[i] = 0;
1116 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001117 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001118 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001119 allAppsIconSizes[i] = 0;
1120 allAppsIconTextSizes[i] = 0;
1121 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001122 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001123 }
1124
1125 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001126 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001127 iconSizes[i] *= w;
1128 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001129 borderSpaces[i].x *= w;
1130 borderSpaces[i].y *= w;
1131 minCellSize[i].x *= w;
1132 minCellSize[i].y *= w;
1133 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001134 hotseatBarBottomSpace[i] *= w;
1135 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001136 allAppsCellSize[i].x *= w;
1137 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001138 allAppsIconSizes[i] *= w;
1139 allAppsIconTextSizes[i] *= w;
1140 allAppsBorderSpaces[i].x *= w;
1141 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001142 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001143
Sunny Goyal415f1732018-11-29 10:33:47 -08001144 return this;
1145 }
1146
1147 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001148 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001149 iconSizes[i] += p.iconSizes[i];
1150 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001151 borderSpaces[i].x += p.borderSpaces[i].x;
1152 borderSpaces[i].y += p.borderSpaces[i].y;
1153 minCellSize[i].x += p.minCellSize[i].x;
1154 minCellSize[i].y += p.minCellSize[i].y;
1155 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001156 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1157 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001158 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1159 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001160 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1161 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1162 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1163 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001164 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001165
Sunny Goyal415f1732018-11-29 10:33:47 -08001166 return this;
1167 }
1168 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001169}