blob: 73ca4a34c4f43f6ab3f9ea5f8e752eddb24475c2 [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;
49
Sunny Goyal68031ca2021-08-02 12:23:44 -070050import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010051import com.android.launcher3.provider.RestoreDbTask;
Sunny Goyalfd58da62020-08-11 12:06:49 -070052import com.android.launcher3.util.DisplayController;
53import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080054import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070055import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080056import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070057import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080058import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070059
Sunny Goyal819e1932016-07-07 16:43:58 -070060import org.xmlpull.v1.XmlPullParser;
61import org.xmlpull.v1.XmlPullParserException;
62
63import java.io.IOException;
Alex Chaud3e8cc42022-05-03 17:45:34 +010064import java.io.PrintWriter;
65import java.io.StringWriter;
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})
82 public @interface DeviceType{}
83 public static final int TYPE_PHONE = 0;
84 public static final int TYPE_MULTI_DISPLAY = 1;
85 public static final int TYPE_TABLET = 2;
86
Hyunyoung Songc55a3502018-12-04 15:43:16 -080087 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080088
Sunny Goyal53d7ee42015-05-22 12:25:45 -070089 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
90
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070091 // Constants that affects the interpolation curve between statically defined device profile
92 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080093 private static final float KNEARESTNEIGHBOR = 3;
94 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070095
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070096 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080097 private static final float WEIGHT_EFFICIENT = 100000f;
98
Thales Lima83bedbf2021-10-05 17:47:39 +010099 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
100 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000101 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100102 static final int INDEX_DEFAULT = 0;
103 static final int INDEX_LANDSCAPE = 1;
104 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
105 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100106
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700107 /**
108 * Number of icons per row and column in the workspace.
109 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700110 public int numRows;
111 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000112 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700113
114 /**
115 * Number of icons per row and column in the folder.
116 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700117 public int numFolderRows;
118 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100119 public float[] iconSize;
120 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700121 public int iconBitmapSize;
122 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000123 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700124
Thales Lima83bedbf2021-10-05 17:47:39 +0100125 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100126
Thales Lima83bedbf2021-10-05 17:47:39 +0100127 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100128 public float folderBorderSpace;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000129 public float[] hotseatBorderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100130
Thales Lima83bedbf2021-10-05 17:47:39 +0100131 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500132
Thales Limab7ef5692022-03-10 10:32:36 +0000133 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000134 public float[] allAppsIconSize;
135 public float[] allAppsIconTextSize;
136 public PointF[] allAppsBorderSpaces;
137
Sunny Goyal5bc18462019-01-07 15:13:39 -0800138 private SparseArray<TypedValue> mExtraAttrs;
139
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700140 /**
141 * Number of icons inside the hotseat area.
142 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800143 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700144
145 /**
Alex Chau6ed408f2022-03-04 12:58:05 +0000146 * Number of icons inside the hotseat area when using 3 buttons navigation.
147 */
148 public int numShrunkenHotseatIcons;
149
150 /**
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700151 * Number of icons inside the hotseat area that is stored in the database. This is greater than
152 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
153 * sizes that share the same DB.
154 */
155 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700156
Thales Lima425f6822022-05-10 13:44:58 -0300157 public int[] hotseatColumnSpan;
158
Jon Miranda6f7e9702019-09-16 14:44:14 -0700159 /**
160 * Number of columns in the all apps list.
161 */
162 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700163 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700164
Jon Mirandae126d722021-02-25 10:45:20 -0500165 /**
166 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
167 */
168 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400169 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500170
Tracy Zhou7df93d22020-01-27 13:44:06 -0800171 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800172 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700173 int demoModeLayoutId;
Thales Lima12d0eff2022-03-25 17:06:11 +0000174 boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700175
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000176 /**
177 * An immutable list of supported profiles.
178 */
179 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700180
sfufa@google.comde013292021-09-21 18:22:44 -0700181 @Nullable
182 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500183
Sunny Goyal6f866092016-03-17 17:04:15 -0700184 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700185 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700186
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700187 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700188
Sunny Goyalf633ef52018-03-13 09:57:05 -0700189 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800190 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700191
Sunny Goyalbbf01842015-10-08 07:41:15 -0700192 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700193 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700194 String gridName = getCurrentGridName(context);
195 String newGridName = initGrid(context, gridName);
196 if (!newGridName.equals(gridName)) {
197 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
198 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700199 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700200
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700201 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100202 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000203 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
204 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100205 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700206 }
207 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700208 }
209
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700210 /**
211 * This constructor should NOT have any monitors by design.
212 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800213 public InvariantDeviceProfile(Context context, String gridName) {
214 String newName = initGrid(context, gridName);
215 if (newName == null || !newName.equals(gridName)) {
216 throw new IllegalArgumentException("Unknown grid name");
217 }
218 }
219
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700220 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800221 * This constructor should NOT have any monitors by design.
222 */
223 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700224 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100225 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700226 String gridName = getCurrentGridName(context);
227
228 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000229 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
230 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700231 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000232 defaultInfo,
233 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
234 /*allowDisabledGrid=*/false),
235 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700236
Alex Chau661f02d2022-06-07 14:03:43 +0100237 Context displayContext = context.createDisplayContext(display);
238 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000239 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700240 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000241 myInfo,
242 getPredefinedDeviceProfiles(context, gridName, deviceType,
243 /*allowDisabledGrid=*/false),
244 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700245
246 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
247 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100248 result.iconSizes[INDEX_DEFAULT] =
249 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
250 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700251 result.iconSizes[i] = Math.min(
252 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000253 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700254
Thales Lima83bedbf2021-10-05 17:47:39 +0100255 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
256 COUNT_SIZES);
257 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
258 COUNT_SIZES);
Thales Lima12d0eff2022-03-25 17:06:11 +0000259 System.arraycopy(defaultDisplayOption.inlineQsb, 0, result.inlineQsb, 0,
260 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500261
Alex Chau1c883d82021-12-01 18:43:10 +0000262 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800263 }
264
Alex Chau238aaee2021-10-06 16:15:24 +0100265 /**
266 * Reinitialize the current grid after a restore, where some grids might now be disabled.
267 */
268 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000269 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100270 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000271 String newGridName = initGrid(context, currentGridName);
272 String newDbFile = dbFile;
273 if (!newDbFile.equals(currentDbFile)) {
274 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100275 + ", migrating to: " + newGridName
276 + ", removing all other grid db files");
277 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
278 if (gridDbFile.equals(currentDbFile)) {
279 continue;
280 }
281 if (context.getDatabasePath(gridDbFile).delete()) {
282 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
283 }
284 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000285 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100286 }
287 }
288
Alex Chau1c883d82021-12-01 18:43:10 +0000289 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800290 int flagPhone = 1 << 0;
291 int flagTablet = 1 << 1;
292
293 int type = displayInfo.supportedBounds.stream()
294 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
295 .reduce(0, (a, b) -> a | b);
296 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
297 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000298 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800299 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000300 return TYPE_TABLET;
301 } else {
302 return TYPE_PHONE;
303 }
304 }
305
Tracy Zhou42255d22020-03-13 00:38:11 -0700306 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700307 return Utilities.isGridOptionsEnabled(context)
308 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700309 }
310
Sunny Goyaleff44f32019-01-09 17:29:49 -0800311 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700312 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000313 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700314
315 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000316 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100317 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700318 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000319 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
320 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700321 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800322 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700323
Alex Chau1c883d82021-12-01 18:43:10 +0000324 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
325 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700326 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700327 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000328 numRows = closestProfile.numRows;
329 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000330 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000331 dbFile = closestProfile.dbFile;
332 defaultLayoutId = closestProfile.defaultLayoutId;
333 demoModeLayoutId = closestProfile.demoModeLayoutId;
334 numFolderRows = closestProfile.numFolderRows;
335 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500336 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400337 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000338 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000339
340 mExtraAttrs = closestProfile.extraAttrs;
341
Thales Lima83bedbf2021-10-05 17:47:39 +0100342 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100343 float maxIconSize = iconSize[0];
344 for (int i = 1; i < iconSize.length; i++) {
345 maxIconSize = Math.max(maxIconSize, iconSize[i]);
346 }
347 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000348 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
349
Thales Lima83bedbf2021-10-05 17:47:39 +0100350 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700351
Thales Lima83bedbf2021-10-05 17:47:39 +0100352 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100353
Thales Lima83bedbf2021-10-05 17:47:39 +0100354 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100355 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700356
Thales Limad90faab2021-09-21 17:18:47 +0100357 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100358
Sunny Goyal19ff7282021-04-22 10:12:54 -0700359 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000360 numShrunkenHotseatIcons = closestProfile.numShrunkenHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000361 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700362 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300363 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000364 hotseatBorderSpaces = displayOption.hotseatBorderSpaces;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700365
366 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000367 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700368 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500369
Thales Limab7ef5692022-03-10 10:32:36 +0000370 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000371 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
372 allAppsIconSize = displayOption.allAppsIconSizes;
373 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100374 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000375 allAppsIconSize = iconSize;
376 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000377 }
378
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400379 if (devicePaddingId != 0) {
380 devicePaddings = new DevicePaddings(context, devicePaddingId);
381 }
382
Thales Lima12d0eff2022-03-25 17:06:11 +0000383 inlineQsb = displayOption.inlineQsb;
384
Sunny Goyalae190ff2020-04-14 00:19:01 +0000385 // If the partner customization apk contains any grid overrides, apply them
386 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700387 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700388
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000389 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700390 defaultWallpaperSize = new Point(displayInfo.currentSize);
391 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000392 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000393 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000394 .setWindowBounds(bounds)
395 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700396
Sunny Goyal19ff7282021-04-22 10:12:54 -0700397 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
398 int displayWidth = bounds.bounds.width();
399 int displayHeight = bounds.bounds.height();
400 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700401
Sunny Goyal19ff7282021-04-22 10:12:54 -0700402 // We need to ensure that there is enough extra space in the wallpaper
403 // for the intended parallax effects
404 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300405 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
406 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700407 ? 2
408 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
409 defaultWallpaperSize.x =
410 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700411 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000412 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700413
414 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
415 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700416 }
417
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700418 public void addOnChangeListener(OnIDPChangeListener listener) {
419 mChangeListeners.add(listener);
420 }
421
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800422 public void removeOnChangeListener(OnIDPChangeListener listener) {
423 mChangeListeners.remove(listener);
424 }
425
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800426
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800427 public void setCurrentGrid(Context context, String gridName) {
428 Context appContext = context.getApplicationContext();
429 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700430 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800431 }
432
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700433 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700434 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000435 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
436 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700437 }
438
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700439 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700440 Object[] oldState = toModelState();
441
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700442 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700443 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700444 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700445
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700446 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700447 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700448 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700449 }
450 }
451
Alex Chau1c883d82021-12-01 18:43:10 +0000452 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
453 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800454 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100455
Alex Chau1c883d82021-12-01 18:43:10 +0000456 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700457 final int depth = parser.getDepth();
458 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700459 while (((type = parser.next()) != XmlPullParser.END_TAG ||
460 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800461 if ((type == XmlPullParser.START_TAG)
462 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800463
Alex Chau1c883d82021-12-01 18:43:10 +0000464 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
465 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100466 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100467 final int displayDepth = parser.getDepth();
468 while (((type = parser.next()) != XmlPullParser.END_TAG
469 || parser.getDepth() > displayDepth)
470 && type != XmlPullParser.END_DOCUMENT) {
471 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
472 parser.getName())) {
473 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100474 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100475 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800476 }
477 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700478 }
479 }
sfufa@google.comde013292021-09-21 18:22:44 -0700480 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700481 throw new RuntimeException(e);
482 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800483
Sunny Goyalae190ff2020-04-14 00:19:01 +0000484 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800485 if (!TextUtils.isEmpty(gridName)) {
486 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100487 if (gridName.equals(option.grid.name)
488 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000489 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800490 }
491 }
492 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000493 if (filteredProfiles.isEmpty()) {
494 // No grid found, use the default options
495 for (DisplayOption option : profiles) {
496 if (option.canBeDefault) {
497 filteredProfiles.add(option);
498 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800499 }
500 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000501 if (filteredProfiles.isEmpty()) {
502 throw new RuntimeException("No display option with canBeDefault=true");
503 }
504 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700505 }
506
Thales Lima7ec83822021-08-05 13:32:10 +0100507 /**
508 * @return all the grid options that can be shown on the device
509 */
510 public List<GridOption> parseAllGridOptions(Context context) {
511 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100512
Alex Chau1c883d82021-12-01 18:43:10 +0000513 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100514 final int depth = parser.getDepth();
515 int type;
516 while (((type = parser.next()) != XmlPullParser.END_TAG
517 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
518 if ((type == XmlPullParser.START_TAG)
519 && GridOption.TAG_NAME.equals(parser.getName())) {
520 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000521 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100522 if (option.isEnabled) {
523 result.add(option);
524 }
525 }
526 }
527 } catch (IOException | XmlPullParserException e) {
528 Log.e(TAG, "Error parsing device profile", e);
529 return Collections.emptyList();
530 }
531 return result;
532 }
533
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700534 private int getLauncherIconDensity(int requiredSize) {
535 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700536 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700537 DisplayMetrics.DENSITY_LOW,
538 DisplayMetrics.DENSITY_MEDIUM,
539 DisplayMetrics.DENSITY_TV,
540 DisplayMetrics.DENSITY_HIGH,
541 DisplayMetrics.DENSITY_XHIGH,
542 DisplayMetrics.DENSITY_XXHIGH,
543 DisplayMetrics.DENSITY_XXXHIGH
544 };
545
546 int density = DisplayMetrics.DENSITY_XXXHIGH;
547 for (int i = densityBuckets.length - 1; i >= 0; i--) {
548 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
549 / DisplayMetrics.DENSITY_DEFAULT;
550 if (expectedSize >= requiredSize) {
551 density = densityBuckets[i];
552 }
553 }
554
555 return density;
556 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700557
Adam Cohen2e6da152015-05-06 11:42:25 -0700558 /**
559 * Apply any Partner customization grid overrides.
560 *
561 * Currently we support: all apps row / column count.
562 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700563 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
564 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700565 if (p != null) {
566 p.applyInvariantDeviceProfileOverrides(this, dm);
567 }
568 }
569
Sunny Goyal415f1732018-11-29 10:33:47 -0800570 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700571 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700572 }
573
Sunny Goyal19ff7282021-04-22 10:12:54 -0700574 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000575 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700576 int minWidthPx = Integer.MAX_VALUE;
577 int minHeightPx = Integer.MAX_VALUE;
578 for (WindowBounds bounds : displayInfo.supportedBounds) {
579 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000580 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700581 // For split displays, take half width per page
582 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
583 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700584
Sunny Goyal19ff7282021-04-22 10:12:54 -0700585 } else if (!isTablet && bounds.isLandscape()) {
586 // We will use transposed layout in this case
587 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
588 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
589 } else {
590 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
591 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
592 }
593 }
594
Thales Lima425f6822022-05-10 13:44:58 -0300595 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
596 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700597
598 // Sort the profiles based on the closeness to the device size
599 Collections.sort(points, (a, b) ->
600 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
601 dist(width, height, b.minWidthDps, b.minHeightDps)));
602
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700603 DisplayOption closestPoint = points.get(0);
604 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700605 float weights = 0;
606
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700607 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
608 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700609 }
610
611 DisplayOption out = new DisplayOption(closestOption);
612 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700613 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700614 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
615 weights += w;
616 out.add(new DisplayOption().add(p).multiply(w));
617 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700618 out.multiply(1.0f / weights);
619
Thales Lima6e0005a2021-10-27 15:53:41 +0100620 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700621 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100622 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
623 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700624 }
625
626 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700627 }
628
Sunny Goyal27835952017-01-13 12:15:53 -0800629 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700630 Resources res = context.getResources();
631 Configuration config = context.getResources().getConfiguration();
632
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400633 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
634 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100635 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
636
637 if (Utilities.IS_DEBUG_DEVICE) {
638 StringWriter stringWriter = new StringWriter();
639 PrintWriter printWriter = new PrintWriter(stringWriter);
640 DisplayController.INSTANCE.get(context).dump(printWriter);
641 printWriter.flush();
642 Log.d("b/231312158", "getDeviceProfile -"
643 + "\nconfig: " + config
644 + "\ndisplayMetrics: " + res.getDisplayMetrics()
645 + "\nrotation: " + rotation
Alex Chau661f02d2022-06-07 14:03:43 +0100646 + "\n" + stringWriter,
Alex Chaud3e8cc42022-05-03 17:45:34 +0100647 new Exception());
648 }
649 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400650 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700651
Sunny Goyal187b16c2022-03-01 16:53:23 -0800652 /**
653 * Returns the device profile matching the provided screen configuration
654 */
655 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700656 DeviceProfile bestMatch = supportedProfiles.get(0);
657 float minDiff = Float.MAX_VALUE;
658
659 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400660 float diff = Math.abs(profile.widthPx - screenWidth)
661 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700662 if (diff < minDiff) {
663 minDiff = diff;
664 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800665 } else if (diff == minDiff && profile.rotationHint == rotation) {
666 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700667 }
668 }
669 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800670 }
671
Sunny Goyal415f1732018-11-29 10:33:47 -0800672 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700673 float d = dist(x0, y0, x1, y1);
674 if (Float.compare(d, 0f) == 0) {
675 return Float.POSITIVE_INFINITY;
676 }
677 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
678 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700679
680 /**
681 * As a ratio of screen height, the total distance we want the parallax effect to span
682 * horizontally
683 */
684 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
685 float aspectRatio = width / (float) height;
686
687 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
688 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
689 // We will use these two data points to extrapolate how much the wallpaper parallax effect
690 // to span (ie travel) at any aspect ratio:
691
sfufa@google.comde013292021-09-21 18:22:44 -0700692 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
693 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700694 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
695 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
696
697 // To find out the desired width at different aspect ratios, we use the following two
698 // formulas, where the coefficient on x is the aspect ratio (width/height):
699 // (16/10)x + y = 1.5
700 // (10/16)x + y = 1.2
701 // We solve for x and y and end up with a final formula:
702 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700703 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
704 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700705 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
706 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
707 return x * aspectRatio + y;
708 }
709
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700710 public interface OnIDPChangeListener {
711
Sunny Goyalb47172b2021-05-03 19:59:51 -0700712 /**
713 * Called when the device provide changes
714 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700715 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700716 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800717
718
Sunny Goyaleff44f32019-01-09 17:29:49 -0800719 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800720
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800721 public static final String TAG_NAME = "grid-option";
722
Alex Chau1c883d82021-12-01 18:43:10 +0000723 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
724 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
725 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
726 private static final int DEVICE_CATEGORY_ALL =
727 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
728
Sunny Goyaleff44f32019-01-09 17:29:49 -0800729 public final String name;
730 public final int numRows;
731 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000732 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100733 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800734
735 private final int numFolderRows;
736 private final int numFolderColumns;
737
Sunny Goyal19ff7282021-04-22 10:12:54 -0700738 private final int numAllAppsColumns;
739 private final int numDatabaseAllAppsColumns;
740 private final int numHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000741 private final int numShrunkenHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700742 private final int numDatabaseHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300743 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800744
Tracy Zhou7df93d22020-01-27 13:44:06 -0800745 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000746
Sunny Goyal415f1732018-11-29 10:33:47 -0800747 private final int defaultLayoutId;
748 private final int demoModeLayoutId;
749
Jon Mirandae126d722021-02-25 10:45:20 -0500750 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400751 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500752
Sunny Goyal5bc18462019-01-07 15:13:39 -0800753 private final SparseArray<TypedValue> extraAttrs;
754
Alex Chau1c883d82021-12-01 18:43:10 +0000755 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800756 TypedArray a = context.obtainStyledAttributes(
757 attrs, R.styleable.GridDisplayOption);
758 name = a.getString(R.styleable.GridDisplayOption_name);
759 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
760 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000761 numSearchContainerColumns = a.getInt(
762 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800763
Tracy Zhou7df93d22020-01-27 13:44:06 -0800764 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000765 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100766 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
767 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
768 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800769 demoModeLayoutId = a.getResourceId(
770 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700771
772 numAllAppsColumns = a.getInt(
773 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
774 numDatabaseAllAppsColumns = a.getInt(
775 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
776
777 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800778 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Alex Chau6ed408f2022-03-04 12:58:05 +0000779 numShrunkenHotseatIcons = a.getInt(
780 R.styleable.GridDisplayOption_numShrunkenHotseatIcons, numHotseatIcons / 2);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700781 numDatabaseHotseatIcons = a.getInt(
782 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Lima425f6822022-05-10 13:44:58 -0300783 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
784 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
785 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
786 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
787 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
788 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
789 numColumns);
790 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
791 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
792 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700793
Sunny Goyal415f1732018-11-29 10:33:47 -0800794 numFolderRows = a.getInt(
795 R.styleable.GridDisplayOption_numFolderRows, numRows);
796 numFolderColumns = a.getInt(
797 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700798
Jon Mirandae126d722021-02-25 10:45:20 -0500799 isScalable = a.getBoolean(
800 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400801 devicePaddingId = a.getResourceId(
802 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500803
Alex Chau1c883d82021-12-01 18:43:10 +0000804 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
805 DEVICE_CATEGORY_ALL);
806 isEnabled = (deviceType == TYPE_PHONE
807 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
808 || (deviceType == TYPE_TABLET
809 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
810 || (deviceType == TYPE_MULTI_DISPLAY
811 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
812 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100813
Sunny Goyal415f1732018-11-29 10:33:47 -0800814 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800815 extraAttrs = Themes.createValueMap(context, attrs,
816 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800817 }
818 }
819
Sunny Goyal19ff7282021-04-22 10:12:54 -0700820 @VisibleForTesting
821 static final class DisplayOption {
Thales Lima12d0eff2022-03-25 17:06:11 +0000822 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
823 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
824 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
825 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
826 private static final int DONT_INLINE_QSB = 0;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700827
828 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800829
Sunny Goyal415f1732018-11-29 10:33:47 -0800830 private final float minWidthDps;
831 private final float minHeightDps;
832 private final boolean canBeDefault;
Thales Lima12d0eff2022-03-25 17:06:11 +0000833 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800834
Thales Lima83bedbf2021-10-05 17:47:39 +0100835 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100836
837 private float folderBorderSpace;
838 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100839 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Lima425f6822022-05-10 13:44:58 -0300840 //TODO(http://b/228998082) remove this when 3 button spaces are fixed
Thales Lima6e7f36c2022-01-04 12:14:11 +0000841 private final float[] hotseatBorderSpaces = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100842
Thales Lima78d00ad2021-09-30 11:29:06 +0100843 private final float[] iconSizes = new float[COUNT_SIZES];
844 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800845
Thales Limab7ef5692022-03-10 10:32:36 +0000846 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000847 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
848 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
849 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
850
Thales Lima1de4d552021-10-13 16:13:25 +0100851 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800852 this.grid = grid;
853
Thales Lima1de4d552021-10-13 16:13:25 +0100854 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800855
Sunny Goyal415f1732018-11-29 10:33:47 -0800856 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
857 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700858
Thales Lima1de4d552021-10-13 16:13:25 +0100859 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800860
Thales Lima12d0eff2022-03-25 17:06:11 +0000861 int inlineForRotation = a.getInt(R.styleable.ProfileDisplayOption_inlineQsb,
862 DONT_INLINE_QSB);
863 inlineQsb[INDEX_DEFAULT] =
864 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
865 inlineQsb[INDEX_LANDSCAPE] =
866 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
867 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
868 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
869 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
870 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
871 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
872 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
873
Thales Lima83bedbf2021-10-05 17:47:39 +0100874 float x;
875 float y;
876
Thales Lima85c942f2021-12-31 13:04:20 +0000877 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
878 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100879 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000880
881 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
882 minCellSize[INDEX_DEFAULT].x);
883 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
884 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100885 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100886
Thales Lima85c942f2021-12-31 13:04:20 +0000887 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100888 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000889 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100890 minCellSize[INDEX_DEFAULT].y);
891 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
892
Thales Lima85c942f2021-12-31 13:04:20 +0000893 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100894 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000895 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100896 minCellSize[INDEX_DEFAULT].y);
897 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100898
Thales Lima85c942f2021-12-31 13:04:20 +0000899 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000900 float borderSpaceLandscape = a.getFloat(
901 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000902 float borderSpaceTwoPanelPortrait = a.getFloat(
903 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
904 float borderSpaceTwoPanelLandscape = a.getFloat(
905 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100906
Thales Lima85c942f2021-12-31 13:04:20 +0000907 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
908 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100909 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000910
911 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
912 borderSpaceLandscape);
913 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
914 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100915 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
916
917 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000918 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
919 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100920 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000921 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
922 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100923 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
924
925 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000926 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
927 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100928 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000929 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
930 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100931 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
932
Thales Limabb7d3882021-12-22 12:56:29 +0000933 folderBorderSpace = borderSpace;
934
Thales Limab7ef5692022-03-10 10:32:36 +0000935 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
936 minCellSize[INDEX_DEFAULT].x);
937 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
938 minCellSize[INDEX_DEFAULT].y);
939 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
940
941 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
942 allAppsCellSize[INDEX_DEFAULT].x);
943 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
944 allAppsCellSize[INDEX_DEFAULT].y);
945 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
946
947 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
948 allAppsCellSize[INDEX_DEFAULT].x);
949 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
950 allAppsCellSize[INDEX_DEFAULT].y);
951 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
952
953 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
954 allAppsCellSize[INDEX_DEFAULT].x);
955 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
956 allAppsCellSize[INDEX_DEFAULT].y);
957 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
958
Thales Lima080d8902022-03-28 15:30:29 +0100959 float allAppsBorderSpace = a.getFloat(
960 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
961 float allAppsBorderSpaceLandscape = a.getFloat(
962 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
963 allAppsBorderSpace);
964 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000965 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100966 allAppsBorderSpace);
967 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000968 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100969 allAppsBorderSpace);
970
971 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
972 allAppsBorderSpace);
973 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
974 allAppsBorderSpace);
975 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
976
977 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
978 allAppsBorderSpaceLandscape);
979 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
980 allAppsBorderSpaceLandscape);
981 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
982
983 x = a.getFloat(
984 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
985 allAppsBorderSpaceTwoPanelPortrait);
986 y = a.getFloat(
987 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
988 allAppsBorderSpaceTwoPanelPortrait);
989 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
990
991 x = a.getFloat(
992 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
993 allAppsBorderSpaceTwoPanelLandscape);
994 y = a.getFloat(
995 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
996 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +0000997 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -0500998
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700999 iconSizes[INDEX_DEFAULT] =
1000 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1001 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001002 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001003 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001004 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001005 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001006 iconSizes[INDEX_DEFAULT]);
1007 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001008 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001009 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001010
Thales Limabb7d3882021-12-22 12:56:29 +00001011 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1012 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
1013 allAppsIconSizes[INDEX_LANDSCAPE] = allAppsIconSizes[INDEX_DEFAULT];
1014 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1015 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1016 allAppsIconSizes[INDEX_DEFAULT]);
1017 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1018 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1019 allAppsIconSizes[INDEX_DEFAULT]);
1020
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001021 textSizes[INDEX_DEFAULT] =
1022 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1023 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001024 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001025 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001026 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001027 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001028 textSizes[INDEX_DEFAULT]);
1029 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001030 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001031 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001032
Thales Limabb7d3882021-12-22 12:56:29 +00001033 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1034 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1035 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1036 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1037 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1038 allAppsIconTextSizes[INDEX_DEFAULT]);
1039 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1040 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1041 allAppsIconTextSizes[INDEX_DEFAULT]);
1042
Thales Lima83bedbf2021-10-05 17:47:39 +01001043 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1044 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001045 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1046 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1047 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001048 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001049 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001050 horizontalMargin[INDEX_DEFAULT]);
1051 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001052 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001053 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001054
Thales Lima6e7f36c2022-01-04 12:14:11 +00001055 hotseatBorderSpaces[INDEX_DEFAULT] = a.getFloat(
1056 R.styleable.ProfileDisplayOption_hotseatBorderSpace, borderSpace);
Thales Limadd027342022-01-07 12:54:37 +00001057 hotseatBorderSpaces[INDEX_LANDSCAPE] = a.getFloat(
1058 R.styleable.ProfileDisplayOption_hotseatBorderSpaceLandscape,
1059 hotseatBorderSpaces[INDEX_DEFAULT]);
Thales Lima6e7f36c2022-01-04 12:14:11 +00001060 hotseatBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1061 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelLandscape,
1062 hotseatBorderSpaces[INDEX_DEFAULT]);
1063 hotseatBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1064 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelPortrait,
1065 hotseatBorderSpaces[INDEX_DEFAULT]);
1066
Sunny Goyal415f1732018-11-29 10:33:47 -08001067 a.recycle();
1068 }
1069
1070 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001071 this(null);
1072 }
1073
1074 DisplayOption(GridOption grid) {
1075 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001076 minWidthDps = 0;
1077 minHeightDps = 0;
1078 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001079 for (int i = 0; i < COUNT_SIZES; i++) {
1080 iconSizes[i] = 0;
1081 textSizes[i] = 0;
1082 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001083 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001084 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001085 allAppsIconSizes[i] = 0;
1086 allAppsIconTextSizes[i] = 0;
1087 allAppsBorderSpaces[i] = new PointF();
Thales Lima12d0eff2022-03-25 17:06:11 +00001088 inlineQsb[i] = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001089 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001090 }
1091
1092 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001093 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001094 iconSizes[i] *= w;
1095 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001096 borderSpaces[i].x *= w;
1097 borderSpaces[i].y *= w;
1098 minCellSize[i].x *= w;
1099 minCellSize[i].y *= w;
1100 horizontalMargin[i] *= w;
Thales Lima6e7f36c2022-01-04 12:14:11 +00001101 hotseatBorderSpaces[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001102 allAppsCellSize[i].x *= w;
1103 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001104 allAppsIconSizes[i] *= w;
1105 allAppsIconTextSizes[i] *= w;
1106 allAppsBorderSpaces[i].x *= w;
1107 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001108 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001109
1110 folderBorderSpace *= w;
1111
Sunny Goyal415f1732018-11-29 10:33:47 -08001112 return this;
1113 }
1114
1115 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001116 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001117 iconSizes[i] += p.iconSizes[i];
1118 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001119 borderSpaces[i].x += p.borderSpaces[i].x;
1120 borderSpaces[i].y += p.borderSpaces[i].y;
1121 minCellSize[i].x += p.minCellSize[i].x;
1122 minCellSize[i].y += p.minCellSize[i].y;
1123 horizontalMargin[i] += p.horizontalMargin[i];
Thales Lima6e7f36c2022-01-04 12:14:11 +00001124 hotseatBorderSpaces[i] += p.hotseatBorderSpaces[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001125 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1126 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001127 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1128 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1129 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1130 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Thales Lima12d0eff2022-03-25 17:06:11 +00001131 inlineQsb[i] |= p.inlineQsb[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001132 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001133
1134 folderBorderSpace += p.folderBorderSpace;
1135
Sunny Goyal415f1732018-11-29 10:33:47 -08001136 return this;
1137 }
1138 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001139}