blob: 21eba00ee2e38819b6969c64609537ff7f783349 [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;
Thales Lima78d00ad2021-09-30 11:29:06 +0100130 public float folderBorderSpace;
Vinit Nayakc7293172022-07-18 16:41:50 -0700131 public int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100132
Thales Lima83bedbf2021-10-05 17:47:39 +0100133 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500134
Thales Limab7ef5692022-03-10 10:32:36 +0000135 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000136 public float[] allAppsIconSize;
137 public float[] allAppsIconTextSize;
138 public PointF[] allAppsBorderSpaces;
139
Sunny Goyal5bc18462019-01-07 15:13:39 -0800140 private SparseArray<TypedValue> mExtraAttrs;
141
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700142 /**
143 * Number of icons inside the hotseat area.
144 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800145 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700146
147 /**
148 * Number of icons inside the hotseat area that is stored in the database. This is greater than
149 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
150 * sizes that share the same DB.
151 */
152 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700153
Thales Lima425f6822022-05-10 13:44:58 -0300154 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100155 public float[] hotseatBarBottomSpace;
156 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300157
Jon Miranda6f7e9702019-09-16 14:44:14 -0700158 /**
159 * Number of columns in the all apps list.
160 */
161 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700162 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700163
Jon Mirandae126d722021-02-25 10:45:20 -0500164 /**
165 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
166 */
167 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400168 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500169
Tracy Zhou7df93d22020-01-27 13:44:06 -0800170 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800171 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700172 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100173 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700174
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000175 /**
176 * An immutable list of supported profiles.
177 */
178 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700179
sfufa@google.comde013292021-09-21 18:22:44 -0700180 @Nullable
181 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500182
Sunny Goyal6f866092016-03-17 17:04:15 -0700183 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700184 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700185
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700186 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700187
Sunny Goyalf633ef52018-03-13 09:57:05 -0700188 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800189 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700190
Sunny Goyalbbf01842015-10-08 07:41:15 -0700191 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700192 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700193 String gridName = getCurrentGridName(context);
194 String newGridName = initGrid(context, gridName);
195 if (!newGridName.equals(gridName)) {
196 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
197 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700198 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700199
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700200 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100201 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000202 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
203 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100204 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700205 }
206 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700207 }
208
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700209 /**
210 * This constructor should NOT have any monitors by design.
211 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800212 public InvariantDeviceProfile(Context context, String gridName) {
213 String newName = initGrid(context, gridName);
214 if (newName == null || !newName.equals(gridName)) {
215 throw new IllegalArgumentException("Unknown grid name");
216 }
217 }
218
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700219 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800220 * This constructor should NOT have any monitors by design.
221 */
222 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700223 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100224 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700225 String gridName = getCurrentGridName(context);
226
227 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000228 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
229 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700230 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000231 defaultInfo,
232 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
233 /*allowDisabledGrid=*/false),
234 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235
Alex Chau661f02d2022-06-07 14:03:43 +0100236 Context displayContext = context.createDisplayContext(display);
237 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000238 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700239 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000240 myInfo,
241 getPredefinedDeviceProfiles(context, gridName, deviceType,
242 /*allowDisabledGrid=*/false),
243 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700244
245 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
246 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100247 result.iconSizes[INDEX_DEFAULT] =
248 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
249 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700250 result.iconSizes[i] = Math.min(
251 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000252 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700253
Thales Lima83bedbf2021-10-05 17:47:39 +0100254 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
255 COUNT_SIZES);
256 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
257 COUNT_SIZES);
Thales Lima12d0eff2022-03-25 17:06:11 +0000258 System.arraycopy(defaultDisplayOption.inlineQsb, 0, result.inlineQsb, 0,
259 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500260
Alex Chau1c883d82021-12-01 18:43:10 +0000261 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800262 }
263
Alex Chau238aaee2021-10-06 16:15:24 +0100264 /**
265 * Reinitialize the current grid after a restore, where some grids might now be disabled.
266 */
267 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000268 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100269 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000270 String newGridName = initGrid(context, currentGridName);
271 String newDbFile = dbFile;
272 if (!newDbFile.equals(currentDbFile)) {
273 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100274 + ", migrating to: " + newGridName
275 + ", removing all other grid db files");
276 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
277 if (gridDbFile.equals(currentDbFile)) {
278 continue;
279 }
280 if (context.getDatabasePath(gridDbFile).delete()) {
281 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
282 }
283 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000284 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100285 }
286 }
287
Alex Chau1c883d82021-12-01 18:43:10 +0000288 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800289 int flagPhone = 1 << 0;
290 int flagTablet = 1 << 1;
291
292 int type = displayInfo.supportedBounds.stream()
293 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
294 .reduce(0, (a, b) -> a | b);
295 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
296 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000297 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800298 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000299 return TYPE_TABLET;
300 } else {
301 return TYPE_PHONE;
302 }
303 }
304
Tracy Zhou42255d22020-03-13 00:38:11 -0700305 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700306 return Utilities.isGridOptionsEnabled(context)
307 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700308 }
309
Sunny Goyaleff44f32019-01-09 17:29:49 -0800310 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700311 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000312 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700313
314 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000315 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100316 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700317 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000318 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
319 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700320 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800321 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700322
Alex Chau1c883d82021-12-01 18:43:10 +0000323 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
324 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700325 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700326 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000327 numRows = closestProfile.numRows;
328 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000329 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000330 dbFile = closestProfile.dbFile;
331 defaultLayoutId = closestProfile.defaultLayoutId;
332 demoModeLayoutId = closestProfile.demoModeLayoutId;
333 numFolderRows = closestProfile.numFolderRows;
334 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500335 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400336 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000337 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000338
Vinit Nayakc7293172022-07-18 16:41:50 -0700339 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
340
Sunny Goyalae190ff2020-04-14 00:19:01 +0000341 mExtraAttrs = closestProfile.extraAttrs;
342
Thales Lima83bedbf2021-10-05 17:47:39 +0100343 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100344 float maxIconSize = iconSize[0];
345 for (int i = 1; i < iconSize.length; i++) {
346 maxIconSize = Math.max(maxIconSize, iconSize[i]);
347 }
348 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000349 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
350
Thales Lima83bedbf2021-10-05 17:47:39 +0100351 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700352
Thales Lima83bedbf2021-10-05 17:47:39 +0100353 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100354
Thales Lima83bedbf2021-10-05 17:47:39 +0100355 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100356 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700357
Thales Limad90faab2021-09-21 17:18:47 +0100358 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100359
Sunny Goyal19ff7282021-04-22 10:12:54 -0700360 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000361 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700362 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300363 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100364 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
365 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700366
367 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000368 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700369 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500370
Thales Limab7ef5692022-03-10 10:32:36 +0000371 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000372 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
373 allAppsIconSize = displayOption.allAppsIconSizes;
374 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100375 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000376 allAppsIconSize = iconSize;
377 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000378 }
379
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400380 if (devicePaddingId != 0) {
381 devicePaddings = new DevicePaddings(context, devicePaddingId);
382 }
383
Thales Lima12d0eff2022-03-25 17:06:11 +0000384 inlineQsb = displayOption.inlineQsb;
385
Sunny Goyalae190ff2020-04-14 00:19:01 +0000386 // If the partner customization apk contains any grid overrides, apply them
387 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700388 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700389
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000390 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700391 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700392 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700393 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000394 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000395 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000396 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700397 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000398 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700399
Sunny Goyal19ff7282021-04-22 10:12:54 -0700400 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
401 int displayWidth = bounds.bounds.width();
402 int displayHeight = bounds.bounds.height();
403 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700404
Sunny Goyal19ff7282021-04-22 10:12:54 -0700405 // We need to ensure that there is enough extra space in the wallpaper
406 // for the intended parallax effects
407 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300408 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
409 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700410 ? 2
411 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
412 defaultWallpaperSize.x =
413 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700414 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000415 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700416
417 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
418 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700419 }
420
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700421 public void addOnChangeListener(OnIDPChangeListener listener) {
422 mChangeListeners.add(listener);
423 }
424
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800425 public void removeOnChangeListener(OnIDPChangeListener listener) {
426 mChangeListeners.remove(listener);
427 }
428
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800429
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800430 public void setCurrentGrid(Context context, String gridName) {
431 Context appContext = context.getApplicationContext();
432 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700433 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800434 }
435
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700436 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700437 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000438 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
439 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700440 }
441
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700442 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700443 Object[] oldState = toModelState();
444
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700445 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700446 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700447 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700448
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700449 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700450 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700451 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700452 }
453 }
454
Alex Chau1c883d82021-12-01 18:43:10 +0000455 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
456 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800457 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100458
Alex Chau1c883d82021-12-01 18:43:10 +0000459 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700460 final int depth = parser.getDepth();
461 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700462 while (((type = parser.next()) != XmlPullParser.END_TAG ||
463 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800464 if ((type == XmlPullParser.START_TAG)
465 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800466
Alex Chau1c883d82021-12-01 18:43:10 +0000467 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
468 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100469 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100470 final int displayDepth = parser.getDepth();
471 while (((type = parser.next()) != XmlPullParser.END_TAG
472 || parser.getDepth() > displayDepth)
473 && type != XmlPullParser.END_DOCUMENT) {
474 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
475 parser.getName())) {
476 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100477 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100478 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800479 }
480 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700481 }
482 }
sfufa@google.comde013292021-09-21 18:22:44 -0700483 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700484 throw new RuntimeException(e);
485 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800486
Sunny Goyalae190ff2020-04-14 00:19:01 +0000487 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800488 if (!TextUtils.isEmpty(gridName)) {
489 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100490 if (gridName.equals(option.grid.name)
491 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000492 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800493 }
494 }
495 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000496 if (filteredProfiles.isEmpty()) {
497 // No grid found, use the default options
498 for (DisplayOption option : profiles) {
499 if (option.canBeDefault) {
500 filteredProfiles.add(option);
501 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800502 }
503 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000504 if (filteredProfiles.isEmpty()) {
505 throw new RuntimeException("No display option with canBeDefault=true");
506 }
507 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700508 }
509
Thales Lima7ec83822021-08-05 13:32:10 +0100510 /**
511 * @return all the grid options that can be shown on the device
512 */
513 public List<GridOption> parseAllGridOptions(Context context) {
514 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100515
Alex Chau1c883d82021-12-01 18:43:10 +0000516 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100517 final int depth = parser.getDepth();
518 int type;
519 while (((type = parser.next()) != XmlPullParser.END_TAG
520 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
521 if ((type == XmlPullParser.START_TAG)
522 && GridOption.TAG_NAME.equals(parser.getName())) {
523 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000524 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100525 if (option.isEnabled) {
526 result.add(option);
527 }
528 }
529 }
530 } catch (IOException | XmlPullParserException e) {
531 Log.e(TAG, "Error parsing device profile", e);
532 return Collections.emptyList();
533 }
534 return result;
535 }
536
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700537 private int getLauncherIconDensity(int requiredSize) {
538 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700539 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700540 DisplayMetrics.DENSITY_LOW,
541 DisplayMetrics.DENSITY_MEDIUM,
542 DisplayMetrics.DENSITY_TV,
543 DisplayMetrics.DENSITY_HIGH,
544 DisplayMetrics.DENSITY_XHIGH,
545 DisplayMetrics.DENSITY_XXHIGH,
546 DisplayMetrics.DENSITY_XXXHIGH
547 };
548
549 int density = DisplayMetrics.DENSITY_XXXHIGH;
550 for (int i = densityBuckets.length - 1; i >= 0; i--) {
551 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
552 / DisplayMetrics.DENSITY_DEFAULT;
553 if (expectedSize >= requiredSize) {
554 density = densityBuckets[i];
555 }
556 }
557
558 return density;
559 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700560
Adam Cohen2e6da152015-05-06 11:42:25 -0700561 /**
562 * Apply any Partner customization grid overrides.
563 *
564 * Currently we support: all apps row / column count.
565 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700566 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
567 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700568 if (p != null) {
569 p.applyInvariantDeviceProfileOverrides(this, dm);
570 }
571 }
572
Sunny Goyal415f1732018-11-29 10:33:47 -0800573 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700574 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700575 }
576
Sunny Goyal19ff7282021-04-22 10:12:54 -0700577 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000578 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700579 int minWidthPx = Integer.MAX_VALUE;
580 int minHeightPx = Integer.MAX_VALUE;
581 for (WindowBounds bounds : displayInfo.supportedBounds) {
582 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000583 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700584 // For split displays, take half width per page
585 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
586 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700587
Sunny Goyal19ff7282021-04-22 10:12:54 -0700588 } else if (!isTablet && bounds.isLandscape()) {
589 // We will use transposed layout in this case
590 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
591 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
592 } else {
593 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
594 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
595 }
596 }
597
Thales Lima425f6822022-05-10 13:44:58 -0300598 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
599 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700600
601 // Sort the profiles based on the closeness to the device size
602 Collections.sort(points, (a, b) ->
603 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
604 dist(width, height, b.minWidthDps, b.minHeightDps)));
605
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700606 DisplayOption closestPoint = points.get(0);
607 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700608 float weights = 0;
609
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700610 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
611 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700612 }
613
614 DisplayOption out = new DisplayOption(closestOption);
615 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700616 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700617 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
618 weights += w;
619 out.add(new DisplayOption().add(p).multiply(w));
620 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700621 out.multiply(1.0f / weights);
622
Thales Lima6e0005a2021-10-27 15:53:41 +0100623 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700624 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100625 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
626 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700627 }
628
629 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700630 }
631
Sunny Goyal27835952017-01-13 12:15:53 -0800632 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700633 Resources res = context.getResources();
634 Configuration config = context.getResources().getConfiguration();
635
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400636 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
637 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100638 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
639
Alex Chaud3e8cc42022-05-03 17:45:34 +0100640 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400641 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700642
Sunny Goyal187b16c2022-03-01 16:53:23 -0800643 /**
644 * Returns the device profile matching the provided screen configuration
645 */
646 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700647 DeviceProfile bestMatch = supportedProfiles.get(0);
648 float minDiff = Float.MAX_VALUE;
649
650 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400651 float diff = Math.abs(profile.widthPx - screenWidth)
652 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700653 if (diff < minDiff) {
654 minDiff = diff;
655 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800656 } else if (diff == minDiff && profile.rotationHint == rotation) {
657 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700658 }
659 }
660 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800661 }
662
Sunny Goyal415f1732018-11-29 10:33:47 -0800663 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700664 float d = dist(x0, y0, x1, y1);
665 if (Float.compare(d, 0f) == 0) {
666 return Float.POSITIVE_INFINITY;
667 }
668 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
669 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700670
671 /**
672 * As a ratio of screen height, the total distance we want the parallax effect to span
673 * horizontally
674 */
675 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
676 float aspectRatio = width / (float) height;
677
678 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
679 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
680 // We will use these two data points to extrapolate how much the wallpaper parallax effect
681 // to span (ie travel) at any aspect ratio:
682
sfufa@google.comde013292021-09-21 18:22:44 -0700683 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
684 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700685 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
686 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
687
688 // To find out the desired width at different aspect ratios, we use the following two
689 // formulas, where the coefficient on x is the aspect ratio (width/height):
690 // (16/10)x + y = 1.5
691 // (10/16)x + y = 1.2
692 // We solve for x and y and end up with a final formula:
693 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700694 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
695 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700696 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
697 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
698 return x * aspectRatio + y;
699 }
700
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700701 public interface OnIDPChangeListener {
702
Sunny Goyalb47172b2021-05-03 19:59:51 -0700703 /**
704 * Called when the device provide changes
705 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700706 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700707 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800708
709
Sunny Goyaleff44f32019-01-09 17:29:49 -0800710 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800711
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800712 public static final String TAG_NAME = "grid-option";
713
Alex Chau1c883d82021-12-01 18:43:10 +0000714 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
715 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
716 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
717 private static final int DEVICE_CATEGORY_ALL =
718 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
719
Sunny Goyaleff44f32019-01-09 17:29:49 -0800720 public final String name;
721 public final int numRows;
722 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000723 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100724 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800725
726 private final int numFolderRows;
727 private final int numFolderColumns;
728
Sunny Goyal19ff7282021-04-22 10:12:54 -0700729 private final int numAllAppsColumns;
730 private final int numDatabaseAllAppsColumns;
731 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700732 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100733
Thales Lima425f6822022-05-10 13:44:58 -0300734 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800735
Vinit Nayakc7293172022-07-18 16:41:50 -0700736 private int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800737 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000738
Sunny Goyal415f1732018-11-29 10:33:47 -0800739 private final int defaultLayoutId;
740 private final int demoModeLayoutId;
741
Jon Mirandae126d722021-02-25 10:45:20 -0500742 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400743 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500744
Sunny Goyal5bc18462019-01-07 15:13:39 -0800745 private final SparseArray<TypedValue> extraAttrs;
746
Alex Chau1c883d82021-12-01 18:43:10 +0000747 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800748 TypedArray a = context.obtainStyledAttributes(
749 attrs, R.styleable.GridDisplayOption);
750 name = a.getString(R.styleable.GridDisplayOption_name);
751 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
752 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000753 numSearchContainerColumns = a.getInt(
754 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800755
Tracy Zhou7df93d22020-01-27 13:44:06 -0800756 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000757 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100758 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
759 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
760 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800761 demoModeLayoutId = a.getResourceId(
762 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700763
764 numAllAppsColumns = a.getInt(
765 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
766 numDatabaseAllAppsColumns = a.getInt(
767 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
768
769 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800770 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700771 numDatabaseHotseatIcons = a.getInt(
772 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100773
Thales Lima425f6822022-05-10 13:44:58 -0300774 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
775 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
776 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
777 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
778 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
779 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
780 numColumns);
781 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
782 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
783 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700784
Vinit Nayakc7293172022-07-18 16:41:50 -0700785 inlineNavButtonsEndSpacing =
786 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
787 R.dimen.taskbar_button_margin_default);
Sunny Goyal415f1732018-11-29 10:33:47 -0800788 numFolderRows = a.getInt(
789 R.styleable.GridDisplayOption_numFolderRows, numRows);
790 numFolderColumns = a.getInt(
791 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700792
Jon Mirandae126d722021-02-25 10:45:20 -0500793 isScalable = a.getBoolean(
794 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400795 devicePaddingId = a.getResourceId(
796 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500797
Alex Chau1c883d82021-12-01 18:43:10 +0000798 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
799 DEVICE_CATEGORY_ALL);
800 isEnabled = (deviceType == TYPE_PHONE
801 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
802 || (deviceType == TYPE_TABLET
803 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
804 || (deviceType == TYPE_MULTI_DISPLAY
805 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
806 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100807
Sunny Goyal415f1732018-11-29 10:33:47 -0800808 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800809 extraAttrs = Themes.createValueMap(context, attrs,
810 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800811 }
812 }
813
Sunny Goyal19ff7282021-04-22 10:12:54 -0700814 @VisibleForTesting
815 static final class DisplayOption {
Thales Lima12d0eff2022-03-25 17:06:11 +0000816 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
817 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
818 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
819 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
820 private static final int DONT_INLINE_QSB = 0;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700821
822 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800823
Sunny Goyal415f1732018-11-29 10:33:47 -0800824 private final float minWidthDps;
825 private final float minHeightDps;
826 private final boolean canBeDefault;
Thales Lima12d0eff2022-03-25 17:06:11 +0000827 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800828
Thales Lima83bedbf2021-10-05 17:47:39 +0100829 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100830
831 private float folderBorderSpace;
832 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100833 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100834 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
835 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100836
Thales Lima78d00ad2021-09-30 11:29:06 +0100837 private final float[] iconSizes = new float[COUNT_SIZES];
838 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800839
Thales Limab7ef5692022-03-10 10:32:36 +0000840 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000841 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
842 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
843 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
844
Thales Lima1de4d552021-10-13 16:13:25 +0100845 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800846 this.grid = grid;
847
Thales Lima1de4d552021-10-13 16:13:25 +0100848 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800849
Sunny Goyal415f1732018-11-29 10:33:47 -0800850 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
851 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700852
Thales Lima1de4d552021-10-13 16:13:25 +0100853 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800854
Thales Lima12d0eff2022-03-25 17:06:11 +0000855 int inlineForRotation = a.getInt(R.styleable.ProfileDisplayOption_inlineQsb,
856 DONT_INLINE_QSB);
857 inlineQsb[INDEX_DEFAULT] =
858 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
859 inlineQsb[INDEX_LANDSCAPE] =
860 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
861 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
862 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
863 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
864 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
865 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
866 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
867
Thales Lima83bedbf2021-10-05 17:47:39 +0100868 float x;
869 float y;
870
Thales Lima85c942f2021-12-31 13:04:20 +0000871 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
872 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100873 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000874
875 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
876 minCellSize[INDEX_DEFAULT].x);
877 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
878 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100879 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100880
Thales Lima85c942f2021-12-31 13:04:20 +0000881 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100882 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000883 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100884 minCellSize[INDEX_DEFAULT].y);
885 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
886
Thales Lima85c942f2021-12-31 13:04:20 +0000887 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100888 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000889 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100890 minCellSize[INDEX_DEFAULT].y);
891 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100892
Thales Lima85c942f2021-12-31 13:04:20 +0000893 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000894 float borderSpaceLandscape = a.getFloat(
895 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000896 float borderSpaceTwoPanelPortrait = a.getFloat(
897 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
898 float borderSpaceTwoPanelLandscape = a.getFloat(
899 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100900
Thales Lima85c942f2021-12-31 13:04:20 +0000901 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
902 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100903 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000904
905 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
906 borderSpaceLandscape);
907 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
908 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100909 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
910
911 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000912 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
913 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100914 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000915 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
916 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100917 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
918
919 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000920 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
921 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100922 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000923 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
924 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100925 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
926
Thales Limabb7d3882021-12-22 12:56:29 +0000927 folderBorderSpace = borderSpace;
928
Thales Limab7ef5692022-03-10 10:32:36 +0000929 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
930 minCellSize[INDEX_DEFAULT].x);
931 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
932 minCellSize[INDEX_DEFAULT].y);
933 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
934
935 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
936 allAppsCellSize[INDEX_DEFAULT].x);
937 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
938 allAppsCellSize[INDEX_DEFAULT].y);
939 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
940
941 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
942 allAppsCellSize[INDEX_DEFAULT].x);
943 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
944 allAppsCellSize[INDEX_DEFAULT].y);
945 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
946
947 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
948 allAppsCellSize[INDEX_DEFAULT].x);
949 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
950 allAppsCellSize[INDEX_DEFAULT].y);
951 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
952
Thales Lima080d8902022-03-28 15:30:29 +0100953 float allAppsBorderSpace = a.getFloat(
954 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
955 float allAppsBorderSpaceLandscape = a.getFloat(
956 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
957 allAppsBorderSpace);
958 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000959 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100960 allAppsBorderSpace);
961 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000962 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100963 allAppsBorderSpace);
964
965 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
966 allAppsBorderSpace);
967 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
968 allAppsBorderSpace);
969 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
970
971 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
972 allAppsBorderSpaceLandscape);
973 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
974 allAppsBorderSpaceLandscape);
975 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
976
977 x = a.getFloat(
978 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
979 allAppsBorderSpaceTwoPanelPortrait);
980 y = a.getFloat(
981 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
982 allAppsBorderSpaceTwoPanelPortrait);
983 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
984
985 x = a.getFloat(
986 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
987 allAppsBorderSpaceTwoPanelLandscape);
988 y = a.getFloat(
989 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
990 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +0000991 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -0500992
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700993 iconSizes[INDEX_DEFAULT] =
994 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
995 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000996 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700997 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700998 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +0000999 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001000 iconSizes[INDEX_DEFAULT]);
1001 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001002 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001003 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001004
Thales Limabb7d3882021-12-22 12:56:29 +00001005 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1006 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
1007 allAppsIconSizes[INDEX_LANDSCAPE] = allAppsIconSizes[INDEX_DEFAULT];
1008 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1009 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1010 allAppsIconSizes[INDEX_DEFAULT]);
1011 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1012 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1013 allAppsIconSizes[INDEX_DEFAULT]);
1014
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001015 textSizes[INDEX_DEFAULT] =
1016 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1017 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001018 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001019 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001020 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001021 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001022 textSizes[INDEX_DEFAULT]);
1023 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001024 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001025 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001026
Thales Limabb7d3882021-12-22 12:56:29 +00001027 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1028 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1029 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1030 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1031 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1032 allAppsIconTextSizes[INDEX_DEFAULT]);
1033 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1034 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1035 allAppsIconTextSizes[INDEX_DEFAULT]);
1036
Thales Lima83bedbf2021-10-05 17:47:39 +01001037 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1038 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001039 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1040 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1041 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001042 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001043 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001044 horizontalMargin[INDEX_DEFAULT]);
1045 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001046 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001047 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001048
Thales Limab8c05952022-05-23 16:58:38 +01001049 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1050 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1051 ResourcesCompat.getFloat(context.getResources(),
1052 R.dimen.hotseat_bar_bottom_space_default));
1053 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1054 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1055 hotseatBarBottomSpace[INDEX_DEFAULT]);
1056 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1057 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1058 hotseatBarBottomSpace[INDEX_DEFAULT]);
1059 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1060 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1061 hotseatBarBottomSpace[INDEX_DEFAULT]);
1062
1063 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1064 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1065 ResourcesCompat.getFloat(context.getResources(),
1066 R.dimen.hotseat_qsb_space_default));
1067 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1068 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1069 hotseatQsbSpace[INDEX_DEFAULT]);
1070 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1071 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1072 hotseatQsbSpace[INDEX_DEFAULT]);
1073 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1074 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1075 hotseatQsbSpace[INDEX_DEFAULT]);
1076
Sunny Goyal415f1732018-11-29 10:33:47 -08001077 a.recycle();
1078 }
1079
1080 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001081 this(null);
1082 }
1083
1084 DisplayOption(GridOption grid) {
1085 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001086 minWidthDps = 0;
1087 minHeightDps = 0;
1088 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001089 for (int i = 0; i < COUNT_SIZES; i++) {
1090 iconSizes[i] = 0;
1091 textSizes[i] = 0;
1092 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001093 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001094 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001095 allAppsIconSizes[i] = 0;
1096 allAppsIconTextSizes[i] = 0;
1097 allAppsBorderSpaces[i] = new PointF();
Thales Lima12d0eff2022-03-25 17:06:11 +00001098 inlineQsb[i] = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001099 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001100 }
1101
1102 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001103 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001104 iconSizes[i] *= w;
1105 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001106 borderSpaces[i].x *= w;
1107 borderSpaces[i].y *= w;
1108 minCellSize[i].x *= w;
1109 minCellSize[i].y *= w;
1110 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001111 hotseatBarBottomSpace[i] *= w;
1112 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001113 allAppsCellSize[i].x *= w;
1114 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001115 allAppsIconSizes[i] *= w;
1116 allAppsIconTextSizes[i] *= w;
1117 allAppsBorderSpaces[i].x *= w;
1118 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001119 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001120
1121 folderBorderSpace *= w;
1122
Sunny Goyal415f1732018-11-29 10:33:47 -08001123 return this;
1124 }
1125
1126 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001127 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001128 iconSizes[i] += p.iconSizes[i];
1129 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001130 borderSpaces[i].x += p.borderSpaces[i].x;
1131 borderSpaces[i].y += p.borderSpaces[i].y;
1132 minCellSize[i].x += p.minCellSize[i].x;
1133 minCellSize[i].y += p.minCellSize[i].y;
1134 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001135 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1136 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001137 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1138 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001139 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1140 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1141 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1142 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Thales Lima12d0eff2022-03-25 17:06:11 +00001143 inlineQsb[i] |= p.inlineQsb[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001144 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001145
1146 folderBorderSpace += p.folderBorderSpace;
1147
Sunny Goyal415f1732018-11-29 10:33:47 -08001148 return this;
1149 }
1150 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001151}