blob: d518eca4f8c1f671c044b7a43a66a88c56463f31 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
Thales Lima1de4d552021-10-13 16:13:25 +01002 * Copyright (C) 2021 The Android Open Source Project
Adam Cohen2e6da152015-05-06 11:42:25 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Sunny Goyal19ff7282021-04-22 10:12:54 -070019import static com.android.launcher3.Utilities.dpiFromPx;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010020import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Sunny Goyal35c7b192021-04-20 16:51:10 -070021import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Alex Chau6ed408f2022-03-04 12:58:05 +000022import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE;
Sunny Goyal19ff7282021-04-22 10:12:54 -070023import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080024import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070025
Sunny Goyalc6205602015-05-21 20:46:33 -070026import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070028import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070029import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080030import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080031import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070032import android.content.res.TypedArray;
33import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010035import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070036import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080037import android.text.TextUtils;
38import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070039import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010040import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080041import android.util.SparseArray;
42import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070043import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080044import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070045
Alex Chau1c883d82021-12-01 18:43:10 +000046import androidx.annotation.IntDef;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070047import androidx.annotation.Nullable;
48import androidx.annotation.VisibleForTesting;
Thales Limab8c05952022-05-23 16:58:38 +010049import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070050
Sunny Goyal65190ae2022-08-02 14:16:26 -070051import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070052import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010053import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070054import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070055import com.android.launcher3.util.DisplayController;
56import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080057import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070058import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080059import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070060import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080061import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070062
Sunny Goyal819e1932016-07-07 16:43:58 -070063import org.xmlpull.v1.XmlPullParser;
64import org.xmlpull.v1.XmlPullParserException;
65
66import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000067import java.lang.annotation.Retention;
68import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070069import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070070import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080071import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070072import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070073
74public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070075
Hyunyoung Songc55a3502018-12-04 15:43:16 -080076 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070077 // We do not need any synchronization for this variable as its only written on UI thread.
78 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070079 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070080
Alex Chau1c883d82021-12-01 18:43:10 +000081 @Retention(RetentionPolicy.SOURCE)
82 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010083 public @interface DeviceType {}
84
Alex Chau1c883d82021-12-01 18:43:10 +000085 public static final int TYPE_PHONE = 0;
86 public static final int TYPE_MULTI_DISPLAY = 1;
87 public static final int TYPE_TABLET = 2;
88
Hyunyoung Songc55a3502018-12-04 15:43:16 -080089 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080090
Sunny Goyal53d7ee42015-05-22 12:25:45 -070091 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
92
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070093 // Constants that affects the interpolation curve between statically defined device profile
94 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080095 private static final float KNEARESTNEIGHBOR = 3;
96 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070097
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080099 private static final float WEIGHT_EFFICIENT = 100000f;
100
Thales Lima83bedbf2021-10-05 17:47:39 +0100101 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
102 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000103 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100104 static final int INDEX_DEFAULT = 0;
105 static final int INDEX_LANDSCAPE = 1;
106 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
107 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100108
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700109 /**
110 * Number of icons per row and column in the workspace.
111 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700112 public int numRows;
113 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000114 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700115
116 /**
117 * Number of icons per row and column in the folder.
118 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700119 public int numFolderRows;
120 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100121 public float[] iconSize;
122 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700123 public int iconBitmapSize;
124 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000125 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700126
Thales Lima83bedbf2021-10-05 17:47:39 +0100127 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100128
Thales Lima83bedbf2021-10-05 17:47:39 +0100129 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100130 public float folderBorderSpace;
Vinit Nayakc7293172022-07-18 16:41:50 -0700131 public int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100132
Thales Lima83bedbf2021-10-05 17:47:39 +0100133 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500134
Thales Limab7ef5692022-03-10 10:32:36 +0000135 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000136 public float[] allAppsIconSize;
137 public float[] allAppsIconTextSize;
138 public PointF[] allAppsBorderSpaces;
139
Sunny Goyal5bc18462019-01-07 15:13:39 -0800140 private SparseArray<TypedValue> mExtraAttrs;
141
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700142 /**
143 * Number of icons inside the hotseat area.
144 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800145 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700146
147 /**
148 * Number of icons inside the hotseat area that is stored in the database. This is greater than
149 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
150 * sizes that share the same DB.
151 */
152 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700153
Thales Lima425f6822022-05-10 13:44:58 -0300154 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100155 public float[] hotseatBarBottomSpace;
156 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300157
Jon Miranda6f7e9702019-09-16 14:44:14 -0700158 /**
159 * Number of columns in the all apps list.
160 */
161 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700162 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700163
Jon Mirandae126d722021-02-25 10:45:20 -0500164 /**
165 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
166 */
167 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400168 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500169
Tracy Zhou7df93d22020-01-27 13:44:06 -0800170 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800171 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700172 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100173 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700174
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000175 /**
176 * An immutable list of supported profiles.
177 */
178 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700179
sfufa@google.comde013292021-09-21 18:22:44 -0700180 @Nullable
181 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500182
Sunny Goyal6f866092016-03-17 17:04:15 -0700183 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700184 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700185
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700186 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700187
Sunny Goyalf633ef52018-03-13 09:57:05 -0700188 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800189 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700190
Sunny Goyalbbf01842015-10-08 07:41:15 -0700191 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700192 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700193 String gridName = getCurrentGridName(context);
194 String newGridName = initGrid(context, gridName);
195 if (!newGridName.equals(gridName)) {
196 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
197 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700198 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700199
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700200 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100201 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000202 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
203 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100204 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700205 }
206 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700207 }
208
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700209 /**
210 * This constructor should NOT have any monitors by design.
211 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800212 public InvariantDeviceProfile(Context context, String gridName) {
213 String newName = initGrid(context, gridName);
214 if (newName == null || !newName.equals(gridName)) {
215 throw new IllegalArgumentException("Unknown grid name");
216 }
217 }
218
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700219 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800220 * This constructor should NOT have any monitors by design.
221 */
222 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700223 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100224 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700225 String gridName = getCurrentGridName(context);
226
227 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000228 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
229 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700230 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000231 defaultInfo,
232 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
233 /*allowDisabledGrid=*/false),
234 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235
Alex Chau661f02d2022-06-07 14:03:43 +0100236 Context displayContext = context.createDisplayContext(display);
237 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000238 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700239 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000240 myInfo,
241 getPredefinedDeviceProfiles(context, gridName, deviceType,
242 /*allowDisabledGrid=*/false),
243 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700244
245 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
246 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100247 result.iconSizes[INDEX_DEFAULT] =
248 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
249 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700250 result.iconSizes[i] = Math.min(
251 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000252 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700253
Thales Lima83bedbf2021-10-05 17:47:39 +0100254 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
255 COUNT_SIZES);
256 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
257 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500258
Alex Chau1c883d82021-12-01 18:43:10 +0000259 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800260 }
261
Alex Chau238aaee2021-10-06 16:15:24 +0100262 /**
263 * Reinitialize the current grid after a restore, where some grids might now be disabled.
264 */
265 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000266 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100267 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000268 String newGridName = initGrid(context, currentGridName);
269 String newDbFile = dbFile;
270 if (!newDbFile.equals(currentDbFile)) {
271 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100272 + ", migrating to: " + newGridName
273 + ", removing all other grid db files");
274 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
275 if (gridDbFile.equals(currentDbFile)) {
276 continue;
277 }
278 if (context.getDatabasePath(gridDbFile).delete()) {
279 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
280 }
281 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000282 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100283 }
284 }
285
Alex Chau1c883d82021-12-01 18:43:10 +0000286 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800287 int flagPhone = 1 << 0;
288 int flagTablet = 1 << 1;
289
290 int type = displayInfo.supportedBounds.stream()
291 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
292 .reduce(0, (a, b) -> a | b);
293 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
294 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000295 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800296 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000297 return TYPE_TABLET;
298 } else {
299 return TYPE_PHONE;
300 }
301 }
302
Tracy Zhou42255d22020-03-13 00:38:11 -0700303 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700304 return Utilities.isGridOptionsEnabled(context)
305 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700306 }
307
Sunny Goyaleff44f32019-01-09 17:29:49 -0800308 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700309 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000310 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700311
312 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000313 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100314 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700315 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000316 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
317 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700318 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800319 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700320
Alex Chau1c883d82021-12-01 18:43:10 +0000321 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
322 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700323 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700324 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000325 numRows = closestProfile.numRows;
326 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000327 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000328 dbFile = closestProfile.dbFile;
329 defaultLayoutId = closestProfile.defaultLayoutId;
330 demoModeLayoutId = closestProfile.demoModeLayoutId;
331 numFolderRows = closestProfile.numFolderRows;
332 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500333 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400334 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000335 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000336
Vinit Nayakc7293172022-07-18 16:41:50 -0700337 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
338
Sunny Goyalae190ff2020-04-14 00:19:01 +0000339 mExtraAttrs = closestProfile.extraAttrs;
340
Thales Lima83bedbf2021-10-05 17:47:39 +0100341 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100342 float maxIconSize = iconSize[0];
343 for (int i = 1; i < iconSize.length; i++) {
344 maxIconSize = Math.max(maxIconSize, iconSize[i]);
345 }
346 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000347 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
348
Thales Lima83bedbf2021-10-05 17:47:39 +0100349 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700350
Thales Lima83bedbf2021-10-05 17:47:39 +0100351 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100352
Thales Lima83bedbf2021-10-05 17:47:39 +0100353 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100354 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700355
Thales Limad90faab2021-09-21 17:18:47 +0100356 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100357
Sunny Goyal19ff7282021-04-22 10:12:54 -0700358 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000359 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700360 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300361 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100362 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
363 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700364
365 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000366 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700367 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500368
Thales Limab7ef5692022-03-10 10:32:36 +0000369 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000370 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
371 allAppsIconSize = displayOption.allAppsIconSizes;
372 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100373 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000374 allAppsIconSize = iconSize;
375 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000376 }
377
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400378 if (devicePaddingId != 0) {
379 devicePaddings = new DevicePaddings(context, devicePaddingId);
380 }
381
Thales Lima11af7bc2022-08-12 15:24:54 +0100382 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000383
Sunny Goyalae190ff2020-04-14 00:19:01 +0000384 // If the partner customization apk contains any grid overrides, apply them
385 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700386 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700387
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000388 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700389 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700390 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700391 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)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700395 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000396 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700397
Sunny Goyal19ff7282021-04-22 10:12:54 -0700398 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
399 int displayWidth = bounds.bounds.width();
400 int displayHeight = bounds.bounds.height();
401 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700402
Sunny Goyal19ff7282021-04-22 10:12:54 -0700403 // We need to ensure that there is enough extra space in the wallpaper
404 // for the intended parallax effects
405 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300406 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
407 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700408 ? 2
409 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
410 defaultWallpaperSize.x =
411 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700412 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000413 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700414
415 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
416 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700417 }
418
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700419 public void addOnChangeListener(OnIDPChangeListener listener) {
420 mChangeListeners.add(listener);
421 }
422
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800423 public void removeOnChangeListener(OnIDPChangeListener listener) {
424 mChangeListeners.remove(listener);
425 }
426
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800427
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800428 public void setCurrentGrid(Context context, String gridName) {
429 Context appContext = context.getApplicationContext();
430 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700431 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800432 }
433
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700434 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700435 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000436 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
437 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700438 }
439
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700440 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700441 Object[] oldState = toModelState();
442
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700443 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700444 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700445 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700446
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700447 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700448 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700449 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700450 }
451 }
452
Alex Chau1c883d82021-12-01 18:43:10 +0000453 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
454 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800455 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100456
Alex Chau1c883d82021-12-01 18:43:10 +0000457 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700458 final int depth = parser.getDepth();
459 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700460 while (((type = parser.next()) != XmlPullParser.END_TAG ||
461 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800462 if ((type == XmlPullParser.START_TAG)
463 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800464
Alex Chau1c883d82021-12-01 18:43:10 +0000465 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
466 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100467 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100468 final int displayDepth = parser.getDepth();
469 while (((type = parser.next()) != XmlPullParser.END_TAG
470 || parser.getDepth() > displayDepth)
471 && type != XmlPullParser.END_DOCUMENT) {
472 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
473 parser.getName())) {
474 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100475 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100476 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800477 }
478 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700479 }
480 }
sfufa@google.comde013292021-09-21 18:22:44 -0700481 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700482 throw new RuntimeException(e);
483 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800484
Sunny Goyalae190ff2020-04-14 00:19:01 +0000485 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800486 if (!TextUtils.isEmpty(gridName)) {
487 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100488 if (gridName.equals(option.grid.name)
489 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000490 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800491 }
492 }
493 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000494 if (filteredProfiles.isEmpty()) {
495 // No grid found, use the default options
496 for (DisplayOption option : profiles) {
497 if (option.canBeDefault) {
498 filteredProfiles.add(option);
499 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800500 }
501 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000502 if (filteredProfiles.isEmpty()) {
503 throw new RuntimeException("No display option with canBeDefault=true");
504 }
505 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700506 }
507
Thales Lima7ec83822021-08-05 13:32:10 +0100508 /**
509 * @return all the grid options that can be shown on the device
510 */
511 public List<GridOption> parseAllGridOptions(Context context) {
512 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100513
Alex Chau1c883d82021-12-01 18:43:10 +0000514 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100515 final int depth = parser.getDepth();
516 int type;
517 while (((type = parser.next()) != XmlPullParser.END_TAG
518 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
519 if ((type == XmlPullParser.START_TAG)
520 && GridOption.TAG_NAME.equals(parser.getName())) {
521 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000522 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100523 if (option.isEnabled) {
524 result.add(option);
525 }
526 }
527 }
528 } catch (IOException | XmlPullParserException e) {
529 Log.e(TAG, "Error parsing device profile", e);
530 return Collections.emptyList();
531 }
532 return result;
533 }
534
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700535 private int getLauncherIconDensity(int requiredSize) {
536 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700537 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700538 DisplayMetrics.DENSITY_LOW,
539 DisplayMetrics.DENSITY_MEDIUM,
540 DisplayMetrics.DENSITY_TV,
541 DisplayMetrics.DENSITY_HIGH,
542 DisplayMetrics.DENSITY_XHIGH,
543 DisplayMetrics.DENSITY_XXHIGH,
544 DisplayMetrics.DENSITY_XXXHIGH
545 };
546
547 int density = DisplayMetrics.DENSITY_XXXHIGH;
548 for (int i = densityBuckets.length - 1; i >= 0; i--) {
549 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
550 / DisplayMetrics.DENSITY_DEFAULT;
551 if (expectedSize >= requiredSize) {
552 density = densityBuckets[i];
553 }
554 }
555
556 return density;
557 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700558
Adam Cohen2e6da152015-05-06 11:42:25 -0700559 /**
560 * Apply any Partner customization grid overrides.
561 *
562 * Currently we support: all apps row / column count.
563 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700564 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
565 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700566 if (p != null) {
567 p.applyInvariantDeviceProfileOverrides(this, dm);
568 }
569 }
570
Sunny Goyal415f1732018-11-29 10:33:47 -0800571 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700572 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700573 }
574
Sunny Goyal19ff7282021-04-22 10:12:54 -0700575 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000576 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700577 int minWidthPx = Integer.MAX_VALUE;
578 int minHeightPx = Integer.MAX_VALUE;
579 for (WindowBounds bounds : displayInfo.supportedBounds) {
580 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000581 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700582 // For split displays, take half width per page
583 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
584 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700585
Sunny Goyal19ff7282021-04-22 10:12:54 -0700586 } else if (!isTablet && bounds.isLandscape()) {
587 // We will use transposed layout in this case
588 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
589 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
590 } else {
591 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
592 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
593 }
594 }
595
Thales Lima425f6822022-05-10 13:44:58 -0300596 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
597 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700598
599 // Sort the profiles based on the closeness to the device size
600 Collections.sort(points, (a, b) ->
601 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
602 dist(width, height, b.minWidthDps, b.minHeightDps)));
603
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700604 DisplayOption closestPoint = points.get(0);
605 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700606 float weights = 0;
607
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700608 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
609 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700610 }
611
612 DisplayOption out = new DisplayOption(closestOption);
613 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700614 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700615 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
616 weights += w;
617 out.add(new DisplayOption().add(p).multiply(w));
618 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700619 out.multiply(1.0f / weights);
620
Thales Lima6e0005a2021-10-27 15:53:41 +0100621 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700622 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100623 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
624 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700625 }
626
627 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700628 }
629
Sunny Goyal27835952017-01-13 12:15:53 -0800630 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700631 Resources res = context.getResources();
632 Configuration config = context.getResources().getConfiguration();
633
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400634 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
635 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100636 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
637
Alex Chaud3e8cc42022-05-03 17:45:34 +0100638 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400639 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700640
Sunny Goyal187b16c2022-03-01 16:53:23 -0800641 /**
642 * Returns the device profile matching the provided screen configuration
643 */
644 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700645 DeviceProfile bestMatch = supportedProfiles.get(0);
646 float minDiff = Float.MAX_VALUE;
647
648 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400649 float diff = Math.abs(profile.widthPx - screenWidth)
650 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700651 if (diff < minDiff) {
652 minDiff = diff;
653 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800654 } else if (diff == minDiff && profile.rotationHint == rotation) {
655 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700656 }
657 }
658 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800659 }
660
Sunny Goyal415f1732018-11-29 10:33:47 -0800661 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700662 float d = dist(x0, y0, x1, y1);
663 if (Float.compare(d, 0f) == 0) {
664 return Float.POSITIVE_INFINITY;
665 }
666 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
667 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700668
669 /**
670 * As a ratio of screen height, the total distance we want the parallax effect to span
671 * horizontally
672 */
673 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
674 float aspectRatio = width / (float) height;
675
676 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
677 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
678 // We will use these two data points to extrapolate how much the wallpaper parallax effect
679 // to span (ie travel) at any aspect ratio:
680
sfufa@google.comde013292021-09-21 18:22:44 -0700681 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
682 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700683 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
684 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
685
686 // To find out the desired width at different aspect ratios, we use the following two
687 // formulas, where the coefficient on x is the aspect ratio (width/height):
688 // (16/10)x + y = 1.5
689 // (10/16)x + y = 1.2
690 // We solve for x and y and end up with a final formula:
691 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700692 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
693 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700694 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
695 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
696 return x * aspectRatio + y;
697 }
698
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700699 public interface OnIDPChangeListener {
700
Sunny Goyalb47172b2021-05-03 19:59:51 -0700701 /**
702 * Called when the device provide changes
703 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700704 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700705 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800706
707
Sunny Goyaleff44f32019-01-09 17:29:49 -0800708 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800709
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800710 public static final String TAG_NAME = "grid-option";
711
Alex Chau1c883d82021-12-01 18:43:10 +0000712 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
713 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
714 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
715 private static final int DEVICE_CATEGORY_ALL =
716 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
717
Thales Lima11af7bc2022-08-12 15:24:54 +0100718 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
719 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
720 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
721 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
722 private static final int DONT_INLINE_QSB = 0;
723
Sunny Goyaleff44f32019-01-09 17:29:49 -0800724 public final String name;
725 public final int numRows;
726 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000727 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100728 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800729
730 private final int numFolderRows;
731 private final int numFolderColumns;
732
Sunny Goyal19ff7282021-04-22 10:12:54 -0700733 private final int numAllAppsColumns;
734 private final int numDatabaseAllAppsColumns;
735 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700736 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100737
Thales Lima425f6822022-05-10 13:44:58 -0300738 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800739
Thales Lima11af7bc2022-08-12 15:24:54 +0100740 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
741
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);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700777 numDatabaseHotseatIcons = a.getInt(
778 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100779
Thales Lima425f6822022-05-10 13:44:58 -0300780 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
781 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
782 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
783 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
784 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
785 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
786 numColumns);
787 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
788 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
789 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700790
Vinit Nayakc7293172022-07-18 16:41:50 -0700791 inlineNavButtonsEndSpacing =
792 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
793 R.dimen.taskbar_button_margin_default);
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
Thales Lima11af7bc2022-08-12 15:24:54 +0100814 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
815 DONT_INLINE_QSB);
816 inlineQsb[INDEX_DEFAULT] =
817 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
818 inlineQsb[INDEX_LANDSCAPE] =
819 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
820 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
821 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
822 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
823 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
824 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
825 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
826
Sunny Goyal415f1732018-11-29 10:33:47 -0800827 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800828 extraAttrs = Themes.createValueMap(context, attrs,
829 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800830 }
831 }
832
Sunny Goyal19ff7282021-04-22 10:12:54 -0700833 @VisibleForTesting
834 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700835 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800836
Sunny Goyal415f1732018-11-29 10:33:47 -0800837 private final float minWidthDps;
838 private final float minHeightDps;
839 private final boolean canBeDefault;
840
Thales Lima83bedbf2021-10-05 17:47:39 +0100841 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100842
843 private float folderBorderSpace;
844 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100845 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100846 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
847 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100848
Thales Lima78d00ad2021-09-30 11:29:06 +0100849 private final float[] iconSizes = new float[COUNT_SIZES];
850 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800851
Thales Limab7ef5692022-03-10 10:32:36 +0000852 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000853 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
854 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
855 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
856
Thales Lima1de4d552021-10-13 16:13:25 +0100857 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800858 this.grid = grid;
859
Thales Lima1de4d552021-10-13 16:13:25 +0100860 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800861
Sunny Goyal415f1732018-11-29 10:33:47 -0800862 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
863 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700864
Thales Lima1de4d552021-10-13 16:13:25 +0100865 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800866
Thales Lima83bedbf2021-10-05 17:47:39 +0100867 float x;
868 float y;
869
Thales Lima85c942f2021-12-31 13:04:20 +0000870 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
871 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100872 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000873
874 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
875 minCellSize[INDEX_DEFAULT].x);
876 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
877 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100878 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100879
Thales Lima85c942f2021-12-31 13:04:20 +0000880 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100881 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000882 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100883 minCellSize[INDEX_DEFAULT].y);
884 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
885
Thales Lima85c942f2021-12-31 13:04:20 +0000886 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100887 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000888 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100889 minCellSize[INDEX_DEFAULT].y);
890 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100891
Thales Lima85c942f2021-12-31 13:04:20 +0000892 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000893 float borderSpaceLandscape = a.getFloat(
894 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000895 float borderSpaceTwoPanelPortrait = a.getFloat(
896 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
897 float borderSpaceTwoPanelLandscape = a.getFloat(
898 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100899
Thales Lima85c942f2021-12-31 13:04:20 +0000900 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
901 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100902 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000903
904 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
905 borderSpaceLandscape);
906 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
907 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100908 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
909
910 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000911 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
912 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100913 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000914 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
915 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100916 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
917
918 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000919 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
920 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100921 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000922 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
923 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100924 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
925
Thales Limabb7d3882021-12-22 12:56:29 +0000926 folderBorderSpace = borderSpace;
927
Thales Limab7ef5692022-03-10 10:32:36 +0000928 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
929 minCellSize[INDEX_DEFAULT].x);
930 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
931 minCellSize[INDEX_DEFAULT].y);
932 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
933
934 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
935 allAppsCellSize[INDEX_DEFAULT].x);
936 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
937 allAppsCellSize[INDEX_DEFAULT].y);
938 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
939
940 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
941 allAppsCellSize[INDEX_DEFAULT].x);
942 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
943 allAppsCellSize[INDEX_DEFAULT].y);
944 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
945
946 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
947 allAppsCellSize[INDEX_DEFAULT].x);
948 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
949 allAppsCellSize[INDEX_DEFAULT].y);
950 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
951
Thales Lima080d8902022-03-28 15:30:29 +0100952 float allAppsBorderSpace = a.getFloat(
953 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
954 float allAppsBorderSpaceLandscape = a.getFloat(
955 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
956 allAppsBorderSpace);
957 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000958 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100959 allAppsBorderSpace);
960 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000961 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100962 allAppsBorderSpace);
963
964 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
965 allAppsBorderSpace);
966 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
967 allAppsBorderSpace);
968 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
969
970 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
971 allAppsBorderSpaceLandscape);
972 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
973 allAppsBorderSpaceLandscape);
974 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
975
976 x = a.getFloat(
977 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
978 allAppsBorderSpaceTwoPanelPortrait);
979 y = a.getFloat(
980 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
981 allAppsBorderSpaceTwoPanelPortrait);
982 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
983
984 x = a.getFloat(
985 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
986 allAppsBorderSpaceTwoPanelLandscape);
987 y = a.getFloat(
988 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
989 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +0000990 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -0500991
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700992 iconSizes[INDEX_DEFAULT] =
993 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
994 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000995 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700996 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700997 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +0000998 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700999 iconSizes[INDEX_DEFAULT]);
1000 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001001 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001002 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001003
Thales Limabb7d3882021-12-22 12:56:29 +00001004 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1005 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001006 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1007 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
1008 iconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001009 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1010 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1011 allAppsIconSizes[INDEX_DEFAULT]);
1012 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1013 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1014 allAppsIconSizes[INDEX_DEFAULT]);
1015
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001016 textSizes[INDEX_DEFAULT] =
1017 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1018 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001019 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001020 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001021 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001022 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001023 textSizes[INDEX_DEFAULT]);
1024 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001025 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001026 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001027
Thales Limabb7d3882021-12-22 12:56:29 +00001028 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1029 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1030 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1031 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1032 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1033 allAppsIconTextSizes[INDEX_DEFAULT]);
1034 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1035 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1036 allAppsIconTextSizes[INDEX_DEFAULT]);
1037
Thales Lima83bedbf2021-10-05 17:47:39 +01001038 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1039 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001040 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1041 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1042 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001043 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001044 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001045 horizontalMargin[INDEX_DEFAULT]);
1046 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001047 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001048 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001049
Thales Limab8c05952022-05-23 16:58:38 +01001050 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1051 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1052 ResourcesCompat.getFloat(context.getResources(),
1053 R.dimen.hotseat_bar_bottom_space_default));
1054 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1055 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1056 hotseatBarBottomSpace[INDEX_DEFAULT]);
1057 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1058 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1059 hotseatBarBottomSpace[INDEX_DEFAULT]);
1060 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1061 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1062 hotseatBarBottomSpace[INDEX_DEFAULT]);
1063
1064 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1065 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1066 ResourcesCompat.getFloat(context.getResources(),
1067 R.dimen.hotseat_qsb_space_default));
1068 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1069 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1070 hotseatQsbSpace[INDEX_DEFAULT]);
1071 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1072 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1073 hotseatQsbSpace[INDEX_DEFAULT]);
1074 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1075 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1076 hotseatQsbSpace[INDEX_DEFAULT]);
1077
Sunny Goyal415f1732018-11-29 10:33:47 -08001078 a.recycle();
1079 }
1080
1081 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001082 this(null);
1083 }
1084
1085 DisplayOption(GridOption grid) {
1086 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001087 minWidthDps = 0;
1088 minHeightDps = 0;
1089 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001090 for (int i = 0; i < COUNT_SIZES; i++) {
1091 iconSizes[i] = 0;
1092 textSizes[i] = 0;
1093 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001094 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001095 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001096 allAppsIconSizes[i] = 0;
1097 allAppsIconTextSizes[i] = 0;
1098 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001099 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001100 }
1101
1102 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001103 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001104 iconSizes[i] *= w;
1105 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001106 borderSpaces[i].x *= w;
1107 borderSpaces[i].y *= w;
1108 minCellSize[i].x *= w;
1109 minCellSize[i].y *= w;
1110 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001111 hotseatBarBottomSpace[i] *= w;
1112 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001113 allAppsCellSize[i].x *= w;
1114 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001115 allAppsIconSizes[i] *= w;
1116 allAppsIconTextSizes[i] *= w;
1117 allAppsBorderSpaces[i].x *= w;
1118 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001119 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001120
1121 folderBorderSpace *= w;
1122
Sunny Goyal415f1732018-11-29 10:33:47 -08001123 return this;
1124 }
1125
1126 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001127 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001128 iconSizes[i] += p.iconSizes[i];
1129 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001130 borderSpaces[i].x += p.borderSpaces[i].x;
1131 borderSpaces[i].y += p.borderSpaces[i].y;
1132 minCellSize[i].x += p.minCellSize[i].x;
1133 minCellSize[i].y += p.minCellSize[i].y;
1134 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001135 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1136 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001137 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1138 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001139 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1140 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1141 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1142 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001143 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001144
1145 folderBorderSpace += p.folderBorderSpace;
1146
Sunny Goyal415f1732018-11-29 10:33:47 -08001147 return this;
1148 }
1149 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001150}