blob: 594d7cbd8286e0896969b583645914bc21eb6b88 [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;
Thales Limab35faed2022-09-05 16:30:01 -030050import androidx.annotation.StyleRes;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070051import androidx.annotation.VisibleForTesting;
Thales Limaae0d7ef2022-11-16 15:53:43 +000052import androidx.annotation.XmlRes;
Thales Limab8c05952022-05-23 16:58:38 +010053import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070054
Sunny Goyal65190ae2022-08-02 14:16:26 -070055import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070056import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010057import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070058import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070059import com.android.launcher3.util.DisplayController;
60import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080061import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070062import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal3e58eea2022-11-14 14:30:07 -080063import com.android.launcher3.util.Partner;
Sunny Goyal5bc18462019-01-07 15:13:39 -080064import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070065import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080066import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070067
Sunny Goyal819e1932016-07-07 16:43:58 -070068import org.xmlpull.v1.XmlPullParser;
69import org.xmlpull.v1.XmlPullParserException;
70
71import java.io.IOException;
Alex Chau5dfd9392022-11-02 17:39:40 +000072import java.io.PrintWriter;
73import java.io.StringWriter;
Alex Chau1c883d82021-12-01 18:43:10 +000074import java.lang.annotation.Retention;
75import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070076import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070077import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080078import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070079import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070080
81public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070082
Hyunyoung Songc55a3502018-12-04 15:43:16 -080083 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070084 // We do not need any synchronization for this variable as its only written on UI thread.
85 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070086 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070087
Alex Chau1c883d82021-12-01 18:43:10 +000088 @Retention(RetentionPolicy.SOURCE)
89 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010090 public @interface DeviceType {}
91
Alex Chau1c883d82021-12-01 18:43:10 +000092 public static final int TYPE_PHONE = 0;
93 public static final int TYPE_MULTI_DISPLAY = 1;
94 public static final int TYPE_TABLET = 2;
95
Hyunyoung Songc55a3502018-12-04 15:43:16 -080096 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080097
Sunny Goyal53d7ee42015-05-22 12:25:45 -070098 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
99
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700100 // Constants that affects the interpolation curve between statically defined device profile
101 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800102 private static final float KNEARESTNEIGHBOR = 3;
103 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -0700104
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700105 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800106 private static final float WEIGHT_EFFICIENT = 100000f;
107
Thales Lima83bedbf2021-10-05 17:47:39 +0100108 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
109 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000110 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100111 static final int INDEX_DEFAULT = 0;
112 static final int INDEX_LANDSCAPE = 1;
113 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
114 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100115
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800116 /** These resources are used to override the device profile */
117 private static final String RES_GRID_NUM_ROWS = "grid_num_rows";
118 private static final String RES_GRID_NUM_COLUMNS = "grid_num_columns";
119 private static final String RES_GRID_ICON_SIZE_DP = "grid_icon_size_dp";
120
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700121 /**
122 * Number of icons per row and column in the workspace.
123 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700124 public int numRows;
125 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000126 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700127
128 /**
129 * Number of icons per row and column in the folder.
130 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700131 public int numFolderRows;
132 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100133 public float[] iconSize;
134 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700135 public int iconBitmapSize;
136 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000137 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700138
Thales Lima83bedbf2021-10-05 17:47:39 +0100139 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100140
Thales Lima83bedbf2021-10-05 17:47:39 +0100141 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300142 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100143
Thales Limab35faed2022-09-05 16:30:01 -0300144 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100145
Thales Lima83bedbf2021-10-05 17:47:39 +0100146 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500147
Thales Limab7ef5692022-03-10 10:32:36 +0000148 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000149 public float[] allAppsIconSize;
150 public float[] allAppsIconTextSize;
151 public PointF[] allAppsBorderSpaces;
152
Sunny Goyal5bc18462019-01-07 15:13:39 -0800153 private SparseArray<TypedValue> mExtraAttrs;
154
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700155 /**
156 * Number of icons inside the hotseat area.
157 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800158 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700159
160 /**
161 * Number of icons inside the hotseat area that is stored in the database. This is greater than
162 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
163 * sizes that share the same DB.
164 */
165 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700166
Thales Lima425f6822022-05-10 13:44:58 -0300167 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100168 public float[] hotseatBarBottomSpace;
169 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300170
Jon Miranda6f7e9702019-09-16 14:44:14 -0700171 /**
172 * Number of columns in the all apps list.
173 */
174 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700175 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700176
Jon Mirandae126d722021-02-25 10:45:20 -0500177 /**
178 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
179 */
180 protected boolean isScalable;
Thales Limaae0d7ef2022-11-16 15:53:43 +0000181 @XmlRes
182 public int devicePaddingId = INVALID_RESOURCE_HANDLE;
Jon Mirandae126d722021-02-25 10:45:20 -0500183
Tracy Zhou7df93d22020-01-27 13:44:06 -0800184 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800185 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700186 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100187 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700188
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000189 /**
190 * An immutable list of supported profiles.
191 */
192 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700193
Sunny Goyal6f866092016-03-17 17:04:15 -0700194 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700195 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700196
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700197 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700198
Sunny Goyalf633ef52018-03-13 09:57:05 -0700199 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800200 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700201
Sunny Goyalbbf01842015-10-08 07:41:15 -0700202 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700203 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700204 String gridName = getCurrentGridName(context);
205 String newGridName = initGrid(context, gridName);
206 if (!newGridName.equals(gridName)) {
Stefan Andonian146701c2022-11-10 23:07:40 +0000207 LauncherPrefs.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName)
208 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700209 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700210 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700211
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700212 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100213 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000214 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
215 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100216 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700217 }
218 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700219 }
220
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700221 /**
222 * This constructor should NOT have any monitors by design.
223 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800224 public InvariantDeviceProfile(Context context, String gridName) {
225 String newName = initGrid(context, gridName);
226 if (newName == null || !newName.equals(gridName)) {
227 throw new IllegalArgumentException("Unknown grid name");
228 }
229 }
230
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700231 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800232 * This constructor should NOT have any monitors by design.
233 */
234 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100236 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700237 String gridName = getCurrentGridName(context);
238
239 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000240 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
241 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700242 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000243 defaultInfo,
244 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
245 /*allowDisabledGrid=*/false),
246 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700247
Alex Chau661f02d2022-06-07 14:03:43 +0100248 Context displayContext = context.createDisplayContext(display);
249 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000250 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700251 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000252 myInfo,
253 getPredefinedDeviceProfiles(context, gridName, deviceType,
254 /*allowDisabledGrid=*/false),
255 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700256
257 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
258 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100259 result.iconSizes[INDEX_DEFAULT] =
260 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
261 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700262 result.iconSizes[i] = Math.min(
263 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000264 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700265
Thales Lima83bedbf2021-10-05 17:47:39 +0100266 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
267 COUNT_SIZES);
268 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
269 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500270
Alex Chau1c883d82021-12-01 18:43:10 +0000271 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800272 }
273
Alex Chau238aaee2021-10-06 16:15:24 +0100274 /**
275 * Reinitialize the current grid after a restore, where some grids might now be disabled.
276 */
277 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000278 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100279 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000280 String newGridName = initGrid(context, currentGridName);
281 String newDbFile = dbFile;
282 if (!newDbFile.equals(currentDbFile)) {
283 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100284 + ", migrating to: " + newGridName
285 + ", removing all other grid db files");
286 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
287 if (gridDbFile.equals(currentDbFile)) {
288 continue;
289 }
290 if (context.getDatabasePath(gridDbFile).delete()) {
291 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
292 }
293 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000294 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100295 }
296 }
297
Alex Chau1c883d82021-12-01 18:43:10 +0000298 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800299 int flagPhone = 1 << 0;
300 int flagTablet = 1 << 1;
301
302 int type = displayInfo.supportedBounds.stream()
303 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
304 .reduce(0, (a, b) -> a | b);
305 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
306 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000307 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800308 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000309 return TYPE_TABLET;
310 } else {
311 return TYPE_PHONE;
312 }
313 }
314
Tracy Zhou42255d22020-03-13 00:38:11 -0700315 public static String getCurrentGridName(Context context) {
Stefan Andonian146701c2022-11-10 23:07:40 +0000316 return LauncherPrefs.getPrefs(context).getString(KEY_IDP_GRID_NAME, null);
Tracy Zhou42255d22020-03-13 00:38:11 -0700317 }
318
Sunny Goyaleff44f32019-01-09 17:29:49 -0800319 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700320 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000321 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700322
323 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000324 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100325 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700326 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000327 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
328 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700329 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800330 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700331
Thales Limab67bfa72022-11-02 15:30:11 +0000332 @VisibleForTesting
333 public static String getDefaultGridName(Context context) {
334 return new InvariantDeviceProfile().initGrid(context, null);
335 }
336
Alex Chau1c883d82021-12-01 18:43:10 +0000337 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
338 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700339 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700340 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000341 numRows = closestProfile.numRows;
342 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000343 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000344 dbFile = closestProfile.dbFile;
345 defaultLayoutId = closestProfile.defaultLayoutId;
346 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300347
Sunny Goyalae190ff2020-04-14 00:19:01 +0000348 numFolderRows = closestProfile.numFolderRows;
349 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300350 folderStyle = closestProfile.folderStyle;
351
Jon Mirandae126d722021-02-25 10:45:20 -0500352 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400353 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000354 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000355
Vinit Nayakc7293172022-07-18 16:41:50 -0700356 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
357
Sunny Goyalae190ff2020-04-14 00:19:01 +0000358 mExtraAttrs = closestProfile.extraAttrs;
359
Thales Lima83bedbf2021-10-05 17:47:39 +0100360 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100361 float maxIconSize = iconSize[0];
362 for (int i = 1; i < iconSize.length; i++) {
363 maxIconSize = Math.max(maxIconSize, iconSize[i]);
364 }
365 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000366 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
367
Thales Lima83bedbf2021-10-05 17:47:39 +0100368 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700369
Thales Lima83bedbf2021-10-05 17:47:39 +0100370 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100371
Thales Lima83bedbf2021-10-05 17:47:39 +0100372 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100373
Thales Limad90faab2021-09-21 17:18:47 +0100374 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100375
Sunny Goyal19ff7282021-04-22 10:12:54 -0700376 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000377 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700378 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300379 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100380 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
381 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700382
383 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000384 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700385 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500386
Thales Limab7ef5692022-03-10 10:32:36 +0000387 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000388 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
389 allAppsIconSize = displayOption.allAppsIconSizes;
390 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000391
Thales Lima11af7bc2022-08-12 15:24:54 +0100392 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000393
Sunny Goyalae190ff2020-04-14 00:19:01 +0000394 // If the partner customization apk contains any grid overrides, apply them
395 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700396 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700397
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000398 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700399 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700400 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700401 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000402 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000403 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000404 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700405 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000406 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700407
Sunny Goyal19ff7282021-04-22 10:12:54 -0700408 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
409 int displayWidth = bounds.bounds.width();
410 int displayHeight = bounds.bounds.height();
411 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700412
Sunny Goyal19ff7282021-04-22 10:12:54 -0700413 // We need to ensure that there is enough extra space in the wallpaper
414 // for the intended parallax effects
415 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300416 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
417 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700418 ? 2
419 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
420 defaultWallpaperSize.x =
421 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700422 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000423 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700424
425 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
426 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700427 }
428
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700429 public void addOnChangeListener(OnIDPChangeListener listener) {
430 mChangeListeners.add(listener);
431 }
432
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800433 public void removeOnChangeListener(OnIDPChangeListener listener) {
434 mChangeListeners.remove(listener);
435 }
436
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800437
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800438 public void setCurrentGrid(Context context, String gridName) {
439 Context appContext = context.getApplicationContext();
Stefan Andonian146701c2022-11-10 23:07:40 +0000440 LauncherPrefs.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700441 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800442 }
443
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700444 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700445 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000446 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
447 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700448 }
449
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700450 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700451 Object[] oldState = toModelState();
452
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700453 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700454 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700455 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700456
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700457 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700458 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700459 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700460 }
461 }
462
Alex Chau1c883d82021-12-01 18:43:10 +0000463 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
464 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800465 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100466
Alex Chau1c883d82021-12-01 18:43:10 +0000467 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700468 final int depth = parser.getDepth();
469 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700470 while (((type = parser.next()) != XmlPullParser.END_TAG ||
471 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800472 if ((type == XmlPullParser.START_TAG)
473 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800474
Alex Chau1c883d82021-12-01 18:43:10 +0000475 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
476 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100477 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100478 final int displayDepth = parser.getDepth();
479 while (((type = parser.next()) != XmlPullParser.END_TAG
480 || parser.getDepth() > displayDepth)
481 && type != XmlPullParser.END_DOCUMENT) {
482 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
483 parser.getName())) {
484 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100485 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100486 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800487 }
488 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700489 }
490 }
sfufa@google.comde013292021-09-21 18:22:44 -0700491 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700492 throw new RuntimeException(e);
493 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800494
Sunny Goyalae190ff2020-04-14 00:19:01 +0000495 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800496 if (!TextUtils.isEmpty(gridName)) {
497 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100498 if (gridName.equals(option.grid.name)
499 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000500 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800501 }
502 }
503 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000504 if (filteredProfiles.isEmpty()) {
505 // No grid found, use the default options
506 for (DisplayOption option : profiles) {
507 if (option.canBeDefault) {
508 filteredProfiles.add(option);
509 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800510 }
511 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000512 if (filteredProfiles.isEmpty()) {
513 throw new RuntimeException("No display option with canBeDefault=true");
514 }
515 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700516 }
517
Thales Lima7ec83822021-08-05 13:32:10 +0100518 /**
519 * @return all the grid options that can be shown on the device
520 */
521 public List<GridOption> parseAllGridOptions(Context context) {
522 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100523
Alex Chau1c883d82021-12-01 18:43:10 +0000524 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100525 final int depth = parser.getDepth();
526 int type;
527 while (((type = parser.next()) != XmlPullParser.END_TAG
528 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
529 if ((type == XmlPullParser.START_TAG)
530 && GridOption.TAG_NAME.equals(parser.getName())) {
531 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000532 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100533 if (option.isEnabled) {
534 result.add(option);
535 }
536 }
537 }
538 } catch (IOException | XmlPullParserException e) {
539 Log.e(TAG, "Error parsing device profile", e);
540 return Collections.emptyList();
541 }
542 return result;
543 }
544
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700545 private int getLauncherIconDensity(int requiredSize) {
546 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700547 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700548 DisplayMetrics.DENSITY_LOW,
549 DisplayMetrics.DENSITY_MEDIUM,
550 DisplayMetrics.DENSITY_TV,
551 DisplayMetrics.DENSITY_HIGH,
552 DisplayMetrics.DENSITY_XHIGH,
553 DisplayMetrics.DENSITY_XXHIGH,
554 DisplayMetrics.DENSITY_XXXHIGH
555 };
556
557 int density = DisplayMetrics.DENSITY_XXXHIGH;
558 for (int i = densityBuckets.length - 1; i >= 0; i--) {
559 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
560 / DisplayMetrics.DENSITY_DEFAULT;
561 if (expectedSize >= requiredSize) {
562 density = densityBuckets[i];
563 }
564 }
565
566 return density;
567 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700568
Adam Cohen2e6da152015-05-06 11:42:25 -0700569 /**
570 * Apply any Partner customization grid overrides.
571 *
572 * Currently we support: all apps row / column count.
573 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700574 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
575 Partner p = Partner.get(context.getPackageManager());
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800576 if (p == null) {
577 return;
578 }
579 try {
580 int numRows = p.getIntValue(RES_GRID_NUM_ROWS, -1);
581 int numColumns = p.getIntValue(RES_GRID_NUM_COLUMNS, -1);
582 float iconSizePx = p.getDimenValue(RES_GRID_ICON_SIZE_DP, -1);
583
584 if (numRows > 0 && numColumns > 0) {
585 this.numRows = numRows;
586 this.numColumns = numColumns;
587 }
588 if (iconSizePx > 0) {
589 this.iconSize[InvariantDeviceProfile.INDEX_DEFAULT] =
590 Utilities.dpiFromPx(iconSizePx, dm.densityDpi);
591 }
592 } catch (Resources.NotFoundException ex) {
593 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700594 }
595 }
596
Sunny Goyal415f1732018-11-29 10:33:47 -0800597 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700598 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700599 }
600
Sunny Goyal19ff7282021-04-22 10:12:54 -0700601 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000602 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700603 int minWidthPx = Integer.MAX_VALUE;
604 int minHeightPx = Integer.MAX_VALUE;
605 for (WindowBounds bounds : displayInfo.supportedBounds) {
606 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000607 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700608 // For split displays, take half width per page
609 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
610 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700611
Sunny Goyal19ff7282021-04-22 10:12:54 -0700612 } else if (!isTablet && bounds.isLandscape()) {
613 // We will use transposed layout in this case
614 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
615 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
616 } else {
617 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
618 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
619 }
620 }
621
Thales Lima425f6822022-05-10 13:44:58 -0300622 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
623 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700624
625 // Sort the profiles based on the closeness to the device size
626 Collections.sort(points, (a, b) ->
627 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
628 dist(width, height, b.minWidthDps, b.minHeightDps)));
629
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700630 DisplayOption closestPoint = points.get(0);
631 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700632 float weights = 0;
633
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700634 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
635 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700636 }
637
638 DisplayOption out = new DisplayOption(closestOption);
639 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700640 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700641 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
642 weights += w;
643 out.add(new DisplayOption().add(p).multiply(w));
644 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700645 out.multiply(1.0f / weights);
646
Thales Lima6e0005a2021-10-27 15:53:41 +0100647 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700648 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100649 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
650 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700651 }
652
653 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700654 }
655
Sunny Goyal27835952017-01-13 12:15:53 -0800656 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700657 Resources res = context.getResources();
658 Configuration config = context.getResources().getConfiguration();
659
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400660 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
661 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100662 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
663
Alex Chau24265142022-11-14 21:19:44 +0000664 if (Utilities.IS_DEBUG_DEVICE && ENABLE_DEVICE_PROFILE_LOGGING.get()) {
Alex Chau5dfd9392022-11-02 17:39:40 +0000665 StringWriter stringWriter = new StringWriter();
666 PrintWriter printWriter = new PrintWriter(stringWriter);
667 DisplayController.INSTANCE.get(context).dump(printWriter);
668 printWriter.flush();
669 Log.d("b/253338238", "getDeviceProfile -"
670 + "\nconfig: " + config
671 + "\ndisplayMetrics: " + res.getDisplayMetrics()
672 + "\nrotation: " + rotation
673 + "\n" + stringWriter,
674 new Exception());
675 }
Alex Chaud3e8cc42022-05-03 17:45:34 +0100676 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400677 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700678
Sunny Goyal187b16c2022-03-01 16:53:23 -0800679 /**
680 * Returns the device profile matching the provided screen configuration
681 */
682 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700683 DeviceProfile bestMatch = supportedProfiles.get(0);
684 float minDiff = Float.MAX_VALUE;
685
686 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400687 float diff = Math.abs(profile.widthPx - screenWidth)
688 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700689 if (diff < minDiff) {
690 minDiff = diff;
691 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800692 } else if (diff == minDiff && profile.rotationHint == rotation) {
693 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700694 }
695 }
696 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800697 }
698
Sunny Goyal415f1732018-11-29 10:33:47 -0800699 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700700 float d = dist(x0, y0, x1, y1);
701 if (Float.compare(d, 0f) == 0) {
702 return Float.POSITIVE_INFINITY;
703 }
704 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
705 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700706
707 /**
708 * As a ratio of screen height, the total distance we want the parallax effect to span
709 * horizontally
710 */
711 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
712 float aspectRatio = width / (float) height;
713
714 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
715 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
716 // We will use these two data points to extrapolate how much the wallpaper parallax effect
717 // to span (ie travel) at any aspect ratio:
718
sfufa@google.comde013292021-09-21 18:22:44 -0700719 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
720 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700721 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
722 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
723
724 // To find out the desired width at different aspect ratios, we use the following two
725 // formulas, where the coefficient on x is the aspect ratio (width/height):
726 // (16/10)x + y = 1.5
727 // (10/16)x + y = 1.2
728 // We solve for x and y and end up with a final formula:
729 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700730 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
731 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700732 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
733 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
734 return x * aspectRatio + y;
735 }
736
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700737 public interface OnIDPChangeListener {
738
Sunny Goyalb47172b2021-05-03 19:59:51 -0700739 /**
740 * Called when the device provide changes
741 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700742 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700743 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800744
745
Sunny Goyaleff44f32019-01-09 17:29:49 -0800746 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800747
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800748 public static final String TAG_NAME = "grid-option";
749
Alex Chau1c883d82021-12-01 18:43:10 +0000750 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
751 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
752 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
753 private static final int DEVICE_CATEGORY_ALL =
754 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
755
Thales Lima11af7bc2022-08-12 15:24:54 +0100756 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
757 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
758 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
759 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
760 private static final int DONT_INLINE_QSB = 0;
761
Sunny Goyaleff44f32019-01-09 17:29:49 -0800762 public final String name;
763 public final int numRows;
764 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000765 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100766 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800767
768 private final int numFolderRows;
769 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300770 private final @StyleRes int folderStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800771
Sunny Goyal19ff7282021-04-22 10:12:54 -0700772 private final int numAllAppsColumns;
773 private final int numDatabaseAllAppsColumns;
774 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700775 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100776
Thales Lima425f6822022-05-10 13:44:58 -0300777 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800778
Thales Lima11af7bc2022-08-12 15:24:54 +0100779 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
780
Thales Limab35faed2022-09-05 16:30:01 -0300781 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800782 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000783
Sunny Goyal415f1732018-11-29 10:33:47 -0800784 private final int defaultLayoutId;
785 private final int demoModeLayoutId;
786
Jon Mirandae126d722021-02-25 10:45:20 -0500787 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400788 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500789
Sunny Goyal5bc18462019-01-07 15:13:39 -0800790 private final SparseArray<TypedValue> extraAttrs;
791
Alex Chau1c883d82021-12-01 18:43:10 +0000792 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800793 TypedArray a = context.obtainStyledAttributes(
794 attrs, R.styleable.GridDisplayOption);
795 name = a.getString(R.styleable.GridDisplayOption_name);
796 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
797 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000798 numSearchContainerColumns = a.getInt(
799 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800800
Tracy Zhou7df93d22020-01-27 13:44:06 -0800801 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000802 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100803 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
804 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
805 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800806 demoModeLayoutId = a.getResourceId(
807 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700808
809 numAllAppsColumns = a.getInt(
810 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
811 numDatabaseAllAppsColumns = a.getInt(
812 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
813
814 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800815 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700816 numDatabaseHotseatIcons = a.getInt(
817 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100818
Thales Lima425f6822022-05-10 13:44:58 -0300819 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
820 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
821 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
822 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
823 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
824 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
825 numColumns);
826 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
827 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
828 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700829
Vinit Nayakc7293172022-07-18 16:41:50 -0700830 inlineNavButtonsEndSpacing =
831 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
832 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300833
Sunny Goyal415f1732018-11-29 10:33:47 -0800834 numFolderRows = a.getInt(
835 R.styleable.GridDisplayOption_numFolderRows, numRows);
836 numFolderColumns = a.getInt(
837 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700838
Thales Limab35faed2022-09-05 16:30:01 -0300839 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
840 INVALID_RESOURCE_HANDLE);
841
Jon Mirandae126d722021-02-25 10:45:20 -0500842 isScalable = a.getBoolean(
843 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400844 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000845 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Jon Mirandae126d722021-02-25 10:45:20 -0500846
Alex Chau1c883d82021-12-01 18:43:10 +0000847 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
848 DEVICE_CATEGORY_ALL);
849 isEnabled = (deviceType == TYPE_PHONE
850 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
851 || (deviceType == TYPE_TABLET
852 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
853 || (deviceType == TYPE_MULTI_DISPLAY
854 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
855 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100856
Thales Lima11af7bc2022-08-12 15:24:54 +0100857 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
858 DONT_INLINE_QSB);
859 inlineQsb[INDEX_DEFAULT] =
860 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
861 inlineQsb[INDEX_LANDSCAPE] =
862 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
863 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
864 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
865 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
866 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
867 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
868 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
869
Sunny Goyal415f1732018-11-29 10:33:47 -0800870 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800871 extraAttrs = Themes.createValueMap(context, attrs,
872 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800873 }
874 }
875
Sunny Goyal19ff7282021-04-22 10:12:54 -0700876 @VisibleForTesting
877 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700878 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800879
Sunny Goyal415f1732018-11-29 10:33:47 -0800880 private final float minWidthDps;
881 private final float minHeightDps;
882 private final boolean canBeDefault;
883
Thales Lima83bedbf2021-10-05 17:47:39 +0100884 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100885
Thales Lima78d00ad2021-09-30 11:29:06 +0100886 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100887 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100888 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
889 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100890
Thales Lima78d00ad2021-09-30 11:29:06 +0100891 private final float[] iconSizes = new float[COUNT_SIZES];
892 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800893
Thales Limab7ef5692022-03-10 10:32:36 +0000894 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000895 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
896 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
897 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
898
Thales Lima1de4d552021-10-13 16:13:25 +0100899 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800900 this.grid = grid;
901
Thales Lima1de4d552021-10-13 16:13:25 +0100902 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800903
Sunny Goyal415f1732018-11-29 10:33:47 -0800904 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
905 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700906
Thales Lima1de4d552021-10-13 16:13:25 +0100907 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800908
Thales Lima83bedbf2021-10-05 17:47:39 +0100909 float x;
910 float y;
911
Thales Lima85c942f2021-12-31 13:04:20 +0000912 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
913 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100914 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000915
916 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
917 minCellSize[INDEX_DEFAULT].x);
918 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
919 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100920 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100921
Thales Lima85c942f2021-12-31 13:04:20 +0000922 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100923 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000924 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100925 minCellSize[INDEX_DEFAULT].y);
926 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
927
Thales Lima85c942f2021-12-31 13:04:20 +0000928 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100929 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000930 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100931 minCellSize[INDEX_DEFAULT].y);
932 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100933
Thales Lima85c942f2021-12-31 13:04:20 +0000934 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000935 float borderSpaceLandscape = a.getFloat(
936 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000937 float borderSpaceTwoPanelPortrait = a.getFloat(
938 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
939 float borderSpaceTwoPanelLandscape = a.getFloat(
940 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100941
Thales Lima85c942f2021-12-31 13:04:20 +0000942 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
943 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100944 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000945
946 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
947 borderSpaceLandscape);
948 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
949 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100950 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
951
952 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000953 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
954 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100955 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000956 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
957 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100958 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
959
960 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000961 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
962 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100963 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000964 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
965 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100966 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
967
Thales Limab7ef5692022-03-10 10:32:36 +0000968 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
969 minCellSize[INDEX_DEFAULT].x);
970 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
971 minCellSize[INDEX_DEFAULT].y);
972 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
973
974 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
975 allAppsCellSize[INDEX_DEFAULT].x);
976 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
977 allAppsCellSize[INDEX_DEFAULT].y);
978 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
979
980 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
981 allAppsCellSize[INDEX_DEFAULT].x);
982 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
983 allAppsCellSize[INDEX_DEFAULT].y);
984 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
985
986 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
987 allAppsCellSize[INDEX_DEFAULT].x);
988 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
989 allAppsCellSize[INDEX_DEFAULT].y);
990 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
991
Thales Lima080d8902022-03-28 15:30:29 +0100992 float allAppsBorderSpace = a.getFloat(
993 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
994 float allAppsBorderSpaceLandscape = a.getFloat(
995 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
996 allAppsBorderSpace);
997 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000998 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100999 allAppsBorderSpace);
1000 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001001 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +01001002 allAppsBorderSpace);
1003
1004 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
1005 allAppsBorderSpace);
1006 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
1007 allAppsBorderSpace);
1008 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
1009
1010 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
1011 allAppsBorderSpaceLandscape);
1012 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1013 allAppsBorderSpaceLandscape);
1014 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1015
1016 x = a.getFloat(
1017 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1018 allAppsBorderSpaceTwoPanelPortrait);
1019 y = a.getFloat(
1020 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1021 allAppsBorderSpaceTwoPanelPortrait);
1022 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1023
1024 x = a.getFloat(
1025 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1026 allAppsBorderSpaceTwoPanelLandscape);
1027 y = a.getFloat(
1028 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1029 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001030 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001031
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001032 iconSizes[INDEX_DEFAULT] =
1033 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1034 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001035 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001036 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001037 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001038 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001039 iconSizes[INDEX_DEFAULT]);
1040 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001041 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001042 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001043
Thales Limabb7d3882021-12-22 12:56:29 +00001044 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1045 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001046 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1047 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001048 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001049 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1050 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1051 allAppsIconSizes[INDEX_DEFAULT]);
1052 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1053 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1054 allAppsIconSizes[INDEX_DEFAULT]);
1055
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001056 textSizes[INDEX_DEFAULT] =
1057 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1058 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001059 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001060 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001061 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001062 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001063 textSizes[INDEX_DEFAULT]);
1064 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001065 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001066 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001067
Thales Limabb7d3882021-12-22 12:56:29 +00001068 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1069 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1070 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1071 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1072 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1073 allAppsIconTextSizes[INDEX_DEFAULT]);
1074 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1075 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1076 allAppsIconTextSizes[INDEX_DEFAULT]);
1077
Thales Lima83bedbf2021-10-05 17:47:39 +01001078 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1079 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001080 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1081 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1082 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001083 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001084 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001085 horizontalMargin[INDEX_DEFAULT]);
1086 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001087 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001088 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001089
Thales Limab8c05952022-05-23 16:58:38 +01001090 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1091 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1092 ResourcesCompat.getFloat(context.getResources(),
1093 R.dimen.hotseat_bar_bottom_space_default));
1094 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1095 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1096 hotseatBarBottomSpace[INDEX_DEFAULT]);
1097 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1098 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1099 hotseatBarBottomSpace[INDEX_DEFAULT]);
1100 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1101 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1102 hotseatBarBottomSpace[INDEX_DEFAULT]);
1103
1104 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1105 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1106 ResourcesCompat.getFloat(context.getResources(),
1107 R.dimen.hotseat_qsb_space_default));
1108 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1109 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1110 hotseatQsbSpace[INDEX_DEFAULT]);
1111 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1112 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1113 hotseatQsbSpace[INDEX_DEFAULT]);
1114 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1115 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1116 hotseatQsbSpace[INDEX_DEFAULT]);
1117
Sunny Goyal415f1732018-11-29 10:33:47 -08001118 a.recycle();
1119 }
1120
1121 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001122 this(null);
1123 }
1124
1125 DisplayOption(GridOption grid) {
1126 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001127 minWidthDps = 0;
1128 minHeightDps = 0;
1129 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001130 for (int i = 0; i < COUNT_SIZES; i++) {
1131 iconSizes[i] = 0;
1132 textSizes[i] = 0;
1133 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001134 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001135 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001136 allAppsIconSizes[i] = 0;
1137 allAppsIconTextSizes[i] = 0;
1138 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001139 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001140 }
1141
1142 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001143 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001144 iconSizes[i] *= w;
1145 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001146 borderSpaces[i].x *= w;
1147 borderSpaces[i].y *= w;
1148 minCellSize[i].x *= w;
1149 minCellSize[i].y *= w;
1150 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001151 hotseatBarBottomSpace[i] *= w;
1152 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001153 allAppsCellSize[i].x *= w;
1154 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001155 allAppsIconSizes[i] *= w;
1156 allAppsIconTextSizes[i] *= w;
1157 allAppsBorderSpaces[i].x *= w;
1158 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001159 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001160
Sunny Goyal415f1732018-11-29 10:33:47 -08001161 return this;
1162 }
1163
1164 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001165 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001166 iconSizes[i] += p.iconSizes[i];
1167 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001168 borderSpaces[i].x += p.borderSpaces[i].x;
1169 borderSpaces[i].y += p.borderSpaces[i].y;
1170 minCellSize[i].x += p.minCellSize[i].x;
1171 minCellSize[i].y += p.minCellSize[i].y;
1172 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001173 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1174 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001175 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1176 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001177 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1178 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1179 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1180 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001181 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001182
Sunny Goyal415f1732018-11-29 10:33:47 -08001183 return this;
1184 }
1185 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001186}