blob: ef06ff12e3e26acfaf7ad82f292e0c9e5ea54894 [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)) {
201 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
202 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700203 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700204
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700205 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100206 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000207 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
208 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100209 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700210 }
211 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700212 }
213
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700214 /**
215 * This constructor should NOT have any monitors by design.
216 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800217 public InvariantDeviceProfile(Context context, String gridName) {
218 String newName = initGrid(context, gridName);
219 if (newName == null || !newName.equals(gridName)) {
220 throw new IllegalArgumentException("Unknown grid name");
221 }
222 }
223
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700224 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800225 * This constructor should NOT have any monitors by design.
226 */
227 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700228 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100229 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700230 String gridName = getCurrentGridName(context);
231
232 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000233 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
234 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000236 defaultInfo,
237 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
238 /*allowDisabledGrid=*/false),
239 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700240
Alex Chau661f02d2022-06-07 14:03:43 +0100241 Context displayContext = context.createDisplayContext(display);
242 Info myInfo = new Info(displayContext);
Alex Chau1c883d82021-12-01 18:43:10 +0000243 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700244 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000245 myInfo,
246 getPredefinedDeviceProfiles(context, gridName, deviceType,
247 /*allowDisabledGrid=*/false),
248 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700249
250 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
251 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100252 result.iconSizes[INDEX_DEFAULT] =
253 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
254 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700255 result.iconSizes[i] = Math.min(
256 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000257 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700258
Thales Lima83bedbf2021-10-05 17:47:39 +0100259 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
260 COUNT_SIZES);
261 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
262 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500263
Alex Chau1c883d82021-12-01 18:43:10 +0000264 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800265 }
266
Alex Chau238aaee2021-10-06 16:15:24 +0100267 /**
268 * Reinitialize the current grid after a restore, where some grids might now be disabled.
269 */
270 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000271 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100272 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000273 String newGridName = initGrid(context, currentGridName);
274 String newDbFile = dbFile;
275 if (!newDbFile.equals(currentDbFile)) {
276 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100277 + ", migrating to: " + newGridName
278 + ", removing all other grid db files");
279 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
280 if (gridDbFile.equals(currentDbFile)) {
281 continue;
282 }
283 if (context.getDatabasePath(gridDbFile).delete()) {
284 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
285 }
286 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000287 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100288 }
289 }
290
Alex Chau1c883d82021-12-01 18:43:10 +0000291 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800292 int flagPhone = 1 << 0;
293 int flagTablet = 1 << 1;
294
295 int type = displayInfo.supportedBounds.stream()
296 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
297 .reduce(0, (a, b) -> a | b);
298 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
299 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000300 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800301 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000302 return TYPE_TABLET;
303 } else {
304 return TYPE_PHONE;
305 }
306 }
307
Tracy Zhou42255d22020-03-13 00:38:11 -0700308 public static String getCurrentGridName(Context context) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700309 return Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null);
Tracy Zhou42255d22020-03-13 00:38:11 -0700310 }
311
Sunny Goyaleff44f32019-01-09 17:29:49 -0800312 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700313 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000314 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700315
316 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000317 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100318 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700319 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000320 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
321 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700322 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800323 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700324
Thales Limab67bfa72022-11-02 15:30:11 +0000325 @VisibleForTesting
326 public static String getDefaultGridName(Context context) {
327 return new InvariantDeviceProfile().initGrid(context, null);
328 }
329
Alex Chau1c883d82021-12-01 18:43:10 +0000330 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
331 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700332 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700333 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000334 numRows = closestProfile.numRows;
335 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000336 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000337 dbFile = closestProfile.dbFile;
338 defaultLayoutId = closestProfile.defaultLayoutId;
339 demoModeLayoutId = closestProfile.demoModeLayoutId;
Thales Limab35faed2022-09-05 16:30:01 -0300340
Sunny Goyalae190ff2020-04-14 00:19:01 +0000341 numFolderRows = closestProfile.numFolderRows;
342 numFolderColumns = closestProfile.numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300343 folderStyle = closestProfile.folderStyle;
344
Jon Mirandae126d722021-02-25 10:45:20 -0500345 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400346 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000347 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000348
Vinit Nayakc7293172022-07-18 16:41:50 -0700349 inlineNavButtonsEndSpacing = closestProfile.inlineNavButtonsEndSpacing;
350
Sunny Goyalae190ff2020-04-14 00:19:01 +0000351 mExtraAttrs = closestProfile.extraAttrs;
352
Thales Lima83bedbf2021-10-05 17:47:39 +0100353 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100354 float maxIconSize = iconSize[0];
355 for (int i = 1; i < iconSize.length; i++) {
356 maxIconSize = Math.max(maxIconSize, iconSize[i]);
357 }
358 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000359 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
360
Thales Lima83bedbf2021-10-05 17:47:39 +0100361 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700362
Thales Lima83bedbf2021-10-05 17:47:39 +0100363 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100364
Thales Lima83bedbf2021-10-05 17:47:39 +0100365 borderSpaces = displayOption.borderSpaces;
Thales Limaa08a4432022-08-09 09:55:17 +0100366
Thales Limad90faab2021-09-21 17:18:47 +0100367 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100368
Sunny Goyal19ff7282021-04-22 10:12:54 -0700369 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000370 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700371 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima425f6822022-05-10 13:44:58 -0300372 hotseatColumnSpan = closestProfile.hotseatColumnSpan;
Thales Limab8c05952022-05-23 16:58:38 +0100373 hotseatBarBottomSpace = displayOption.hotseatBarBottomSpace;
374 hotseatQsbSpace = displayOption.hotseatQsbSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700375
376 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000377 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700378 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500379
Thales Limab7ef5692022-03-10 10:32:36 +0000380 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000381 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
382 allAppsIconSize = displayOption.allAppsIconSizes;
383 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000384
Thales Lima11af7bc2022-08-12 15:24:54 +0100385 inlineQsb = closestProfile.inlineQsb;
Thales Lima12d0eff2022-03-25 17:06:11 +0000386
Sunny Goyalae190ff2020-04-14 00:19:01 +0000387 // If the partner customization apk contains any grid overrides, apply them
388 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700389 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700390
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000391 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700392 defaultWallpaperSize = new Point(displayInfo.currentSize);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700393 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700394 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000395 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000396 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000397 .setWindowBounds(bounds)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700398 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000399 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700400
Sunny Goyal19ff7282021-04-22 10:12:54 -0700401 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
402 int displayWidth = bounds.bounds.width();
403 int displayHeight = bounds.bounds.height();
404 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700405
Sunny Goyal19ff7282021-04-22 10:12:54 -0700406 // We need to ensure that there is enough extra space in the wallpaper
407 // for the intended parallax effects
408 float parallaxFactor =
Thales Lima425f6822022-05-10 13:44:58 -0300409 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.getDensityDpi())
410 < 720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700411 ? 2
412 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
413 defaultWallpaperSize.x =
414 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700415 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000416 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700417
418 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
419 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700420 }
421
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700422 public void addOnChangeListener(OnIDPChangeListener listener) {
423 mChangeListeners.add(listener);
424 }
425
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800426 public void removeOnChangeListener(OnIDPChangeListener listener) {
427 mChangeListeners.remove(listener);
428 }
429
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800430
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800431 public void setCurrentGrid(Context context, String gridName) {
432 Context appContext = context.getApplicationContext();
433 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700434 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800435 }
436
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700437 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700438 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000439 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
440 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700441 }
442
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700443 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700444 Object[] oldState = toModelState();
445
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700446 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700447 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700448 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700449
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700450 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700451 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700452 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700453 }
454 }
455
Alex Chau1c883d82021-12-01 18:43:10 +0000456 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
457 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800458 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100459
Alex Chau1c883d82021-12-01 18:43:10 +0000460 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700461 final int depth = parser.getDepth();
462 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700463 while (((type = parser.next()) != XmlPullParser.END_TAG ||
464 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800465 if ((type == XmlPullParser.START_TAG)
466 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800467
Alex Chau1c883d82021-12-01 18:43:10 +0000468 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
469 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100470 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100471 final int displayDepth = parser.getDepth();
472 while (((type = parser.next()) != XmlPullParser.END_TAG
473 || parser.getDepth() > displayDepth)
474 && type != XmlPullParser.END_DOCUMENT) {
475 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
476 parser.getName())) {
477 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100478 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100479 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800480 }
481 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700482 }
483 }
sfufa@google.comde013292021-09-21 18:22:44 -0700484 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700485 throw new RuntimeException(e);
486 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800487
Sunny Goyalae190ff2020-04-14 00:19:01 +0000488 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800489 if (!TextUtils.isEmpty(gridName)) {
490 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100491 if (gridName.equals(option.grid.name)
492 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000493 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800494 }
495 }
496 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000497 if (filteredProfiles.isEmpty()) {
498 // No grid found, use the default options
499 for (DisplayOption option : profiles) {
500 if (option.canBeDefault) {
501 filteredProfiles.add(option);
502 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800503 }
504 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000505 if (filteredProfiles.isEmpty()) {
506 throw new RuntimeException("No display option with canBeDefault=true");
507 }
508 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700509 }
510
Thales Lima7ec83822021-08-05 13:32:10 +0100511 /**
512 * @return all the grid options that can be shown on the device
513 */
514 public List<GridOption> parseAllGridOptions(Context context) {
515 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100516
Alex Chau1c883d82021-12-01 18:43:10 +0000517 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100518 final int depth = parser.getDepth();
519 int type;
520 while (((type = parser.next()) != XmlPullParser.END_TAG
521 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
522 if ((type == XmlPullParser.START_TAG)
523 && GridOption.TAG_NAME.equals(parser.getName())) {
524 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000525 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100526 if (option.isEnabled) {
527 result.add(option);
528 }
529 }
530 }
531 } catch (IOException | XmlPullParserException e) {
532 Log.e(TAG, "Error parsing device profile", e);
533 return Collections.emptyList();
534 }
535 return result;
536 }
537
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700538 private int getLauncherIconDensity(int requiredSize) {
539 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700540 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700541 DisplayMetrics.DENSITY_LOW,
542 DisplayMetrics.DENSITY_MEDIUM,
543 DisplayMetrics.DENSITY_TV,
544 DisplayMetrics.DENSITY_HIGH,
545 DisplayMetrics.DENSITY_XHIGH,
546 DisplayMetrics.DENSITY_XXHIGH,
547 DisplayMetrics.DENSITY_XXXHIGH
548 };
549
550 int density = DisplayMetrics.DENSITY_XXXHIGH;
551 for (int i = densityBuckets.length - 1; i >= 0; i--) {
552 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
553 / DisplayMetrics.DENSITY_DEFAULT;
554 if (expectedSize >= requiredSize) {
555 density = densityBuckets[i];
556 }
557 }
558
559 return density;
560 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700561
Adam Cohen2e6da152015-05-06 11:42:25 -0700562 /**
563 * Apply any Partner customization grid overrides.
564 *
565 * Currently we support: all apps row / column count.
566 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700567 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
568 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700569 if (p != null) {
570 p.applyInvariantDeviceProfileOverrides(this, dm);
571 }
572 }
573
Sunny Goyal415f1732018-11-29 10:33:47 -0800574 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700575 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700576 }
577
Sunny Goyal19ff7282021-04-22 10:12:54 -0700578 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000579 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700580 int minWidthPx = Integer.MAX_VALUE;
581 int minHeightPx = Integer.MAX_VALUE;
582 for (WindowBounds bounds : displayInfo.supportedBounds) {
583 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000584 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700585 // For split displays, take half width per page
586 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
587 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700588
Sunny Goyal19ff7282021-04-22 10:12:54 -0700589 } else if (!isTablet && bounds.isLandscape()) {
590 // We will use transposed layout in this case
591 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
592 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
593 } else {
594 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
595 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
596 }
597 }
598
Thales Lima425f6822022-05-10 13:44:58 -0300599 float width = dpiFromPx(minWidthPx, displayInfo.getDensityDpi());
600 float height = dpiFromPx(minHeightPx, displayInfo.getDensityDpi());
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700601
602 // Sort the profiles based on the closeness to the device size
603 Collections.sort(points, (a, b) ->
604 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
605 dist(width, height, b.minWidthDps, b.minHeightDps)));
606
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700607 DisplayOption closestPoint = points.get(0);
608 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700609 float weights = 0;
610
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700611 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
612 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700613 }
614
615 DisplayOption out = new DisplayOption(closestOption);
616 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700617 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700618 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
619 weights += w;
620 out.add(new DisplayOption().add(p).multiply(w));
621 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700622 out.multiply(1.0f / weights);
623
Thales Lima6e0005a2021-10-27 15:53:41 +0100624 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700625 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100626 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
627 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700628 }
629
630 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700631 }
632
Sunny Goyal27835952017-01-13 12:15:53 -0800633 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700634 Resources res = context.getResources();
635 Configuration config = context.getResources().getConfiguration();
636
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400637 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
638 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Alex Chaud3e8cc42022-05-03 17:45:34 +0100639 int rotation = WindowManagerProxy.INSTANCE.get(context).getRotation(context);
640
Alex Chau24265142022-11-14 21:19:44 +0000641 if (Utilities.IS_DEBUG_DEVICE && ENABLE_DEVICE_PROFILE_LOGGING.get()) {
Alex Chau5dfd9392022-11-02 17:39:40 +0000642 StringWriter stringWriter = new StringWriter();
643 PrintWriter printWriter = new PrintWriter(stringWriter);
644 DisplayController.INSTANCE.get(context).dump(printWriter);
645 printWriter.flush();
646 Log.d("b/253338238", "getDeviceProfile -"
647 + "\nconfig: " + config
648 + "\ndisplayMetrics: " + res.getDisplayMetrics()
649 + "\nrotation: " + rotation
650 + "\n" + stringWriter,
651 new Exception());
652 }
Alex Chaud3e8cc42022-05-03 17:45:34 +0100653 return getBestMatch(screenWidth, screenHeight, rotation);
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400654 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700655
Sunny Goyal187b16c2022-03-01 16:53:23 -0800656 /**
657 * Returns the device profile matching the provided screen configuration
658 */
659 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700660 DeviceProfile bestMatch = supportedProfiles.get(0);
661 float minDiff = Float.MAX_VALUE;
662
663 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400664 float diff = Math.abs(profile.widthPx - screenWidth)
665 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700666 if (diff < minDiff) {
667 minDiff = diff;
668 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800669 } else if (diff == minDiff && profile.rotationHint == rotation) {
670 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700671 }
672 }
673 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800674 }
675
Sunny Goyal415f1732018-11-29 10:33:47 -0800676 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700677 float d = dist(x0, y0, x1, y1);
678 if (Float.compare(d, 0f) == 0) {
679 return Float.POSITIVE_INFINITY;
680 }
681 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
682 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700683
684 /**
685 * As a ratio of screen height, the total distance we want the parallax effect to span
686 * horizontally
687 */
688 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
689 float aspectRatio = width / (float) height;
690
691 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
692 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
693 // We will use these two data points to extrapolate how much the wallpaper parallax effect
694 // to span (ie travel) at any aspect ratio:
695
sfufa@google.comde013292021-09-21 18:22:44 -0700696 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
697 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700698 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
699 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
700
701 // To find out the desired width at different aspect ratios, we use the following two
702 // formulas, where the coefficient on x is the aspect ratio (width/height):
703 // (16/10)x + y = 1.5
704 // (10/16)x + y = 1.2
705 // We solve for x and y and end up with a final formula:
706 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700707 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
708 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700709 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
710 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
711 return x * aspectRatio + y;
712 }
713
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700714 public interface OnIDPChangeListener {
715
Sunny Goyalb47172b2021-05-03 19:59:51 -0700716 /**
717 * Called when the device provide changes
718 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700719 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700720 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800721
722
Sunny Goyaleff44f32019-01-09 17:29:49 -0800723 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800724
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800725 public static final String TAG_NAME = "grid-option";
726
Alex Chau1c883d82021-12-01 18:43:10 +0000727 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
728 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
729 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
730 private static final int DEVICE_CATEGORY_ALL =
731 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
732
Thales Lima11af7bc2022-08-12 15:24:54 +0100733 private static final int INLINE_QSB_FOR_PORTRAIT = 1 << 0;
734 private static final int INLINE_QSB_FOR_LANDSCAPE = 1 << 1;
735 private static final int INLINE_QSB_FOR_TWO_PANEL_PORTRAIT = 1 << 2;
736 private static final int INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE = 1 << 3;
737 private static final int DONT_INLINE_QSB = 0;
738
Sunny Goyaleff44f32019-01-09 17:29:49 -0800739 public final String name;
740 public final int numRows;
741 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000742 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100743 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800744
745 private final int numFolderRows;
746 private final int numFolderColumns;
Thales Limab35faed2022-09-05 16:30:01 -0300747 private final @StyleRes int folderStyle;
Sunny Goyal415f1732018-11-29 10:33:47 -0800748
Sunny Goyal19ff7282021-04-22 10:12:54 -0700749 private final int numAllAppsColumns;
750 private final int numDatabaseAllAppsColumns;
751 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700752 private final int numDatabaseHotseatIcons;
Thales Limab8c05952022-05-23 16:58:38 +0100753
Thales Lima425f6822022-05-10 13:44:58 -0300754 private final int[] hotseatColumnSpan = new int[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800755
Thales Lima11af7bc2022-08-12 15:24:54 +0100756 private final boolean[] inlineQsb = new boolean[COUNT_SIZES];
757
Thales Limab35faed2022-09-05 16:30:01 -0300758 private @DimenRes int inlineNavButtonsEndSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800759 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000760
Sunny Goyal415f1732018-11-29 10:33:47 -0800761 private final int defaultLayoutId;
762 private final int demoModeLayoutId;
763
Jon Mirandae126d722021-02-25 10:45:20 -0500764 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400765 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500766
Sunny Goyal5bc18462019-01-07 15:13:39 -0800767 private final SparseArray<TypedValue> extraAttrs;
768
Alex Chau1c883d82021-12-01 18:43:10 +0000769 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800770 TypedArray a = context.obtainStyledAttributes(
771 attrs, R.styleable.GridDisplayOption);
772 name = a.getString(R.styleable.GridDisplayOption_name);
773 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
774 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000775 numSearchContainerColumns = a.getInt(
776 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800777
Tracy Zhou7df93d22020-01-27 13:44:06 -0800778 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000779 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100780 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
781 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
782 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800783 demoModeLayoutId = a.getResourceId(
784 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700785
786 numAllAppsColumns = a.getInt(
787 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
788 numDatabaseAllAppsColumns = a.getInt(
789 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
790
791 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800792 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700793 numDatabaseHotseatIcons = a.getInt(
794 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
Thales Limab8c05952022-05-23 16:58:38 +0100795
Thales Lima425f6822022-05-10 13:44:58 -0300796 hotseatColumnSpan[INDEX_DEFAULT] = a.getInt(
797 R.styleable.GridDisplayOption_hotseatColumnSpan, numColumns);
798 hotseatColumnSpan[INDEX_LANDSCAPE] = a.getInt(
799 R.styleable.GridDisplayOption_hotseatColumnSpanLandscape, numColumns);
800 hotseatColumnSpan[INDEX_TWO_PANEL_LANDSCAPE] = a.getInt(
801 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelLandscape,
802 numColumns);
803 hotseatColumnSpan[INDEX_TWO_PANEL_PORTRAIT] = a.getInt(
804 R.styleable.GridDisplayOption_hotseatColumnSpanTwoPanelPortrait,
805 numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700806
Vinit Nayakc7293172022-07-18 16:41:50 -0700807 inlineNavButtonsEndSpacing =
808 a.getResourceId(R.styleable.GridDisplayOption_inlineNavButtonsEndSpacing,
809 R.dimen.taskbar_button_margin_default);
Thales Limab35faed2022-09-05 16:30:01 -0300810
Sunny Goyal415f1732018-11-29 10:33:47 -0800811 numFolderRows = a.getInt(
812 R.styleable.GridDisplayOption_numFolderRows, numRows);
813 numFolderColumns = a.getInt(
814 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700815
Thales Limab35faed2022-09-05 16:30:01 -0300816 folderStyle = a.getResourceId(R.styleable.GridDisplayOption_folderStyle,
817 INVALID_RESOURCE_HANDLE);
818
Jon Mirandae126d722021-02-25 10:45:20 -0500819 isScalable = a.getBoolean(
820 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400821 devicePaddingId = a.getResourceId(
Thales Limaae0d7ef2022-11-16 15:53:43 +0000822 R.styleable.GridDisplayOption_devicePaddingId, INVALID_RESOURCE_HANDLE);
Jon Mirandae126d722021-02-25 10:45:20 -0500823
Alex Chau1c883d82021-12-01 18:43:10 +0000824 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
825 DEVICE_CATEGORY_ALL);
826 isEnabled = (deviceType == TYPE_PHONE
827 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
828 || (deviceType == TYPE_TABLET
829 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
830 || (deviceType == TYPE_MULTI_DISPLAY
831 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
832 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100833
Thales Lima11af7bc2022-08-12 15:24:54 +0100834 int inlineForRotation = a.getInt(R.styleable.GridDisplayOption_inlineQsb,
835 DONT_INLINE_QSB);
836 inlineQsb[INDEX_DEFAULT] =
837 (inlineForRotation & INLINE_QSB_FOR_PORTRAIT) == INLINE_QSB_FOR_PORTRAIT;
838 inlineQsb[INDEX_LANDSCAPE] =
839 (inlineForRotation & INLINE_QSB_FOR_LANDSCAPE) == INLINE_QSB_FOR_LANDSCAPE;
840 inlineQsb[INDEX_TWO_PANEL_PORTRAIT] =
841 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_PORTRAIT)
842 == INLINE_QSB_FOR_TWO_PANEL_PORTRAIT;
843 inlineQsb[INDEX_TWO_PANEL_LANDSCAPE] =
844 (inlineForRotation & INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE)
845 == INLINE_QSB_FOR_TWO_PANEL_LANDSCAPE;
846
Sunny Goyal415f1732018-11-29 10:33:47 -0800847 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800848 extraAttrs = Themes.createValueMap(context, attrs,
849 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800850 }
851 }
852
Sunny Goyal19ff7282021-04-22 10:12:54 -0700853 @VisibleForTesting
854 static final class DisplayOption {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700855 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800856
Sunny Goyal415f1732018-11-29 10:33:47 -0800857 private final float minWidthDps;
858 private final float minHeightDps;
859 private final boolean canBeDefault;
860
Thales Lima83bedbf2021-10-05 17:47:39 +0100861 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100862
Thales Lima78d00ad2021-09-30 11:29:06 +0100863 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100864 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limab8c05952022-05-23 16:58:38 +0100865 private final float[] hotseatBarBottomSpace = new float[COUNT_SIZES];
866 private final float[] hotseatQsbSpace = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100867
Thales Lima78d00ad2021-09-30 11:29:06 +0100868 private final float[] iconSizes = new float[COUNT_SIZES];
869 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800870
Thales Limab7ef5692022-03-10 10:32:36 +0000871 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000872 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
873 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
874 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
875
Thales Lima1de4d552021-10-13 16:13:25 +0100876 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800877 this.grid = grid;
878
Thales Lima1de4d552021-10-13 16:13:25 +0100879 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800880
Sunny Goyal415f1732018-11-29 10:33:47 -0800881 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
882 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700883
Thales Lima1de4d552021-10-13 16:13:25 +0100884 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800885
Thales Lima83bedbf2021-10-05 17:47:39 +0100886 float x;
887 float y;
888
Thales Lima85c942f2021-12-31 13:04:20 +0000889 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
890 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100891 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000892
893 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
894 minCellSize[INDEX_DEFAULT].x);
895 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
896 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100897 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100898
Thales Lima85c942f2021-12-31 13:04:20 +0000899 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100900 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000901 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100902 minCellSize[INDEX_DEFAULT].y);
903 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
904
Thales Lima85c942f2021-12-31 13:04:20 +0000905 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100906 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000907 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100908 minCellSize[INDEX_DEFAULT].y);
909 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100910
Thales Lima85c942f2021-12-31 13:04:20 +0000911 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000912 float borderSpaceLandscape = a.getFloat(
913 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000914 float borderSpaceTwoPanelPortrait = a.getFloat(
915 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
916 float borderSpaceTwoPanelLandscape = a.getFloat(
917 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100918
Thales Lima85c942f2021-12-31 13:04:20 +0000919 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
920 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100921 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000922
923 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
924 borderSpaceLandscape);
925 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
926 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100927 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
928
929 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000930 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
931 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100932 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000933 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
934 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100935 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
936
937 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000938 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
939 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100940 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000941 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
942 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100943 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
944
Thales Limab7ef5692022-03-10 10:32:36 +0000945 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
946 minCellSize[INDEX_DEFAULT].x);
947 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
948 minCellSize[INDEX_DEFAULT].y);
949 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
950
951 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
952 allAppsCellSize[INDEX_DEFAULT].x);
953 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
954 allAppsCellSize[INDEX_DEFAULT].y);
955 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
956
957 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
958 allAppsCellSize[INDEX_DEFAULT].x);
959 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
960 allAppsCellSize[INDEX_DEFAULT].y);
961 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
962
963 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
964 allAppsCellSize[INDEX_DEFAULT].x);
965 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
966 allAppsCellSize[INDEX_DEFAULT].y);
967 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
968
Thales Lima080d8902022-03-28 15:30:29 +0100969 float allAppsBorderSpace = a.getFloat(
970 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
971 float allAppsBorderSpaceLandscape = a.getFloat(
972 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
973 allAppsBorderSpace);
974 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000975 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100976 allAppsBorderSpace);
977 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000978 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100979 allAppsBorderSpace);
980
981 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
982 allAppsBorderSpace);
983 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
984 allAppsBorderSpace);
985 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
986
987 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
988 allAppsBorderSpaceLandscape);
989 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
990 allAppsBorderSpaceLandscape);
991 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
992
993 x = a.getFloat(
994 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
995 allAppsBorderSpaceTwoPanelPortrait);
996 y = a.getFloat(
997 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
998 allAppsBorderSpaceTwoPanelPortrait);
999 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
1000
1001 x = a.getFloat(
1002 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
1003 allAppsBorderSpaceTwoPanelLandscape);
1004 y = a.getFloat(
1005 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
1006 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +00001007 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -05001008
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001009 iconSizes[INDEX_DEFAULT] =
1010 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
1011 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001012 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001013 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001014 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001015 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001016 iconSizes[INDEX_DEFAULT]);
1017 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001018 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001019 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001020
Thales Limabb7d3882021-12-22 12:56:29 +00001021 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
1022 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
Thales Lima1fb075d2022-08-10 10:47:50 +01001023 allAppsIconSizes[INDEX_LANDSCAPE] = a.getFloat(
1024 R.styleable.ProfileDisplayOption_allAppsIconSizeLandscape,
Thales Limaed8c69b2022-08-17 12:21:21 -04001025 allAppsIconSizes[INDEX_DEFAULT]);
Thales Limabb7d3882021-12-22 12:56:29 +00001026 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1027 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
1028 allAppsIconSizes[INDEX_DEFAULT]);
1029 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1030 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
1031 allAppsIconSizes[INDEX_DEFAULT]);
1032
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001033 textSizes[INDEX_DEFAULT] =
1034 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
1035 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001036 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001037 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001038 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +00001039 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001040 textSizes[INDEX_DEFAULT]);
1041 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +00001042 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001043 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001044
Thales Limabb7d3882021-12-22 12:56:29 +00001045 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
1046 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
1047 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
1048 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1049 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
1050 allAppsIconTextSizes[INDEX_DEFAULT]);
1051 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1052 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
1053 allAppsIconTextSizes[INDEX_DEFAULT]);
1054
Thales Lima83bedbf2021-10-05 17:47:39 +01001055 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
1056 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +00001057 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
1058 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
1059 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +01001060 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001061 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +01001062 horizontalMargin[INDEX_DEFAULT]);
1063 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +00001064 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +01001065 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +01001066
Thales Limab8c05952022-05-23 16:58:38 +01001067 hotseatBarBottomSpace[INDEX_DEFAULT] = a.getFloat(
1068 R.styleable.ProfileDisplayOption_hotseatBarBottomSpace,
1069 ResourcesCompat.getFloat(context.getResources(),
1070 R.dimen.hotseat_bar_bottom_space_default));
1071 hotseatBarBottomSpace[INDEX_LANDSCAPE] = a.getFloat(
1072 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceLandscape,
1073 hotseatBarBottomSpace[INDEX_DEFAULT]);
1074 hotseatBarBottomSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1075 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelLandscape,
1076 hotseatBarBottomSpace[INDEX_DEFAULT]);
1077 hotseatBarBottomSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1078 R.styleable.ProfileDisplayOption_hotseatBarBottomSpaceTwoPanelPortrait,
1079 hotseatBarBottomSpace[INDEX_DEFAULT]);
1080
1081 hotseatQsbSpace[INDEX_DEFAULT] = a.getFloat(
1082 R.styleable.ProfileDisplayOption_hotseatQsbSpace,
1083 ResourcesCompat.getFloat(context.getResources(),
1084 R.dimen.hotseat_qsb_space_default));
1085 hotseatQsbSpace[INDEX_LANDSCAPE] = a.getFloat(
1086 R.styleable.ProfileDisplayOption_hotseatQsbSpaceLandscape,
1087 hotseatQsbSpace[INDEX_DEFAULT]);
1088 hotseatQsbSpace[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1089 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelLandscape,
1090 hotseatQsbSpace[INDEX_DEFAULT]);
1091 hotseatQsbSpace[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1092 R.styleable.ProfileDisplayOption_hotseatQsbSpaceTwoPanelPortrait,
1093 hotseatQsbSpace[INDEX_DEFAULT]);
1094
Sunny Goyal415f1732018-11-29 10:33:47 -08001095 a.recycle();
1096 }
1097
1098 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001099 this(null);
1100 }
1101
1102 DisplayOption(GridOption grid) {
1103 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001104 minWidthDps = 0;
1105 minHeightDps = 0;
1106 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001107 for (int i = 0; i < COUNT_SIZES; i++) {
1108 iconSizes[i] = 0;
1109 textSizes[i] = 0;
1110 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001111 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001112 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001113 allAppsIconSizes[i] = 0;
1114 allAppsIconTextSizes[i] = 0;
1115 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001116 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001117 }
1118
1119 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001120 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001121 iconSizes[i] *= w;
1122 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001123 borderSpaces[i].x *= w;
1124 borderSpaces[i].y *= w;
1125 minCellSize[i].x *= w;
1126 minCellSize[i].y *= w;
1127 horizontalMargin[i] *= w;
Thales Limab8c05952022-05-23 16:58:38 +01001128 hotseatBarBottomSpace[i] *= w;
1129 hotseatQsbSpace[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001130 allAppsCellSize[i].x *= w;
1131 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001132 allAppsIconSizes[i] *= w;
1133 allAppsIconTextSizes[i] *= w;
1134 allAppsBorderSpaces[i].x *= w;
1135 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001136 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001137
Sunny Goyal415f1732018-11-29 10:33:47 -08001138 return this;
1139 }
1140
1141 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001142 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001143 iconSizes[i] += p.iconSizes[i];
1144 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001145 borderSpaces[i].x += p.borderSpaces[i].x;
1146 borderSpaces[i].y += p.borderSpaces[i].y;
1147 minCellSize[i].x += p.minCellSize[i].x;
1148 minCellSize[i].y += p.minCellSize[i].y;
1149 horizontalMargin[i] += p.horizontalMargin[i];
Thales Limab8c05952022-05-23 16:58:38 +01001150 hotseatBarBottomSpace[i] += p.hotseatBarBottomSpace[i];
1151 hotseatQsbSpace[i] += p.hotseatQsbSpace[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001152 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1153 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001154 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1155 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1156 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1157 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001158 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001159
Sunny Goyal415f1732018-11-29 10:33:47 -08001160 return this;
1161 }
1162 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001163}