blob: 3d7d14f1abc97df171d29344034dc18fa0f6beaa [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) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700311 return Utilities.isGridOptionsEnabled(context)
312 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700313 }
314
Sunny Goyaleff44f32019-01-09 17:29:49 -0800315 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700316 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000317 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700318
319 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000320 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100321 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700322 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000323 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
324 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700325 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800326 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700327
Thales Limab67bfa72022-11-02 15:30:11 +0000328 @VisibleForTesting
329 public static String getDefaultGridName(Context context) {
330 return new InvariantDeviceProfile().initGrid(context, null);
331 }
332
Alex Chau1c883d82021-12-01 18:43:10 +0000333 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
334 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700335 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700336 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000337 numRows = closestProfile.numRows;
338 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000339 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000340 dbFile = closestProfile.dbFile;
341 defaultLayoutId = closestProfile.defaultLayoutId;
342 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300343
Sunny Goyalae190ff2020-04-14 00:19:01 +0000344 numFolderRows = closestProfile.numFolderRows;
345 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300346 folderStyle = closestProfile.folderStyle;
347
Jon Mirandae126d722021-02-25 10:45:20 -0500348 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400349 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000350 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000351
Vinit Nayakc7293172022-07-18 16:41:50 -0700352 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
353
Sunny Goyalae190ff2020-04-14 00:19:01 +0000354 mExtraAttrs = closestProfile.extraAttrs;
355
Thales Lima83bedbf2021-10-05 17:47:39 +0100356 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100357 float maxIconSize = iconSize[0];
358 for (int i = 1; i < iconSize.length; i++) {
359 maxIconSize = Math.max(maxIconSize, iconSize[i]);
360 }
361 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000362 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
363
Thales Lima83bedbf2021-10-05 17:47:39 +0100364 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700365
Thales Lima83bedbf2021-10-05 17:47:39 +0100366 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100367
Thales Lima83bedbf2021-10-05 17:47:39 +0100368 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100369
Thales Limad90faab2021-09-21 17:18:47 +0100370 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100371
Sunny Goyal19ff7282021-04-22 10:12:54 -0700372 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000373 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700374 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300375 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100376 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
377 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700378
379 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000380 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700381 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500382
Thales Limab7ef5692022-03-10 10:32:36 +0000383 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000384 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
385 allAppsIconSize = displayOption.allAppsIconSizes;
386 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100387 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000388 allAppsIconSize = iconSize;
389 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000390 }
391
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400392 if (devicePaddingId != 0) {
393 devicePaddings = new DevicePaddings(context, devicePaddingId);
394 }
395
Thales Lima11af7bc2022-08-12 15:24:54 +0100396 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000397
Sunny Goyalae190ff2020-04-14 00:19:01 +0000398 // If the partner customization apk contains any grid overrides, apply them
399 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700400 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700401
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000402 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700403 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700404 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700405 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000406 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000407 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000408 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700409 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000410 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700411
Sunny Goyal19ff7282021-04-22 10:12:54 -0700412 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
413 int displayWidth = bounds.bounds.width();
414 int displayHeight = bounds.bounds.height();
415 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700416
Sunny Goyal19ff7282021-04-22 10:12:54 -0700417 // We need to ensure that there is enough extra space in the wallpaper
418 // for the intended parallax effects
419 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300420 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
421 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700422 ? 2
423 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
424 defaultWallpaperSize.x =
425 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700426 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000427 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700428
429 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
430 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700431 }
432
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700433 public void addOnChangeListener(OnIDPChangeListener listener) {
434 mChangeListeners.add(listener);
435 }
436
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800437 public void removeOnChangeListener(OnIDPChangeListener listener) {
438 mChangeListeners.remove(listener);
439 }
440
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800441
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800442 public void setCurrentGrid(Context context, String gridName) {
443 Context appContext = context.getApplicationContext();
444 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700445 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800446 }
447
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700448 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700449 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000450 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
451 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700452 }
453
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700454 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700455 Object[] oldState = toModelState();
456
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700457 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700458 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700459 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700460
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700461 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700462 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700463 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700464 }
465 }
466
Alex Chau1c883d82021-12-01 18:43:10 +0000467 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
468 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800469 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100470
Alex Chau1c883d82021-12-01 18:43:10 +0000471 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700472 final int depth = parser.getDepth();
473 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700474 while (((type = parser.next()) != XmlPullParser.END_TAG ||
475 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800476 if ((type == XmlPullParser.START_TAG)
477 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800478
Alex Chau1c883d82021-12-01 18:43:10 +0000479 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
480 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100481 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100482 final int displayDepth = parser.getDepth();
483 while (((type = parser.next()) != XmlPullParser.END_TAG
484 || parser.getDepth() > displayDepth)
485 && type != XmlPullParser.END_DOCUMENT) {
486 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
487 parser.getName())) {
488 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100489 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100490 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800491 }
492 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700493 }
494 }
sfufa@google.comde013292021-09-21 18:22:44 -0700495 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700496 throw new RuntimeException(e);
497 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800498
Sunny Goyalae190ff2020-04-14 00:19:01 +0000499 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800500 if (!TextUtils.isEmpty(gridName)) {
501 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100502 if (gridName.equals(option.grid.name)
503 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000504 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800505 }
506 }
507 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000508 if (filteredProfiles.isEmpty()) {
509 // No grid found, use the default options
510 for (DisplayOption option : profiles) {
511 if (option.canBeDefault) {
512 filteredProfiles.add(option);
513 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800514 }
515 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000516 if (filteredProfiles.isEmpty()) {
517 throw new RuntimeException("No display option with canBeDefault=true");
518 }
519 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700520 }
521
Thales Lima7ec83822021-08-05 13:32:10 +0100522 /**
523 * @return all the grid options that can be shown on the device
524 */
525 public List<GridOption> parseAllGridOptions(Context context) {
526 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100527
Alex Chau1c883d82021-12-01 18:43:10 +0000528 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100529 final int depth = parser.getDepth();
530 int type;
531 while (((type = parser.next()) != XmlPullParser.END_TAG
532 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
533 if ((type == XmlPullParser.START_TAG)
534 && GridOption.TAG_NAME.equals(parser.getName())) {
535 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000536 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100537 if (option.isEnabled) {
538 result.add(option);
539 }
540 }
541 }
542 } catch (IOException | XmlPullParserException e) {
543 Log.e(TAG, "Error parsing device profile", e);
544 return Collections.emptyList();
545 }
546 return result;
547 }
548
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700549 private int getLauncherIconDensity(int requiredSize) {
550 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700551 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700552 DisplayMetrics.DENSITY_LOW,
553 DisplayMetrics.DENSITY_MEDIUM,
554 DisplayMetrics.DENSITY_TV,
555 DisplayMetrics.DENSITY_HIGH,
556 DisplayMetrics.DENSITY_XHIGH,
557 DisplayMetrics.DENSITY_XXHIGH,
558 DisplayMetrics.DENSITY_XXXHIGH
559 };
560
561 int density = DisplayMetrics.DENSITY_XXXHIGH;
562 for (int i = densityBuckets.length - 1; i >= 0; i--) {
563 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
564 / DisplayMetrics.DENSITY_DEFAULT;
565 if (expectedSize >= requiredSize) {
566 density = densityBuckets[i];
567 }
568 }
569
570 return density;
571 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700572
Adam Cohen2e6da152015-05-06 11:42:25 -0700573 /**
574 * Apply any Partner customization grid overrides.
575 *
576 * Currently we support: all apps row / column count.
577 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700578 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
579 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700580 if (p != null) {
581 p.applyInvariantDeviceProfileOverrides(this, dm);
582 }
583 }
584
Sunny Goyal415f1732018-11-29 10:33:47 -0800585 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700586 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700587 }
588
Sunny Goyal19ff7282021-04-22 10:12:54 -0700589 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000590 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700591 int minWidthPx = Integer.MAX_VALUE;
592 int minHeightPx = Integer.MAX_VALUE;
593 for (WindowBounds bounds : displayInfo.supportedBounds) {
594 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000595 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700596 // For split displays, take half width per page
597 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
598 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700599
Sunny Goyal19ff7282021-04-22 10:12:54 -0700600 } else if (!isTablet && bounds.isLandscape()) {
601 // We will use transposed layout in this case
602 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
603 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
604 } else {
605 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
606 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
607 }
608 }
609
Thales Lima425f6822022-05-10 13:44:58 -0300610 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
611 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700612
613 // Sort the profiles based on the closeness to the device size
614 Collections.sort(points, (a, b) ->
615 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
616 dist(width, height, b.minWidthDps, b.minHeightDps)));
617
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700618 DisplayOption closestPoint = points.get(0);
619 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700620 float weights = 0;
621
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700622 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
623 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700624 }
625
626 DisplayOption out = new DisplayOption(closestOption);
627 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700628 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700629 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
630 weights += w;
631 out.add(new DisplayOption().add(p).multiply(w));
632 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700633 out.multiply(1.0f / weights);
634
Thales Lima6e0005a2021-10-27 15:53:41 +0100635 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700636 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100637 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
638 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700639 }
640
641 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700642 }
643
Sunny Goyal27835952017-01-13 12:15:53 -0800644 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700645 Resources res = context.getResources();
646 Configuration config = context.getResources().getConfiguration();
647
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400648 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
649 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100650 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
651
Alex Chau24265142022-11-14 21:19:44 +0000652 if (Utilities.IS_DEBUG_DEVICE && ENABLE_DEVICE_PROFILE_LOGGING.get()) {
Alex Chau5dfd9392022-11-02 17:39:40 +0000653 StringWriter stringWriter = new StringWriter();
654 PrintWriter printWriter = new PrintWriter(stringWriter);
655 DisplayController.INSTANCE.get(context).dump(printWriter);
656 printWriter.flush();
657 Log.d("b/253338238", "getDeviceProfile -"
658 + "\nconfig: " + config
659 + "\ndisplayMetrics: " + res.getDisplayMetrics()
660 + "\nrotation: " + rotation
661 + "\n" + stringWriter,
662 new Exception());
663 }
Alex Chaud3e8cc42022-05-03 17:45:34 +0100664 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400665 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700666
Sunny Goyal187b16c2022-03-01 16:53:23 -0800667 /**
668 * Returns the device profile matching the provided screen configuration
669 */
670 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700671 DeviceProfile bestMatch = supportedProfiles.get(0);
672 float minDiff = Float.MAX_VALUE;
673
674 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400675 float diff = Math.abs(profile.widthPx - screenWidth)
676 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700677 if (diff < minDiff) {
678 minDiff = diff;
679 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800680 } else if (diff == minDiff && profile.rotationHint == rotation) {
681 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700682 }
683 }
684 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800685 }
686
Sunny Goyal415f1732018-11-29 10:33:47 -0800687 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700688 float d = dist(x0, y0, x1, y1);
689 if (Float.compare(d, 0f) == 0) {
690 return Float.POSITIVE_INFINITY;
691 }
692 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
693 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700694
695 /**
696 * As a ratio of screen height, the total distance we want the parallax effect to span
697 * horizontally
698 */
699 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
700 float aspectRatio = width / (float) height;
701
702 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
703 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
704 // We will use these two data points to extrapolate how much the wallpaper parallax effect
705 // to span (ie travel) at any aspect ratio:
706
sfufa@google.comde013292021-09-21 18:22:44 -0700707 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
708 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700709 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
710 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
711
712 // To find out the desired width at different aspect ratios, we use the following two
713 // formulas, where the coefficient on x is the aspect ratio (width/height):
714 // (16/10)x + y = 1.5
715 // (10/16)x + y = 1.2
716 // We solve for x and y and end up with a final formula:
717 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700718 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
719 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700720 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
721 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
722 return x * aspectRatio + y;
723 }
724
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700725 public interface OnIDPChangeListener {
726
Sunny Goyalb47172b2021-05-03 19:59:51 -0700727 /**
728 * Called when the device provide changes
729 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700730 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700731 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800732
733
Sunny Goyaleff44f32019-01-09 17:29:49 -0800734 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800735
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800736 public static final String TAG_NAME = "grid-option";
737
Alex Chau1c883d82021-12-01 18:43:10 +0000738 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
739 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
740 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
741 private static final int DEVICE_CATEGORY_ALL =
742 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
743
Thales Lima11af7bc2022-08-12 15:24:54 +0100744 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
745 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
746 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
747 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
748 private static final int DONT_INLINE_QSB = 0;
749
Sunny Goyaleff44f32019-01-09 17:29:49 -0800750 public final String name;
751 public final int numRows;
752 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000753 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100754 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800755
756 private final int numFolderRows;
757 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300758 private final @StyleRes int folderStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800759
Sunny Goyal19ff7282021-04-22 10:12:54 -0700760 private final int numAllAppsColumns;
761 private final int numDatabaseAllAppsColumns;
762 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700763 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100764
Thales Lima425f6822022-05-10 13:44:58 -0300765 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800766
Thales Lima11af7bc2022-08-12 15:24:54 +0100767 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
768
Thales Limab35faed2022-09-05 16:30:01 -0300769 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800770 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000771
Sunny Goyal415f1732018-11-29 10:33:47 -0800772 private final int defaultLayoutId;
773 private final int demoModeLayoutId;
774
Jon Mirandae126d722021-02-25 10:45:20 -0500775 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400776 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500777
Sunny Goyal5bc18462019-01-07 15:13:39 -0800778 private final SparseArray<TypedValue> extraAttrs;
779
Alex Chau1c883d82021-12-01 18:43:10 +0000780 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800781 TypedArray a = context.obtainStyledAttributes(
782 attrs, R.styleable.GridDisplayOption);
783 name = a.getString(R.styleable.GridDisplayOption_name);
784 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
785 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000786 numSearchContainerColumns = a.getInt(
787 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800788
Tracy Zhou7df93d22020-01-27 13:44:06 -0800789 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000790 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100791 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
792 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
793 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800794 demoModeLayoutId = a.getResourceId(
795 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700796
797 numAllAppsColumns = a.getInt(
798 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
799 numDatabaseAllAppsColumns = a.getInt(
800 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
801
802 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800803 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700804 numDatabaseHotseatIcons = a.getInt(
805 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100806
Thales Lima425f6822022-05-10 13:44:58 -0300807 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
808 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
809 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
810 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
811 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
812 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
813 numColumns);
814 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
815 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
816 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700817
Vinit Nayakc7293172022-07-18 16:41:50 -0700818 inlineNavButtonsEndSpacing =
819 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
820 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300821
Sunny Goyal415f1732018-11-29 10:33:47 -0800822 numFolderRows = a.getInt(
823 R.styleable.GridDisplayOption_numFolderRows, numRows);
824 numFolderColumns = a.getInt(
825 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700826
Thales Limab35faed2022-09-05 16:30:01 -0300827 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
828 INVALID_RESOURCE_HANDLE);
829
Jon Mirandae126d722021-02-25 10:45:20 -0500830 isScalable = a.getBoolean(
831 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400832 devicePaddingId = a.getResourceId(
833 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500834
Alex Chau1c883d82021-12-01 18:43:10 +0000835 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
836 DEVICE_CATEGORY_ALL);
837 isEnabled = (deviceType == TYPE_PHONE
838 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
839 || (deviceType == TYPE_TABLET
840 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
841 || (deviceType == TYPE_MULTI_DISPLAY
842 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
843 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100844
Thales Lima11af7bc2022-08-12 15:24:54 +0100845 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
846 DONT_INLINE_QSB);
847 inlineQsb[INDEX_DEFAULT] =
848 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
849 inlineQsb[INDEX_LANDSCAPE] =
850 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
851 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
852 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
853 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
854 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
855 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
856 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
857
Sunny Goyal415f1732018-11-29 10:33:47 -0800858 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800859 extraAttrs = Themes.createValueMap(context, attrs,
860 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800861 }
862 }
863
Sunny Goyal19ff7282021-04-22 10:12:54 -0700864 @VisibleForTesting
865 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700866 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800867
Sunny Goyal415f1732018-11-29 10:33:47 -0800868 private final float minWidthDps;
869 private final float minHeightDps;
870 private final boolean canBeDefault;
871
Thales Lima83bedbf2021-10-05 17:47:39 +0100872 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100873
Thales Lima78d00ad2021-09-30 11:29:06 +0100874 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100875 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100876 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
877 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100878
Thales Lima78d00ad2021-09-30 11:29:06 +0100879 private final float[] iconSizes = new float[COUNT_SIZES];
880 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800881
Thales Limab7ef5692022-03-10 10:32:36 +0000882 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000883 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
884 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
885 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
886
Thales Lima1de4d552021-10-13 16:13:25 +0100887 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800888 this.grid = grid;
889
Thales Lima1de4d552021-10-13 16:13:25 +0100890 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800891
Sunny Goyal415f1732018-11-29 10:33:47 -0800892 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
893 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700894
Thales Lima1de4d552021-10-13 16:13:25 +0100895 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800896
Thales Lima83bedbf2021-10-05 17:47:39 +0100897 float x;
898 float y;
899
Thales Lima85c942f2021-12-31 13:04:20 +0000900 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
901 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100902 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000903
904 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
905 minCellSize[INDEX_DEFAULT].x);
906 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
907 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100908 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100909
Thales Lima85c942f2021-12-31 13:04:20 +0000910 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100911 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000912 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100913 minCellSize[INDEX_DEFAULT].y);
914 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
915
Thales Lima85c942f2021-12-31 13:04:20 +0000916 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100917 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000918 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100919 minCellSize[INDEX_DEFAULT].y);
920 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100921
Thales Lima85c942f2021-12-31 13:04:20 +0000922 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000923 float borderSpaceLandscape = a.getFloat(
924 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000925 float borderSpaceTwoPanelPortrait = a.getFloat(
926 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
927 float borderSpaceTwoPanelLandscape = a.getFloat(
928 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100929
Thales Lima85c942f2021-12-31 13:04:20 +0000930 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
931 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100932 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000933
934 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
935 borderSpaceLandscape);
936 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
937 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100938 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
939
940 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000941 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
942 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100943 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000944 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
945 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100946 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
947
948 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000949 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
950 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100951 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000952 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
953 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100954 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
955
Thales Limab7ef5692022-03-10 10:32:36 +0000956 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
957 minCellSize[INDEX_DEFAULT].x);
958 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
959 minCellSize[INDEX_DEFAULT].y);
960 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
961
962 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
963 allAppsCellSize[INDEX_DEFAULT].x);
964 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
965 allAppsCellSize[INDEX_DEFAULT].y);
966 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
967
968 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
969 allAppsCellSize[INDEX_DEFAULT].x);
970 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
971 allAppsCellSize[INDEX_DEFAULT].y);
972 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
973
974 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
975 allAppsCellSize[INDEX_DEFAULT].x);
976 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
977 allAppsCellSize[INDEX_DEFAULT].y);
978 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
979
Thales Lima080d8902022-03-28 15:30:29 +0100980 float allAppsBorderSpace = a.getFloat(
981 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
982 float allAppsBorderSpaceLandscape = a.getFloat(
983 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
984 allAppsBorderSpace);
985 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000986 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100987 allAppsBorderSpace);
988 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000989 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100990 allAppsBorderSpace);
991
992 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
993 allAppsBorderSpace);
994 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
995 allAppsBorderSpace);
996 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
997
998 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
999 allAppsBorderSpaceLandscape);
1000 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1001 allAppsBorderSpaceLandscape);
1002 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1003
1004 x = a.getFloat(
1005 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1006 allAppsBorderSpaceTwoPanelPortrait);
1007 y = a.getFloat(
1008 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1009 allAppsBorderSpaceTwoPanelPortrait);
1010 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1011
1012 x = a.getFloat(
1013 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1014 allAppsBorderSpaceTwoPanelLandscape);
1015 y = a.getFloat(
1016 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1017 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001018 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001019
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001020 iconSizes[INDEX_DEFAULT] =
1021 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1022 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001023 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001024 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001025 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001026 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001027 iconSizes[INDEX_DEFAULT]);
1028 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001029 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001030 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001031
Thales Limabb7d3882021-12-22 12:56:29 +00001032 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1033 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001034 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1035 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001036 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001037 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1038 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1039 allAppsIconSizes[INDEX_DEFAULT]);
1040 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1041 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1042 allAppsIconSizes[INDEX_DEFAULT]);
1043
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001044 textSizes[INDEX_DEFAULT] =
1045 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1046 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001047 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001048 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001049 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001050 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001051 textSizes[INDEX_DEFAULT]);
1052 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001053 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001054 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001055
Thales Limabb7d3882021-12-22 12:56:29 +00001056 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1057 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1058 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1059 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1060 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1061 allAppsIconTextSizes[INDEX_DEFAULT]);
1062 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1063 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1064 allAppsIconTextSizes[INDEX_DEFAULT]);
1065
Thales Lima83bedbf2021-10-05 17:47:39 +01001066 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1067 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001068 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1069 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1070 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001071 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001072 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001073 horizontalMargin[INDEX_DEFAULT]);
1074 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001075 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001076 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001077
Thales Limab8c05952022-05-23 16:58:38 +01001078 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1079 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1080 ResourcesCompat.getFloat(context.getResources(),
1081 R.dimen.hotseat_bar_bottom_space_default));
1082 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1083 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1084 hotseatBarBottomSpace[INDEX_DEFAULT]);
1085 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1086 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1087 hotseatBarBottomSpace[INDEX_DEFAULT]);
1088 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1089 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1090 hotseatBarBottomSpace[INDEX_DEFAULT]);
1091
1092 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1093 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1094 ResourcesCompat.getFloat(context.getResources(),
1095 R.dimen.hotseat_qsb_space_default));
1096 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1097 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1098 hotseatQsbSpace[INDEX_DEFAULT]);
1099 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1100 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1101 hotseatQsbSpace[INDEX_DEFAULT]);
1102 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1103 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1104 hotseatQsbSpace[INDEX_DEFAULT]);
1105
Sunny Goyal415f1732018-11-29 10:33:47 -08001106 a.recycle();
1107 }
1108
1109 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001110 this(null);
1111 }
1112
1113 DisplayOption(GridOption grid) {
1114 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001115 minWidthDps = 0;
1116 minHeightDps = 0;
1117 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001118 for (int i = 0; i < COUNT_SIZES; i++) {
1119 iconSizes[i] = 0;
1120 textSizes[i] = 0;
1121 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001122 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001123 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001124 allAppsIconSizes[i] = 0;
1125 allAppsIconTextSizes[i] = 0;
1126 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001127 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001128 }
1129
1130 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001131 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001132 iconSizes[i] *= w;
1133 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001134 borderSpaces[i].x *= w;
1135 borderSpaces[i].y *= w;
1136 minCellSize[i].x *= w;
1137 minCellSize[i].y *= w;
1138 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001139 hotseatBarBottomSpace[i] *= w;
1140 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001141 allAppsCellSize[i].x *= w;
1142 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001143 allAppsIconSizes[i] *= w;
1144 allAppsIconTextSizes[i] *= w;
1145 allAppsBorderSpaces[i].x *= w;
1146 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001147 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001148
Sunny Goyal415f1732018-11-29 10:33:47 -08001149 return this;
1150 }
1151
1152 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001153 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001154 iconSizes[i] += p.iconSizes[i];
1155 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001156 borderSpaces[i].x += p.borderSpaces[i].x;
1157 borderSpaces[i].y += p.borderSpaces[i].y;
1158 minCellSize[i].x += p.minCellSize[i].x;
1159 minCellSize[i].y += p.minCellSize[i].y;
1160 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001161 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1162 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001163 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1164 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001165 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1166 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1167 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1168 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001169 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001170
Sunny Goyal415f1732018-11-29 10:33:47 -08001171 return this;
1172 }
1173 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001174}