blob: 2d0d6407725d9a73d87aa3e577decf77e412b328 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
Thales Lima1de4d552021-10-13 16:13:25 +01002 * Copyright (C) 2021 The Android Open Source Project
Adam Cohen2e6da152015-05-06 11:42:25 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Sunny Goyal19ff7282021-04-22 10:12:54 -070019import static com.android.launcher3.Utilities.dpiFromPx;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010020import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Sunny Goyal35c7b192021-04-20 16:51:10 -070021import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Sunny Goyal19ff7282021-04-22 10:12:54 -070022import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080023import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070024
Sunny Goyalc6205602015-05-21 20:46:33 -070025import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070026import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080029import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080030import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070031import android.content.res.TypedArray;
32import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070033import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010034import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070035import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080036import android.text.TextUtils;
37import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070038import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010039import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080040import android.util.SparseArray;
41import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070042import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080043import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070044
Alex Chau1c883d82021-12-01 18:43:10 +000045import androidx.annotation.IntDef;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070046import androidx.annotation.Nullable;
47import androidx.annotation.VisibleForTesting;
48
Sunny Goyal68031ca2021-08-02 12:23:44 -070049import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010050import com.android.launcher3.provider.RestoreDbTask;
Sunny Goyalfd58da62020-08-11 12:06:49 -070051import com.android.launcher3.util.DisplayController;
52import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080053import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070054import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080055import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070056import com.android.launcher3.util.WindowBounds;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070057
Sunny Goyal819e1932016-07-07 16:43:58 -070058import org.xmlpull.v1.XmlPullParser;
59import org.xmlpull.v1.XmlPullParserException;
60
61import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000062import java.lang.annotation.Retention;
63import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070064import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070065import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080066import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070067import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070068
69public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070070
Hyunyoung Songc55a3502018-12-04 15:43:16 -080071 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070072 // We do not need any synchronization for this variable as its only written on UI thread.
73 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070074 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070075
Alex Chau1c883d82021-12-01 18:43:10 +000076 @Retention(RetentionPolicy.SOURCE)
77 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
78 public @interface DeviceType{}
79 public static final int TYPE_PHONE = 0;
80 public static final int TYPE_MULTI_DISPLAY = 1;
81 public static final int TYPE_TABLET = 2;
82
Hyunyoung Songc55a3502018-12-04 15:43:16 -080083 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080084
Sunny Goyal53d7ee42015-05-22 12:25:45 -070085 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
86
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070087 // Constants that affects the interpolation curve between statically defined device profile
88 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080089 private static final float KNEARESTNEIGHBOR = 3;
90 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070091
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070092 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080093 private static final float WEIGHT_EFFICIENT = 100000f;
94
Thales Lima83bedbf2021-10-05 17:47:39 +010095 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
96 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +000097 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +010098 static final int INDEX_DEFAULT = 0;
99 static final int INDEX_LANDSCAPE = 1;
100 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
101 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100102
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700103 /**
104 * Number of icons per row and column in the workspace.
105 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700106 public int numRows;
107 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700108
109 /**
110 * Number of icons per row and column in the folder.
111 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700112 public int numFolderRows;
113 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100114 public float[] iconSize;
115 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700116 public int iconBitmapSize;
117 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000118 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700119
Thales Lima83bedbf2021-10-05 17:47:39 +0100120 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100121
Thales Lima83bedbf2021-10-05 17:47:39 +0100122 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100123 public float folderBorderSpace;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000124 public float[] hotseatBorderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100125
Thales Lima83bedbf2021-10-05 17:47:39 +0100126 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500127
Thales Limabb7d3882021-12-22 12:56:29 +0000128 public float[] allAppsIconSize;
129 public float[] allAppsIconTextSize;
130 public PointF[] allAppsBorderSpaces;
131
Sunny Goyal5bc18462019-01-07 15:13:39 -0800132 private SparseArray<TypedValue> mExtraAttrs;
133
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700134 /**
135 * Number of icons inside the hotseat area.
136 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700137 protected int numShownHotseatIcons;
138
139 /**
140 * Number of icons inside the hotseat area that is stored in the database. This is greater than
141 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
142 * sizes that share the same DB.
143 */
144 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700145
Jon Miranda6f7e9702019-09-16 14:44:14 -0700146 /**
147 * Number of columns in the all apps list.
148 */
149 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700150 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700151
Jon Mirandae126d722021-02-25 10:45:20 -0500152 /**
153 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
154 */
155 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400156 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500157
Tracy Zhou7df93d22020-01-27 13:44:06 -0800158 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800159 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700160 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700161
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000162 /**
163 * An immutable list of supported profiles.
164 */
165 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700166
sfufa@google.comde013292021-09-21 18:22:44 -0700167 @Nullable
168 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500169
Sunny Goyal6f866092016-03-17 17:04:15 -0700170 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700171 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700172
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700173 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700174
Sunny Goyalf633ef52018-03-13 09:57:05 -0700175 @VisibleForTesting
sfufa@google.comde013292021-09-21 18:22:44 -0700176 public InvariantDeviceProfile() {
177 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700178
Sunny Goyalbbf01842015-10-08 07:41:15 -0700179 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700180 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700181 String gridName = getCurrentGridName(context);
182 String newGridName = initGrid(context, gridName);
183 if (!newGridName.equals(gridName)) {
184 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
185 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700186 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700187
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700188 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100189 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700190 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100191 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700192 }
193 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700194 }
195
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700196 /**
197 * This constructor should NOT have any monitors by design.
198 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800199 public InvariantDeviceProfile(Context context, String gridName) {
200 String newName = initGrid(context, gridName);
201 if (newName == null || !newName.equals(gridName)) {
202 throw new IllegalArgumentException("Unknown grid name");
203 }
204 }
205
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700206 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800207 * This constructor should NOT have any monitors by design.
208 */
209 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700210 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100211 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700212 String gridName = getCurrentGridName(context);
213
214 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000215 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
216 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700217 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000218 defaultInfo,
219 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
220 /*allowDisabledGrid=*/false),
221 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700222
Alex Chau417bd722021-01-26 15:22:18 +0000223 Info myInfo = new Info(context, display);
Alex Chau1c883d82021-12-01 18:43:10 +0000224 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700225 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000226 myInfo,
227 getPredefinedDeviceProfiles(context, gridName, deviceType,
228 /*allowDisabledGrid=*/false),
229 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700230
231 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
232 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100233 result.iconSizes[INDEX_DEFAULT] =
234 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
235 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700236 result.iconSizes[i] = Math.min(
237 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000238 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700239
Thales Lima83bedbf2021-10-05 17:47:39 +0100240 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
241 COUNT_SIZES);
242 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
243 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500244
Alex Chau1c883d82021-12-01 18:43:10 +0000245 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800246 }
247
Alex Chau238aaee2021-10-06 16:15:24 +0100248 /**
249 * Reinitialize the current grid after a restore, where some grids might now be disabled.
250 */
251 public void reinitializeAfterRestore(Context context) {
252 String currentDbFile = dbFile;
253 String gridName = getCurrentGridName(context);
254 String newGridName = initGrid(context, gridName);
255 if (!newGridName.equals(gridName)) {
256 Log.d(TAG, "Restored grid is disabled : " + gridName
257 + ", migrating to: " + newGridName
258 + ", removing all other grid db files");
259 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
260 if (gridDbFile.equals(currentDbFile)) {
261 continue;
262 }
263 if (context.getDatabasePath(gridDbFile).delete()) {
264 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
265 }
266 }
267 setCurrentGrid(context, gridName);
268 }
269 }
270
Alex Chau1c883d82021-12-01 18:43:10 +0000271 private static @DeviceType int getDeviceType(Info displayInfo) {
272 // Each screen has two profiles (portrait/landscape), so devices with four or more
273 // supported profiles implies two or more internal displays.
274 if (displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get()) {
275 return TYPE_MULTI_DISPLAY;
276 } else if (displayInfo.supportedBounds.stream().allMatch(displayInfo::isTablet)) {
277 return TYPE_TABLET;
278 } else {
279 return TYPE_PHONE;
280 }
281 }
282
Tracy Zhou42255d22020-03-13 00:38:11 -0700283 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700284 return Utilities.isGridOptionsEnabled(context)
285 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700286 }
287
Sunny Goyaleff44f32019-01-09 17:29:49 -0800288 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700289 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000290 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700291
292 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000293 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100294 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700295 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000296 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
297 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700298 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800299 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700300
Alex Chau1c883d82021-12-01 18:43:10 +0000301 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
302 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700303 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700304 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000305 numRows = closestProfile.numRows;
306 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000307 dbFile = closestProfile.dbFile;
308 defaultLayoutId = closestProfile.defaultLayoutId;
309 demoModeLayoutId = closestProfile.demoModeLayoutId;
310 numFolderRows = closestProfile.numFolderRows;
311 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500312 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400313 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000314 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000315
316 mExtraAttrs = closestProfile.extraAttrs;
317
Thales Lima83bedbf2021-10-05 17:47:39 +0100318 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100319 float maxIconSize = iconSize[0];
320 for (int i = 1; i < iconSize.length; i++) {
321 maxIconSize = Math.max(maxIconSize, iconSize[i]);
322 }
323 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000324 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
325
Thales Lima83bedbf2021-10-05 17:47:39 +0100326 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700327
Thales Lima83bedbf2021-10-05 17:47:39 +0100328 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100329
Thales Lima83bedbf2021-10-05 17:47:39 +0100330 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100331 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700332
Thales Limad90faab2021-09-21 17:18:47 +0100333 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100334
Sunny Goyal19ff7282021-04-22 10:12:54 -0700335 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000336 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700337 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000338 hotseatBorderSpaces = displayOption.hotseatBorderSpaces;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700339
340 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000341 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700342 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500343
Thales Limabb7d3882021-12-22 12:56:29 +0000344 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
345 allAppsIconSize = displayOption.allAppsIconSizes;
346 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100347 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000348 allAppsIconSize = iconSize;
349 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000350 }
351
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400352 if (devicePaddingId != 0) {
353 devicePaddings = new DevicePaddings(context, devicePaddingId);
354 }
355
Sunny Goyalae190ff2020-04-14 00:19:01 +0000356 // If the partner customization apk contains any grid overrides, apply them
357 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700358 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700359
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000360 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700361 defaultWallpaperSize = new Point(displayInfo.currentSize);
362 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000363 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000364 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700365 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700366
Sunny Goyal19ff7282021-04-22 10:12:54 -0700367 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
368 int displayWidth = bounds.bounds.width();
369 int displayHeight = bounds.bounds.height();
370 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700371
Sunny Goyal19ff7282021-04-22 10:12:54 -0700372 // We need to ensure that there is enough extra space in the wallpaper
373 // for the intended parallax effects
374 float parallaxFactor =
375 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
376 ? 2
377 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
378 defaultWallpaperSize.x =
379 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700380 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000381 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700382
383 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
384 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700385 }
386
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700387 public void addOnChangeListener(OnIDPChangeListener listener) {
388 mChangeListeners.add(listener);
389 }
390
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800391 public void removeOnChangeListener(OnIDPChangeListener listener) {
392 mChangeListeners.remove(listener);
393 }
394
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800395
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800396 public void setCurrentGrid(Context context, String gridName) {
397 Context appContext = context.getApplicationContext();
398 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700399 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800400 }
401
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700402 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700403 return new Object[]{
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700404 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
405 numDatabaseAllAppsColumns, dbFile};
406 }
407
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700408 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700409 Object[] oldState = toModelState();
410
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700411 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700412 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700413 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700414
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700415 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700416 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700417 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700418 }
419 }
420
Alex Chau1c883d82021-12-01 18:43:10 +0000421 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
422 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800423 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100424
Alex Chau1c883d82021-12-01 18:43:10 +0000425 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700426 final int depth = parser.getDepth();
427 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700428 while (((type = parser.next()) != XmlPullParser.END_TAG ||
429 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800430 if ((type == XmlPullParser.START_TAG)
431 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800432
Alex Chau1c883d82021-12-01 18:43:10 +0000433 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
434 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100435 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100436 final int displayDepth = parser.getDepth();
437 while (((type = parser.next()) != XmlPullParser.END_TAG
438 || parser.getDepth() > displayDepth)
439 && type != XmlPullParser.END_DOCUMENT) {
440 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
441 parser.getName())) {
442 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100443 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100444 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800445 }
446 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700447 }
448 }
sfufa@google.comde013292021-09-21 18:22:44 -0700449 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700450 throw new RuntimeException(e);
451 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800452
Sunny Goyalae190ff2020-04-14 00:19:01 +0000453 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800454 if (!TextUtils.isEmpty(gridName)) {
455 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100456 if (gridName.equals(option.grid.name)
457 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000458 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800459 }
460 }
461 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000462 if (filteredProfiles.isEmpty()) {
463 // No grid found, use the default options
464 for (DisplayOption option : profiles) {
465 if (option.canBeDefault) {
466 filteredProfiles.add(option);
467 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800468 }
469 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000470 if (filteredProfiles.isEmpty()) {
471 throw new RuntimeException("No display option with canBeDefault=true");
472 }
473 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700474 }
475
Thales Lima7ec83822021-08-05 13:32:10 +0100476 /**
477 * @return all the grid options that can be shown on the device
478 */
479 public List<GridOption> parseAllGridOptions(Context context) {
480 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100481
Alex Chau1c883d82021-12-01 18:43:10 +0000482 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100483 final int depth = parser.getDepth();
484 int type;
485 while (((type = parser.next()) != XmlPullParser.END_TAG
486 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
487 if ((type == XmlPullParser.START_TAG)
488 && GridOption.TAG_NAME.equals(parser.getName())) {
489 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000490 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100491 if (option.isEnabled) {
492 result.add(option);
493 }
494 }
495 }
496 } catch (IOException | XmlPullParserException e) {
497 Log.e(TAG, "Error parsing device profile", e);
498 return Collections.emptyList();
499 }
500 return result;
501 }
502
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700503 private int getLauncherIconDensity(int requiredSize) {
504 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700505 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700506 DisplayMetrics.DENSITY_LOW,
507 DisplayMetrics.DENSITY_MEDIUM,
508 DisplayMetrics.DENSITY_TV,
509 DisplayMetrics.DENSITY_HIGH,
510 DisplayMetrics.DENSITY_XHIGH,
511 DisplayMetrics.DENSITY_XXHIGH,
512 DisplayMetrics.DENSITY_XXXHIGH
513 };
514
515 int density = DisplayMetrics.DENSITY_XXXHIGH;
516 for (int i = densityBuckets.length - 1; i >= 0; i--) {
517 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
518 / DisplayMetrics.DENSITY_DEFAULT;
519 if (expectedSize >= requiredSize) {
520 density = densityBuckets[i];
521 }
522 }
523
524 return density;
525 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700526
Adam Cohen2e6da152015-05-06 11:42:25 -0700527 /**
528 * Apply any Partner customization grid overrides.
529 *
530 * Currently we support: all apps row / column count.
531 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700532 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
533 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700534 if (p != null) {
535 p.applyInvariantDeviceProfileOverrides(this, dm);
536 }
537 }
538
Sunny Goyal415f1732018-11-29 10:33:47 -0800539 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700540 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700541 }
542
Sunny Goyal19ff7282021-04-22 10:12:54 -0700543 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000544 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700545 int minWidthPx = Integer.MAX_VALUE;
546 int minHeightPx = Integer.MAX_VALUE;
547 for (WindowBounds bounds : displayInfo.supportedBounds) {
548 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000549 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700550 // For split displays, take half width per page
551 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
552 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700553
Sunny Goyal19ff7282021-04-22 10:12:54 -0700554 } else if (!isTablet && bounds.isLandscape()) {
555 // We will use transposed layout in this case
556 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
557 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
558 } else {
559 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
560 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
561 }
562 }
563
564 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
565 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700566
567 // Sort the profiles based on the closeness to the device size
568 Collections.sort(points, (a, b) ->
569 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
570 dist(width, height, b.minWidthDps, b.minHeightDps)));
571
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700572 DisplayOption closestPoint = points.get(0);
573 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700574 float weights = 0;
575
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700576 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
577 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700578 }
579
580 DisplayOption out = new DisplayOption(closestOption);
581 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700582 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700583 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
584 weights += w;
585 out.add(new DisplayOption().add(p).multiply(w));
586 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700587 out.multiply(1.0f / weights);
588
Thales Lima6e0005a2021-10-27 15:53:41 +0100589 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700590 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100591 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
592 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700593 }
594
595 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700596 }
597
Sunny Goyal27835952017-01-13 12:15:53 -0800598 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700599 Resources res = context.getResources();
600 Configuration config = context.getResources().getConfiguration();
601
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400602 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
603 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
604 return getBestMatch(screenWidth, screenHeight);
605 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700606
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400607 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700608 DeviceProfile bestMatch = supportedProfiles.get(0);
609 float minDiff = Float.MAX_VALUE;
610
611 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400612 float diff = Math.abs(profile.widthPx - screenWidth)
613 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700614 if (diff < minDiff) {
615 minDiff = diff;
616 bestMatch = profile;
617 }
618 }
619 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800620 }
621
Sunny Goyal415f1732018-11-29 10:33:47 -0800622 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700623 float d = dist(x0, y0, x1, y1);
624 if (Float.compare(d, 0f) == 0) {
625 return Float.POSITIVE_INFINITY;
626 }
627 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
628 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700629
630 /**
631 * As a ratio of screen height, the total distance we want the parallax effect to span
632 * horizontally
633 */
634 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
635 float aspectRatio = width / (float) height;
636
637 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
638 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
639 // We will use these two data points to extrapolate how much the wallpaper parallax effect
640 // to span (ie travel) at any aspect ratio:
641
sfufa@google.comde013292021-09-21 18:22:44 -0700642 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
643 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700644 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
645 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
646
647 // To find out the desired width at different aspect ratios, we use the following two
648 // formulas, where the coefficient on x is the aspect ratio (width/height):
649 // (16/10)x + y = 1.5
650 // (10/16)x + y = 1.2
651 // We solve for x and y and end up with a final formula:
652 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700653 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
654 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700655 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
656 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
657 return x * aspectRatio + y;
658 }
659
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700660 public interface OnIDPChangeListener {
661
Sunny Goyalb47172b2021-05-03 19:59:51 -0700662 /**
663 * Called when the device provide changes
664 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700665 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700666 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800667
668
Sunny Goyaleff44f32019-01-09 17:29:49 -0800669 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800670
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800671 public static final String TAG_NAME = "grid-option";
672
Alex Chau1c883d82021-12-01 18:43:10 +0000673 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
674 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
675 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
676 private static final int DEVICE_CATEGORY_ALL =
677 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
678
Sunny Goyaleff44f32019-01-09 17:29:49 -0800679 public final String name;
680 public final int numRows;
681 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100682 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800683
684 private final int numFolderRows;
685 private final int numFolderColumns;
686
Sunny Goyal19ff7282021-04-22 10:12:54 -0700687 private final int numAllAppsColumns;
688 private final int numDatabaseAllAppsColumns;
689 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700690 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800691
Tracy Zhou7df93d22020-01-27 13:44:06 -0800692 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000693
Sunny Goyal415f1732018-11-29 10:33:47 -0800694 private final int defaultLayoutId;
695 private final int demoModeLayoutId;
696
Jon Mirandae126d722021-02-25 10:45:20 -0500697 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400698 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500699
Sunny Goyal5bc18462019-01-07 15:13:39 -0800700 private final SparseArray<TypedValue> extraAttrs;
701
Alex Chau1c883d82021-12-01 18:43:10 +0000702 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800703 TypedArray a = context.obtainStyledAttributes(
704 attrs, R.styleable.GridDisplayOption);
705 name = a.getString(R.styleable.GridDisplayOption_name);
706 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
707 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
708
Tracy Zhou7df93d22020-01-27 13:44:06 -0800709 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000710 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100711 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
712 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
713 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800714 demoModeLayoutId = a.getResourceId(
715 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700716
717 numAllAppsColumns = a.getInt(
718 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
719 numDatabaseAllAppsColumns = a.getInt(
720 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
721
722 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800723 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700724 numDatabaseHotseatIcons = a.getInt(
725 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
726
Sunny Goyal415f1732018-11-29 10:33:47 -0800727 numFolderRows = a.getInt(
728 R.styleable.GridDisplayOption_numFolderRows, numRows);
729 numFolderColumns = a.getInt(
730 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700731
Jon Mirandae126d722021-02-25 10:45:20 -0500732 isScalable = a.getBoolean(
733 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400734 devicePaddingId = a.getResourceId(
735 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500736
Alex Chau1c883d82021-12-01 18:43:10 +0000737 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
738 DEVICE_CATEGORY_ALL);
739 isEnabled = (deviceType == TYPE_PHONE
740 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
741 || (deviceType == TYPE_TABLET
742 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
743 || (deviceType == TYPE_MULTI_DISPLAY
744 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
745 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100746
Sunny Goyal415f1732018-11-29 10:33:47 -0800747 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800748 extraAttrs = Themes.createValueMap(context, attrs,
749 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800750 }
751 }
752
Sunny Goyal19ff7282021-04-22 10:12:54 -0700753 @VisibleForTesting
754 static final class DisplayOption {
755
756 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800757
Sunny Goyal415f1732018-11-29 10:33:47 -0800758 private final float minWidthDps;
759 private final float minHeightDps;
760 private final boolean canBeDefault;
761
Thales Lima83bedbf2021-10-05 17:47:39 +0100762 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100763
764 private float folderBorderSpace;
765 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100766 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Lima6e7f36c2022-01-04 12:14:11 +0000767 private final float[] hotseatBorderSpaces = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100768
Thales Lima78d00ad2021-09-30 11:29:06 +0100769 private final float[] iconSizes = new float[COUNT_SIZES];
770 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800771
Thales Limabb7d3882021-12-22 12:56:29 +0000772 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
773 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
774 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
775
Thales Lima1de4d552021-10-13 16:13:25 +0100776 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800777 this.grid = grid;
778
Thales Lima1de4d552021-10-13 16:13:25 +0100779 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800780
Sunny Goyal415f1732018-11-29 10:33:47 -0800781 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
782 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700783
Thales Lima1de4d552021-10-13 16:13:25 +0100784 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800785
Thales Lima83bedbf2021-10-05 17:47:39 +0100786 float x;
787 float y;
788
Thales Lima85c942f2021-12-31 13:04:20 +0000789 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
790 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100791 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
792 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100793
Thales Lima85c942f2021-12-31 13:04:20 +0000794 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100795 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000796 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100797 minCellSize[INDEX_DEFAULT].y);
798 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
799
Thales Lima85c942f2021-12-31 13:04:20 +0000800 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100801 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000802 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100803 minCellSize[INDEX_DEFAULT].y);
804 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100805
Thales Lima85c942f2021-12-31 13:04:20 +0000806 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
807 float borderSpaceTwoPanelPortrait = a.getFloat(
808 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
809 float borderSpaceTwoPanelLandscape = a.getFloat(
810 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100811
Thales Lima85c942f2021-12-31 13:04:20 +0000812 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
813 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100814 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
815 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
816
817 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000818 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
819 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100820 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000821 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
822 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100823 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
824
825 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000826 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
827 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100828 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000829 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
830 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100831 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
832
Thales Limabb7d3882021-12-22 12:56:29 +0000833 folderBorderSpace = borderSpace;
834
Thales Lima85c942f2021-12-31 13:04:20 +0000835 x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpace,
Thales Lima78d00ad2021-09-30 11:29:06 +0100836 borderSpace);
Thales Limabb7d3882021-12-22 12:56:29 +0000837 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
838 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
839 x = y = a.getFloat(
840 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
841 allAppsBorderSpaces[INDEX_DEFAULT].x);
842 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
843 x = y = a.getFloat(
844 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
845 allAppsBorderSpaces[INDEX_DEFAULT].x);
846 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -0500847
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700848 iconSizes[INDEX_DEFAULT] =
849 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
850 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000851 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700852 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700853 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +0000854 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700855 iconSizes[INDEX_DEFAULT]);
856 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000857 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100858 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700859
Thales Limabb7d3882021-12-22 12:56:29 +0000860 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
861 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
862 allAppsIconSizes[INDEX_LANDSCAPE] = allAppsIconSizes[INDEX_DEFAULT];
863 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
864 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
865 allAppsIconSizes[INDEX_DEFAULT]);
866 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
867 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
868 allAppsIconSizes[INDEX_DEFAULT]);
869
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700870 textSizes[INDEX_DEFAULT] =
871 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
872 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000873 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700874 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700875 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +0000876 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700877 textSizes[INDEX_DEFAULT]);
878 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000879 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100880 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700881
Thales Limabb7d3882021-12-22 12:56:29 +0000882 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
883 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
884 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
885 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
886 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
887 allAppsIconTextSizes[INDEX_DEFAULT]);
888 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
889 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
890 allAppsIconTextSizes[INDEX_DEFAULT]);
891
Thales Lima83bedbf2021-10-05 17:47:39 +0100892 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
893 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
894 horizontalMargin[INDEX_LANDSCAPE] = horizontalMargin[INDEX_DEFAULT];
Thales Lima83bedbf2021-10-05 17:47:39 +0100895 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000896 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100897 horizontalMargin[INDEX_DEFAULT]);
898 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000899 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100900 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +0100901
Thales Lima6e7f36c2022-01-04 12:14:11 +0000902 hotseatBorderSpaces[INDEX_DEFAULT] = a.getFloat(
903 R.styleable.ProfileDisplayOption_hotseatBorderSpace, borderSpace);
904 hotseatBorderSpaces[INDEX_LANDSCAPE] = hotseatBorderSpaces[INDEX_DEFAULT];
Thales Lima6e7f36c2022-01-04 12:14:11 +0000905 hotseatBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
906 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelLandscape,
907 hotseatBorderSpaces[INDEX_DEFAULT]);
908 hotseatBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
909 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelPortrait,
910 hotseatBorderSpaces[INDEX_DEFAULT]);
911
Sunny Goyal415f1732018-11-29 10:33:47 -0800912 a.recycle();
913 }
914
915 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700916 this(null);
917 }
918
919 DisplayOption(GridOption grid) {
920 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800921 minWidthDps = 0;
922 minHeightDps = 0;
923 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +0100924 for (int i = 0; i < COUNT_SIZES; i++) {
925 iconSizes[i] = 0;
926 textSizes[i] = 0;
927 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +0100928 minCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +0000929 allAppsIconSizes[i] = 0;
930 allAppsIconTextSizes[i] = 0;
931 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +0100932 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800933 }
934
935 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100936 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700937 iconSizes[i] *= w;
938 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +0100939 borderSpaces[i].x *= w;
940 borderSpaces[i].y *= w;
941 minCellSize[i].x *= w;
942 minCellSize[i].y *= w;
943 horizontalMargin[i] *= w;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000944 hotseatBorderSpaces[i] *= w;
Thales Limabb7d3882021-12-22 12:56:29 +0000945 allAppsIconSizes[i] *= w;
946 allAppsIconTextSizes[i] *= w;
947 allAppsBorderSpaces[i].x *= w;
948 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700949 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100950
951 folderBorderSpace *= w;
952
Sunny Goyal415f1732018-11-29 10:33:47 -0800953 return this;
954 }
955
956 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100957 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700958 iconSizes[i] += p.iconSizes[i];
959 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +0100960 borderSpaces[i].x += p.borderSpaces[i].x;
961 borderSpaces[i].y += p.borderSpaces[i].y;
962 minCellSize[i].x += p.minCellSize[i].x;
963 minCellSize[i].y += p.minCellSize[i].y;
964 horizontalMargin[i] += p.horizontalMargin[i];
Thales Lima6e7f36c2022-01-04 12:14:11 +0000965 hotseatBorderSpaces[i] += p.hotseatBorderSpaces[i];
Thales Limabb7d3882021-12-22 12:56:29 +0000966 allAppsIconSizes[i] += p.allAppsIconSizes[i];
967 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
968 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
969 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700970 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100971
972 folderBorderSpace += p.folderBorderSpace;
973
Sunny Goyal415f1732018-11-29 10:33:47 -0800974 return this;
975 }
976 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000977}