blob: b675ed5d670810471afc268b9ce11c4d1d41135a [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
97 static final int COUNT_SIZES = 5;
98 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;
102 static final int INDEX_ALL_APPS = 4;
103
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700104 /**
105 * Number of icons per row and column in the workspace.
106 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700107 public int numRows;
108 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700109
110 /**
111 * Number of icons per row and column in the folder.
112 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700113 public int numFolderRows;
114 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100115 public float[] iconSize;
116 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700117 public int iconBitmapSize;
118 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000119 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700120
Thales Lima83bedbf2021-10-05 17:47:39 +0100121 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100122
Thales Lima83bedbf2021-10-05 17:47:39 +0100123 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100124 public float folderBorderSpace;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000125 public float[] hotseatBorderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100126
Thales Lima83bedbf2021-10-05 17:47:39 +0100127 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500128
Sunny Goyal5bc18462019-01-07 15:13:39 -0800129 private SparseArray<TypedValue> mExtraAttrs;
130
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700131 /**
132 * Number of icons inside the hotseat area.
133 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700134 protected int numShownHotseatIcons;
135
136 /**
137 * Number of icons inside the hotseat area that is stored in the database. This is greater than
138 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
139 * sizes that share the same DB.
140 */
141 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700142
Jon Miranda6f7e9702019-09-16 14:44:14 -0700143 /**
144 * Number of columns in the all apps list.
145 */
146 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700147 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700148
Jon Mirandae126d722021-02-25 10:45:20 -0500149 /**
150 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
151 */
152 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400153 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500154
Tracy Zhou7df93d22020-01-27 13:44:06 -0800155 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800156 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700157 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700158
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000159 /**
160 * An immutable list of supported profiles.
161 */
162 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700163
sfufa@google.comde013292021-09-21 18:22:44 -0700164 @Nullable
165 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500166
Sunny Goyal6f866092016-03-17 17:04:15 -0700167 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700168 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700169
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700170 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700171
Sunny Goyalf633ef52018-03-13 09:57:05 -0700172 @VisibleForTesting
sfufa@google.comde013292021-09-21 18:22:44 -0700173 public InvariantDeviceProfile() {
174 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700175
Sunny Goyalbbf01842015-10-08 07:41:15 -0700176 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700177 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700178 String gridName = getCurrentGridName(context);
179 String newGridName = initGrid(context, gridName);
180 if (!newGridName.equals(gridName)) {
181 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
182 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700183 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700184
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700185 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100186 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700187 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100188 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700189 }
190 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700191 }
192
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700193 /**
194 * This constructor should NOT have any monitors by design.
195 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800196 public InvariantDeviceProfile(Context context, String gridName) {
197 String newName = initGrid(context, gridName);
198 if (newName == null || !newName.equals(gridName)) {
199 throw new IllegalArgumentException("Unknown grid name");
200 }
201 }
202
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700203 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800204 * This constructor should NOT have any monitors by design.
205 */
206 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700207 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100208 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700209 String gridName = getCurrentGridName(context);
210
211 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000212 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
213 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700214 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000215 defaultInfo,
216 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
217 /*allowDisabledGrid=*/false),
218 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700219
Alex Chau417bd722021-01-26 15:22:18 +0000220 Info myInfo = new Info(context, display);
Alex Chau1c883d82021-12-01 18:43:10 +0000221 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700222 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000223 myInfo,
224 getPredefinedDeviceProfiles(context, gridName, deviceType,
225 /*allowDisabledGrid=*/false),
226 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700227
228 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
229 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100230 result.iconSizes[INDEX_DEFAULT] =
231 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
232 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700233 result.iconSizes[i] = Math.min(
234 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000235 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700236
Thales Lima83bedbf2021-10-05 17:47:39 +0100237 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
238 COUNT_SIZES);
239 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
240 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500241
Alex Chau1c883d82021-12-01 18:43:10 +0000242 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800243 }
244
Alex Chau238aaee2021-10-06 16:15:24 +0100245 /**
246 * Reinitialize the current grid after a restore, where some grids might now be disabled.
247 */
248 public void reinitializeAfterRestore(Context context) {
249 String currentDbFile = dbFile;
250 String gridName = getCurrentGridName(context);
251 String newGridName = initGrid(context, gridName);
252 if (!newGridName.equals(gridName)) {
253 Log.d(TAG, "Restored grid is disabled : " + gridName
254 + ", migrating to: " + newGridName
255 + ", removing all other grid db files");
256 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
257 if (gridDbFile.equals(currentDbFile)) {
258 continue;
259 }
260 if (context.getDatabasePath(gridDbFile).delete()) {
261 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
262 }
263 }
264 setCurrentGrid(context, gridName);
265 }
266 }
267
Alex Chau1c883d82021-12-01 18:43:10 +0000268 private static @DeviceType int getDeviceType(Info displayInfo) {
269 // Each screen has two profiles (portrait/landscape), so devices with four or more
270 // supported profiles implies two or more internal displays.
271 if (displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get()) {
272 return TYPE_MULTI_DISPLAY;
273 } else if (displayInfo.supportedBounds.stream().allMatch(displayInfo::isTablet)) {
274 return TYPE_TABLET;
275 } else {
276 return TYPE_PHONE;
277 }
278 }
279
Tracy Zhou42255d22020-03-13 00:38:11 -0700280 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700281 return Utilities.isGridOptionsEnabled(context)
282 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700283 }
284
Sunny Goyaleff44f32019-01-09 17:29:49 -0800285 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700286 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000287 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700288
289 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000290 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100291 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700292 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000293 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
294 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700295 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800296 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700297
Alex Chau1c883d82021-12-01 18:43:10 +0000298 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
299 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700300 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700301 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000302 numRows = closestProfile.numRows;
303 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000304 dbFile = closestProfile.dbFile;
305 defaultLayoutId = closestProfile.defaultLayoutId;
306 demoModeLayoutId = closestProfile.demoModeLayoutId;
307 numFolderRows = closestProfile.numFolderRows;
308 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500309 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400310 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000311 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000312
313 mExtraAttrs = closestProfile.extraAttrs;
314
Thales Lima83bedbf2021-10-05 17:47:39 +0100315 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100316 float maxIconSize = iconSize[0];
317 for (int i = 1; i < iconSize.length; i++) {
318 maxIconSize = Math.max(maxIconSize, iconSize[i]);
319 }
320 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000321 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
322
Thales Lima83bedbf2021-10-05 17:47:39 +0100323 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700324
Thales Lima83bedbf2021-10-05 17:47:39 +0100325 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100326
Thales Lima83bedbf2021-10-05 17:47:39 +0100327 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100328 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700329
Thales Limad90faab2021-09-21 17:18:47 +0100330 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100331
Sunny Goyal19ff7282021-04-22 10:12:54 -0700332 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000333 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700334 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000335 hotseatBorderSpaces = displayOption.hotseatBorderSpaces;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700336
337 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000338 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700339 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500340
Thales Lima83bedbf2021-10-05 17:47:39 +0100341 if (!Utilities.isGridOptionsEnabled(context)) {
342 iconSize[INDEX_ALL_APPS] = iconSize[INDEX_DEFAULT];
343 iconTextSize[INDEX_ALL_APPS] = iconTextSize[INDEX_DEFAULT];
Sunny Goyalae190ff2020-04-14 00:19:01 +0000344 }
345
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400346 if (devicePaddingId != 0) {
347 devicePaddings = new DevicePaddings(context, devicePaddingId);
348 }
349
Sunny Goyalae190ff2020-04-14 00:19:01 +0000350 // If the partner customization apk contains any grid overrides, apply them
351 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700352 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700353
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000354 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700355 defaultWallpaperSize = new Point(displayInfo.currentSize);
356 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000357 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000358 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700359 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700360
Sunny Goyal19ff7282021-04-22 10:12:54 -0700361 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
362 int displayWidth = bounds.bounds.width();
363 int displayHeight = bounds.bounds.height();
364 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700365
Sunny Goyal19ff7282021-04-22 10:12:54 -0700366 // We need to ensure that there is enough extra space in the wallpaper
367 // for the intended parallax effects
368 float parallaxFactor =
369 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
370 ? 2
371 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
372 defaultWallpaperSize.x =
373 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700374 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000375 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700376
377 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
378 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700379 }
380
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700381 public void addOnChangeListener(OnIDPChangeListener listener) {
382 mChangeListeners.add(listener);
383 }
384
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800385 public void removeOnChangeListener(OnIDPChangeListener listener) {
386 mChangeListeners.remove(listener);
387 }
388
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800389
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800390 public void setCurrentGrid(Context context, String gridName) {
391 Context appContext = context.getApplicationContext();
392 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700393 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800394 }
395
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700396 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700397 return new Object[]{
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700398 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
399 numDatabaseAllAppsColumns, dbFile};
400 }
401
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700402 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700403 Object[] oldState = toModelState();
404
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700405 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700406 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700407 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700408
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700409 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700410 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700411 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700412 }
413 }
414
Alex Chau1c883d82021-12-01 18:43:10 +0000415 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
416 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800417 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100418
Alex Chau1c883d82021-12-01 18:43:10 +0000419 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700420 final int depth = parser.getDepth();
421 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700422 while (((type = parser.next()) != XmlPullParser.END_TAG ||
423 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800424 if ((type == XmlPullParser.START_TAG)
425 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800426
Alex Chau1c883d82021-12-01 18:43:10 +0000427 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
428 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100429 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100430 final int displayDepth = parser.getDepth();
431 while (((type = parser.next()) != XmlPullParser.END_TAG
432 || parser.getDepth() > displayDepth)
433 && type != XmlPullParser.END_DOCUMENT) {
434 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
435 parser.getName())) {
436 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100437 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100438 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800439 }
440 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700441 }
442 }
sfufa@google.comde013292021-09-21 18:22:44 -0700443 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700444 throw new RuntimeException(e);
445 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800446
Sunny Goyalae190ff2020-04-14 00:19:01 +0000447 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800448 if (!TextUtils.isEmpty(gridName)) {
449 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100450 if (gridName.equals(option.grid.name)
451 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000452 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800453 }
454 }
455 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000456 if (filteredProfiles.isEmpty()) {
457 // No grid found, use the default options
458 for (DisplayOption option : profiles) {
459 if (option.canBeDefault) {
460 filteredProfiles.add(option);
461 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800462 }
463 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000464 if (filteredProfiles.isEmpty()) {
465 throw new RuntimeException("No display option with canBeDefault=true");
466 }
467 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700468 }
469
Thales Lima7ec83822021-08-05 13:32:10 +0100470 /**
471 * @return all the grid options that can be shown on the device
472 */
473 public List<GridOption> parseAllGridOptions(Context context) {
474 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100475
Alex Chau1c883d82021-12-01 18:43:10 +0000476 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100477 final int depth = parser.getDepth();
478 int type;
479 while (((type = parser.next()) != XmlPullParser.END_TAG
480 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
481 if ((type == XmlPullParser.START_TAG)
482 && GridOption.TAG_NAME.equals(parser.getName())) {
483 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000484 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100485 if (option.isEnabled) {
486 result.add(option);
487 }
488 }
489 }
490 } catch (IOException | XmlPullParserException e) {
491 Log.e(TAG, "Error parsing device profile", e);
492 return Collections.emptyList();
493 }
494 return result;
495 }
496
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700497 private int getLauncherIconDensity(int requiredSize) {
498 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700499 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700500 DisplayMetrics.DENSITY_LOW,
501 DisplayMetrics.DENSITY_MEDIUM,
502 DisplayMetrics.DENSITY_TV,
503 DisplayMetrics.DENSITY_HIGH,
504 DisplayMetrics.DENSITY_XHIGH,
505 DisplayMetrics.DENSITY_XXHIGH,
506 DisplayMetrics.DENSITY_XXXHIGH
507 };
508
509 int density = DisplayMetrics.DENSITY_XXXHIGH;
510 for (int i = densityBuckets.length - 1; i >= 0; i--) {
511 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
512 / DisplayMetrics.DENSITY_DEFAULT;
513 if (expectedSize >= requiredSize) {
514 density = densityBuckets[i];
515 }
516 }
517
518 return density;
519 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700520
Adam Cohen2e6da152015-05-06 11:42:25 -0700521 /**
522 * Apply any Partner customization grid overrides.
523 *
524 * Currently we support: all apps row / column count.
525 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700526 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
527 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700528 if (p != null) {
529 p.applyInvariantDeviceProfileOverrides(this, dm);
530 }
531 }
532
Sunny Goyal415f1732018-11-29 10:33:47 -0800533 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700534 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700535 }
536
Sunny Goyal19ff7282021-04-22 10:12:54 -0700537 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000538 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700539 int minWidthPx = Integer.MAX_VALUE;
540 int minHeightPx = Integer.MAX_VALUE;
541 for (WindowBounds bounds : displayInfo.supportedBounds) {
542 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000543 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700544 // For split displays, take half width per page
545 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
546 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700547
Sunny Goyal19ff7282021-04-22 10:12:54 -0700548 } else if (!isTablet && bounds.isLandscape()) {
549 // We will use transposed layout in this case
550 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
551 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
552 } else {
553 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
554 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
555 }
556 }
557
558 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
559 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700560
561 // Sort the profiles based on the closeness to the device size
562 Collections.sort(points, (a, b) ->
563 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
564 dist(width, height, b.minWidthDps, b.minHeightDps)));
565
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700566 DisplayOption closestPoint = points.get(0);
567 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700568 float weights = 0;
569
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700570 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
571 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700572 }
573
574 DisplayOption out = new DisplayOption(closestOption);
575 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700576 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700577 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
578 weights += w;
579 out.add(new DisplayOption().add(p).multiply(w));
580 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700581 out.multiply(1.0f / weights);
582
Thales Lima6e0005a2021-10-27 15:53:41 +0100583 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700584 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100585 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
586 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700587 }
588
589 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700590 }
591
Sunny Goyal27835952017-01-13 12:15:53 -0800592 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700593 Resources res = context.getResources();
594 Configuration config = context.getResources().getConfiguration();
595
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400596 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
597 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
598 return getBestMatch(screenWidth, screenHeight);
599 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700600
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400601 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700602 DeviceProfile bestMatch = supportedProfiles.get(0);
603 float minDiff = Float.MAX_VALUE;
604
605 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400606 float diff = Math.abs(profile.widthPx - screenWidth)
607 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700608 if (diff < minDiff) {
609 minDiff = diff;
610 bestMatch = profile;
611 }
612 }
613 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800614 }
615
Sunny Goyal415f1732018-11-29 10:33:47 -0800616 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700617 float d = dist(x0, y0, x1, y1);
618 if (Float.compare(d, 0f) == 0) {
619 return Float.POSITIVE_INFINITY;
620 }
621 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
622 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700623
624 /**
625 * As a ratio of screen height, the total distance we want the parallax effect to span
626 * horizontally
627 */
628 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
629 float aspectRatio = width / (float) height;
630
631 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
632 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
633 // We will use these two data points to extrapolate how much the wallpaper parallax effect
634 // to span (ie travel) at any aspect ratio:
635
sfufa@google.comde013292021-09-21 18:22:44 -0700636 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
637 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700638 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
639 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
640
641 // To find out the desired width at different aspect ratios, we use the following two
642 // formulas, where the coefficient on x is the aspect ratio (width/height):
643 // (16/10)x + y = 1.5
644 // (10/16)x + y = 1.2
645 // We solve for x and y and end up with a final formula:
646 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700647 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
648 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700649 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
650 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
651 return x * aspectRatio + y;
652 }
653
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700654 public interface OnIDPChangeListener {
655
Sunny Goyalb47172b2021-05-03 19:59:51 -0700656 /**
657 * Called when the device provide changes
658 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700659 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700660 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800661
662
Sunny Goyaleff44f32019-01-09 17:29:49 -0800663 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800664
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800665 public static final String TAG_NAME = "grid-option";
666
Alex Chau1c883d82021-12-01 18:43:10 +0000667 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
668 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
669 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
670 private static final int DEVICE_CATEGORY_ALL =
671 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
672
Sunny Goyaleff44f32019-01-09 17:29:49 -0800673 public final String name;
674 public final int numRows;
675 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100676 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800677
678 private final int numFolderRows;
679 private final int numFolderColumns;
680
Sunny Goyal19ff7282021-04-22 10:12:54 -0700681 private final int numAllAppsColumns;
682 private final int numDatabaseAllAppsColumns;
683 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700684 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800685
Tracy Zhou7df93d22020-01-27 13:44:06 -0800686 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000687
Sunny Goyal415f1732018-11-29 10:33:47 -0800688 private final int defaultLayoutId;
689 private final int demoModeLayoutId;
690
Jon Mirandae126d722021-02-25 10:45:20 -0500691 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400692 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500693
Sunny Goyal5bc18462019-01-07 15:13:39 -0800694 private final SparseArray<TypedValue> extraAttrs;
695
Alex Chau1c883d82021-12-01 18:43:10 +0000696 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800697 TypedArray a = context.obtainStyledAttributes(
698 attrs, R.styleable.GridDisplayOption);
699 name = a.getString(R.styleable.GridDisplayOption_name);
700 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
701 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
702
Tracy Zhou7df93d22020-01-27 13:44:06 -0800703 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000704 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100705 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
706 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
707 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800708 demoModeLayoutId = a.getResourceId(
709 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700710
711 numAllAppsColumns = a.getInt(
712 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
713 numDatabaseAllAppsColumns = a.getInt(
714 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
715
716 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800717 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700718 numDatabaseHotseatIcons = a.getInt(
719 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
720
Sunny Goyal415f1732018-11-29 10:33:47 -0800721 numFolderRows = a.getInt(
722 R.styleable.GridDisplayOption_numFolderRows, numRows);
723 numFolderColumns = a.getInt(
724 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700725
Jon Mirandae126d722021-02-25 10:45:20 -0500726 isScalable = a.getBoolean(
727 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400728 devicePaddingId = a.getResourceId(
729 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500730
Alex Chau1c883d82021-12-01 18:43:10 +0000731 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
732 DEVICE_CATEGORY_ALL);
733 isEnabled = (deviceType == TYPE_PHONE
734 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
735 || (deviceType == TYPE_TABLET
736 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
737 || (deviceType == TYPE_MULTI_DISPLAY
738 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
739 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100740
Sunny Goyal415f1732018-11-29 10:33:47 -0800741 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800742 extraAttrs = Themes.createValueMap(context, attrs,
743 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800744 }
745 }
746
Sunny Goyal19ff7282021-04-22 10:12:54 -0700747 @VisibleForTesting
748 static final class DisplayOption {
749
750 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800751
Sunny Goyal415f1732018-11-29 10:33:47 -0800752 private final float minWidthDps;
753 private final float minHeightDps;
754 private final boolean canBeDefault;
755
Thales Lima83bedbf2021-10-05 17:47:39 +0100756 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100757
758 private float folderBorderSpace;
759 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100760 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Lima6e7f36c2022-01-04 12:14:11 +0000761 private final float[] hotseatBorderSpaces = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100762
Thales Lima78d00ad2021-09-30 11:29:06 +0100763 private final float[] iconSizes = new float[COUNT_SIZES];
764 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800765
Thales Lima1de4d552021-10-13 16:13:25 +0100766 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800767 this.grid = grid;
768
Thales Lima1de4d552021-10-13 16:13:25 +0100769 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800770
Sunny Goyal415f1732018-11-29 10:33:47 -0800771 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
772 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700773
Thales Lima1de4d552021-10-13 16:13:25 +0100774 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800775
Thales Lima83bedbf2021-10-05 17:47:39 +0100776 float x;
777 float y;
778
Thales Lima85c942f2021-12-31 13:04:20 +0000779 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
780 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100781 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
782 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
783 minCellSize[INDEX_ALL_APPS] = new PointF(x, y);
784
Thales Lima85c942f2021-12-31 13:04:20 +0000785 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100786 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000787 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100788 minCellSize[INDEX_DEFAULT].y);
789 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
790
Thales Lima85c942f2021-12-31 13:04:20 +0000791 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100792 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000793 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100794 minCellSize[INDEX_DEFAULT].y);
795 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100796
Thales Lima85c942f2021-12-31 13:04:20 +0000797 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
798 float borderSpaceTwoPanelPortrait = a.getFloat(
799 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
800 float borderSpaceTwoPanelLandscape = a.getFloat(
801 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100802
Thales Lima85c942f2021-12-31 13:04:20 +0000803 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
804 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100805 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
806 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
807
808 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000809 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
810 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100811 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000812 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
813 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100814 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
815
816 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000817 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
818 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100819 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000820 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
821 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100822 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
823
Thales Lima85c942f2021-12-31 13:04:20 +0000824 x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpace,
Thales Lima78d00ad2021-09-30 11:29:06 +0100825 borderSpace);
826 borderSpaces[INDEX_ALL_APPS] = new PointF(x, y);
827 folderBorderSpace = borderSpace;
Jon Mirandae126d722021-02-25 10:45:20 -0500828
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700829 iconSizes[INDEX_DEFAULT] =
830 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
831 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000832 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700833 iconSizes[INDEX_DEFAULT]);
834 iconSizes[INDEX_ALL_APPS] =
835 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
836 iconSizes[INDEX_DEFAULT]);
837 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +0000838 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700839 iconSizes[INDEX_DEFAULT]);
840 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000841 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100842 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700843
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700844 textSizes[INDEX_DEFAULT] =
845 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
846 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000847 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700848 textSizes[INDEX_DEFAULT]);
849 textSizes[INDEX_ALL_APPS] =
850 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
851 textSizes[INDEX_DEFAULT]);
852 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +0000853 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700854 textSizes[INDEX_DEFAULT]);
855 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000856 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100857 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700858
Thales Lima83bedbf2021-10-05 17:47:39 +0100859 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
860 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
861 horizontalMargin[INDEX_LANDSCAPE] = horizontalMargin[INDEX_DEFAULT];
862 horizontalMargin[INDEX_ALL_APPS] = horizontalMargin[INDEX_DEFAULT];
863 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000864 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100865 horizontalMargin[INDEX_DEFAULT]);
866 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000867 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100868 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +0100869
Thales Lima6e7f36c2022-01-04 12:14:11 +0000870 hotseatBorderSpaces[INDEX_DEFAULT] = a.getFloat(
871 R.styleable.ProfileDisplayOption_hotseatBorderSpace, borderSpace);
872 hotseatBorderSpaces[INDEX_LANDSCAPE] = hotseatBorderSpaces[INDEX_DEFAULT];
873 hotseatBorderSpaces[INDEX_ALL_APPS] = hotseatBorderSpaces[INDEX_DEFAULT];
874 hotseatBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
875 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelLandscape,
876 hotseatBorderSpaces[INDEX_DEFAULT]);
877 hotseatBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
878 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelPortrait,
879 hotseatBorderSpaces[INDEX_DEFAULT]);
880
Sunny Goyal415f1732018-11-29 10:33:47 -0800881 a.recycle();
882 }
883
884 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700885 this(null);
886 }
887
888 DisplayOption(GridOption grid) {
889 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800890 minWidthDps = 0;
891 minHeightDps = 0;
892 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +0100893 for (int i = 0; i < COUNT_SIZES; i++) {
894 iconSizes[i] = 0;
895 textSizes[i] = 0;
896 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +0100897 minCellSize[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +0100898 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800899 }
900
901 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100902 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700903 iconSizes[i] *= w;
904 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +0100905 borderSpaces[i].x *= w;
906 borderSpaces[i].y *= w;
907 minCellSize[i].x *= w;
908 minCellSize[i].y *= w;
909 horizontalMargin[i] *= w;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000910 hotseatBorderSpaces[i] *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700911 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100912
913 folderBorderSpace *= w;
914
Sunny Goyal415f1732018-11-29 10:33:47 -0800915 return this;
916 }
917
918 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100919 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700920 iconSizes[i] += p.iconSizes[i];
921 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +0100922 borderSpaces[i].x += p.borderSpaces[i].x;
923 borderSpaces[i].y += p.borderSpaces[i].y;
924 minCellSize[i].x += p.minCellSize[i].x;
925 minCellSize[i].y += p.minCellSize[i].y;
926 horizontalMargin[i] += p.horizontalMargin[i];
Thales Lima6e7f36c2022-01-04 12:14:11 +0000927 hotseatBorderSpaces[i] += p.hotseatBorderSpaces[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700928 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100929
930 folderBorderSpace += p.folderBorderSpace;
931
Sunny Goyal415f1732018-11-29 10:33:47 -0800932 return this;
933 }
934 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000935}