blob: ffe81ad2aaa9244aadf226755ae15e1bda2516df [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 Chauab800f72022-12-13 17:46:06 +0000403 .setIsMultiDisplay(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
Thales Lima6a590062022-11-22 15:52:49 +0000425 int numMinShownHotseatIconsForTablet = supportedProfiles
426 .stream()
427 .filter(deviceProfile -> deviceProfile.isTablet)
428 .mapToInt(deviceProfile -> deviceProfile.numShownHotseatIcons)
429 .min()
430 .orElse(0);
431
432 supportedProfiles
433 .stream()
434 .filter(deviceProfile -> deviceProfile.isTablet)
435 .forEach(deviceProfile -> {
436 deviceProfile.numShownHotseatIcons = numMinShownHotseatIconsForTablet;
437 deviceProfile.recalculateHotseatWidthAndBorderSpace();
438 });
439
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700440 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
441 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700442 }
443
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700444 public void addOnChangeListener(OnIDPChangeListener listener) {
445 mChangeListeners.add(listener);
446 }
447
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800448 public void removeOnChangeListener(OnIDPChangeListener listener) {
449 mChangeListeners.remove(listener);
450 }
451
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800452
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800453 public void setCurrentGrid(Context context, String gridName) {
454 Context appContext = context.getApplicationContext();
Stefan Andonian146701c2022-11-10 23:07:40 +0000455 LauncherPrefs.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700456 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800457 }
458
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700459 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700460 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000461 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
462 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700463 }
464
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700465 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700466 Object[] oldState = toModelState();
467
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700468 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700469 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700470 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700471
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700472 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700473 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700474 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700475 }
476 }
477
Alex Chau1c883d82021-12-01 18:43:10 +0000478 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
479 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800480 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100481
Alex Chau1c883d82021-12-01 18:43:10 +0000482 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700483 final int depth = parser.getDepth();
484 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700485 while (((type = parser.next()) != XmlPullParser.END_TAG ||
486 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800487 if ((type == XmlPullParser.START_TAG)
488 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800489
Alex Chau1c883d82021-12-01 18:43:10 +0000490 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
491 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100492 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100493 final int displayDepth = parser.getDepth();
494 while (((type = parser.next()) != XmlPullParser.END_TAG
495 || parser.getDepth() > displayDepth)
496 && type != XmlPullParser.END_DOCUMENT) {
497 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
498 parser.getName())) {
499 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100500 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100501 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800502 }
503 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700504 }
505 }
sfufa@google.comde013292021-09-21 18:22:44 -0700506 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700507 throw new RuntimeException(e);
508 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800509
Sunny Goyalae190ff2020-04-14 00:19:01 +0000510 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800511 if (!TextUtils.isEmpty(gridName)) {
512 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100513 if (gridName.equals(option.grid.name)
514 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000515 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800516 }
517 }
518 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000519 if (filteredProfiles.isEmpty()) {
520 // No grid found, use the default options
521 for (DisplayOption option : profiles) {
522 if (option.canBeDefault) {
523 filteredProfiles.add(option);
524 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800525 }
526 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000527 if (filteredProfiles.isEmpty()) {
528 throw new RuntimeException("No display option with canBeDefault=true");
529 }
530 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700531 }
532
Thales Lima7ec83822021-08-05 13:32:10 +0100533 /**
534 * @return all the grid options that can be shown on the device
535 */
536 public List<GridOption> parseAllGridOptions(Context context) {
537 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100538
Alex Chau1c883d82021-12-01 18:43:10 +0000539 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100540 final int depth = parser.getDepth();
541 int type;
542 while (((type = parser.next()) != XmlPullParser.END_TAG
543 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
544 if ((type == XmlPullParser.START_TAG)
545 && GridOption.TAG_NAME.equals(parser.getName())) {
546 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000547 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100548 if (option.isEnabled) {
549 result.add(option);
550 }
551 }
552 }
553 } catch (IOException | XmlPullParserException e) {
554 Log.e(TAG, "Error parsing device profile", e);
555 return Collections.emptyList();
556 }
557 return result;
558 }
559
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700560 private int getLauncherIconDensity(int requiredSize) {
561 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700562 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700563 DisplayMetrics.DENSITY_LOW,
564 DisplayMetrics.DENSITY_MEDIUM,
565 DisplayMetrics.DENSITY_TV,
566 DisplayMetrics.DENSITY_HIGH,
567 DisplayMetrics.DENSITY_XHIGH,
568 DisplayMetrics.DENSITY_XXHIGH,
569 DisplayMetrics.DENSITY_XXXHIGH
570 };
571
572 int density = DisplayMetrics.DENSITY_XXXHIGH;
573 for (int i = densityBuckets.length - 1; i >= 0; i--) {
574 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
575 / DisplayMetrics.DENSITY_DEFAULT;
576 if (expectedSize >= requiredSize) {
577 density = densityBuckets[i];
578 }
579 }
580
581 return density;
582 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700583
Adam Cohen2e6da152015-05-06 11:42:25 -0700584 /**
585 * Apply any Partner customization grid overrides.
586 *
587 * Currently we support: all apps row / column count.
588 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700589 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
590 Partner p = Partner.get(context.getPackageManager());
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800591 if (p == null) {
592 return;
593 }
594 try {
595 int numRows = p.getIntValue(RES_GRID_NUM_ROWS, -1);
596 int numColumns = p.getIntValue(RES_GRID_NUM_COLUMNS, -1);
597 float iconSizePx = p.getDimenValue(RES_GRID_ICON_SIZE_DP, -1);
598
599 if (numRows > 0 && numColumns > 0) {
600 this.numRows = numRows;
601 this.numColumns = numColumns;
602 }
603 if (iconSizePx > 0) {
604 this.iconSize[InvariantDeviceProfile.INDEX_DEFAULT] =
605 Utilities.dpiFromPx(iconSizePx, dm.densityDpi);
606 }
607 } catch (Resources.NotFoundException ex) {
608 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700609 }
610 }
611
Sunny Goyal415f1732018-11-29 10:33:47 -0800612 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700613 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700614 }
615
Sunny Goyal19ff7282021-04-22 10:12:54 -0700616 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000617 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700618 int minWidthPx = Integer.MAX_VALUE;
619 int minHeightPx = Integer.MAX_VALUE;
620 for (WindowBounds bounds : displayInfo.supportedBounds) {
621 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000622 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700623 // For split displays, take half width per page
624 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
625 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700626
Sunny Goyal19ff7282021-04-22 10:12:54 -0700627 } else if (!isTablet && bounds.isLandscape()) {
628 // We will use transposed layout in this case
629 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
630 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
631 } else {
632 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
633 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
634 }
635 }
636
Thales Lima425f6822022-05-10 13:44:58 -0300637 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
638 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700639
640 // Sort the profiles based on the closeness to the device size
641 Collections.sort(points, (a, b) ->
642 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
643 dist(width, height, b.minWidthDps, b.minHeightDps)));
644
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700645 DisplayOption closestPoint = points.get(0);
646 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700647 float weights = 0;
648
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700649 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
650 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700651 }
652
653 DisplayOption out = new DisplayOption(closestOption);
654 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700655 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700656 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
657 weights += w;
658 out.add(new DisplayOption().add(p).multiply(w));
659 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700660 out.multiply(1.0f / weights);
661
Thales Lima6e0005a2021-10-27 15:53:41 +0100662 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700663 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100664 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
665 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700666 }
667
668 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700669 }
670
Sunny Goyal27835952017-01-13 12:15:53 -0800671 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700672 Resources res = context.getResources();
673 Configuration config = context.getResources().getConfiguration();
674
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400675 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
676 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100677 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
678
Alex Chau24265142022-11-14 21:19:44 +0000679 if (Utilities.IS_DEBUG_DEVICE && ENABLE_DEVICE_PROFILE_LOGGING.get()) {
Alex Chau5dfd9392022-11-02 17:39:40 +0000680 StringWriter stringWriter = new StringWriter();
681 PrintWriter printWriter = new PrintWriter(stringWriter);
682 DisplayController.INSTANCE.get(context).dump(printWriter);
683 printWriter.flush();
684 Log.d("b/253338238", "getDeviceProfile -"
685 + "\nconfig: " + config
686 + "\ndisplayMetrics: " + res.getDisplayMetrics()
687 + "\nrotation: " + rotation
688 + "\n" + stringWriter,
689 new Exception());
690 }
Alex Chaud3e8cc42022-05-03 17:45:34 +0100691 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400692 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700693
Sunny Goyal187b16c2022-03-01 16:53:23 -0800694 /**
695 * Returns the device profile matching the provided screen configuration
696 */
697 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700698 DeviceProfile bestMatch = supportedProfiles.get(0);
699 float minDiff = Float.MAX_VALUE;
700
701 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400702 float diff = Math.abs(profile.widthPx - screenWidth)
703 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700704 if (diff < minDiff) {
705 minDiff = diff;
706 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800707 } else if (diff == minDiff && profile.rotationHint == rotation) {
708 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700709 }
710 }
711 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800712 }
713
Sunny Goyal415f1732018-11-29 10:33:47 -0800714 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700715 float d = dist(x0, y0, x1, y1);
716 if (Float.compare(d, 0f) == 0) {
717 return Float.POSITIVE_INFINITY;
718 }
719 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
720 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700721
722 /**
723 * As a ratio of screen height, the total distance we want the parallax effect to span
724 * horizontally
725 */
726 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
727 float aspectRatio = width / (float) height;
728
729 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
730 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
731 // We will use these two data points to extrapolate how much the wallpaper parallax effect
732 // to span (ie travel) at any aspect ratio:
733
sfufa@google.comde013292021-09-21 18:22:44 -0700734 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
735 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700736 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
737 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
738
739 // To find out the desired width at different aspect ratios, we use the following two
740 // formulas, where the coefficient on x is the aspect ratio (width/height):
741 // (16/10)x + y = 1.5
742 // (10/16)x + y = 1.2
743 // We solve for x and y and end up with a final formula:
744 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700745 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
746 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700747 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
748 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
749 return x * aspectRatio + y;
750 }
751
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700752 public interface OnIDPChangeListener {
753
Sunny Goyalb47172b2021-05-03 19:59:51 -0700754 /**
755 * Called when the device provide changes
756 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700757 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700758 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800759
760
Sunny Goyaleff44f32019-01-09 17:29:49 -0800761 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800762
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800763 public static final String TAG_NAME = "grid-option";
764
Alex Chau1c883d82021-12-01 18:43:10 +0000765 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
766 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
767 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
768 private static final int DEVICE_CATEGORY_ALL =
769 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
770
Thales Lima11af7bc2022-08-12 15:24:54 +0100771 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
772 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
773 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
774 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
775 private static final int DONT_INLINE_QSB = 0;
776
Sunny Goyaleff44f32019-01-09 17:29:49 -0800777 public final String name;
778 public final int numRows;
779 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000780 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100781 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800782
783 private final int numFolderRows;
784 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300785 private final @StyleRes int folderStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800786
Sunny Goyal19ff7282021-04-22 10:12:54 -0700787 private final int numAllAppsColumns;
788 private final int numDatabaseAllAppsColumns;
789 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700790 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100791
Thales Lima425f6822022-05-10 13:44:58 -0300792 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800793
Thales Lima11af7bc2022-08-12 15:24:54 +0100794 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
795
Thales Limab35faed2022-09-05 16:30:01 -0300796 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800797 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000798
Sunny Goyal415f1732018-11-29 10:33:47 -0800799 private final int defaultLayoutId;
800 private final int demoModeLayoutId;
801
Jon Mirandae126d722021-02-25 10:45:20 -0500802 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400803 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500804
Sunny Goyal5bc18462019-01-07 15:13:39 -0800805 private final SparseArray<TypedValue> extraAttrs;
806
Alex Chau1c883d82021-12-01 18:43:10 +0000807 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800808 TypedArray a = context.obtainStyledAttributes(
809 attrs, R.styleable.GridDisplayOption);
810 name = a.getString(R.styleable.GridDisplayOption_name);
811 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
812 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000813 numSearchContainerColumns = a.getInt(
814 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800815
Tracy Zhou7df93d22020-01-27 13:44:06 -0800816 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau766cd2b2022-07-06 17:15:21 +0100817 defaultLayoutId = a.getResourceId(
818 R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800819 demoModeLayoutId = a.getResourceId(
820 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700821
822 numAllAppsColumns = a.getInt(
823 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
824 numDatabaseAllAppsColumns = a.getInt(
825 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
826
827 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800828 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700829 numDatabaseHotseatIcons = a.getInt(
830 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100831
Thales Lima425f6822022-05-10 13:44:58 -0300832 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
833 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
834 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
835 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
836 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
837 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
838 numColumns);
839 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
840 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
841 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700842
Vinit Nayakc7293172022-07-18 16:41:50 -0700843 inlineNavButtonsEndSpacing =
844 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
845 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300846
Sunny Goyal415f1732018-11-29 10:33:47 -0800847 numFolderRows = a.getInt(
848 R.styleable.GridDisplayOption_numFolderRows, numRows);
849 numFolderColumns = a.getInt(
850 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700851
Thales Limab35faed2022-09-05 16:30:01 -0300852 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
853 INVALID_RESOURCE_HANDLE);
854
Jon Mirandae126d722021-02-25 10:45:20 -0500855 isScalable = a.getBoolean(
856 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400857 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000858 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Jon Mirandae126d722021-02-25 10:45:20 -0500859
Alex Chau1c883d82021-12-01 18:43:10 +0000860 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
861 DEVICE_CATEGORY_ALL);
862 isEnabled = (deviceType == TYPE_PHONE
863 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
864 || (deviceType == TYPE_TABLET
865 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
866 || (deviceType == TYPE_MULTI_DISPLAY
867 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
868 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100869
Thales Lima11af7bc2022-08-12 15:24:54 +0100870 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
871 DONT_INLINE_QSB);
872 inlineQsb[INDEX_DEFAULT] =
873 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
874 inlineQsb[INDEX_LANDSCAPE] =
875 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
876 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
877 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
878 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
879 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
880 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
881 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
882
Sunny Goyal415f1732018-11-29 10:33:47 -0800883 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800884 extraAttrs = Themes.createValueMap(context, attrs,
885 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800886 }
887 }
888
Sunny Goyal19ff7282021-04-22 10:12:54 -0700889 @VisibleForTesting
890 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700891 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800892
Sunny Goyal415f1732018-11-29 10:33:47 -0800893 private final float minWidthDps;
894 private final float minHeightDps;
895 private final boolean canBeDefault;
896
Thales Lima83bedbf2021-10-05 17:47:39 +0100897 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100898
Thales Lima78d00ad2021-09-30 11:29:06 +0100899 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100900 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100901 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
902 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100903
Thales Lima78d00ad2021-09-30 11:29:06 +0100904 private final float[] iconSizes = new float[COUNT_SIZES];
905 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800906
Thales Limab7ef5692022-03-10 10:32:36 +0000907 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000908 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
909 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
910 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
911
Thales Lima1de4d552021-10-13 16:13:25 +0100912 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800913 this.grid = grid;
914
Thales Lima1de4d552021-10-13 16:13:25 +0100915 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800916
Sunny Goyal415f1732018-11-29 10:33:47 -0800917 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
918 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700919
Thales Lima1de4d552021-10-13 16:13:25 +0100920 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800921
Thales Lima83bedbf2021-10-05 17:47:39 +0100922 float x;
923 float y;
924
Thales Lima85c942f2021-12-31 13:04:20 +0000925 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
926 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100927 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000928
929 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
930 minCellSize[INDEX_DEFAULT].x);
931 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
932 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100933 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100934
Thales Lima85c942f2021-12-31 13:04:20 +0000935 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100936 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000937 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100938 minCellSize[INDEX_DEFAULT].y);
939 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
940
Thales Lima85c942f2021-12-31 13:04:20 +0000941 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100942 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000943 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100944 minCellSize[INDEX_DEFAULT].y);
945 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100946
Thales Lima85c942f2021-12-31 13:04:20 +0000947 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000948 float borderSpaceLandscape = a.getFloat(
949 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000950 float borderSpaceTwoPanelPortrait = a.getFloat(
951 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
952 float borderSpaceTwoPanelLandscape = a.getFloat(
953 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100954
Thales Lima85c942f2021-12-31 13:04:20 +0000955 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
956 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100957 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000958
959 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
960 borderSpaceLandscape);
961 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
962 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100963 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
964
965 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000966 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
967 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100968 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000969 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
970 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100971 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
972
973 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000974 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
975 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100976 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000977 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
978 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100979 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
980
Thales Limab7ef5692022-03-10 10:32:36 +0000981 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
982 minCellSize[INDEX_DEFAULT].x);
983 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
984 minCellSize[INDEX_DEFAULT].y);
985 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
986
987 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
988 allAppsCellSize[INDEX_DEFAULT].x);
989 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
990 allAppsCellSize[INDEX_DEFAULT].y);
991 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
992
993 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
994 allAppsCellSize[INDEX_DEFAULT].x);
995 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
996 allAppsCellSize[INDEX_DEFAULT].y);
997 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
998
999 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
1000 allAppsCellSize[INDEX_DEFAULT].x);
1001 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
1002 allAppsCellSize[INDEX_DEFAULT].y);
1003 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
1004
Thales Lima080d8902022-03-28 15:30:29 +01001005 float allAppsBorderSpace = a.getFloat(
1006 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
1007 float allAppsBorderSpaceLandscape = a.getFloat(
1008 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
1009 allAppsBorderSpace);
1010 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001011 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +01001012 allAppsBorderSpace);
1013 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001014 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +01001015 allAppsBorderSpace);
1016
1017 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
1018 allAppsBorderSpace);
1019 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
1020 allAppsBorderSpace);
1021 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
1022
1023 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
1024 allAppsBorderSpaceLandscape);
1025 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1026 allAppsBorderSpaceLandscape);
1027 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1028
1029 x = a.getFloat(
1030 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1031 allAppsBorderSpaceTwoPanelPortrait);
1032 y = a.getFloat(
1033 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1034 allAppsBorderSpaceTwoPanelPortrait);
1035 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1036
1037 x = a.getFloat(
1038 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1039 allAppsBorderSpaceTwoPanelLandscape);
1040 y = a.getFloat(
1041 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1042 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001043 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001044
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001045 iconSizes[INDEX_DEFAULT] =
1046 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1047 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001048 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001049 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001050 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001051 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001052 iconSizes[INDEX_DEFAULT]);
1053 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001054 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001055 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001056
Thales Limabb7d3882021-12-22 12:56:29 +00001057 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1058 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001059 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1060 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001061 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001062 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1063 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1064 allAppsIconSizes[INDEX_DEFAULT]);
1065 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1066 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1067 allAppsIconSizes[INDEX_DEFAULT]);
1068
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001069 textSizes[INDEX_DEFAULT] =
1070 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1071 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001072 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001073 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001074 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001075 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001076 textSizes[INDEX_DEFAULT]);
1077 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001078 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001079 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001080
Thales Limabb7d3882021-12-22 12:56:29 +00001081 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1082 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1083 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1084 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1085 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1086 allAppsIconTextSizes[INDEX_DEFAULT]);
1087 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1088 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1089 allAppsIconTextSizes[INDEX_DEFAULT]);
1090
Thales Lima83bedbf2021-10-05 17:47:39 +01001091 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1092 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001093 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1094 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1095 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001096 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001097 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001098 horizontalMargin[INDEX_DEFAULT]);
1099 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001100 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001101 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001102
Thales Limab8c05952022-05-23 16:58:38 +01001103 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1104 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1105 ResourcesCompat.getFloat(context.getResources(),
1106 R.dimen.hotseat_bar_bottom_space_default));
1107 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1108 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1109 hotseatBarBottomSpace[INDEX_DEFAULT]);
1110 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1111 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1112 hotseatBarBottomSpace[INDEX_DEFAULT]);
1113 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1114 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1115 hotseatBarBottomSpace[INDEX_DEFAULT]);
1116
1117 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1118 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1119 ResourcesCompat.getFloat(context.getResources(),
1120 R.dimen.hotseat_qsb_space_default));
1121 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1122 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1123 hotseatQsbSpace[INDEX_DEFAULT]);
1124 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1125 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1126 hotseatQsbSpace[INDEX_DEFAULT]);
1127 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1128 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1129 hotseatQsbSpace[INDEX_DEFAULT]);
1130
Sunny Goyal415f1732018-11-29 10:33:47 -08001131 a.recycle();
1132 }
1133
1134 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001135 this(null);
1136 }
1137
1138 DisplayOption(GridOption grid) {
1139 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001140 minWidthDps = 0;
1141 minHeightDps = 0;
1142 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001143 for (int i = 0; i < COUNT_SIZES; i++) {
1144 iconSizes[i] = 0;
1145 textSizes[i] = 0;
1146 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001147 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001148 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001149 allAppsIconSizes[i] = 0;
1150 allAppsIconTextSizes[i] = 0;
1151 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001152 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001153 }
1154
1155 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001156 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001157 iconSizes[i] *= w;
1158 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001159 borderSpaces[i].x *= w;
1160 borderSpaces[i].y *= w;
1161 minCellSize[i].x *= w;
1162 minCellSize[i].y *= w;
1163 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001164 hotseatBarBottomSpace[i] *= w;
1165 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001166 allAppsCellSize[i].x *= w;
1167 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001168 allAppsIconSizes[i] *= w;
1169 allAppsIconTextSizes[i] *= w;
1170 allAppsBorderSpaces[i].x *= w;
1171 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001172 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001173
Sunny Goyal415f1732018-11-29 10:33:47 -08001174 return this;
1175 }
1176
1177 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001178 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001179 iconSizes[i] += p.iconSizes[i];
1180 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001181 borderSpaces[i].x += p.borderSpaces[i].x;
1182 borderSpaces[i].y += p.borderSpaces[i].y;
1183 minCellSize[i].x += p.minCellSize[i].x;
1184 minCellSize[i].y += p.minCellSize[i].y;
1185 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001186 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1187 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001188 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1189 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001190 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1191 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1192 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1193 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001194 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001195
Sunny Goyal415f1732018-11-29 10:33:47 -08001196 return this;
1197 }
1198 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001199}