blob: 9b017342f4d909f5e448b4bf90ec524ef3f378c9 [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 Goyal68031ca2021-08-02 12:23:44 -070051import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010052import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070053import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070054import com.android.launcher3.util.DisplayController;
55import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080056import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070057import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080058import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070059import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080060import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070061
Sunny Goyal819e1932016-07-07 16:43:58 -070062import org.xmlpull.v1.XmlPullParser;
63import org.xmlpull.v1.XmlPullParserException;
64
65import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000066import java.lang.annotation.Retention;
67import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070068import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070069import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080070import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070071import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070072
73public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070074
Hyunyoung Songc55a3502018-12-04 15:43:16 -080075 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070076 // We do not need any synchronization for this variable as its only written on UI thread.
77 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070078 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070079
Alex Chau1c883d82021-12-01 18:43:10 +000080 @Retention(RetentionPolicy.SOURCE)
81 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010082 public @interface DeviceType {}
83
Alex Chau1c883d82021-12-01 18:43:10 +000084 public static final int TYPE_PHONE = 0;
85 public static final int TYPE_MULTI_DISPLAY = 1;
86 public static final int TYPE_TABLET = 2;
87
Hyunyoung Songc55a3502018-12-04 15:43:16 -080088 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080089
Sunny Goyal53d7ee42015-05-22 12:25:45 -070090 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
91
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070092 // Constants that affects the interpolation curve between statically defined device profile
93 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080094 private static final float KNEARESTNEIGHBOR = 3;
95 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070096
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070097 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080098 private static final float WEIGHT_EFFICIENT = 100000f;
99
Thales Lima83bedbf2021-10-05 17:47:39 +0100100 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
101 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000102 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100103 static final int INDEX_DEFAULT = 0;
104 static final int INDEX_LANDSCAPE = 1;
105 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
106 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100107
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700108 /**
109 * Number of icons per row and column in the workspace.
110 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700111 public int numRows;
112 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000113 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700114
115 /**
116 * Number of icons per row and column in the folder.
117 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700118 public int numFolderRows;
119 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100120 public float[] iconSize;
121 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700122 public int iconBitmapSize;
123 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000124 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700125
Thales Lima83bedbf2021-10-05 17:47:39 +0100126 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100127
Thales Lima83bedbf2021-10-05 17:47:39 +0100128 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100129 public float folderBorderSpace;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000130 public float[] hotseatBorderSpaces;
Vinit Nayakc7293172022-07-18 16:41:50 -0700131 public int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100132
Thales Lima83bedbf2021-10-05 17:47:39 +0100133 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500134
Thales Limab7ef5692022-03-10 10:32:36 +0000135 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000136 public float[] allAppsIconSize;
137 public float[] allAppsIconTextSize;
138 public PointF[] allAppsBorderSpaces;
139
Sunny Goyal5bc18462019-01-07 15:13:39 -0800140 private SparseArray<TypedValue> mExtraAttrs;
141
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700142 /**
143 * Number of icons inside the hotseat area.
144 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800145 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700146
147 /**
Alex Chau6ed408f2022-03-04 12:58:05 +0000148 * Number of icons inside the hotseat area when using 3 buttons navigation.
149 */
150 public int numShrunkenHotseatIcons;
151
152 /**
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700153 * Number of icons inside the hotseat area that is stored in the database. This is greater than
154 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
155 * sizes that share the same DB.
156 */
157 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700158
Thales Lima425f6822022-05-10 13:44:58 -0300159 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100160 public float[] hotseatBarBottomSpace;
161 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300162
Jon Miranda6f7e9702019-09-16 14:44:14 -0700163 /**
164 * Number of columns in the all apps list.
165 */
166 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700167 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700168
Jon Mirandae126d722021-02-25 10:45:20 -0500169 /**
170 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
171 */
172 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400173 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500174
Tracy Zhou7df93d22020-01-27 13:44:06 -0800175 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800176 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700177 int demoModeLayoutId;
Thales Lima12d0eff2022-03-25 17:06:11 +0000178 boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700179
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000180 /**
181 * An immutable list of supported profiles.
182 */
183 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700184
sfufa@google.comde013292021-09-21 18:22:44 -0700185 @Nullable
186 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500187
Sunny Goyal6f866092016-03-17 17:04:15 -0700188 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700189 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700190
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700191 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700192
Sunny Goyalf633ef52018-03-13 09:57:05 -0700193 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800194 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700195
Sunny Goyalbbf01842015-10-08 07:41:15 -0700196 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700197 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700198 String gridName = getCurrentGridName(context);
199 String newGridName = initGrid(context, gridName);
200 if (!newGridName.equals(gridName)) {
201 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
202 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700203 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700204
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700205 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100206 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000207 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
208 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100209 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700210 }
211 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700212 }
213
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700214 /**
215 * This constructor should NOT have any monitors by design.
216 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800217 public InvariantDeviceProfile(Context context, String gridName) {
218 String newName = initGrid(context, gridName);
219 if (newName == null || !newName.equals(gridName)) {
220 throw new IllegalArgumentException("Unknown grid name");
221 }
222 }
223
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700224 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800225 * This constructor should NOT have any monitors by design.
226 */
227 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700228 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100229 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700230 String gridName = getCurrentGridName(context);
231
232 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000233 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
234 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000236 defaultInfo,
237 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
238 /*allowDisabledGrid=*/false),
239 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700240
Alex Chau661f02d2022-06-07 14:03:43 +0100241 Context displayContext = context.createDisplayContext(display);
242 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000243 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700244 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000245 myInfo,
246 getPredefinedDeviceProfiles(context, gridName, deviceType,
247 /*allowDisabledGrid=*/false),
248 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700249
250 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
251 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100252 result.iconSizes[INDEX_DEFAULT] =
253 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
254 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700255 result.iconSizes[i] = Math.min(
256 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000257 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700258
Thales Lima83bedbf2021-10-05 17:47:39 +0100259 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
260 COUNT_SIZES);
261 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
262 COUNT_SIZES);
Thales Lima12d0eff2022-03-25 17:06:11 +0000263 System.arraycopy(defaultDisplayOption.inlineQsb, 0, result.inlineQsb, 0,
264 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500265
Alex Chau1c883d82021-12-01 18:43:10 +0000266 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800267 }
268
Alex Chau238aaee2021-10-06 16:15:24 +0100269 /**
270 * Reinitialize the current grid after a restore, where some grids might now be disabled.
271 */
272 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000273 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100274 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000275 String newGridName = initGrid(context, currentGridName);
276 String newDbFile = dbFile;
277 if (!newDbFile.equals(currentDbFile)) {
278 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100279 + ", migrating to: " + newGridName
280 + ", removing all other grid db files");
281 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
282 if (gridDbFile.equals(currentDbFile)) {
283 continue;
284 }
285 if (context.getDatabasePath(gridDbFile).delete()) {
286 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
287 }
288 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000289 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100290 }
291 }
292
Alex Chau1c883d82021-12-01 18:43:10 +0000293 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800294 int flagPhone = 1 << 0;
295 int flagTablet = 1 << 1;
296
297 int type = displayInfo.supportedBounds.stream()
298 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
299 .reduce(0, (a, b) -> a | b);
300 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
301 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000302 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800303 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000304 return TYPE_TABLET;
305 } else {
306 return TYPE_PHONE;
307 }
308 }
309
Tracy Zhou42255d22020-03-13 00:38:11 -0700310 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700311 return Utilities.isGridOptionsEnabled(context)
312 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700313 }
314
Sunny Goyaleff44f32019-01-09 17:29:49 -0800315 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700316 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000317 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700318
319 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000320 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100321 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700322 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000323 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
324 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700325 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800326 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700327
Alex Chau1c883d82021-12-01 18:43:10 +0000328 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
329 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700330 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700331 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000332 numRows = closestProfile.numRows;
333 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000334 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000335 dbFile = closestProfile.dbFile;
336 defaultLayoutId = closestProfile.defaultLayoutId;
337 demoModeLayoutId = closestProfile.demoModeLayoutId;
338 numFolderRows = closestProfile.numFolderRows;
339 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500340 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400341 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000342 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000343
Vinit Nayakc7293172022-07-18 16:41:50 -0700344 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
345
Sunny Goyalae190ff2020-04-14 00:19:01 +0000346 mExtraAttrs = closestProfile.extraAttrs;
347
Thales Lima83bedbf2021-10-05 17:47:39 +0100348 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100349 float maxIconSize = iconSize[0];
350 for (int i = 1; i < iconSize.length; i++) {
351 maxIconSize = Math.max(maxIconSize, iconSize[i]);
352 }
353 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000354 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
355
Thales Lima83bedbf2021-10-05 17:47:39 +0100356 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700357
Thales Lima83bedbf2021-10-05 17:47:39 +0100358 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100359
Thales Lima83bedbf2021-10-05 17:47:39 +0100360 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100361 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700362
Thales Limad90faab2021-09-21 17:18:47 +0100363 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100364
Sunny Goyal19ff7282021-04-22 10:12:54 -0700365 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000366 numShrunkenHotseatIcons = closestProfile.numShrunkenHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000367 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700368 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300369 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000370 hotseatBorderSpaces = displayOption.hotseatBorderSpaces;
Thales Limab8c05952022-05-23 16:58:38 +0100371 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
372 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700373
374 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000375 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700376 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500377
Thales Limab7ef5692022-03-10 10:32:36 +0000378 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000379 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
380 allAppsIconSize = displayOption.allAppsIconSizes;
381 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100382 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000383 allAppsIconSize = iconSize;
384 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000385 }
386
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400387 if (devicePaddingId != 0) {
388 devicePaddings = new DevicePaddings(context, devicePaddingId);
389 }
390
Thales Lima12d0eff2022-03-25 17:06:11 +0000391 inlineQsb = displayOption.inlineQsb;
392
Sunny Goyalae190ff2020-04-14 00:19:01 +0000393 // If the partner customization apk contains any grid overrides, apply them
394 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700395 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700396
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000397 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700398 defaultWallpaperSize = new Point(displayInfo.currentSize);
399 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000400 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000401 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000402 .setWindowBounds(bounds)
403 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700404
Sunny Goyal19ff7282021-04-22 10:12:54 -0700405 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
406 int displayWidth = bounds.bounds.width();
407 int displayHeight = bounds.bounds.height();
408 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700409
Sunny Goyal19ff7282021-04-22 10:12:54 -0700410 // We need to ensure that there is enough extra space in the wallpaper
411 // for the intended parallax effects
412 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300413 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
414 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700415 ? 2
416 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
417 defaultWallpaperSize.x =
418 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700419 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000420 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700421
422 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
423 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700424 }
425
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700426 public void addOnChangeListener(OnIDPChangeListener listener) {
427 mChangeListeners.add(listener);
428 }
429
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800430 public void removeOnChangeListener(OnIDPChangeListener listener) {
431 mChangeListeners.remove(listener);
432 }
433
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800434
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800435 public void setCurrentGrid(Context context, String gridName) {
436 Context appContext = context.getApplicationContext();
437 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700438 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800439 }
440
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700441 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700442 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000443 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
444 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700445 }
446
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700447 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700448 Object[] oldState = toModelState();
449
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700450 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700451 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700452 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700453
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700454 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700455 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700456 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700457 }
458 }
459
Alex Chau1c883d82021-12-01 18:43:10 +0000460 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
461 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800462 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100463
Alex Chau1c883d82021-12-01 18:43:10 +0000464 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700465 final int depth = parser.getDepth();
466 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700467 while (((type = parser.next()) != XmlPullParser.END_TAG ||
468 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800469 if ((type == XmlPullParser.START_TAG)
470 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800471
Alex Chau1c883d82021-12-01 18:43:10 +0000472 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
473 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100474 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100475 final int displayDepth = parser.getDepth();
476 while (((type = parser.next()) != XmlPullParser.END_TAG
477 || parser.getDepth() > displayDepth)
478 && type != XmlPullParser.END_DOCUMENT) {
479 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
480 parser.getName())) {
481 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100482 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100483 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800484 }
485 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700486 }
487 }
sfufa@google.comde013292021-09-21 18:22:44 -0700488 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700489 throw new RuntimeException(e);
490 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800491
Sunny Goyalae190ff2020-04-14 00:19:01 +0000492 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800493 if (!TextUtils.isEmpty(gridName)) {
494 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100495 if (gridName.equals(option.grid.name)
496 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000497 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800498 }
499 }
500 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000501 if (filteredProfiles.isEmpty()) {
502 // No grid found, use the default options
503 for (DisplayOption option : profiles) {
504 if (option.canBeDefault) {
505 filteredProfiles.add(option);
506 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800507 }
508 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000509 if (filteredProfiles.isEmpty()) {
510 throw new RuntimeException("No display option with canBeDefault=true");
511 }
512 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700513 }
514
Thales Lima7ec83822021-08-05 13:32:10 +0100515 /**
516 * @return all the grid options that can be shown on the device
517 */
518 public List<GridOption> parseAllGridOptions(Context context) {
519 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100520
Alex Chau1c883d82021-12-01 18:43:10 +0000521 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100522 final int depth = parser.getDepth();
523 int type;
524 while (((type = parser.next()) != XmlPullParser.END_TAG
525 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
526 if ((type == XmlPullParser.START_TAG)
527 && GridOption.TAG_NAME.equals(parser.getName())) {
528 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000529 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100530 if (option.isEnabled) {
531 result.add(option);
532 }
533 }
534 }
535 } catch (IOException | XmlPullParserException e) {
536 Log.e(TAG, "Error parsing device profile", e);
537 return Collections.emptyList();
538 }
539 return result;
540 }
541
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700542 private int getLauncherIconDensity(int requiredSize) {
543 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700544 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700545 DisplayMetrics.DENSITY_LOW,
546 DisplayMetrics.DENSITY_MEDIUM,
547 DisplayMetrics.DENSITY_TV,
548 DisplayMetrics.DENSITY_HIGH,
549 DisplayMetrics.DENSITY_XHIGH,
550 DisplayMetrics.DENSITY_XXHIGH,
551 DisplayMetrics.DENSITY_XXXHIGH
552 };
553
554 int density = DisplayMetrics.DENSITY_XXXHIGH;
555 for (int i = densityBuckets.length - 1; i >= 0; i--) {
556 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
557 / DisplayMetrics.DENSITY_DEFAULT;
558 if (expectedSize >= requiredSize) {
559 density = densityBuckets[i];
560 }
561 }
562
563 return density;
564 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700565
Adam Cohen2e6da152015-05-06 11:42:25 -0700566 /**
567 * Apply any Partner customization grid overrides.
568 *
569 * Currently we support: all apps row / column count.
570 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700571 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
572 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700573 if (p != null) {
574 p.applyInvariantDeviceProfileOverrides(this, dm);
575 }
576 }
577
Sunny Goyal415f1732018-11-29 10:33:47 -0800578 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700579 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700580 }
581
Sunny Goyal19ff7282021-04-22 10:12:54 -0700582 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000583 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700584 int minWidthPx = Integer.MAX_VALUE;
585 int minHeightPx = Integer.MAX_VALUE;
586 for (WindowBounds bounds : displayInfo.supportedBounds) {
587 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000588 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700589 // For split displays, take half width per page
590 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
591 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700592
Sunny Goyal19ff7282021-04-22 10:12:54 -0700593 } else if (!isTablet && bounds.isLandscape()) {
594 // We will use transposed layout in this case
595 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
596 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
597 } else {
598 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
599 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
600 }
601 }
602
Thales Lima425f6822022-05-10 13:44:58 -0300603 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
604 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700605
606 // Sort the profiles based on the closeness to the device size
607 Collections.sort(points, (a, b) ->
608 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
609 dist(width, height, b.minWidthDps, b.minHeightDps)));
610
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700611 DisplayOption closestPoint = points.get(0);
612 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700613 float weights = 0;
614
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700615 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
616 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700617 }
618
619 DisplayOption out = new DisplayOption(closestOption);
620 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700621 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700622 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
623 weights += w;
624 out.add(new DisplayOption().add(p).multiply(w));
625 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700626 out.multiply(1.0f / weights);
627
Thales Lima6e0005a2021-10-27 15:53:41 +0100628 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700629 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100630 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
631 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700632 }
633
634 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700635 }
636
Sunny Goyal27835952017-01-13 12:15:53 -0800637 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700638 Resources res = context.getResources();
639 Configuration config = context.getResources().getConfiguration();
640
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400641 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
642 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100643 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
644
Alex Chaud3e8cc42022-05-03 17:45:34 +0100645 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400646 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700647
Sunny Goyal187b16c2022-03-01 16:53:23 -0800648 /**
649 * Returns the device profile matching the provided screen configuration
650 */
651 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700652 DeviceProfile bestMatch = supportedProfiles.get(0);
653 float minDiff = Float.MAX_VALUE;
654
655 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400656 float diff = Math.abs(profile.widthPx - screenWidth)
657 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700658 if (diff < minDiff) {
659 minDiff = diff;
660 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800661 } else if (diff == minDiff && profile.rotationHint == rotation) {
662 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700663 }
664 }
665 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800666 }
667
Sunny Goyal415f1732018-11-29 10:33:47 -0800668 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700669 float d = dist(x0, y0, x1, y1);
670 if (Float.compare(d, 0f) == 0) {
671 return Float.POSITIVE_INFINITY;
672 }
673 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
674 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700675
676 /**
677 * As a ratio of screen height, the total distance we want the parallax effect to span
678 * horizontally
679 */
680 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
681 float aspectRatio = width / (float) height;
682
683 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
684 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
685 // We will use these two data points to extrapolate how much the wallpaper parallax effect
686 // to span (ie travel) at any aspect ratio:
687
sfufa@google.comde013292021-09-21 18:22:44 -0700688 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
689 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700690 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
691 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
692
693 // To find out the desired width at different aspect ratios, we use the following two
694 // formulas, where the coefficient on x is the aspect ratio (width/height):
695 // (16/10)x + y = 1.5
696 // (10/16)x + y = 1.2
697 // We solve for x and y and end up with a final formula:
698 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700699 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
700 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700701 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
702 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
703 return x * aspectRatio + y;
704 }
705
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700706 public interface OnIDPChangeListener {
707
Sunny Goyalb47172b2021-05-03 19:59:51 -0700708 /**
709 * Called when the device provide changes
710 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700711 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700712 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800713
714
Sunny Goyaleff44f32019-01-09 17:29:49 -0800715 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800716
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800717 public static final String TAG_NAME = "grid-option";
718
Alex Chau1c883d82021-12-01 18:43:10 +0000719 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
720 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
721 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
722 private static final int DEVICE_CATEGORY_ALL =
723 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
724
Sunny Goyaleff44f32019-01-09 17:29:49 -0800725 public final String name;
726 public final int numRows;
727 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000728 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100729 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800730
731 private final int numFolderRows;
732 private final int numFolderColumns;
733
Sunny Goyal19ff7282021-04-22 10:12:54 -0700734 private final int numAllAppsColumns;
735 private final int numDatabaseAllAppsColumns;
736 private final int numHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000737 private final int numShrunkenHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700738 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100739
Thales Lima425f6822022-05-10 13:44:58 -0300740 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800741
Vinit Nayakc7293172022-07-18 16:41:50 -0700742 private int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800743 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000744
Sunny Goyal415f1732018-11-29 10:33:47 -0800745 private final int defaultLayoutId;
746 private final int demoModeLayoutId;
747
Jon Mirandae126d722021-02-25 10:45:20 -0500748 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400749 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500750
Sunny Goyal5bc18462019-01-07 15:13:39 -0800751 private final SparseArray<TypedValue> extraAttrs;
752
Alex Chau1c883d82021-12-01 18:43:10 +0000753 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800754 TypedArray a = context.obtainStyledAttributes(
755 attrs, R.styleable.GridDisplayOption);
756 name = a.getString(R.styleable.GridDisplayOption_name);
757 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
758 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000759 numSearchContainerColumns = a.getInt(
760 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800761
Tracy Zhou7df93d22020-01-27 13:44:06 -0800762 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000763 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100764 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
765 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
766 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800767 demoModeLayoutId = a.getResourceId(
768 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700769
770 numAllAppsColumns = a.getInt(
771 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
772 numDatabaseAllAppsColumns = a.getInt(
773 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
774
775 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800776 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Alex Chau6ed408f2022-03-04 12:58:05 +0000777 numShrunkenHotseatIcons = a.getInt(
778 R.styleable.GridDisplayOption_numShrunkenHotseatIcons, numHotseatIcons / 2);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700779 numDatabaseHotseatIcons = a.getInt(
780 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100781
Thales Lima425f6822022-05-10 13:44:58 -0300782 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
783 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
784 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
785 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
786 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
787 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
788 numColumns);
789 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
790 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
791 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700792
Vinit Nayakc7293172022-07-18 16:41:50 -0700793 inlineNavButtonsEndSpacing =
794 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
795 R.dimen.taskbar_button_margin_default);
Sunny Goyal415f1732018-11-29 10:33:47 -0800796 numFolderRows = a.getInt(
797 R.styleable.GridDisplayOption_numFolderRows, numRows);
798 numFolderColumns = a.getInt(
799 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700800
Jon Mirandae126d722021-02-25 10:45:20 -0500801 isScalable = a.getBoolean(
802 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400803 devicePaddingId = a.getResourceId(
804 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500805
Alex Chau1c883d82021-12-01 18:43:10 +0000806 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
807 DEVICE_CATEGORY_ALL);
808 isEnabled = (deviceType == TYPE_PHONE
809 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
810 || (deviceType == TYPE_TABLET
811 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
812 || (deviceType == TYPE_MULTI_DISPLAY
813 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
814 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100815
Sunny Goyal415f1732018-11-29 10:33:47 -0800816 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800817 extraAttrs = Themes.createValueMap(context, attrs,
818 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800819 }
820 }
821
Sunny Goyal19ff7282021-04-22 10:12:54 -0700822 @VisibleForTesting
823 static final class DisplayOption {
Thales Lima12d0eff2022-03-25 17:06:11 +0000824 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
825 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
826 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
827 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
828 private static final int DONT_INLINE_QSB = 0;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700829
830 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800831
Sunny Goyal415f1732018-11-29 10:33:47 -0800832 private final float minWidthDps;
833 private final float minHeightDps;
834 private final boolean canBeDefault;
Thales Lima12d0eff2022-03-25 17:06:11 +0000835 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800836
Thales Lima83bedbf2021-10-05 17:47:39 +0100837 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100838
839 private float folderBorderSpace;
840 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100841 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Lima425f6822022-05-10 13:44:58 -0300842 //TODO(http://b/228998082) remove this when 3 button spaces are fixed
Thales Lima6e7f36c2022-01-04 12:14:11 +0000843 private final float[] hotseatBorderSpaces = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100844 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
845 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100846
Thales Lima78d00ad2021-09-30 11:29:06 +0100847 private final float[] iconSizes = new float[COUNT_SIZES];
848 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800849
Thales Limab7ef5692022-03-10 10:32:36 +0000850 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000851 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
852 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
853 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
854
Thales Lima1de4d552021-10-13 16:13:25 +0100855 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800856 this.grid = grid;
857
Thales Lima1de4d552021-10-13 16:13:25 +0100858 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800859
Sunny Goyal415f1732018-11-29 10:33:47 -0800860 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
861 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700862
Thales Lima1de4d552021-10-13 16:13:25 +0100863 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800864
Thales Lima12d0eff2022-03-25 17:06:11 +0000865 int inlineForRotation = a.getInt(R.styleable.ProfileDisplayOption_inlineQsb,
866 DONT_INLINE_QSB);
867 inlineQsb[INDEX_DEFAULT] =
868 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
869 inlineQsb[INDEX_LANDSCAPE] =
870 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
871 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
872 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
873 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
874 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
875 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
876 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
877
Thales Lima83bedbf2021-10-05 17:47:39 +0100878 float x;
879 float y;
880
Thales Lima85c942f2021-12-31 13:04:20 +0000881 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
882 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100883 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000884
885 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
886 minCellSize[INDEX_DEFAULT].x);
887 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
888 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100889 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100890
Thales Lima85c942f2021-12-31 13:04:20 +0000891 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100892 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000893 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100894 minCellSize[INDEX_DEFAULT].y);
895 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
896
Thales Lima85c942f2021-12-31 13:04:20 +0000897 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100898 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000899 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100900 minCellSize[INDEX_DEFAULT].y);
901 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100902
Thales Lima85c942f2021-12-31 13:04:20 +0000903 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000904 float borderSpaceLandscape = a.getFloat(
905 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000906 float borderSpaceTwoPanelPortrait = a.getFloat(
907 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
908 float borderSpaceTwoPanelLandscape = a.getFloat(
909 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100910
Thales Lima85c942f2021-12-31 13:04:20 +0000911 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
912 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100913 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000914
915 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
916 borderSpaceLandscape);
917 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
918 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100919 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
920
921 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000922 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
923 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100924 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000925 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
926 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100927 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
928
929 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000930 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
931 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100932 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000933 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
934 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100935 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
936
Thales Limabb7d3882021-12-22 12:56:29 +0000937 folderBorderSpace = borderSpace;
938
Thales Limab7ef5692022-03-10 10:32:36 +0000939 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
940 minCellSize[INDEX_DEFAULT].x);
941 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
942 minCellSize[INDEX_DEFAULT].y);
943 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
944
945 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
946 allAppsCellSize[INDEX_DEFAULT].x);
947 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
948 allAppsCellSize[INDEX_DEFAULT].y);
949 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
950
951 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
952 allAppsCellSize[INDEX_DEFAULT].x);
953 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
954 allAppsCellSize[INDEX_DEFAULT].y);
955 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
956
957 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
958 allAppsCellSize[INDEX_DEFAULT].x);
959 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
960 allAppsCellSize[INDEX_DEFAULT].y);
961 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
962
Thales Lima080d8902022-03-28 15:30:29 +0100963 float allAppsBorderSpace = a.getFloat(
964 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
965 float allAppsBorderSpaceLandscape = a.getFloat(
966 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
967 allAppsBorderSpace);
968 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000969 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100970 allAppsBorderSpace);
971 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000972 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100973 allAppsBorderSpace);
974
975 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
976 allAppsBorderSpace);
977 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
978 allAppsBorderSpace);
979 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
980
981 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
982 allAppsBorderSpaceLandscape);
983 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
984 allAppsBorderSpaceLandscape);
985 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
986
987 x = a.getFloat(
988 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
989 allAppsBorderSpaceTwoPanelPortrait);
990 y = a.getFloat(
991 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
992 allAppsBorderSpaceTwoPanelPortrait);
993 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
994
995 x = a.getFloat(
996 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
997 allAppsBorderSpaceTwoPanelLandscape);
998 y = a.getFloat(
999 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1000 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001001 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001002
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001003 iconSizes[INDEX_DEFAULT] =
1004 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1005 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001006 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001007 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001008 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001009 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001010 iconSizes[INDEX_DEFAULT]);
1011 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001012 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001013 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001014
Thales Limabb7d3882021-12-22 12:56:29 +00001015 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1016 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
1017 allAppsIconSizes[INDEX_LANDSCAPE] = allAppsIconSizes[INDEX_DEFAULT];
1018 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1019 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1020 allAppsIconSizes[INDEX_DEFAULT]);
1021 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1022 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1023 allAppsIconSizes[INDEX_DEFAULT]);
1024
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001025 textSizes[INDEX_DEFAULT] =
1026 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1027 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001028 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001029 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001030 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001031 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001032 textSizes[INDEX_DEFAULT]);
1033 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001034 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001035 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001036
Thales Limabb7d3882021-12-22 12:56:29 +00001037 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1038 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1039 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1040 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1041 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1042 allAppsIconTextSizes[INDEX_DEFAULT]);
1043 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1044 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1045 allAppsIconTextSizes[INDEX_DEFAULT]);
1046
Thales Lima83bedbf2021-10-05 17:47:39 +01001047 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1048 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001049 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1050 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1051 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001052 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001053 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001054 horizontalMargin[INDEX_DEFAULT]);
1055 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001056 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001057 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001058
Thales Lima6e7f36c2022-01-04 12:14:11 +00001059 hotseatBorderSpaces[INDEX_DEFAULT] = a.getFloat(
1060 R.styleable.ProfileDisplayOption_hotseatBorderSpace, borderSpace);
Thales Limadd027342022-01-07 12:54:37 +00001061 hotseatBorderSpaces[INDEX_LANDSCAPE] = a.getFloat(
1062 R.styleable.ProfileDisplayOption_hotseatBorderSpaceLandscape,
1063 hotseatBorderSpaces[INDEX_DEFAULT]);
Thales Lima6e7f36c2022-01-04 12:14:11 +00001064 hotseatBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1065 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelLandscape,
1066 hotseatBorderSpaces[INDEX_DEFAULT]);
1067 hotseatBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1068 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelPortrait,
1069 hotseatBorderSpaces[INDEX_DEFAULT]);
1070
Thales Limab8c05952022-05-23 16:58:38 +01001071 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1072 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1073 ResourcesCompat.getFloat(context.getResources(),
1074 R.dimen.hotseat_bar_bottom_space_default));
1075 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1076 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1077 hotseatBarBottomSpace[INDEX_DEFAULT]);
1078 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1079 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1080 hotseatBarBottomSpace[INDEX_DEFAULT]);
1081 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1082 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1083 hotseatBarBottomSpace[INDEX_DEFAULT]);
1084
1085 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1086 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1087 ResourcesCompat.getFloat(context.getResources(),
1088 R.dimen.hotseat_qsb_space_default));
1089 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1090 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1091 hotseatQsbSpace[INDEX_DEFAULT]);
1092 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1093 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1094 hotseatQsbSpace[INDEX_DEFAULT]);
1095 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1096 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1097 hotseatQsbSpace[INDEX_DEFAULT]);
1098
Sunny Goyal415f1732018-11-29 10:33:47 -08001099 a.recycle();
1100 }
1101
1102 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001103 this(null);
1104 }
1105
1106 DisplayOption(GridOption grid) {
1107 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001108 minWidthDps = 0;
1109 minHeightDps = 0;
1110 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001111 for (int i = 0; i < COUNT_SIZES; i++) {
1112 iconSizes[i] = 0;
1113 textSizes[i] = 0;
1114 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001115 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001116 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001117 allAppsIconSizes[i] = 0;
1118 allAppsIconTextSizes[i] = 0;
1119 allAppsBorderSpaces[i] = new PointF();
Thales Lima12d0eff2022-03-25 17:06:11 +00001120 inlineQsb[i] = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001121 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001122 }
1123
1124 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001125 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001126 iconSizes[i] *= w;
1127 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001128 borderSpaces[i].x *= w;
1129 borderSpaces[i].y *= w;
1130 minCellSize[i].x *= w;
1131 minCellSize[i].y *= w;
1132 horizontalMargin[i] *= w;
Thales Lima6e7f36c2022-01-04 12:14:11 +00001133 hotseatBorderSpaces[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001134 hotseatBarBottomSpace[i] *= w;
1135 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001136 allAppsCellSize[i].x *= w;
1137 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001138 allAppsIconSizes[i] *= w;
1139 allAppsIconTextSizes[i] *= w;
1140 allAppsBorderSpaces[i].x *= w;
1141 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001142 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001143
1144 folderBorderSpace *= w;
1145
Sunny Goyal415f1732018-11-29 10:33:47 -08001146 return this;
1147 }
1148
1149 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001150 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001151 iconSizes[i] += p.iconSizes[i];
1152 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001153 borderSpaces[i].x += p.borderSpaces[i].x;
1154 borderSpaces[i].y += p.borderSpaces[i].y;
1155 minCellSize[i].x += p.minCellSize[i].x;
1156 minCellSize[i].y += p.minCellSize[i].y;
1157 horizontalMargin[i] += p.horizontalMargin[i];
Thales Lima6e7f36c2022-01-04 12:14:11 +00001158 hotseatBorderSpaces[i] += p.hotseatBorderSpaces[i];
Thales Limab8c05952022-05-23 16:58:38 +01001159 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1160 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001161 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1162 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001163 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1164 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1165 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1166 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Thales Lima12d0eff2022-03-25 17:06:11 +00001167 inlineQsb[i] |= p.inlineQsb[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001168 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001169
1170 folderBorderSpace += p.folderBorderSpace;
1171
Sunny Goyal415f1732018-11-29 10:33:47 -08001172 return this;
1173 }
1174 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001175}