blob: 67620e3ef94b5aef237d5a17244a6964e83f5b0c [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;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000131 public float[] hotseatBorderSpaces;
Vinit Nayakc7293172022-07-18 16:41:50 -0700132 public int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100133
Thales Lima83bedbf2021-10-05 17:47:39 +0100134 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500135
Thales Limab7ef5692022-03-10 10:32:36 +0000136 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000137 public float[] allAppsIconSize;
138 public float[] allAppsIconTextSize;
139 public PointF[] allAppsBorderSpaces;
140
Sunny Goyal5bc18462019-01-07 15:13:39 -0800141 private SparseArray<TypedValue> mExtraAttrs;
142
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700143 /**
144 * Number of icons inside the hotseat area.
145 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800146 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700147
148 /**
Alex Chau6ed408f2022-03-04 12:58:05 +0000149 * Number of icons inside the hotseat area when using 3 buttons navigation.
150 */
151 public int numShrunkenHotseatIcons;
152
153 /**
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700154 * 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 Lima12d0eff2022-03-25 17:06:11 +0000179 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);
Thales Lima12d0eff2022-03-25 17:06:11 +0000264 System.arraycopy(defaultDisplayOption.inlineQsb, 0, result.inlineQsb, 0,
265 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500266
Alex Chau1c883d82021-12-01 18:43:10 +0000267 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800268 }
269
Alex Chau238aaee2021-10-06 16:15:24 +0100270 /**
271 * Reinitialize the current grid after a restore, where some grids might now be disabled.
272 */
273 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000274 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100275 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000276 String newGridName = initGrid(context, currentGridName);
277 String newDbFile = dbFile;
278 if (!newDbFile.equals(currentDbFile)) {
279 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100280 + ", migrating to: " + newGridName
281 + ", removing all other grid db files");
282 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
283 if (gridDbFile.equals(currentDbFile)) {
284 continue;
285 }
286 if (context.getDatabasePath(gridDbFile).delete()) {
287 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
288 }
289 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000290 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100291 }
292 }
293
Alex Chau1c883d82021-12-01 18:43:10 +0000294 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800295 int flagPhone = 1 << 0;
296 int flagTablet = 1 << 1;
297
298 int type = displayInfo.supportedBounds.stream()
299 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
300 .reduce(0, (a, b) -> a | b);
301 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
302 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000303 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800304 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000305 return TYPE_TABLET;
306 } else {
307 return TYPE_PHONE;
308 }
309 }
310
Tracy Zhou42255d22020-03-13 00:38:11 -0700311 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700312 return Utilities.isGridOptionsEnabled(context)
313 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700314 }
315
Sunny Goyaleff44f32019-01-09 17:29:49 -0800316 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700317 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000318 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700319
320 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000321 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100322 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700323 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000324 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
325 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700326 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800327 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700328
Alex Chau1c883d82021-12-01 18:43:10 +0000329 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
330 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700331 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700332 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000333 numRows = closestProfile.numRows;
334 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000335 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000336 dbFile = closestProfile.dbFile;
337 defaultLayoutId = closestProfile.defaultLayoutId;
338 demoModeLayoutId = closestProfile.demoModeLayoutId;
339 numFolderRows = closestProfile.numFolderRows;
340 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500341 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400342 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000343 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000344
Vinit Nayakc7293172022-07-18 16:41:50 -0700345 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
346
Sunny Goyalae190ff2020-04-14 00:19:01 +0000347 mExtraAttrs = closestProfile.extraAttrs;
348
Thales Lima83bedbf2021-10-05 17:47:39 +0100349 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100350 float maxIconSize = iconSize[0];
351 for (int i = 1; i < iconSize.length; i++) {
352 maxIconSize = Math.max(maxIconSize, iconSize[i]);
353 }
354 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000355 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
356
Thales Lima83bedbf2021-10-05 17:47:39 +0100357 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700358
Thales Lima83bedbf2021-10-05 17:47:39 +0100359 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100360
Thales Lima83bedbf2021-10-05 17:47:39 +0100361 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100362 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700363
Thales Limad90faab2021-09-21 17:18:47 +0100364 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100365
Sunny Goyal19ff7282021-04-22 10:12:54 -0700366 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000367 numShrunkenHotseatIcons = closestProfile.numShrunkenHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000368 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700369 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300370 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000371 hotseatBorderSpaces = displayOption.hotseatBorderSpaces;
Thales Limab8c05952022-05-23 16:58:38 +0100372 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
373 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700374
375 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000376 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700377 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500378
Thales Limab7ef5692022-03-10 10:32:36 +0000379 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000380 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
381 allAppsIconSize = displayOption.allAppsIconSizes;
382 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100383 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000384 allAppsIconSize = iconSize;
385 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000386 }
387
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400388 if (devicePaddingId != 0) {
389 devicePaddings = new DevicePaddings(context, devicePaddingId);
390 }
391
Thales Lima12d0eff2022-03-25 17:06:11 +0000392 inlineQsb = displayOption.inlineQsb;
393
Sunny Goyalae190ff2020-04-14 00:19:01 +0000394 // If the partner customization apk contains any grid overrides, apply them
395 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700396 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700397
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000398 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700399 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700400 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700401 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000402 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000403 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000404 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700405 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000406 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700407
Sunny Goyal19ff7282021-04-22 10:12:54 -0700408 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
409 int displayWidth = bounds.bounds.width();
410 int displayHeight = bounds.bounds.height();
411 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700412
Sunny Goyal19ff7282021-04-22 10:12:54 -0700413 // We need to ensure that there is enough extra space in the wallpaper
414 // for the intended parallax effects
415 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300416 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
417 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700418 ? 2
419 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
420 defaultWallpaperSize.x =
421 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700422 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000423 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700424
425 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
426 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700427 }
428
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700429 public void addOnChangeListener(OnIDPChangeListener listener) {
430 mChangeListeners.add(listener);
431 }
432
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800433 public void removeOnChangeListener(OnIDPChangeListener listener) {
434 mChangeListeners.remove(listener);
435 }
436
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800437
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800438 public void setCurrentGrid(Context context, String gridName) {
439 Context appContext = context.getApplicationContext();
440 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700441 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800442 }
443
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700444 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700445 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000446 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
447 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700448 }
449
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700450 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700451 Object[] oldState = toModelState();
452
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700453 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700454 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700455 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700456
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700457 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700458 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700459 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700460 }
461 }
462
Alex Chau1c883d82021-12-01 18:43:10 +0000463 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
464 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800465 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100466
Alex Chau1c883d82021-12-01 18:43:10 +0000467 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700468 final int depth = parser.getDepth();
469 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700470 while (((type = parser.next()) != XmlPullParser.END_TAG ||
471 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800472 if ((type == XmlPullParser.START_TAG)
473 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800474
Alex Chau1c883d82021-12-01 18:43:10 +0000475 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
476 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100477 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100478 final int displayDepth = parser.getDepth();
479 while (((type = parser.next()) != XmlPullParser.END_TAG
480 || parser.getDepth() > displayDepth)
481 && type != XmlPullParser.END_DOCUMENT) {
482 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
483 parser.getName())) {
484 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100485 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100486 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800487 }
488 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700489 }
490 }
sfufa@google.comde013292021-09-21 18:22:44 -0700491 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700492 throw new RuntimeException(e);
493 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800494
Sunny Goyalae190ff2020-04-14 00:19:01 +0000495 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800496 if (!TextUtils.isEmpty(gridName)) {
497 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100498 if (gridName.equals(option.grid.name)
499 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000500 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800501 }
502 }
503 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000504 if (filteredProfiles.isEmpty()) {
505 // No grid found, use the default options
506 for (DisplayOption option : profiles) {
507 if (option.canBeDefault) {
508 filteredProfiles.add(option);
509 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800510 }
511 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000512 if (filteredProfiles.isEmpty()) {
513 throw new RuntimeException("No display option with canBeDefault=true");
514 }
515 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700516 }
517
Thales Lima7ec83822021-08-05 13:32:10 +0100518 /**
519 * @return all the grid options that can be shown on the device
520 */
521 public List<GridOption> parseAllGridOptions(Context context) {
522 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100523
Alex Chau1c883d82021-12-01 18:43:10 +0000524 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100525 final int depth = parser.getDepth();
526 int type;
527 while (((type = parser.next()) != XmlPullParser.END_TAG
528 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
529 if ((type == XmlPullParser.START_TAG)
530 && GridOption.TAG_NAME.equals(parser.getName())) {
531 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000532 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100533 if (option.isEnabled) {
534 result.add(option);
535 }
536 }
537 }
538 } catch (IOException | XmlPullParserException e) {
539 Log.e(TAG, "Error parsing device profile", e);
540 return Collections.emptyList();
541 }
542 return result;
543 }
544
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700545 private int getLauncherIconDensity(int requiredSize) {
546 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700547 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700548 DisplayMetrics.DENSITY_LOW,
549 DisplayMetrics.DENSITY_MEDIUM,
550 DisplayMetrics.DENSITY_TV,
551 DisplayMetrics.DENSITY_HIGH,
552 DisplayMetrics.DENSITY_XHIGH,
553 DisplayMetrics.DENSITY_XXHIGH,
554 DisplayMetrics.DENSITY_XXXHIGH
555 };
556
557 int density = DisplayMetrics.DENSITY_XXXHIGH;
558 for (int i = densityBuckets.length - 1; i >= 0; i--) {
559 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
560 / DisplayMetrics.DENSITY_DEFAULT;
561 if (expectedSize >= requiredSize) {
562 density = densityBuckets[i];
563 }
564 }
565
566 return density;
567 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700568
Adam Cohen2e6da152015-05-06 11:42:25 -0700569 /**
570 * Apply any Partner customization grid overrides.
571 *
572 * Currently we support: all apps row / column count.
573 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700574 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
575 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700576 if (p != null) {
577 p.applyInvariantDeviceProfileOverrides(this, dm);
578 }
579 }
580
Sunny Goyal415f1732018-11-29 10:33:47 -0800581 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700582 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700583 }
584
Sunny Goyal19ff7282021-04-22 10:12:54 -0700585 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000586 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700587 int minWidthPx = Integer.MAX_VALUE;
588 int minHeightPx = Integer.MAX_VALUE;
589 for (WindowBounds bounds : displayInfo.supportedBounds) {
590 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000591 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700592 // For split displays, take half width per page
593 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
594 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700595
Sunny Goyal19ff7282021-04-22 10:12:54 -0700596 } else if (!isTablet && bounds.isLandscape()) {
597 // We will use transposed layout in this case
598 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
599 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
600 } else {
601 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
602 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
603 }
604 }
605
Thales Lima425f6822022-05-10 13:44:58 -0300606 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
607 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700608
609 // Sort the profiles based on the closeness to the device size
610 Collections.sort(points, (a, b) ->
611 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
612 dist(width, height, b.minWidthDps, b.minHeightDps)));
613
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700614 DisplayOption closestPoint = points.get(0);
615 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700616 float weights = 0;
617
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700618 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
619 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700620 }
621
622 DisplayOption out = new DisplayOption(closestOption);
623 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700624 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700625 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
626 weights += w;
627 out.add(new DisplayOption().add(p).multiply(w));
628 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700629 out.multiply(1.0f / weights);
630
Thales Lima6e0005a2021-10-27 15:53:41 +0100631 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700632 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100633 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
634 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700635 }
636
637 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700638 }
639
Sunny Goyal27835952017-01-13 12:15:53 -0800640 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700641 Resources res = context.getResources();
642 Configuration config = context.getResources().getConfiguration();
643
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400644 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
645 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100646 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
647
Alex Chaud3e8cc42022-05-03 17:45:34 +0100648 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400649 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700650
Sunny Goyal187b16c2022-03-01 16:53:23 -0800651 /**
652 * Returns the device profile matching the provided screen configuration
653 */
654 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700655 DeviceProfile bestMatch = supportedProfiles.get(0);
656 float minDiff = Float.MAX_VALUE;
657
658 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400659 float diff = Math.abs(profile.widthPx - screenWidth)
660 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700661 if (diff < minDiff) {
662 minDiff = diff;
663 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800664 } else if (diff == minDiff && profile.rotationHint == rotation) {
665 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700666 }
667 }
668 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800669 }
670
Sunny Goyal415f1732018-11-29 10:33:47 -0800671 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700672 float d = dist(x0, y0, x1, y1);
673 if (Float.compare(d, 0f) == 0) {
674 return Float.POSITIVE_INFINITY;
675 }
676 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
677 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700678
679 /**
680 * As a ratio of screen height, the total distance we want the parallax effect to span
681 * horizontally
682 */
683 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
684 float aspectRatio = width / (float) height;
685
686 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
687 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
688 // We will use these two data points to extrapolate how much the wallpaper parallax effect
689 // to span (ie travel) at any aspect ratio:
690
sfufa@google.comde013292021-09-21 18:22:44 -0700691 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
692 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700693 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
694 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
695
696 // To find out the desired width at different aspect ratios, we use the following two
697 // formulas, where the coefficient on x is the aspect ratio (width/height):
698 // (16/10)x + y = 1.5
699 // (10/16)x + y = 1.2
700 // We solve for x and y and end up with a final formula:
701 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700702 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
703 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700704 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
705 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
706 return x * aspectRatio + y;
707 }
708
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700709 public interface OnIDPChangeListener {
710
Sunny Goyalb47172b2021-05-03 19:59:51 -0700711 /**
712 * Called when the device provide changes
713 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700714 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700715 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800716
717
Sunny Goyaleff44f32019-01-09 17:29:49 -0800718 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800719
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800720 public static final String TAG_NAME = "grid-option";
721
Alex Chau1c883d82021-12-01 18:43:10 +0000722 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
723 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
724 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
725 private static final int DEVICE_CATEGORY_ALL =
726 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
727
Sunny Goyaleff44f32019-01-09 17:29:49 -0800728 public final String name;
729 public final int numRows;
730 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000731 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100732 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800733
734 private final int numFolderRows;
735 private final int numFolderColumns;
736
Sunny Goyal19ff7282021-04-22 10:12:54 -0700737 private final int numAllAppsColumns;
738 private final int numDatabaseAllAppsColumns;
739 private final int numHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000740 private final int numShrunkenHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700741 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100742
Thales Lima425f6822022-05-10 13:44:58 -0300743 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800744
Vinit Nayakc7293172022-07-18 16:41:50 -0700745 private int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800746 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000747
Sunny Goyal415f1732018-11-29 10:33:47 -0800748 private final int defaultLayoutId;
749 private final int demoModeLayoutId;
750
Jon Mirandae126d722021-02-25 10:45:20 -0500751 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400752 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500753
Sunny Goyal5bc18462019-01-07 15:13:39 -0800754 private final SparseArray<TypedValue> extraAttrs;
755
Alex Chau1c883d82021-12-01 18:43:10 +0000756 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800757 TypedArray a = context.obtainStyledAttributes(
758 attrs, R.styleable.GridDisplayOption);
759 name = a.getString(R.styleable.GridDisplayOption_name);
760 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
761 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000762 numSearchContainerColumns = a.getInt(
763 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800764
Tracy Zhou7df93d22020-01-27 13:44:06 -0800765 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000766 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100767 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
768 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
769 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800770 demoModeLayoutId = a.getResourceId(
771 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700772
773 numAllAppsColumns = a.getInt(
774 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
775 numDatabaseAllAppsColumns = a.getInt(
776 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
777
778 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800779 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Alex Chau6ed408f2022-03-04 12:58:05 +0000780 numShrunkenHotseatIcons = a.getInt(
781 R.styleable.GridDisplayOption_numShrunkenHotseatIcons, numHotseatIcons / 2);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700782 numDatabaseHotseatIcons = a.getInt(
783 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100784
Thales Lima425f6822022-05-10 13:44:58 -0300785 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
786 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
787 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
788 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
789 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
790 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
791 numColumns);
792 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
793 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
794 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700795
Vinit Nayakc7293172022-07-18 16:41:50 -0700796 inlineNavButtonsEndSpacing =
797 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
798 R.dimen.taskbar_button_margin_default);
Sunny Goyal415f1732018-11-29 10:33:47 -0800799 numFolderRows = a.getInt(
800 R.styleable.GridDisplayOption_numFolderRows, numRows);
801 numFolderColumns = a.getInt(
802 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700803
Jon Mirandae126d722021-02-25 10:45:20 -0500804 isScalable = a.getBoolean(
805 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400806 devicePaddingId = a.getResourceId(
807 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500808
Alex Chau1c883d82021-12-01 18:43:10 +0000809 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
810 DEVICE_CATEGORY_ALL);
811 isEnabled = (deviceType == TYPE_PHONE
812 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
813 || (deviceType == TYPE_TABLET
814 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
815 || (deviceType == TYPE_MULTI_DISPLAY
816 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
817 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100818
Sunny Goyal415f1732018-11-29 10:33:47 -0800819 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800820 extraAttrs = Themes.createValueMap(context, attrs,
821 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800822 }
823 }
824
Sunny Goyal19ff7282021-04-22 10:12:54 -0700825 @VisibleForTesting
826 static final class DisplayOption {
Thales Lima12d0eff2022-03-25 17:06:11 +0000827 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
828 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
829 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
830 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
831 private static final int DONT_INLINE_QSB = 0;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700832
833 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800834
Sunny Goyal415f1732018-11-29 10:33:47 -0800835 private final float minWidthDps;
836 private final float minHeightDps;
837 private final boolean canBeDefault;
Thales Lima12d0eff2022-03-25 17:06:11 +0000838 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800839
Thales Lima83bedbf2021-10-05 17:47:39 +0100840 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100841
842 private float folderBorderSpace;
843 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100844 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Lima425f6822022-05-10 13:44:58 -0300845 //TODO(http://b/228998082) remove this when 3 button spaces are fixed
Thales Lima6e7f36c2022-01-04 12:14:11 +0000846 private final float[] hotseatBorderSpaces = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100847 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
848 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100849
Thales Lima78d00ad2021-09-30 11:29:06 +0100850 private final float[] iconSizes = new float[COUNT_SIZES];
851 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800852
Thales Limab7ef5692022-03-10 10:32:36 +0000853 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000854 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
855 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
856 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
857
Thales Lima1de4d552021-10-13 16:13:25 +0100858 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800859 this.grid = grid;
860
Thales Lima1de4d552021-10-13 16:13:25 +0100861 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800862
Sunny Goyal415f1732018-11-29 10:33:47 -0800863 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
864 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700865
Thales Lima1de4d552021-10-13 16:13:25 +0100866 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800867
Thales Lima12d0eff2022-03-25 17:06:11 +0000868 int inlineForRotation = a.getInt(R.styleable.ProfileDisplayOption_inlineQsb,
869 DONT_INLINE_QSB);
870 inlineQsb[INDEX_DEFAULT] =
871 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
872 inlineQsb[INDEX_LANDSCAPE] =
873 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
874 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
875 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
876 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
877 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
878 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
879 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
880
Thales Lima83bedbf2021-10-05 17:47:39 +0100881 float x;
882 float y;
883
Thales Lima85c942f2021-12-31 13:04:20 +0000884 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
885 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100886 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000887
888 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
889 minCellSize[INDEX_DEFAULT].x);
890 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
891 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100892 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100893
Thales Lima85c942f2021-12-31 13:04:20 +0000894 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100895 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000896 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100897 minCellSize[INDEX_DEFAULT].y);
898 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
899
Thales Lima85c942f2021-12-31 13:04:20 +0000900 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100901 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000902 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100903 minCellSize[INDEX_DEFAULT].y);
904 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100905
Thales Lima85c942f2021-12-31 13:04:20 +0000906 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000907 float borderSpaceLandscape = a.getFloat(
908 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000909 float borderSpaceTwoPanelPortrait = a.getFloat(
910 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
911 float borderSpaceTwoPanelLandscape = a.getFloat(
912 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100913
Thales Lima85c942f2021-12-31 13:04:20 +0000914 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
915 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100916 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000917
918 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
919 borderSpaceLandscape);
920 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
921 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100922 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
923
924 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000925 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
926 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100927 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000928 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
929 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100930 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
931
932 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000933 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
934 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100935 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000936 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
937 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100938 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
939
Thales Limabb7d3882021-12-22 12:56:29 +0000940 folderBorderSpace = borderSpace;
941
Thales Limab7ef5692022-03-10 10:32:36 +0000942 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
943 minCellSize[INDEX_DEFAULT].x);
944 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
945 minCellSize[INDEX_DEFAULT].y);
946 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
947
948 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
949 allAppsCellSize[INDEX_DEFAULT].x);
950 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
951 allAppsCellSize[INDEX_DEFAULT].y);
952 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
953
954 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
955 allAppsCellSize[INDEX_DEFAULT].x);
956 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
957 allAppsCellSize[INDEX_DEFAULT].y);
958 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
959
960 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
961 allAppsCellSize[INDEX_DEFAULT].x);
962 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
963 allAppsCellSize[INDEX_DEFAULT].y);
964 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
965
Thales Lima080d8902022-03-28 15:30:29 +0100966 float allAppsBorderSpace = a.getFloat(
967 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
968 float allAppsBorderSpaceLandscape = a.getFloat(
969 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
970 allAppsBorderSpace);
971 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000972 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100973 allAppsBorderSpace);
974 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000975 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100976 allAppsBorderSpace);
977
978 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
979 allAppsBorderSpace);
980 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
981 allAppsBorderSpace);
982 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
983
984 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
985 allAppsBorderSpaceLandscape);
986 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
987 allAppsBorderSpaceLandscape);
988 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
989
990 x = a.getFloat(
991 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
992 allAppsBorderSpaceTwoPanelPortrait);
993 y = a.getFloat(
994 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
995 allAppsBorderSpaceTwoPanelPortrait);
996 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
997
998 x = a.getFloat(
999 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1000 allAppsBorderSpaceTwoPanelLandscape);
1001 y = a.getFloat(
1002 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1003 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001004 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001005
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001006 iconSizes[INDEX_DEFAULT] =
1007 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1008 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001009 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001010 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001011 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001012 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001013 iconSizes[INDEX_DEFAULT]);
1014 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001015 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001016 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001017
Thales Limabb7d3882021-12-22 12:56:29 +00001018 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1019 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
1020 allAppsIconSizes[INDEX_LANDSCAPE] = allAppsIconSizes[INDEX_DEFAULT];
1021 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1022 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1023 allAppsIconSizes[INDEX_DEFAULT]);
1024 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1025 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1026 allAppsIconSizes[INDEX_DEFAULT]);
1027
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001028 textSizes[INDEX_DEFAULT] =
1029 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1030 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001031 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001032 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001033 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001034 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001035 textSizes[INDEX_DEFAULT]);
1036 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001037 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001038 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001039
Thales Limabb7d3882021-12-22 12:56:29 +00001040 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1041 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1042 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1043 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1044 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1045 allAppsIconTextSizes[INDEX_DEFAULT]);
1046 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1047 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1048 allAppsIconTextSizes[INDEX_DEFAULT]);
1049
Thales Lima83bedbf2021-10-05 17:47:39 +01001050 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1051 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001052 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1053 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1054 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001055 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001056 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001057 horizontalMargin[INDEX_DEFAULT]);
1058 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001059 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001060 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001061
Thales Lima6e7f36c2022-01-04 12:14:11 +00001062 hotseatBorderSpaces[INDEX_DEFAULT] = a.getFloat(
1063 R.styleable.ProfileDisplayOption_hotseatBorderSpace, borderSpace);
Thales Limadd027342022-01-07 12:54:37 +00001064 hotseatBorderSpaces[INDEX_LANDSCAPE] = a.getFloat(
1065 R.styleable.ProfileDisplayOption_hotseatBorderSpaceLandscape,
1066 hotseatBorderSpaces[INDEX_DEFAULT]);
Thales Lima6e7f36c2022-01-04 12:14:11 +00001067 hotseatBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1068 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelLandscape,
1069 hotseatBorderSpaces[INDEX_DEFAULT]);
1070 hotseatBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1071 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelPortrait,
1072 hotseatBorderSpaces[INDEX_DEFAULT]);
1073
Thales Limab8c05952022-05-23 16:58:38 +01001074 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1075 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1076 ResourcesCompat.getFloat(context.getResources(),
1077 R.dimen.hotseat_bar_bottom_space_default));
1078 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1079 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1080 hotseatBarBottomSpace[INDEX_DEFAULT]);
1081 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1082 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1083 hotseatBarBottomSpace[INDEX_DEFAULT]);
1084 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1085 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1086 hotseatBarBottomSpace[INDEX_DEFAULT]);
1087
1088 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1089 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1090 ResourcesCompat.getFloat(context.getResources(),
1091 R.dimen.hotseat_qsb_space_default));
1092 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1093 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1094 hotseatQsbSpace[INDEX_DEFAULT]);
1095 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1096 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1097 hotseatQsbSpace[INDEX_DEFAULT]);
1098 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1099 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1100 hotseatQsbSpace[INDEX_DEFAULT]);
1101
Sunny Goyal415f1732018-11-29 10:33:47 -08001102 a.recycle();
1103 }
1104
1105 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001106 this(null);
1107 }
1108
1109 DisplayOption(GridOption grid) {
1110 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001111 minWidthDps = 0;
1112 minHeightDps = 0;
1113 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001114 for (int i = 0; i < COUNT_SIZES; i++) {
1115 iconSizes[i] = 0;
1116 textSizes[i] = 0;
1117 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001118 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001119 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001120 allAppsIconSizes[i] = 0;
1121 allAppsIconTextSizes[i] = 0;
1122 allAppsBorderSpaces[i] = new PointF();
Thales Lima12d0eff2022-03-25 17:06:11 +00001123 inlineQsb[i] = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001124 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001125 }
1126
1127 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001128 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001129 iconSizes[i] *= w;
1130 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001131 borderSpaces[i].x *= w;
1132 borderSpaces[i].y *= w;
1133 minCellSize[i].x *= w;
1134 minCellSize[i].y *= w;
1135 horizontalMargin[i] *= w;
Thales Lima6e7f36c2022-01-04 12:14:11 +00001136 hotseatBorderSpaces[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001137 hotseatBarBottomSpace[i] *= w;
1138 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001139 allAppsCellSize[i].x *= w;
1140 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001141 allAppsIconSizes[i] *= w;
1142 allAppsIconTextSizes[i] *= w;
1143 allAppsBorderSpaces[i].x *= w;
1144 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001145 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001146
1147 folderBorderSpace *= w;
1148
Sunny Goyal415f1732018-11-29 10:33:47 -08001149 return this;
1150 }
1151
1152 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001153 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001154 iconSizes[i] += p.iconSizes[i];
1155 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001156 borderSpaces[i].x += p.borderSpaces[i].x;
1157 borderSpaces[i].y += p.borderSpaces[i].y;
1158 minCellSize[i].x += p.minCellSize[i].x;
1159 minCellSize[i].y += p.minCellSize[i].y;
1160 horizontalMargin[i] += p.horizontalMargin[i];
Thales Lima6e7f36c2022-01-04 12:14:11 +00001161 hotseatBorderSpaces[i] += p.hotseatBorderSpaces[i];
Thales Limab8c05952022-05-23 16:58:38 +01001162 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1163 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001164 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1165 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001166 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1167 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1168 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1169 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Thales Lima12d0eff2022-03-25 17:06:11 +00001170 inlineQsb[i] |= p.inlineQsb[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001171 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001172
1173 folderBorderSpace += p.folderBorderSpace;
1174
Sunny Goyal415f1732018-11-29 10:33:47 -08001175 return this;
1176 }
1177 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001178}