blob: d97eac94fb61ec8049926779661f560c73ca148d [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 Limad852d652023-01-17 14:01:13 +0000146 public @StyleRes int cellStyle;
147
Thales Lima83bedbf2021-10-05 17:47:39 +0100148 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500149
Thales Limab7ef5692022-03-10 10:32:36 +0000150 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000151 public float[] allAppsIconSize;
152 public float[] allAppsIconTextSize;
153 public PointF[] allAppsBorderSpaces;
154
Sunny Goyal5bc18462019-01-07 15:13:39 -0800155 private SparseArray<TypedValue> mExtraAttrs;
156
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700157 /**
158 * Number of icons inside the hotseat area.
159 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800160 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700161
162 /**
163 * Number of icons inside the hotseat area that is stored in the database. This is greater than
164 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
165 * sizes that share the same DB.
166 */
167 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700168
Thales Lima425f6822022-05-10 13:44:58 -0300169 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100170 public float[] hotseatBarBottomSpace;
171 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300172
Jon Miranda6f7e9702019-09-16 14:44:14 -0700173 /**
174 * Number of columns in the all apps list.
175 */
176 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700177 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700178
Jon Mirandae126d722021-02-25 10:45:20 -0500179 /**
180 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
181 */
182 protected boolean isScalable;
Thales Limaae0d7ef2022-11-16 15:53:43 +0000183 @XmlRes
184 public int devicePaddingId = INVALID_RESOURCE_HANDLE;
Jon Mirandae126d722021-02-25 10:45:20 -0500185
Tracy Zhou7df93d22020-01-27 13:44:06 -0800186 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800187 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700188 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100189 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700190
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000191 /**
192 * An immutable list of supported profiles.
193 */
194 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700195
Sunny Goyal6f866092016-03-17 17:04:15 -0700196 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700197 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700198
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700199 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700200
Sunny Goyalf633ef52018-03-13 09:57:05 -0700201 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800202 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700203
Sunny Goyalbbf01842015-10-08 07:41:15 -0700204 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700205 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700206 String gridName = getCurrentGridName(context);
207 String newGridName = initGrid(context, gridName);
208 if (!newGridName.equals(gridName)) {
Stefan Andonian146701c2022-11-10 23:07:40 +0000209 LauncherPrefs.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName)
210 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700211 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700212 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700213
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700214 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100215 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000216 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
217 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100218 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700219 }
220 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700221 }
222
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700223 /**
224 * This constructor should NOT have any monitors by design.
225 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800226 public InvariantDeviceProfile(Context context, String gridName) {
227 String newName = initGrid(context, gridName);
228 if (newName == null || !newName.equals(gridName)) {
229 throw new IllegalArgumentException("Unknown grid name");
230 }
231 }
232
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700233 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800234 * This constructor should NOT have any monitors by design.
235 */
236 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700237 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100238 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700239 String gridName = getCurrentGridName(context);
240
241 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000242 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
243 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700244 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000245 defaultInfo,
246 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
247 /*allowDisabledGrid=*/false),
248 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700249
Alex Chau661f02d2022-06-07 14:03:43 +0100250 Context displayContext = context.createDisplayContext(display);
251 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000252 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700253 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000254 myInfo,
255 getPredefinedDeviceProfiles(context, gridName, deviceType,
256 /*allowDisabledGrid=*/false),
257 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700258
259 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
260 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100261 result.iconSizes[INDEX_DEFAULT] =
262 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
263 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700264 result.iconSizes[i] = Math.min(
265 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000266 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700267
Thales Lima83bedbf2021-10-05 17:47:39 +0100268 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
269 COUNT_SIZES);
270 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
271 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500272
Alex Chau1c883d82021-12-01 18:43:10 +0000273 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800274 }
275
Alex Chau238aaee2021-10-06 16:15:24 +0100276 /**
277 * Reinitialize the current grid after a restore, where some grids might now be disabled.
278 */
279 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000280 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100281 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000282 String newGridName = initGrid(context, currentGridName);
283 String newDbFile = dbFile;
284 if (!newDbFile.equals(currentDbFile)) {
285 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100286 + ", migrating to: " + newGridName
287 + ", removing all other grid db files");
288 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
289 if (gridDbFile.equals(currentDbFile)) {
290 continue;
291 }
292 if (context.getDatabasePath(gridDbFile).delete()) {
293 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
294 }
295 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000296 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100297 }
298 }
299
Alex Chau1c883d82021-12-01 18:43:10 +0000300 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800301 int flagPhone = 1 << 0;
302 int flagTablet = 1 << 1;
303
304 int type = displayInfo.supportedBounds.stream()
305 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
306 .reduce(0, (a, b) -> a | b);
307 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
308 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000309 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800310 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000311 return TYPE_TABLET;
312 } else {
313 return TYPE_PHONE;
314 }
315 }
316
Tracy Zhou42255d22020-03-13 00:38:11 -0700317 public static String getCurrentGridName(Context context) {
Stefan Andonian146701c2022-11-10 23:07:40 +0000318 return LauncherPrefs.getPrefs(context).getString(KEY_IDP_GRID_NAME, null);
Tracy Zhou42255d22020-03-13 00:38:11 -0700319 }
320
Sunny Goyaleff44f32019-01-09 17:29:49 -0800321 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700322 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000323 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700324
325 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000326 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100327 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700328 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000329 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
330 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700331 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800332 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700333
Thales Limab67bfa72022-11-02 15:30:11 +0000334 @VisibleForTesting
335 public static String getDefaultGridName(Context context) {
336 return new InvariantDeviceProfile().initGrid(context, null);
337 }
338
Alex Chau1c883d82021-12-01 18:43:10 +0000339 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
340 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700341 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700342 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000343 numRows = closestProfile.numRows;
344 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000345 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000346 dbFile = closestProfile.dbFile;
347 defaultLayoutId = closestProfile.defaultLayoutId;
348 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300349
Sunny Goyalae190ff2020-04-14 00:19:01 +0000350 numFolderRows = closestProfile.numFolderRows;
351 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300352 folderStyle = closestProfile.folderStyle;
353
Thales Limad852d652023-01-17 14:01:13 +0000354 cellStyle = closestProfile.cellStyle;
355
Jon Mirandae126d722021-02-25 10:45:20 -0500356 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400357 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000358 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000359
Vinit Nayakc7293172022-07-18 16:41:50 -0700360 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
361
Sunny Goyalae190ff2020-04-14 00:19:01 +0000362 mExtraAttrs = closestProfile.extraAttrs;
363
Thales Lima83bedbf2021-10-05 17:47:39 +0100364 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100365 float maxIconSize = iconSize[0];
366 for (int i = 1; i < iconSize.length; i++) {
367 maxIconSize = Math.max(maxIconSize, iconSize[i]);
368 }
369 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000370 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
371
Thales Lima83bedbf2021-10-05 17:47:39 +0100372 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700373
Thales Lima83bedbf2021-10-05 17:47:39 +0100374 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100375
Thales Lima83bedbf2021-10-05 17:47:39 +0100376 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100377
Thales Limad90faab2021-09-21 17:18:47 +0100378 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100379
Sunny Goyal19ff7282021-04-22 10:12:54 -0700380 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000381 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700382 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300383 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100384 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
385 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700386
387 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000388 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700389 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500390
Thales Limab7ef5692022-03-10 10:32:36 +0000391 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000392 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
393 allAppsIconSize = displayOption.allAppsIconSizes;
394 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000395
Thales Lima11af7bc2022-08-12 15:24:54 +0100396 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000397
Sunny Goyalae190ff2020-04-14 00:19:01 +0000398 // If the partner customization apk contains any grid overrides, apply them
399 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700400 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700401
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000402 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700403 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700404 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700405 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000406 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chauab800f72022-12-13 17:46:06 +0000407 .setIsMultiDisplay(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000408 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700409 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000410 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700411
Sunny Goyal19ff7282021-04-22 10:12:54 -0700412 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
413 int displayWidth = bounds.bounds.width();
414 int displayHeight = bounds.bounds.height();
415 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700416
Sunny Goyal19ff7282021-04-22 10:12:54 -0700417 // We need to ensure that there is enough extra space in the wallpaper
418 // for the intended parallax effects
419 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300420 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
421 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700422 ? 2
423 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
424 defaultWallpaperSize.x =
425 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700426 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000427 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700428
Thales Lima6a590062022-11-22 15:52:49 +0000429 int numMinShownHotseatIconsForTablet = supportedProfiles
430 .stream()
431 .filter(deviceProfile -> deviceProfile.isTablet)
432 .mapToInt(deviceProfile -> deviceProfile.numShownHotseatIcons)
433 .min()
434 .orElse(0);
435
436 supportedProfiles
437 .stream()
438 .filter(deviceProfile -> deviceProfile.isTablet)
439 .forEach(deviceProfile -> {
440 deviceProfile.numShownHotseatIcons = numMinShownHotseatIconsForTablet;
441 deviceProfile.recalculateHotseatWidthAndBorderSpace();
442 });
443
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700444 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
445 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700446 }
447
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700448 public void addOnChangeListener(OnIDPChangeListener listener) {
449 mChangeListeners.add(listener);
450 }
451
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800452 public void removeOnChangeListener(OnIDPChangeListener listener) {
453 mChangeListeners.remove(listener);
454 }
455
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800456
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800457 public void setCurrentGrid(Context context, String gridName) {
458 Context appContext = context.getApplicationContext();
Stefan Andonian146701c2022-11-10 23:07:40 +0000459 LauncherPrefs.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700460 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800461 }
462
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700463 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700464 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000465 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
466 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700467 }
468
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700469 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700470 Object[] oldState = toModelState();
471
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700472 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700473 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700474 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700475
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700476 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700477 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700478 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700479 }
480 }
481
Alex Chau1c883d82021-12-01 18:43:10 +0000482 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
483 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800484 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100485
Alex Chau1c883d82021-12-01 18:43:10 +0000486 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700487 final int depth = parser.getDepth();
488 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700489 while (((type = parser.next()) != XmlPullParser.END_TAG ||
490 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800491 if ((type == XmlPullParser.START_TAG)
492 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800493
Alex Chau1c883d82021-12-01 18:43:10 +0000494 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
495 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100496 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100497 final int displayDepth = parser.getDepth();
498 while (((type = parser.next()) != XmlPullParser.END_TAG
499 || parser.getDepth() > displayDepth)
500 && type != XmlPullParser.END_DOCUMENT) {
501 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
502 parser.getName())) {
503 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100504 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100505 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800506 }
507 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700508 }
509 }
sfufa@google.comde013292021-09-21 18:22:44 -0700510 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700511 throw new RuntimeException(e);
512 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800513
Sunny Goyalae190ff2020-04-14 00:19:01 +0000514 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800515 if (!TextUtils.isEmpty(gridName)) {
516 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100517 if (gridName.equals(option.grid.name)
518 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000519 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800520 }
521 }
522 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000523 if (filteredProfiles.isEmpty()) {
524 // No grid found, use the default options
525 for (DisplayOption option : profiles) {
526 if (option.canBeDefault) {
527 filteredProfiles.add(option);
528 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800529 }
530 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000531 if (filteredProfiles.isEmpty()) {
532 throw new RuntimeException("No display option with canBeDefault=true");
533 }
534 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700535 }
536
Thales Lima7ec83822021-08-05 13:32:10 +0100537 /**
538 * @return all the grid options that can be shown on the device
539 */
540 public List<GridOption> parseAllGridOptions(Context context) {
541 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100542
Alex Chau1c883d82021-12-01 18:43:10 +0000543 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100544 final int depth = parser.getDepth();
545 int type;
546 while (((type = parser.next()) != XmlPullParser.END_TAG
547 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
548 if ((type == XmlPullParser.START_TAG)
549 && GridOption.TAG_NAME.equals(parser.getName())) {
550 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000551 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100552 if (option.isEnabled) {
553 result.add(option);
554 }
555 }
556 }
557 } catch (IOException | XmlPullParserException e) {
558 Log.e(TAG, "Error parsing device profile", e);
559 return Collections.emptyList();
560 }
561 return result;
562 }
563
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700564 private int getLauncherIconDensity(int requiredSize) {
565 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700566 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700567 DisplayMetrics.DENSITY_LOW,
568 DisplayMetrics.DENSITY_MEDIUM,
569 DisplayMetrics.DENSITY_TV,
570 DisplayMetrics.DENSITY_HIGH,
571 DisplayMetrics.DENSITY_XHIGH,
572 DisplayMetrics.DENSITY_XXHIGH,
573 DisplayMetrics.DENSITY_XXXHIGH
574 };
575
576 int density = DisplayMetrics.DENSITY_XXXHIGH;
577 for (int i = densityBuckets.length - 1; i >= 0; i--) {
578 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
579 / DisplayMetrics.DENSITY_DEFAULT;
580 if (expectedSize >= requiredSize) {
581 density = densityBuckets[i];
582 }
583 }
584
585 return density;
586 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700587
Adam Cohen2e6da152015-05-06 11:42:25 -0700588 /**
589 * Apply any Partner customization grid overrides.
590 *
591 * Currently we support: all apps row / column count.
592 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700593 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
594 Partner p = Partner.get(context.getPackageManager());
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800595 if (p == null) {
596 return;
597 }
598 try {
599 int numRows = p.getIntValue(RES_GRID_NUM_ROWS, -1);
600 int numColumns = p.getIntValue(RES_GRID_NUM_COLUMNS, -1);
601 float iconSizePx = p.getDimenValue(RES_GRID_ICON_SIZE_DP, -1);
602
603 if (numRows > 0 && numColumns > 0) {
604 this.numRows = numRows;
605 this.numColumns = numColumns;
606 }
607 if (iconSizePx > 0) {
608 this.iconSize[InvariantDeviceProfile.INDEX_DEFAULT] =
609 Utilities.dpiFromPx(iconSizePx, dm.densityDpi);
610 }
611 } catch (Resources.NotFoundException ex) {
612 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700613 }
614 }
615
Sunny Goyal415f1732018-11-29 10:33:47 -0800616 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700617 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700618 }
619
Sunny Goyal19ff7282021-04-22 10:12:54 -0700620 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000621 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700622 int minWidthPx = Integer.MAX_VALUE;
623 int minHeightPx = Integer.MAX_VALUE;
624 for (WindowBounds bounds : displayInfo.supportedBounds) {
625 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000626 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700627 // For split displays, take half width per page
628 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
629 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700630
Sunny Goyal19ff7282021-04-22 10:12:54 -0700631 } else if (!isTablet && bounds.isLandscape()) {
632 // We will use transposed layout in this case
633 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
634 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
635 } else {
636 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
637 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
638 }
639 }
640
Thales Lima425f6822022-05-10 13:44:58 -0300641 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
642 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700643
644 // Sort the profiles based on the closeness to the device size
645 Collections.sort(points, (a, b) ->
646 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
647 dist(width, height, b.minWidthDps, b.minHeightDps)));
648
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700649 DisplayOption closestPoint = points.get(0);
650 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700651 float weights = 0;
652
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700653 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
654 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700655 }
656
657 DisplayOption out = new DisplayOption(closestOption);
658 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700659 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700660 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
661 weights += w;
662 out.add(new DisplayOption().add(p).multiply(w));
663 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700664 out.multiply(1.0f / weights);
665
Thales Lima6e0005a2021-10-27 15:53:41 +0100666 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700667 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100668 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
669 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700670 }
671
672 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700673 }
674
Sunny Goyal27835952017-01-13 12:15:53 -0800675 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700676 Resources res = context.getResources();
677 Configuration config = context.getResources().getConfiguration();
678
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400679 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
680 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100681 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
682
Alex Chau24265142022-11-14 21:19:44 +0000683 if (Utilities.IS_DEBUG_DEVICE && ENABLE_DEVICE_PROFILE_LOGGING.get()) {
Alex Chau5dfd9392022-11-02 17:39:40 +0000684 StringWriter stringWriter = new StringWriter();
685 PrintWriter printWriter = new PrintWriter(stringWriter);
686 DisplayController.INSTANCE.get(context).dump(printWriter);
687 printWriter.flush();
688 Log.d("b/253338238", "getDeviceProfile -"
689 + "\nconfig: " + config
690 + "\ndisplayMetrics: " + res.getDisplayMetrics()
691 + "\nrotation: " + rotation
692 + "\n" + stringWriter,
693 new Exception());
694 }
Alex Chaud3e8cc42022-05-03 17:45:34 +0100695 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400696 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700697
Sunny Goyal187b16c2022-03-01 16:53:23 -0800698 /**
699 * Returns the device profile matching the provided screen configuration
700 */
701 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700702 DeviceProfile bestMatch = supportedProfiles.get(0);
703 float minDiff = Float.MAX_VALUE;
704
705 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400706 float diff = Math.abs(profile.widthPx - screenWidth)
707 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700708 if (diff < minDiff) {
709 minDiff = diff;
710 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800711 } else if (diff == minDiff && profile.rotationHint == rotation) {
712 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700713 }
714 }
715 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800716 }
717
Sunny Goyal415f1732018-11-29 10:33:47 -0800718 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700719 float d = dist(x0, y0, x1, y1);
720 if (Float.compare(d, 0f) == 0) {
721 return Float.POSITIVE_INFINITY;
722 }
723 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
724 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700725
726 /**
727 * As a ratio of screen height, the total distance we want the parallax effect to span
728 * horizontally
729 */
730 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
731 float aspectRatio = width / (float) height;
732
733 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
734 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
735 // We will use these two data points to extrapolate how much the wallpaper parallax effect
736 // to span (ie travel) at any aspect ratio:
737
sfufa@google.comde013292021-09-21 18:22:44 -0700738 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
739 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700740 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
741 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
742
743 // To find out the desired width at different aspect ratios, we use the following two
744 // formulas, where the coefficient on x is the aspect ratio (width/height):
745 // (16/10)x + y = 1.5
746 // (10/16)x + y = 1.2
747 // We solve for x and y and end up with a final formula:
748 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700749 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
750 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700751 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
752 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
753 return x * aspectRatio + y;
754 }
755
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700756 public interface OnIDPChangeListener {
757
Sunny Goyalb47172b2021-05-03 19:59:51 -0700758 /**
759 * Called when the device provide changes
760 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700761 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700762 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800763
764
Sunny Goyaleff44f32019-01-09 17:29:49 -0800765 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800766
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800767 public static final String TAG_NAME = "grid-option";
768
Alex Chau1c883d82021-12-01 18:43:10 +0000769 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
770 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
771 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
772 private static final int DEVICE_CATEGORY_ALL =
773 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
774
Thales Lima11af7bc2022-08-12 15:24:54 +0100775 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
776 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
777 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
778 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
779 private static final int DONT_INLINE_QSB = 0;
780
Sunny Goyaleff44f32019-01-09 17:29:49 -0800781 public final String name;
782 public final int numRows;
783 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000784 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100785 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800786
787 private final int numFolderRows;
788 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300789 private final @StyleRes int folderStyle;
Thales Limad852d652023-01-17 14:01:13 +0000790 private final @StyleRes int cellStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800791
Sunny Goyal19ff7282021-04-22 10:12:54 -0700792 private final int numAllAppsColumns;
793 private final int numDatabaseAllAppsColumns;
794 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700795 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100796
Thales Lima425f6822022-05-10 13:44:58 -0300797 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800798
Thales Lima11af7bc2022-08-12 15:24:54 +0100799 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
800
Thales Limab35faed2022-09-05 16:30:01 -0300801 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800802 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000803
Sunny Goyal415f1732018-11-29 10:33:47 -0800804 private final int defaultLayoutId;
805 private final int demoModeLayoutId;
806
Jon Mirandae126d722021-02-25 10:45:20 -0500807 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400808 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500809
Sunny Goyal5bc18462019-01-07 15:13:39 -0800810 private final SparseArray<TypedValue> extraAttrs;
811
Alex Chau1c883d82021-12-01 18:43:10 +0000812 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800813 TypedArray a = context.obtainStyledAttributes(
814 attrs, R.styleable.GridDisplayOption);
815 name = a.getString(R.styleable.GridDisplayOption_name);
816 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
817 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000818 numSearchContainerColumns = a.getInt(
819 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800820
Tracy Zhou7df93d22020-01-27 13:44:06 -0800821 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau766cd2b2022-07-06 17:15:21 +0100822 defaultLayoutId = a.getResourceId(
823 R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800824 demoModeLayoutId = a.getResourceId(
825 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700826
827 numAllAppsColumns = a.getInt(
828 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
829 numDatabaseAllAppsColumns = a.getInt(
830 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
831
832 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800833 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700834 numDatabaseHotseatIcons = a.getInt(
835 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100836
Thales Lima425f6822022-05-10 13:44:58 -0300837 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
838 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
839 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
840 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
841 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
842 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
843 numColumns);
844 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
845 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
846 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700847
Vinit Nayakc7293172022-07-18 16:41:50 -0700848 inlineNavButtonsEndSpacing =
849 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
850 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300851
Sunny Goyal415f1732018-11-29 10:33:47 -0800852 numFolderRows = a.getInt(
853 R.styleable.GridDisplayOption_numFolderRows, numRows);
854 numFolderColumns = a.getInt(
855 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700856
Thales Limab35faed2022-09-05 16:30:01 -0300857 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
858 INVALID_RESOURCE_HANDLE);
859
Thales Limad852d652023-01-17 14:01:13 +0000860 cellStyle = a.getResourceId(R.styleable.GridDisplayOption_cellStyle,
861 R.style.CellStyleDefault);
862
Jon Mirandae126d722021-02-25 10:45:20 -0500863 isScalable = a.getBoolean(
864 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400865 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000866 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Jon Mirandae126d722021-02-25 10:45:20 -0500867
Alex Chau1c883d82021-12-01 18:43:10 +0000868 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
869 DEVICE_CATEGORY_ALL);
870 isEnabled = (deviceType == TYPE_PHONE
871 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
872 || (deviceType == TYPE_TABLET
873 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
874 || (deviceType == TYPE_MULTI_DISPLAY
875 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
876 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100877
Thales Lima11af7bc2022-08-12 15:24:54 +0100878 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
879 DONT_INLINE_QSB);
880 inlineQsb[INDEX_DEFAULT] =
881 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
882 inlineQsb[INDEX_LANDSCAPE] =
883 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
884 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
885 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
886 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
887 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
888 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
889 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
890
Sunny Goyal415f1732018-11-29 10:33:47 -0800891 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800892 extraAttrs = Themes.createValueMap(context, attrs,
893 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800894 }
895 }
896
Sunny Goyal19ff7282021-04-22 10:12:54 -0700897 @VisibleForTesting
898 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700899 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800900
Sunny Goyal415f1732018-11-29 10:33:47 -0800901 private final float minWidthDps;
902 private final float minHeightDps;
903 private final boolean canBeDefault;
904
Thales Lima83bedbf2021-10-05 17:47:39 +0100905 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100906
Thales Lima78d00ad2021-09-30 11:29:06 +0100907 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100908 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100909 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
910 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100911
Thales Lima78d00ad2021-09-30 11:29:06 +0100912 private final float[] iconSizes = new float[COUNT_SIZES];
913 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800914
Thales Limab7ef5692022-03-10 10:32:36 +0000915 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000916 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
917 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
918 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
919
Thales Lima1de4d552021-10-13 16:13:25 +0100920 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800921 this.grid = grid;
922
Thales Lima1de4d552021-10-13 16:13:25 +0100923 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800924
Sunny Goyal415f1732018-11-29 10:33:47 -0800925 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
926 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700927
Thales Lima1de4d552021-10-13 16:13:25 +0100928 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800929
Thales Lima83bedbf2021-10-05 17:47:39 +0100930 float x;
931 float y;
932
Thales Lima85c942f2021-12-31 13:04:20 +0000933 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
934 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100935 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000936
937 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
938 minCellSize[INDEX_DEFAULT].x);
939 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
940 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100941 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100942
Thales Lima85c942f2021-12-31 13:04:20 +0000943 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100944 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000945 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100946 minCellSize[INDEX_DEFAULT].y);
947 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
948
Thales Lima85c942f2021-12-31 13:04:20 +0000949 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100950 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000951 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100952 minCellSize[INDEX_DEFAULT].y);
953 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100954
Thales Lima85c942f2021-12-31 13:04:20 +0000955 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000956 float borderSpaceLandscape = a.getFloat(
957 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000958 float borderSpaceTwoPanelPortrait = a.getFloat(
959 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
960 float borderSpaceTwoPanelLandscape = a.getFloat(
961 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100962
Thales Lima85c942f2021-12-31 13:04:20 +0000963 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
964 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100965 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000966
967 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
968 borderSpaceLandscape);
969 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
970 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100971 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
972
973 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000974 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
975 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100976 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000977 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
978 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100979 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
980
981 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000982 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
983 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100984 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000985 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
986 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100987 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
988
Thales Limab7ef5692022-03-10 10:32:36 +0000989 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
990 minCellSize[INDEX_DEFAULT].x);
991 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
992 minCellSize[INDEX_DEFAULT].y);
993 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
994
995 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
996 allAppsCellSize[INDEX_DEFAULT].x);
997 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
998 allAppsCellSize[INDEX_DEFAULT].y);
999 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
1000
1001 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
1002 allAppsCellSize[INDEX_DEFAULT].x);
1003 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
1004 allAppsCellSize[INDEX_DEFAULT].y);
1005 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1006
1007 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
1008 allAppsCellSize[INDEX_DEFAULT].x);
1009 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
1010 allAppsCellSize[INDEX_DEFAULT].y);
1011 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
1012
Thales Lima080d8902022-03-28 15:30:29 +01001013 float allAppsBorderSpace = a.getFloat(
1014 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
1015 float allAppsBorderSpaceLandscape = a.getFloat(
1016 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
1017 allAppsBorderSpace);
1018 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001019 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +01001020 allAppsBorderSpace);
1021 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001022 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +01001023 allAppsBorderSpace);
1024
1025 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
1026 allAppsBorderSpace);
1027 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
1028 allAppsBorderSpace);
1029 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
1030
1031 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
1032 allAppsBorderSpaceLandscape);
1033 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1034 allAppsBorderSpaceLandscape);
1035 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1036
1037 x = a.getFloat(
1038 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1039 allAppsBorderSpaceTwoPanelPortrait);
1040 y = a.getFloat(
1041 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1042 allAppsBorderSpaceTwoPanelPortrait);
1043 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1044
1045 x = a.getFloat(
1046 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1047 allAppsBorderSpaceTwoPanelLandscape);
1048 y = a.getFloat(
1049 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1050 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001051 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001052
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001053 iconSizes[INDEX_DEFAULT] =
1054 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1055 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001056 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001057 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001058 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001059 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001060 iconSizes[INDEX_DEFAULT]);
1061 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001062 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001063 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001064
Thales Limabb7d3882021-12-22 12:56:29 +00001065 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1066 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001067 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1068 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001069 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001070 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1071 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1072 allAppsIconSizes[INDEX_DEFAULT]);
1073 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1074 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1075 allAppsIconSizes[INDEX_DEFAULT]);
1076
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001077 textSizes[INDEX_DEFAULT] =
1078 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1079 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001080 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001081 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001082 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001083 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001084 textSizes[INDEX_DEFAULT]);
1085 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001086 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001087 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001088
Thales Limabb7d3882021-12-22 12:56:29 +00001089 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1090 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1091 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1092 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1093 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1094 allAppsIconTextSizes[INDEX_DEFAULT]);
1095 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1096 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1097 allAppsIconTextSizes[INDEX_DEFAULT]);
1098
Thales Lima83bedbf2021-10-05 17:47:39 +01001099 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1100 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001101 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1102 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1103 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001104 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001105 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001106 horizontalMargin[INDEX_DEFAULT]);
1107 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001108 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001109 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001110
Thales Limab8c05952022-05-23 16:58:38 +01001111 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1112 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1113 ResourcesCompat.getFloat(context.getResources(),
1114 R.dimen.hotseat_bar_bottom_space_default));
1115 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1116 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1117 hotseatBarBottomSpace[INDEX_DEFAULT]);
1118 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1119 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1120 hotseatBarBottomSpace[INDEX_DEFAULT]);
1121 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1122 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1123 hotseatBarBottomSpace[INDEX_DEFAULT]);
1124
1125 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1126 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1127 ResourcesCompat.getFloat(context.getResources(),
1128 R.dimen.hotseat_qsb_space_default));
1129 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1130 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1131 hotseatQsbSpace[INDEX_DEFAULT]);
1132 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1133 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1134 hotseatQsbSpace[INDEX_DEFAULT]);
1135 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1136 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1137 hotseatQsbSpace[INDEX_DEFAULT]);
1138
Sunny Goyal415f1732018-11-29 10:33:47 -08001139 a.recycle();
1140 }
1141
1142 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001143 this(null);
1144 }
1145
1146 DisplayOption(GridOption grid) {
1147 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001148 minWidthDps = 0;
1149 minHeightDps = 0;
1150 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001151 for (int i = 0; i < COUNT_SIZES; i++) {
1152 iconSizes[i] = 0;
1153 textSizes[i] = 0;
1154 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001155 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001156 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001157 allAppsIconSizes[i] = 0;
1158 allAppsIconTextSizes[i] = 0;
1159 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001160 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001161 }
1162
1163 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001164 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001165 iconSizes[i] *= w;
1166 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001167 borderSpaces[i].x *= w;
1168 borderSpaces[i].y *= w;
1169 minCellSize[i].x *= w;
1170 minCellSize[i].y *= w;
1171 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001172 hotseatBarBottomSpace[i] *= w;
1173 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001174 allAppsCellSize[i].x *= w;
1175 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001176 allAppsIconSizes[i] *= w;
1177 allAppsIconTextSizes[i] *= w;
1178 allAppsBorderSpaces[i].x *= w;
1179 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001180 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001181
Sunny Goyal415f1732018-11-29 10:33:47 -08001182 return this;
1183 }
1184
1185 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001186 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001187 iconSizes[i] += p.iconSizes[i];
1188 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001189 borderSpaces[i].x += p.borderSpaces[i].x;
1190 borderSpaces[i].y += p.borderSpaces[i].y;
1191 minCellSize[i].x += p.minCellSize[i].x;
1192 minCellSize[i].y += p.minCellSize[i].y;
1193 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001194 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1195 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001196 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1197 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001198 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1199 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1200 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1201 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001202 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001203
Sunny Goyal415f1732018-11-29 10:33:47 -08001204 return this;
1205 }
1206 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001207}