blob: 4ac7f078bfcd0780ed5853609c06b556a0a4b2cf [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
Stefan Andonian4c9612b2023-02-22 00:00:03 +000019import static com.android.launcher3.LauncherPrefs.GRID_NAME;
Sunny Goyal19ff7282021-04-22 10:12:54 -070020import static com.android.launcher3.Utilities.dpiFromPx;
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 Chau1c883d82021-12-01 18:43:10 +000072import java.lang.annotation.Retention;
73import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070074import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070075import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080076import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070077import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070078
79public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070080
Hyunyoung Songc55a3502018-12-04 15:43:16 -080081 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070082 // We do not need any synchronization for this variable as its only written on UI thread.
83 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070084 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070085
Alex Chau1c883d82021-12-01 18:43:10 +000086 @Retention(RetentionPolicy.SOURCE)
87 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010088 public @interface DeviceType {}
89
Alex Chau1c883d82021-12-01 18:43:10 +000090 public static final int TYPE_PHONE = 0;
91 public static final int TYPE_MULTI_DISPLAY = 1;
92 public static final int TYPE_TABLET = 2;
93
Sunny Goyal53d7ee42015-05-22 12:25:45 -070094 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
95
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070096 // Constants that affects the interpolation curve between statically defined device profile
97 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080098 private static final float KNEARESTNEIGHBOR = 3;
99 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -0700100
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700101 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800102 private static final float WEIGHT_EFFICIENT = 100000f;
103
Thales Lima83bedbf2021-10-05 17:47:39 +0100104 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
105 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000106 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100107 static final int INDEX_DEFAULT = 0;
108 static final int INDEX_LANDSCAPE = 1;
109 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
110 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100111
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800112 /** These resources are used to override the device profile */
113 private static final String RES_GRID_NUM_ROWS = "grid_num_rows";
114 private static final String RES_GRID_NUM_COLUMNS = "grid_num_columns";
115 private static final String RES_GRID_ICON_SIZE_DP = "grid_icon_size_dp";
116
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700117 /**
118 * Number of icons per row and column in the workspace.
119 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700120 public int numRows;
121 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000122 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700123
124 /**
125 * Number of icons per row and column in the folder.
126 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700127 public int numFolderRows;
128 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100129 public float[] iconSize;
130 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700131 public int iconBitmapSize;
132 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000133 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700134
Thales Lima83bedbf2021-10-05 17:47:39 +0100135 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100136
Thales Lima83bedbf2021-10-05 17:47:39 +0100137 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300138 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100139
Thales Limab35faed2022-09-05 16:30:01 -0300140 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100141
Thales Limad852d652023-01-17 14:01:13 +0000142 public @StyleRes int cellStyle;
143
Thales Lima83bedbf2021-10-05 17:47:39 +0100144 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500145
Thales Limab7ef5692022-03-10 10:32:36 +0000146 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000147 public float[] allAppsIconSize;
148 public float[] allAppsIconTextSize;
149 public PointF[] allAppsBorderSpaces;
150
Sunny Goyal5bc18462019-01-07 15:13:39 -0800151 private SparseArray<TypedValue> mExtraAttrs;
152
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700153 /**
154 * Number of icons inside the hotseat area.
155 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800156 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700157
158 /**
159 * Number of icons inside the hotseat area that is stored in the database. This is greater than
160 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
161 * sizes that share the same DB.
162 */
163 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700164
Thales Lima425f6822022-05-10 13:44:58 -0300165 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100166 public float[] hotseatBarBottomSpace;
167 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300168
Jon Miranda6f7e9702019-09-16 14:44:14 -0700169 /**
170 * Number of columns in the all apps list.
171 */
172 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700173 public int numDatabaseAllAppsColumns;
Thales Limae9273ea2023-01-26 12:33:52 +0000174 public @StyleRes int allAppsStyle;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700175
Jon Mirandae126d722021-02-25 10:45:20 -0500176 /**
177 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
178 */
179 protected boolean isScalable;
Thales Limaae0d7ef2022-11-16 15:53:43 +0000180 @XmlRes
181 public int devicePaddingId = INVALID_RESOURCE_HANDLE;
Jon Mirandae126d722021-02-25 10:45:20 -0500182
Tracy Zhou7df93d22020-01-27 13:44:06 -0800183 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800184 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700185 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100186 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700187
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000188 /**
189 * An immutable list of supported profiles.
190 */
191 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700192
Sunny Goyal6f866092016-03-17 17:04:15 -0700193 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700194 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700195
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700196 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700197
Sunny Goyalf633ef52018-03-13 09:57:05 -0700198 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800199 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700200
Sunny Goyalbbf01842015-10-08 07:41:15 -0700201 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700202 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700203 String gridName = getCurrentGridName(context);
204 String newGridName = initGrid(context, gridName);
205 if (!newGridName.equals(gridName)) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000206 LauncherPrefs.get(context).put(GRID_NAME, newGridName);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700207 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700208 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700209
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700210 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100211 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000212 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
213 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100214 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700215 }
216 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700217 }
218
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700219 /**
220 * This constructor should NOT have any monitors by design.
221 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800222 public InvariantDeviceProfile(Context context, String gridName) {
223 String newName = initGrid(context, gridName);
224 if (newName == null || !newName.equals(gridName)) {
225 throw new IllegalArgumentException("Unknown grid name");
226 }
227 }
228
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700229 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800230 * This constructor should NOT have any monitors by design.
231 */
232 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700233 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100234 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235 String gridName = getCurrentGridName(context);
236
237 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000238 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
239 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700240 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000241 defaultInfo,
242 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
243 /*allowDisabledGrid=*/false),
244 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700245
Alex Chau661f02d2022-06-07 14:03:43 +0100246 Context displayContext = context.createDisplayContext(display);
247 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000248 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700249 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000250 myInfo,
251 getPredefinedDeviceProfiles(context, gridName, deviceType,
252 /*allowDisabledGrid=*/false),
253 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700254
255 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
256 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100257 result.iconSizes[INDEX_DEFAULT] =
258 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
259 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700260 result.iconSizes[i] = Math.min(
261 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000262 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700263
Thales Lima83bedbf2021-10-05 17:47:39 +0100264 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
265 COUNT_SIZES);
266 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
267 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500268
Alex Chau1c883d82021-12-01 18:43:10 +0000269 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800270 }
271
Alex Chau238aaee2021-10-06 16:15:24 +0100272 /**
273 * Reinitialize the current grid after a restore, where some grids might now be disabled.
274 */
275 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000276 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100277 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000278 String newGridName = initGrid(context, currentGridName);
279 String newDbFile = dbFile;
280 if (!newDbFile.equals(currentDbFile)) {
281 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100282 + ", migrating to: " + newGridName
283 + ", removing all other grid db files");
284 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
285 if (gridDbFile.equals(currentDbFile)) {
286 continue;
287 }
288 if (context.getDatabasePath(gridDbFile).delete()) {
289 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
290 }
291 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000292 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100293 }
294 }
295
Alex Chau1c883d82021-12-01 18:43:10 +0000296 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800297 int flagPhone = 1 << 0;
298 int flagTablet = 1 << 1;
299
300 int type = displayInfo.supportedBounds.stream()
301 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
302 .reduce(0, (a, b) -> a | b);
303 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
304 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000305 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800306 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000307 return TYPE_TABLET;
308 } else {
309 return TYPE_PHONE;
310 }
311 }
312
Tracy Zhou42255d22020-03-13 00:38:11 -0700313 public static String getCurrentGridName(Context context) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000314 return LauncherPrefs.get(context).get(GRID_NAME);
Tracy Zhou42255d22020-03-13 00:38:11 -0700315 }
316
Sunny Goyaleff44f32019-01-09 17:29:49 -0800317 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700318 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000319 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700320
321 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000322 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100323 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700324 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000325 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
326 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700327 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800328 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700329
Thales Limab67bfa72022-11-02 15:30:11 +0000330 @VisibleForTesting
331 public static String getDefaultGridName(Context context) {
332 return new InvariantDeviceProfile().initGrid(context, null);
333 }
334
Alex Chau1c883d82021-12-01 18:43:10 +0000335 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
336 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700337 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700338 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000339 numRows = closestProfile.numRows;
340 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000341 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000342 dbFile = closestProfile.dbFile;
343 defaultLayoutId = closestProfile.defaultLayoutId;
344 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300345
Sunny Goyalae190ff2020-04-14 00:19:01 +0000346 numFolderRows = closestProfile.numFolderRows;
347 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300348 folderStyle = closestProfile.folderStyle;
349
Thales Limad852d652023-01-17 14:01:13 +0000350 cellStyle = closestProfile.cellStyle;
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
Thales Limae9273ea2023-01-26 12:33:52 +0000383 allAppsStyle = closestProfile.allAppsStyle;
384
Sunny Goyal19ff7282021-04-22 10:12:54 -0700385 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000386 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700387 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500388
Thales Limab7ef5692022-03-10 10:32:36 +0000389 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000390 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
391 allAppsIconSize = displayOption.allAppsIconSizes;
392 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000393
Thales Lima11af7bc2022-08-12 15:24:54 +0100394 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000395
Sunny Goyalae190ff2020-04-14 00:19:01 +0000396 // If the partner customization apk contains any grid overrides, apply them
397 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700398 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700399
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000400 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700401 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700402 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700403 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000404 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chauab800f72022-12-13 17:46:06 +0000405 .setIsMultiDisplay(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000406 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700407 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000408 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700409
Sunny Goyal19ff7282021-04-22 10:12:54 -0700410 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
411 int displayWidth = bounds.bounds.width();
412 int displayHeight = bounds.bounds.height();
413 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700414
Sunny Goyal19ff7282021-04-22 10:12:54 -0700415 // We need to ensure that there is enough extra space in the wallpaper
416 // for the intended parallax effects
417 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300418 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
419 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700420 ? 2
421 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
422 defaultWallpaperSize.x =
423 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700424 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000425 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700426
Thales Lima6a590062022-11-22 15:52:49 +0000427 int numMinShownHotseatIconsForTablet = supportedProfiles
428 .stream()
429 .filter(deviceProfile -> deviceProfile.isTablet)
430 .mapToInt(deviceProfile -> deviceProfile.numShownHotseatIcons)
431 .min()
432 .orElse(0);
433
434 supportedProfiles
435 .stream()
436 .filter(deviceProfile -> deviceProfile.isTablet)
437 .forEach(deviceProfile -> {
438 deviceProfile.numShownHotseatIcons = numMinShownHotseatIconsForTablet;
439 deviceProfile.recalculateHotseatWidthAndBorderSpace();
440 });
441
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700442 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
443 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700444 }
445
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700446 public void addOnChangeListener(OnIDPChangeListener listener) {
447 mChangeListeners.add(listener);
448 }
449
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800450 public void removeOnChangeListener(OnIDPChangeListener listener) {
451 mChangeListeners.remove(listener);
452 }
453
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800454
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800455 public void setCurrentGrid(Context context, String gridName) {
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000456 LauncherPrefs.get(context).put(GRID_NAME, gridName);
457 MAIN_EXECUTOR.execute(() -> onConfigChanged(context.getApplicationContext()));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800458 }
459
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700460 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700461 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000462 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
463 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700464 }
465
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700466 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700467 Object[] oldState = toModelState();
468
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700469 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700470 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700471 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700472
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700473 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700474 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700475 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700476 }
477 }
478
Alex Chau1c883d82021-12-01 18:43:10 +0000479 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
480 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800481 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100482
Alex Chau1c883d82021-12-01 18:43:10 +0000483 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700484 final int depth = parser.getDepth();
485 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700486 while (((type = parser.next()) != XmlPullParser.END_TAG ||
487 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800488 if ((type == XmlPullParser.START_TAG)
489 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800490
Alex Chau1c883d82021-12-01 18:43:10 +0000491 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
492 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100493 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100494 final int displayDepth = parser.getDepth();
495 while (((type = parser.next()) != XmlPullParser.END_TAG
496 || parser.getDepth() > displayDepth)
497 && type != XmlPullParser.END_DOCUMENT) {
498 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
499 parser.getName())) {
500 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100501 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100502 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800503 }
504 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700505 }
506 }
sfufa@google.comde013292021-09-21 18:22:44 -0700507 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700508 throw new RuntimeException(e);
509 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800510
Sunny Goyalae190ff2020-04-14 00:19:01 +0000511 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800512 if (!TextUtils.isEmpty(gridName)) {
513 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100514 if (gridName.equals(option.grid.name)
515 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000516 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800517 }
518 }
519 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000520 if (filteredProfiles.isEmpty()) {
521 // No grid found, use the default options
522 for (DisplayOption option : profiles) {
523 if (option.canBeDefault) {
524 filteredProfiles.add(option);
525 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800526 }
527 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000528 if (filteredProfiles.isEmpty()) {
529 throw new RuntimeException("No display option with canBeDefault=true");
530 }
531 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700532 }
533
Thales Lima7ec83822021-08-05 13:32:10 +0100534 /**
535 * @return all the grid options that can be shown on the device
536 */
537 public List<GridOption> parseAllGridOptions(Context context) {
538 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100539
Alex Chau1c883d82021-12-01 18:43:10 +0000540 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100541 final int depth = parser.getDepth();
542 int type;
543 while (((type = parser.next()) != XmlPullParser.END_TAG
544 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
545 if ((type == XmlPullParser.START_TAG)
546 && GridOption.TAG_NAME.equals(parser.getName())) {
547 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000548 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100549 if (option.isEnabled) {
550 result.add(option);
551 }
552 }
553 }
554 } catch (IOException | XmlPullParserException e) {
555 Log.e(TAG, "Error parsing device profile", e);
556 return Collections.emptyList();
557 }
558 return result;
559 }
560
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700561 private int getLauncherIconDensity(int requiredSize) {
562 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700563 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700564 DisplayMetrics.DENSITY_LOW,
565 DisplayMetrics.DENSITY_MEDIUM,
566 DisplayMetrics.DENSITY_TV,
567 DisplayMetrics.DENSITY_HIGH,
568 DisplayMetrics.DENSITY_XHIGH,
569 DisplayMetrics.DENSITY_XXHIGH,
570 DisplayMetrics.DENSITY_XXXHIGH
571 };
572
573 int density = DisplayMetrics.DENSITY_XXXHIGH;
574 for (int i = densityBuckets.length - 1; i >= 0; i--) {
575 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
576 / DisplayMetrics.DENSITY_DEFAULT;
577 if (expectedSize >= requiredSize) {
578 density = densityBuckets[i];
579 }
580 }
581
582 return density;
583 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700584
Adam Cohen2e6da152015-05-06 11:42:25 -0700585 /**
586 * Apply any Partner customization grid overrides.
587 *
588 * Currently we support: all apps row / column count.
589 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700590 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
591 Partner p = Partner.get(context.getPackageManager());
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800592 if (p == null) {
593 return;
594 }
595 try {
596 int numRows = p.getIntValue(RES_GRID_NUM_ROWS, -1);
597 int numColumns = p.getIntValue(RES_GRID_NUM_COLUMNS, -1);
598 float iconSizePx = p.getDimenValue(RES_GRID_ICON_SIZE_DP, -1);
599
600 if (numRows > 0 && numColumns > 0) {
601 this.numRows = numRows;
602 this.numColumns = numColumns;
603 }
604 if (iconSizePx > 0) {
605 this.iconSize[InvariantDeviceProfile.INDEX_DEFAULT] =
606 Utilities.dpiFromPx(iconSizePx, dm.densityDpi);
607 }
608 } catch (Resources.NotFoundException ex) {
609 Log.e(TAG, "Invalid Partner grid resource!", ex);
Adam Cohen2e6da152015-05-06 11:42:25 -0700610 }
611 }
612
Sunny Goyal415f1732018-11-29 10:33:47 -0800613 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700614 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700615 }
616
Sunny Goyal19ff7282021-04-22 10:12:54 -0700617 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000618 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700619 int minWidthPx = Integer.MAX_VALUE;
620 int minHeightPx = Integer.MAX_VALUE;
621 for (WindowBounds bounds : displayInfo.supportedBounds) {
622 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000623 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700624 // For split displays, take half width per page
625 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
626 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700627
Sunny Goyal19ff7282021-04-22 10:12:54 -0700628 } else if (!isTablet && bounds.isLandscape()) {
629 // We will use transposed layout in this case
630 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
631 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
632 } else {
633 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
634 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
635 }
636 }
637
Thales Lima425f6822022-05-10 13:44:58 -0300638 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
639 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700640
641 // Sort the profiles based on the closeness to the device size
642 Collections.sort(points, (a, b) ->
643 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
644 dist(width, height, b.minWidthDps, b.minHeightDps)));
645
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700646 DisplayOption closestPoint = points.get(0);
647 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700648 float weights = 0;
649
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700650 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
651 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700652 }
653
654 DisplayOption out = new DisplayOption(closestOption);
655 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700656 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700657 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
658 weights += w;
659 out.add(new DisplayOption().add(p).multiply(w));
660 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700661 out.multiply(1.0f / weights);
662
Thales Lima6e0005a2021-10-27 15:53:41 +0100663 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700664 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100665 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
666 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700667 }
668
669 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700670 }
671
Sunny Goyal27835952017-01-13 12:15:53 -0800672 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700673 Resources res = context.getResources();
674 Configuration config = context.getResources().getConfiguration();
675
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400676 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
677 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100678 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
679
Alex Chaud3e8cc42022-05-03 17:45:34 +0100680 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400681 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700682
Sunny Goyal187b16c2022-03-01 16:53:23 -0800683 /**
684 * Returns the device profile matching the provided screen configuration
685 */
686 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700687 DeviceProfile bestMatch = supportedProfiles.get(0);
688 float minDiff = Float.MAX_VALUE;
689
690 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400691 float diff = Math.abs(profile.widthPx - screenWidth)
692 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700693 if (diff < minDiff) {
694 minDiff = diff;
695 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800696 } else if (diff == minDiff && profile.rotationHint == rotation) {
697 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700698 }
699 }
700 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800701 }
702
Sunny Goyal415f1732018-11-29 10:33:47 -0800703 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700704 float d = dist(x0, y0, x1, y1);
705 if (Float.compare(d, 0f) == 0) {
706 return Float.POSITIVE_INFINITY;
707 }
708 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
709 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700710
711 /**
712 * As a ratio of screen height, the total distance we want the parallax effect to span
713 * horizontally
714 */
715 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
716 float aspectRatio = width / (float) height;
717
718 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
719 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
720 // We will use these two data points to extrapolate how much the wallpaper parallax effect
721 // to span (ie travel) at any aspect ratio:
722
sfufa@google.comde013292021-09-21 18:22:44 -0700723 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
724 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700725 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
726 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
727
728 // To find out the desired width at different aspect ratios, we use the following two
729 // formulas, where the coefficient on x is the aspect ratio (width/height):
730 // (16/10)x + y = 1.5
731 // (10/16)x + y = 1.2
732 // We solve for x and y and end up with a final formula:
733 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700734 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
735 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700736 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
737 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
738 return x * aspectRatio + y;
739 }
740
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700741 public interface OnIDPChangeListener {
742
Sunny Goyalb47172b2021-05-03 19:59:51 -0700743 /**
744 * Called when the device provide changes
745 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700746 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700747 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800748
749
Sunny Goyaleff44f32019-01-09 17:29:49 -0800750 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800751
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800752 public static final String TAG_NAME = "grid-option";
753
Alex Chau1c883d82021-12-01 18:43:10 +0000754 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
755 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
756 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
757 private static final int DEVICE_CATEGORY_ALL =
758 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
759
Thales Lima11af7bc2022-08-12 15:24:54 +0100760 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
761 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
762 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
763 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
764 private static final int DONT_INLINE_QSB = 0;
765
Sunny Goyaleff44f32019-01-09 17:29:49 -0800766 public final String name;
767 public final int numRows;
768 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000769 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100770 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800771
772 private final int numFolderRows;
773 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300774 private final @StyleRes int folderStyle;
Thales Limad852d652023-01-17 14:01:13 +0000775 private final @StyleRes int cellStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800776
Thales Limae9273ea2023-01-26 12:33:52 +0000777 private final @StyleRes int allAppsStyle;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700778 private final int numAllAppsColumns;
779 private final int numDatabaseAllAppsColumns;
780 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700781 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100782
Thales Lima425f6822022-05-10 13:44:58 -0300783 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800784
Thales Lima11af7bc2022-08-12 15:24:54 +0100785 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
786
Thales Limab35faed2022-09-05 16:30:01 -0300787 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800788 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000789
Sunny Goyal415f1732018-11-29 10:33:47 -0800790 private final int defaultLayoutId;
791 private final int demoModeLayoutId;
792
Jon Mirandae126d722021-02-25 10:45:20 -0500793 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400794 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500795
Sunny Goyal5bc18462019-01-07 15:13:39 -0800796 private final SparseArray<TypedValue> extraAttrs;
797
Alex Chau1c883d82021-12-01 18:43:10 +0000798 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800799 TypedArray a = context.obtainStyledAttributes(
800 attrs, R.styleable.GridDisplayOption);
801 name = a.getString(R.styleable.GridDisplayOption_name);
802 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
803 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000804 numSearchContainerColumns = a.getInt(
805 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800806
Tracy Zhou7df93d22020-01-27 13:44:06 -0800807 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau766cd2b2022-07-06 17:15:21 +0100808 defaultLayoutId = a.getResourceId(
809 R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800810 demoModeLayoutId = a.getResourceId(
811 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700812
Thales Limae9273ea2023-01-26 12:33:52 +0000813 allAppsStyle = a.getResourceId(R.styleable.GridDisplayOption_allAppsStyle,
814 R.style.AllAppsStyleDefault);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700815 numAllAppsColumns = a.getInt(
816 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
817 numDatabaseAllAppsColumns = a.getInt(
818 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
819
820 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800821 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700822 numDatabaseHotseatIcons = a.getInt(
823 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100824
Thales Lima425f6822022-05-10 13:44:58 -0300825 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
826 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
827 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
828 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
829 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
830 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
831 numColumns);
832 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
833 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
834 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700835
Vinit Nayakc7293172022-07-18 16:41:50 -0700836 inlineNavButtonsEndSpacing =
837 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
838 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300839
Sunny Goyal415f1732018-11-29 10:33:47 -0800840 numFolderRows = a.getInt(
841 R.styleable.GridDisplayOption_numFolderRows, numRows);
842 numFolderColumns = a.getInt(
843 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700844
Thales Limab35faed2022-09-05 16:30:01 -0300845 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
846 INVALID_RESOURCE_HANDLE);
847
Thales Limad852d652023-01-17 14:01:13 +0000848 cellStyle = a.getResourceId(R.styleable.GridDisplayOption_cellStyle,
849 R.style.CellStyleDefault);
850
Jon Mirandae126d722021-02-25 10:45:20 -0500851 isScalable = a.getBoolean(
852 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400853 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000854 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Jon Mirandae126d722021-02-25 10:45:20 -0500855
Alex Chau1c883d82021-12-01 18:43:10 +0000856 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
857 DEVICE_CATEGORY_ALL);
858 isEnabled = (deviceType == TYPE_PHONE
859 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
860 || (deviceType == TYPE_TABLET
861 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
862 || (deviceType == TYPE_MULTI_DISPLAY
863 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
864 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100865
Thales Lima11af7bc2022-08-12 15:24:54 +0100866 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
867 DONT_INLINE_QSB);
868 inlineQsb[INDEX_DEFAULT] =
869 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
870 inlineQsb[INDEX_LANDSCAPE] =
871 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
872 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
873 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
874 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
875 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
876 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
877 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
878
Sunny Goyal415f1732018-11-29 10:33:47 -0800879 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800880 extraAttrs = Themes.createValueMap(context, attrs,
881 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800882 }
883 }
884
Sunny Goyal19ff7282021-04-22 10:12:54 -0700885 @VisibleForTesting
886 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700887 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800888
Sunny Goyal415f1732018-11-29 10:33:47 -0800889 private final float minWidthDps;
890 private final float minHeightDps;
891 private final boolean canBeDefault;
892
Thales Lima83bedbf2021-10-05 17:47:39 +0100893 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100894
Thales Lima78d00ad2021-09-30 11:29:06 +0100895 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100896 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100897 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
898 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100899
Thales Lima78d00ad2021-09-30 11:29:06 +0100900 private final float[] iconSizes = new float[COUNT_SIZES];
901 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800902
Thales Limab7ef5692022-03-10 10:32:36 +0000903 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000904 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
905 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
906 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
907
Thales Lima1de4d552021-10-13 16:13:25 +0100908 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800909 this.grid = grid;
910
Thales Lima1de4d552021-10-13 16:13:25 +0100911 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800912
Sunny Goyal415f1732018-11-29 10:33:47 -0800913 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
914 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700915
Thales Lima1de4d552021-10-13 16:13:25 +0100916 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800917
Thales Lima83bedbf2021-10-05 17:47:39 +0100918 float x;
919 float y;
920
Thales Lima85c942f2021-12-31 13:04:20 +0000921 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
922 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100923 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000924
925 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
926 minCellSize[INDEX_DEFAULT].x);
927 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
928 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100929 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100930
Thales Lima85c942f2021-12-31 13:04:20 +0000931 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100932 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000933 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100934 minCellSize[INDEX_DEFAULT].y);
935 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
936
Thales Lima85c942f2021-12-31 13:04:20 +0000937 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100938 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000939 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100940 minCellSize[INDEX_DEFAULT].y);
941 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100942
Thales Lima85c942f2021-12-31 13:04:20 +0000943 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000944 float borderSpaceLandscape = a.getFloat(
945 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000946 float borderSpaceTwoPanelPortrait = a.getFloat(
947 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
948 float borderSpaceTwoPanelLandscape = a.getFloat(
949 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100950
Thales Lima85c942f2021-12-31 13:04:20 +0000951 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
952 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100953 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000954
955 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
956 borderSpaceLandscape);
957 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
958 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100959 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
960
961 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000962 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
963 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100964 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000965 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
966 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100967 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
968
969 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000970 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
971 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100972 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000973 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
974 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100975 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
976
Thales Limab7ef5692022-03-10 10:32:36 +0000977 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
978 minCellSize[INDEX_DEFAULT].x);
979 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
980 minCellSize[INDEX_DEFAULT].y);
981 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
982
983 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
984 allAppsCellSize[INDEX_DEFAULT].x);
985 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
986 allAppsCellSize[INDEX_DEFAULT].y);
987 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
988
989 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
990 allAppsCellSize[INDEX_DEFAULT].x);
991 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
992 allAppsCellSize[INDEX_DEFAULT].y);
993 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
994
995 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
996 allAppsCellSize[INDEX_DEFAULT].x);
997 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
998 allAppsCellSize[INDEX_DEFAULT].y);
999 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
1000
Thales Lima080d8902022-03-28 15:30:29 +01001001 float allAppsBorderSpace = a.getFloat(
1002 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
1003 float allAppsBorderSpaceLandscape = a.getFloat(
1004 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
1005 allAppsBorderSpace);
1006 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001007 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +01001008 allAppsBorderSpace);
1009 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +00001010 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +01001011 allAppsBorderSpace);
1012
1013 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
1014 allAppsBorderSpace);
1015 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
1016 allAppsBorderSpace);
1017 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
1018
1019 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
1020 allAppsBorderSpaceLandscape);
1021 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1022 allAppsBorderSpaceLandscape);
1023 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1024
1025 x = a.getFloat(
1026 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1027 allAppsBorderSpaceTwoPanelPortrait);
1028 y = a.getFloat(
1029 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1030 allAppsBorderSpaceTwoPanelPortrait);
1031 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1032
1033 x = a.getFloat(
1034 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1035 allAppsBorderSpaceTwoPanelLandscape);
1036 y = a.getFloat(
1037 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1038 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001039 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001040
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001041 iconSizes[INDEX_DEFAULT] =
1042 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1043 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001044 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001045 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001046 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001047 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001048 iconSizes[INDEX_DEFAULT]);
1049 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001050 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001051 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001052
Thales Limabb7d3882021-12-22 12:56:29 +00001053 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1054 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001055 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1056 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001057 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001058 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1059 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1060 allAppsIconSizes[INDEX_DEFAULT]);
1061 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1062 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1063 allAppsIconSizes[INDEX_DEFAULT]);
1064
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001065 textSizes[INDEX_DEFAULT] =
1066 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1067 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001068 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001069 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001070 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001071 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001072 textSizes[INDEX_DEFAULT]);
1073 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001074 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001075 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001076
Thales Limabb7d3882021-12-22 12:56:29 +00001077 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1078 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1079 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1080 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1081 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1082 allAppsIconTextSizes[INDEX_DEFAULT]);
1083 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1084 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1085 allAppsIconTextSizes[INDEX_DEFAULT]);
1086
Thales Lima83bedbf2021-10-05 17:47:39 +01001087 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1088 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001089 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1090 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1091 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001092 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001093 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001094 horizontalMargin[INDEX_DEFAULT]);
1095 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001096 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001097 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001098
Thales Limab8c05952022-05-23 16:58:38 +01001099 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1100 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1101 ResourcesCompat.getFloat(context.getResources(),
1102 R.dimen.hotseat_bar_bottom_space_default));
1103 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1104 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1105 hotseatBarBottomSpace[INDEX_DEFAULT]);
1106 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1107 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1108 hotseatBarBottomSpace[INDEX_DEFAULT]);
1109 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1110 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1111 hotseatBarBottomSpace[INDEX_DEFAULT]);
1112
1113 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1114 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1115 ResourcesCompat.getFloat(context.getResources(),
1116 R.dimen.hotseat_qsb_space_default));
1117 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1118 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1119 hotseatQsbSpace[INDEX_DEFAULT]);
1120 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1121 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1122 hotseatQsbSpace[INDEX_DEFAULT]);
1123 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1124 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1125 hotseatQsbSpace[INDEX_DEFAULT]);
1126
Sunny Goyal415f1732018-11-29 10:33:47 -08001127 a.recycle();
1128 }
1129
1130 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001131 this(null);
1132 }
1133
1134 DisplayOption(GridOption grid) {
1135 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001136 minWidthDps = 0;
1137 minHeightDps = 0;
1138 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001139 for (int i = 0; i < COUNT_SIZES; i++) {
1140 iconSizes[i] = 0;
1141 textSizes[i] = 0;
1142 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001143 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001144 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001145 allAppsIconSizes[i] = 0;
1146 allAppsIconTextSizes[i] = 0;
1147 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001148 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001149 }
1150
1151 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001152 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001153 iconSizes[i] *= w;
1154 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001155 borderSpaces[i].x *= w;
1156 borderSpaces[i].y *= w;
1157 minCellSize[i].x *= w;
1158 minCellSize[i].y *= w;
1159 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001160 hotseatBarBottomSpace[i] *= w;
1161 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001162 allAppsCellSize[i].x *= w;
1163 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001164 allAppsIconSizes[i] *= w;
1165 allAppsIconTextSizes[i] *= w;
1166 allAppsBorderSpaces[i].x *= w;
1167 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001168 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001169
Sunny Goyal415f1732018-11-29 10:33:47 -08001170 return this;
1171 }
1172
1173 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001174 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001175 iconSizes[i] += p.iconSizes[i];
1176 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001177 borderSpaces[i].x += p.borderSpaces[i].x;
1178 borderSpaces[i].y += p.borderSpaces[i].y;
1179 minCellSize[i].x += p.minCellSize[i].x;
1180 minCellSize[i].y += p.minCellSize[i].y;
1181 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001182 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1183 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001184 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1185 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001186 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1187 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1188 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1189 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001190 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001191
Sunny Goyal415f1732018-11-29 10:33:47 -08001192 return this;
1193 }
1194 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001195}