blob: 22d393a631c87fd1b279fc1df3ca7aee15605f30 [file] [log] [blame]
Winson Chungb3800242013-10-24 11:01:54 -07001/*
2 * Copyright (C) 2008 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
Kateryna Ivanova71203732023-05-24 15:09:00 +000019import static com.android.app.animation.Interpolators.LINEAR;
Thales Lima12d0eff2022-03-25 17:06:11 +000020import static com.android.launcher3.InvariantDeviceProfile.INDEX_DEFAULT;
21import static com.android.launcher3.InvariantDeviceProfile.INDEX_LANDSCAPE;
22import static com.android.launcher3.InvariantDeviceProfile.INDEX_TWO_PANEL_LANDSCAPE;
23import static com.android.launcher3.InvariantDeviceProfile.INDEX_TWO_PANEL_PORTRAIT;
Jon Miranda58561d42021-03-17 10:51:54 -040024import static com.android.launcher3.Utilities.dpiFromPx;
Jon Mirandaaf3aed02021-05-06 18:28:45 -070025import static com.android.launcher3.Utilities.pxFromSp;
Alex Chauab800f72022-12-13 17:46:06 +000026import static com.android.launcher3.config.FeatureFlags.ENABLE_MULTI_DISPLAY_PARTIAL_DEPTH;
Jon Miranda92c1b5d2021-07-20 13:57:16 -070027import static com.android.launcher3.folder.ClippedFolderIconLayoutRule.ICON_OVERLAP_FACTOR;
Sunny Goyal65190ae2022-08-02 14:16:26 -070028import static com.android.launcher3.icons.GraphicsUtils.getShapePath;
Jon Miranda95f44722023-06-06 19:48:19 -070029import static com.android.launcher3.icons.IconNormalizer.ICON_VISIBLE_AREA_FACTOR;
Thales Limab35faed2022-09-05 16:30:01 -030030import static com.android.launcher3.testing.shared.ResourceUtils.INVALID_RESOURCE_HANDLE;
Pat Manning5f74bfd2022-07-20 12:08:54 +010031import static com.android.launcher3.testing.shared.ResourceUtils.pxFromDp;
Thales Lima7eee74b2023-03-16 16:00:55 -030032import static com.android.launcher3.testing.shared.ResourceUtils.roundPxValueFromFloat;
Jon Miranda228877d2021-02-09 11:05:00 -050033
Sunny Goyal35c7b192021-04-20 16:51:10 -070034import android.annotation.SuppressLint;
Winson Chungb3800242013-10-24 11:01:54 -070035import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070036import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070037import android.content.res.Resources;
Thales Limab35faed2022-09-05 16:30:01 -030038import android.content.res.TypedArray;
Winson Chungb3800242013-10-24 11:01:54 -070039import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080040import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070041import android.graphics.Rect;
Sunny Goyal35c7b192021-04-20 16:51:10 -070042import android.util.DisplayMetrics;
Sunny Goyal65190ae2022-08-02 14:16:26 -070043import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070044import android.view.Surface;
Winson Chungb3800242013-10-24 11:01:54 -070045
Sunny Goyal65190ae2022-08-02 14:16:26 -070046import androidx.annotation.NonNull;
Sihua Mae04aa202022-07-18 12:43:56 -070047import androidx.annotation.Nullable;
Jon Miranda9c478b62023-03-23 21:38:49 -070048import androidx.core.content.res.ResourcesCompat;
Sunny Goyal65190ae2022-08-02 14:16:26 -070049
Sunny Goyalc13403c2016-11-18 23:44:48 -080050import com.android.launcher3.CellLayout.ContainerType;
Jon Miranda228877d2021-02-09 11:05:00 -050051import com.android.launcher3.DevicePaddings.DevicePadding;
Alex Chaud67ddc42022-09-30 18:15:56 +010052import com.android.launcher3.config.FeatureFlags;
Tony Wickham8912b042018-11-29 15:28:53 -080053import com.android.launcher3.icons.DotRenderer;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070054import com.android.launcher3.icons.IconNormalizer;
Sihua Mae04aa202022-07-18 12:43:56 -070055import com.android.launcher3.model.data.ItemInfo;
Sunny Goyal57b22792021-05-25 14:35:01 -070056import com.android.launcher3.uioverrides.ApiWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070057import com.android.launcher3.util.DisplayController;
58import com.android.launcher3.util.DisplayController.Info;
Thales Limac98b7812023-04-14 15:04:23 +010059import com.android.launcher3.util.ResourceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070060import com.android.launcher3.util.WindowBounds;
Thales Limac98b7812023-04-14 15:04:23 +010061import com.android.launcher3.workspace.CalculatedWorkspaceSpec;
62import com.android.launcher3.workspace.WorkspaceSpecs;
Winson1f064272016-07-18 17:18:02 -070063
Jon Miranda58561d42021-03-17 10:51:54 -040064import java.io.PrintWriter;
Alex Chaue818bcb2022-09-02 17:27:11 +010065import java.util.Locale;
Jon Miranda2b25ded2023-02-02 14:48:45 -080066import java.util.function.Consumer;
Jon Miranda58561d42021-03-17 10:51:54 -040067
Sunny Goyal35c7b192021-04-20 16:51:10 -070068@SuppressLint("NewApi")
Winson Chungb3800242013-10-24 11:01:54 -070069public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070070
Sunny Goyal8b9919d2021-04-07 14:45:17 -070071 private static final int DEFAULT_DOT_SIZE = 100;
Alex Chau206ede92022-08-01 17:46:12 +010072 private static final float ALL_APPS_TABLET_MAX_ROWS = 5.5f;
Thales Limab35faed2022-09-05 16:30:01 -030073 private static final float MIN_FOLDER_TEXT_SIZE_SP = 16f;
Sunny Goyal6c304b12023-04-10 12:03:04 -070074 private static final float MIN_WIDGET_PADDING_DP = 6f;
Alex Chau206ede92022-08-01 17:46:12 +010075
Sihua Mae04aa202022-07-18 12:43:56 -070076 public static final PointF DEFAULT_SCALE = new PointF(1.0f, 1.0f);
77 public static final ViewScaleProvider DEFAULT_PROVIDER = itemInfo -> DEFAULT_SCALE;
Jon Miranda2b25ded2023-02-02 14:48:45 -080078 public static final Consumer<DeviceProfile> DEFAULT_DIMENSION_PROVIDER = dp -> {};
Sihua Mae04aa202022-07-18 12:43:56 -070079
Adam Cohen2e6da152015-05-06 11:42:25 -070080 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070081 private final Info mInfo;
Sunny Goyal35c7b192021-04-20 16:51:10 -070082 private final DisplayMetrics mMetrics;
Winson Chungbe876472014-05-14 14:15:20 -070083
Sunny Goyalc6205602015-05-21 20:46:33 -070084 // Device properties
85 public final boolean isTablet;
Sunny Goyalc6205602015-05-21 20:46:33 -070086 public final boolean isPhone;
87 public final boolean transposeLayoutWithOrientation;
Alex Chauab800f72022-12-13 17:46:06 +000088 public final boolean isMultiDisplay;
Sunny Goyal19ff7282021-04-22 10:12:54 -070089 public final boolean isTwoPanels;
Thales Limaa1012902022-01-13 17:58:42 +000090 public final boolean isQsbInline;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070091
Sunny Goyalc6205602015-05-21 20:46:33 -070092 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070093 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080094 public final boolean isMultiWindowMode;
Alex Chau6ed408f2022-03-04 12:58:05 +000095 public final boolean isGestureMode;
Sunny Goyald70e75a2018-02-22 10:07:32 -080096
Sunny Goyal0addbf02020-04-28 14:17:35 -070097 public final int windowX;
98 public final int windowY;
Sunny Goyalc6205602015-05-21 20:46:33 -070099 public final int widthPx;
100 public final int heightPx;
101 public final int availableWidthPx;
102 public final int availableHeightPx;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800103 public final int rotationHint;
Jon Mirandabba64512019-03-27 10:54:17 -0700104
105 public final float aspectRatio;
106
Thales Lima79456492023-05-23 11:32:22 +0100107 private final boolean mIsScalableGrid;
Thales Lima83bedbf2021-10-05 17:47:39 +0100108 private final int mTypeIndex;
Jon Mirandae126d722021-02-25 10:45:20 -0500109
Thales Limac98b7812023-04-14 15:04:23 +0100110 // Responsive grid
111 private final boolean mIsResponsiveGrid;
112 private WorkspaceSpecs mWorkspaceSpecs;
113 private CalculatedWorkspaceSpec mResponsiveWidthSpec;
114 private CalculatedWorkspaceSpec mResponsiveHeightSpec;
115
Tony Wickhamd6b40372015-09-23 18:37:57 -0700116 /**
117 * The maximum amount of left/right workspace padding as a percentage of the screen width.
118 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
119 * 7% of the screen width can be used as right padding.
120 */
121 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -0700122
Jon Miranda3f9bab22017-07-28 14:50:17 -0700123 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700124 private static final float TALLER_DEVICE_ASPECT_RATIO_THRESHOLD = 2.15f;
Jon Miranda80dda302021-07-28 14:14:59 -0700125 private static final float TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP = 252;
126 private static final float TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP = 268;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700127
Sunny Goyalc6205602015-05-21 20:46:33 -0700128 // Workspace
Thales Limad90faab2021-09-21 17:18:47 +0100129 public final int desiredWorkspaceHorizontalMarginOriginalPx;
130 public int desiredWorkspaceHorizontalMarginPx;
Alex Chau51da2192022-05-20 13:32:10 +0100131 public int gridVisualizationPaddingX;
132 public int gridVisualizationPaddingY;
Thales Lima78d00ad2021-09-30 11:29:06 +0100133 public Point cellLayoutBorderSpaceOriginalPx;
134 public Point cellLayoutBorderSpacePx;
Pat Manning08610ca2022-04-05 21:03:16 +0100135 public Rect cellLayoutPaddingPx = new Rect();
136
Sunny Goyalc6205602015-05-21 20:46:33 -0700137 public final int edgeMarginPx;
Alex Chau0c4e11b2022-07-08 18:41:36 +0100138 public final float workspaceContentScale;
Alex Chau906d8822022-05-23 10:42:25 +0100139 public final int workspaceSpringLoadedMinNextPageVisiblePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700140
Jon Miranda58561d42021-03-17 10:51:54 -0400141 private final int extraSpace;
Thales Lima171ee662022-11-22 15:52:49 +0000142 private int maxEmptySpace;
Jon Miranda228877d2021-02-09 11:05:00 -0500143 public int workspaceTopPadding;
144 public int workspaceBottomPadding;
145
Tony Wickham5edf9e22020-03-27 20:06:52 -0700146 // Workspace page indicator
147 public final int workspacePageIndicatorHeight;
148 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -0700149
Sunny Goyalc6205602015-05-21 20:46:33 -0700150 // Workspace icons
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400151 public float iconScale;
Sunny Goyalc6205602015-05-21 20:46:33 -0700152 public int iconSizePx;
153 public int iconTextSizePx;
154 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700155 public int iconDrawablePaddingOriginalPx;
Thales Lima79456492023-05-23 11:32:22 +0100156 public boolean iconCenterVertically;
Winson Chungb3800242013-10-24 11:01:54 -0700157
Jon Mirandaab3c6812021-06-28 15:42:28 -0700158 public float cellScaleToFit;
Adam Cohen59400422014-03-05 18:07:04 -0800159 public int cellWidthPx;
160 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700161 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800162
Thales Lima79456492023-05-23 11:32:22 +0100163 public int cellYPaddingPx = -1;
Jon Mirandae126d722021-02-25 10:45:20 -0500164
Sunny Goyalc6205602015-05-21 20:46:33 -0700165 // Folder
Jon Mirandae126d722021-02-25 10:45:20 -0500166 public float folderLabelTextScale;
167 public int folderLabelTextSizePx;
Thales Limab35faed2022-09-05 16:30:01 -0300168 public int folderFooterHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700169 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700170 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700171
Jon Mirandae126d722021-02-25 10:45:20 -0500172 // Folder content
Thales Limab35faed2022-09-05 16:30:01 -0300173 public int folderCellLayoutBorderSpacePx;
Jon Mirandae126d722021-02-25 10:45:20 -0500174 public int folderContentPaddingLeftRight;
175 public int folderContentPaddingTop;
176
Jon Mirandabf7d8122016-11-03 15:29:29 -0700177 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700178 public int folderCellWidthPx;
179 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700180
181 // Folder child
182 public int folderChildIconSizePx;
183 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700184 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700185
Sunny Goyalc6205602015-05-21 20:46:33 -0700186 // Hotseat
Thales Lima9938c2f2022-07-25 14:38:16 +0100187 public int numShownHotseatIcons;
Sunny Goyalc6205602015-05-21 20:46:33 -0700188 public int hotseatCellHeightPx;
Pat Manning26f70f72022-07-07 13:50:39 +0100189 public final boolean areNavButtonsInline;
Jon Miranda18751b62017-07-31 17:25:27 -0700190 // In portrait: size = height, in landscape: size = width
191 public int hotseatBarSizePx;
Thales Limab8c05952022-05-23 16:58:38 +0100192 public int hotseatBarBottomSpacePx;
Pat Manning26f70f72022-07-07 13:50:39 +0100193 public int hotseatBarEndOffset;
Thales Limab8c05952022-05-23 16:58:38 +0100194 public int hotseatQsbSpace;
Pat Manningde25c0d2022-04-05 19:11:26 +0100195 public int springLoadedHotseatBarTopMarginPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700196 // Start is the side next to the nav bar, end is the side next to the workspace
197 public final int hotseatBarSidePaddingStartPx;
198 public final int hotseatBarSidePaddingEndPx;
Alex Chau206ede92022-08-01 17:46:12 +0100199 public int hotseatQsbWidth; // only used when isQsbInline
Sunny Goyal57b22792021-05-25 14:35:01 -0700200 public final int hotseatQsbHeight;
Thales Limab8c05952022-05-23 16:58:38 +0100201 public final int hotseatQsbVisualHeight;
202 private final int hotseatQsbShadowHeight;
Thales Limadd027342022-01-07 12:54:37 +0000203 public int hotseatBorderSpace;
Thales Limaffc68b02023-01-09 16:20:23 +0000204 private final int mMinHotseatIconSpacePx;
205 private final int mMinHotseatQsbWidthPx;
206 private final int mMaxHotseatIconSpacePx;
Jon Mirandae17a9492023-03-08 16:42:20 -0800207 public final int inlineNavButtonsEndSpacingPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700208
Alex Chau3d2c0622022-09-01 21:28:14 +0100209 // Bottom sheets
210 public int bottomSheetTopPadding;
211 public int bottomSheetOpenDuration;
212 public int bottomSheetCloseDuration;
213 public float bottomSheetWorkspaceScale;
214 public float bottomSheetDepth;
215
Sunny Goyalc6205602015-05-21 20:46:33 -0700216 // All apps
Thales Lima85c942f2021-12-31 13:04:20 +0000217 public Point allAppsBorderSpacePx;
Alex Chaub8c22e12022-02-14 18:35:58 +0000218 public int allAppsShiftRange;
219 public int allAppsTopPadding;
Alex Chau3d2c0622022-09-01 21:28:14 +0100220 public int allAppsOpenDuration;
221 public int allAppsCloseDuration;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700222 public int allAppsCellHeightPx;
Samuel Fufaf1424a32019-10-04 15:15:19 -0700223 public int allAppsCellWidthPx;
Winson1f064272016-07-18 17:18:02 -0700224 public int allAppsIconSizePx;
225 public int allAppsIconDrawablePaddingPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700226 public int allAppsLeftRightPadding;
Alex Chau27b39b92022-01-14 18:02:18 +0000227 public int allAppsLeftRightMargin;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700228 public final int numShownAllAppsColumns;
Winson1f064272016-07-18 17:18:02 -0700229 public float allAppsIconTextSizePx;
230
Zak Cohen334efeb2021-03-19 16:42:07 -0700231 // Overview
Zak Cohen334efeb2021-03-19 16:42:07 -0700232 public int overviewTaskMarginPx;
Alex Chaudedbc8a2021-03-17 16:53:26 +0000233 public int overviewTaskIconSizePx;
Alex Chauac9df382021-08-02 19:08:41 +0100234 public int overviewTaskIconDrawableSizePx;
235 public int overviewTaskIconDrawableSizeGridPx;
Alex Chaudedbc8a2021-03-17 16:53:26 +0000236 public int overviewTaskThumbnailTopMarginPx;
Alex Chau19c6eca2022-03-10 20:12:56 +0000237 public final int overviewActionsHeight;
Alex Chaua2fc7642022-04-21 14:20:23 +0100238 public final int overviewActionsTopMarginPx;
Alex Chau1bf0fe12021-11-30 15:32:45 +0000239 public final int overviewActionsButtonSpacing;
Alex Chau5fd9d492021-07-22 17:27:11 +0100240 public int overviewPageSpacing;
241 public int overviewRowSpacing;
Alex Chau56bd2572021-11-03 18:48:18 +0000242 public int overviewGridSideMargin;
Zak Cohen334efeb2021-03-19 16:42:07 -0700243
Jeremy Sim0ac47082022-10-10 13:59:40 -0700244 // Split staging
245 public int splitPlaceholderInset;
246
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800247 // Widgets
Sihua Mae04aa202022-07-18 12:43:56 -0700248 private final ViewScaleProvider mViewScaleProvider;
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800249
Sunny Goyal47328fd2016-05-25 18:56:41 -0700250 // Drop Target
251 public int dropTargetBarSizePx;
Pat Manningde25c0d2022-04-05 19:11:26 +0100252 public int dropTargetBarTopMarginPx;
253 public int dropTargetBarBottomMarginPx;
Alex Chaua02eddc2021-04-29 00:36:06 +0100254 public int dropTargetDragPaddingPx;
255 public int dropTargetTextSizePx;
Pat Manningde25c0d2022-04-05 19:11:26 +0100256 public int dropTargetHorizontalPaddingPx;
257 public int dropTargetVerticalPaddingPx;
258 public int dropTargetGapPx;
Alex Chau5b019302022-05-23 10:42:25 +0100259 public int dropTargetButtonWorkspaceEdgeGapPx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700260
Winson1f064272016-07-18 17:18:02 -0700261 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800262 private final Rect mInsets = new Rect();
263 public final Rect workspacePadding = new Rect();
Sunny Goyal6c304b12023-04-10 12:03:04 -0700264 // Additional padding added to the widget inside its cellSpace. It is applied outside
265 // the widgetView, such that the actual view size is same as the widget size.
266 public final Rect widgetPadding = new Rect();
267
Jon Miranda0bd63d12019-03-06 17:29:29 -0800268 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700269 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700270
Tony Wickhamf34bee82018-12-03 18:11:39 -0800271 // Notification dots
Sunny Goyal65190ae2022-08-02 14:16:26 -0700272 public final DotRenderer mDotRendererWorkSpace;
273 public final DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800274
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100275 // Taskbar
Tony Wickham15883892021-02-12 11:24:40 -0800276 public boolean isTaskbarPresent;
Tony Wickham635e1802021-07-22 14:28:04 -1000277 // Whether Taskbar will inset the bottom of apps by taskbarSize.
278 public boolean isTaskbarPresentInApps;
Jon Miranda9c478b62023-03-23 21:38:49 -0700279 public final int taskbarHeight;
280 public final int stashedTaskbarHeight;
281 public final int taskbarBottomMargin;
282 public final int taskbarIconSize;
Jon Miranda04f05102023-04-04 12:16:31 -0700283 // If true, used to layout taskbar in 3 button navigation mode.
284 public final boolean startAlignTaskbar;
Tony Wickham15883892021-02-12 11:24:40 -0800285
Alex Chaua02eddc2021-04-29 00:36:06 +0100286 // DragController
287 public int flingToDeleteThresholdVelocity;
288
Vinit Nayak17c4b332021-08-05 12:54:58 -0700289 /** TODO: Once we fully migrate to staged split, remove "isMultiWindowMode" */
Sunny Goyal19ff7282021-04-22 10:12:54 -0700290 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info, WindowBounds windowBounds,
Sunny Goyal65190ae2022-08-02 14:16:26 -0700291 SparseArray<DotRenderer> dotRendererCache, boolean isMultiWindowMode,
Alex Chauab800f72022-12-13 17:46:06 +0000292 boolean transposeLayoutWithOrientation, boolean isMultiDisplay, boolean isGestureMode,
Jon Miranda2b25ded2023-02-02 14:48:45 -0800293 @NonNull final ViewScaleProvider viewScaleProvider,
294 @NonNull final Consumer<DeviceProfile> dimensionOverrideProvider) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700295
Adam Cohen2e6da152015-05-06 11:42:25 -0700296 this.inv = inv;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700297 this.isLandscape = windowBounds.isLandscape();
Sunny Goyald70e75a2018-02-22 10:07:32 -0800298 this.isMultiWindowMode = isMultiWindowMode;
Jon Mirandae126d722021-02-25 10:45:20 -0500299 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Alex Chauab800f72022-12-13 17:46:06 +0000300 this.isMultiDisplay = isMultiDisplay;
Alex Chau6ed408f2022-03-04 12:58:05 +0000301 this.isGestureMode = isGestureMode;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700302 windowX = windowBounds.bounds.left;
303 windowY = windowBounds.bounds.top;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800304 this.rotationHint = windowBounds.rotationHint;
Alex Chaue0227552022-04-06 19:44:43 +0100305 mInsets.set(windowBounds.insets);
Winson Chungb3800242013-10-24 11:01:54 -0700306
Thales Limac98b7812023-04-14 15:04:23 +0100307 // TODO(b/241386436): shouldn't change any launcher behaviour
308 mIsResponsiveGrid = inv.workspaceSpecsId != INVALID_RESOURCE_HANDLE;
Thales Limaec5abba2023-04-05 16:33:50 +0100309
Thales Lima79456492023-05-23 11:32:22 +0100310 mIsScalableGrid = inv.isScalable && !isVerticalBarLayout() && !isMultiWindowMode;
Alex Chaudfc8ddf2022-01-25 11:26:25 +0000311 // Determine device posture.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700312 mInfo = info;
Alex Chau29983072021-11-19 15:14:20 +0000313 isTablet = info.isTablet(windowBounds);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700314 isPhone = !isTablet;
Alex Chauab800f72022-12-13 17:46:06 +0000315 isTwoPanels = isTablet && isMultiDisplay;
Vinit Nayak58c27cc2022-02-16 17:42:21 -0800316 isTaskbarPresent = isTablet && ApiWrapper.TASKBAR_DRAWN_IN_PROCESS;
Adam Cohen2e6da152015-05-06 11:42:25 -0700317
Alex Chaudfc8ddf2022-01-25 11:26:25 +0000318 // Some more constants.
Alex Chau635b3ab2022-01-26 16:49:10 +0000319 context = getContext(context, info, isVerticalBarLayout() || (isTablet && isLandscape)
Brandon Dayauon07ca8842022-04-27 10:52:28 -0700320 ? Configuration.ORIENTATION_LANDSCAPE
321 : Configuration.ORIENTATION_PORTRAIT,
Alex Chaua6907dc2022-03-18 15:24:07 +0000322 windowBounds);
Sunny Goyal1890f672020-04-30 12:28:00 -0700323 final Resources res = context.getResources();
Alex Chaudfc8ddf2022-01-25 11:26:25 +0000324 mMetrics = res.getDisplayMetrics();
325
326 // Determine sizes.
327 widthPx = windowBounds.bounds.width();
328 heightPx = windowBounds.bounds.height();
329 availableWidthPx = windowBounds.availableSize.x;
Thales Limab8c05952022-05-23 16:58:38 +0100330 availableHeightPx = windowBounds.availableSize.y;
Alex Chaudfc8ddf2022-01-25 11:26:25 +0000331
332 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Thales Lima83bedbf2021-10-05 17:47:39 +0100333 if (isTwoPanels) {
334 if (isLandscape) {
Thales Lima12d0eff2022-03-25 17:06:11 +0000335 mTypeIndex = INDEX_TWO_PANEL_LANDSCAPE;
Thales Lima83bedbf2021-10-05 17:47:39 +0100336 } else {
Thales Lima12d0eff2022-03-25 17:06:11 +0000337 mTypeIndex = INDEX_TWO_PANEL_PORTRAIT;
Thales Lima83bedbf2021-10-05 17:47:39 +0100338 }
339 } else {
340 if (isLandscape) {
Thales Lima12d0eff2022-03-25 17:06:11 +0000341 mTypeIndex = INDEX_LANDSCAPE;
Thales Lima83bedbf2021-10-05 17:47:39 +0100342 } else {
Thales Lima12d0eff2022-03-25 17:06:11 +0000343 mTypeIndex = INDEX_DEFAULT;
Thales Lima83bedbf2021-10-05 17:47:39 +0100344 }
345 }
346
Jon Miranda9c478b62023-03-23 21:38:49 -0700347 if (DisplayController.isTransientTaskbar(context)) {
348 float invTransientIconSizeDp = inv.transientTaskbarIconSize[mTypeIndex];
349 taskbarIconSize = pxFromDp(invTransientIconSizeDp, mMetrics);
Jon Miranda95f44722023-06-06 19:48:19 -0700350 taskbarHeight = Math.round((taskbarIconSize * ICON_VISIBLE_AREA_FACTOR)
351 + (2 * res.getDimensionPixelSize(R.dimen.transient_taskbar_padding)));
Jon Miranda9c478b62023-03-23 21:38:49 -0700352 stashedTaskbarHeight =
353 res.getDimensionPixelSize(R.dimen.transient_taskbar_stashed_height);
354 taskbarBottomMargin =
355 res.getDimensionPixelSize(R.dimen.transient_taskbar_bottom_margin);
Jon Miranda04f05102023-04-04 12:16:31 -0700356 startAlignTaskbar = false;
Jon Miranda9c478b62023-03-23 21:38:49 -0700357 } else {
358 taskbarIconSize = pxFromDp(ResourcesCompat.getFloat(res, R.dimen.taskbar_icon_size),
359 mMetrics);
360 taskbarHeight = res.getDimensionPixelSize(R.dimen.taskbar_size);
361 stashedTaskbarHeight = res.getDimensionPixelSize(R.dimen.taskbar_stashed_size);
362 taskbarBottomMargin = 0;
Jon Miranda04f05102023-04-04 12:16:31 -0700363 startAlignTaskbar = inv.startAlignTaskbar[mTypeIndex];
Tony Wickham15883892021-02-12 11:24:40 -0800364 }
Tony Wickham15883892021-02-12 11:24:40 -0800365
Winson Chungb3800242013-10-24 11:01:54 -0700366 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Alex Chau0c4e11b2022-07-08 18:41:36 +0100367 workspaceContentScale = res.getFloat(R.dimen.workspace_content_scale);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700368
Alex Chau51da2192022-05-20 13:32:10 +0100369 gridVisualizationPaddingX = res.getDimensionPixelSize(
370 R.dimen.grid_visualization_horizontal_cell_spacing);
371 gridVisualizationPaddingY = res.getDimensionPixelSize(
372 R.dimen.grid_visualization_vertical_cell_spacing);
Samuel Fufaee9aff92021-04-05 13:30:40 -0500373
Alex Chaue0227552022-04-06 19:44:43 +0100374 bottomSheetTopPadding = mInsets.top // statusbar height
Andras Kloczl142b0542022-03-09 21:26:02 +0000375 + res.getDimensionPixelSize(R.dimen.bottom_sheet_extra_top_padding)
376 + (isTablet ? 0 : edgeMarginPx); // phones need edgeMarginPx additional padding
Alex Chau3d2c0622022-09-01 21:28:14 +0100377 bottomSheetOpenDuration = res.getInteger(R.integer.config_bottomSheetOpenDuration);
378 bottomSheetCloseDuration = res.getInteger(R.integer.config_bottomSheetCloseDuration);
379 if (isTablet) {
380 bottomSheetWorkspaceScale = workspaceContentScale;
Alex Chauab800f72022-12-13 17:46:06 +0000381 if (isMultiDisplay && !ENABLE_MULTI_DISPLAY_PARTIAL_DEPTH.get()) {
382 // TODO(b/259893832): Revert to use maxWallpaperScale to calculate bottomSheetDepth
383 // when screen recorder bug is fixed.
384 bottomSheetDepth = 1f;
385 } else {
386 // The goal is to set wallpaper to zoom at workspaceContentScale when in AllApps.
387 // When depth is 0, wallpaper zoom is set to maxWallpaperScale.
388 // When depth is 1, wallpaper zoom is set to 1.
389 // For depth to achieve zoom set to maxWallpaperScale * workspaceContentScale:
390 float maxWallpaperScale = res.getFloat(R.dimen.config_wallpaperMaxScale);
391 bottomSheetDepth = Utilities.mapToRange(maxWallpaperScale * workspaceContentScale,
392 maxWallpaperScale, 1f, 0f, 1f, LINEAR);
393 }
Alex Chau3d2c0622022-09-01 21:28:14 +0100394 } else {
395 bottomSheetWorkspaceScale = 1f;
396 bottomSheetDepth = 0f;
397 }
Andras Kloczl142b0542022-03-09 21:26:02 +0000398
Jon Mirandae126d722021-02-25 10:45:20 -0500399 folderLabelTextScale = res.getFloat(R.dimen.folder_label_text_scale);
Thales Limab35faed2022-09-05 16:30:01 -0300400
Thales Lima79456492023-05-23 11:32:22 +0100401 if (mIsScalableGrid && inv.folderStyle != INVALID_RESOURCE_HANDLE) {
Thales Limab35faed2022-09-05 16:30:01 -0300402 TypedArray folderStyle = context.obtainStyledAttributes(inv.folderStyle,
Thales Limad852d652023-01-17 14:01:13 +0000403 R.styleable.FolderStyle);
Thales Limab35faed2022-09-05 16:30:01 -0300404 // These are re-set in #updateFolderCellSize if the grid is not scalable
405 folderCellHeightPx = folderStyle.getDimensionPixelSize(
Thales Limad852d652023-01-17 14:01:13 +0000406 R.styleable.FolderStyle_folderCellHeight, 0);
Thales Limab35faed2022-09-05 16:30:01 -0300407 folderCellWidthPx = folderStyle.getDimensionPixelSize(
Thales Limad852d652023-01-17 14:01:13 +0000408 R.styleable.FolderStyle_folderCellWidth, 0);
Thales Limab35faed2022-09-05 16:30:01 -0300409
410 folderContentPaddingTop = folderStyle.getDimensionPixelSize(
Thales Limad852d652023-01-17 14:01:13 +0000411 R.styleable.FolderStyle_folderTopPadding, 0);
Thales Limab35faed2022-09-05 16:30:01 -0300412 folderCellLayoutBorderSpacePx = folderStyle.getDimensionPixelSize(
Thales Limad852d652023-01-17 14:01:13 +0000413 R.styleable.FolderStyle_folderBorderSpace, 0);
Thales Limab35faed2022-09-05 16:30:01 -0300414 folderFooterHeightPx = folderStyle.getDimensionPixelSize(
Thales Limad852d652023-01-17 14:01:13 +0000415 R.styleable.FolderStyle_folderFooterHeight, 0);
Thales Limab35faed2022-09-05 16:30:01 -0300416 folderStyle.recycle();
417 } else {
418 folderCellLayoutBorderSpacePx = 0;
Thales Lima7eee74b2023-03-16 16:00:55 -0300419 folderFooterHeightPx = res.getDimensionPixelSize(R.dimen.folder_footer_height_default);
Thales Limab35faed2022-09-05 16:30:01 -0300420 folderContentPaddingTop = res.getDimensionPixelSize(R.dimen.folder_top_padding_default);
421 }
Jon Mirandae126d722021-02-25 10:45:20 -0500422
Thales Lima85c942f2021-12-31 13:04:20 +0000423 allAppsBorderSpacePx = new Point(
Thales Limabb7d3882021-12-22 12:56:29 +0000424 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].x, mMetrics),
425 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].y, mMetrics));
Thales Limae9273ea2023-01-26 12:33:52 +0000426 setupAllAppsStyle(context);
Jon Mirandae126d722021-02-25 10:45:20 -0500427
Tony Wickham5edf9e22020-03-27 20:06:52 -0700428 workspacePageIndicatorHeight = res.getDimensionPixelSize(
429 R.dimen.workspace_page_indicator_height);
430 mWorkspacePageIndicatorOverlapWorkspace =
431 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700432
Thales Limad852d652023-01-17 14:01:13 +0000433 TypedArray cellStyle;
434 if (inv.cellStyle != INVALID_RESOURCE_HANDLE) {
435 cellStyle = context.obtainStyledAttributes(inv.cellStyle,
436 R.styleable.CellStyle);
437 } else {
438 cellStyle = context.obtainStyledAttributes(R.style.CellStyleDefault,
439 R.styleable.CellStyle);
440 }
441 iconDrawablePaddingOriginalPx = cellStyle.getDimensionPixelSize(
442 R.styleable.CellStyle_iconDrawablePadding, 0);
443 cellStyle.recycle();
Alex Chaua02eddc2021-04-29 00:36:06 +0100444
Sunny Goyal47328fd2016-05-25 18:56:41 -0700445 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
Pat Manningde25c0d2022-04-05 19:11:26 +0100446 dropTargetBarTopMarginPx = res.getDimensionPixelSize(R.dimen.drop_target_top_margin);
447 dropTargetBarBottomMarginPx = res.getDimensionPixelSize(R.dimen.drop_target_bottom_margin);
Alex Chaua02eddc2021-04-29 00:36:06 +0100448 dropTargetDragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
449 dropTargetTextSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size);
Pat Manningde25c0d2022-04-05 19:11:26 +0100450 dropTargetHorizontalPaddingPx = res.getDimensionPixelSize(
451 R.dimen.drop_target_button_drawable_horizontal_padding);
452 dropTargetVerticalPaddingPx = res.getDimensionPixelSize(
453 R.dimen.drop_target_button_drawable_vertical_padding);
454 dropTargetGapPx = res.getDimensionPixelSize(R.dimen.drop_target_button_gap);
Alex Chau5b019302022-05-23 10:42:25 +0100455 dropTargetButtonWorkspaceEdgeGapPx = res.getDimensionPixelSize(
456 R.dimen.drop_target_button_workspace_edge_gap);
Alex Chaua02eddc2021-04-29 00:36:06 +0100457
Alex Chau906d8822022-05-23 10:42:25 +0100458 workspaceSpringLoadedMinNextPageVisiblePx = res.getDimensionPixelSize(
459 R.dimen.dynamic_grid_spring_loaded_min_next_space_visible);
Sunny Goyald5190522017-04-24 03:06:54 -0700460
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700461 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700462
Thales Limaa1012902022-01-13 17:58:42 +0000463 hotseatQsbHeight = res.getDimensionPixelSize(R.dimen.qsb_widget_height);
Thales Limab8c05952022-05-23 16:58:38 +0100464 hotseatQsbShadowHeight = res.getDimensionPixelSize(R.dimen.qsb_shadow_height);
465 hotseatQsbVisualHeight = hotseatQsbHeight - 2 * hotseatQsbShadowHeight;
466
Thales Lima12d0eff2022-03-25 17:06:11 +0000467 // Whether QSB might be inline in appropriate orientation (e.g. landscape).
468 boolean canQsbInline = (isTwoPanels ? inv.inlineQsb[INDEX_TWO_PANEL_PORTRAIT]
469 || inv.inlineQsb[INDEX_TWO_PANEL_LANDSCAPE]
470 : inv.inlineQsb[INDEX_DEFAULT] || inv.inlineQsb[INDEX_LANDSCAPE])
471 && hotseatQsbHeight > 0;
Thales Lima79456492023-05-23 11:32:22 +0100472 isQsbInline = mIsScalableGrid && inv.inlineQsb[mTypeIndex] && canQsbInline;
Alex Chau6ed408f2022-03-04 12:58:05 +0000473
Thales Lima425f6822022-05-10 13:44:58 -0300474 areNavButtonsInline = isTaskbarPresent && !isGestureMode;
Thales Lima9938c2f2022-07-25 14:38:16 +0100475 numShownHotseatIcons =
476 isTwoPanels ? inv.numDatabaseHotseatIcons : inv.numShownHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000477
Sunny Goyal19ff7282021-04-22 10:12:54 -0700478 numShownAllAppsColumns =
479 isTwoPanels ? inv.numDatabaseAllAppsColumns : inv.numAllAppsColumns;
Thales Limab8c05952022-05-23 16:58:38 +0100480
481 int hotseatBarBottomSpace = pxFromDp(inv.hotseatBarBottomSpace[mTypeIndex], mMetrics);
482 int minQsbMargin = res.getDimensionPixelSize(R.dimen.min_qsb_margin);
483 hotseatQsbSpace = pxFromDp(inv.hotseatQsbSpace[mTypeIndex], mMetrics);
484 // Have a little space between the inset and the QSB
485 if (mInsets.bottom + minQsbMargin > hotseatBarBottomSpace) {
486 int availableSpace = hotseatQsbSpace - (mInsets.bottom - hotseatBarBottomSpace);
487
488 // Only change the spaces if there is space
489 if (availableSpace > 0) {
490 // Make sure there is enough space between hotseat/QSB and QSB/navBar
491 if (availableSpace < minQsbMargin * 2) {
492 minQsbMargin = availableSpace / 2;
493 hotseatQsbSpace = minQsbMargin;
494 } else {
495 hotseatQsbSpace -= minQsbMargin;
496 }
497 }
498 hotseatBarBottomSpacePx = mInsets.bottom + minQsbMargin;
499
Thales Limaa1012902022-01-13 17:58:42 +0000500 } else {
Thales Limab8c05952022-05-23 16:58:38 +0100501 hotseatBarBottomSpacePx = hotseatBarBottomSpace;
Thales Limaa1012902022-01-13 17:58:42 +0000502 }
Thales Lima425f6822022-05-10 13:44:58 -0300503
Pat Manningde25c0d2022-04-05 19:11:26 +0100504 springLoadedHotseatBarTopMarginPx = res.getDimensionPixelSize(
505 R.dimen.spring_loaded_hotseat_top_margin);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700506 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800507 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700508 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700509 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Thales Limab8c05952022-05-23 16:58:38 +0100510 updateHotseatSizes(pxFromDp(inv.iconSize[INDEX_DEFAULT], mMetrics));
Vinit Nayak8a3d0552022-08-05 10:43:29 -0700511 if (areNavButtonsInline && !isPhone) {
Jon Mirandae17a9492023-03-08 16:42:20 -0800512 inlineNavButtonsEndSpacingPx =
Thales Limaffc68b02023-01-09 16:20:23 +0000513 res.getDimensionPixelSize(inv.inlineNavButtonsEndSpacing);
Pat Manning26f70f72022-07-07 13:50:39 +0100514 /*
515 * 3 nav buttons +
Vinit Nayakc7293172022-07-18 16:41:50 -0700516 * Spacing between nav buttons +
Thales Lima6a590062022-11-22 15:52:49 +0000517 * Space at the end for contextual buttons
Pat Manning26f70f72022-07-07 13:50:39 +0100518 */
519 hotseatBarEndOffset = 3 * res.getDimensionPixelSize(R.dimen.taskbar_nav_buttons_size)
Vinit Nayakc7293172022-07-18 16:41:50 -0700520 + 2 * res.getDimensionPixelSize(R.dimen.taskbar_button_space_inbetween)
Jon Mirandae17a9492023-03-08 16:42:20 -0800521 + inlineNavButtonsEndSpacingPx;
Pat Manning26f70f72022-07-07 13:50:39 +0100522 } else {
Jon Mirandae17a9492023-03-08 16:42:20 -0800523 inlineNavButtonsEndSpacingPx = 0;
Pat Manning26f70f72022-07-07 13:50:39 +0100524 hotseatBarEndOffset = 0;
525 }
Jon Miranda8bdb2222021-06-23 18:10:10 -0700526
Thales Lima79456492023-05-23 11:32:22 +0100527 // Needs to be calculated after hotseatBarSizePx is correct,
528 // for the available height to be correct
529 if (mIsResponsiveGrid) {
530 mWorkspaceSpecs = new WorkspaceSpecs(new ResourceHelper(context, inv.workspaceSpecsId));
531 mResponsiveWidthSpec = mWorkspaceSpecs.getCalculatedWidthSpec(inv.numColumns,
532 availableWidthPx);
533 mResponsiveHeightSpec = mWorkspaceSpecs.getCalculatedHeightSpec(inv.numRows,
534 // don't use availableHeightPx because it subtracts bottom padding,
535 // but the hotseat go behind it
536 heightPx - mInsets.top - hotseatBarSizePx);
537 }
538
539 desiredWorkspaceHorizontalMarginPx = getHorizontalMarginPx(inv, res);
540 desiredWorkspaceHorizontalMarginOriginalPx = desiredWorkspaceHorizontalMarginPx;
541
Alex Chau635b3ab2022-01-26 16:49:10 +0000542 overviewTaskMarginPx = res.getDimensionPixelSize(R.dimen.overview_task_margin);
Alex Chau5fd9d492021-07-22 17:27:11 +0100543 overviewTaskIconSizePx = res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_size);
Alex Chauac9df382021-08-02 19:08:41 +0100544 overviewTaskIconDrawableSizePx =
545 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size);
546 overviewTaskIconDrawableSizeGridPx =
547 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size_grid);
Jeremy Sim1cfe6d42022-07-15 14:40:38 -0700548 overviewTaskThumbnailTopMarginPx = overviewTaskIconSizePx + overviewTaskMarginPx;
Jeremy Sim3c2c3f12022-05-16 20:37:43 -0700549 overviewActionsTopMarginPx = res.getDimensionPixelSize(R.dimen.overview_actions_top_margin);
Alex Chau635b3ab2022-01-26 16:49:10 +0000550 overviewPageSpacing = res.getDimensionPixelSize(R.dimen.overview_page_spacing);
551 overviewActionsButtonSpacing = res.getDimensionPixelSize(
552 R.dimen.overview_actions_button_spacing);
Alex Chau19c6eca2022-03-10 20:12:56 +0000553 overviewActionsHeight = res.getDimensionPixelSize(R.dimen.overview_actions_height);
Jeremy Sim1cfe6d42022-07-15 14:40:38 -0700554 overviewRowSpacing = res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing);
Alex Chau635b3ab2022-01-26 16:49:10 +0000555 overviewGridSideMargin = res.getDimensionPixelSize(R.dimen.overview_grid_side_margin);
Zak Cohen334efeb2021-03-19 16:42:07 -0700556
Jeremy Sim0ac47082022-10-10 13:59:40 -0700557 splitPlaceholderInset = res.getDimensionPixelSize(R.dimen.split_placeholder_inset);
558
Jon Miranda2ed276e2017-06-29 11:36:34 -0700559 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400560 extraSpace = updateAvailableDimensions(res);
Jon Miranda80cddbc2021-07-22 13:51:16 -0700561
Thales Lima79456492023-05-23 11:32:22 +0100562 calculateAndSetWorkspaceVerticalPadding(context, inv, extraSpace);
Pat Manning08610ca2022-04-05 21:03:16 +0100563
564 int cellLayoutPadding =
565 isTwoPanels ? cellLayoutBorderSpacePx.x / 2 : res.getDimensionPixelSize(
566 R.dimen.cell_layout_padding);
567 cellLayoutPaddingPx = new Rect(cellLayoutPadding, cellLayoutPadding, cellLayoutPadding,
568 cellLayoutPadding);
Sunny Goyal07b69292018-01-08 14:19:34 -0800569 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800570
Thales Lima7eee74b2023-03-16 16:00:55 -0300571 // Folder scaling requires correct workspace paddings
572 updateAvailableFolderCellDimensions(res);
573
Thales Limaffc68b02023-01-09 16:20:23 +0000574 mMinHotseatIconSpacePx = res.getDimensionPixelSize(R.dimen.min_hotseat_icon_space);
575 mMinHotseatQsbWidthPx = res.getDimensionPixelSize(R.dimen.min_hotseat_qsb_width);
576 mMaxHotseatIconSpacePx = areNavButtonsInline
Thales Lima6a590062022-11-22 15:52:49 +0000577 ? res.getDimensionPixelSize(R.dimen.max_hotseat_icon_space) : Integer.MAX_VALUE;
Thales Lima425f6822022-05-10 13:44:58 -0300578 // Hotseat and QSB width depends on updated cellSize and workspace padding
Thales Lima6a590062022-11-22 15:52:49 +0000579 recalculateHotseatWidthAndBorderSpace();
Alex Chau206ede92022-08-01 17:46:12 +0100580
581 // AllApps height calculation depends on updated cellSize
582 if (isTablet) {
583 int collapseHandleHeight =
584 res.getDimensionPixelOffset(R.dimen.bottom_sheet_handle_area_height);
585 int contentHeight = heightPx - collapseHandleHeight - hotseatQsbHeight;
586 int targetContentHeight = (int) (allAppsCellHeightPx * ALL_APPS_TABLET_MAX_ROWS);
587 allAppsTopPadding = Math.max(mInsets.top, contentHeight - targetContentHeight);
588 allAppsShiftRange = heightPx - allAppsTopPadding;
589 } else {
590 allAppsTopPadding = 0;
591 allAppsShiftRange =
592 res.getDimensionPixelSize(R.dimen.all_apps_starting_vertical_translate);
593 }
Alex Chau3d2c0622022-09-01 21:28:14 +0100594 allAppsOpenDuration = res.getInteger(R.integer.config_allAppsOpenDuration);
595 allAppsCloseDuration = res.getInteger(R.integer.config_allAppsCloseDuration);
Thales Lima425f6822022-05-10 13:44:58 -0300596
Alex Chaua02eddc2021-04-29 00:36:06 +0100597 flingToDeleteThresholdVelocity = res.getDimensionPixelSize(
598 R.dimen.drag_flingToDeleteMinVelocity);
599
Sihua Mae04aa202022-07-18 12:43:56 -0700600 mViewScaleProvider = viewScaleProvider;
601
Jon Miranda2b25ded2023-02-02 14:48:45 -0800602 dimensionOverrideProvider.accept(this);
603
Tony Wickham1237df02017-02-24 08:59:36 -0800604 // This is done last, after iconSizePx is calculated above.
Sunny Goyal799e1202023-02-09 10:54:28 -0800605 mDotRendererWorkSpace = createDotRenderer(context, iconSizePx, dotRendererCache);
606 mDotRendererAllApps = createDotRenderer(context, allAppsIconSizePx, dotRendererCache);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700607 }
608
609 private static DotRenderer createDotRenderer(
Sunny Goyal799e1202023-02-09 10:54:28 -0800610 @NonNull Context context, int size, @NonNull SparseArray<DotRenderer> cache) {
Sunny Goyal65190ae2022-08-02 14:16:26 -0700611 DotRenderer renderer = cache.get(size);
612 if (renderer == null) {
Sunny Goyal799e1202023-02-09 10:54:28 -0800613 renderer = new DotRenderer(size, getShapePath(context, DEFAULT_DOT_SIZE),
614 DEFAULT_DOT_SIZE);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700615 cache.put(size, renderer);
616 }
617 return renderer;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700618 }
619
Thales Lima425f6822022-05-10 13:44:58 -0300620 /**
621 * QSB width is always calculated because when in 3 button nav the width doesn't follow the
622 * width of the hotseat.
623 */
Thales Lima9938c2f2022-07-25 14:38:16 +0100624 private int calculateQsbWidth(int hotseatBorderSpace) {
Thales Lima425f6822022-05-10 13:44:58 -0300625 if (isQsbInline) {
Alex Chau906d8822022-05-23 10:42:25 +0100626 int columns = getPanelCount() * inv.numColumns;
Thales Lima425f6822022-05-10 13:44:58 -0300627 return getIconToIconWidthForColumns(columns)
628 - iconSizePx * numShownHotseatIcons
629 - hotseatBorderSpace * numShownHotseatIcons;
630 } else {
631 int columns = inv.hotseatColumnSpan[mTypeIndex];
632 return getIconToIconWidthForColumns(columns);
633 }
634 }
Thales Lima2903a622022-03-17 13:52:19 +0000635
Thales Lima425f6822022-05-10 13:44:58 -0300636 private int getIconToIconWidthForColumns(int columns) {
637 return columns * getCellSize().x
638 + (columns - 1) * cellLayoutBorderSpacePx.x
Thales Lima1e8b45f2022-08-25 11:50:59 -0400639 - getCellHorizontalSpace();
Thales Limaa1012902022-01-13 17:58:42 +0000640 }
641
Thales Limad90faab2021-09-21 17:18:47 +0100642 private int getHorizontalMarginPx(InvariantDeviceProfile idp, Resources res) {
Thales Lima79456492023-05-23 11:32:22 +0100643 if (mIsResponsiveGrid) {
644 return mResponsiveWidthSpec.getStartPaddingPx();
645 }
646
Thales Limad90faab2021-09-21 17:18:47 +0100647 if (isVerticalBarLayout()) {
648 return 0;
649 }
650
Thales Lima79456492023-05-23 11:32:22 +0100651 return mIsScalableGrid
Thales Lima83bedbf2021-10-05 17:47:39 +0100652 ? pxFromDp(idp.horizontalMargin[mTypeIndex], mMetrics)
653 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
Thales Limad90faab2021-09-21 17:18:47 +0100654 }
655
Thales Lima79456492023-05-23 11:32:22 +0100656 private void calculateAndSetWorkspaceVerticalPadding(Context context,
657 InvariantDeviceProfile inv,
658 int extraSpace) {
659 if (mIsResponsiveGrid) {
660 workspaceTopPadding = mResponsiveHeightSpec.getStartPaddingPx();
661 workspaceBottomPadding = mResponsiveHeightSpec.getEndPaddingPx();
662 } else if (mIsScalableGrid && inv.devicePaddingId != INVALID_RESOURCE_HANDLE) {
663 // Paddings were created assuming no scaling, so we first unscale the extra space.
664 int unscaledExtraSpace = (int) (extraSpace / cellScaleToFit);
665 DevicePaddings devicePaddings = new DevicePaddings(context, inv.devicePaddingId);
666 DevicePadding padding = devicePaddings.getDevicePadding(unscaledExtraSpace);
667 maxEmptySpace = padding.getMaxEmptySpacePx();
668
669 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
670 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
671
672 workspaceTopPadding = Math.round(paddingWorkspaceTop * cellScaleToFit);
673 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * cellScaleToFit);
674 }
675 }
676
Thales Limab8c05952022-05-23 16:58:38 +0100677 /** Updates hotseatCellHeightPx and hotseatBarSizePx */
678 private void updateHotseatSizes(int hotseatIconSizePx) {
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700679 // Ensure there is enough space for folder icons, which have a slightly larger radius.
680 hotseatCellHeightPx = (int) Math.ceil(hotseatIconSizePx * ICON_OVERLAP_FACTOR);
Thales Limab8c05952022-05-23 16:58:38 +0100681
Jon Miranda0d284852021-06-22 14:50:55 -0700682 if (isVerticalBarLayout()) {
683 hotseatBarSizePx = hotseatIconSizePx + hotseatBarSidePaddingStartPx
684 + hotseatBarSidePaddingEndPx;
Thales Limab8c05952022-05-23 16:58:38 +0100685 } else if (isQsbInline) {
686 hotseatBarSizePx = Math.max(hotseatIconSizePx, hotseatQsbVisualHeight)
687 + hotseatBarBottomSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700688 } else {
Thales Limab8c05952022-05-23 16:58:38 +0100689 hotseatBarSizePx = hotseatIconSizePx
690 + hotseatQsbSpace
691 + hotseatQsbVisualHeight
692 + hotseatBarBottomSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700693 }
694 }
695
Thales Lima6a590062022-11-22 15:52:49 +0000696 /**
697 * Calculates the width of the hotseat, changing spaces between the icons and removing icons if
698 * necessary.
699 */
700 public void recalculateHotseatWidthAndBorderSpace() {
Thales Lima79456492023-05-23 11:32:22 +0100701 if (!mIsScalableGrid) return;
Thales Lima6a590062022-11-22 15:52:49 +0000702
703 int columns = inv.hotseatColumnSpan[mTypeIndex];
704 float hotseatWidthPx = getIconToIconWidthForColumns(columns);
705 hotseatBorderSpace = calculateHotseatBorderSpace(hotseatWidthPx, /* numExtraBorder= */ 0);
Thales Lima9938c2f2022-07-25 14:38:16 +0100706 hotseatQsbWidth = calculateQsbWidth(hotseatBorderSpace);
Thales Lima6a590062022-11-22 15:52:49 +0000707 // Spaces should be correct when the nav buttons are not inline
Thales Lima9938c2f2022-07-25 14:38:16 +0100708 if (!areNavButtonsInline) {
709 return;
710 }
711
Thales Lima6a590062022-11-22 15:52:49 +0000712 // The side space with inline buttons should be what is defined in InvariantDeviceProfile
Jon Mirandae17a9492023-03-08 16:42:20 -0800713 int sideSpacePx = inlineNavButtonsEndSpacingPx;
Thales Limaffc68b02023-01-09 16:20:23 +0000714 int maxHotseatWidthPx = availableWidthPx - sideSpacePx - hotseatBarEndOffset;
715 int maxHotseatIconsWidthPx = maxHotseatWidthPx - (isQsbInline ? hotseatQsbWidth : 0);
716 hotseatBorderSpace = calculateHotseatBorderSpace(maxHotseatIconsWidthPx,
Thales Lima6a590062022-11-22 15:52:49 +0000717 (isQsbInline ? 1 : 0) + /* border between nav buttons and first icon */ 1);
Thales Lima9938c2f2022-07-25 14:38:16 +0100718
Thales Limaffc68b02023-01-09 16:20:23 +0000719 if (hotseatBorderSpace >= mMinHotseatIconSpacePx) {
Thales Lima9938c2f2022-07-25 14:38:16 +0100720 return;
721 }
722
723 // Border space can't be less than the minimum
Thales Limaffc68b02023-01-09 16:20:23 +0000724 hotseatBorderSpace = mMinHotseatIconSpacePx;
Thales Lima6a590062022-11-22 15:52:49 +0000725 int requiredWidth = getHotseatRequiredWidth();
Thales Lima9938c2f2022-07-25 14:38:16 +0100726
727 // If there is an inline qsb, change its size
728 if (isQsbInline) {
Thales Limaffc68b02023-01-09 16:20:23 +0000729 hotseatQsbWidth -= requiredWidth - maxHotseatWidthPx;
730 if (hotseatQsbWidth >= mMinHotseatQsbWidthPx) {
Thales Lima9938c2f2022-07-25 14:38:16 +0100731 return;
732 }
733
734 // QSB can't be less than the minimum
Thales Limaffc68b02023-01-09 16:20:23 +0000735 hotseatQsbWidth = mMinHotseatQsbWidthPx;
Thales Lima9938c2f2022-07-25 14:38:16 +0100736 }
737
Thales Limaffc68b02023-01-09 16:20:23 +0000738 maxHotseatIconsWidthPx = maxHotseatWidthPx - (isQsbInline ? hotseatQsbWidth : 0);
Thales Lima6a590062022-11-22 15:52:49 +0000739
Thales Lima9938c2f2022-07-25 14:38:16 +0100740 // If it still doesn't fit, start removing icons
741 do {
742 numShownHotseatIcons--;
Thales Limaffc68b02023-01-09 16:20:23 +0000743 hotseatBorderSpace = calculateHotseatBorderSpace(maxHotseatIconsWidthPx,
Thales Lima6a590062022-11-22 15:52:49 +0000744 (isQsbInline ? 1 : 0) + /* border between nav buttons and first icon */ 1);
Thales Limaffc68b02023-01-09 16:20:23 +0000745 } while (hotseatBorderSpace < mMinHotseatIconSpacePx && numShownHotseatIcons > 1);
Thales Lima9938c2f2022-07-25 14:38:16 +0100746
Thales Lima9938c2f2022-07-25 14:38:16 +0100747 }
748
Thales Lima78d00ad2021-09-30 11:29:06 +0100749 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp) {
Thales Lima080d8902022-03-28 15:30:29 +0100750 return getCellLayoutBorderSpace(idp, 1f);
Thales Lima080d8902022-03-28 15:30:29 +0100751 }
752
753 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp, float scale) {
Thales Lima79456492023-05-23 11:32:22 +0100754 int horizontalSpacePx = 0;
755 int verticalSpacePx = 0;
Thales Lima78d00ad2021-09-30 11:29:06 +0100756
Thales Lima79456492023-05-23 11:32:22 +0100757 if (mIsResponsiveGrid) {
758 horizontalSpacePx = mResponsiveWidthSpec.getGutterPx();
759 verticalSpacePx = mResponsiveHeightSpec.getGutterPx();
760 } else if (mIsScalableGrid) {
761 horizontalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].x, mMetrics, scale);
762 verticalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].y, mMetrics, scale);
763 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100764
765 return new Point(horizontalSpacePx, verticalSpacePx);
766 }
767
Sunny Goyal69a8eec2021-07-22 10:02:50 -0700768 public Info getDisplayInfo() {
769 return mInfo;
770 }
771
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700772 public Builder toBuilder(Context context) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800773 WindowBounds bounds = new WindowBounds(
774 widthPx, heightPx, availableWidthPx, availableHeightPx, rotationHint);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700775 bounds.bounds.offsetTo(windowX, windowY);
Alex Chaue0227552022-04-06 19:44:43 +0100776 bounds.insets.set(mInsets);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700777
778 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
779 dotRendererCache.put(iconSizePx, mDotRendererWorkSpace);
780 dotRendererCache.put(allAppsIconSizePx, mDotRendererAllApps);
781
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700782 return new Builder(context, inv, mInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700783 .setWindowBounds(bounds)
Alex Chauab800f72022-12-13 17:46:06 +0000784 .setIsMultiDisplay(isMultiDisplay)
Alex Chau6ed408f2022-03-04 12:58:05 +0000785 .setMultiWindowMode(isMultiWindowMode)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700786 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000787 .setGestureMode(isGestureMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800788 }
789
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700790 public DeviceProfile copy(Context context) {
791 return toBuilder(context).build();
792 }
793
794 /**
795 * TODO: Move this to the builder as part of setMultiWindowMode
796 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700797 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700798 DeviceProfile profile = toBuilder(context)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700799 .setWindowBounds(windowBounds)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700800 .setMultiWindowMode(true)
801 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800802
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800803 // We use these scales to measure and layout the widgets using their full invariant profile
804 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
805 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
806 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
Sihua Mae04aa202022-07-18 12:43:56 -0700807 if (appWidgetScaleX != 1 || appWidgetScaleY != 1) {
808 final PointF p = new PointF(appWidgetScaleX, appWidgetScaleY);
809 profile = profile.toBuilder(context)
810 .setViewScaleProvider(i -> p)
811 .build();
812 }
813
814 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800815
Jon Miranda93e1f042016-11-11 14:13:04 -0800816 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700817 }
818
Adam Cohen63f1ec02014-08-12 09:23:13 -0700819 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400820 * Checks if there is enough space for labels on the workspace.
821 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700822 * It is important to call this method after the All Apps variables have been set.
823 */
Jon Miranda941375e2021-03-31 13:10:45 -0400824 private void hideWorkspaceLabelsIfNotEnoughSpace() {
825 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
826 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
827 - iconTextHeight;
828
829 // We want enough space so that the text is closer to its corresponding icon.
830 if (workspaceCellPaddingY < iconTextHeight) {
831 iconTextSizePx = 0;
832 iconDrawablePaddingPx = 0;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700833 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda941375e2021-03-31 13:10:45 -0400834 autoResizeAllAppsCells();
835 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700836 }
Jon Miranda1091e532017-07-21 13:31:50 -0700837
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700838 /**
839 * Re-computes the all-apps cell size to be independent of workspace
840 */
841 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400842 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
843 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700844 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400845 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700846 }
847
Thales Limab7ef5692022-03-10 10:32:36 +0000848 private void updateAllAppsContainerWidth(Resources res) {
Pat Manning08610ca2022-04-05 21:03:16 +0100849 int cellLayoutHorizontalPadding =
850 (cellLayoutPaddingPx.left + cellLayoutPaddingPx.right) / 2;
Alex Chau27b39b92022-01-14 18:02:18 +0000851 if (isTablet) {
sfufa@google.comde013292021-09-21 18:22:44 -0700852 int usedWidth = (allAppsCellWidthPx * numShownAllAppsColumns)
Thales Limab7ef5692022-03-10 10:32:36 +0000853 + (allAppsBorderSpacePx.x * (numShownAllAppsColumns - 1))
Alex Chau27b39b92022-01-14 18:02:18 +0000854 + allAppsLeftRightPadding * 2;
855 allAppsLeftRightMargin = Math.max(1, (availableWidthPx - usedWidth) / 2);
sfufa@google.comde013292021-09-21 18:22:44 -0700856 } else {
857 allAppsLeftRightPadding =
Pat Manning08610ca2022-04-05 21:03:16 +0100858 desiredWorkspaceHorizontalMarginPx + cellLayoutHorizontalPadding;
sfufa@google.comde013292021-09-21 18:22:44 -0700859 }
860 }
861
Thales Limae9273ea2023-01-26 12:33:52 +0000862 private void setupAllAppsStyle(Context context) {
863 TypedArray allAppsStyle;
864 if (inv.allAppsStyle != INVALID_RESOURCE_HANDLE) {
865 allAppsStyle = context.obtainStyledAttributes(inv.allAppsStyle,
866 R.styleable.AllAppsStyle);
867 } else {
868 allAppsStyle = context.obtainStyledAttributes(R.style.AllAppsStyleDefault,
869 R.styleable.AllAppsStyle);
870 }
871 allAppsLeftRightPadding = allAppsStyle.getDimensionPixelSize(
872 R.styleable.AllAppsStyle_horizontalPadding, 0);
873 allAppsStyle.recycle();
874 }
875
Jon Miranda228877d2021-02-09 11:05:00 -0500876 /**
877 * Returns the amount of extra (or unused) vertical space.
878 */
879 private int updateAvailableDimensions(Resources res) {
Alex Chaua68164d2022-12-15 16:16:03 +0000880 float invIconSizeDp = inv.iconSize[mTypeIndex];
881 float invIconTextSizeSp = inv.iconTextSize[mTypeIndex];
882 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics));
883 iconTextSizePx = pxFromSp(invIconTextSizeSp, mMetrics);
Thales Lima79456492023-05-23 11:32:22 +0100884 iconCenterVertically = mIsScalableGrid || mIsResponsiveGrid;
Alex Chaua68164d2022-12-15 16:16:03 +0000885
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700886 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800887
Pat Manning08610ca2022-04-05 21:03:16 +0100888 updateWorkspacePadding();
Jon Mirandae126d722021-02-25 10:45:20 -0500889
890 // Check to see if the icons fit within the available height.
Pat Manninga2e14992022-04-22 11:29:17 +0100891 float usedHeight = getCellLayoutHeightSpecification();
Pat Manning25d53342022-05-10 09:58:49 +0000892 final int maxHeight = getCellLayoutHeight();
Jon Miranda228877d2021-02-09 11:05:00 -0500893 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500894 float scaleY = maxHeight / usedHeight;
895 boolean shouldScale = scaleY < 1f;
896
897 float scaleX = 1f;
Thales Lima79456492023-05-23 11:32:22 +0100898 if (mIsScalableGrid) {
Jon Mirandae126d722021-02-25 10:45:20 -0500899 // We scale to fit the cellWidth and cellHeight in the available space.
900 // The benefit of scalable grids is that we can get consistent aspect ratios between
901 // devices.
Pat Manninga2e14992022-04-22 11:29:17 +0100902 float usedWidth =
903 getCellLayoutWidthSpecification() + (desiredWorkspaceHorizontalMarginPx * 2);
Jon Mirandae126d722021-02-25 10:45:20 -0500904 // We do not subtract padding here, as we also scale the workspace padding if needed.
905 scaleX = availableWidthPx / usedWidth;
906 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700907 }
Jon Mirandae126d722021-02-25 10:45:20 -0500908
909 if (shouldScale) {
910 float scale = Math.min(scaleX, scaleY);
911 updateIconSize(scale, res);
Pat Manninga2e14992022-04-22 11:29:17 +0100912 extraHeight = Math.max(0, maxHeight - getCellLayoutHeightSpecification());
Jon Mirandae126d722021-02-25 10:45:20 -0500913 }
914
Jon Miranda228877d2021-02-09 11:05:00 -0500915 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700916 }
917
Pat Manninga2e14992022-04-22 11:29:17 +0100918 private int getCellLayoutHeightSpecification() {
Pat Manning08610ca2022-04-05 21:03:16 +0100919 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacePx.y * (inv.numRows - 1))
920 + cellLayoutPaddingPx.top + cellLayoutPaddingPx.bottom;
921 }
922
Pat Manninga2e14992022-04-22 11:29:17 +0100923 private int getCellLayoutWidthSpecification() {
Pat Manning25d53342022-05-10 09:58:49 +0000924 int numColumns = getPanelCount() * inv.numColumns;
Pat Manning08610ca2022-04-05 21:03:16 +0100925 return (cellWidthPx * numColumns) + (cellLayoutBorderSpacePx.x * (numColumns - 1))
926 + cellLayoutPaddingPx.left + cellLayoutPaddingPx.right;
Jon Mirandae126d722021-02-25 10:45:20 -0500927 }
928
Jon Miranda6f7e9702019-09-16 14:44:14 -0700929 /**
930 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
931 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
932 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
933 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800934 public void updateIconSize(float scale, Resources res) {
Jon Mirandaab3c6812021-06-28 15:42:28 -0700935 // Icon scale should never exceed 1, otherwise pixellation may occur.
936 iconScale = Math.min(1f, scale);
937 cellScaleToFit = scale;
938
Jon Miranda18751b62017-07-31 17:25:27 -0700939 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800940 final boolean isVerticalLayout = isVerticalBarLayout();
Jon Mirandaab3c6812021-06-28 15:42:28 -0700941 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * iconScale);
Thales Lima080d8902022-03-28 15:30:29 +0100942 cellLayoutBorderSpacePx = getCellLayoutBorderSpace(inv, scale);
Thales Lima79456492023-05-23 11:32:22 +0100943 int cellTextAndPaddingHeight =
944 iconDrawablePaddingPx + Utilities.calculateTextHeight(iconTextSizePx);
Jon Mirandae126d722021-02-25 10:45:20 -0500945
Thales Lima79456492023-05-23 11:32:22 +0100946 if (mIsResponsiveGrid) {
947 int cellContentHeight = iconSizePx + cellTextAndPaddingHeight;
948
949 cellWidthPx = mResponsiveWidthSpec.getCellSizePx();
950 cellHeightPx = mResponsiveHeightSpec.getCellSizePx();
951 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
952
953 // TODO(b/283929701): decrease icon size if content doesn't fit on cell
954 } else if (mIsScalableGrid) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100955 cellWidthPx = pxFromDp(inv.minCellSize[mTypeIndex].x, mMetrics, scale);
956 cellHeightPx = pxFromDp(inv.minCellSize[mTypeIndex].y, mMetrics, scale);
Alex Chaube753132022-12-09 16:46:54 +0000957
958 if (cellWidthPx < iconSizePx) {
959 // If cellWidth no longer fit iconSize, reduce borderSpace to make cellWidth bigger.
960 int numColumns = getPanelCount() * inv.numColumns;
961 int numBorders = numColumns - 1;
962 int extraWidthRequired = (iconSizePx - cellWidthPx) * numColumns;
963 if (cellLayoutBorderSpacePx.x * numBorders >= extraWidthRequired) {
964 cellWidthPx = iconSizePx;
965 cellLayoutBorderSpacePx.x -= extraWidthRequired / numBorders;
966 } else {
967 // If it still doesn't fit, set borderSpace to 0 and distribute the space for
968 // cellWidth, and reduce iconSize.
969 cellWidthPx = (cellWidthPx * numColumns
970 + cellLayoutBorderSpacePx.x * numBorders) / numColumns;
971 iconSizePx = Math.min(iconSizePx, cellWidthPx);
972 cellLayoutBorderSpacePx.x = 0;
973 }
974 }
975
Alex Chaube753132022-12-09 16:46:54 +0000976 int cellContentHeight = iconSizePx + cellTextAndPaddingHeight;
977 if (cellHeightPx < cellContentHeight) {
978 // If cellHeight no longer fit iconSize, reduce borderSpace to make cellHeight
979 // bigger.
980 int numBorders = inv.numRows - 1;
981 int extraHeightRequired = (cellContentHeight - cellHeightPx) * inv.numRows;
982 if (cellLayoutBorderSpacePx.y * numBorders >= extraHeightRequired) {
983 cellHeightPx = cellContentHeight;
984 cellLayoutBorderSpacePx.y -= extraHeightRequired / numBorders;
985 } else {
Alex Chau06f36e82023-01-06 10:50:54 +0000986 // If it still doesn't fit, set borderSpace to 0 to recover space.
Alex Chaube753132022-12-09 16:46:54 +0000987 cellHeightPx = (cellHeightPx * inv.numRows
988 + cellLayoutBorderSpacePx.y * numBorders) / inv.numRows;
Alex Chaube753132022-12-09 16:46:54 +0000989 cellLayoutBorderSpacePx.y = 0;
Alex Chau06f36e82023-01-06 10:50:54 +0000990 // Reduce iconDrawablePaddingPx to make cellContentHeight smaller.
991 int cellContentWithoutPadding = cellContentHeight - iconDrawablePaddingPx;
992 if (cellContentWithoutPadding <= cellHeightPx) {
993 iconDrawablePaddingPx = cellContentHeight - cellHeightPx;
994 } else {
995 // If it still doesn't fit, set iconDrawablePaddingPx to 0 to recover space,
996 // then proportional reduce iconSizePx and iconTextSizePx to fit.
997 iconDrawablePaddingPx = 0;
998 float ratio = cellHeightPx / (float) cellContentWithoutPadding;
999 iconSizePx = (int) (iconSizePx * ratio);
1000 iconTextSizePx = (int) (iconTextSizePx * ratio);
1001 }
1002 cellTextAndPaddingHeight =
1003 iconDrawablePaddingPx + Utilities.calculateTextHeight(iconTextSizePx);
Alex Chaube753132022-12-09 16:46:54 +00001004 }
1005 cellContentHeight = iconSizePx + cellTextAndPaddingHeight;
1006 }
Jon Mirandae126d722021-02-25 10:45:20 -05001007 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
Thales Limad90faab2021-09-21 17:18:47 +01001008 desiredWorkspaceHorizontalMarginPx =
1009 (int) (desiredWorkspaceHorizontalMarginOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -05001010 } else {
Jon Mirandae126d722021-02-25 10:45:20 -05001011 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001012 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR)
1013 + iconDrawablePaddingPx
Jon Miranda228877d2021-02-09 11:05:00 -05001014 + Utilities.calculateTextHeight(iconTextSizePx);
1015 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
1016 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
1017 && !isMultiWindowMode) {
1018 // Ensures that the label is closer to its corresponding icon. This is not an issue
1019 // with vertical bar layout or multi-window mode since the issue is handled
1020 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
1021 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
1022 iconDrawablePaddingPx = cellPaddingY;
1023 }
Jon Miranda846455e2017-10-02 14:58:52 -07001024 }
Jon Miranda18751b62017-07-31 17:25:27 -07001025
Sunny Goyalae190ff2020-04-14 00:19:01 +00001026 // All apps
Brian Isganitis25b2ac82022-01-28 16:25:13 -05001027 updateAllAppsIconSize(scale, res);
Winson Chungb3800242013-10-24 11:01:54 -07001028
Thales Limab8c05952022-05-23 16:58:38 +01001029 updateHotseatSizes(iconSizePx);
Winson Chungb3800242013-10-24 11:01:54 -07001030
Sunny Goyalbaec6ff2016-09-14 11:26:21 -07001031 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -07001032 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
1033 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Sunny Goyal6c304b12023-04-10 12:03:04 -07001034
1035 // Update widget padding:
1036 float minSpacing = pxFromDp(MIN_WIDGET_PADDING_DP, mMetrics);
1037 if (cellLayoutBorderSpacePx.x < minSpacing
1038 || cellLayoutBorderSpacePx.y < minSpacing) {
1039 widgetPadding.left = widgetPadding.right =
1040 Math.round(Math.max(0, minSpacing - cellLayoutBorderSpacePx.x));
1041 widgetPadding.top = widgetPadding.bottom =
1042 Math.round(Math.max(0, minSpacing - cellLayoutBorderSpacePx.y));
1043 } else {
1044 widgetPadding.setEmpty();
1045 }
Winson Chung0f785722015-04-08 10:27:49 -07001046 }
1047
Thales Lima425f6822022-05-10 13:44:58 -03001048 /**
Thales Lima6a590062022-11-22 15:52:49 +00001049 * This method calculates the space between the icons to achieve a certain width.
Thales Lima425f6822022-05-10 13:44:58 -03001050 */
Thales Lima6a590062022-11-22 15:52:49 +00001051 private int calculateHotseatBorderSpace(float hotseatWidthPx, int numExtraBorder) {
Thales Lima9938c2f2022-07-25 14:38:16 +01001052 float hotseatIconsTotalPx = iconSizePx * numShownHotseatIcons;
Thales Limaffc68b02023-01-09 16:20:23 +00001053 int hotseatBorderSpacePx =
Thales Lima6a590062022-11-22 15:52:49 +00001054 (int) (hotseatWidthPx - hotseatIconsTotalPx)
1055 / (numShownHotseatIcons - 1 + numExtraBorder);
Thales Limaffc68b02023-01-09 16:20:23 +00001056 return Math.min(hotseatBorderSpacePx, mMaxHotseatIconSpacePx);
Thales Lima425f6822022-05-10 13:44:58 -03001057 }
1058
Brian Isganitis25b2ac82022-01-28 16:25:13 -05001059
1060 /**
1061 * Updates the iconSize for allApps* variants.
1062 */
Brian Isganitisa9a78112022-05-23 18:11:47 -07001063 private void updateAllAppsIconSize(float scale, Resources res) {
Thales Lima080d8902022-03-28 15:30:29 +01001064 allAppsBorderSpacePx = new Point(
1065 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].x, mMetrics, scale),
1066 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].y, mMetrics, scale));
Brandon Dayauon07ca8842022-04-27 10:52:28 -07001067 // AllApps cells don't have real space between cells,
1068 // so we add the border space to the cell height
Alex Chaube753132022-12-09 16:46:54 +00001069 allAppsCellHeightPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].y, mMetrics)
Brandon Dayauon07ca8842022-04-27 10:52:28 -07001070 + allAppsBorderSpacePx.y;
1071 // but width is just the cell,
1072 // the border is added in #updateAllAppsContainerWidth
Thales Lima79456492023-05-23 11:32:22 +01001073 if (mIsScalableGrid) {
Alex Chaube753132022-12-09 16:46:54 +00001074 allAppsIconSizePx = pxFromDp(inv.allAppsIconSize[mTypeIndex], mMetrics);
1075 allAppsIconTextSizePx = pxFromSp(inv.allAppsIconTextSize[mTypeIndex], mMetrics);
Brian Isganitis25b2ac82022-01-28 16:25:13 -05001076 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Brandon Dayauon7a8a9ed2022-06-06 14:32:22 -07001077 allAppsCellWidthPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].x, mMetrics, scale);
Alex Chaube753132022-12-09 16:46:54 +00001078
1079 if (allAppsCellWidthPx < allAppsIconSizePx) {
1080 // If allAppsCellWidth no longer fit allAppsIconSize, reduce allAppsBorderSpace to
1081 // make allAppsCellWidth bigger.
1082 int numBorders = inv.numAllAppsColumns - 1;
1083 int extraWidthRequired =
1084 (allAppsIconSizePx - allAppsCellWidthPx) * inv.numAllAppsColumns;
1085 if (allAppsBorderSpacePx.x * numBorders >= extraWidthRequired) {
1086 allAppsCellWidthPx = allAppsIconSizePx;
1087 allAppsBorderSpacePx.x -= extraWidthRequired / numBorders;
1088 } else {
1089 // If it still doesn't fit, set allAppsBorderSpace to 0 and distribute the space
1090 // for allAppsCellWidth, and reduce allAppsIconSize.
1091 allAppsCellWidthPx = (allAppsCellWidthPx * inv.numAllAppsColumns
1092 + allAppsBorderSpacePx.x * numBorders) / inv.numAllAppsColumns;
1093 allAppsIconSizePx = Math.min(allAppsIconSizePx, allAppsCellWidthPx);
1094 allAppsBorderSpacePx.x = 0;
1095 }
1096 }
1097
1098 int cellContentHeight = allAppsIconSizePx
1099 + Utilities.calculateTextHeight(allAppsIconTextSizePx) + allAppsBorderSpacePx.y;
1100 if (allAppsCellHeightPx < cellContentHeight) {
1101 // Increase allAppsCellHeight to fit its content.
1102 allAppsCellHeightPx = cellContentHeight;
1103 }
Brian Isganitis25b2ac82022-01-28 16:25:13 -05001104 } else {
Brandon Dayauon07ca8842022-04-27 10:52:28 -07001105 float invIconSizeDp = inv.allAppsIconSize[mTypeIndex];
1106 float invIconTextSizeSp = inv.allAppsIconTextSize[mTypeIndex];
Brian Isganitis25b2ac82022-01-28 16:25:13 -05001107 allAppsIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
1108 allAppsIconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * scale);
Brandon Dayauon07ca8842022-04-27 10:52:28 -07001109 allAppsIconDrawablePaddingPx =
1110 res.getDimensionPixelSize(R.dimen.all_apps_icon_drawable_padding);
Brandon Dayauon7a8a9ed2022-06-06 14:32:22 -07001111 allAppsCellWidthPx = allAppsIconSizePx + (2 * allAppsIconDrawablePaddingPx);
Brian Isganitis25b2ac82022-01-28 16:25:13 -05001112 }
1113
Thales Limab7ef5692022-03-10 10:32:36 +00001114 updateAllAppsContainerWidth(res);
Brian Isganitis25b2ac82022-01-28 16:25:13 -05001115 if (isVerticalBarLayout()) {
1116 hideWorkspaceLabelsIfNotEnoughSpace();
1117 }
1118 }
1119
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001120 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001121 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -07001122
Thales Lima7eee74b2023-03-16 16:00:55 -03001123 // For usability we can't have the folder use the whole width of the screen
Sunny Goyal07b69292018-01-08 14:19:34 -08001124 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -08001125
Thales Lima7eee74b2023-03-16 16:00:55 -03001126 // Check if the folder fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -05001127 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Thales Lima7eee74b2023-03-16 16:00:55 -03001128 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacePx)
1129 + folderFooterHeightPx
1130 + folderContentPaddingTop;
1131 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -08001132 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -08001133
Thales Lima7eee74b2023-03-16 16:00:55 -03001134 // Check if the folder fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -05001135 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Thales Lima7eee74b2023-03-16 16:00:55 -03001136 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacePx)
1137 + folderContentPaddingLeftRight * 2;
1138 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -08001139 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -08001140
1141 float scale = Math.min(scaleX, scaleY);
1142 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001143 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -07001144 }
1145 }
1146
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001147 private void updateFolderCellSize(float scale, Resources res) {
Alex Chau0d4f1ac2022-12-20 12:31:14 +00001148 float invIconSizeDp = inv.iconSize[mTypeIndex];
Sunny Goyal35c7b192021-04-20 16:51:10 -07001149 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Alex Chau0d4f1ac2022-12-20 12:31:14 +00001150 folderChildTextSizePx = pxFromSp(inv.iconTextSize[mTypeIndex], mMetrics, scale);
Thales Lima7eee74b2023-03-16 16:00:55 -03001151 folderLabelTextSizePx = Math.max(pxFromSp(MIN_FOLDER_TEXT_SIZE_SP, mMetrics, scale),
Thales Limab35faed2022-09-05 16:30:01 -03001152 (int) (folderChildTextSizePx * folderLabelTextScale));
Jon Mirandabf7d8122016-11-03 15:29:29 -07001153
1154 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -07001155
Thales Lima79456492023-05-23 11:32:22 +01001156 if (mIsScalableGrid) {
Thales Limab35faed2022-09-05 16:30:01 -03001157 if (inv.folderStyle == INVALID_RESOURCE_HANDLE) {
Thales Lima7eee74b2023-03-16 16:00:55 -03001158 folderCellWidthPx = roundPxValueFromFloat(getCellSize().x * scale);
1159 folderCellHeightPx = roundPxValueFromFloat(getCellSize().y * scale);
1160 } else {
1161 folderCellWidthPx = roundPxValueFromFloat(folderCellWidthPx * scale);
1162 folderCellHeightPx = roundPxValueFromFloat(folderCellHeightPx * scale);
Thales Limab35faed2022-09-05 16:30:01 -03001163 }
Jon Mirandaf33f5b32021-07-22 17:15:31 -07001164
Thales Lima7eee74b2023-03-16 16:00:55 -03001165 folderContentPaddingTop = roundPxValueFromFloat(folderContentPaddingTop * scale);
1166 folderCellLayoutBorderSpacePx = roundPxValueFromFloat(
1167 folderCellLayoutBorderSpacePx * scale);
1168 folderFooterHeightPx = roundPxValueFromFloat(folderFooterHeightPx * scale);
1169
Thales Limab35faed2022-09-05 16:30:01 -03001170 folderContentPaddingLeftRight = folderCellLayoutBorderSpacePx;
Jon Miranda823da222021-03-23 08:08:45 -04001171 } else {
1172 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
1173 * scale);
1174 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
1175 * scale);
1176
1177 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
1178 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
Thales Lima7eee74b2023-03-16 16:00:55 -03001179 folderContentPaddingTop = roundPxValueFromFloat(folderContentPaddingTop * scale);
Thales Limab35faed2022-09-05 16:30:01 -03001180 folderContentPaddingLeftRight =
1181 res.getDimensionPixelSize(R.dimen.folder_content_padding_left_right);
1182 folderFooterHeightPx =
Thales Lima7eee74b2023-03-16 16:00:55 -03001183 roundPxValueFromFloat(
1184 res.getDimensionPixelSize(R.dimen.folder_footer_height_default)
1185 * scale);
1186
Jon Miranda823da222021-03-23 08:08:45 -04001187 }
1188
Jonathan Miranda9ad87462017-07-26 17:41:02 +00001189 folderChildDrawablePaddingPx = Math.max(0,
1190 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -07001191 }
1192
Winson1f064272016-07-18 17:18:02 -07001193 public void updateInsets(Rect insets) {
1194 mInsets.set(insets);
1195 }
1196
Sunny Goyalae6e3182019-04-30 12:04:37 -07001197 /**
1198 * The current device insets. This is generally same as the insets being dispatched to
1199 * {@link Insettable} elements, but can differ if the element is using a different profile.
1200 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -08001201 public Rect getInsets() {
1202 return mInsets;
1203 }
1204
Sunny Goyal756cd262015-08-20 12:33:21 -07001205 public Point getCellSize() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001206 return getCellSize(null);
Jon Miranda6f7e9702019-09-16 14:44:14 -07001207 }
1208
Sunny Goyal19ff7282021-04-22 10:12:54 -07001209 public Point getCellSize(Point result) {
1210 if (result == null) {
1211 result = new Point();
1212 }
Thales Limad1df5fc2021-09-03 18:37:19 +01001213
Pat Manning25d53342022-05-10 09:58:49 +00001214 int shortcutAndWidgetContainerWidth =
1215 getCellLayoutWidth() - (cellLayoutPaddingPx.left + cellLayoutPaddingPx.right);
1216 result.x = calculateCellWidth(shortcutAndWidgetContainerWidth, cellLayoutBorderSpacePx.x,
1217 inv.numColumns);
1218 int shortcutAndWidgetContainerHeight =
1219 getCellLayoutHeight() - (cellLayoutPaddingPx.top + cellLayoutPaddingPx.bottom);
1220 result.y = calculateCellHeight(shortcutAndWidgetContainerHeight, cellLayoutBorderSpacePx.y,
1221 inv.numRows);
Sunny Goyal3e8a04b2022-05-09 19:31:45 +00001222 return result;
Pat Manningb45d6c42022-05-03 14:32:06 +01001223 }
1224
1225 /**
Thales Lima1e8b45f2022-08-25 11:50:59 -04001226 * Returns the left and right space on the cell, which is the cell width - icon size
1227 */
1228 public int getCellHorizontalSpace() {
1229 return getCellSize().x - iconSizePx;
1230 }
1231
1232 /**
Pat Manning25d53342022-05-10 09:58:49 +00001233 * Gets the number of panels within the workspace.
1234 */
1235 public int getPanelCount() {
1236 return isTwoPanels ? 2 : 1;
1237 }
1238
1239 /**
Pat Manningde25c0d2022-04-05 19:11:26 +01001240 * Gets the space in px from the bottom of last item in the vertical-bar hotseat to the
1241 * bottom of the screen.
1242 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001243 private int getVerticalHotseatLastItemBottomOffset(Context context) {
1244 Rect hotseatBarPadding = getHotseatLayoutPadding(context);
Pat Manningde25c0d2022-04-05 19:11:26 +01001245 int cellHeight = calculateCellHeight(
Pat Manning5f74bfd2022-07-20 12:08:54 +01001246 heightPx - hotseatBarPadding.top - hotseatBarPadding.bottom, hotseatBorderSpace,
Pat Manningde25c0d2022-04-05 19:11:26 +01001247 numShownHotseatIcons);
Pat Manningde25c0d2022-04-05 19:11:26 +01001248 int extraIconEndSpacing = (cellHeight - iconSizePx) / 2;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001249 return extraIconEndSpacing + hotseatBarPadding.bottom;
Pat Manningde25c0d2022-04-05 19:11:26 +01001250 }
1251
1252 /**
1253 * Gets the scaled top of the workspace in px for the spring-loaded edit state.
1254 */
Pat Manning25d53342022-05-10 09:58:49 +00001255 public float getCellLayoutSpringLoadShrunkTop() {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001256 return mInsets.top + dropTargetBarTopMarginPx + dropTargetBarSizePx
Pat Manningde25c0d2022-04-05 19:11:26 +01001257 + dropTargetBarBottomMarginPx;
Pat Manningde25c0d2022-04-05 19:11:26 +01001258 }
1259
1260 /**
1261 * Gets the scaled bottom of the workspace in px for the spring-loaded edit state.
1262 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001263 public float getCellLayoutSpringLoadShrunkBottom(Context context) {
Pat Manningde25c0d2022-04-05 19:11:26 +01001264 int topOfHotseat = hotseatBarSizePx + springLoadedHotseatBarTopMarginPx;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001265 return heightPx - (isVerticalBarLayout()
1266 ? getVerticalHotseatLastItemBottomOffset(context) : topOfHotseat);
Pat Manningde25c0d2022-04-05 19:11:26 +01001267 }
1268
Pat Manningea5c1d22022-04-14 10:58:16 +01001269 /**
Pat Manninga2e14992022-04-22 11:29:17 +01001270 * Gets the scale of the workspace for the spring-loaded edit state.
1271 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001272 public float getWorkspaceSpringLoadScale(Context context) {
1273 float scale =
1274 (getCellLayoutSpringLoadShrunkBottom(context) - getCellLayoutSpringLoadShrunkTop())
1275 / getCellLayoutHeight();
Pat Manninga2e14992022-04-22 11:29:17 +01001276 scale = Math.min(scale, 1f);
1277
Pat Manninge63dd252022-08-02 16:15:29 +01001278 // Reduce scale if next pages would not be visible after scaling the workspace.
Pat Manning25d53342022-05-10 09:58:49 +00001279 int workspaceWidth = availableWidthPx;
Pat Manninga2e14992022-04-22 11:29:17 +01001280 float scaledWorkspaceWidth = workspaceWidth * scale;
Alex Chau906d8822022-05-23 10:42:25 +01001281 float maxAvailableWidth = workspaceWidth - (2 * workspaceSpringLoadedMinNextPageVisiblePx);
Pat Manninga2e14992022-04-22 11:29:17 +01001282 if (scaledWorkspaceWidth > maxAvailableWidth) {
1283 scale *= maxAvailableWidth / scaledWorkspaceWidth;
1284 }
1285 return scale;
1286 }
1287
Pat Manning25d53342022-05-10 09:58:49 +00001288 /**
1289 * Gets the width of a single Cell Layout, aka a single panel within a Workspace.
1290 *
1291 * <p>This is the width of a Workspace, less its horizontal padding. Note that two-panel
1292 * layouts have two Cell Layouts per workspace.
1293 */
1294 public int getCellLayoutWidth() {
1295 return (availableWidthPx - getTotalWorkspacePadding().x) / getPanelCount();
Alex Chau60953332021-11-24 12:41:07 +00001296 }
1297
Pat Manning25d53342022-05-10 09:58:49 +00001298 /**
1299 * Gets the height of a single Cell Layout, aka a single panel within a Workspace.
1300 *
1301 * <p>This is the height of a Workspace, less its vertical padding.
1302 */
1303 public int getCellLayoutHeight() {
1304 return availableHeightPx - getTotalWorkspacePadding().y;
Pat Manning08610ca2022-04-05 21:03:16 +01001305 }
1306
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001307 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -08001308 return new Point(workspacePadding.left + workspacePadding.right,
1309 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001310 }
1311
1312 /**
Sunny Goyal07b69292018-01-08 14:19:34 -08001313 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
1314 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001315 */
Sunny Goyal07b69292018-01-08 14:19:34 -08001316 private void updateWorkspacePadding() {
1317 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -07001318 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001319 padding.top = 0;
1320 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001321 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -07001322 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -07001323 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -07001324 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -07001325 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -07001326 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -07001327 }
Winson Chungb3800242013-10-24 11:01:54 -07001328 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001329 // Pad the bottom of the workspace with hotseat bar
1330 // and leave a bit of space in case a widget go all the way down
Thales Lima79456492023-05-23 11:32:22 +01001331 int paddingBottom = hotseatBarSizePx + workspaceBottomPadding - mInsets.bottom;
1332 if (!mIsResponsiveGrid) {
1333 paddingBottom +=
1334 workspacePageIndicatorHeight - mWorkspacePageIndicatorOverlapWorkspace;
1335 }
1336 int paddingTop = workspaceTopPadding + (mIsScalableGrid ? 0 : edgeMarginPx);
Pat Manning08610ca2022-04-05 21:03:16 +01001337 int paddingSide = desiredWorkspaceHorizontalMarginPx;
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001338
Pat Manning08610ca2022-04-05 21:03:16 +01001339 padding.set(paddingSide, paddingTop, paddingSide, paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -07001340 }
Pat Manning08610ca2022-04-05 21:03:16 +01001341 insetPadding(workspacePadding, cellLayoutPaddingPx);
1342 }
1343
1344 private void insetPadding(Rect paddings, Rect insets) {
1345 insets.left = Math.min(insets.left, paddings.left);
1346 paddings.left -= insets.left;
1347
1348 insets.top = Math.min(insets.top, paddings.top);
1349 paddings.top -= insets.top;
1350
1351 insets.right = Math.min(insets.right, paddings.right);
1352 paddings.right -= insets.right;
1353
1354 insets.bottom = Math.min(insets.bottom, paddings.bottom);
1355 paddings.bottom -= insets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -07001356 }
1357
Sunny Goyal57b22792021-05-25 14:35:01 -07001358 /**
1359 * Returns the padding for hotseat view
1360 */
1361 public Rect getHotseatLayoutPadding(Context context) {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001362 Rect hotseatBarPadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001363 if (isVerticalBarLayout()) {
Sihua Ma38bb3b02022-03-21 22:20:14 -07001364 // The hotseat icons will be placed in the middle of the hotseat cells.
1365 // Changing the hotseatCellHeightPx is not affecting hotseat icon positions
1366 // in vertical bar layout.
1367 // Workspace icons are moved up by a small factor. The variable diffOverlapFactor
1368 // is set to account for that difference.
1369 float diffOverlapFactor = iconSizePx * (ICON_OVERLAP_FACTOR - 1) / 2;
Pat Manning08610ca2022-04-05 21:03:16 +01001370 int paddingTop = Math.max((int) (mInsets.top + cellLayoutPaddingPx.top
1371 - diffOverlapFactor), 0);
1372 int paddingBottom = Math.max((int) (mInsets.bottom + cellLayoutPaddingPx.bottom
Sihua Ma38bb3b02022-03-21 22:20:14 -07001373 + diffOverlapFactor), 0);
1374
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001375 if (isSeascape()) {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001376 hotseatBarPadding.set(mInsets.left + hotseatBarSidePaddingStartPx, paddingTop,
Pat Manning08610ca2022-04-05 21:03:16 +01001377 hotseatBarSidePaddingEndPx, paddingBottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001378 } else {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001379 hotseatBarPadding.set(hotseatBarSidePaddingEndPx, paddingTop,
Pat Manning08610ca2022-04-05 21:03:16 +01001380 mInsets.right + hotseatBarSidePaddingStartPx, paddingBottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001381 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001382 } else if (isTaskbarPresent) {
Alex Chau51da2192022-05-20 13:32:10 +01001383 // Center the QSB vertically with hotseat
Thales Limab8c05952022-05-23 16:58:38 +01001384 int hotseatBarBottomPadding = getHotseatBarBottomPadding();
1385 int hotseatBarTopPadding =
1386 hotseatBarSizePx - hotseatBarBottomPadding - hotseatCellHeightPx;
Alex Chau51da2192022-05-20 13:32:10 +01001387
Thales Lima6a590062022-11-22 15:52:49 +00001388 int hotseatWidth = getHotseatRequiredWidth();
Helen Cheuke76291f2023-02-14 17:11:05 +00001389 int startSpacing;
1390 int endSpacing;
Thales Lima6a590062022-11-22 15:52:49 +00001391 // Hotseat aligns to the left with nav buttons
1392 if (hotseatBarEndOffset > 0) {
Jon Mirandae17a9492023-03-08 16:42:20 -08001393 startSpacing = inlineNavButtonsEndSpacingPx;
Helen Cheuke76291f2023-02-14 17:11:05 +00001394 endSpacing = availableWidthPx - hotseatWidth - startSpacing + hotseatBorderSpace;
1395 } else {
1396 startSpacing = (availableWidthPx - hotseatWidth) / 2;
1397 endSpacing = startSpacing;
Thales Lima6a590062022-11-22 15:52:49 +00001398 }
Helen Cheuke76291f2023-02-14 17:11:05 +00001399 startSpacing += getAdditionalQsbSpace();
Thales Lima612230d2022-03-31 18:04:37 +01001400
Helen Cheuke76291f2023-02-14 17:11:05 +00001401 hotseatBarPadding.top = hotseatBarTopPadding;
1402 hotseatBarPadding.bottom = hotseatBarBottomPadding;
Alex Chau51da2192022-05-20 13:32:10 +01001403 boolean isRtl = Utilities.isRtl(context.getResources());
Thales Lima612230d2022-03-31 18:04:37 +01001404 if (isRtl) {
Helen Cheuke76291f2023-02-14 17:11:05 +00001405 hotseatBarPadding.left = endSpacing;
1406 hotseatBarPadding.right = startSpacing;
Thales Lima612230d2022-03-31 18:04:37 +01001407 } else {
Helen Cheuke76291f2023-02-14 17:11:05 +00001408 hotseatBarPadding.left = startSpacing;
1409 hotseatBarPadding.right = endSpacing;
Thales Lima612230d2022-03-31 18:04:37 +01001410 }
Helen Cheuke76291f2023-02-14 17:11:05 +00001411
Thales Lima79456492023-05-23 11:32:22 +01001412 } else if (mIsScalableGrid) {
Alex Chau206ede92022-08-01 17:46:12 +01001413 int sideSpacing = (availableWidthPx - hotseatQsbWidth) / 2;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001414 hotseatBarPadding.set(sideSpacing,
Thales Limab8c05952022-05-23 16:58:38 +01001415 0,
Thales Lima425f6822022-05-10 13:44:58 -03001416 sideSpacing,
Thales Limab8c05952022-05-23 16:58:38 +01001417 getHotseatBarBottomPadding());
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001418 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001419 // We want the edges of the hotseat to line up with the edges of the workspace, but the
1420 // icons in the hotseat are a different size, and so don't line up perfectly. To account
1421 // for this, we pad the left and right of the hotseat with half of the difference of a
1422 // workspace cell vs a hotseat cell.
1423 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001424 float hotseatCellWidth = (float) widthPx / numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001425 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Pat Manning5f74bfd2022-07-20 12:08:54 +01001426 hotseatBarPadding.set(
Thales Limab8c05952022-05-23 16:58:38 +01001427 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingPx.left
1428 + mInsets.left,
1429 0,
Pat Manning08610ca2022-04-05 21:03:16 +01001430 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingPx.right
Sunny Goyal786940a2020-06-02 02:31:31 -07001431 + mInsets.right,
Thales Limab8c05952022-05-23 16:58:38 +01001432 getHotseatBarBottomPadding());
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001433 }
Pat Manning5f74bfd2022-07-20 12:08:54 +01001434 return hotseatBarPadding;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001435 }
1436
Thales Lima9938c2f2022-07-25 14:38:16 +01001437 private int getAdditionalQsbSpace() {
1438 return isQsbInline ? hotseatQsbWidth + hotseatBorderSpace : 0;
1439 }
1440
1441 /**
1442 * Calculate how much space the hotseat needs to be shown completely
1443 */
1444 private int getHotseatRequiredWidth() {
1445 int additionalQsbSpace = getAdditionalQsbSpace();
1446 return iconSizePx * numShownHotseatIcons
Thales Lima6a590062022-11-22 15:52:49 +00001447 + hotseatBorderSpace * (numShownHotseatIcons - (areNavButtonsInline ? 0 : 1))
Thales Lima9938c2f2022-07-25 14:38:16 +01001448 + additionalQsbSpace;
1449 }
1450
Winsonfadbe8f2016-07-22 16:35:37 -07001451 /**
Sunny Goyal57b22792021-05-25 14:35:01 -07001452 * Returns the number of pixels the QSB is translated from the bottom of the screen.
1453 */
1454 public int getQsbOffsetY() {
Thales Limaa1012902022-01-13 17:58:42 +00001455 if (isQsbInline) {
Thales Limab8c05952022-05-23 16:58:38 +01001456 return getHotseatBarBottomPadding() - ((hotseatQsbHeight - hotseatCellHeightPx) / 2);
1457 } else if (isTaskbarPresent) { // QSB on top
1458 return hotseatBarSizePx - hotseatQsbHeight + hotseatQsbShadowHeight;
Jonathan Miranda8f16a772021-07-23 23:10:37 +00001459 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001460 return hotseatBarBottomSpacePx - hotseatQsbShadowHeight;
Jonathan Miranda7a273e22021-06-24 21:29:10 +00001461 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001462 }
1463
Thales Limab8c05952022-05-23 16:58:38 +01001464 /**
1465 * Returns the number of pixels the hotseat is translated from the bottom of the screen.
1466 */
1467 private int getHotseatBarBottomPadding() {
1468 if (isTaskbarPresent) { // QSB on top or inline
1469 return hotseatBarBottomSpacePx - (Math.abs(hotseatCellHeightPx - iconSizePx) / 2);
Thales Limaa1012902022-01-13 17:58:42 +00001470 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001471 return hotseatBarSizePx - hotseatCellHeightPx;
Thales Limaa1012902022-01-13 17:58:42 +00001472 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001473 }
1474
1475 /**
Alex Chau51da2192022-05-20 13:32:10 +01001476 * Returns the number of pixels the taskbar is translated from the bottom of the screen.
1477 */
1478 public int getTaskbarOffsetY() {
Jon Miranda9c478b62023-03-23 21:38:49 -07001479 int taskbarIconBottomSpace = (taskbarHeight - iconSizePx) / 2;
Alex Chau51da2192022-05-20 13:32:10 +01001480 int launcherIconBottomSpace =
1481 Math.min((hotseatCellHeightPx - iconSizePx) / 2, gridVisualizationPaddingY);
Thales Limab8c05952022-05-23 16:58:38 +01001482 return getHotseatBarBottomPadding() + launcherIconBottomSpace - taskbarIconBottomSpace;
Alex Chau51da2192022-05-20 13:32:10 +01001483 }
1484
1485 /**
Alex Chaua2fc7642022-04-21 14:20:23 +01001486 * Returns the number of pixels required below OverviewActions excluding insets.
1487 */
1488 public int getOverviewActionsClaimedSpaceBelow() {
Alex Chaud67ddc42022-09-30 18:15:56 +01001489 if (isTaskbarPresent) {
Jon Miranda9c478b62023-03-23 21:38:49 -07001490 return taskbarHeight + taskbarBottomMargin * 2;
Alex Chaud67ddc42022-09-30 18:15:56 +01001491 }
1492 return mInsets.bottom;
Alex Chaua2fc7642022-04-21 14:20:23 +01001493 }
1494
1495 /** Gets the space that the overview actions will take, including bottom margin. */
1496 public int getOverviewActionsClaimedSpace() {
Pat Manning7b8f7662023-01-18 14:48:30 +00001497 int overviewActionsSpace = isTablet && FeatureFlags.ENABLE_GRID_ONLY_OVERVIEW.get()
1498 ? 0
1499 : (overviewActionsTopMarginPx + overviewActionsHeight);
1500 return overviewActionsSpace + getOverviewActionsClaimedSpaceBelow();
Alex Chaua2fc7642022-04-21 14:20:23 +01001501 }
1502
1503 /**
Sihua Mae04aa202022-07-18 12:43:56 -07001504 * Takes the View and return the scales of width and height depending on the DeviceProfile
1505 * specifications
1506 *
1507 * @param itemInfo The tag of the widget view
1508 * @return A PointF instance with the x set to be the scale of width, and y being the scale of
1509 * height
1510 */
1511 @NonNull
1512 public PointF getAppWidgetScale(@Nullable final ItemInfo itemInfo) {
1513 return mViewScaleProvider.getScaleFromItemInfo(itemInfo);
1514 }
1515
1516 /**
Winsonfadbe8f2016-07-22 16:35:37 -07001517 * @return the bounds for which the open folders should be contained within
1518 */
1519 public Rect getAbsoluteOpenFolderBounds() {
1520 if (isVerticalBarLayout()) {
1521 // Folders should only appear right of the drop target bar and left of the hotseat
1522 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
1523 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -07001524 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -07001525 mInsets.top + availableHeightPx);
1526 } else {
1527 // Folders should only appear below the drop target bar and above the hotseat
Jon Miranda9c478b62023-03-23 21:38:49 -07001528 int hotseatTop = isTaskbarPresent ? taskbarHeight : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -08001529 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -07001530 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -08001531 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -08001532 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -07001533 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -07001534 }
1535 }
1536
Jon Miranda228877d2021-02-09 11:05:00 -05001537 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
1538 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -07001539 }
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +01001540
Jon Miranda228877d2021-02-09 11:05:00 -05001541 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
1542 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -07001543 }
1544
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -07001545 /**
Tony Wickham55616cd2015-09-23 14:55:17 -07001546 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
1547 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
1548 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -07001549 */
Tony Wickhamab946a12015-09-16 15:38:16 -07001550 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -07001551 return isLandscape && transposeLayoutWithOrientation;
1552 }
1553
Sunny Goyal59d086c2018-05-07 17:31:40 -07001554 /**
1555 * Updates orientation information and returns true if it has changed from the previous value.
1556 */
Winson Chung13c1c2c2019-09-06 11:46:19 -07001557 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001558 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001559 boolean isSeascape = DisplayController.INSTANCE.get(context)
1560 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -07001561 if (mIsSeascape != isSeascape) {
1562 mIsSeascape = isSeascape;
Pat Manning08610ca2022-04-05 21:03:16 +01001563 // Hotseat changing sides requires updating workspace left/right paddings
1564 updateWorkspacePadding();
Sunny Goyal59d086c2018-05-07 17:31:40 -07001565 return true;
1566 }
1567 }
1568 return false;
1569 }
1570
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001571 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001572 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001573 }
1574
Sunny Goyal07b69292018-01-08 14:19:34 -08001575 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001576 return isVerticalBarLayout();
Winson Chungb3800242013-10-24 11:01:54 -07001577 }
1578
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001579 public int getCellContentHeight(@ContainerType int containerType) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001580 switch (containerType) {
1581 case CellLayout.WORKSPACE:
1582 return cellHeightPx;
1583 case CellLayout.FOLDER:
1584 return folderCellHeightPx;
1585 case CellLayout.HOTSEAT:
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001586 // The hotseat is the only container where the cell height is going to be
1587 // different from the content within that cell.
1588 return iconSizePx;
Sunny Goyalc13403c2016-11-18 23:44:48 -08001589 default:
1590 // ??
1591 return 0;
1592 }
1593 }
Sunny Goyal13178ac2016-04-04 16:35:22 -07001594
Jon Miranda58561d42021-03-17 10:51:54 -04001595 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001596 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -04001597 }
1598
Alex Chaue818bcb2022-09-02 17:27:11 +01001599 private String dpPointFToString(String name, PointF value) {
1600 return String.format(Locale.ENGLISH, "\t%s: PointF(%.1f, %.1f)dp", name, value.x, value.y);
1601 }
1602
Pat Manning5f74bfd2022-07-20 12:08:54 +01001603 /** Dumps various DeviceProfile variables to the specified writer. */
1604 public void dump(Context context, String prefix, PrintWriter writer) {
Jon Miranda58561d42021-03-17 10:51:54 -04001605 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -07001606 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -04001607
1608 writer.println(prefix + "\tisTablet:" + isTablet);
Jon Miranda58561d42021-03-17 10:51:54 -04001609 writer.println(prefix + "\tisPhone:" + isPhone);
1610 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
1611 + transposeLayoutWithOrientation);
Alex Chau6ed408f2022-03-04 12:58:05 +00001612 writer.println(prefix + "\tisGestureMode:" + isGestureMode);
Jon Miranda58561d42021-03-17 10:51:54 -04001613
1614 writer.println(prefix + "\tisLandscape:" + isLandscape);
1615 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
Sunny Goyal19ff7282021-04-22 10:12:54 -07001616 writer.println(prefix + "\tisTwoPanels:" + isTwoPanels);
Jon Miranda58561d42021-03-17 10:51:54 -04001617
1618 writer.println(prefix + pxToDpStr("windowX", windowX));
1619 writer.println(prefix + pxToDpStr("windowY", windowY));
1620 writer.println(prefix + pxToDpStr("widthPx", widthPx));
1621 writer.println(prefix + pxToDpStr("heightPx", heightPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001622 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
1623 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
Alex Chaue0227552022-04-06 19:44:43 +01001624 writer.println(prefix + pxToDpStr("mInsets.left", mInsets.left));
1625 writer.println(prefix + pxToDpStr("mInsets.top", mInsets.top));
1626 writer.println(prefix + pxToDpStr("mInsets.right", mInsets.right));
1627 writer.println(prefix + pxToDpStr("mInsets.bottom", mInsets.bottom));
Jon Miranda58561d42021-03-17 10:51:54 -04001628
1629 writer.println(prefix + "\taspectRatio:" + aspectRatio);
1630
Thales Limac98b7812023-04-14 15:04:23 +01001631 writer.println(prefix + "\tisResponsiveGrid:" + mIsResponsiveGrid);
Thales Lima79456492023-05-23 11:32:22 +01001632 writer.println(prefix + "\tisScalableGrid:" + mIsScalableGrid);
Jon Miranda58561d42021-03-17 10:51:54 -04001633
Thales Lima83bedbf2021-10-05 17:47:39 +01001634 writer.println(prefix + "\tinv.numRows: " + inv.numRows);
Alex Chau9fee3fd2021-12-01 18:43:10 +00001635 writer.println(prefix + "\tinv.numColumns: " + inv.numColumns);
1636 writer.println(prefix + "\tinv.numSearchContainerColumns: "
1637 + inv.numSearchContainerColumns);
Jon Miranda58561d42021-03-17 10:51:54 -04001638
Alex Chaue818bcb2022-09-02 17:27:11 +01001639 writer.println(prefix + dpPointFToString("minCellSize", inv.minCellSize[mTypeIndex]));
Jon Mirandaab3c6812021-06-28 15:42:28 -07001640
Jon Miranda58561d42021-03-17 10:51:54 -04001641 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
1642 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
1643
1644 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
1645 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
1646
Thales Lima83bedbf2021-10-05 17:47:39 +01001647 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Horizontal",
1648 cellLayoutBorderSpacePx.x));
1649 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Vertical",
1650 cellLayoutBorderSpacePx.y));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001651 writer.println(
1652 prefix + pxToDpStr("cellLayoutPaddingPx.left", cellLayoutPaddingPx.left));
1653 writer.println(
1654 prefix + pxToDpStr("cellLayoutPaddingPx.top", cellLayoutPaddingPx.top));
1655 writer.println(
1656 prefix + pxToDpStr("cellLayoutPaddingPx.right", cellLayoutPaddingPx.right));
Pat Manning08610ca2022-04-05 21:03:16 +01001657 writer.println(
1658 prefix + pxToDpStr("cellLayoutPaddingPx.bottom", cellLayoutPaddingPx.bottom));
Thales Lima83bedbf2021-10-05 17:47:39 +01001659
Jon Miranda58561d42021-03-17 10:51:54 -04001660 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
1661 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
1662 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
1663
Helen Cheuk599109b2023-02-23 17:15:39 +00001664 writer.println(prefix + "\tinv.numFolderRows: " + inv.numFolderRows);
1665 writer.println(prefix + "\tinv.numFolderColumns: " + inv.numFolderColumns);
Jon Miranda58561d42021-03-17 10:51:54 -04001666 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
1667 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
1668 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
1669 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
1670 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
1671 folderChildDrawablePaddingPx));
Thales Limab35faed2022-09-05 16:30:01 -03001672 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx",
1673 folderCellLayoutBorderSpacePx));
Thales Limaa08a4432022-08-09 09:55:17 +01001674 writer.println(prefix + pxToDpStr("folderContentPaddingLeftRight",
1675 folderContentPaddingLeftRight));
1676 writer.println(prefix + pxToDpStr("folderTopPadding", folderContentPaddingTop));
Thales Limab35faed2022-09-05 16:30:01 -03001677 writer.println(prefix + pxToDpStr("folderFooterHeight", folderFooterHeightPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001678
Alex Chaue0227552022-04-06 19:44:43 +01001679 writer.println(prefix + pxToDpStr("bottomSheetTopPadding", bottomSheetTopPadding));
Alex Chau3d2c0622022-09-01 21:28:14 +01001680 writer.println(prefix + "\tbottomSheetOpenDuration: " + bottomSheetOpenDuration);
1681 writer.println(prefix + "\tbottomSheetCloseDuration: " + bottomSheetCloseDuration);
1682 writer.println(prefix + "\tbottomSheetWorkspaceScale: " + bottomSheetWorkspaceScale);
1683 writer.println(prefix + "\tbottomSheetDepth: " + bottomSheetDepth);
Alex Chaue0227552022-04-06 19:44:43 +01001684
1685 writer.println(prefix + pxToDpStr("allAppsShiftRange", allAppsShiftRange));
1686 writer.println(prefix + pxToDpStr("allAppsTopPadding", allAppsTopPadding));
Alex Chau3d2c0622022-09-01 21:28:14 +01001687 writer.println(prefix + "\tallAppsOpenDuration: " + allAppsOpenDuration);
1688 writer.println(prefix + "\tallAppsCloseDuration: " + allAppsCloseDuration);
Jon Miranda58561d42021-03-17 10:51:54 -04001689 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
1690 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
1691 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
1692 allAppsIconDrawablePaddingPx));
1693 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
Alex Chau27b39b92022-01-14 18:02:18 +00001694 writer.println(prefix + pxToDpStr("allAppsCellWidthPx", allAppsCellWidthPx));
Pat Manning26f70f72022-07-07 13:50:39 +01001695 writer.println(prefix + pxToDpStr("allAppsBorderSpacePxX", allAppsBorderSpacePx.x));
1696 writer.println(prefix + pxToDpStr("allAppsBorderSpacePxY", allAppsBorderSpacePx.y));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001697 writer.println(prefix + "\tnumShownAllAppsColumns: " + numShownAllAppsColumns);
Alex Chau62572c02022-07-25 18:36:37 +00001698 writer.println(prefix + pxToDpStr("allAppsLeftRightPadding", allAppsLeftRightPadding));
Alex Chau27b39b92022-01-14 18:02:18 +00001699 writer.println(prefix + pxToDpStr("allAppsLeftRightMargin", allAppsLeftRightMargin));
Jon Miranda58561d42021-03-17 10:51:54 -04001700
1701 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
Thales Lima425f6822022-05-10 13:44:58 -03001702 writer.println(prefix + "\tinv.hotseatColumnSpan: " + inv.hotseatColumnSpan[mTypeIndex]);
Jon Miranda58561d42021-03-17 10:51:54 -04001703 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
Alex Chau62572c02022-07-25 18:36:37 +00001704 writer.println(prefix + pxToDpStr("hotseatBarBottomSpacePx", hotseatBarBottomSpacePx));
Jon Miranda58561d42021-03-17 10:51:54 -04001705 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
1706 hotseatBarSidePaddingStartPx));
1707 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
1708 hotseatBarSidePaddingEndPx));
Pat Manning26f70f72022-07-07 13:50:39 +01001709 writer.println(prefix + pxToDpStr("hotseatBarEndOffset", hotseatBarEndOffset));
Thales Limab8c05952022-05-23 16:58:38 +01001710 writer.println(prefix + pxToDpStr("hotseatQsbSpace", hotseatQsbSpace));
1711 writer.println(prefix + pxToDpStr("hotseatQsbHeight", hotseatQsbHeight));
Pat Manningde25c0d2022-04-05 19:11:26 +01001712 writer.println(prefix + pxToDpStr("springLoadedHotseatBarTopMarginPx",
1713 springLoadedHotseatBarTopMarginPx));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001714 Rect hotseatLayoutPadding = getHotseatLayoutPadding(context);
1715 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).top",
1716 hotseatLayoutPadding.top));
1717 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).bottom",
1718 hotseatLayoutPadding.bottom));
1719 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).left",
1720 hotseatLayoutPadding.left));
1721 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).right",
1722 hotseatLayoutPadding.right));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001723 writer.println(prefix + "\tnumShownHotseatIcons: " + numShownHotseatIcons);
Thales Lima116b51a2022-02-03 16:19:47 +00001724 writer.println(prefix + pxToDpStr("hotseatBorderSpace", hotseatBorderSpace));
Thales Limaa1012902022-01-13 17:58:42 +00001725 writer.println(prefix + "\tisQsbInline: " + isQsbInline);
Alex Chau206ede92022-08-01 17:46:12 +01001726 writer.println(prefix + pxToDpStr("hotseatQsbWidth", hotseatQsbWidth));
Jon Miranda58561d42021-03-17 10:51:54 -04001727
1728 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
Tony Wickham635e1802021-07-22 14:28:04 -10001729 writer.println(prefix + "\tisTaskbarPresentInApps:" + isTaskbarPresentInApps);
Jon Miranda9c478b62023-03-23 21:38:49 -07001730 writer.println(prefix + pxToDpStr("taskbarHeight", taskbarHeight));
1731 writer.println(prefix + pxToDpStr("stashedTaskbarHeight", stashedTaskbarHeight));
1732 writer.println(prefix + pxToDpStr("taskbarBottomMargin", taskbarBottomMargin));
1733 writer.println(prefix + pxToDpStr("taskbarIconSize", taskbarIconSize));
Jon Miranda58561d42021-03-17 10:51:54 -04001734
Thales Lima83bedbf2021-10-05 17:47:39 +01001735 writer.println(prefix + pxToDpStr("desiredWorkspaceHorizontalMarginPx",
1736 desiredWorkspaceHorizontalMarginPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001737 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
1738 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
1739 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
Alex Chau62572c02022-07-25 18:36:37 +00001740 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
Jon Miranda58561d42021-03-17 10:51:54 -04001741
Jon Mirandaab3c6812021-06-28 15:42:28 -07001742 writer.println(prefix + pxToDpStr("iconScale", iconScale));
1743 writer.println(prefix + pxToDpStr("cellScaleToFit ", cellScaleToFit));
Jon Miranda58561d42021-03-17 10:51:54 -04001744 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Alex Chau62572c02022-07-25 18:36:37 +00001745 writer.println(prefix + pxToDpStr("unscaled extraSpace", extraSpace / iconScale));
Jon Mirandac9e69fa2021-03-22 17:13:34 -04001746
Thales Lima171ee662022-11-22 15:52:49 +00001747 writer.println(prefix + pxToDpStr("maxEmptySpace", maxEmptySpace));
Jon Miranda58561d42021-03-17 10:51:54 -04001748 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
1749 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
Alex Chau635b3ab2022-01-26 16:49:10 +00001750
1751 writer.println(prefix + pxToDpStr("overviewTaskMarginPx", overviewTaskMarginPx));
Alex Chau635b3ab2022-01-26 16:49:10 +00001752 writer.println(prefix + pxToDpStr("overviewTaskIconSizePx", overviewTaskIconSizePx));
1753 writer.println(prefix + pxToDpStr("overviewTaskIconDrawableSizePx",
1754 overviewTaskIconDrawableSizePx));
1755 writer.println(prefix + pxToDpStr("overviewTaskIconDrawableSizeGridPx",
1756 overviewTaskIconDrawableSizeGridPx));
1757 writer.println(prefix + pxToDpStr("overviewTaskThumbnailTopMarginPx",
1758 overviewTaskThumbnailTopMarginPx));
Alex Chaua2fc7642022-04-21 14:20:23 +01001759 writer.println(prefix + pxToDpStr("overviewActionsTopMarginPx",
1760 overviewActionsTopMarginPx));
1761 writer.println(prefix + pxToDpStr("overviewActionsHeight",
1762 overviewActionsHeight));
Alex Chau1b276a12022-12-19 14:01:36 +00001763 writer.println(prefix + pxToDpStr("overviewActionsClaimedSpaceBelow",
1764 getOverviewActionsClaimedSpaceBelow()));
Alex Chau635b3ab2022-01-26 16:49:10 +00001765 writer.println(prefix + pxToDpStr("overviewActionsButtonSpacing",
1766 overviewActionsButtonSpacing));
1767 writer.println(prefix + pxToDpStr("overviewPageSpacing", overviewPageSpacing));
1768 writer.println(prefix + pxToDpStr("overviewRowSpacing", overviewRowSpacing));
1769 writer.println(prefix + pxToDpStr("overviewGridSideMargin", overviewGridSideMargin));
Pat Manningde25c0d2022-04-05 19:11:26 +01001770
Alex Chau62572c02022-07-25 18:36:37 +00001771 writer.println(prefix + pxToDpStr("dropTargetBarTopMarginPx", dropTargetBarTopMarginPx));
Pat Manningde25c0d2022-04-05 19:11:26 +01001772 writer.println(prefix + pxToDpStr("dropTargetBarSizePx", dropTargetBarSizePx));
Alex Chau62572c02022-07-25 18:36:37 +00001773 writer.println(
1774 prefix + pxToDpStr("dropTargetBarBottomMarginPx", dropTargetBarBottomMarginPx));
Pat Manningde25c0d2022-04-05 19:11:26 +01001775
Pat Manning5f74bfd2022-07-20 12:08:54 +01001776 writer.println(prefix + pxToDpStr("getCellLayoutSpringLoadShrunkTop()",
1777 getCellLayoutSpringLoadShrunkTop()));
1778 writer.println(prefix + pxToDpStr("getCellLayoutSpringLoadShrunkBottom()",
1779 getCellLayoutSpringLoadShrunkBottom(context)));
Alex Chau906d8822022-05-23 10:42:25 +01001780 writer.println(prefix + pxToDpStr("workspaceSpringLoadedMinNextPageVisiblePx",
1781 workspaceSpringLoadedMinNextPageVisiblePx));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001782 writer.println(prefix + pxToDpStr("getWorkspaceSpringLoadScale()",
1783 getWorkspaceSpringLoadScale(context)));
Thales Limab8c05952022-05-23 16:58:38 +01001784 writer.println(prefix + pxToDpStr("getCellLayoutHeight()", getCellLayoutHeight()));
1785 writer.println(prefix + pxToDpStr("getCellLayoutWidth()", getCellLayoutWidth()));
Thales Lima79456492023-05-23 11:32:22 +01001786 if (mIsResponsiveGrid) {
1787 writer.println(prefix + "\tmResponsiveHeightSpec:" + mResponsiveHeightSpec.toString());
1788 writer.println(prefix + "\tmResponsiveWidthSpec:" + mResponsiveWidthSpec.toString());
1789 }
Jon Miranda58561d42021-03-17 10:51:54 -04001790 }
1791
Nicolo' Mazzucatocb7bd502023-01-16 19:46:30 +00001792 /** Returns a reduced representation of this DeviceProfile. */
1793 public String toSmallString() {
1794 return "isTablet:" + isTablet + ", "
1795 + "isMultiDisplay:" + isMultiDisplay + ", "
1796 + "widthPx:" + widthPx + ", "
1797 + "heightPx:" + heightPx + ", "
1798 + "insets:" + mInsets + ", "
1799 + "rotationHint:" + rotationHint;
1800 }
1801
Alex Chaua6907dc2022-03-18 15:24:07 +00001802 private static Context getContext(Context c, Info info, int orientation, WindowBounds bounds) {
Sunny Goyal1890f672020-04-30 12:28:00 -07001803 Configuration config = new Configuration(c.getResources().getConfiguration());
1804 config.orientation = orientation;
Thales Lima425f6822022-05-10 13:44:58 -03001805 config.densityDpi = info.getDensityDpi();
Alex Chaua6907dc2022-03-18 15:24:07 +00001806 config.smallestScreenWidthDp = (int) info.smallestSizeDp(bounds);
Sunny Goyal1890f672020-04-30 12:28:00 -07001807 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -07001808 }
Sunny Goyalfde55052018-02-01 14:46:13 -08001809
1810 /**
1811 * Callback when a component changes the DeviceProfile associated with it, as a result of
1812 * configuration change
1813 */
1814 public interface OnDeviceProfileChangeListener {
1815
1816 /**
1817 * Called when the device profile is reassigned. Note that for layout and measurements, it
1818 * is sufficient to listen for inset changes. Use this callback when you need to perform
1819 * a one time operation.
1820 */
1821 void onDeviceProfileChanged(DeviceProfile dp);
1822 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001823
Sihua Mae04aa202022-07-18 12:43:56 -07001824 /**
1825 * Handler that deals with ItemInfo of the views for the DeviceProfile
1826 */
1827 @FunctionalInterface
1828 public interface ViewScaleProvider {
1829 /**
1830 * Get the scales from the view
1831 *
1832 * @param itemInfo The tag of the widget view
1833 * @return PointF instance containing the scale information, or null if using the default
1834 * app widget scale of this device profile.
1835 */
1836 @NonNull
1837 PointF getScaleFromItemInfo(@Nullable ItemInfo itemInfo);
1838 }
1839
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001840 public static class Builder {
1841 private Context mContext;
1842 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -07001843 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001844
Sunny Goyal19ff7282021-04-22 10:12:54 -07001845 private WindowBounds mWindowBounds;
Alex Chauab800f72022-12-13 17:46:06 +00001846 private boolean mIsMultiDisplay;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001847
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001848 private boolean mIsMultiWindowMode = false;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001849 private Boolean mTransposeLayoutWithOrientation;
Alex Chau6ed408f2022-03-04 12:58:05 +00001850 private Boolean mIsGestureMode;
Sihua Mae04aa202022-07-18 12:43:56 -07001851 private ViewScaleProvider mViewScaleProvider = null;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001852
Sunny Goyal65190ae2022-08-02 14:16:26 -07001853 private SparseArray<DotRenderer> mDotRendererCache;
1854
Jon Miranda2b25ded2023-02-02 14:48:45 -08001855 private Consumer<DeviceProfile> mOverrideProvider;
1856
Sunny Goyalfd58da62020-08-11 12:06:49 -07001857 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001858 mContext = context;
1859 mInv = inv;
1860 mInfo = info;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001861 }
1862
1863 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
1864 mIsMultiWindowMode = isMultiWindowMode;
1865 return this;
1866 }
1867
Alex Chauab800f72022-12-13 17:46:06 +00001868 public Builder setIsMultiDisplay(boolean isMultiDisplay) {
1869 mIsMultiDisplay = isMultiDisplay;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001870 return this;
1871 }
1872
Sunny Goyal65190ae2022-08-02 14:16:26 -07001873 public Builder setDotRendererCache(SparseArray<DotRenderer> dotRendererCache) {
1874 mDotRendererCache = dotRendererCache;
1875 return this;
1876 }
Sunny Goyal19ff7282021-04-22 10:12:54 -07001877
1878 public Builder setWindowBounds(WindowBounds bounds) {
1879 mWindowBounds = bounds;
Sunny Goyal0addbf02020-04-28 14:17:35 -07001880 return this;
1881 }
1882
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001883 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
1884 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
1885 return this;
1886 }
1887
Alex Chau6ed408f2022-03-04 12:58:05 +00001888 public Builder setGestureMode(boolean isGestureMode) {
1889 mIsGestureMode = isGestureMode;
1890 return this;
1891 }
1892
Jon Miranda2b25ded2023-02-02 14:48:45 -08001893 public Builder withDimensionsOverride(Consumer<DeviceProfile> overrideProvider) {
1894 mOverrideProvider = overrideProvider;
1895 return this;
1896 }
1897
Sihua Mae04aa202022-07-18 12:43:56 -07001898 /**
1899 * Set the viewScaleProvider for the builder
1900 *
1901 * @param viewScaleProvider The viewScaleProvider to be set for the
Thales Limae9273ea2023-01-26 12:33:52 +00001902 * DeviceProfile
Sihua Mae04aa202022-07-18 12:43:56 -07001903 * @return This builder
1904 */
1905 @NonNull
1906 public Builder setViewScaleProvider(@Nullable ViewScaleProvider viewScaleProvider) {
1907 mViewScaleProvider = viewScaleProvider;
1908 return this;
1909 }
1910
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001911 public DeviceProfile build() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001912 if (mWindowBounds == null) {
1913 throw new IllegalArgumentException("Window bounds not set");
1914 }
1915 if (mTransposeLayoutWithOrientation == null) {
1916 mTransposeLayoutWithOrientation = !mInfo.isTablet(mWindowBounds);
1917 }
Alex Chau6ed408f2022-03-04 12:58:05 +00001918 if (mIsGestureMode == null) {
Alex Chaue818bcb2022-09-02 17:27:11 +01001919 mIsGestureMode = mInfo.navigationMode.hasGestures;
Alex Chau6ed408f2022-03-04 12:58:05 +00001920 }
Sunny Goyal65190ae2022-08-02 14:16:26 -07001921 if (mDotRendererCache == null) {
1922 mDotRendererCache = new SparseArray<>();
1923 }
Sihua Mae04aa202022-07-18 12:43:56 -07001924 if (mViewScaleProvider == null) {
1925 mViewScaleProvider = DEFAULT_PROVIDER;
1926 }
Jon Miranda2b25ded2023-02-02 14:48:45 -08001927 if (mOverrideProvider == null) {
1928 mOverrideProvider = DEFAULT_DIMENSION_PROVIDER;
1929 }
Sunny Goyal65190ae2022-08-02 14:16:26 -07001930 return new DeviceProfile(mContext, mInv, mInfo, mWindowBounds, mDotRendererCache,
Alex Chauab800f72022-12-13 17:46:06 +00001931 mIsMultiWindowMode, mTransposeLayoutWithOrientation, mIsMultiDisplay,
Jon Miranda2b25ded2023-02-02 14:48:45 -08001932 mIsGestureMode, mViewScaleProvider, mOverrideProvider);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001933 }
1934 }
1935
Winson Chungb3800242013-10-24 11:01:54 -07001936}