blob: cfc14b6dd6dd7b3e13fb7383befbf53489b8b165 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070045import androidx.annotation.Nullable;
46import androidx.annotation.VisibleForTesting;
47
Sunny Goyal68031ca2021-08-02 12:23:44 -070048import com.android.launcher3.model.DeviceGridState;
Sunny Goyalfd58da62020-08-11 12:06:49 -070049import com.android.launcher3.util.DisplayController;
50import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080051import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070052import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080053import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070054import com.android.launcher3.util.WindowBounds;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070055
Sunny Goyal819e1932016-07-07 16:43:58 -070056import org.xmlpull.v1.XmlPullParser;
57import org.xmlpull.v1.XmlPullParserException;
58
59import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070060import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070061import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080062import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070063import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070064
65public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070066
Hyunyoung Songc55a3502018-12-04 15:43:16 -080067 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070068 // We do not need any synchronization for this variable as its only written on UI thread.
69 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070070 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070071
Sunny Goyal19ff7282021-04-22 10:12:54 -070072 private static final int DEFAULT_TRUE = -1;
73 private static final int DEFAULT_SPLIT_DISPLAY = 2;
Thales Lima7ec83822021-08-05 13:32:10 +010074 private static final int GRID_ENABLED_ALL_DISPLAYS = 0;
75 private static final int GRID_ENABLED_SINGLE_DISPLAY = 1;
Sunny Goyal19ff7282021-04-22 10:12:54 -070076
Hyunyoung Songc55a3502018-12-04 15:43:16 -080077 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080078
Sunny Goyal53d7ee42015-05-22 12:25:45 -070079 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
80
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070081 // Constants that affects the interpolation curve between statically defined device profile
82 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080083 private static final float KNEARESTNEIGHBOR = 3;
84 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070085
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070086 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080087 private static final float WEIGHT_EFFICIENT = 100000f;
88
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070089 /**
90 * Number of icons per row and column in the workspace.
91 */
Adam Cohen2e6da152015-05-06 11:42:25 -070092 public int numRows;
93 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070094
95 /**
96 * Number of icons per row and column in the folder.
97 */
Adam Cohen2e6da152015-05-06 11:42:25 -070098 public int numFolderRows;
99 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700100 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700101 public float landscapeIconSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200102 public float twoPanelPortraitIconSize;
103 public float twoPanelLandscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200104 public float landscapeIconTextSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200105 public float twoPanelPortraitIconTextSize;
106 public float twoPanelLandscapeIconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700107 public int iconBitmapSize;
108 public int fillResIconDpi;
109 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000110 public float allAppsIconSize;
111 public float allAppsIconTextSize;
sfufa@google.comde013292021-09-21 18:22:44 -0700112 public float allAppsCellSpacing;
Thales Lima7ec83822021-08-05 13:32:10 +0100113 public boolean isSplitDisplay;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700114
Jon Mirandae126d722021-02-25 10:45:20 -0500115 public float minCellHeight;
116 public float minCellWidth;
Thales Limad1df5fc2021-09-03 18:37:19 +0100117 public float twoPanelPortraitMinCellHeightDps;
118 public float twoPanelPortraitMinCellWidthDps;
119 public float twoPanelLandscapeMinCellHeightDps;
120 public float twoPanelLandscapeMinCellWidthDps;
Thales Lima78d00ad2021-09-30 11:29:06 +0100121
122 public PointF borderSpace;
123 public PointF twoPanelPortraitBorderSpace;
124 public PointF twoPanelLandscapeBorderSpace;
125 public float folderBorderSpace;
126
Thales Limad90faab2021-09-21 17:18:47 +0100127 public float horizontalMargin;
128 public float twoPanelLandscapeHorizontalMargin;
129 public float twoPanelPortraitHorizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500130
Sunny Goyal5bc18462019-01-07 15:13:39 -0800131 private SparseArray<TypedValue> mExtraAttrs;
132
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700133 /**
134 * Number of icons inside the hotseat area.
135 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700136 protected int numShownHotseatIcons;
137
138 /**
139 * Number of icons inside the hotseat area that is stored in the database. This is greater than
140 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
141 * sizes that share the same DB.
142 */
143 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700144
Jon Miranda6f7e9702019-09-16 14:44:14 -0700145 /**
146 * Number of columns in the all apps list.
147 */
148 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700149 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700150
Jon Mirandae126d722021-02-25 10:45:20 -0500151 /**
152 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
153 */
154 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400155 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500156
Tracy Zhou7df93d22020-01-27 13:44:06 -0800157 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800158 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700159 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700160
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000161 /**
162 * An immutable list of supported profiles.
163 */
164 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700165
sfufa@google.comde013292021-09-21 18:22:44 -0700166 @Nullable
167 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500168
Sunny Goyal6f866092016-03-17 17:04:15 -0700169 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700170 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700171
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700172 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700173
Sunny Goyalf633ef52018-03-13 09:57:05 -0700174 @VisibleForTesting
sfufa@google.comde013292021-09-21 18:22:44 -0700175 public InvariantDeviceProfile() {
176 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700177
Sunny Goyalbbf01842015-10-08 07:41:15 -0700178 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700179 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700180 String gridName = getCurrentGridName(context);
181 String newGridName = initGrid(context, gridName);
182 if (!newGridName.equals(gridName)) {
183 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
184 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700185 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700186
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700187 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100188 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700189 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100190 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700191 }
192 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700193 }
194
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700195 /**
196 * This constructor should NOT have any monitors by design.
197 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800198 public InvariantDeviceProfile(Context context, String gridName) {
199 String newName = initGrid(context, gridName);
200 if (newName == null || !newName.equals(gridName)) {
201 throw new IllegalArgumentException("Unknown grid name");
202 }
203 }
204
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700205 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800206 * This constructor should NOT have any monitors by design.
207 */
208 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700209 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100210 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700211 String gridName = getCurrentGridName(context);
212
213 // Get the display info based on default display and interpolate it to existing display
214 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700215 DisplayController.INSTANCE.get(context).getInfo(),
Sunny Goyal19ff7282021-04-22 10:12:54 -0700216 getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700217
Alex Chau417bd722021-01-26 15:22:18 +0000218 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700219 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700220 myInfo, getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700221
222 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
223 .add(myDisplayOption);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700224 result.iconSizes[DisplayOption.INDEX_DEFAULT] =
225 defaultDisplayOption.iconSizes[DisplayOption.INDEX_DEFAULT];
Thales Lima78d00ad2021-09-30 11:29:06 +0100226 for (int i = 1; i < DisplayOption.COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700227 result.iconSizes[i] = Math.min(
228 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000229 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700230
Jon Mirandae126d722021-02-25 10:45:20 -0500231 result.minCellHeight = defaultDisplayOption.minCellHeight;
232 result.minCellWidth = defaultDisplayOption.minCellWidth;
Thales Lima78d00ad2021-09-30 11:29:06 +0100233 result.borderSpaces[DisplayOption.INDEX_ALL_APPS] =
234 defaultDisplayOption.borderSpaces[DisplayOption.INDEX_ALL_APPS];
Jon Miranda228877d2021-02-09 11:05:00 -0500235
Sunny Goyal19ff7282021-04-22 10:12:54 -0700236 initGrid(context, myInfo, result, false);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800237 }
238
Tracy Zhou42255d22020-03-13 00:38:11 -0700239 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700240 return Utilities.isGridOptionsEnabled(context)
241 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700242 }
243
Sunny Goyaleff44f32019-01-09 17:29:49 -0800244 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700245 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Pat Manning67094bf2021-05-26 12:38:13 +0000246 // Each screen has two profiles (portrait/landscape), so devices with four or more
247 // supported profiles implies two or more internal displays.
248 boolean isSplitDisplay =
249 displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700250
251 ArrayList<DisplayOption> allOptions =
252 getPredefinedDeviceProfiles(context, gridName, isSplitDisplay);
253 DisplayOption displayOption =
254 invDistWeightedInterpolate(displayInfo, allOptions, isSplitDisplay);
255 initGrid(context, displayInfo, displayOption, isSplitDisplay);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700256 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800257 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700258
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700259 private void initGrid(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700260 Context context, Info displayInfo, DisplayOption displayOption,
261 boolean isSplitDisplay) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700262 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700263 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000264 numRows = closestProfile.numRows;
265 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000266 dbFile = closestProfile.dbFile;
267 defaultLayoutId = closestProfile.defaultLayoutId;
268 demoModeLayoutId = closestProfile.demoModeLayoutId;
269 numFolderRows = closestProfile.numFolderRows;
270 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500271 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400272 devicePaddingId = closestProfile.devicePaddingId;
Thales Lima7ec83822021-08-05 13:32:10 +0100273 this.isSplitDisplay = isSplitDisplay;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000274
275 mExtraAttrs = closestProfile.extraAttrs;
276
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700277 iconSize = displayOption.iconSizes[DisplayOption.INDEX_DEFAULT];
278 landscapeIconSize = displayOption.iconSizes[DisplayOption.INDEX_LANDSCAPE];
279 twoPanelPortraitIconSize = displayOption.iconSizes[DisplayOption.INDEX_TWO_PANEL_PORTRAIT];
280 twoPanelLandscapeIconSize =
281 displayOption.iconSizes[DisplayOption.INDEX_TWO_PANEL_LANDSCAPE];
Sunny Goyal35c7b192021-04-20 16:51:10 -0700282 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000283 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
284
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700285 iconTextSize = displayOption.textSizes[DisplayOption.INDEX_DEFAULT];
286 landscapeIconTextSize = displayOption.textSizes[DisplayOption.INDEX_LANDSCAPE];
287 twoPanelPortraitIconTextSize =
288 displayOption.textSizes[DisplayOption.INDEX_TWO_PANEL_PORTRAIT];
289 twoPanelLandscapeIconTextSize =
290 displayOption.textSizes[DisplayOption.INDEX_TWO_PANEL_LANDSCAPE];
291
Jon Mirandae126d722021-02-25 10:45:20 -0500292 minCellHeight = displayOption.minCellHeight;
293 minCellWidth = displayOption.minCellWidth;
Thales Limad1df5fc2021-09-03 18:37:19 +0100294 twoPanelPortraitMinCellHeightDps = displayOption.twoPanelPortraitMinCellHeightDps;
295 twoPanelPortraitMinCellWidthDps = displayOption.twoPanelPortraitMinCellWidthDps;
296 twoPanelLandscapeMinCellHeightDps = displayOption.twoPanelLandscapeMinCellHeightDps;
297 twoPanelLandscapeMinCellWidthDps = displayOption.twoPanelLandscapeMinCellWidthDps;
Thales Lima78d00ad2021-09-30 11:29:06 +0100298
299 borderSpace = displayOption.borderSpaces[DisplayOption.INDEX_DEFAULT];
300 twoPanelPortraitBorderSpace =
301 displayOption.borderSpaces[DisplayOption.INDEX_TWO_PANEL_PORTRAIT];
302 twoPanelLandscapeBorderSpace =
303 displayOption.borderSpaces[DisplayOption.INDEX_TWO_PANEL_LANDSCAPE];
304 allAppsCellSpacing = displayOption.borderSpaces[DisplayOption.INDEX_ALL_APPS].x;
305 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700306
Thales Limad90faab2021-09-21 17:18:47 +0100307 horizontalMargin = displayOption.horizontalMargin;
308 twoPanelLandscapeHorizontalMargin = displayOption.twoPanelLandscapeHorizontalMargin;
309 twoPanelPortraitHorizontalMargin = displayOption.twoPanelPortraitHorizontalMargin;
310
Sunny Goyal19ff7282021-04-22 10:12:54 -0700311 numShownHotseatIcons = closestProfile.numHotseatIcons;
312 numDatabaseHotseatIcons = isSplitDisplay
313 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
314
315 numAllAppsColumns = closestProfile.numAllAppsColumns;
316 numDatabaseAllAppsColumns = isSplitDisplay
317 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500318
Tracy Zhouc6060e62020-04-27 13:05:34 -0700319 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700320 allAppsIconSize = displayOption.iconSizes[DisplayOption.INDEX_ALL_APPS];
321 allAppsIconTextSize = displayOption.textSizes[DisplayOption.INDEX_ALL_APPS];
Sunny Goyalae190ff2020-04-14 00:19:01 +0000322 } else {
323 allAppsIconSize = iconSize;
324 allAppsIconTextSize = iconTextSize;
325 }
326
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400327 if (devicePaddingId != 0) {
328 devicePaddings = new DevicePaddings(context, devicePaddingId);
329 }
330
Sunny Goyalae190ff2020-04-14 00:19:01 +0000331 // If the partner customization apk contains any grid overrides, apply them
332 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700333 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700334
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000335 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700336 defaultWallpaperSize = new Point(displayInfo.currentSize);
337 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000338 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700339 .setUseTwoPanels(isSplitDisplay)
340 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700341
Sunny Goyal19ff7282021-04-22 10:12:54 -0700342 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
343 int displayWidth = bounds.bounds.width();
344 int displayHeight = bounds.bounds.height();
345 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700346
Sunny Goyal19ff7282021-04-22 10:12:54 -0700347 // We need to ensure that there is enough extra space in the wallpaper
348 // for the intended parallax effects
349 float parallaxFactor =
350 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
351 ? 2
352 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
353 defaultWallpaperSize.x =
354 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700355 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000356 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700357
358 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
359 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700360 }
361
Sunny Goyal5bc18462019-01-07 15:13:39 -0800362 @Nullable
363 public TypedValue getAttrValue(int attr) {
364 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
365 }
366
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700367 public void addOnChangeListener(OnIDPChangeListener listener) {
368 mChangeListeners.add(listener);
369 }
370
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800371 public void removeOnChangeListener(OnIDPChangeListener listener) {
372 mChangeListeners.remove(listener);
373 }
374
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800375
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800376 public void setCurrentGrid(Context context, String gridName) {
377 Context appContext = context.getApplicationContext();
378 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700379 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800380 }
381
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700382 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700383 return new Object[]{
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700384 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
385 numDatabaseAllAppsColumns, dbFile};
386 }
387
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700388 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700389 Object[] oldState = toModelState();
390
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700391 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700392 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700393 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700394
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700395 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700396 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700397 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700398 }
399 }
400
Sunny Goyal19ff7282021-04-22 10:12:54 -0700401 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
402 Context context, String gridName, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800403 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700404 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
405 final int depth = parser.getDepth();
406 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700407 while (((type = parser.next()) != XmlPullParser.END_TAG ||
408 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800409 if ((type == XmlPullParser.START_TAG)
410 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800411
Thales Lima7ec83822021-08-05 13:32:10 +0100412 GridOption gridOption =
413 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
414 if (gridOption.isEnabled) {
415 final int displayDepth = parser.getDepth();
416 while (((type = parser.next()) != XmlPullParser.END_TAG
417 || parser.getDepth() > displayDepth)
418 && type != XmlPullParser.END_DOCUMENT) {
419 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
420 parser.getName())) {
421 profiles.add(new DisplayOption(gridOption, context,
422 Xml.asAttributeSet(parser),
423 isSplitDisplay ? DEFAULT_SPLIT_DISPLAY : DEFAULT_TRUE));
424 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800425 }
426 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700427 }
428 }
sfufa@google.comde013292021-09-21 18:22:44 -0700429 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700430 throw new RuntimeException(e);
431 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800432
Sunny Goyalae190ff2020-04-14 00:19:01 +0000433 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800434 if (!TextUtils.isEmpty(gridName)) {
435 for (DisplayOption option : profiles) {
Thales Lima7ec83822021-08-05 13:32:10 +0100436 if (gridName.equals(option.grid.name) && option.grid.isEnabled) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000437 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800438 }
439 }
440 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000441 if (filteredProfiles.isEmpty()) {
442 // No grid found, use the default options
443 for (DisplayOption option : profiles) {
444 if (option.canBeDefault) {
445 filteredProfiles.add(option);
446 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800447 }
448 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000449 if (filteredProfiles.isEmpty()) {
450 throw new RuntimeException("No display option with canBeDefault=true");
451 }
452 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700453 }
454
Thales Lima7ec83822021-08-05 13:32:10 +0100455 /**
456 * @return all the grid options that can be shown on the device
457 */
458 public List<GridOption> parseAllGridOptions(Context context) {
459 List<GridOption> result = new ArrayList<>();
460 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
461 final int depth = parser.getDepth();
462 int type;
463 while (((type = parser.next()) != XmlPullParser.END_TAG
464 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
465 if ((type == XmlPullParser.START_TAG)
466 && GridOption.TAG_NAME.equals(parser.getName())) {
467 GridOption option =
468 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
469 if (option.isEnabled) {
470 result.add(option);
471 }
472 }
473 }
474 } catch (IOException | XmlPullParserException e) {
475 Log.e(TAG, "Error parsing device profile", e);
476 return Collections.emptyList();
477 }
478 return result;
479 }
480
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700481 private int getLauncherIconDensity(int requiredSize) {
482 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700483 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700484 DisplayMetrics.DENSITY_LOW,
485 DisplayMetrics.DENSITY_MEDIUM,
486 DisplayMetrics.DENSITY_TV,
487 DisplayMetrics.DENSITY_HIGH,
488 DisplayMetrics.DENSITY_XHIGH,
489 DisplayMetrics.DENSITY_XXHIGH,
490 DisplayMetrics.DENSITY_XXXHIGH
491 };
492
493 int density = DisplayMetrics.DENSITY_XXXHIGH;
494 for (int i = densityBuckets.length - 1; i >= 0; i--) {
495 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
496 / DisplayMetrics.DENSITY_DEFAULT;
497 if (expectedSize >= requiredSize) {
498 density = densityBuckets[i];
499 }
500 }
501
502 return density;
503 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700504
Adam Cohen2e6da152015-05-06 11:42:25 -0700505 /**
506 * Apply any Partner customization grid overrides.
507 *
508 * Currently we support: all apps row / column count.
509 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700510 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
511 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700512 if (p != null) {
513 p.applyInvariantDeviceProfileOverrides(this, dm);
514 }
515 }
516
Sunny Goyal415f1732018-11-29 10:33:47 -0800517 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700518 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700519 }
520
Sunny Goyal19ff7282021-04-22 10:12:54 -0700521 private static DisplayOption invDistWeightedInterpolate(
522 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
523 int minWidthPx = Integer.MAX_VALUE;
524 int minHeightPx = Integer.MAX_VALUE;
525 for (WindowBounds bounds : displayInfo.supportedBounds) {
526 boolean isTablet = displayInfo.isTablet(bounds);
527 if (isTablet && isSplitDisplay) {
528 // For split displays, take half width per page
529 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
530 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700531
Sunny Goyal19ff7282021-04-22 10:12:54 -0700532 } else if (!isTablet && bounds.isLandscape()) {
533 // We will use transposed layout in this case
534 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
535 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
536 } else {
537 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
538 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
539 }
540 }
541
542 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
543 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700544
545 // Sort the profiles based on the closeness to the device size
546 Collections.sort(points, (a, b) ->
547 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
548 dist(width, height, b.minWidthDps, b.minHeightDps)));
549
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700550 DisplayOption closestPoint = points.get(0);
551 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700552 float weights = 0;
553
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700554 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
555 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700556 }
557
558 DisplayOption out = new DisplayOption(closestOption);
559 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700560 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700561 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
562 weights += w;
563 out.add(new DisplayOption().add(p).multiply(w));
564 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700565 out.multiply(1.0f / weights);
566
567 // Since the bitmaps are persisted, ensure that the default bitmap size is same as
568 // predefined size to avoid cache invalidation
569 out.iconSizes[DisplayOption.INDEX_DEFAULT] =
570 closestPoint.iconSizes[DisplayOption.INDEX_DEFAULT];
Thales Lima78d00ad2021-09-30 11:29:06 +0100571 for (int i = DisplayOption.INDEX_DEFAULT + 1; i < DisplayOption.COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700572 out.iconSizes[i] = Math.min(out.iconSizes[i],
573 out.iconSizes[DisplayOption.INDEX_DEFAULT]);
574 }
575
576 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700577 }
578
Sunny Goyal27835952017-01-13 12:15:53 -0800579 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700580 Resources res = context.getResources();
581 Configuration config = context.getResources().getConfiguration();
582
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400583 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
584 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
585 return getBestMatch(screenWidth, screenHeight);
586 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700587
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400588 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700589 DeviceProfile bestMatch = supportedProfiles.get(0);
590 float minDiff = Float.MAX_VALUE;
591
592 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400593 float diff = Math.abs(profile.widthPx - screenWidth)
594 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700595 if (diff < minDiff) {
596 minDiff = diff;
597 bestMatch = profile;
598 }
599 }
600 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800601 }
602
Sunny Goyal415f1732018-11-29 10:33:47 -0800603 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700604 float d = dist(x0, y0, x1, y1);
605 if (Float.compare(d, 0f) == 0) {
606 return Float.POSITIVE_INFINITY;
607 }
608 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
609 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700610
611 /**
612 * As a ratio of screen height, the total distance we want the parallax effect to span
613 * horizontally
614 */
615 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
616 float aspectRatio = width / (float) height;
617
618 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
619 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
620 // We will use these two data points to extrapolate how much the wallpaper parallax effect
621 // to span (ie travel) at any aspect ratio:
622
sfufa@google.comde013292021-09-21 18:22:44 -0700623 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
624 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700625 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
626 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
627
628 // To find out the desired width at different aspect ratios, we use the following two
629 // formulas, where the coefficient on x is the aspect ratio (width/height):
630 // (16/10)x + y = 1.5
631 // (10/16)x + y = 1.2
632 // We solve for x and y and end up with a final formula:
633 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700634 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
635 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700636 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
637 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
638 return x * aspectRatio + y;
639 }
640
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700641 public interface OnIDPChangeListener {
642
Sunny Goyalb47172b2021-05-03 19:59:51 -0700643 /**
644 * Called when the device provide changes
645 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700646 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700647 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800648
649
Sunny Goyaleff44f32019-01-09 17:29:49 -0800650 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800651
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800652 public static final String TAG_NAME = "grid-option";
653
Sunny Goyaleff44f32019-01-09 17:29:49 -0800654 public final String name;
655 public final int numRows;
656 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100657 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800658
659 private final int numFolderRows;
660 private final int numFolderColumns;
661
Sunny Goyal19ff7282021-04-22 10:12:54 -0700662 private final int numAllAppsColumns;
663 private final int numDatabaseAllAppsColumns;
664 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700665 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800666
Tracy Zhou7df93d22020-01-27 13:44:06 -0800667 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000668
Sunny Goyal415f1732018-11-29 10:33:47 -0800669 private final int defaultLayoutId;
670 private final int demoModeLayoutId;
671
Jon Mirandae126d722021-02-25 10:45:20 -0500672 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400673 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500674
Sunny Goyal5bc18462019-01-07 15:13:39 -0800675 private final SparseArray<TypedValue> extraAttrs;
676
Thales Lima7ec83822021-08-05 13:32:10 +0100677 public GridOption(Context context, AttributeSet attrs, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800678 TypedArray a = context.obtainStyledAttributes(
679 attrs, R.styleable.GridDisplayOption);
680 name = a.getString(R.styleable.GridDisplayOption_name);
681 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
682 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
683
Tracy Zhou7df93d22020-01-27 13:44:06 -0800684 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1e448462021-08-13 21:48:35 +0100685 defaultLayoutId = a.getResourceId(isSplitDisplay && a.hasValue(
686 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
687 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
688 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800689 demoModeLayoutId = a.getResourceId(
690 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700691
692 numAllAppsColumns = a.getInt(
693 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
694 numDatabaseAllAppsColumns = a.getInt(
695 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
696
697 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800698 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700699 numDatabaseHotseatIcons = a.getInt(
700 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
701
Sunny Goyal415f1732018-11-29 10:33:47 -0800702 numFolderRows = a.getInt(
703 R.styleable.GridDisplayOption_numFolderRows, numRows);
704 numFolderColumns = a.getInt(
705 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700706
Jon Mirandae126d722021-02-25 10:45:20 -0500707 isScalable = a.getBoolean(
708 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400709 devicePaddingId = a.getResourceId(
710 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500711
Thales Lima7ec83822021-08-05 13:32:10 +0100712 final int enabledInt =
713 a.getInteger(R.styleable.GridDisplayOption_gridEnabled,
714 GRID_ENABLED_ALL_DISPLAYS);
715 isEnabled = enabledInt == GRID_ENABLED_ALL_DISPLAYS
716 || enabledInt == GRID_ENABLED_SINGLE_DISPLAY && !isSplitDisplay;
717
Sunny Goyal415f1732018-11-29 10:33:47 -0800718 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800719 extraAttrs = Themes.createValueMap(context, attrs,
720 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800721 }
722 }
723
Sunny Goyal19ff7282021-04-22 10:12:54 -0700724 @VisibleForTesting
725 static final class DisplayOption {
726
Thales Lima78d00ad2021-09-30 11:29:06 +0100727 static final int COUNT_SIZES = 5;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700728 static final int INDEX_DEFAULT = 0;
729 static final int INDEX_LANDSCAPE = 1;
Thales Lima78d00ad2021-09-30 11:29:06 +0100730 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
731 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
732 static final int INDEX_ALL_APPS = 4;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700733
Sunny Goyal19ff7282021-04-22 10:12:54 -0700734 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800735
Sunny Goyal415f1732018-11-29 10:33:47 -0800736 private final float minWidthDps;
737 private final float minHeightDps;
738 private final boolean canBeDefault;
739
Jon Mirandae126d722021-02-25 10:45:20 -0500740 private float minCellHeight;
741 private float minCellWidth;
Thales Limad1df5fc2021-09-03 18:37:19 +0100742 private float twoPanelPortraitMinCellHeightDps;
743 private float twoPanelPortraitMinCellWidthDps;
744 private float twoPanelLandscapeMinCellHeightDps;
745 private float twoPanelLandscapeMinCellWidthDps;
Thales Lima78d00ad2021-09-30 11:29:06 +0100746
747 private float folderBorderSpace;
748 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Jon Mirandae126d722021-02-25 10:45:20 -0500749
Thales Limad90faab2021-09-21 17:18:47 +0100750 private float horizontalMargin;
751 private float twoPanelLandscapeHorizontalMargin;
752 private float twoPanelPortraitHorizontalMargin;
753
Thales Lima78d00ad2021-09-30 11:29:06 +0100754 private final float[] iconSizes = new float[COUNT_SIZES];
755 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800756
Sunny Goyal19ff7282021-04-22 10:12:54 -0700757 DisplayOption(GridOption grid, Context context, AttributeSet attrs, int defaultFlagValue) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800758 this.grid = grid;
759
760 TypedArray a = context.obtainStyledAttributes(
761 attrs, R.styleable.ProfileDisplayOption);
762
Sunny Goyal415f1732018-11-29 10:33:47 -0800763 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
764 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700765
766 canBeDefault = a.getInt(R.styleable.ProfileDisplayOption_canBeDefault, 0)
767 == defaultFlagValue;
Sunny Goyal415f1732018-11-29 10:33:47 -0800768
Jon Mirandae126d722021-02-25 10:45:20 -0500769 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
770 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
Thales Limad1df5fc2021-09-03 18:37:19 +0100771 twoPanelPortraitMinCellHeightDps = a.getFloat(
772 R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellHeightDps,
773 minCellHeight);
774 twoPanelPortraitMinCellWidthDps = a.getFloat(
775 R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellWidthDps, minCellWidth);
776 twoPanelLandscapeMinCellHeightDps = a.getFloat(
777 R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellHeightDps,
778 twoPanelPortraitMinCellHeightDps);
779 twoPanelLandscapeMinCellWidthDps = a.getFloat(
780 R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellWidthDps,
781 twoPanelPortraitMinCellWidthDps);
Thales Lima78d00ad2021-09-30 11:29:06 +0100782
783 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceDps, 0);
784 float twoPanelPortraitBorderSpaceDps = a.getFloat(
785 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceDps, borderSpace);
786 float twoPanelLandscapeBorderSpaceDps = a.getFloat(
787 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceDps, borderSpace);
788 float x;
789 float y;
790
791 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontalDps, borderSpace);
792 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVerticalDps, borderSpace);
793 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
794 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
795
796 x = a.getFloat(
797 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceHorizontalDps,
798 twoPanelPortraitBorderSpaceDps);
799 y = a.getFloat(
800 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceVerticalDps,
801 twoPanelPortraitBorderSpaceDps);
802 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
803
804 x = a.getFloat(
805 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceHorizontalDps,
806 twoPanelLandscapeBorderSpaceDps);
807 y = a.getFloat(
808 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceVerticalDps,
809 twoPanelLandscapeBorderSpaceDps);
810 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
811
812 x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellSpacingDps,
813 borderSpace);
814 borderSpaces[INDEX_ALL_APPS] = new PointF(x, y);
815 folderBorderSpace = borderSpace;
Jon Mirandae126d722021-02-25 10:45:20 -0500816
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700817 iconSizes[INDEX_DEFAULT] =
818 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
819 iconSizes[INDEX_LANDSCAPE] =
820 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
821 iconSizes[INDEX_DEFAULT]);
822 iconSizes[INDEX_ALL_APPS] =
823 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
824 iconSizes[INDEX_DEFAULT]);
825 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
826 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconSize,
827 iconSizes[INDEX_DEFAULT]);
828 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
829 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconSize,
830 iconSizes[INDEX_LANDSCAPE]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700831
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700832 textSizes[INDEX_DEFAULT] =
833 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
834 textSizes[INDEX_LANDSCAPE] =
835 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconTextSize,
836 textSizes[INDEX_DEFAULT]);
837 textSizes[INDEX_ALL_APPS] =
838 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
839 textSizes[INDEX_DEFAULT]);
840 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
841 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconTextSize,
842 textSizes[INDEX_DEFAULT]);
843 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
844 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconTextSize,
845 textSizes[INDEX_LANDSCAPE]);
846
Thales Limad90faab2021-09-21 17:18:47 +0100847 horizontalMargin = a.getFloat(R.styleable.ProfileDisplayOption_horizontalMargin, 0);
848 twoPanelLandscapeHorizontalMargin = a.getFloat(
849 R.styleable.ProfileDisplayOption_twoPanelLandscapeHorizontalMargin,
850 horizontalMargin);
851 twoPanelPortraitHorizontalMargin = a.getFloat(
852 R.styleable.ProfileDisplayOption_twoPanelPortraitHorizontalMargin,
853 horizontalMargin);
854
Sunny Goyal415f1732018-11-29 10:33:47 -0800855 a.recycle();
856 }
857
858 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700859 this(null);
860 }
861
862 DisplayOption(GridOption grid) {
863 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800864 minWidthDps = 0;
865 minHeightDps = 0;
866 canBeDefault = false;
Jon Mirandae126d722021-02-25 10:45:20 -0500867 minCellHeight = 0;
868 minCellWidth = 0;
Thales Lima78d00ad2021-09-30 11:29:06 +0100869 for (int i = 0; i < COUNT_SIZES; i++) {
870 iconSizes[i] = 0;
871 textSizes[i] = 0;
872 borderSpaces[i] = new PointF();
873 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800874 }
875
876 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100877 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700878 iconSizes[i] *= w;
879 textSizes[i] *= w;
Thales Lima78d00ad2021-09-30 11:29:06 +0100880 borderSpaces[i].x *= w;
881 borderSpaces[i].y *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700882 }
Jon Mirandae126d722021-02-25 10:45:20 -0500883 minCellHeight *= w;
884 minCellWidth *= w;
Thales Limad1df5fc2021-09-03 18:37:19 +0100885 twoPanelPortraitMinCellHeightDps *= w;
886 twoPanelPortraitMinCellWidthDps *= w;
887 twoPanelLandscapeMinCellHeightDps *= w;
888 twoPanelLandscapeMinCellWidthDps *= w;
Thales Lima78d00ad2021-09-30 11:29:06 +0100889
890 folderBorderSpace *= w;
891
Thales Limad90faab2021-09-21 17:18:47 +0100892 horizontalMargin *= w;
893 twoPanelLandscapeHorizontalMargin *= w;
894 twoPanelPortraitHorizontalMargin *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800895 return this;
896 }
897
898 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100899 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700900 iconSizes[i] += p.iconSizes[i];
901 textSizes[i] += p.textSizes[i];
Thales Lima78d00ad2021-09-30 11:29:06 +0100902 borderSpaces[i].x += p.borderSpaces[i].x;
903 borderSpaces[i].y += p.borderSpaces[i].y;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700904 }
Jon Mirandae126d722021-02-25 10:45:20 -0500905 minCellHeight += p.minCellHeight;
906 minCellWidth += p.minCellWidth;
Thales Limad1df5fc2021-09-03 18:37:19 +0100907 twoPanelPortraitMinCellHeightDps += p.twoPanelPortraitMinCellHeightDps;
908 twoPanelPortraitMinCellWidthDps += p.twoPanelPortraitMinCellWidthDps;
909 twoPanelLandscapeMinCellHeightDps += p.twoPanelLandscapeMinCellHeightDps;
910 twoPanelLandscapeMinCellWidthDps += p.twoPanelLandscapeMinCellWidthDps;
Thales Lima78d00ad2021-09-30 11:29:06 +0100911
912 folderBorderSpace += p.folderBorderSpace;
913
Thales Limad90faab2021-09-21 17:18:47 +0100914 horizontalMargin += p.horizontalMargin;
915 twoPanelLandscapeHorizontalMargin += p.twoPanelLandscapeHorizontalMargin;
916 twoPanelPortraitHorizontalMargin += p.twoPanelPortraitHorizontalMargin;
Sunny Goyal415f1732018-11-29 10:33:47 -0800917 return this;
918 }
919 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000920}