blob: 9ceef403d6a32beb95581aab97ab33e1d2e7064d [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;
Alex Chau24265142022-11-14 21:19:44 +000020import static com.android.launcher3.config.FeatureFlags.ENABLE_DEVICE_PROFILE_LOGGING;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010021import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Thales Limab35faed2022-09-05 16:30:01 -030022import static com.android.launcher3.testing.shared.ResourceUtils.INVALID_RESOURCE_HANDLE;
Sunny Goyal35c7b192021-04-20 16:51:10 -070023import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Alex Chau6ed408f2022-03-04 12:58:05 +000024import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE;
Sunny Goyal19ff7282021-04-22 10:12:54 -070025import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080026import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070027
Sunny Goyalc6205602015-05-21 20:46:33 -070028import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070029import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070030import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070031import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080032import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080033import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070034import android.content.res.TypedArray;
35import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070036import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010037import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070038import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080039import android.text.TextUtils;
40import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070041import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010042import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080043import android.util.SparseArray;
44import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070045import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080046import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070047
Thales Limab35faed2022-09-05 16:30:01 -030048import androidx.annotation.DimenRes;
Alex Chau1c883d82021-12-01 18:43:10 +000049import androidx.annotation.IntDef;
Thales Limab35faed2022-09-05 16:30:01 -030050import androidx.annotation.StyleRes;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070051import androidx.annotation.VisibleForTesting;
Thales Limaae0d7ef2022-11-16 15:53:43 +000052import androidx.annotation.XmlRes;
Thales Limab8c05952022-05-23 16:58:38 +010053import androidx.core.content.res.ResourcesCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070054
Sunny Goyal65190ae2022-08-02 14:16:26 -070055import com.android.launcher3.icons.DotRenderer;
Sunny Goyal68031ca2021-08-02 12:23:44 -070056import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010057import com.android.launcher3.provider.RestoreDbTask;
vadimtf6ef8792022-07-26 13:54:31 -070058import com.android.launcher3.testing.shared.ResourceUtils;
Sunny Goyalfd58da62020-08-11 12:06:49 -070059import com.android.launcher3.util.DisplayController;
60import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080061import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070062import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080063import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070064import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080065import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070066
Sunny Goyal819e1932016-07-07 16:43:58 -070067import org.xmlpull.v1.XmlPullParser;
68import org.xmlpull.v1.XmlPullParserException;
69
70import java.io.IOException;
Alex Chau5dfd9392022-11-02 17:39:40 +000071import java.io.PrintWriter;
72import java.io.StringWriter;
Alex Chau1c883d82021-12-01 18:43:10 +000073import java.lang.annotation.Retention;
74import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070075import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070076import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080077import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070078import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070079
80public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070081
Hyunyoung Songc55a3502018-12-04 15:43:16 -080082 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070083 // We do not need any synchronization for this variable as its only written on UI thread.
84 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070085 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070086
Alex Chau1c883d82021-12-01 18:43:10 +000087 @Retention(RetentionPolicy.SOURCE)
88 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
Thales Limab8c05952022-05-23 16:58:38 +010089 public @interface DeviceType {}
90
Alex Chau1c883d82021-12-01 18:43:10 +000091 public static final int TYPE_PHONE = 0;
92 public static final int TYPE_MULTI_DISPLAY = 1;
93 public static final int TYPE_TABLET = 2;
94
Hyunyoung Songc55a3502018-12-04 15:43:16 -080095 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080096
Sunny Goyal53d7ee42015-05-22 12:25:45 -070097 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
98
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099 // Constants that affects the interpolation curve between statically defined device profile
100 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800101 private static final float KNEARESTNEIGHBOR = 3;
102 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -0700103
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700104 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800105 private static final float WEIGHT_EFFICIENT = 100000f;
106
Thales Lima83bedbf2021-10-05 17:47:39 +0100107 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
108 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +0000109 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100110 static final int INDEX_DEFAULT = 0;
111 static final int INDEX_LANDSCAPE = 1;
112 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
113 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100114
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700115 /**
116 * Number of icons per row and column in the workspace.
117 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700118 public int numRows;
119 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000120 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700121
122 /**
123 * Number of icons per row and column in the folder.
124 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700125 public int numFolderRows;
126 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100127 public float[] iconSize;
128 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700129 public int iconBitmapSize;
130 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000131 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700132
Thales Lima83bedbf2021-10-05 17:47:39 +0100133 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100134
Thales Lima83bedbf2021-10-05 17:47:39 +0100135 public PointF[] borderSpaces;
Thales Limab35faed2022-09-05 16:30:01 -0300136 public @DimenRes int inlineNavButtonsEndSpacing;
Thales Lima78d00ad2021-09-30 11:29:06 +0100137
Thales Limab35faed2022-09-05 16:30:01 -0300138 public @StyleRes int folderStyle;
Thales Limaa08a4432022-08-09 09:55:17 +0100139
Thales Lima83bedbf2021-10-05 17:47:39 +0100140 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500141
Thales Limab7ef5692022-03-10 10:32:36 +0000142 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000143 public float[] allAppsIconSize;
144 public float[] allAppsIconTextSize;
145 public PointF[] allAppsBorderSpaces;
146
Sunny Goyal5bc18462019-01-07 15:13:39 -0800147 private SparseArray<TypedValue> mExtraAttrs;
148
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700149 /**
150 * Number of icons inside the hotseat area.
151 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800152 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700153
154 /**
155 * Number of icons inside the hotseat area that is stored in the database. This is greater than
156 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
157 * sizes that share the same DB.
158 */
159 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700160
Thales Lima425f6822022-05-10 13:44:58 -0300161 public int[] hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100162 public float[] hotseatBarBottomSpace;
163 public float[] hotseatQsbSpace;
Thales Lima425f6822022-05-10 13:44:58 -0300164
Jon Miranda6f7e9702019-09-16 14:44:14 -0700165 /**
166 * Number of columns in the all apps list.
167 */
168 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700169 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700170
Jon Mirandae126d722021-02-25 10:45:20 -0500171 /**
172 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
173 */
174 protected boolean isScalable;
Thales Limaae0d7ef2022-11-16 15:53:43 +0000175 @XmlRes
176 public int devicePaddingId = INVALID_RESOURCE_HANDLE;
Jon Mirandae126d722021-02-25 10:45:20 -0500177
Tracy Zhou7df93d22020-01-27 13:44:06 -0800178 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800179 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700180 int demoModeLayoutId;
Thales Lima9938c2f2022-07-25 14:38:16 +0100181 public boolean[] inlineQsb = new boolean[COUNT_SIZES];
Adam Cohen2e6da152015-05-06 11:42:25 -0700182
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000183 /**
184 * An immutable list of supported profiles.
185 */
186 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700187
Sunny Goyal6f866092016-03-17 17:04:15 -0700188 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700189 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700190
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700191 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700192
Sunny Goyalf633ef52018-03-13 09:57:05 -0700193 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800194 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700195
Sunny Goyalbbf01842015-10-08 07:41:15 -0700196 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700197 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700198 String gridName = getCurrentGridName(context);
199 String newGridName = initGrid(context, gridName);
200 if (!newGridName.equals(gridName)) {
Stefan Andonian146701c2022-11-10 23:07:40 +0000201 LauncherPrefs.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName)
202 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700203 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700204 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700205
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700206 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100207 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000208 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
209 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100210 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700211 }
212 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700213 }
214
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700215 /**
216 * This constructor should NOT have any monitors by design.
217 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800218 public InvariantDeviceProfile(Context context, String gridName) {
219 String newName = initGrid(context, gridName);
220 if (newName == null || !newName.equals(gridName)) {
221 throw new IllegalArgumentException("Unknown grid name");
222 }
223 }
224
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700225 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800226 * This constructor should NOT have any monitors by design.
227 */
228 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700229 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100230 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700231 String gridName = getCurrentGridName(context);
232
233 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000234 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
235 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700236 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000237 defaultInfo,
238 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
239 /*allowDisabledGrid=*/false),
240 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700241
Alex Chau661f02d2022-06-07 14:03:43 +0100242 Context displayContext = context.createDisplayContext(display);
243 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000244 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700245 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000246 myInfo,
247 getPredefinedDeviceProfiles(context, gridName, deviceType,
248 /*allowDisabledGrid=*/false),
249 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700250
251 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
252 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100253 result.iconSizes[INDEX_DEFAULT] =
254 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
255 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700256 result.iconSizes[i] = Math.min(
257 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000258 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700259
Thales Lima83bedbf2021-10-05 17:47:39 +0100260 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
261 COUNT_SIZES);
262 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
263 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500264
Alex Chau1c883d82021-12-01 18:43:10 +0000265 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800266 }
267
Alex Chau238aaee2021-10-06 16:15:24 +0100268 /**
269 * Reinitialize the current grid after a restore, where some grids might now be disabled.
270 */
271 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000272 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100273 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000274 String newGridName = initGrid(context, currentGridName);
275 String newDbFile = dbFile;
276 if (!newDbFile.equals(currentDbFile)) {
277 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100278 + ", migrating to: " + newGridName
279 + ", removing all other grid db files");
280 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
281 if (gridDbFile.equals(currentDbFile)) {
282 continue;
283 }
284 if (context.getDatabasePath(gridDbFile).delete()) {
285 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
286 }
287 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000288 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100289 }
290 }
291
Alex Chau1c883d82021-12-01 18:43:10 +0000292 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800293 int flagPhone = 1 << 0;
294 int flagTablet = 1 << 1;
295
296 int type = displayInfo.supportedBounds.stream()
297 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
298 .reduce(0, (a, b) -> a | b);
299 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
300 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000301 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800302 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000303 return TYPE_TABLET;
304 } else {
305 return TYPE_PHONE;
306 }
307 }
308
Tracy Zhou42255d22020-03-13 00:38:11 -0700309 public static String getCurrentGridName(Context context) {
Stefan Andonian146701c2022-11-10 23:07:40 +0000310 return LauncherPrefs.getPrefs(context).getString(KEY_IDP_GRID_NAME, null);
Tracy Zhou42255d22020-03-13 00:38:11 -0700311 }
312
Sunny Goyaleff44f32019-01-09 17:29:49 -0800313 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700314 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000315 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700316
317 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000318 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100319 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700320 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000321 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
322 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700323 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800324 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700325
Thales Limab67bfa72022-11-02 15:30:11 +0000326 @VisibleForTesting
327 public static String getDefaultGridName(Context context) {
328 return new InvariantDeviceProfile().initGrid(context, null);
329 }
330
Alex Chau1c883d82021-12-01 18:43:10 +0000331 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
332 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700333 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700334 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000335 numRows = closestProfile.numRows;
336 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000337 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000338 dbFile = closestProfile.dbFile;
339 defaultLayoutId = closestProfile.defaultLayoutId;
340 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300341
Sunny Goyalae190ff2020-04-14 00:19:01 +0000342 numFolderRows = closestProfile.numFolderRows;
343 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300344 folderStyle = closestProfile.folderStyle;
345
Jon Mirandae126d722021-02-25 10:45:20 -0500346 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400347 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000348 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000349
Vinit Nayakc7293172022-07-18 16:41:50 -0700350 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
351
Sunny Goyalae190ff2020-04-14 00:19:01 +0000352 mExtraAttrs = closestProfile.extraAttrs;
353
Thales Lima83bedbf2021-10-05 17:47:39 +0100354 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100355 float maxIconSize = iconSize[0];
356 for (int i = 1; i < iconSize.length; i++) {
357 maxIconSize = Math.max(maxIconSize, iconSize[i]);
358 }
359 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000360 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
361
Thales Lima83bedbf2021-10-05 17:47:39 +0100362 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700363
Thales Lima83bedbf2021-10-05 17:47:39 +0100364 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100365
Thales Lima83bedbf2021-10-05 17:47:39 +0100366 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100367
Thales Limad90faab2021-09-21 17:18:47 +0100368 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100369
Sunny Goyal19ff7282021-04-22 10:12:54 -0700370 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000371 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700372 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300373 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100374 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
375 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700376
377 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000378 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700379 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500380
Thales Limab7ef5692022-03-10 10:32:36 +0000381 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000382 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
383 allAppsIconSize = displayOption.allAppsIconSizes;
384 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000385
Thales Lima11af7bc2022-08-12 15:24:54 +0100386 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000387
Sunny Goyalae190ff2020-04-14 00:19:01 +0000388 // If the partner customization apk contains any grid overrides, apply them
389 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700390 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700391
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000392 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700393 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700394 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700395 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000396 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000397 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000398 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700399 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000400 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700401
Sunny Goyal19ff7282021-04-22 10:12:54 -0700402 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
403 int displayWidth = bounds.bounds.width();
404 int displayHeight = bounds.bounds.height();
405 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700406
Sunny Goyal19ff7282021-04-22 10:12:54 -0700407 // We need to ensure that there is enough extra space in the wallpaper
408 // for the intended parallax effects
409 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300410 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
411 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700412 ? 2
413 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
414 defaultWallpaperSize.x =
415 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700416 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000417 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700418
419 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
420 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700421 }
422
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700423 public void addOnChangeListener(OnIDPChangeListener listener) {
424 mChangeListeners.add(listener);
425 }
426
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800427 public void removeOnChangeListener(OnIDPChangeListener listener) {
428 mChangeListeners.remove(listener);
429 }
430
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800431
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800432 public void setCurrentGrid(Context context, String gridName) {
433 Context appContext = context.getApplicationContext();
Stefan Andonian146701c2022-11-10 23:07:40 +0000434 LauncherPrefs.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700435 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800436 }
437
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700438 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700439 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000440 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
441 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700442 }
443
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700444 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700445 Object[] oldState = toModelState();
446
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700447 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700448 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700449 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700450
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700451 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700452 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700453 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700454 }
455 }
456
Alex Chau1c883d82021-12-01 18:43:10 +0000457 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
458 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800459 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100460
Alex Chau1c883d82021-12-01 18:43:10 +0000461 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700462 final int depth = parser.getDepth();
463 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700464 while (((type = parser.next()) != XmlPullParser.END_TAG ||
465 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800466 if ((type == XmlPullParser.START_TAG)
467 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800468
Alex Chau1c883d82021-12-01 18:43:10 +0000469 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
470 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100471 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100472 final int displayDepth = parser.getDepth();
473 while (((type = parser.next()) != XmlPullParser.END_TAG
474 || parser.getDepth() > displayDepth)
475 && type != XmlPullParser.END_DOCUMENT) {
476 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
477 parser.getName())) {
478 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100479 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100480 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800481 }
482 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700483 }
484 }
sfufa@google.comde013292021-09-21 18:22:44 -0700485 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700486 throw new RuntimeException(e);
487 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800488
Sunny Goyalae190ff2020-04-14 00:19:01 +0000489 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800490 if (!TextUtils.isEmpty(gridName)) {
491 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100492 if (gridName.equals(option.grid.name)
493 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000494 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800495 }
496 }
497 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000498 if (filteredProfiles.isEmpty()) {
499 // No grid found, use the default options
500 for (DisplayOption option : profiles) {
501 if (option.canBeDefault) {
502 filteredProfiles.add(option);
503 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800504 }
505 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000506 if (filteredProfiles.isEmpty()) {
507 throw new RuntimeException("No display option with canBeDefault=true");
508 }
509 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700510 }
511
Thales Lima7ec83822021-08-05 13:32:10 +0100512 /**
513 * @return all the grid options that can be shown on the device
514 */
515 public List<GridOption> parseAllGridOptions(Context context) {
516 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100517
Alex Chau1c883d82021-12-01 18:43:10 +0000518 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100519 final int depth = parser.getDepth();
520 int type;
521 while (((type = parser.next()) != XmlPullParser.END_TAG
522 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
523 if ((type == XmlPullParser.START_TAG)
524 && GridOption.TAG_NAME.equals(parser.getName())) {
525 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000526 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100527 if (option.isEnabled) {
528 result.add(option);
529 }
530 }
531 }
532 } catch (IOException | XmlPullParserException e) {
533 Log.e(TAG, "Error parsing device profile", e);
534 return Collections.emptyList();
535 }
536 return result;
537 }
538
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700539 private int getLauncherIconDensity(int requiredSize) {
540 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700541 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700542 DisplayMetrics.DENSITY_LOW,
543 DisplayMetrics.DENSITY_MEDIUM,
544 DisplayMetrics.DENSITY_TV,
545 DisplayMetrics.DENSITY_HIGH,
546 DisplayMetrics.DENSITY_XHIGH,
547 DisplayMetrics.DENSITY_XXHIGH,
548 DisplayMetrics.DENSITY_XXXHIGH
549 };
550
551 int density = DisplayMetrics.DENSITY_XXXHIGH;
552 for (int i = densityBuckets.length - 1; i >= 0; i--) {
553 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
554 / DisplayMetrics.DENSITY_DEFAULT;
555 if (expectedSize >= requiredSize) {
556 density = densityBuckets[i];
557 }
558 }
559
560 return density;
561 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700562
Adam Cohen2e6da152015-05-06 11:42:25 -0700563 /**
564 * Apply any Partner customization grid overrides.
565 *
566 * Currently we support: all apps row / column count.
567 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700568 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
569 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700570 if (p != null) {
571 p.applyInvariantDeviceProfileOverrides(this, dm);
572 }
573 }
574
Sunny Goyal415f1732018-11-29 10:33:47 -0800575 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700576 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700577 }
578
Sunny Goyal19ff7282021-04-22 10:12:54 -0700579 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000580 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700581 int minWidthPx = Integer.MAX_VALUE;
582 int minHeightPx = Integer.MAX_VALUE;
583 for (WindowBounds bounds : displayInfo.supportedBounds) {
584 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000585 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700586 // For split displays, take half width per page
587 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
588 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700589
Sunny Goyal19ff7282021-04-22 10:12:54 -0700590 } else if (!isTablet && bounds.isLandscape()) {
591 // We will use transposed layout in this case
592 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
593 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
594 } else {
595 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
596 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
597 }
598 }
599
Thales Lima425f6822022-05-10 13:44:58 -0300600 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
601 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700602
603 // Sort the profiles based on the closeness to the device size
604 Collections.sort(points, (a, b) ->
605 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
606 dist(width, height, b.minWidthDps, b.minHeightDps)));
607
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700608 DisplayOption closestPoint = points.get(0);
609 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700610 float weights = 0;
611
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700612 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
613 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700614 }
615
616 DisplayOption out = new DisplayOption(closestOption);
617 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700618 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700619 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
620 weights += w;
621 out.add(new DisplayOption().add(p).multiply(w));
622 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700623 out.multiply(1.0f / weights);
624
Thales Lima6e0005a2021-10-27 15:53:41 +0100625 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700626 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100627 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
628 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700629 }
630
631 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700632 }
633
Sunny Goyal27835952017-01-13 12:15:53 -0800634 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700635 Resources res = context.getResources();
636 Configuration config = context.getResources().getConfiguration();
637
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400638 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
639 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100640 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
641
Alex Chau24265142022-11-14 21:19:44 +0000642 if (Utilities.IS_DEBUG_DEVICE && ENABLE_DEVICE_PROFILE_LOGGING.get()) {
Alex Chau5dfd9392022-11-02 17:39:40 +0000643 StringWriter stringWriter = new StringWriter();
644 PrintWriter printWriter = new PrintWriter(stringWriter);
645 DisplayController.INSTANCE.get(context).dump(printWriter);
646 printWriter.flush();
647 Log.d("b/253338238", "getDeviceProfile -"
648 + "\nconfig: " + config
649 + "\ndisplayMetrics: " + res.getDisplayMetrics()
650 + "\nrotation: " + rotation
651 + "\n" + stringWriter,
652 new Exception());
653 }
Alex Chaud3e8cc42022-05-03 17:45:34 +0100654 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400655 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700656
Sunny Goyal187b16c2022-03-01 16:53:23 -0800657 /**
658 * Returns the device profile matching the provided screen configuration
659 */
660 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700661 DeviceProfile bestMatch = supportedProfiles.get(0);
662 float minDiff = Float.MAX_VALUE;
663
664 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400665 float diff = Math.abs(profile.widthPx - screenWidth)
666 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700667 if (diff < minDiff) {
668 minDiff = diff;
669 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800670 } else if (diff == minDiff && profile.rotationHint == rotation) {
671 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700672 }
673 }
674 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800675 }
676
Sunny Goyal415f1732018-11-29 10:33:47 -0800677 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700678 float d = dist(x0, y0, x1, y1);
679 if (Float.compare(d, 0f) == 0) {
680 return Float.POSITIVE_INFINITY;
681 }
682 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
683 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700684
685 /**
686 * As a ratio of screen height, the total distance we want the parallax effect to span
687 * horizontally
688 */
689 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
690 float aspectRatio = width / (float) height;
691
692 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
693 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
694 // We will use these two data points to extrapolate how much the wallpaper parallax effect
695 // to span (ie travel) at any aspect ratio:
696
sfufa@google.comde013292021-09-21 18:22:44 -0700697 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
698 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700699 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
700 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
701
702 // To find out the desired width at different aspect ratios, we use the following two
703 // formulas, where the coefficient on x is the aspect ratio (width/height):
704 // (16/10)x + y = 1.5
705 // (10/16)x + y = 1.2
706 // We solve for x and y and end up with a final formula:
707 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700708 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
709 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700710 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
711 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
712 return x * aspectRatio + y;
713 }
714
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700715 public interface OnIDPChangeListener {
716
Sunny Goyalb47172b2021-05-03 19:59:51 -0700717 /**
718 * Called when the device provide changes
719 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700720 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700721 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800722
723
Sunny Goyaleff44f32019-01-09 17:29:49 -0800724 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800725
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800726 public static final String TAG_NAME = "grid-option";
727
Alex Chau1c883d82021-12-01 18:43:10 +0000728 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
729 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
730 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
731 private static final int DEVICE_CATEGORY_ALL =
732 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
733
Thales Lima11af7bc2022-08-12 15:24:54 +0100734 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
735 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
736 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
737 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
738 private static final int DONT_INLINE_QSB = 0;
739
Sunny Goyaleff44f32019-01-09 17:29:49 -0800740 public final String name;
741 public final int numRows;
742 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000743 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100744 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800745
746 private final int numFolderRows;
747 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300748 private final @StyleRes int folderStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800749
Sunny Goyal19ff7282021-04-22 10:12:54 -0700750 private final int numAllAppsColumns;
751 private final int numDatabaseAllAppsColumns;
752 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700753 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100754
Thales Lima425f6822022-05-10 13:44:58 -0300755 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800756
Thales Lima11af7bc2022-08-12 15:24:54 +0100757 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
758
Thales Limab35faed2022-09-05 16:30:01 -0300759 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800760 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000761
Sunny Goyal415f1732018-11-29 10:33:47 -0800762 private final int defaultLayoutId;
763 private final int demoModeLayoutId;
764
Jon Mirandae126d722021-02-25 10:45:20 -0500765 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400766 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500767
Sunny Goyal5bc18462019-01-07 15:13:39 -0800768 private final SparseArray<TypedValue> extraAttrs;
769
Alex Chau1c883d82021-12-01 18:43:10 +0000770 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800771 TypedArray a = context.obtainStyledAttributes(
772 attrs, R.styleable.GridDisplayOption);
773 name = a.getString(R.styleable.GridDisplayOption_name);
774 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
775 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000776 numSearchContainerColumns = a.getInt(
777 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800778
Tracy Zhou7df93d22020-01-27 13:44:06 -0800779 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau766cd2b2022-07-06 17:15:21 +0100780 defaultLayoutId = a.getResourceId(
781 R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800782 demoModeLayoutId = a.getResourceId(
783 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700784
785 numAllAppsColumns = a.getInt(
786 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
787 numDatabaseAllAppsColumns = a.getInt(
788 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
789
790 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800791 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700792 numDatabaseHotseatIcons = a.getInt(
793 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100794
Thales Lima425f6822022-05-10 13:44:58 -0300795 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
796 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
797 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
798 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
799 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
800 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
801 numColumns);
802 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
803 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
804 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700805
Vinit Nayakc7293172022-07-18 16:41:50 -0700806 inlineNavButtonsEndSpacing =
807 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
808 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300809
Sunny Goyal415f1732018-11-29 10:33:47 -0800810 numFolderRows = a.getInt(
811 R.styleable.GridDisplayOption_numFolderRows, numRows);
812 numFolderColumns = a.getInt(
813 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700814
Thales Limab35faed2022-09-05 16:30:01 -0300815 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
816 INVALID_RESOURCE_HANDLE);
817
Jon Mirandae126d722021-02-25 10:45:20 -0500818 isScalable = a.getBoolean(
819 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400820 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000821 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Jon Mirandae126d722021-02-25 10:45:20 -0500822
Alex Chau1c883d82021-12-01 18:43:10 +0000823 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
824 DEVICE_CATEGORY_ALL);
825 isEnabled = (deviceType == TYPE_PHONE
826 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
827 || (deviceType == TYPE_TABLET
828 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
829 || (deviceType == TYPE_MULTI_DISPLAY
830 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
831 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100832
Thales Lima11af7bc2022-08-12 15:24:54 +0100833 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
834 DONT_INLINE_QSB);
835 inlineQsb[INDEX_DEFAULT] =
836 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
837 inlineQsb[INDEX_LANDSCAPE] =
838 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
839 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
840 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
841 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
842 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
843 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
844 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
845
Sunny Goyal415f1732018-11-29 10:33:47 -0800846 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800847 extraAttrs = Themes.createValueMap(context, attrs,
848 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800849 }
850 }
851
Sunny Goyal19ff7282021-04-22 10:12:54 -0700852 @VisibleForTesting
853 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700854 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800855
Sunny Goyal415f1732018-11-29 10:33:47 -0800856 private final float minWidthDps;
857 private final float minHeightDps;
858 private final boolean canBeDefault;
859
Thales Lima83bedbf2021-10-05 17:47:39 +0100860 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100861
Thales Lima78d00ad2021-09-30 11:29:06 +0100862 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100863 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100864 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
865 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100866
Thales Lima78d00ad2021-09-30 11:29:06 +0100867 private final float[] iconSizes = new float[COUNT_SIZES];
868 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800869
Thales Limab7ef5692022-03-10 10:32:36 +0000870 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000871 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
872 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
873 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
874
Thales Lima1de4d552021-10-13 16:13:25 +0100875 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800876 this.grid = grid;
877
Thales Lima1de4d552021-10-13 16:13:25 +0100878 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800879
Sunny Goyal415f1732018-11-29 10:33:47 -0800880 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
881 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700882
Thales Lima1de4d552021-10-13 16:13:25 +0100883 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800884
Thales Lima83bedbf2021-10-05 17:47:39 +0100885 float x;
886 float y;
887
Thales Lima85c942f2021-12-31 13:04:20 +0000888 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
889 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100890 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000891
892 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
893 minCellSize[INDEX_DEFAULT].x);
894 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
895 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100896 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100897
Thales Lima85c942f2021-12-31 13:04:20 +0000898 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100899 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000900 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100901 minCellSize[INDEX_DEFAULT].y);
902 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
903
Thales Lima85c942f2021-12-31 13:04:20 +0000904 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100905 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000906 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100907 minCellSize[INDEX_DEFAULT].y);
908 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100909
Thales Lima85c942f2021-12-31 13:04:20 +0000910 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000911 float borderSpaceLandscape = a.getFloat(
912 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000913 float borderSpaceTwoPanelPortrait = a.getFloat(
914 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
915 float borderSpaceTwoPanelLandscape = a.getFloat(
916 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100917
Thales Lima85c942f2021-12-31 13:04:20 +0000918 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
919 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100920 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000921
922 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
923 borderSpaceLandscape);
924 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
925 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100926 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
927
928 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000929 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
930 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100931 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000932 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
933 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100934 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
935
936 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000937 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
938 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100939 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000940 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
941 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100942 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
943
Thales Limab7ef5692022-03-10 10:32:36 +0000944 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
945 minCellSize[INDEX_DEFAULT].x);
946 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
947 minCellSize[INDEX_DEFAULT].y);
948 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
949
950 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
951 allAppsCellSize[INDEX_DEFAULT].x);
952 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
953 allAppsCellSize[INDEX_DEFAULT].y);
954 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
955
956 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
957 allAppsCellSize[INDEX_DEFAULT].x);
958 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
959 allAppsCellSize[INDEX_DEFAULT].y);
960 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
961
962 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
963 allAppsCellSize[INDEX_DEFAULT].x);
964 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
965 allAppsCellSize[INDEX_DEFAULT].y);
966 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
967
Thales Lima080d8902022-03-28 15:30:29 +0100968 float allAppsBorderSpace = a.getFloat(
969 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
970 float allAppsBorderSpaceLandscape = a.getFloat(
971 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
972 allAppsBorderSpace);
973 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000974 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100975 allAppsBorderSpace);
976 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000977 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100978 allAppsBorderSpace);
979
980 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
981 allAppsBorderSpace);
982 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
983 allAppsBorderSpace);
984 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
985
986 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
987 allAppsBorderSpaceLandscape);
988 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
989 allAppsBorderSpaceLandscape);
990 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
991
992 x = a.getFloat(
993 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
994 allAppsBorderSpaceTwoPanelPortrait);
995 y = a.getFloat(
996 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
997 allAppsBorderSpaceTwoPanelPortrait);
998 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
999
1000 x = a.getFloat(
1001 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1002 allAppsBorderSpaceTwoPanelLandscape);
1003 y = a.getFloat(
1004 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1005 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001006 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001007
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001008 iconSizes[INDEX_DEFAULT] =
1009 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1010 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001011 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001012 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001013 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001014 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001015 iconSizes[INDEX_DEFAULT]);
1016 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001017 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001018 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001019
Thales Limabb7d3882021-12-22 12:56:29 +00001020 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1021 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001022 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1023 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001024 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001025 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1026 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1027 allAppsIconSizes[INDEX_DEFAULT]);
1028 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1029 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1030 allAppsIconSizes[INDEX_DEFAULT]);
1031
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001032 textSizes[INDEX_DEFAULT] =
1033 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1034 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001035 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001036 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001037 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001038 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001039 textSizes[INDEX_DEFAULT]);
1040 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001041 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001042 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001043
Thales Limabb7d3882021-12-22 12:56:29 +00001044 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1045 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1046 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1047 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1048 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1049 allAppsIconTextSizes[INDEX_DEFAULT]);
1050 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1051 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1052 allAppsIconTextSizes[INDEX_DEFAULT]);
1053
Thales Lima83bedbf2021-10-05 17:47:39 +01001054 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1055 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001056 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1057 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1058 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001059 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001060 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001061 horizontalMargin[INDEX_DEFAULT]);
1062 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001063 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001064 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001065
Thales Limab8c05952022-05-23 16:58:38 +01001066 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1067 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1068 ResourcesCompat.getFloat(context.getResources(),
1069 R.dimen.hotseat_bar_bottom_space_default));
1070 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1071 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1072 hotseatBarBottomSpace[INDEX_DEFAULT]);
1073 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1074 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1075 hotseatBarBottomSpace[INDEX_DEFAULT]);
1076 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1077 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1078 hotseatBarBottomSpace[INDEX_DEFAULT]);
1079
1080 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1081 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1082 ResourcesCompat.getFloat(context.getResources(),
1083 R.dimen.hotseat_qsb_space_default));
1084 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1085 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1086 hotseatQsbSpace[INDEX_DEFAULT]);
1087 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1088 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1089 hotseatQsbSpace[INDEX_DEFAULT]);
1090 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1091 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1092 hotseatQsbSpace[INDEX_DEFAULT]);
1093
Sunny Goyal415f1732018-11-29 10:33:47 -08001094 a.recycle();
1095 }
1096
1097 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001098 this(null);
1099 }
1100
1101 DisplayOption(GridOption grid) {
1102 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001103 minWidthDps = 0;
1104 minHeightDps = 0;
1105 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001106 for (int i = 0; i < COUNT_SIZES; i++) {
1107 iconSizes[i] = 0;
1108 textSizes[i] = 0;
1109 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001110 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001111 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001112 allAppsIconSizes[i] = 0;
1113 allAppsIconTextSizes[i] = 0;
1114 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001115 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001116 }
1117
1118 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001119 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001120 iconSizes[i] *= w;
1121 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001122 borderSpaces[i].x *= w;
1123 borderSpaces[i].y *= w;
1124 minCellSize[i].x *= w;
1125 minCellSize[i].y *= w;
1126 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001127 hotseatBarBottomSpace[i] *= w;
1128 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001129 allAppsCellSize[i].x *= w;
1130 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001131 allAppsIconSizes[i] *= w;
1132 allAppsIconTextSizes[i] *= w;
1133 allAppsBorderSpaces[i].x *= w;
1134 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001135 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001136
Sunny Goyal415f1732018-11-29 10:33:47 -08001137 return this;
1138 }
1139
1140 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001141 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001142 iconSizes[i] += p.iconSizes[i];
1143 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001144 borderSpaces[i].x += p.borderSpaces[i].x;
1145 borderSpaces[i].y += p.borderSpaces[i].y;
1146 minCellSize[i].x += p.minCellSize[i].x;
1147 minCellSize[i].y += p.minCellSize[i].y;
1148 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001149 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1150 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001151 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1152 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001153 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1154 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1155 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1156 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001157 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001158
Sunny Goyal415f1732018-11-29 10:33:47 -08001159 return this;
1160 }
1161 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001162}