blob: 59ef28380b9b11de37d3ff381810af746bdbce1e [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
Thales Lima1de4d552021-10-13 16:13:25 +01002 * Copyright (C) 2021 The Android Open Source Project
Adam Cohen2e6da152015-05-06 11:42:25 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Sunny Goyal19ff7282021-04-22 10:12:54 -070019import static com.android.launcher3.Utilities.dpiFromPx;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010020import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Sunny Goyal35c7b192021-04-20 16:51:10 -070021import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Alex Chau6ed408f2022-03-04 12:58:05 +000022import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE;
Sunny Goyal19ff7282021-04-22 10:12:54 -070023import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080024import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070025
Sunny Goyalc6205602015-05-21 20:46:33 -070026import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070028import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070029import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080030import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080031import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070032import android.content.res.TypedArray;
33import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010035import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070036import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080037import android.text.TextUtils;
38import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070039import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010040import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080041import android.util.SparseArray;
42import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070043import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080044import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070045
Alex Chau1c883d82021-12-01 18:43:10 +000046import androidx.annotation.IntDef;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070047import androidx.annotation.Nullable;
48import androidx.annotation.VisibleForTesting;
49
Sunny Goyal68031ca2021-08-02 12:23:44 -070050import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010051import com.android.launcher3.provider.RestoreDbTask;
Sunny Goyalfd58da62020-08-11 12:06:49 -070052import com.android.launcher3.util.DisplayController;
53import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080054import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070055import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080056import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070057import com.android.launcher3.util.WindowBounds;
Sunny Goyal187b16c2022-03-01 16:53:23 -080058import com.android.launcher3.util.window.WindowManagerProxy;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070059
Sunny Goyal819e1932016-07-07 16:43:58 -070060import org.xmlpull.v1.XmlPullParser;
61import org.xmlpull.v1.XmlPullParserException;
62
63import java.io.IOException;
Alex Chau1c883d82021-12-01 18:43:10 +000064import java.lang.annotation.Retention;
65import java.lang.annotation.RetentionPolicy;
Adam Cohen2e6da152015-05-06 11:42:25 -070066import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070067import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080068import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070069import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070070
71public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070072
Hyunyoung Songc55a3502018-12-04 15:43:16 -080073 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070074 // We do not need any synchronization for this variable as its only written on UI thread.
75 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070076 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070077
Alex Chau1c883d82021-12-01 18:43:10 +000078 @Retention(RetentionPolicy.SOURCE)
79 @IntDef({TYPE_PHONE, TYPE_MULTI_DISPLAY, TYPE_TABLET})
80 public @interface DeviceType{}
81 public static final int TYPE_PHONE = 0;
82 public static final int TYPE_MULTI_DISPLAY = 1;
83 public static final int TYPE_TABLET = 2;
84
Hyunyoung Songc55a3502018-12-04 15:43:16 -080085 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080086
Sunny Goyal53d7ee42015-05-22 12:25:45 -070087 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
88
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070089 // Constants that affects the interpolation curve between statically defined device profile
90 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080091 private static final float KNEARESTNEIGHBOR = 3;
92 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070093
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070094 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080095 private static final float WEIGHT_EFFICIENT = 100000f;
96
Thales Lima83bedbf2021-10-05 17:47:39 +010097 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
98 // constraints
Thales Limabb7d3882021-12-22 12:56:29 +000099 static final int COUNT_SIZES = 4;
Thales Lima83bedbf2021-10-05 17:47:39 +0100100 static final int INDEX_DEFAULT = 0;
101 static final int INDEX_LANDSCAPE = 1;
102 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
103 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
Thales Lima83bedbf2021-10-05 17:47:39 +0100104
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700105 /**
106 * Number of icons per row and column in the workspace.
107 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700108 public int numRows;
109 public int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000110 public int numSearchContainerColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700111
112 /**
113 * Number of icons per row and column in the folder.
114 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700115 public int numFolderRows;
116 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100117 public float[] iconSize;
118 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700119 public int iconBitmapSize;
120 public int fillResIconDpi;
Alex Chau1c883d82021-12-01 18:43:10 +0000121 public @DeviceType int deviceType;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700122
Thales Lima83bedbf2021-10-05 17:47:39 +0100123 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100124
Thales Lima83bedbf2021-10-05 17:47:39 +0100125 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100126 public float folderBorderSpace;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000127 public float[] hotseatBorderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100128
Thales Lima83bedbf2021-10-05 17:47:39 +0100129 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500130
Thales Limab7ef5692022-03-10 10:32:36 +0000131 public PointF[] allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000132 public float[] allAppsIconSize;
133 public float[] allAppsIconTextSize;
134 public PointF[] allAppsBorderSpaces;
135
Sunny Goyal5bc18462019-01-07 15:13:39 -0800136 private SparseArray<TypedValue> mExtraAttrs;
137
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700138 /**
139 * Number of icons inside the hotseat area.
140 */
Jon Mirandafd48b0c2022-01-31 16:25:22 -0800141 public int numShownHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700142
143 /**
Alex Chau6ed408f2022-03-04 12:58:05 +0000144 * Number of icons inside the hotseat area when using 3 buttons navigation.
145 */
146 public int numShrunkenHotseatIcons;
147
148 /**
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700149 * Number of icons inside the hotseat area that is stored in the database. This is greater than
150 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
151 * sizes that share the same DB.
152 */
153 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700154
Jon Miranda6f7e9702019-09-16 14:44:14 -0700155 /**
156 * Number of columns in the all apps list.
157 */
158 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700159 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700160
Jon Mirandae126d722021-02-25 10:45:20 -0500161 /**
162 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
163 */
164 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400165 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500166
Tracy Zhou7df93d22020-01-27 13:44:06 -0800167 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800168 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700169 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700170
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000171 /**
172 * An immutable list of supported profiles.
173 */
174 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700175
sfufa@google.comde013292021-09-21 18:22:44 -0700176 @Nullable
177 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500178
Sunny Goyal6f866092016-03-17 17:04:15 -0700179 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700180 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700181
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700182 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700183
Sunny Goyalf633ef52018-03-13 09:57:05 -0700184 @VisibleForTesting
Sunny Goyal187b16c2022-03-01 16:53:23 -0800185 public InvariantDeviceProfile() { }
Adam Cohen2e6da152015-05-06 11:42:25 -0700186
Sunny Goyalbbf01842015-10-08 07:41:15 -0700187 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700188 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700189 String gridName = getCurrentGridName(context);
190 String newGridName = initGrid(context, gridName);
191 if (!newGridName.equals(gridName)) {
192 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
193 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700194 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700195
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700196 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100197 (displayContext, info, flags) -> {
Alex Chau6ed408f2022-03-04 12:58:05 +0000198 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS
199 | CHANGE_NAVIGATION_MODE)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100200 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700201 }
202 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700203 }
204
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700205 /**
206 * This constructor should NOT have any monitors by design.
207 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800208 public InvariantDeviceProfile(Context context, String gridName) {
209 String newName = initGrid(context, gridName);
210 if (newName == null || !newName.equals(gridName)) {
211 throw new IllegalArgumentException("Unknown grid name");
212 }
213 }
214
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700215 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800216 * This constructor should NOT have any monitors by design.
217 */
218 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700219 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100220 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700221 String gridName = getCurrentGridName(context);
222
223 // Get the display info based on default display and interpolate it to existing display
Alex Chau1c883d82021-12-01 18:43:10 +0000224 Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
225 @DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700226 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000227 defaultInfo,
228 getPredefinedDeviceProfiles(context, gridName, defaultDeviceType,
229 /*allowDisabledGrid=*/false),
230 defaultDeviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700231
Alex Chau417bd722021-01-26 15:22:18 +0000232 Info myInfo = new Info(context, display);
Alex Chau1c883d82021-12-01 18:43:10 +0000233 @DeviceType int deviceType = getDeviceType(myInfo);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700234 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000235 myInfo,
236 getPredefinedDeviceProfiles(context, gridName, deviceType,
237 /*allowDisabledGrid=*/false),
238 deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700239
240 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
241 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100242 result.iconSizes[INDEX_DEFAULT] =
243 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
244 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700245 result.iconSizes[i] = Math.min(
246 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000247 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700248
Thales Lima83bedbf2021-10-05 17:47:39 +0100249 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
250 COUNT_SIZES);
251 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
252 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500253
Alex Chau1c883d82021-12-01 18:43:10 +0000254 initGrid(context, myInfo, result, deviceType);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800255 }
256
Alex Chau238aaee2021-10-06 16:15:24 +0100257 /**
258 * Reinitialize the current grid after a restore, where some grids might now be disabled.
259 */
260 public void reinitializeAfterRestore(Context context) {
Alex Chau29a96ad2022-02-10 13:12:20 +0000261 String currentGridName = getCurrentGridName(context);
Alex Chau238aaee2021-10-06 16:15:24 +0100262 String currentDbFile = dbFile;
Alex Chau29a96ad2022-02-10 13:12:20 +0000263 String newGridName = initGrid(context, currentGridName);
264 String newDbFile = dbFile;
265 if (!newDbFile.equals(currentDbFile)) {
266 Log.d(TAG, "Restored grid is disabled : " + currentGridName
Alex Chau238aaee2021-10-06 16:15:24 +0100267 + ", migrating to: " + newGridName
268 + ", removing all other grid db files");
269 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
270 if (gridDbFile.equals(currentDbFile)) {
271 continue;
272 }
273 if (context.getDatabasePath(gridDbFile).delete()) {
274 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
275 }
276 }
Alex Chau29a96ad2022-02-10 13:12:20 +0000277 setCurrentGrid(context, newGridName);
Alex Chau238aaee2021-10-06 16:15:24 +0100278 }
279 }
280
Alex Chau1c883d82021-12-01 18:43:10 +0000281 private static @DeviceType int getDeviceType(Info displayInfo) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800282 int flagPhone = 1 << 0;
283 int flagTablet = 1 << 1;
284
285 int type = displayInfo.supportedBounds.stream()
286 .mapToInt(bounds -> displayInfo.isTablet(bounds) ? flagTablet : flagPhone)
287 .reduce(0, (a, b) -> a | b);
288 if ((type == (flagPhone | flagTablet)) && ENABLE_TWO_PANEL_HOME.get()) {
289 // device has profiles supporting both phone and table modes
Alex Chau1c883d82021-12-01 18:43:10 +0000290 return TYPE_MULTI_DISPLAY;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800291 } else if (type == flagTablet) {
Alex Chau1c883d82021-12-01 18:43:10 +0000292 return TYPE_TABLET;
293 } else {
294 return TYPE_PHONE;
295 }
296 }
297
Tracy Zhou42255d22020-03-13 00:38:11 -0700298 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700299 return Utilities.isGridOptionsEnabled(context)
300 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700301 }
302
Sunny Goyaleff44f32019-01-09 17:29:49 -0800303 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700304 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Alex Chau1c883d82021-12-01 18:43:10 +0000305 @DeviceType int deviceType = getDeviceType(displayInfo);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700306
307 ArrayList<DisplayOption> allOptions =
Alex Chau1c883d82021-12-01 18:43:10 +0000308 getPredefinedDeviceProfiles(context, gridName, deviceType,
Alex Chau238aaee2021-10-06 16:15:24 +0100309 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700310 DisplayOption displayOption =
Alex Chau1c883d82021-12-01 18:43:10 +0000311 invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
312 initGrid(context, displayInfo, displayOption, deviceType);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700313 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800314 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700315
Alex Chau1c883d82021-12-01 18:43:10 +0000316 private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
317 @DeviceType int deviceType) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700318 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700319 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000320 numRows = closestProfile.numRows;
321 numColumns = closestProfile.numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000322 numSearchContainerColumns = closestProfile.numSearchContainerColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000323 dbFile = closestProfile.dbFile;
324 defaultLayoutId = closestProfile.defaultLayoutId;
325 demoModeLayoutId = closestProfile.demoModeLayoutId;
326 numFolderRows = closestProfile.numFolderRows;
327 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500328 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400329 devicePaddingId = closestProfile.devicePaddingId;
Alex Chau1c883d82021-12-01 18:43:10 +0000330 this.deviceType = deviceType;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000331
332 mExtraAttrs = closestProfile.extraAttrs;
333
Thales Lima83bedbf2021-10-05 17:47:39 +0100334 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100335 float maxIconSize = iconSize[0];
336 for (int i = 1; i < iconSize.length; i++) {
337 maxIconSize = Math.max(maxIconSize, iconSize[i]);
338 }
339 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000340 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
341
Thales Lima83bedbf2021-10-05 17:47:39 +0100342 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700343
Thales Lima83bedbf2021-10-05 17:47:39 +0100344 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100345
Thales Lima83bedbf2021-10-05 17:47:39 +0100346 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100347 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700348
Thales Limad90faab2021-09-21 17:18:47 +0100349 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100350
Sunny Goyal19ff7282021-04-22 10:12:54 -0700351 numShownHotseatIcons = closestProfile.numHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000352 numShrunkenHotseatIcons = closestProfile.numShrunkenHotseatIcons;
Alex Chau1c883d82021-12-01 18:43:10 +0000353 numDatabaseHotseatIcons = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700354 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
Thales Lima6e7f36c2022-01-04 12:14:11 +0000355 hotseatBorderSpaces = displayOption.hotseatBorderSpaces;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700356
357 numAllAppsColumns = closestProfile.numAllAppsColumns;
Alex Chau1c883d82021-12-01 18:43:10 +0000358 numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
Sunny Goyal19ff7282021-04-22 10:12:54 -0700359 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500360
Thales Limab7ef5692022-03-10 10:32:36 +0000361 allAppsCellSize = displayOption.allAppsCellSize;
Thales Limabb7d3882021-12-22 12:56:29 +0000362 allAppsBorderSpaces = displayOption.allAppsBorderSpaces;
363 allAppsIconSize = displayOption.allAppsIconSizes;
364 allAppsIconTextSize = displayOption.allAppsIconTextSizes;
Thales Lima83bedbf2021-10-05 17:47:39 +0100365 if (!Utilities.isGridOptionsEnabled(context)) {
Thales Limabb7d3882021-12-22 12:56:29 +0000366 allAppsIconSize = iconSize;
367 allAppsIconTextSize = iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000368 }
369
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400370 if (devicePaddingId != 0) {
371 devicePaddings = new DevicePaddings(context, devicePaddingId);
372 }
373
Sunny Goyalae190ff2020-04-14 00:19:01 +0000374 // If the partner customization apk contains any grid overrides, apply them
375 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700376 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700377
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000378 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700379 defaultWallpaperSize = new Point(displayInfo.currentSize);
380 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000381 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Alex Chau1c883d82021-12-01 18:43:10 +0000382 .setUseTwoPanels(deviceType == TYPE_MULTI_DISPLAY)
Alex Chau6ed408f2022-03-04 12:58:05 +0000383 .setWindowBounds(bounds)
384 .build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700385
Sunny Goyal19ff7282021-04-22 10:12:54 -0700386 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
387 int displayWidth = bounds.bounds.width();
388 int displayHeight = bounds.bounds.height();
389 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700390
Sunny Goyal19ff7282021-04-22 10:12:54 -0700391 // We need to ensure that there is enough extra space in the wallpaper
392 // for the intended parallax effects
393 float parallaxFactor =
394 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
395 ? 2
396 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
397 defaultWallpaperSize.x =
398 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700399 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000400 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700401
402 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
403 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700404 }
405
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700406 public void addOnChangeListener(OnIDPChangeListener listener) {
407 mChangeListeners.add(listener);
408 }
409
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800410 public void removeOnChangeListener(OnIDPChangeListener listener) {
411 mChangeListeners.remove(listener);
412 }
413
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800414
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800415 public void setCurrentGrid(Context context, String gridName) {
416 Context appContext = context.getApplicationContext();
417 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700418 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800419 }
420
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700421 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700422 return new Object[]{
Alex Chau9fee3fd2021-12-01 18:43:10 +0000423 numColumns, numRows, numSearchContainerColumns, numDatabaseHotseatIcons,
424 iconBitmapSize, fillResIconDpi, numDatabaseAllAppsColumns, dbFile};
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700425 }
426
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700427 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700428 Object[] oldState = toModelState();
429
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700430 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700431 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700432 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700433
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700434 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700435 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700436 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700437 }
438 }
439
Alex Chau1c883d82021-12-01 18:43:10 +0000440 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context,
441 String gridName, @DeviceType int deviceType, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800442 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100443
Alex Chau1c883d82021-12-01 18:43:10 +0000444 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700445 final int depth = parser.getDepth();
446 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700447 while (((type = parser.next()) != XmlPullParser.END_TAG ||
448 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800449 if ((type == XmlPullParser.START_TAG)
450 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800451
Alex Chau1c883d82021-12-01 18:43:10 +0000452 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser),
453 deviceType);
Alex Chau238aaee2021-10-06 16:15:24 +0100454 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100455 final int displayDepth = parser.getDepth();
456 while (((type = parser.next()) != XmlPullParser.END_TAG
457 || parser.getDepth() > displayDepth)
458 && type != XmlPullParser.END_DOCUMENT) {
459 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
460 parser.getName())) {
461 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100462 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100463 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800464 }
465 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700466 }
467 }
sfufa@google.comde013292021-09-21 18:22:44 -0700468 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700469 throw new RuntimeException(e);
470 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800471
Sunny Goyalae190ff2020-04-14 00:19:01 +0000472 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800473 if (!TextUtils.isEmpty(gridName)) {
474 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100475 if (gridName.equals(option.grid.name)
476 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000477 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800478 }
479 }
480 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000481 if (filteredProfiles.isEmpty()) {
482 // No grid found, use the default options
483 for (DisplayOption option : profiles) {
484 if (option.canBeDefault) {
485 filteredProfiles.add(option);
486 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800487 }
488 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000489 if (filteredProfiles.isEmpty()) {
490 throw new RuntimeException("No display option with canBeDefault=true");
491 }
492 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700493 }
494
Thales Lima7ec83822021-08-05 13:32:10 +0100495 /**
496 * @return all the grid options that can be shown on the device
497 */
498 public List<GridOption> parseAllGridOptions(Context context) {
499 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100500
Alex Chau1c883d82021-12-01 18:43:10 +0000501 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100502 final int depth = parser.getDepth();
503 int type;
504 while (((type = parser.next()) != XmlPullParser.END_TAG
505 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
506 if ((type == XmlPullParser.START_TAG)
507 && GridOption.TAG_NAME.equals(parser.getName())) {
508 GridOption option =
Alex Chau1c883d82021-12-01 18:43:10 +0000509 new GridOption(context, Xml.asAttributeSet(parser), deviceType);
Thales Lima7ec83822021-08-05 13:32:10 +0100510 if (option.isEnabled) {
511 result.add(option);
512 }
513 }
514 }
515 } catch (IOException | XmlPullParserException e) {
516 Log.e(TAG, "Error parsing device profile", e);
517 return Collections.emptyList();
518 }
519 return result;
520 }
521
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700522 private int getLauncherIconDensity(int requiredSize) {
523 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700524 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700525 DisplayMetrics.DENSITY_LOW,
526 DisplayMetrics.DENSITY_MEDIUM,
527 DisplayMetrics.DENSITY_TV,
528 DisplayMetrics.DENSITY_HIGH,
529 DisplayMetrics.DENSITY_XHIGH,
530 DisplayMetrics.DENSITY_XXHIGH,
531 DisplayMetrics.DENSITY_XXXHIGH
532 };
533
534 int density = DisplayMetrics.DENSITY_XXXHIGH;
535 for (int i = densityBuckets.length - 1; i >= 0; i--) {
536 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
537 / DisplayMetrics.DENSITY_DEFAULT;
538 if (expectedSize >= requiredSize) {
539 density = densityBuckets[i];
540 }
541 }
542
543 return density;
544 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700545
Adam Cohen2e6da152015-05-06 11:42:25 -0700546 /**
547 * Apply any Partner customization grid overrides.
548 *
549 * Currently we support: all apps row / column count.
550 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700551 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
552 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700553 if (p != null) {
554 p.applyInvariantDeviceProfileOverrides(this, dm);
555 }
556 }
557
Sunny Goyal415f1732018-11-29 10:33:47 -0800558 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700559 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700560 }
561
Sunny Goyal19ff7282021-04-22 10:12:54 -0700562 private static DisplayOption invDistWeightedInterpolate(
Alex Chau1c883d82021-12-01 18:43:10 +0000563 Info displayInfo, ArrayList<DisplayOption> points, @DeviceType int deviceType) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700564 int minWidthPx = Integer.MAX_VALUE;
565 int minHeightPx = Integer.MAX_VALUE;
566 for (WindowBounds bounds : displayInfo.supportedBounds) {
567 boolean isTablet = displayInfo.isTablet(bounds);
Alex Chau1c883d82021-12-01 18:43:10 +0000568 if (isTablet && deviceType == TYPE_MULTI_DISPLAY) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700569 // For split displays, take half width per page
570 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
571 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700572
Sunny Goyal19ff7282021-04-22 10:12:54 -0700573 } else if (!isTablet && bounds.isLandscape()) {
574 // We will use transposed layout in this case
575 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
576 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
577 } else {
578 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
579 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
580 }
581 }
582
583 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
584 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700585
586 // Sort the profiles based on the closeness to the device size
587 Collections.sort(points, (a, b) ->
588 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
589 dist(width, height, b.minWidthDps, b.minHeightDps)));
590
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700591 DisplayOption closestPoint = points.get(0);
592 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700593 float weights = 0;
594
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700595 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
596 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700597 }
598
599 DisplayOption out = new DisplayOption(closestOption);
600 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700601 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700602 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
603 weights += w;
604 out.add(new DisplayOption().add(p).multiply(w));
605 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700606 out.multiply(1.0f / weights);
607
Thales Lima6e0005a2021-10-27 15:53:41 +0100608 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700609 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100610 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
611 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700612 }
613
614 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700615 }
616
Sunny Goyal27835952017-01-13 12:15:53 -0800617 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700618 Resources res = context.getResources();
619 Configuration config = context.getResources().getConfiguration();
620
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400621 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
622 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800623 return getBestMatch(screenWidth, screenHeight,
624 WindowManagerProxy.INSTANCE.get(context).getRotation(context));
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400625 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700626
Sunny Goyal187b16c2022-03-01 16:53:23 -0800627 /**
628 * Returns the device profile matching the provided screen configuration
629 */
630 public DeviceProfile getBestMatch(float screenWidth, float screenHeight, int rotation) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700631 DeviceProfile bestMatch = supportedProfiles.get(0);
632 float minDiff = Float.MAX_VALUE;
633
634 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400635 float diff = Math.abs(profile.widthPx - screenWidth)
636 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700637 if (diff < minDiff) {
638 minDiff = diff;
639 bestMatch = profile;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800640 } else if (diff == minDiff && profile.rotationHint == rotation) {
641 bestMatch = profile;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700642 }
643 }
644 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800645 }
646
Sunny Goyal415f1732018-11-29 10:33:47 -0800647 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700648 float d = dist(x0, y0, x1, y1);
649 if (Float.compare(d, 0f) == 0) {
650 return Float.POSITIVE_INFINITY;
651 }
652 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
653 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700654
655 /**
656 * As a ratio of screen height, the total distance we want the parallax effect to span
657 * horizontally
658 */
659 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
660 float aspectRatio = width / (float) height;
661
662 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
663 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
664 // We will use these two data points to extrapolate how much the wallpaper parallax effect
665 // to span (ie travel) at any aspect ratio:
666
sfufa@google.comde013292021-09-21 18:22:44 -0700667 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
668 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700669 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
670 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
671
672 // To find out the desired width at different aspect ratios, we use the following two
673 // formulas, where the coefficient on x is the aspect ratio (width/height):
674 // (16/10)x + y = 1.5
675 // (10/16)x + y = 1.2
676 // We solve for x and y and end up with a final formula:
677 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700678 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
679 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700680 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
681 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
682 return x * aspectRatio + y;
683 }
684
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700685 public interface OnIDPChangeListener {
686
Sunny Goyalb47172b2021-05-03 19:59:51 -0700687 /**
688 * Called when the device provide changes
689 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700690 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700691 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800692
693
Sunny Goyaleff44f32019-01-09 17:29:49 -0800694 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800695
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800696 public static final String TAG_NAME = "grid-option";
697
Alex Chau1c883d82021-12-01 18:43:10 +0000698 private static final int DEVICE_CATEGORY_PHONE = 1 << 0;
699 private static final int DEVICE_CATEGORY_TABLET = 1 << 1;
700 private static final int DEVICE_CATEGORY_MULTI_DISPLAY = 1 << 2;
701 private static final int DEVICE_CATEGORY_ALL =
702 DEVICE_CATEGORY_PHONE | DEVICE_CATEGORY_TABLET | DEVICE_CATEGORY_MULTI_DISPLAY;
703
Sunny Goyaleff44f32019-01-09 17:29:49 -0800704 public final String name;
705 public final int numRows;
706 public final int numColumns;
Alex Chau9fee3fd2021-12-01 18:43:10 +0000707 public final int numSearchContainerColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100708 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800709
710 private final int numFolderRows;
711 private final int numFolderColumns;
712
Sunny Goyal19ff7282021-04-22 10:12:54 -0700713 private final int numAllAppsColumns;
714 private final int numDatabaseAllAppsColumns;
715 private final int numHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000716 private final int numShrunkenHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700717 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800718
Tracy Zhou7df93d22020-01-27 13:44:06 -0800719 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000720
Sunny Goyal415f1732018-11-29 10:33:47 -0800721 private final int defaultLayoutId;
722 private final int demoModeLayoutId;
723
Jon Mirandae126d722021-02-25 10:45:20 -0500724 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400725 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500726
Sunny Goyal5bc18462019-01-07 15:13:39 -0800727 private final SparseArray<TypedValue> extraAttrs;
728
Alex Chau1c883d82021-12-01 18:43:10 +0000729 public GridOption(Context context, AttributeSet attrs, @DeviceType int deviceType) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800730 TypedArray a = context.obtainStyledAttributes(
731 attrs, R.styleable.GridDisplayOption);
732 name = a.getString(R.styleable.GridDisplayOption_name);
733 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
734 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
Alex Chau9fee3fd2021-12-01 18:43:10 +0000735 numSearchContainerColumns = a.getInt(
736 R.styleable.GridDisplayOption_numSearchContainerColumns, numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800737
Tracy Zhou7df93d22020-01-27 13:44:06 -0800738 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1c883d82021-12-01 18:43:10 +0000739 defaultLayoutId = a.getResourceId(deviceType == TYPE_MULTI_DISPLAY && a.hasValue(
Alex Chau1e448462021-08-13 21:48:35 +0100740 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
741 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
742 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800743 demoModeLayoutId = a.getResourceId(
744 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700745
746 numAllAppsColumns = a.getInt(
747 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
748 numDatabaseAllAppsColumns = a.getInt(
749 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
750
751 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800752 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Alex Chau6ed408f2022-03-04 12:58:05 +0000753 numShrunkenHotseatIcons = a.getInt(
754 R.styleable.GridDisplayOption_numShrunkenHotseatIcons, numHotseatIcons / 2);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700755 numDatabaseHotseatIcons = a.getInt(
756 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
757
Sunny Goyal415f1732018-11-29 10:33:47 -0800758 numFolderRows = a.getInt(
759 R.styleable.GridDisplayOption_numFolderRows, numRows);
760 numFolderColumns = a.getInt(
761 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700762
Jon Mirandae126d722021-02-25 10:45:20 -0500763 isScalable = a.getBoolean(
764 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400765 devicePaddingId = a.getResourceId(
766 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500767
Alex Chau1c883d82021-12-01 18:43:10 +0000768 int deviceCategory = a.getInt(R.styleable.GridDisplayOption_deviceCategory,
769 DEVICE_CATEGORY_ALL);
770 isEnabled = (deviceType == TYPE_PHONE
771 && ((deviceCategory & DEVICE_CATEGORY_PHONE) == DEVICE_CATEGORY_PHONE))
772 || (deviceType == TYPE_TABLET
773 && ((deviceCategory & DEVICE_CATEGORY_TABLET) == DEVICE_CATEGORY_TABLET))
774 || (deviceType == TYPE_MULTI_DISPLAY
775 && ((deviceCategory & DEVICE_CATEGORY_MULTI_DISPLAY)
776 == DEVICE_CATEGORY_MULTI_DISPLAY));
Thales Lima7ec83822021-08-05 13:32:10 +0100777
Sunny Goyal415f1732018-11-29 10:33:47 -0800778 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800779 extraAttrs = Themes.createValueMap(context, attrs,
780 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800781 }
782 }
783
Sunny Goyal19ff7282021-04-22 10:12:54 -0700784 @VisibleForTesting
785 static final class DisplayOption {
786
787 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800788
Sunny Goyal415f1732018-11-29 10:33:47 -0800789 private final float minWidthDps;
790 private final float minHeightDps;
791 private final boolean canBeDefault;
792
Thales Lima83bedbf2021-10-05 17:47:39 +0100793 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100794
795 private float folderBorderSpace;
796 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100797 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Lima6e7f36c2022-01-04 12:14:11 +0000798 private final float[] hotseatBorderSpaces = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100799
Thales Lima78d00ad2021-09-30 11:29:06 +0100800 private final float[] iconSizes = new float[COUNT_SIZES];
801 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800802
Thales Limab7ef5692022-03-10 10:32:36 +0000803 private final PointF[] allAppsCellSize = new PointF[COUNT_SIZES];
Thales Limabb7d3882021-12-22 12:56:29 +0000804 private final float[] allAppsIconSizes = new float[COUNT_SIZES];
805 private final float[] allAppsIconTextSizes = new float[COUNT_SIZES];
806 private final PointF[] allAppsBorderSpaces = new PointF[COUNT_SIZES];
807
Thales Lima1de4d552021-10-13 16:13:25 +0100808 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800809 this.grid = grid;
810
Thales Lima1de4d552021-10-13 16:13:25 +0100811 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800812
Sunny Goyal415f1732018-11-29 10:33:47 -0800813 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
814 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700815
Thales Lima1de4d552021-10-13 16:13:25 +0100816 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800817
Thales Lima83bedbf2021-10-05 17:47:39 +0100818 float x;
819 float y;
820
Thales Lima85c942f2021-12-31 13:04:20 +0000821 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidth, 0);
822 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeight, 0);
Thales Lima83bedbf2021-10-05 17:47:39 +0100823 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
Thales Limadd027342022-01-07 12:54:37 +0000824
825 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthLandscape,
826 minCellSize[INDEX_DEFAULT].x);
827 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightLandscape,
828 minCellSize[INDEX_DEFAULT].y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100829 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
Thales Lima83bedbf2021-10-05 17:47:39 +0100830
Thales Lima85c942f2021-12-31 13:04:20 +0000831 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100832 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000833 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100834 minCellSize[INDEX_DEFAULT].y);
835 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
836
Thales Lima85c942f2021-12-31 13:04:20 +0000837 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100838 minCellSize[INDEX_DEFAULT].x);
Thales Lima85c942f2021-12-31 13:04:20 +0000839 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100840 minCellSize[INDEX_DEFAULT].y);
841 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100842
Thales Lima85c942f2021-12-31 13:04:20 +0000843 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpace, 0);
Thales Lima44cc3a22022-03-18 14:28:24 +0000844 float borderSpaceLandscape = a.getFloat(
845 R.styleable.ProfileDisplayOption_borderSpaceLandscape, borderSpace);
Thales Lima85c942f2021-12-31 13:04:20 +0000846 float borderSpaceTwoPanelPortrait = a.getFloat(
847 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortrait, borderSpace);
848 float borderSpaceTwoPanelLandscape = a.getFloat(
849 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscape, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100850
Thales Lima85c942f2021-12-31 13:04:20 +0000851 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontal, borderSpace);
852 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVertical, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100853 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
Thales Lima44cc3a22022-03-18 14:28:24 +0000854
855 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeHorizontal,
856 borderSpaceLandscape);
857 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceLandscapeVertical,
858 borderSpaceLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100859 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
860
861 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000862 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitHorizontal,
863 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100864 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000865 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelPortraitVertical,
866 borderSpaceTwoPanelPortrait);
Thales Lima78d00ad2021-09-30 11:29:06 +0100867 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
868
869 x = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000870 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeHorizontal,
871 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100872 y = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000873 R.styleable.ProfileDisplayOption_borderSpaceTwoPanelLandscapeVertical,
874 borderSpaceTwoPanelLandscape);
Thales Lima78d00ad2021-09-30 11:29:06 +0100875 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
876
Thales Limabb7d3882021-12-22 12:56:29 +0000877 folderBorderSpace = borderSpace;
878
Thales Limab7ef5692022-03-10 10:32:36 +0000879 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidth,
880 minCellSize[INDEX_DEFAULT].x);
881 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeight,
882 minCellSize[INDEX_DEFAULT].y);
883 allAppsCellSize[INDEX_DEFAULT] = new PointF(x, y);
884
885 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthLandscape,
886 allAppsCellSize[INDEX_DEFAULT].x);
887 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightLandscape,
888 allAppsCellSize[INDEX_DEFAULT].y);
889 allAppsCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
890
891 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelPortrait,
892 allAppsCellSize[INDEX_DEFAULT].x);
893 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelPortrait,
894 allAppsCellSize[INDEX_DEFAULT].y);
895 allAppsCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
896
897 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellWidthTwoPanelLandscape,
898 allAppsCellSize[INDEX_DEFAULT].x);
899 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellHeightTwoPanelLandscape,
900 allAppsCellSize[INDEX_DEFAULT].y);
901 allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
902
Thales Lima080d8902022-03-28 15:30:29 +0100903 float allAppsBorderSpace = a.getFloat(
904 R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace);
905 float allAppsBorderSpaceLandscape = a.getFloat(
906 R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape,
907 allAppsBorderSpace);
908 float allAppsBorderSpaceTwoPanelPortrait = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000909 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait,
Thales Lima080d8902022-03-28 15:30:29 +0100910 allAppsBorderSpace);
911 float allAppsBorderSpaceTwoPanelLandscape = a.getFloat(
Thales Limabb7d3882021-12-22 12:56:29 +0000912 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape,
Thales Lima080d8902022-03-28 15:30:29 +0100913 allAppsBorderSpace);
914
915 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal,
916 allAppsBorderSpace);
917 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical,
918 allAppsBorderSpace);
919 allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y);
920
921 x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal,
922 allAppsBorderSpaceLandscape);
923 y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical,
924 allAppsBorderSpaceLandscape);
925 allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
926
927 x = a.getFloat(
928 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal,
929 allAppsBorderSpaceTwoPanelPortrait);
930 y = a.getFloat(
931 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical,
932 allAppsBorderSpaceTwoPanelPortrait);
933 allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
934
935 x = a.getFloat(
936 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal,
937 allAppsBorderSpaceTwoPanelLandscape);
938 y = a.getFloat(
939 R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical,
940 allAppsBorderSpaceTwoPanelLandscape);
Thales Limabb7d3882021-12-22 12:56:29 +0000941 allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Jon Mirandae126d722021-02-25 10:45:20 -0500942
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700943 iconSizes[INDEX_DEFAULT] =
944 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
945 iconSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000946 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700947 iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700948 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +0000949 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700950 iconSizes[INDEX_DEFAULT]);
951 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000952 a.getFloat(R.styleable.ProfileDisplayOption_iconSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100953 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700954
Thales Limabb7d3882021-12-22 12:56:29 +0000955 allAppsIconSizes[INDEX_DEFAULT] = a.getFloat(
956 R.styleable.ProfileDisplayOption_allAppsIconSize, iconSizes[INDEX_DEFAULT]);
957 allAppsIconSizes[INDEX_LANDSCAPE] = allAppsIconSizes[INDEX_DEFAULT];
958 allAppsIconSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
959 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelPortrait,
960 allAppsIconSizes[INDEX_DEFAULT]);
961 allAppsIconSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
962 R.styleable.ProfileDisplayOption_allAppsIconSizeTwoPanelLandscape,
963 allAppsIconSizes[INDEX_DEFAULT]);
964
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700965 textSizes[INDEX_DEFAULT] =
966 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
967 textSizes[INDEX_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000968 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeLandscape,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700969 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700970 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
Thales Lima85c942f2021-12-31 13:04:20 +0000971 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelPortrait,
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700972 textSizes[INDEX_DEFAULT]);
973 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
Thales Lima85c942f2021-12-31 13:04:20 +0000974 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSizeTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100975 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700976
Thales Limabb7d3882021-12-22 12:56:29 +0000977 allAppsIconTextSizes[INDEX_DEFAULT] = a.getFloat(
978 R.styleable.ProfileDisplayOption_allAppsIconTextSize, textSizes[INDEX_DEFAULT]);
979 allAppsIconTextSizes[INDEX_LANDSCAPE] = allAppsIconTextSizes[INDEX_DEFAULT];
980 allAppsIconTextSizes[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
981 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelPortrait,
982 allAppsIconTextSizes[INDEX_DEFAULT]);
983 allAppsIconTextSizes[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
984 R.styleable.ProfileDisplayOption_allAppsIconTextSizeTwoPanelLandscape,
985 allAppsIconTextSizes[INDEX_DEFAULT]);
986
Thales Lima83bedbf2021-10-05 17:47:39 +0100987 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
988 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
Thales Limadd027342022-01-07 12:54:37 +0000989 horizontalMargin[INDEX_LANDSCAPE] = a.getFloat(
990 R.styleable.ProfileDisplayOption_horizontalMarginLandscape,
991 horizontalMargin[INDEX_DEFAULT]);
Thales Lima83bedbf2021-10-05 17:47:39 +0100992 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000993 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelLandscape,
Thales Lima83bedbf2021-10-05 17:47:39 +0100994 horizontalMargin[INDEX_DEFAULT]);
995 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Lima85c942f2021-12-31 13:04:20 +0000996 R.styleable.ProfileDisplayOption_horizontalMarginTwoPanelPortrait,
Thales Lima83bedbf2021-10-05 17:47:39 +0100997 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +0100998
Thales Lima6e7f36c2022-01-04 12:14:11 +0000999 hotseatBorderSpaces[INDEX_DEFAULT] = a.getFloat(
1000 R.styleable.ProfileDisplayOption_hotseatBorderSpace, borderSpace);
Thales Limadd027342022-01-07 12:54:37 +00001001 hotseatBorderSpaces[INDEX_LANDSCAPE] = a.getFloat(
1002 R.styleable.ProfileDisplayOption_hotseatBorderSpaceLandscape,
1003 hotseatBorderSpaces[INDEX_DEFAULT]);
Thales Lima6e7f36c2022-01-04 12:14:11 +00001004 hotseatBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
1005 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelLandscape,
1006 hotseatBorderSpaces[INDEX_DEFAULT]);
1007 hotseatBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
1008 R.styleable.ProfileDisplayOption_hotseatBorderSpaceTwoPanelPortrait,
1009 hotseatBorderSpaces[INDEX_DEFAULT]);
1010
Sunny Goyal415f1732018-11-29 10:33:47 -08001011 a.recycle();
1012 }
1013
1014 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001015 this(null);
1016 }
1017
1018 DisplayOption(GridOption grid) {
1019 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -08001020 minWidthDps = 0;
1021 minHeightDps = 0;
1022 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +01001023 for (int i = 0; i < COUNT_SIZES; i++) {
1024 iconSizes[i] = 0;
1025 textSizes[i] = 0;
1026 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +01001027 minCellSize[i] = new PointF();
Thales Limab7ef5692022-03-10 10:32:36 +00001028 allAppsCellSize[i] = new PointF();
Thales Limabb7d3882021-12-22 12:56:29 +00001029 allAppsIconSizes[i] = 0;
1030 allAppsIconTextSizes[i] = 0;
1031 allAppsBorderSpaces[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +01001032 }
Sunny Goyal415f1732018-11-29 10:33:47 -08001033 }
1034
1035 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001036 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001037 iconSizes[i] *= w;
1038 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +01001039 borderSpaces[i].x *= w;
1040 borderSpaces[i].y *= w;
1041 minCellSize[i].x *= w;
1042 minCellSize[i].y *= w;
1043 horizontalMargin[i] *= w;
Thales Lima6e7f36c2022-01-04 12:14:11 +00001044 hotseatBorderSpaces[i] *= w;
Thales Limab7ef5692022-03-10 10:32:36 +00001045 allAppsCellSize[i].x *= w;
1046 allAppsCellSize[i].y *= w;
Thales Limabb7d3882021-12-22 12:56:29 +00001047 allAppsIconSizes[i] *= w;
1048 allAppsIconTextSizes[i] *= w;
1049 allAppsBorderSpaces[i].x *= w;
1050 allAppsBorderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001051 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001052
1053 folderBorderSpace *= w;
1054
Sunny Goyal415f1732018-11-29 10:33:47 -08001055 return this;
1056 }
1057
1058 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +01001059 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001060 iconSizes[i] += p.iconSizes[i];
1061 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +01001062 borderSpaces[i].x += p.borderSpaces[i].x;
1063 borderSpaces[i].y += p.borderSpaces[i].y;
1064 minCellSize[i].x += p.minCellSize[i].x;
1065 minCellSize[i].y += p.minCellSize[i].y;
1066 horizontalMargin[i] += p.horizontalMargin[i];
Thales Lima6e7f36c2022-01-04 12:14:11 +00001067 hotseatBorderSpaces[i] += p.hotseatBorderSpaces[i];
Thales Limab7ef5692022-03-10 10:32:36 +00001068 allAppsCellSize[i].x += p.allAppsCellSize[i].x;
1069 allAppsCellSize[i].y += p.allAppsCellSize[i].y;
Thales Limabb7d3882021-12-22 12:56:29 +00001070 allAppsIconSizes[i] += p.allAppsIconSizes[i];
1071 allAppsIconTextSizes[i] += p.allAppsIconTextSizes[i];
1072 allAppsBorderSpaces[i].x += p.allAppsBorderSpaces[i].x;
1073 allAppsBorderSpaces[i].y += p.allAppsBorderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -07001074 }
Thales Lima78d00ad2021-09-30 11:29:06 +01001075
1076 folderBorderSpace += p.folderBorderSpace;
1077
Sunny Goyal415f1732018-11-29 10:33:47 -08001078 return this;
1079 }
1080 }
Sunny Goyalae190ff2020-04-14 00:19:01 +00001081}