blob: bb7c0a4ce49ea10d433c9014e9eb97e5e5e13d70 [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;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010020import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Sunny Goyal35c7b192021-04-20 16:51:10 -070021import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Alex Chau6ed408f2022-03-04 12:58:05 +000022import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE;
Sunny Goyal19ff7282021-04-22 10:12:54 -070023import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080024import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070025
Sunny Goyalc6205602015-05-21 20:46:33 -070026import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070028import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070029import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080030import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080031import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070032import android.content.res.TypedArray;
33import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010035import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070036import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080037import android.text.TextUtils;
38import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070039import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010040import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080041import android.util.SparseArray;
42import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070043import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080044import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070045
Alex Chau1c883d82021-12-01 18:43:10 +000046import androidx.annotation.IntDef;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070047import androidx.annotation.Nullable;
48import androidx.annotation.VisibleForTesting;
Thales Limab8c05952022-05-23 16:58:38 +010049import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070050
Sunny Goyal65190ae2022-08-02 14:16:26 -070051import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070052import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010053import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070054import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070055import com.android.launcher3.util.DisplayController;
56import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080057import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070058import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080059import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070060import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080061import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070062
Sunny Goyal819e1932016-07-07 16:43:58 -070063import org.xmlpull.v1.XmlPullParser;
64import org.xmlpull.v1.XmlPullParserException;
65
66import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000067import java.lang.annotation.Retention;
68import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070069import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070070import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080071import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070072import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070073
74public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070075
Hyunyoung Songc55a3502018-12-04 15:43:16 -080076 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070077 // We do not need any synchronization for this variable as its only written on UI thread.
78 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070079 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070080
Alex Chau1c883d82021-12-01 18:43:10 +000081 @Retention(RetentionPolicy.SOURCE)
82 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010083 public @interface DeviceType {}
84
Alex Chau1c883d82021-12-01 18:43:10 +000085 public static final int TYPE_PHONE = 0;
86 public static final int TYPE_MULTI_DISPLAY = 1;
87 public static final int TYPE_TABLET = 2;
88
Hyunyoung Songc55a3502018-12-04 15:43:16 -080089 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080090
Sunny Goyal53d7ee42015-05-22 12:25:45 -070091 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
92
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070093 // Constants that affects the interpolation curve between statically defined device profile
94 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080095 private static final float KNEARESTNEIGHBOR = 3;
96 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070097
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080099 private static final float WEIGHT_EFFICIENT = 100000f;
100
Thales Lima83bedbf2021-10-05 17:47:39 +0100101 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
102 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000103 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100104 static final int INDEX_DEFAULT = 0;
105 static final int INDEX_LANDSCAPE = 1;
106 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
107 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100108
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700109 /**
110 * Number of icons per row and column in the workspace.
111 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700112 public int numRows;
113 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000114 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700115
116 /**
117 * Number of icons per row and column in the folder.
118 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700119 public int numFolderRows;
120 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100121 public float[] iconSize;
122 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700123 public int iconBitmapSize;
124 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000125 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700126
Thales Lima83bedbf2021-10-05 17:47:39 +0100127 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100128
Thales Lima83bedbf2021-10-05 17:47:39 +0100129 public PointF[] borderSpaces;
Vinit Nayakc7293172022-07-18 16:41:50 -0700130 public int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100131
Thales Limaa08a4432022-08-09 09:55:17 +0100132 public PointF folderBorderSpaces;
133 public PointF folderCellSize;
134 public float folderTopPadding;
135
Thales Lima83bedbf2021-10-05 17:47:39 +0100136 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500137
Thales Limab7ef5692022-03-10 10:32:36 +0000138 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000139 public float[] allAppsIconSize;
140 public float[] allAppsIconTextSize;
141 public PointF[] allAppsBorderSpaces;
142
Sunny Goyal5bc18462019-01-07 15:13:39 -0800143 private SparseArray<TypedValue> mExtraAttrs;
144
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700145 /**
146 * Number of icons inside the hotseat area.
147 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800148 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700149
150 /**
151 * Number of icons inside the hotseat area that is stored in the database. This is greater than
152 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
153 * sizes that share the same DB.
154 */
155 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700156
Thales Lima425f6822022-05-10 13:44:58 -0300157 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100158 public float[] hotseatBarBottomSpace;
159 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300160
Jon Miranda6f7e9702019-09-16 14:44:14 -0700161 /**
162 * Number of columns in the all apps list.
163 */
164 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700165 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700166
Jon Mirandae126d722021-02-25 10:45:20 -0500167 /**
168 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
169 */
170 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400171 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500172
Tracy Zhou7df93d22020-01-27 13:44:06 -0800173 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800174 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700175 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100176 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700177
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000178 /**
179 * An immutable list of supported profiles.
180 */
181 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700182
sfufa@google.comde013292021-09-21 18:22:44 -0700183 @Nullable
184 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500185
Sunny Goyal6f866092016-03-17 17:04:15 -0700186 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700187 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700188
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700189 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700190
Sunny Goyalf633ef52018-03-13 09:57:05 -0700191 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800192 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700193
Sunny Goyalbbf01842015-10-08 07:41:15 -0700194 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700195 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700196 String gridName = getCurrentGridName(context);
197 String newGridName = initGrid(context, gridName);
198 if (!newGridName.equals(gridName)) {
199 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
200 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700201 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700202
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700203 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100204 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000205 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
206 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100207 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700208 }
209 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700210 }
211
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700212 /**
213 * This constructor should NOT have any monitors by design.
214 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800215 public InvariantDeviceProfile(Context context, String gridName) {
216 String newName = initGrid(context, gridName);
217 if (newName == null || !newName.equals(gridName)) {
218 throw new IllegalArgumentException("Unknown grid name");
219 }
220 }
221
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700222 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800223 * This constructor should NOT have any monitors by design.
224 */
225 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700226 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100227 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700228 String gridName = getCurrentGridName(context);
229
230 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000231 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
232 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700233 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000234 defaultInfo,
235 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
236 /*allowDisabledGrid=*/false),
237 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700238
Alex Chau661f02d2022-06-07 14:03:43 +0100239 Context displayContext = context.createDisplayContext(display);
240 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000241 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700242 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000243 myInfo,
244 getPredefinedDeviceProfiles(context, gridName, deviceType,
245 /*allowDisabledGrid=*/false),
246 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700247
248 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
249 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100250 result.iconSizes[INDEX_DEFAULT] =
251 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
252 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700253 result.iconSizes[i] = Math.min(
254 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000255 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700256
Thales Lima83bedbf2021-10-05 17:47:39 +0100257 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
258 COUNT_SIZES);
259 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
260 COUNT_SIZES);
Thales Lima12d0eff2022-03-25 17:06:11 +0000261 System.arraycopy(defaultDisplayOption.inlineQsb, 0, result.inlineQsb, 0,
262 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500263
Alex Chau1c883d82021-12-01 18:43:10 +0000264 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800265 }
266
Alex Chau238aaee2021-10-06 16:15:24 +0100267 /**
268 * Reinitialize the current grid after a restore, where some grids might now be disabled.
269 */
270 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000271 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100272 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000273 String newGridName = initGrid(context, currentGridName);
274 String newDbFile = dbFile;
275 if (!newDbFile.equals(currentDbFile)) {
276 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100277 + ", migrating to: " + newGridName
278 + ", removing all other grid db files");
279 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
280 if (gridDbFile.equals(currentDbFile)) {
281 continue;
282 }
283 if (context.getDatabasePath(gridDbFile).delete()) {
284 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
285 }
286 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000287 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100288 }
289 }
290
Alex Chau1c883d82021-12-01 18:43:10 +0000291 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800292 int flagPhone = 1 << 0;
293 int flagTablet = 1 << 1;
294
295 int type = displayInfo.supportedBounds.stream()
296 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
297 .reduce(0, (a, b) -> a | b);
298 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
299 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000300 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800301 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000302 return TYPE_TABLET;
303 } else {
304 return TYPE_PHONE;
305 }
306 }
307
Tracy Zhou42255d22020-03-13 00:38:11 -0700308 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700309 return Utilities.isGridOptionsEnabled(context)
310 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700311 }
312
Sunny Goyaleff44f32019-01-09 17:29:49 -0800313 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700314 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000315 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700316
317 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000318 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100319 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700320 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000321 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
322 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700323 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800324 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700325
Alex Chau1c883d82021-12-01 18:43:10 +0000326 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
327 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700328 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700329 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000330 numRows = closestProfile.numRows;
331 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000332 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000333 dbFile = closestProfile.dbFile;
334 defaultLayoutId = closestProfile.defaultLayoutId;
335 demoModeLayoutId = closestProfile.demoModeLayoutId;
336 numFolderRows = closestProfile.numFolderRows;
337 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500338 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400339 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000340 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000341
Vinit Nayakc7293172022-07-18 16:41:50 -0700342 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
343
Sunny Goyalae190ff2020-04-14 00:19:01 +0000344 mExtraAttrs = closestProfile.extraAttrs;
345
Thales Lima83bedbf2021-10-05 17:47:39 +0100346 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100347 float maxIconSize = iconSize[0];
348 for (int i = 1; i < iconSize.length; i++) {
349 maxIconSize = Math.max(maxIconSize, iconSize[i]);
350 }
351 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000352 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
353
Thales Lima83bedbf2021-10-05 17:47:39 +0100354 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700355
Thales Lima83bedbf2021-10-05 17:47:39 +0100356 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100357
Thales Lima83bedbf2021-10-05 17:47:39 +0100358 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100359
360 folderBorderSpaces = displayOption.folderBorderSpaces;
361 folderCellSize = displayOption.folderCellSize;
362 folderTopPadding = displayOption.folderTopPadding;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700363
Thales Limad90faab2021-09-21 17:18:47 +0100364 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100365
Sunny Goyal19ff7282021-04-22 10:12:54 -0700366 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000367 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700368 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300369 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100370 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
371 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700372
373 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000374 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700375 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500376
Thales Limab7ef5692022-03-10 10:32:36 +0000377 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000378 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
379 allAppsIconSize = displayOption.allAppsIconSizes;
380 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100381 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000382 allAppsIconSize = iconSize;
383 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000384 }
385
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400386 if (devicePaddingId != 0) {
387 devicePaddings = new DevicePaddings(context, devicePaddingId);
388 }
389
Thales Lima12d0eff2022-03-25 17:06:11 +0000390 inlineQsb = displayOption.inlineQsb;
391
Sunny Goyalae190ff2020-04-14 00:19:01 +0000392 // If the partner customization apk contains any grid overrides, apply them
393 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700394 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700395
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000396 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700397 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700398 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700399 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000400 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000401 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000402 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700403 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000404 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700405
Sunny Goyal19ff7282021-04-22 10:12:54 -0700406 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
407 int displayWidth = bounds.bounds.width();
408 int displayHeight = bounds.bounds.height();
409 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700410
Sunny Goyal19ff7282021-04-22 10:12:54 -0700411 // We need to ensure that there is enough extra space in the wallpaper
412 // for the intended parallax effects
413 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300414 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
415 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700416 ? 2
417 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
418 defaultWallpaperSize.x =
419 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700420 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000421 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700422
423 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
424 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700425 }
426
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700427 public void addOnChangeListener(OnIDPChangeListener listener) {
428 mChangeListeners.add(listener);
429 }
430
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800431 public void removeOnChangeListener(OnIDPChangeListener listener) {
432 mChangeListeners.remove(listener);
433 }
434
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800435
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800436 public void setCurrentGrid(Context context, String gridName) {
437 Context appContext = context.getApplicationContext();
438 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700439 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800440 }
441
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700442 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700443 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000444 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
445 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700446 }
447
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700448 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700449 Object[] oldState = toModelState();
450
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700451 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700452 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700453 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700454
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700455 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700456 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700457 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700458 }
459 }
460
Alex Chau1c883d82021-12-01 18:43:10 +0000461 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
462 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800463 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100464
Alex Chau1c883d82021-12-01 18:43:10 +0000465 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700466 final int depth = parser.getDepth();
467 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700468 while (((type = parser.next()) != XmlPullParser.END_TAG ||
469 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800470 if ((type == XmlPullParser.START_TAG)
471 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800472
Alex Chau1c883d82021-12-01 18:43:10 +0000473 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
474 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100475 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100476 final int displayDepth = parser.getDepth();
477 while (((type = parser.next()) != XmlPullParser.END_TAG
478 || parser.getDepth() > displayDepth)
479 && type != XmlPullParser.END_DOCUMENT) {
480 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
481 parser.getName())) {
482 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100483 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100484 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800485 }
486 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700487 }
488 }
sfufa@google.comde013292021-09-21 18:22:44 -0700489 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700490 throw new RuntimeException(e);
491 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800492
Sunny Goyalae190ff2020-04-14 00:19:01 +0000493 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800494 if (!TextUtils.isEmpty(gridName)) {
495 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100496 if (gridName.equals(option.grid.name)
497 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000498 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800499 }
500 }
501 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000502 if (filteredProfiles.isEmpty()) {
503 // No grid found, use the default options
504 for (DisplayOption option : profiles) {
505 if (option.canBeDefault) {
506 filteredProfiles.add(option);
507 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800508 }
509 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000510 if (filteredProfiles.isEmpty()) {
511 throw new RuntimeException("No display option with canBeDefault=true");
512 }
513 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700514 }
515
Thales Lima7ec83822021-08-05 13:32:10 +0100516 /**
517 * @return all the grid options that can be shown on the device
518 */
519 public List<GridOption> parseAllGridOptions(Context context) {
520 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100521
Alex Chau1c883d82021-12-01 18:43:10 +0000522 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100523 final int depth = parser.getDepth();
524 int type;
525 while (((type = parser.next()) != XmlPullParser.END_TAG
526 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
527 if ((type == XmlPullParser.START_TAG)
528 && GridOption.TAG_NAME.equals(parser.getName())) {
529 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000530 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100531 if (option.isEnabled) {
532 result.add(option);
533 }
534 }
535 }
536 } catch (IOException | XmlPullParserException e) {
537 Log.e(TAG, "Error parsing device profile", e);
538 return Collections.emptyList();
539 }
540 return result;
541 }
542
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700543 private int getLauncherIconDensity(int requiredSize) {
544 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700545 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700546 DisplayMetrics.DENSITY_LOW,
547 DisplayMetrics.DENSITY_MEDIUM,
548 DisplayMetrics.DENSITY_TV,
549 DisplayMetrics.DENSITY_HIGH,
550 DisplayMetrics.DENSITY_XHIGH,
551 DisplayMetrics.DENSITY_XXHIGH,
552 DisplayMetrics.DENSITY_XXXHIGH
553 };
554
555 int density = DisplayMetrics.DENSITY_XXXHIGH;
556 for (int i = densityBuckets.length - 1; i >= 0; i--) {
557 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
558 / DisplayMetrics.DENSITY_DEFAULT;
559 if (expectedSize >= requiredSize) {
560 density = densityBuckets[i];
561 }
562 }
563
564 return density;
565 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700566
Adam Cohen2e6da152015-05-06 11:42:25 -0700567 /**
568 * Apply any Partner customization grid overrides.
569 *
570 * Currently we support: all apps row / column count.
571 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700572 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
573 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700574 if (p != null) {
575 p.applyInvariantDeviceProfileOverrides(this, dm);
576 }
577 }
578
Sunny Goyal415f1732018-11-29 10:33:47 -0800579 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700580 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700581 }
582
Sunny Goyal19ff7282021-04-22 10:12:54 -0700583 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000584 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700585 int minWidthPx = Integer.MAX_VALUE;
586 int minHeightPx = Integer.MAX_VALUE;
587 for (WindowBounds bounds : displayInfo.supportedBounds) {
588 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000589 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700590 // For split displays, take half width per page
591 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
592 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700593
Sunny Goyal19ff7282021-04-22 10:12:54 -0700594 } else if (!isTablet && bounds.isLandscape()) {
595 // We will use transposed layout in this case
596 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
597 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
598 } else {
599 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
600 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
601 }
602 }
603
Thales Lima425f6822022-05-10 13:44:58 -0300604 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
605 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700606
607 // Sort the profiles based on the closeness to the device size
608 Collections.sort(points, (a, b) ->
609 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
610 dist(width, height, b.minWidthDps, b.minHeightDps)));
611
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700612 DisplayOption closestPoint = points.get(0);
613 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700614 float weights = 0;
615
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700616 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
617 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700618 }
619
620 DisplayOption out = new DisplayOption(closestOption);
621 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700622 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700623 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
624 weights += w;
625 out.add(new DisplayOption().add(p).multiply(w));
626 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700627 out.multiply(1.0f / weights);
628
Thales Lima6e0005a2021-10-27 15:53:41 +0100629 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700630 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100631 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
632 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700633 }
634
635 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700636 }
637
Sunny Goyal27835952017-01-13 12:15:53 -0800638 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700639 Resources res = context.getResources();
640 Configuration config = context.getResources().getConfiguration();
641
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400642 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
643 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100644 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
645
Alex Chaud3e8cc42022-05-03 17:45:34 +0100646 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400647 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700648
Sunny Goyal187b16c2022-03-01 16:53:23 -0800649 /**
650 * Returns the device profile matching the provided screen configuration
651 */
652 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700653 DeviceProfile bestMatch = supportedProfiles.get(0);
654 float minDiff = Float.MAX_VALUE;
655
656 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400657 float diff = Math.abs(profile.widthPx - screenWidth)
658 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700659 if (diff < minDiff) {
660 minDiff = diff;
661 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800662 } else if (diff == minDiff && profile.rotationHint == rotation) {
663 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700664 }
665 }
666 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800667 }
668
Sunny Goyal415f1732018-11-29 10:33:47 -0800669 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700670 float d = dist(x0, y0, x1, y1);
671 if (Float.compare(d, 0f) == 0) {
672 return Float.POSITIVE_INFINITY;
673 }
674 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
675 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700676
677 /**
678 * As a ratio of screen height, the total distance we want the parallax effect to span
679 * horizontally
680 */
681 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
682 float aspectRatio = width / (float) height;
683
684 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
685 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
686 // We will use these two data points to extrapolate how much the wallpaper parallax effect
687 // to span (ie travel) at any aspect ratio:
688
sfufa@google.comde013292021-09-21 18:22:44 -0700689 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
690 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700691 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
692 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
693
694 // To find out the desired width at different aspect ratios, we use the following two
695 // formulas, where the coefficient on x is the aspect ratio (width/height):
696 // (16/10)x + y = 1.5
697 // (10/16)x + y = 1.2
698 // We solve for x and y and end up with a final formula:
699 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700700 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
701 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700702 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
703 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
704 return x * aspectRatio + y;
705 }
706
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700707 public interface OnIDPChangeListener {
708
Sunny Goyalb47172b2021-05-03 19:59:51 -0700709 /**
710 * Called when the device provide changes
711 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700712 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700713 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800714
715
Sunny Goyaleff44f32019-01-09 17:29:49 -0800716 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800717
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800718 public static final String TAG_NAME = "grid-option";
719
Alex Chau1c883d82021-12-01 18:43:10 +0000720 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
721 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
722 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
723 private static final int DEVICE_CATEGORY_ALL =
724 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
725
Sunny Goyaleff44f32019-01-09 17:29:49 -0800726 public final String name;
727 public final int numRows;
728 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000729 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100730 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800731
732 private final int numFolderRows;
733 private final int numFolderColumns;
734
Sunny Goyal19ff7282021-04-22 10:12:54 -0700735 private final int numAllAppsColumns;
736 private final int numDatabaseAllAppsColumns;
737 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700738 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100739
Thales Lima425f6822022-05-10 13:44:58 -0300740 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800741
Vinit Nayakc7293172022-07-18 16:41:50 -0700742 private int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800743 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000744
Sunny Goyal415f1732018-11-29 10:33:47 -0800745 private final int defaultLayoutId;
746 private final int demoModeLayoutId;
747
Jon Mirandae126d722021-02-25 10:45:20 -0500748 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400749 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500750
Sunny Goyal5bc18462019-01-07 15:13:39 -0800751 private final SparseArray<TypedValue> extraAttrs;
752
Alex Chau1c883d82021-12-01 18:43:10 +0000753 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800754 TypedArray a = context.obtainStyledAttributes(
755 attrs, R.styleable.GridDisplayOption);
756 name = a.getString(R.styleable.GridDisplayOption_name);
757 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
758 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000759 numSearchContainerColumns = a.getInt(
760 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800761
Tracy Zhou7df93d22020-01-27 13:44:06 -0800762 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000763 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100764 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
765 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
766 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800767 demoModeLayoutId = a.getResourceId(
768 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700769
770 numAllAppsColumns = a.getInt(
771 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
772 numDatabaseAllAppsColumns = a.getInt(
773 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
774
775 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800776 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700777 numDatabaseHotseatIcons = a.getInt(
778 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100779
Thales Lima425f6822022-05-10 13:44:58 -0300780 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
781 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
782 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
783 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
784 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
785 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
786 numColumns);
787 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
788 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
789 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700790
Vinit Nayakc7293172022-07-18 16:41:50 -0700791 inlineNavButtonsEndSpacing =
792 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
793 R.dimen.taskbar_button_margin_default);
Sunny Goyal415f1732018-11-29 10:33:47 -0800794 numFolderRows = a.getInt(
795 R.styleable.GridDisplayOption_numFolderRows, numRows);
796 numFolderColumns = a.getInt(
797 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700798
Jon Mirandae126d722021-02-25 10:45:20 -0500799 isScalable = a.getBoolean(
800 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400801 devicePaddingId = a.getResourceId(
802 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500803
Alex Chau1c883d82021-12-01 18:43:10 +0000804 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
805 DEVICE_CATEGORY_ALL);
806 isEnabled = (deviceType == TYPE_PHONE
807 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
808 || (deviceType == TYPE_TABLET
809 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
810 || (deviceType == TYPE_MULTI_DISPLAY
811 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
812 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100813
Sunny Goyal415f1732018-11-29 10:33:47 -0800814 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800815 extraAttrs = Themes.createValueMap(context, attrs,
816 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800817 }
818 }
819
Sunny Goyal19ff7282021-04-22 10:12:54 -0700820 @VisibleForTesting
821 static final class DisplayOption {
Thales Lima12d0eff2022-03-25 17:06:11 +0000822 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
823 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
824 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
825 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
826 private static final int DONT_INLINE_QSB = 0;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700827
828 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800829
Sunny Goyal415f1732018-11-29 10:33:47 -0800830 private final float minWidthDps;
831 private final float minHeightDps;
832 private final boolean canBeDefault;
Thales Lima12d0eff2022-03-25 17:06:11 +0000833 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800834
Thales Lima83bedbf2021-10-05 17:47:39 +0100835 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100836
Thales Limaa08a4432022-08-09 09:55:17 +0100837 private final PointF folderCellSize;
838 private final PointF folderBorderSpaces;
839 private float folderTopPadding;
840
Thales Lima78d00ad2021-09-30 11:29:06 +0100841 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100842 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100843 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
844 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100845
Thales Lima78d00ad2021-09-30 11:29:06 +0100846 private final float[] iconSizes = new float[COUNT_SIZES];
847 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800848
Thales Limab7ef5692022-03-10 10:32:36 +0000849 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000850 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
851 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
852 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
853
Thales Lima1de4d552021-10-13 16:13:25 +0100854 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800855 this.grid = grid;
856
Thales Lima1de4d552021-10-13 16:13:25 +0100857 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800858
Sunny Goyal415f1732018-11-29 10:33:47 -0800859 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
860 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700861
Thales Lima1de4d552021-10-13 16:13:25 +0100862 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800863
Thales Lima12d0eff2022-03-25 17:06:11 +0000864 int inlineForRotation = a.getInt(R.styleable.ProfileDisplayOption_inlineQsb,
865 DONT_INLINE_QSB);
866 inlineQsb[INDEX_DEFAULT] =
867 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
868 inlineQsb[INDEX_LANDSCAPE] =
869 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
870 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
871 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
872 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
873 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
874 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
875 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
876
Thales Lima83bedbf2021-10-05 17:47:39 +0100877 float x;
878 float y;
879
Thales Lima85c942f2021-12-31 13:04:20 +0000880 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
881 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100882 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000883
884 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
885 minCellSize[INDEX_DEFAULT].x);
886 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
887 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100888 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100889
Thales Lima85c942f2021-12-31 13:04:20 +0000890 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100891 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000892 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100893 minCellSize[INDEX_DEFAULT].y);
894 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
895
Thales Lima85c942f2021-12-31 13:04:20 +0000896 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100897 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000898 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100899 minCellSize[INDEX_DEFAULT].y);
900 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100901
Thales Lima85c942f2021-12-31 13:04:20 +0000902 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000903 float borderSpaceLandscape = a.getFloat(
904 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000905 float borderSpaceTwoPanelPortrait = a.getFloat(
906 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
907 float borderSpaceTwoPanelLandscape = a.getFloat(
908 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100909
Thales Lima85c942f2021-12-31 13:04:20 +0000910 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
911 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100912 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000913
914 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
915 borderSpaceLandscape);
916 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
917 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100918 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
919
920 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000921 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
922 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100923 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000924 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
925 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100926 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
927
928 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000929 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
930 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100931 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000932 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
933 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100934 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
935
Thales Limaa08a4432022-08-09 09:55:17 +0100936 x = a.getFloat(R.styleable.ProfileDisplayOption_folderCellWidth,
937 minCellSize[INDEX_DEFAULT].x);
938 y = a.getFloat(R.styleable.ProfileDisplayOption_folderCellHeight,
939 minCellSize[INDEX_DEFAULT].y);
940 folderCellSize = new PointF(x, y);
941
942 float folderBorderSpace = a.getFloat(R.styleable.ProfileDisplayOption_folderBorderSpace,
943 borderSpace);
944
945 x = y = folderBorderSpace;
946 folderBorderSpaces = new PointF(x, y);
947
948 folderTopPadding = a.getFloat(R.styleable.ProfileDisplayOption_folderTopPadding,
949 folderBorderSpaces.y);
Thales Limabb7d3882021-12-22 12:56:29 +0000950
Thales Limab7ef5692022-03-10 10:32:36 +0000951 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
952 minCellSize[INDEX_DEFAULT].x);
953 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
954 minCellSize[INDEX_DEFAULT].y);
955 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
956
957 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
958 allAppsCellSize[INDEX_DEFAULT].x);
959 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
960 allAppsCellSize[INDEX_DEFAULT].y);
961 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
962
963 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
964 allAppsCellSize[INDEX_DEFAULT].x);
965 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
966 allAppsCellSize[INDEX_DEFAULT].y);
967 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
968
969 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
970 allAppsCellSize[INDEX_DEFAULT].x);
971 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
972 allAppsCellSize[INDEX_DEFAULT].y);
973 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
974
Thales Lima080d8902022-03-28 15:30:29 +0100975 float allAppsBorderSpace = a.getFloat(
976 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
977 float allAppsBorderSpaceLandscape = a.getFloat(
978 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
979 allAppsBorderSpace);
980 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000981 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100982 allAppsBorderSpace);
983 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000984 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100985 allAppsBorderSpace);
986
987 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
988 allAppsBorderSpace);
989 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
990 allAppsBorderSpace);
991 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
992
993 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
994 allAppsBorderSpaceLandscape);
995 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
996 allAppsBorderSpaceLandscape);
997 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
998
999 x = a.getFloat(
1000 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1001 allAppsBorderSpaceTwoPanelPortrait);
1002 y = a.getFloat(
1003 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1004 allAppsBorderSpaceTwoPanelPortrait);
1005 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1006
1007 x = a.getFloat(
1008 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1009 allAppsBorderSpaceTwoPanelLandscape);
1010 y = a.getFloat(
1011 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1012 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001013 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001014
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001015 iconSizes[INDEX_DEFAULT] =
1016 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1017 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001018 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001019 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001020 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001021 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001022 iconSizes[INDEX_DEFAULT]);
1023 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001024 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001025 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001026
Thales Limabb7d3882021-12-22 12:56:29 +00001027 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1028 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001029 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1030 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
1031 iconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001032 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1033 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1034 allAppsIconSizes[INDEX_DEFAULT]);
1035 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1036 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1037 allAppsIconSizes[INDEX_DEFAULT]);
1038
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001039 textSizes[INDEX_DEFAULT] =
1040 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1041 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001042 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001043 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001044 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001045 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001046 textSizes[INDEX_DEFAULT]);
1047 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001048 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001049 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001050
Thales Limabb7d3882021-12-22 12:56:29 +00001051 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1052 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1053 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1054 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1055 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1056 allAppsIconTextSizes[INDEX_DEFAULT]);
1057 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1058 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1059 allAppsIconTextSizes[INDEX_DEFAULT]);
1060
Thales Lima83bedbf2021-10-05 17:47:39 +01001061 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1062 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001063 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1064 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1065 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001066 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001067 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001068 horizontalMargin[INDEX_DEFAULT]);
1069 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001070 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001071 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001072
Thales Limab8c05952022-05-23 16:58:38 +01001073 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1074 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1075 ResourcesCompat.getFloat(context.getResources(),
1076 R.dimen.hotseat_bar_bottom_space_default));
1077 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1078 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1079 hotseatBarBottomSpace[INDEX_DEFAULT]);
1080 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1081 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1082 hotseatBarBottomSpace[INDEX_DEFAULT]);
1083 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1084 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1085 hotseatBarBottomSpace[INDEX_DEFAULT]);
1086
1087 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1088 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1089 ResourcesCompat.getFloat(context.getResources(),
1090 R.dimen.hotseat_qsb_space_default));
1091 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1092 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1093 hotseatQsbSpace[INDEX_DEFAULT]);
1094 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1095 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1096 hotseatQsbSpace[INDEX_DEFAULT]);
1097 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1098 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1099 hotseatQsbSpace[INDEX_DEFAULT]);
1100
Sunny Goyal415f1732018-11-29 10:33:47 -08001101 a.recycle();
1102 }
1103
1104 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001105 this(null);
1106 }
1107
1108 DisplayOption(GridOption grid) {
1109 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001110 minWidthDps = 0;
1111 minHeightDps = 0;
1112 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001113 for (int i = 0; i < COUNT_SIZES; i++) {
1114 iconSizes[i] = 0;
1115 textSizes[i] = 0;
1116 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001117 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001118 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001119 allAppsIconSizes[i] = 0;
1120 allAppsIconTextSizes[i] = 0;
1121 allAppsBorderSpaces[i] = new PointF();
Thales Lima12d0eff2022-03-25 17:06:11 +00001122 inlineQsb[i] = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001123 }
Thales Limaa08a4432022-08-09 09:55:17 +01001124 folderBorderSpaces = new PointF();
1125 folderCellSize = new PointF();
1126 folderTopPadding = 0f;
Sunny Goyal415f1732018-11-29 10:33:47 -08001127 }
1128
1129 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001130 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001131 iconSizes[i] *= w;
1132 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001133 borderSpaces[i].x *= w;
1134 borderSpaces[i].y *= w;
1135 minCellSize[i].x *= w;
1136 minCellSize[i].y *= w;
1137 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001138 hotseatBarBottomSpace[i] *= w;
1139 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001140 allAppsCellSize[i].x *= w;
1141 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001142 allAppsIconSizes[i] *= w;
1143 allAppsIconTextSizes[i] *= w;
1144 allAppsBorderSpaces[i].x *= w;
1145 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001146 }
Thales Limaa08a4432022-08-09 09:55:17 +01001147 folderBorderSpaces.x *= w;
1148 folderBorderSpaces.y *= w;
1149 folderCellSize.x *= w;
1150 folderCellSize.y *= w;
1151 folderTopPadding *= w;
Thales Lima78d00ad2021-09-30 11:29:06 +01001152
Sunny Goyal415f1732018-11-29 10:33:47 -08001153 return this;
1154 }
1155
1156 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001157 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001158 iconSizes[i] += p.iconSizes[i];
1159 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001160 borderSpaces[i].x += p.borderSpaces[i].x;
1161 borderSpaces[i].y += p.borderSpaces[i].y;
1162 minCellSize[i].x += p.minCellSize[i].x;
1163 minCellSize[i].y += p.minCellSize[i].y;
1164 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001165 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1166 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001167 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1168 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001169 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1170 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1171 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1172 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Thales Lima12d0eff2022-03-25 17:06:11 +00001173 inlineQsb[i] |= p.inlineQsb[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001174 }
Thales Limaa08a4432022-08-09 09:55:17 +01001175 folderBorderSpaces.x += p.folderBorderSpaces.x;
1176 folderBorderSpaces.y += p.folderBorderSpaces.y;
1177 folderCellSize.x += p.folderCellSize.x;
1178 folderCellSize.y += p.folderCellSize.y;
1179 folderTopPadding += p.folderTopPadding;
Thales Lima78d00ad2021-09-30 11:29:06 +01001180
Sunny Goyal415f1732018-11-29 10:33:47 -08001181 return this;
1182 }
1183 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001184}