blob: ca92aa48f68e89cccfd04bd14dbf08e153c01e4f [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;
Thales Limab35faed2022-09-05 16:30:01 -030021import static com.android.launcher3.testing.shared.ResourceUtils.INVALID_RESOURCE_HANDLE;
Sunny Goyal35c7b192021-04-20 16:51:10 -070022import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Alex Chau6ed408f2022-03-04 12:58:05 +000023import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE;
Sunny Goyal19ff7282021-04-22 10:12:54 -070024import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080025import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070026
Sunny Goyalc6205602015-05-21 20:46:33 -070027import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070028import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070029import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070030import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080031import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080032import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070033import android.content.res.TypedArray;
34import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070035import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010036import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070037import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080038import android.text.TextUtils;
39import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070040import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010041import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080042import android.util.SparseArray;
43import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070044import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080045import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070046
Thales Limab35faed2022-09-05 16:30:01 -030047import androidx.annotation.DimenRes;
Alex Chau1c883d82021-12-01 18:43:10 +000048import androidx.annotation.IntDef;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070049import androidx.annotation.Nullable;
Thales Limab35faed2022-09-05 16:30:01 -030050import androidx.annotation.StyleRes;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070051import androidx.annotation.VisibleForTesting;
Thales Limab8c05952022-05-23 16:58:38 +010052import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070053
Sunny Goyal65190ae2022-08-02 14:16:26 -070054import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070055import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010056import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070057import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070058import com.android.launcher3.util.DisplayController;
59import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080060import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070061import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080062import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070063import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080064import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070065
Sunny Goyal819e1932016-07-07 16:43:58 -070066import org.xmlpull.v1.XmlPullParser;
67import org.xmlpull.v1.XmlPullParserException;
68
69import java.io.IOException;
Alex Chau5dfd9392022-11-02 17:39:40 +000070import java.io.PrintWriter;
71import java.io.StringWriter;
Alex Chau1c883d82021-12-01 18:43:10 +000072import java.lang.annotation.Retention;
73import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070074import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070075import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080076import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070077import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070078
79public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070080
Hyunyoung Songc55a3502018-12-04 15:43:16 -080081 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070082 // We do not need any synchronization for this variable as its only written on UI thread.
83 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070084 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070085
Alex Chau1c883d82021-12-01 18:43:10 +000086 @Retention(RetentionPolicy.SOURCE)
87 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010088 public @interface DeviceType {}
89
Alex Chau1c883d82021-12-01 18:43:10 +000090 public static final int TYPE_PHONE = 0;
91 public static final int TYPE_MULTI_DISPLAY = 1;
92 public static final int TYPE_TABLET = 2;
93
Hyunyoung Songc55a3502018-12-04 15:43:16 -080094 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080095
Sunny Goyal53d7ee42015-05-22 12:25:45 -070096 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
97
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098 // Constants that affects the interpolation curve between statically defined device profile
99 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800100 private static final float KNEARESTNEIGHBOR = 3;
101 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -0700102
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700103 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800104 private static final float WEIGHT_EFFICIENT = 100000f;
105
Thales Lima83bedbf2021-10-05 17:47:39 +0100106 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
107 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000108 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100109 static final int INDEX_DEFAULT = 0;
110 static final int INDEX_LANDSCAPE = 1;
111 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
112 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100113
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700114 /**
115 * Number of icons per row and column in the workspace.
116 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700117 public int numRows;
118 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000119 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700120
121 /**
122 * Number of icons per row and column in the folder.
123 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700124 public int numFolderRows;
125 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100126 public float[] iconSize;
127 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700128 public int iconBitmapSize;
129 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000130 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700131
Thales Lima83bedbf2021-10-05 17:47:39 +0100132 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100133
Thales Lima83bedbf2021-10-05 17:47:39 +0100134 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300135 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100136
Thales Limab35faed2022-09-05 16:30:01 -0300137 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100138
Thales Lima83bedbf2021-10-05 17:47:39 +0100139 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500140
Thales Limab7ef5692022-03-10 10:32:36 +0000141 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000142 public float[] allAppsIconSize;
143 public float[] allAppsIconTextSize;
144 public PointF[] allAppsBorderSpaces;
145
Sunny Goyal5bc18462019-01-07 15:13:39 -0800146 private SparseArray<TypedValue> mExtraAttrs;
147
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700148 /**
149 * Number of icons inside the hotseat area.
150 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800151 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700152
153 /**
154 * Number of icons inside the hotseat area that is stored in the database. This is greater than
155 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
156 * sizes that share the same DB.
157 */
158 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700159
Thales Lima425f6822022-05-10 13:44:58 -0300160 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100161 public float[] hotseatBarBottomSpace;
162 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300163
Jon Miranda6f7e9702019-09-16 14:44:14 -0700164 /**
165 * Number of columns in the all apps list.
166 */
167 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700168 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700169
Jon Mirandae126d722021-02-25 10:45:20 -0500170 /**
171 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
172 */
173 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400174 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500175
Tracy Zhou7df93d22020-01-27 13:44:06 -0800176 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800177 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700178 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100179 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700180
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000181 /**
182 * An immutable list of supported profiles.
183 */
184 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700185
sfufa@google.comde013292021-09-21 18:22:44 -0700186 @Nullable
187 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500188
Sunny Goyal6f866092016-03-17 17:04:15 -0700189 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700190 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700191
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700192 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700193
Sunny Goyalf633ef52018-03-13 09:57:05 -0700194 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800195 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700196
Sunny Goyalbbf01842015-10-08 07:41:15 -0700197 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700198 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700199 String gridName = getCurrentGridName(context);
200 String newGridName = initGrid(context, gridName);
201 if (!newGridName.equals(gridName)) {
202 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
203 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700204 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700205
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700206 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100207 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000208 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
209 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100210 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700211 }
212 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700213 }
214
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700215 /**
216 * This constructor should NOT have any monitors by design.
217 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800218 public InvariantDeviceProfile(Context context, String gridName) {
219 String newName = initGrid(context, gridName);
220 if (newName == null || !newName.equals(gridName)) {
221 throw new IllegalArgumentException("Unknown grid name");
222 }
223 }
224
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700225 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800226 * This constructor should NOT have any monitors by design.
227 */
228 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700229 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100230 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700231 String gridName = getCurrentGridName(context);
232
233 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000234 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
235 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700236 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000237 defaultInfo,
238 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
239 /*allowDisabledGrid=*/false),
240 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700241
Alex Chau661f02d2022-06-07 14:03:43 +0100242 Context displayContext = context.createDisplayContext(display);
243 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000244 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700245 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000246 myInfo,
247 getPredefinedDeviceProfiles(context, gridName, deviceType,
248 /*allowDisabledGrid=*/false),
249 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700250
251 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
252 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100253 result.iconSizes[INDEX_DEFAULT] =
254 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
255 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700256 result.iconSizes[i] = Math.min(
257 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000258 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700259
Thales Lima83bedbf2021-10-05 17:47:39 +0100260 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
261 COUNT_SIZES);
262 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
263 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500264
Alex Chau1c883d82021-12-01 18:43:10 +0000265 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800266 }
267
Alex Chau238aaee2021-10-06 16:15:24 +0100268 /**
269 * Reinitialize the current grid after a restore, where some grids might now be disabled.
270 */
271 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000272 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100273 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000274 String newGridName = initGrid(context, currentGridName);
275 String newDbFile = dbFile;
276 if (!newDbFile.equals(currentDbFile)) {
277 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100278 + ", migrating to: " + newGridName
279 + ", removing all other grid db files");
280 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
281 if (gridDbFile.equals(currentDbFile)) {
282 continue;
283 }
284 if (context.getDatabasePath(gridDbFile).delete()) {
285 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
286 }
287 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000288 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100289 }
290 }
291
Alex Chau1c883d82021-12-01 18:43:10 +0000292 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800293 int flagPhone = 1 << 0;
294 int flagTablet = 1 << 1;
295
296 int type = displayInfo.supportedBounds.stream()
297 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
298 .reduce(0, (a, b) -> a | b);
299 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
300 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000301 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800302 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000303 return TYPE_TABLET;
304 } else {
305 return TYPE_PHONE;
306 }
307 }
308
Tracy Zhou42255d22020-03-13 00:38:11 -0700309 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700310 return Utilities.isGridOptionsEnabled(context)
311 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700312 }
313
Sunny Goyaleff44f32019-01-09 17:29:49 -0800314 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700315 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000316 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700317
318 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000319 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100320 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700321 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000322 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
323 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700324 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800325 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700326
Thales Limab67bfa72022-11-02 15:30:11 +0000327 @VisibleForTesting
328 public static String getDefaultGridName(Context context) {
329 return new InvariantDeviceProfile().initGrid(context, null);
330 }
331
Alex Chau1c883d82021-12-01 18:43:10 +0000332 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
333 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700334 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700335 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000336 numRows = closestProfile.numRows;
337 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000338 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000339 dbFile = closestProfile.dbFile;
340 defaultLayoutId = closestProfile.defaultLayoutId;
341 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300342
Sunny Goyalae190ff2020-04-14 00:19:01 +0000343 numFolderRows = closestProfile.numFolderRows;
344 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300345 folderStyle = closestProfile.folderStyle;
346
Jon Mirandae126d722021-02-25 10:45:20 -0500347 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400348 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000349 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000350
Vinit Nayakc7293172022-07-18 16:41:50 -0700351 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
352
Sunny Goyalae190ff2020-04-14 00:19:01 +0000353 mExtraAttrs = closestProfile.extraAttrs;
354
Thales Lima83bedbf2021-10-05 17:47:39 +0100355 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100356 float maxIconSize = iconSize[0];
357 for (int i = 1; i < iconSize.length; i++) {
358 maxIconSize = Math.max(maxIconSize, iconSize[i]);
359 }
360 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000361 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
362
Thales Lima83bedbf2021-10-05 17:47:39 +0100363 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700364
Thales Lima83bedbf2021-10-05 17:47:39 +0100365 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100366
Thales Lima83bedbf2021-10-05 17:47:39 +0100367 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100368
Thales Limad90faab2021-09-21 17:18:47 +0100369 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100370
Sunny Goyal19ff7282021-04-22 10:12:54 -0700371 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000372 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700373 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300374 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100375 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
376 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700377
378 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000379 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700380 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500381
Thales Limab7ef5692022-03-10 10:32:36 +0000382 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000383 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
384 allAppsIconSize = displayOption.allAppsIconSizes;
385 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100386 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000387 allAppsIconSize = iconSize;
388 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000389 }
390
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400391 if (devicePaddingId != 0) {
392 devicePaddings = new DevicePaddings(context, devicePaddingId);
393 }
394
Thales Lima11af7bc2022-08-12 15:24:54 +0100395 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000396
Sunny Goyalae190ff2020-04-14 00:19:01 +0000397 // If the partner customization apk contains any grid overrides, apply them
398 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700399 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700400
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000401 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700402 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700403 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700404 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000405 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000406 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000407 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700408 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000409 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700410
Sunny Goyal19ff7282021-04-22 10:12:54 -0700411 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
412 int displayWidth = bounds.bounds.width();
413 int displayHeight = bounds.bounds.height();
414 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700415
Sunny Goyal19ff7282021-04-22 10:12:54 -0700416 // We need to ensure that there is enough extra space in the wallpaper
417 // for the intended parallax effects
418 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300419 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
420 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700421 ? 2
422 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
423 defaultWallpaperSize.x =
424 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700425 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000426 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700427
428 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
429 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700430 }
431
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700432 public void addOnChangeListener(OnIDPChangeListener listener) {
433 mChangeListeners.add(listener);
434 }
435
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800436 public void removeOnChangeListener(OnIDPChangeListener listener) {
437 mChangeListeners.remove(listener);
438 }
439
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800440
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800441 public void setCurrentGrid(Context context, String gridName) {
442 Context appContext = context.getApplicationContext();
443 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700444 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800445 }
446
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700447 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700448 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000449 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
450 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700451 }
452
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700453 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700454 Object[] oldState = toModelState();
455
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700456 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700457 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700458 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700459
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700460 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700461 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700462 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700463 }
464 }
465
Alex Chau1c883d82021-12-01 18:43:10 +0000466 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
467 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800468 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100469
Alex Chau1c883d82021-12-01 18:43:10 +0000470 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700471 final int depth = parser.getDepth();
472 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700473 while (((type = parser.next()) != XmlPullParser.END_TAG ||
474 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800475 if ((type == XmlPullParser.START_TAG)
476 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800477
Alex Chau1c883d82021-12-01 18:43:10 +0000478 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
479 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100480 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100481 final int displayDepth = parser.getDepth();
482 while (((type = parser.next()) != XmlPullParser.END_TAG
483 || parser.getDepth() > displayDepth)
484 && type != XmlPullParser.END_DOCUMENT) {
485 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
486 parser.getName())) {
487 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100488 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100489 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800490 }
491 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700492 }
493 }
sfufa@google.comde013292021-09-21 18:22:44 -0700494 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700495 throw new RuntimeException(e);
496 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800497
Sunny Goyalae190ff2020-04-14 00:19:01 +0000498 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800499 if (!TextUtils.isEmpty(gridName)) {
500 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100501 if (gridName.equals(option.grid.name)
502 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000503 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800504 }
505 }
506 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000507 if (filteredProfiles.isEmpty()) {
508 // No grid found, use the default options
509 for (DisplayOption option : profiles) {
510 if (option.canBeDefault) {
511 filteredProfiles.add(option);
512 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800513 }
514 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000515 if (filteredProfiles.isEmpty()) {
516 throw new RuntimeException("No display option with canBeDefault=true");
517 }
518 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700519 }
520
Thales Lima7ec83822021-08-05 13:32:10 +0100521 /**
522 * @return all the grid options that can be shown on the device
523 */
524 public List<GridOption> parseAllGridOptions(Context context) {
525 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100526
Alex Chau1c883d82021-12-01 18:43:10 +0000527 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100528 final int depth = parser.getDepth();
529 int type;
530 while (((type = parser.next()) != XmlPullParser.END_TAG
531 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
532 if ((type == XmlPullParser.START_TAG)
533 && GridOption.TAG_NAME.equals(parser.getName())) {
534 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000535 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100536 if (option.isEnabled) {
537 result.add(option);
538 }
539 }
540 }
541 } catch (IOException | XmlPullParserException e) {
542 Log.e(TAG, "Error parsing device profile", e);
543 return Collections.emptyList();
544 }
545 return result;
546 }
547
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700548 private int getLauncherIconDensity(int requiredSize) {
549 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700550 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700551 DisplayMetrics.DENSITY_LOW,
552 DisplayMetrics.DENSITY_MEDIUM,
553 DisplayMetrics.DENSITY_TV,
554 DisplayMetrics.DENSITY_HIGH,
555 DisplayMetrics.DENSITY_XHIGH,
556 DisplayMetrics.DENSITY_XXHIGH,
557 DisplayMetrics.DENSITY_XXXHIGH
558 };
559
560 int density = DisplayMetrics.DENSITY_XXXHIGH;
561 for (int i = densityBuckets.length - 1; i >= 0; i--) {
562 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
563 / DisplayMetrics.DENSITY_DEFAULT;
564 if (expectedSize >= requiredSize) {
565 density = densityBuckets[i];
566 }
567 }
568
569 return density;
570 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700571
Adam Cohen2e6da152015-05-06 11:42:25 -0700572 /**
573 * Apply any Partner customization grid overrides.
574 *
575 * Currently we support: all apps row / column count.
576 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700577 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
578 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700579 if (p != null) {
580 p.applyInvariantDeviceProfileOverrides(this, dm);
581 }
582 }
583
Sunny Goyal415f1732018-11-29 10:33:47 -0800584 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700585 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700586 }
587
Sunny Goyal19ff7282021-04-22 10:12:54 -0700588 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000589 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700590 int minWidthPx = Integer.MAX_VALUE;
591 int minHeightPx = Integer.MAX_VALUE;
592 for (WindowBounds bounds : displayInfo.supportedBounds) {
593 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000594 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700595 // For split displays, take half width per page
596 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
597 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700598
Sunny Goyal19ff7282021-04-22 10:12:54 -0700599 } else if (!isTablet && bounds.isLandscape()) {
600 // We will use transposed layout in this case
601 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
602 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
603 } else {
604 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
605 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
606 }
607 }
608
Thales Lima425f6822022-05-10 13:44:58 -0300609 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
610 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700611
612 // Sort the profiles based on the closeness to the device size
613 Collections.sort(points, (a, b) ->
614 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
615 dist(width, height, b.minWidthDps, b.minHeightDps)));
616
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700617 DisplayOption closestPoint = points.get(0);
618 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700619 float weights = 0;
620
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700621 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
622 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700623 }
624
625 DisplayOption out = new DisplayOption(closestOption);
626 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700627 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700628 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
629 weights += w;
630 out.add(new DisplayOption().add(p).multiply(w));
631 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700632 out.multiply(1.0f / weights);
633
Thales Lima6e0005a2021-10-27 15:53:41 +0100634 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700635 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100636 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
637 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700638 }
639
640 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700641 }
642
Sunny Goyal27835952017-01-13 12:15:53 -0800643 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700644 Resources res = context.getResources();
645 Configuration config = context.getResources().getConfiguration();
646
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400647 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
648 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100649 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
650
Alex Chau5dfd9392022-11-02 17:39:40 +0000651 if (Utilities.IS_DEBUG_DEVICE) {
652 StringWriter stringWriter = new StringWriter();
653 PrintWriter printWriter = new PrintWriter(stringWriter);
654 DisplayController.INSTANCE.get(context).dump(printWriter);
655 printWriter.flush();
656 Log.d("b/253338238", "getDeviceProfile -"
657 + "\nconfig: " + config
658 + "\ndisplayMetrics: " + res.getDisplayMetrics()
659 + "\nrotation: " + rotation
660 + "\n" + stringWriter,
661 new Exception());
662 }
Alex Chaud3e8cc42022-05-03 17:45:34 +0100663 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400664 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700665
Sunny Goyal187b16c2022-03-01 16:53:23 -0800666 /**
667 * Returns the device profile matching the provided screen configuration
668 */
669 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700670 DeviceProfile bestMatch = supportedProfiles.get(0);
671 float minDiff = Float.MAX_VALUE;
672
673 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400674 float diff = Math.abs(profile.widthPx - screenWidth)
675 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700676 if (diff < minDiff) {
677 minDiff = diff;
678 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800679 } else if (diff == minDiff && profile.rotationHint == rotation) {
680 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700681 }
682 }
683 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800684 }
685
Sunny Goyal415f1732018-11-29 10:33:47 -0800686 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700687 float d = dist(x0, y0, x1, y1);
688 if (Float.compare(d, 0f) == 0) {
689 return Float.POSITIVE_INFINITY;
690 }
691 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
692 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700693
694 /**
695 * As a ratio of screen height, the total distance we want the parallax effect to span
696 * horizontally
697 */
698 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
699 float aspectRatio = width / (float) height;
700
701 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
702 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
703 // We will use these two data points to extrapolate how much the wallpaper parallax effect
704 // to span (ie travel) at any aspect ratio:
705
sfufa@google.comde013292021-09-21 18:22:44 -0700706 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
707 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700708 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
709 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
710
711 // To find out the desired width at different aspect ratios, we use the following two
712 // formulas, where the coefficient on x is the aspect ratio (width/height):
713 // (16/10)x + y = 1.5
714 // (10/16)x + y = 1.2
715 // We solve for x and y and end up with a final formula:
716 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700717 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
718 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700719 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
720 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
721 return x * aspectRatio + y;
722 }
723
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700724 public interface OnIDPChangeListener {
725
Sunny Goyalb47172b2021-05-03 19:59:51 -0700726 /**
727 * Called when the device provide changes
728 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700729 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700730 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800731
732
Sunny Goyaleff44f32019-01-09 17:29:49 -0800733 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800734
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800735 public static final String TAG_NAME = "grid-option";
736
Alex Chau1c883d82021-12-01 18:43:10 +0000737 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
738 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
739 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
740 private static final int DEVICE_CATEGORY_ALL =
741 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
742
Thales Lima11af7bc2022-08-12 15:24:54 +0100743 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
744 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
745 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
746 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
747 private static final int DONT_INLINE_QSB = 0;
748
Sunny Goyaleff44f32019-01-09 17:29:49 -0800749 public final String name;
750 public final int numRows;
751 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000752 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100753 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800754
755 private final int numFolderRows;
756 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300757 private final @StyleRes int folderStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800758
Sunny Goyal19ff7282021-04-22 10:12:54 -0700759 private final int numAllAppsColumns;
760 private final int numDatabaseAllAppsColumns;
761 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700762 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100763
Thales Lima425f6822022-05-10 13:44:58 -0300764 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800765
Thales Lima11af7bc2022-08-12 15:24:54 +0100766 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
767
Thales Limab35faed2022-09-05 16:30:01 -0300768 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800769 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000770
Sunny Goyal415f1732018-11-29 10:33:47 -0800771 private final int defaultLayoutId;
772 private final int demoModeLayoutId;
773
Jon Mirandae126d722021-02-25 10:45:20 -0500774 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400775 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500776
Sunny Goyal5bc18462019-01-07 15:13:39 -0800777 private final SparseArray<TypedValue> extraAttrs;
778
Alex Chau1c883d82021-12-01 18:43:10 +0000779 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800780 TypedArray a = context.obtainStyledAttributes(
781 attrs, R.styleable.GridDisplayOption);
782 name = a.getString(R.styleable.GridDisplayOption_name);
783 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
784 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000785 numSearchContainerColumns = a.getInt(
786 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800787
Tracy Zhou7df93d22020-01-27 13:44:06 -0800788 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000789 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100790 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
791 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
792 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800793 demoModeLayoutId = a.getResourceId(
794 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700795
796 numAllAppsColumns = a.getInt(
797 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
798 numDatabaseAllAppsColumns = a.getInt(
799 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
800
801 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800802 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700803 numDatabaseHotseatIcons = a.getInt(
804 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100805
Thales Lima425f6822022-05-10 13:44:58 -0300806 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
807 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
808 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
809 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
810 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
811 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
812 numColumns);
813 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
814 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
815 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700816
Vinit Nayakc7293172022-07-18 16:41:50 -0700817 inlineNavButtonsEndSpacing =
818 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
819 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300820
Sunny Goyal415f1732018-11-29 10:33:47 -0800821 numFolderRows = a.getInt(
822 R.styleable.GridDisplayOption_numFolderRows, numRows);
823 numFolderColumns = a.getInt(
824 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700825
Thales Limab35faed2022-09-05 16:30:01 -0300826 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
827 INVALID_RESOURCE_HANDLE);
828
Jon Mirandae126d722021-02-25 10:45:20 -0500829 isScalable = a.getBoolean(
830 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400831 devicePaddingId = a.getResourceId(
832 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500833
Alex Chau1c883d82021-12-01 18:43:10 +0000834 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
835 DEVICE_CATEGORY_ALL);
836 isEnabled = (deviceType == TYPE_PHONE
837 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
838 || (deviceType == TYPE_TABLET
839 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
840 || (deviceType == TYPE_MULTI_DISPLAY
841 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
842 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100843
Thales Lima11af7bc2022-08-12 15:24:54 +0100844 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
845 DONT_INLINE_QSB);
846 inlineQsb[INDEX_DEFAULT] =
847 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
848 inlineQsb[INDEX_LANDSCAPE] =
849 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
850 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
851 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
852 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
853 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
854 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
855 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
856
Sunny Goyal415f1732018-11-29 10:33:47 -0800857 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800858 extraAttrs = Themes.createValueMap(context, attrs,
859 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800860 }
861 }
862
Sunny Goyal19ff7282021-04-22 10:12:54 -0700863 @VisibleForTesting
864 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700865 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800866
Sunny Goyal415f1732018-11-29 10:33:47 -0800867 private final float minWidthDps;
868 private final float minHeightDps;
869 private final boolean canBeDefault;
870
Thales Lima83bedbf2021-10-05 17:47:39 +0100871 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100872
Thales Lima78d00ad2021-09-30 11:29:06 +0100873 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100874 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100875 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
876 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100877
Thales Lima78d00ad2021-09-30 11:29:06 +0100878 private final float[] iconSizes = new float[COUNT_SIZES];
879 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800880
Thales Limab7ef5692022-03-10 10:32:36 +0000881 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000882 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
883 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
884 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
885
Thales Lima1de4d552021-10-13 16:13:25 +0100886 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800887 this.grid = grid;
888
Thales Lima1de4d552021-10-13 16:13:25 +0100889 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800890
Sunny Goyal415f1732018-11-29 10:33:47 -0800891 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
892 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700893
Thales Lima1de4d552021-10-13 16:13:25 +0100894 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800895
Thales Lima83bedbf2021-10-05 17:47:39 +0100896 float x;
897 float y;
898
Thales Lima85c942f2021-12-31 13:04:20 +0000899 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
900 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100901 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000902
903 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
904 minCellSize[INDEX_DEFAULT].x);
905 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
906 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100907 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100908
Thales Lima85c942f2021-12-31 13:04:20 +0000909 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100910 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000911 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100912 minCellSize[INDEX_DEFAULT].y);
913 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
914
Thales Lima85c942f2021-12-31 13:04:20 +0000915 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100916 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000917 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100918 minCellSize[INDEX_DEFAULT].y);
919 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100920
Thales Lima85c942f2021-12-31 13:04:20 +0000921 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000922 float borderSpaceLandscape = a.getFloat(
923 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000924 float borderSpaceTwoPanelPortrait = a.getFloat(
925 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
926 float borderSpaceTwoPanelLandscape = a.getFloat(
927 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100928
Thales Lima85c942f2021-12-31 13:04:20 +0000929 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
930 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100931 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000932
933 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
934 borderSpaceLandscape);
935 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
936 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100937 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
938
939 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000940 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
941 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100942 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000943 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
944 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100945 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
946
947 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000948 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
949 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100950 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000951 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
952 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100953 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
954
Thales Limab7ef5692022-03-10 10:32:36 +0000955 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
956 minCellSize[INDEX_DEFAULT].x);
957 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
958 minCellSize[INDEX_DEFAULT].y);
959 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
960
961 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
962 allAppsCellSize[INDEX_DEFAULT].x);
963 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
964 allAppsCellSize[INDEX_DEFAULT].y);
965 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
966
967 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
968 allAppsCellSize[INDEX_DEFAULT].x);
969 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
970 allAppsCellSize[INDEX_DEFAULT].y);
971 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
972
973 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
974 allAppsCellSize[INDEX_DEFAULT].x);
975 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
976 allAppsCellSize[INDEX_DEFAULT].y);
977 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
978
Thales Lima080d8902022-03-28 15:30:29 +0100979 float allAppsBorderSpace = a.getFloat(
980 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
981 float allAppsBorderSpaceLandscape = a.getFloat(
982 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
983 allAppsBorderSpace);
984 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000985 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100986 allAppsBorderSpace);
987 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000988 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100989 allAppsBorderSpace);
990
991 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
992 allAppsBorderSpace);
993 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
994 allAppsBorderSpace);
995 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
996
997 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
998 allAppsBorderSpaceLandscape);
999 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
1000 allAppsBorderSpaceLandscape);
1001 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
1002
1003 x = a.getFloat(
1004 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
1005 allAppsBorderSpaceTwoPanelPortrait);
1006 y = a.getFloat(
1007 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
1008 allAppsBorderSpaceTwoPanelPortrait);
1009 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1010
1011 x = a.getFloat(
1012 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1013 allAppsBorderSpaceTwoPanelLandscape);
1014 y = a.getFloat(
1015 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1016 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001017 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001018
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001019 iconSizes[INDEX_DEFAULT] =
1020 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1021 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001022 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001023 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001024 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001025 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001026 iconSizes[INDEX_DEFAULT]);
1027 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001028 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001029 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001030
Thales Limabb7d3882021-12-22 12:56:29 +00001031 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1032 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001033 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1034 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001035 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001036 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1037 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1038 allAppsIconSizes[INDEX_DEFAULT]);
1039 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1040 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1041 allAppsIconSizes[INDEX_DEFAULT]);
1042
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001043 textSizes[INDEX_DEFAULT] =
1044 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1045 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001046 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001047 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001048 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001049 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001050 textSizes[INDEX_DEFAULT]);
1051 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001052 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001053 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001054
Thales Limabb7d3882021-12-22 12:56:29 +00001055 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1056 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1057 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1058 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1059 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1060 allAppsIconTextSizes[INDEX_DEFAULT]);
1061 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1062 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1063 allAppsIconTextSizes[INDEX_DEFAULT]);
1064
Thales Lima83bedbf2021-10-05 17:47:39 +01001065 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1066 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001067 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1068 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1069 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001070 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001071 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001072 horizontalMargin[INDEX_DEFAULT]);
1073 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001074 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001075 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001076
Thales Limab8c05952022-05-23 16:58:38 +01001077 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1078 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1079 ResourcesCompat.getFloat(context.getResources(),
1080 R.dimen.hotseat_bar_bottom_space_default));
1081 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1082 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1083 hotseatBarBottomSpace[INDEX_DEFAULT]);
1084 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1085 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1086 hotseatBarBottomSpace[INDEX_DEFAULT]);
1087 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1088 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1089 hotseatBarBottomSpace[INDEX_DEFAULT]);
1090
1091 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1092 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1093 ResourcesCompat.getFloat(context.getResources(),
1094 R.dimen.hotseat_qsb_space_default));
1095 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1096 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1097 hotseatQsbSpace[INDEX_DEFAULT]);
1098 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1099 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1100 hotseatQsbSpace[INDEX_DEFAULT]);
1101 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1102 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1103 hotseatQsbSpace[INDEX_DEFAULT]);
1104
Sunny Goyal415f1732018-11-29 10:33:47 -08001105 a.recycle();
1106 }
1107
1108 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001109 this(null);
1110 }
1111
1112 DisplayOption(GridOption grid) {
1113 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001114 minWidthDps = 0;
1115 minHeightDps = 0;
1116 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001117 for (int i = 0; i < COUNT_SIZES; i++) {
1118 iconSizes[i] = 0;
1119 textSizes[i] = 0;
1120 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001121 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001122 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001123 allAppsIconSizes[i] = 0;
1124 allAppsIconTextSizes[i] = 0;
1125 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001126 }
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 Lima78d00ad2021-09-30 11:29:06 +01001147
Sunny Goyal415f1732018-11-29 10:33:47 -08001148 return this;
1149 }
1150
1151 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001152 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001153 iconSizes[i] += p.iconSizes[i];
1154 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001155 borderSpaces[i].x += p.borderSpaces[i].x;
1156 borderSpaces[i].y += p.borderSpaces[i].y;
1157 minCellSize[i].x += p.minCellSize[i].x;
1158 minCellSize[i].y += p.minCellSize[i].y;
1159 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001160 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1161 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001162 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1163 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001164 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1165 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1166 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1167 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001168 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001169
Sunny Goyal415f1732018-11-29 10:33:47 -08001170 return this;
1171 }
1172 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001173}