blob: 5c16b4c0d64799b331b73c391896ffeb6b64b044 [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
Alex Chaua02eddc2021-04-29 00:36:06 +010019import static android.util.DisplayMetrics.DENSITY_DEVICE_STABLE;
Sunny Goyal35c7b192021-04-20 16:51:10 -070020
Jon Mirandae126d722021-02-25 10:45:20 -050021import static com.android.launcher3.ResourceUtils.pxFromDp;
Jon Miranda58561d42021-03-17 10:51:54 -040022import static com.android.launcher3.Utilities.dpiFromPx;
Jon Mirandaaf3aed02021-05-06 18:28:45 -070023import static com.android.launcher3.Utilities.pxFromSp;
Jon Miranda92c1b5d2021-07-20 13:57:16 -070024import static com.android.launcher3.folder.ClippedFolderIconLayoutRule.ICON_OVERLAP_FACTOR;
Alex Chaua02eddc2021-04-29 00:36:06 +010025import static com.android.launcher3.util.WindowManagerCompat.MIN_TABLET_WIDTH;
Jon Miranda228877d2021-02-09 11:05:00 -050026
Sunny Goyal35c7b192021-04-20 16:51:10 -070027import android.annotation.SuppressLint;
Winson Chungb3800242013-10-24 11:01:54 -070028import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070029import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070030import android.content.res.Resources;
Sunny Goyal8b9919d2021-04-07 14:45:17 -070031import android.graphics.Path;
Winson Chungb3800242013-10-24 11:01:54 -070032import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080033import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070034import android.graphics.Rect;
Sunny Goyal35c7b192021-04-20 16:51:10 -070035import android.util.DisplayMetrics;
Sunny Goyal59d086c2018-05-07 17:31:40 -070036import android.view.Surface;
Winson Chungb3800242013-10-24 11:01:54 -070037
Sunny Goyalc13403c2016-11-18 23:44:48 -080038import com.android.launcher3.CellLayout.ContainerType;
Jon Miranda228877d2021-02-09 11:05:00 -050039import com.android.launcher3.DevicePaddings.DevicePadding;
Tony Wickham15883892021-02-12 11:24:40 -080040import com.android.launcher3.config.FeatureFlags;
Tony Wickham8912b042018-11-29 15:28:53 -080041import com.android.launcher3.icons.DotRenderer;
Sunny Goyal8b9919d2021-04-07 14:45:17 -070042import com.android.launcher3.icons.GraphicsUtils;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070043import com.android.launcher3.icons.IconNormalizer;
Sunny Goyal57b22792021-05-25 14:35:01 -070044import com.android.launcher3.uioverrides.ApiWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070045import com.android.launcher3.util.DisplayController;
46import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb46703d2020-05-27 17:52:03 -070047import com.android.launcher3.util.WindowBounds;
Winson1f064272016-07-18 17:18:02 -070048
Jon Miranda58561d42021-03-17 10:51:54 -040049import java.io.PrintWriter;
50
Sunny Goyal35c7b192021-04-20 16:51:10 -070051@SuppressLint("NewApi")
Winson Chungb3800242013-10-24 11:01:54 -070052public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070053
Sunny Goyal8b9919d2021-04-07 14:45:17 -070054 private static final int DEFAULT_DOT_SIZE = 100;
Sunny Goyal57b22792021-05-25 14:35:01 -070055 // Ratio of empty space, qsb should take up to appear visually centered.
56 private static final float QSB_CENTER_FACTOR = .325f;
Sunny Goyal1890f672020-04-30 12:28:00 -070057
Adam Cohen2e6da152015-05-06 11:42:25 -070058 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070059 private final Info mInfo;
Sunny Goyal35c7b192021-04-20 16:51:10 -070060 private final DisplayMetrics mMetrics;
Winson Chungbe876472014-05-14 14:15:20 -070061
Sunny Goyalc6205602015-05-21 20:46:33 -070062 // Device properties
63 public final boolean isTablet;
Sunny Goyalc6205602015-05-21 20:46:33 -070064 public final boolean isPhone;
65 public final boolean transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -070066 public final boolean isTwoPanels;
Alex Chaua02eddc2021-04-29 00:36:06 +010067 public final boolean allowRotation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070068
Sunny Goyalc6205602015-05-21 20:46:33 -070069 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070070 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080071 public final boolean isMultiWindowMode;
72
Sunny Goyal0addbf02020-04-28 14:17:35 -070073 public final int windowX;
74 public final int windowY;
Sunny Goyalc6205602015-05-21 20:46:33 -070075 public final int widthPx;
76 public final int heightPx;
77 public final int availableWidthPx;
78 public final int availableHeightPx;
Jon Mirandabba64512019-03-27 10:54:17 -070079
80 public final float aspectRatio;
81
Jon Mirandae126d722021-02-25 10:45:20 -050082 public final boolean isScalableGrid;
Thales Lima83bedbf2021-10-05 17:47:39 +010083 private final int mTypeIndex;
Jon Mirandae126d722021-02-25 10:45:20 -050084
Tony Wickhamd6b40372015-09-23 18:37:57 -070085 /**
86 * The maximum amount of left/right workspace padding as a percentage of the screen width.
87 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
88 * 7% of the screen width can be used as right padding.
89 */
90 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070091
Jon Miranda3f9bab22017-07-28 14:50:17 -070092 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda80cddbc2021-07-22 13:51:16 -070093 private static final float TALLER_DEVICE_ASPECT_RATIO_THRESHOLD = 2.15f;
Jon Miranda80dda302021-07-28 14:14:59 -070094 private static final float TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP = 252;
95 private static final float TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP = 268;
Jon Miranda30d0aa22017-07-25 15:55:29 -070096
Jon Miranda5eacbb72018-07-31 11:14:46 -070097 // To evenly space the icons, increase the left/right margins for tablets in portrait mode.
98 private static final int PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER = 4;
99
Sunny Goyalc6205602015-05-21 20:46:33 -0700100 // Workspace
Thales Limad90faab2021-09-21 17:18:47 +0100101 public final int desiredWorkspaceHorizontalMarginOriginalPx;
102 public int desiredWorkspaceHorizontalMarginPx;
Thales Lima78d00ad2021-09-30 11:29:06 +0100103 public Point cellLayoutBorderSpaceOriginalPx;
104 public Point cellLayoutBorderSpacePx;
Jon Miranda28032002017-07-13 16:18:56 -0700105 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700106 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700107 public final int edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700108 public float workspaceSpringLoadShrinkFactor;
109 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700110
Jon Miranda58561d42021-03-17 10:51:54 -0400111 private final int extraSpace;
Jon Miranda228877d2021-02-09 11:05:00 -0500112 public int workspaceTopPadding;
113 public int workspaceBottomPadding;
Jon Miranda58561d42021-03-17 10:51:54 -0400114 public int extraHotseatBottomPadding;
Jon Miranda228877d2021-02-09 11:05:00 -0500115
Tony Wickham5edf9e22020-03-27 20:06:52 -0700116 // Workspace page indicator
117 public final int workspacePageIndicatorHeight;
118 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -0700119
Sunny Goyalc6205602015-05-21 20:46:33 -0700120 // Workspace icons
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400121 public float iconScale;
Sunny Goyalc6205602015-05-21 20:46:33 -0700122 public int iconSizePx;
123 public int iconTextSizePx;
124 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700125 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700126
Jon Mirandaab3c6812021-06-28 15:42:28 -0700127 public float cellScaleToFit;
Adam Cohen59400422014-03-05 18:07:04 -0800128 public int cellWidthPx;
129 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700130 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800131
Jon Mirandae126d722021-02-25 10:45:20 -0500132 public int cellYPaddingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500133
Sunny Goyalc6205602015-05-21 20:46:33 -0700134 // Folder
Jon Mirandae126d722021-02-25 10:45:20 -0500135 public float folderLabelTextScale;
136 public int folderLabelTextSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700137 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700138 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700139
Jon Mirandae126d722021-02-25 10:45:20 -0500140 // Folder content
Thales Lima78d00ad2021-09-30 11:29:06 +0100141 public Point folderCellLayoutBorderSpacePx;
142 public int folderCellLayoutBorderSpaceOriginalPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500143 public int folderContentPaddingLeftRight;
144 public int folderContentPaddingTop;
145
Jon Mirandabf7d8122016-11-03 15:29:29 -0700146 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700147 public int folderCellWidthPx;
148 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700149
150 // Folder child
151 public int folderChildIconSizePx;
152 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700153 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700154
Sunny Goyalc6205602015-05-21 20:46:33 -0700155 // Hotseat
Jon Miranda80cddbc2021-07-22 13:51:16 -0700156 public int hotseatBarSizeExtraSpacePx;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700157 public final int numShownHotseatIcons;
Sunny Goyalc6205602015-05-21 20:46:33 -0700158 public int hotseatCellHeightPx;
Jon Miranda0d284852021-06-22 14:50:55 -0700159 private final int hotseatExtraVerticalSize;
Jon Miranda18751b62017-07-31 17:25:27 -0700160 // In portrait: size = height, in landscape: size = width
161 public int hotseatBarSizePx;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700162 public int hotseatBarTopPaddingPx;
Jon Miranda0d284852021-06-22 14:50:55 -0700163 public final int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700164 // Start is the side next to the nav bar, end is the side next to the workspace
165 public final int hotseatBarSidePaddingStartPx;
166 public final int hotseatBarSidePaddingEndPx;
Sunny Goyal57b22792021-05-25 14:35:01 -0700167 public final int hotseatQsbHeight;
Adam Cohen2e6da152015-05-06 11:42:25 -0700168
Jon Miranda8bdb2222021-06-23 18:10:10 -0700169 public final float qsbBottomMarginOriginalPx;
170 public int qsbBottomMarginPx;
171
Sunny Goyalc6205602015-05-21 20:46:33 -0700172 // All apps
Thales Lima83bedbf2021-10-05 17:47:39 +0100173 public Point allAppsCellSpacePx;
Samuel Fufaee9aff92021-04-05 13:30:40 -0500174 public int allAppsOpenVerticalTranslate;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700175 public int allAppsCellHeightPx;
Samuel Fufaf1424a32019-10-04 15:15:19 -0700176 public int allAppsCellWidthPx;
Winson1f064272016-07-18 17:18:02 -0700177 public int allAppsIconSizePx;
178 public int allAppsIconDrawablePaddingPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700179 public int allAppsLeftRightPadding;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700180 public final int numShownAllAppsColumns;
Winson1f064272016-07-18 17:18:02 -0700181 public float allAppsIconTextSizePx;
182
Zak Cohen334efeb2021-03-19 16:42:07 -0700183 // Overview
Alex Chau646f8042021-07-26 11:56:22 +0100184 public final boolean overviewShowAsGrid;
Zak Cohen334efeb2021-03-19 16:42:07 -0700185 public int overviewTaskMarginPx;
Alex Chaueaa6d682021-08-17 12:56:15 +0100186 public int overviewTaskMarginGridPx;
Alex Chaudedbc8a2021-03-17 16:53:26 +0000187 public int overviewTaskIconSizePx;
Alex Chauac9df382021-08-02 19:08:41 +0100188 public int overviewTaskIconDrawableSizePx;
189 public int overviewTaskIconDrawableSizeGridPx;
Alex Chaudedbc8a2021-03-17 16:53:26 +0000190 public int overviewTaskThumbnailTopMarginPx;
zakcohendad8a802021-05-20 09:52:26 -0700191 public final int overviewActionsMarginThreeButtonPx;
Alex Chau5fd9d492021-07-22 17:27:11 +0100192 public final int overviewActionsTopMarginGesturePx;
193 public final int overviewActionsBottomMarginGesturePx;
194 public int overviewPageSpacing;
195 public int overviewRowSpacing;
Alex Chau56bd2572021-11-03 18:48:18 +0000196 public int overviewGridSideMargin;
Zak Cohen334efeb2021-03-19 16:42:07 -0700197
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800198 // Widgets
199 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
200
Sunny Goyal47328fd2016-05-25 18:56:41 -0700201 // Drop Target
202 public int dropTargetBarSizePx;
Alex Chaua02eddc2021-04-29 00:36:06 +0100203 public int dropTargetDragPaddingPx;
204 public int dropTargetTextSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700205
Winson1f064272016-07-18 17:18:02 -0700206 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800207 private final Rect mInsets = new Rect();
208 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700209 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800210 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700211 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700212
Tony Wickhamf34bee82018-12-03 18:11:39 -0800213 // Notification dots
Samuel Fufac96fa242019-09-26 23:06:32 -0700214 public DotRenderer mDotRendererWorkSpace;
215 public DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800216
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100217 // Taskbar
Tony Wickham15883892021-02-12 11:24:40 -0800218 public boolean isTaskbarPresent;
Tony Wickham635e1802021-07-22 14:28:04 -1000219 // Whether Taskbar will inset the bottom of apps by taskbarSize.
220 public boolean isTaskbarPresentInApps;
Tony Wickham15883892021-02-12 11:24:40 -0800221 public int taskbarSize;
222
Alex Chaua02eddc2021-04-29 00:36:06 +0100223 // DragController
224 public int flingToDeleteThresholdVelocity;
225
Vinit Nayak17c4b332021-08-05 12:54:58 -0700226 /** TODO: Once we fully migrate to staged split, remove "isMultiWindowMode" */
Sunny Goyal19ff7282021-04-22 10:12:54 -0700227 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info, WindowBounds windowBounds,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700228 boolean isMultiWindowMode, boolean transposeLayoutWithOrientation,
Sunny Goyal19ff7282021-04-22 10:12:54 -0700229 boolean useTwoPanels) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700230
Adam Cohen2e6da152015-05-06 11:42:25 -0700231 this.inv = inv;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700232 this.isLandscape = windowBounds.isLandscape();
Sunny Goyald70e75a2018-02-22 10:07:32 -0800233 this.isMultiWindowMode = isMultiWindowMode;
Jon Mirandae126d722021-02-25 10:45:20 -0500234 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700235 windowX = windowBounds.bounds.left;
236 windowY = windowBounds.bounds.top;
Winson Chungb3800242013-10-24 11:01:54 -0700237
Jon Mirandae126d722021-02-25 10:45:20 -0500238 isScalableGrid = inv.isScalable && !isVerticalBarLayout() && !isMultiWindowMode;
239
Jon Miranda7e183c32018-06-20 11:05:27 -0700240 // Determine sizes.
Sunny Goyal19ff7282021-04-22 10:12:54 -0700241 widthPx = windowBounds.bounds.width();
242 heightPx = windowBounds.bounds.height();
243 availableWidthPx = windowBounds.availableSize.x;
Tony Wickham21970cc2021-09-15 15:44:05 -0700244 availableHeightPx = windowBounds.availableSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700245
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700246 mInfo = info;
Alex Chaua02eddc2021-04-29 00:36:06 +0100247 // If the device's pixel density was scaled (usually via settings for A11y), use the
248 // original dimensions to determine if rotation is allowed of not.
249 float originalSmallestWidth = dpiFromPx(Math.min(widthPx, heightPx), DENSITY_DEVICE_STABLE);
250 allowRotation = originalSmallestWidth >= MIN_TABLET_WIDTH;
251 // Tablet UI does not support emulated landscape.
252 isTablet = allowRotation && info.isTablet(windowBounds);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700253 isPhone = !isTablet;
Alex Chaub1c36ce2021-08-25 11:54:08 +0100254 isTwoPanels = isTablet && useTwoPanels
255 && (isLandscape || FeatureFlags.ENABLE_TWO_PANEL_HOME_IN_PORTRAIT.get());
Adam Cohen2e6da152015-05-06 11:42:25 -0700256
Jon Mirandabba64512019-03-27 10:54:17 -0700257 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda7e183c32018-06-20 11:05:27 -0700258 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700259
260 // Some more constants
Sunny Goyal1890f672020-04-30 12:28:00 -0700261 context = getContext(context, info, isVerticalBarLayout()
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700262 ? Configuration.ORIENTATION_LANDSCAPE
263 : Configuration.ORIENTATION_PORTRAIT);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700264 mMetrics = context.getResources().getDisplayMetrics();
Sunny Goyal1890f672020-04-30 12:28:00 -0700265 final Resources res = context.getResources();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700266
Thales Lima83bedbf2021-10-05 17:47:39 +0100267 if (isTwoPanels) {
268 if (isLandscape) {
269 mTypeIndex = InvariantDeviceProfile.INDEX_TWO_PANEL_LANDSCAPE;
270 } else {
271 mTypeIndex = InvariantDeviceProfile.INDEX_TWO_PANEL_PORTRAIT;
272 }
273 } else {
274 if (isLandscape) {
275 mTypeIndex = InvariantDeviceProfile.INDEX_LANDSCAPE;
276 } else {
277 mTypeIndex = InvariantDeviceProfile.INDEX_DEFAULT;
278 }
279 }
280
Sunny Goyal57b22792021-05-25 14:35:01 -0700281 hotseatQsbHeight = res.getDimensionPixelSize(R.dimen.qsb_widget_height);
Sunny Goyal71ef9682021-07-29 12:59:27 -0700282 isTaskbarPresent = isTablet && ApiWrapper.TASKBAR_DRAWN_IN_PROCESS
283 && FeatureFlags.ENABLE_TASKBAR.get();
Tony Wickham15883892021-02-12 11:24:40 -0800284 if (isTaskbarPresent) {
Tony Wickham15883892021-02-12 11:24:40 -0800285 taskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_size);
Tony Wickham15883892021-02-12 11:24:40 -0800286 }
Tony Wickham15883892021-02-12 11:24:40 -0800287
Winson Chungb3800242013-10-24 11:01:54 -0700288 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700289
Thales Limad90faab2021-09-21 17:18:47 +0100290 desiredWorkspaceHorizontalMarginPx = getHorizontalMarginPx(inv, res);
291 desiredWorkspaceHorizontalMarginOriginalPx = desiredWorkspaceHorizontalMarginPx;
Samuel Fufaee9aff92021-04-05 13:30:40 -0500292
293 allAppsOpenVerticalTranslate = res.getDimensionPixelSize(
294 R.dimen.all_apps_open_vertical_translate);
295
Jon Mirandae126d722021-02-25 10:45:20 -0500296 folderLabelTextScale = res.getFloat(R.dimen.folder_label_text_scale);
297 folderContentPaddingLeftRight =
298 res.getDimensionPixelSize(R.dimen.folder_content_padding_left_right);
299 folderContentPaddingTop = res.getDimensionPixelSize(R.dimen.folder_content_padding_top);
300
Thales Lima78d00ad2021-09-30 11:29:06 +0100301 cellLayoutBorderSpacePx = getCellLayoutBorderSpace(inv);
Thales Lima83bedbf2021-10-05 17:47:39 +0100302 allAppsCellSpacePx = new Point(
303 pxFromDp(inv.borderSpaces[InvariantDeviceProfile.INDEX_ALL_APPS].x, mMetrics, 1f),
304 pxFromDp(inv.borderSpaces[InvariantDeviceProfile.INDEX_ALL_APPS].y, mMetrics, 1f));
Thales Lima78d00ad2021-09-30 11:29:06 +0100305 cellLayoutBorderSpaceOriginalPx = new Point(cellLayoutBorderSpacePx);
306 folderCellLayoutBorderSpaceOriginalPx = pxFromDp(inv.folderBorderSpace, mMetrics, 1f);
307 folderCellLayoutBorderSpacePx = new Point(folderCellLayoutBorderSpaceOriginalPx,
308 folderCellLayoutBorderSpaceOriginalPx);
Jon Mirandae126d722021-02-25 10:45:20 -0500309
Jon Miranda5eacbb72018-07-31 11:14:46 -0700310 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
311 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Jon Mirandae126d722021-02-25 10:45:20 -0500312 int cellLayoutPadding = isScalableGrid
313 ? 0
314 : res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100315
Sunny Goyal19ff7282021-04-22 10:12:54 -0700316 if (isTwoPanels) {
Thales Lima7a6752d2021-09-23 14:31:00 +0100317 cellLayoutPaddingLeftRightPx = 0;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100318 cellLayoutBottomPaddingPx = 0;
319 } else if (isLandscape) {
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700320 cellLayoutPaddingLeftRightPx = 0;
321 cellLayoutBottomPaddingPx = cellLayoutPadding;
322 } else {
323 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
324 cellLayoutBottomPaddingPx = 0;
325 }
326
Tony Wickham5edf9e22020-03-27 20:06:52 -0700327 workspacePageIndicatorHeight = res.getDimensionPixelSize(
328 R.dimen.workspace_page_indicator_height);
329 mWorkspacePageIndicatorOverlapWorkspace =
330 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700331
Winson Chungb3800242013-10-24 11:01:54 -0700332 iconDrawablePaddingOriginalPx =
333 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Alex Chaua02eddc2021-04-29 00:36:06 +0100334
Sunny Goyal47328fd2016-05-25 18:56:41 -0700335 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
Alex Chaua02eddc2021-04-29 00:36:06 +0100336 dropTargetDragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
337 dropTargetTextSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size);
338
Sunny Goyal47328fd2016-05-25 18:56:41 -0700339 workspaceSpringLoadedBottomSpace =
340 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700341
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700342 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700343
Sunny Goyal19ff7282021-04-22 10:12:54 -0700344 numShownHotseatIcons =
345 isTwoPanels ? inv.numDatabaseHotseatIcons : inv.numShownHotseatIcons;
346 numShownAllAppsColumns =
347 isTwoPanels ? inv.numDatabaseAllAppsColumns : inv.numAllAppsColumns;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700348 hotseatBarSizeExtraSpacePx = 0;
Winson1f064272016-07-18 17:18:02 -0700349 hotseatBarTopPaddingPx =
350 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700351 hotseatBarBottomPaddingPx = (isTallDevice ? 0
352 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
353 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700354 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800355 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700356 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700357 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Jon Miranda0d284852021-06-22 14:50:55 -0700358 hotseatExtraVerticalSize =
Jon Miranda228877d2021-02-09 11:05:00 -0500359 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size);
Thales Lima83bedbf2021-10-05 17:47:39 +0100360 updateHotseatIconSize(
361 pxFromDp(inv.iconSize[InvariantDeviceProfile.INDEX_DEFAULT], mMetrics, 1f));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700362
Jon Miranda8bdb2222021-06-23 18:10:10 -0700363 qsbBottomMarginOriginalPx = isScalableGrid
364 ? res.getDimensionPixelSize(R.dimen.scalable_grid_qsb_bottom_margin)
365 : 0;
366
Alex Chau646f8042021-07-26 11:56:22 +0100367 overviewShowAsGrid = isTablet && FeatureFlags.ENABLE_OVERVIEW_GRID.get();
Alex Chau5fd9d492021-07-22 17:27:11 +0100368 overviewTaskMarginPx = overviewShowAsGrid
Alex Chaueaa6d682021-08-17 12:56:15 +0100369 ? res.getDimensionPixelSize(R.dimen.overview_task_margin_focused)
Alex Chau5fd9d492021-07-22 17:27:11 +0100370 : res.getDimensionPixelSize(R.dimen.overview_task_margin);
Alex Chaueaa6d682021-08-17 12:56:15 +0100371 overviewTaskMarginGridPx = res.getDimensionPixelSize(R.dimen.overview_task_margin_grid);
Alex Chau5fd9d492021-07-22 17:27:11 +0100372 overviewTaskIconSizePx = res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_size);
Alex Chauac9df382021-08-02 19:08:41 +0100373 overviewTaskIconDrawableSizePx =
374 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size);
375 overviewTaskIconDrawableSizeGridPx =
376 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size_grid);
Alex Chaudedbc8a2021-03-17 16:53:26 +0000377 overviewTaskThumbnailTopMarginPx = overviewTaskIconSizePx + overviewTaskMarginPx * 2;
Alex Chau5fd9d492021-07-22 17:27:11 +0100378 if (overviewShowAsGrid) {
379 if (isLandscape) {
380 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
381 R.dimen.overview_actions_top_margin_gesture_grid_landscape);
382 overviewActionsBottomMarginGesturePx = res.getDimensionPixelSize(
383 R.dimen.overview_actions_bottom_margin_gesture_grid_landscape);
384 } else {
385 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
386 R.dimen.overview_actions_top_margin_gesture_grid_portrait);
387 overviewActionsBottomMarginGesturePx = res.getDimensionPixelSize(
388 R.dimen.overview_actions_bottom_margin_gesture_grid_portrait);
389 }
390 } else {
391 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
392 R.dimen.overview_actions_margin_gesture);
393 overviewActionsBottomMarginGesturePx = overviewActionsTopMarginGesturePx;
394 }
zakcohendad8a802021-05-20 09:52:26 -0700395 overviewActionsMarginThreeButtonPx = res.getDimensionPixelSize(
Alex Chau5fd9d492021-07-22 17:27:11 +0100396 R.dimen.overview_actions_margin_three_button);
397 overviewPageSpacing = overviewShowAsGrid
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100398 ? res.getDimensionPixelSize(R.dimen.recents_page_spacing_grid)
399 : res.getDimensionPixelSize(R.dimen.recents_page_spacing);
Alex Chau5fd9d492021-07-22 17:27:11 +0100400 overviewRowSpacing = isLandscape
401 ? res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing_landscape)
402 : res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing_portrait);
Alex Chau56bd2572021-11-03 18:48:18 +0000403 overviewGridSideMargin = isLandscape
404 ? res.getDimensionPixelSize(R.dimen.overview_grid_side_margin_landscape)
405 : res.getDimensionPixelSize(R.dimen.overview_grid_side_margin_portrait);
Zak Cohen334efeb2021-03-19 16:42:07 -0700406
Jon Miranda2ed276e2017-06-29 11:36:34 -0700407 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400408 extraSpace = updateAvailableDimensions(res);
Jon Miranda80cddbc2021-07-22 13:51:16 -0700409
Jon Miranda2ed276e2017-06-29 11:36:34 -0700410 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400411 if (isScalableGrid && inv.devicePaddings != null) {
412 // Paddings were created assuming no scaling, so we first unscale the extra space.
Jon Mirandaab3c6812021-06-28 15:42:28 -0700413 int unscaledExtraSpace = (int) (extraSpace / cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400414 DevicePadding padding = inv.devicePaddings.getDevicePadding(unscaledExtraSpace);
Jon Miranda228877d2021-02-09 11:05:00 -0500415
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400416 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
417 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
418 int paddingHotseatBottom = padding.getHotseatBottomPadding(unscaledExtraSpace);
419
Jon Mirandaab3c6812021-06-28 15:42:28 -0700420 workspaceTopPadding = Math.round(paddingWorkspaceTop * cellScaleToFit);
421 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * cellScaleToFit);
422 extraHotseatBottomPadding = Math.round(paddingHotseatBottom * cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400423
Jon Miranda58561d42021-03-17 10:51:54 -0400424 hotseatBarSizePx += extraHotseatBottomPadding;
Jon Miranda8bdb2222021-06-23 18:10:10 -0700425
Jon Mirandaab3c6812021-06-28 15:42:28 -0700426 qsbBottomMarginPx = Math.round(qsbBottomMarginOriginalPx * cellScaleToFit);
Jon Miranda228877d2021-02-09 11:05:00 -0500427 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700428 // We increase the hotseat size when there is extra space.
Jon Miranda80cddbc2021-07-22 13:51:16 -0700429
Jon Miranda80dda302021-07-28 14:14:59 -0700430 if (Float.compare(aspectRatio, TALLER_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0
431 && extraSpace >= Utilities.dpToPx(TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP)) {
432 // For taller devices, we will take a piece of the extra space from each row,
Jon Miranda80cddbc2021-07-22 13:51:16 -0700433 // and add it to the space above and below the hotseat.
Jon Miranda80dda302021-07-28 14:14:59 -0700434
435 // For devices with more extra space, we take a larger piece from each cell.
436 int piece = extraSpace < Utilities.dpToPx(TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP)
Jon Mirandaa56cb842021-08-03 17:26:44 -0700437 ? 7 : 5;
Jon Miranda80dda302021-07-28 14:14:59 -0700438
Jon Miranda80cddbc2021-07-22 13:51:16 -0700439 int extraSpace = ((getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2)
Jon Miranda80dda302021-07-28 14:14:59 -0700440 * inv.numRows) / piece;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700441
Jon Mirandaa56cb842021-08-03 17:26:44 -0700442 workspaceTopPadding = extraSpace / 8;
443 int halfLeftOver = (extraSpace - workspaceTopPadding) / 2;
444 hotseatBarTopPaddingPx += halfLeftOver;
445 hotseatBarSizeExtraSpacePx = halfLeftOver;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700446 } else {
447 // ie. For a display with a large aspect ratio, we can keep the icons on the
448 // workspace in portrait mode closer together by adding more height to the hotseat.
449 // Note: This calculation was created after noticing a pattern in the design spec.
450 hotseatBarSizeExtraSpacePx = getCellSize().y - iconSizePx
451 - iconDrawablePaddingPx * 2 - workspacePageIndicatorHeight;
452 }
453
454 updateHotseatIconSize(iconSizePx);
Jon Miranda30d0aa22017-07-25 15:55:29 -0700455
Jon Miranda3f9bab22017-07-28 14:50:17 -0700456 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700457 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700458 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800459 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800460
Alex Chaua02eddc2021-04-29 00:36:06 +0100461 flingToDeleteThresholdVelocity = res.getDimensionPixelSize(
462 R.dimen.drag_flingToDeleteMinVelocity);
463
Tony Wickham1237df02017-02-24 08:59:36 -0800464 // This is done last, after iconSizePx is calculated above.
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700465 Path dotPath = GraphicsUtils.getShapePath(DEFAULT_DOT_SIZE);
466 mDotRendererWorkSpace = new DotRenderer(iconSizePx, dotPath, DEFAULT_DOT_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700467 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700468 new DotRenderer(allAppsIconSizePx, dotPath, DEFAULT_DOT_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700469 }
470
Thales Limad90faab2021-09-21 17:18:47 +0100471 private int getHorizontalMarginPx(InvariantDeviceProfile idp, Resources res) {
472 if (isVerticalBarLayout()) {
473 return 0;
474 }
475
Thales Lima83bedbf2021-10-05 17:47:39 +0100476 return isScalableGrid
477 ? pxFromDp(idp.horizontalMargin[mTypeIndex], mMetrics)
478 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
Thales Limad90faab2021-09-21 17:18:47 +0100479 }
480
Jon Miranda0d284852021-06-22 14:50:55 -0700481 private void updateHotseatIconSize(int hotseatIconSizePx) {
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700482 // Ensure there is enough space for folder icons, which have a slightly larger radius.
483 hotseatCellHeightPx = (int) Math.ceil(hotseatIconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda0d284852021-06-22 14:50:55 -0700484 if (isVerticalBarLayout()) {
485 hotseatBarSizePx = hotseatIconSizePx + hotseatBarSidePaddingStartPx
486 + hotseatBarSidePaddingEndPx;
487 } else {
488 hotseatBarSizePx = hotseatIconSizePx + hotseatBarTopPaddingPx
Jon Miranda80cddbc2021-07-22 13:51:16 -0700489 + hotseatBarBottomPaddingPx + (isScalableGrid ? 0 : hotseatExtraVerticalSize)
490 + hotseatBarSizeExtraSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700491 }
492 }
493
Thales Lima78d00ad2021-09-30 11:29:06 +0100494 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp) {
495 if (!isScalableGrid) {
496 return new Point(0, 0);
497 }
498
Thales Lima83bedbf2021-10-05 17:47:39 +0100499 int horizontalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].x, mMetrics);
500 int verticalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].y, mMetrics);
Thales Lima78d00ad2021-09-30 11:29:06 +0100501
502 return new Point(horizontalSpacePx, verticalSpacePx);
503 }
504
505 private Point getCellLayoutBorderSpaceScaled(InvariantDeviceProfile idp, float scale) {
506 Point original = getCellLayoutBorderSpace(idp);
507 return new Point((int) (original.x * scale), (int) (original.y * scale));
Jon Mirandae126d722021-02-25 10:45:20 -0500508 }
509
Sunny Goyal69a8eec2021-07-22 10:02:50 -0700510 public Info getDisplayInfo() {
511 return mInfo;
512 }
513
Jon Miranda611dba42021-03-02 14:12:13 -0500514 /**
515 * We inset the widget padding added by the system and instead rely on the border spacing
516 * between cells to create reliable consistency between widgets
517 */
518 public boolean shouldInsetWidgets() {
519 Rect widgetPadding = inv.defaultWidgetPadding;
520
Jon Miranda49811182021-06-03 09:52:40 -0700521 // Check all sides to ensure that the widget won't overlap into another cell, or into
522 // status bar.
523 return workspaceTopPadding > widgetPadding.top
Thales Lima78d00ad2021-09-30 11:29:06 +0100524 && cellLayoutBorderSpacePx.x > widgetPadding.left
525 && cellLayoutBorderSpacePx.y > widgetPadding.top
526 && cellLayoutBorderSpacePx.x > widgetPadding.right
527 && cellLayoutBorderSpacePx.y > widgetPadding.bottom;
Jon Miranda611dba42021-03-02 14:12:13 -0500528 }
Jon Mirandae126d722021-02-25 10:45:20 -0500529
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700530 public Builder toBuilder(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700531 WindowBounds bounds =
532 new WindowBounds(widthPx, heightPx, availableWidthPx, availableHeightPx);
533 bounds.bounds.offsetTo(windowX, windowY);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700534 return new Builder(context, inv, mInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700535 .setWindowBounds(bounds)
536 .setUseTwoPanels(isTwoPanels)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700537 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800538 }
539
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700540 public DeviceProfile copy(Context context) {
541 return toBuilder(context).build();
542 }
543
544 /**
545 * TODO: Move this to the builder as part of setMultiWindowMode
546 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700547 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700548 DeviceProfile profile = toBuilder(context)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700549 .setWindowBounds(windowBounds)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700550 .setMultiWindowMode(true)
551 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800552
Jon Miranda941375e2021-03-31 13:10:45 -0400553 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda93e1f042016-11-11 14:13:04 -0800554
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800555 // We use these scales to measure and layout the widgets using their full invariant profile
556 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
557 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
558 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
559 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800560 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800561
Jon Miranda93e1f042016-11-11 14:13:04 -0800562 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700563 }
564
Adam Cohen63f1ec02014-08-12 09:23:13 -0700565 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400566 * Checks if there is enough space for labels on the workspace.
567 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700568 * It is important to call this method after the All Apps variables have been set.
569 */
Jon Miranda941375e2021-03-31 13:10:45 -0400570 private void hideWorkspaceLabelsIfNotEnoughSpace() {
571 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
572 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
573 - iconTextHeight;
574
575 // We want enough space so that the text is closer to its corresponding icon.
576 if (workspaceCellPaddingY < iconTextHeight) {
577 iconTextSizePx = 0;
578 iconDrawablePaddingPx = 0;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700579 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda941375e2021-03-31 13:10:45 -0400580 autoResizeAllAppsCells();
581 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700582 }
Jon Miranda1091e532017-07-21 13:31:50 -0700583
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700584 /**
585 * Re-computes the all-apps cell size to be independent of workspace
586 */
587 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400588 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
589 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700590 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400591 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700592 }
593
sfufa@google.comde013292021-09-21 18:22:44 -0700594 private void updateAllAppsWidth() {
595 if (isTwoPanels) {
596 int usedWidth = (allAppsCellWidthPx * numShownAllAppsColumns)
Thales Lima83bedbf2021-10-05 17:47:39 +0100597 + (allAppsCellSpacePx.x * (numShownAllAppsColumns + 1));
sfufa@google.comde013292021-09-21 18:22:44 -0700598 allAppsLeftRightPadding = Math.max(1, (availableWidthPx - usedWidth) / 2);
599 } else {
600 allAppsLeftRightPadding =
Thales Limad90faab2021-09-21 17:18:47 +0100601 desiredWorkspaceHorizontalMarginPx + cellLayoutPaddingLeftRightPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700602 }
603 }
604
Jon Miranda228877d2021-02-09 11:05:00 -0500605 /**
606 * Returns the amount of extra (or unused) vertical space.
607 */
608 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700609 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800610
Jon Mirandae126d722021-02-25 10:45:20 -0500611 Point workspacePadding = getTotalWorkspacePadding();
612
613 // Check to see if the icons fit within the available height.
614 float usedHeight = getCellLayoutHeight();
615 final int maxHeight = availableHeightPx - workspacePadding.y;
Jon Miranda228877d2021-02-09 11:05:00 -0500616 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500617 float scaleY = maxHeight / usedHeight;
618 boolean shouldScale = scaleY < 1f;
619
620 float scaleX = 1f;
621 if (isScalableGrid) {
622 // We scale to fit the cellWidth and cellHeight in the available space.
623 // The benefit of scalable grids is that we can get consistent aspect ratios between
624 // devices.
Thales Limad1df5fc2021-09-03 18:37:19 +0100625 int numColumns = isTwoPanels ? inv.numColumns * 2 : inv.numColumns;
626 float usedWidth = (cellWidthPx * numColumns)
Thales Lima78d00ad2021-09-30 11:29:06 +0100627 + (cellLayoutBorderSpacePx.x * (numColumns - 1))
Thales Limad90faab2021-09-21 17:18:47 +0100628 + (desiredWorkspaceHorizontalMarginPx * 2);
Jon Mirandae126d722021-02-25 10:45:20 -0500629 // We do not subtract padding here, as we also scale the workspace padding if needed.
630 scaleX = availableWidthPx / usedWidth;
631 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700632 }
Jon Mirandae126d722021-02-25 10:45:20 -0500633
634 if (shouldScale) {
635 float scale = Math.min(scaleX, scaleY);
636 updateIconSize(scale, res);
637 extraHeight = Math.max(0, maxHeight - getCellLayoutHeight());
638 }
639
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700640 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500641 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700642 }
643
Jon Mirandae126d722021-02-25 10:45:20 -0500644 private int getCellLayoutHeight() {
Thales Lima78d00ad2021-09-30 11:29:06 +0100645 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacePx.y * (inv.numRows - 1));
Jon Mirandae126d722021-02-25 10:45:20 -0500646 }
647
Jon Miranda6f7e9702019-09-16 14:44:14 -0700648 /**
649 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
650 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
651 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
652 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800653 public void updateIconSize(float scale, Resources res) {
Jon Mirandaab3c6812021-06-28 15:42:28 -0700654 // Icon scale should never exceed 1, otherwise pixellation may occur.
655 iconScale = Math.min(1f, scale);
656 cellScaleToFit = scale;
657
Jon Miranda18751b62017-07-31 17:25:27 -0700658 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800659 final boolean isVerticalLayout = isVerticalBarLayout();
Thales Lima83bedbf2021-10-05 17:47:39 +0100660 float invIconSizeDp = inv.iconSize[mTypeIndex];
661 float invIconTextSizeSp = inv.iconTextSize[mTypeIndex];
Andras Kloczl30fe9152021-08-05 18:02:29 +0200662
Jon Mirandaab3c6812021-06-28 15:42:28 -0700663 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, iconScale));
Jon Mirandaab3c6812021-06-28 15:42:28 -0700664 iconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * iconScale);
665 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * iconScale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700666
Thales Lima78d00ad2021-09-30 11:29:06 +0100667 cellLayoutBorderSpacePx = getCellLayoutBorderSpaceScaled(inv, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500668
669 if (isScalableGrid) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100670 cellWidthPx = pxFromDp(inv.minCellSize[mTypeIndex].x, mMetrics, scale);
671 cellHeightPx = pxFromDp(inv.minCellSize[mTypeIndex].y, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500672 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
673 + Utilities.calculateTextHeight(iconTextSizePx);
674 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
Thales Limad90faab2021-09-21 17:18:47 +0100675 desiredWorkspaceHorizontalMarginPx =
676 (int) (desiredWorkspaceHorizontalMarginOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500677 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500678 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700679 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR)
680 + iconDrawablePaddingPx
Jon Miranda228877d2021-02-09 11:05:00 -0500681 + Utilities.calculateTextHeight(iconTextSizePx);
682 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
683 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
684 && !isMultiWindowMode) {
685 // Ensures that the label is closer to its corresponding icon. This is not an issue
686 // with vertical bar layout or multi-window mode since the issue is handled
687 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
688 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
689 iconDrawablePaddingPx = cellPaddingY;
690 }
Jon Miranda846455e2017-10-02 14:58:52 -0700691 }
Jon Miranda18751b62017-07-31 17:25:27 -0700692
Sunny Goyalae190ff2020-04-14 00:19:01 +0000693 // All apps
Sunny Goyal19ff7282021-04-22 10:12:54 -0700694 if (numShownAllAppsColumns != inv.numColumns) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100695 allAppsIconSizePx =
696 pxFromDp(inv.iconSize[InvariantDeviceProfile.INDEX_ALL_APPS], mMetrics);
697 allAppsIconTextSizePx =
698 pxFromSp(inv.iconTextSize[InvariantDeviceProfile.INDEX_ALL_APPS], mMetrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000699 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda941375e2021-03-31 13:10:45 -0400700 autoResizeAllAppsCells();
Sunny Goyalae190ff2020-04-14 00:19:01 +0000701 } else {
702 allAppsIconSizePx = iconSizePx;
703 allAppsIconTextSizePx = iconTextSizePx;
704 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
705 allAppsCellHeightPx = getCellSize().y;
706 }
707 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700708 updateAllAppsWidth();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700709
Jon Miranda941375e2021-03-31 13:10:45 -0400710 if (isVerticalLayout) {
711 hideWorkspaceLabelsIfNotEnoughSpace();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700712 }
Winson Chungb3800242013-10-24 11:01:54 -0700713
Winson Chungb3800242013-10-24 11:01:54 -0700714 // Hotseat
Jon Miranda0d284852021-06-22 14:50:55 -0700715 updateHotseatIconSize(iconSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700716
Sunny Goyal07b69292018-01-08 14:19:34 -0800717 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700718 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700719 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700720 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
721 workspaceSpringLoadShrinkFactor = Math.min(
722 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
723 1 - (minRequiredHeight / expectedWorkspaceHeight));
724 } else {
725 workspaceSpringLoadShrinkFactor =
726 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
727 }
728
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700729 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700730 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
731 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700732 }
733
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700734 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700735 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700736
Jon Mirandae126d722021-02-25 10:45:20 -0500737 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
738
Jon Mirandac1b23992016-12-13 08:57:36 -0800739 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700740 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800741 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800742
743 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500744 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Thales Lima78d00ad2021-09-30 11:29:06 +0100745 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacePx.y);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800746 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500747 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800748 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800749
750 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500751 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Thales Lima78d00ad2021-09-30 11:29:06 +0100752 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacePx.x);
Jon Mirandae126d722021-02-25 10:45:20 -0500753 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
754 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800755 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800756
757 float scale = Math.min(scaleX, scaleY);
758 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700759 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700760 }
761 }
762
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700763 private void updateFolderCellSize(float scale, Resources res) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100764 float invIconSizeDp = isVerticalBarLayout()
765 ? inv.iconSize[InvariantDeviceProfile.INDEX_LANDSCAPE]
766 : inv.iconSize[InvariantDeviceProfile.INDEX_DEFAULT];
Sunny Goyal35c7b192021-04-20 16:51:10 -0700767 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Thales Lima83bedbf2021-10-05 17:47:39 +0100768 folderChildTextSizePx =
769 pxFromSp(inv.iconTextSize[InvariantDeviceProfile.INDEX_DEFAULT], mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500770 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700771
772 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700773
Jon Miranda823da222021-03-23 08:08:45 -0400774 if (isScalableGrid) {
Jon Mirandaf33f5b32021-07-22 17:15:31 -0700775 int minWidth = folderChildIconSizePx + iconDrawablePaddingPx * 2;
776 int minHeight = folderChildIconSizePx + iconDrawablePaddingPx * 2 + textHeight;
777
778 folderCellWidthPx = (int) Math.max(minWidth, cellWidthPx * scale);
779 folderCellHeightPx = (int) Math.max(minHeight, cellHeightPx * scale);
Jon Miranda823da222021-03-23 08:08:45 -0400780
Thales Lima78d00ad2021-09-30 11:29:06 +0100781 int scaledSpace = (int) (folderCellLayoutBorderSpaceOriginalPx * scale);
782 folderCellLayoutBorderSpacePx = new Point(scaledSpace, scaledSpace);
783 folderContentPaddingLeftRight = scaledSpace;
784 folderContentPaddingTop = scaledSpace;
Jon Miranda823da222021-03-23 08:08:45 -0400785 } else {
786 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
787 * scale);
788 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
789 * scale);
790
791 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
792 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
793 }
794
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000795 folderChildDrawablePaddingPx = Math.max(0,
796 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700797 }
798
Winson1f064272016-07-18 17:18:02 -0700799 public void updateInsets(Rect insets) {
800 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800801 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700802 }
803
Sunny Goyalae6e3182019-04-30 12:04:37 -0700804 /**
805 * The current device insets. This is generally same as the insets being dispatched to
806 * {@link Insettable} elements, but can differ if the element is using a different profile.
807 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800808 public Rect getInsets() {
809 return mInsets;
810 }
811
Sunny Goyal756cd262015-08-20 12:33:21 -0700812 public Point getCellSize() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700813 return getCellSize(null);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700814 }
815
Sunny Goyal19ff7282021-04-22 10:12:54 -0700816 public Point getCellSize(Point result) {
817 if (result == null) {
818 result = new Point();
819 }
Thales Limad1df5fc2021-09-03 18:37:19 +0100820
Sunny Goyal756cd262015-08-20 12:33:21 -0700821 // Since we are only concerned with the overall padding, layout direction does
822 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700823 Point padding = getTotalWorkspacePadding();
Thales Limad1df5fc2021-09-03 18:37:19 +0100824
825 int numColumns = isTwoPanels ? inv.numColumns * 2 : inv.numColumns;
Thales Lima7a6752d2021-09-23 14:31:00 +0100826 int cellLayoutTotalPadding =
827 isTwoPanels ? 4 * cellLayoutPaddingLeftRightPx : 2 * cellLayoutPaddingLeftRightPx;
828 int screenWidthPx = availableWidthPx - padding.x - cellLayoutTotalPadding;
Thales Lima78d00ad2021-09-30 11:29:06 +0100829 result.x = calculateCellWidth(screenWidthPx, cellLayoutBorderSpacePx.x, numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700830 result.y = calculateCellHeight(availableHeightPx - padding.y
Thales Lima78d00ad2021-09-30 11:29:06 +0100831 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacePx.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700832 return result;
833 }
834
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700835 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800836 updateWorkspacePadding();
837 return new Point(workspacePadding.left + workspacePadding.right,
838 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700839 }
840
841 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800842 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
843 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700844 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800845 private void updateWorkspacePadding() {
846 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700847 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800848 padding.top = 0;
849 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800850 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700851 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700852 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700853 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700854 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700855 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700856 }
Winson Chungb3800242013-10-24 11:01:54 -0700857 } else {
Thales Lima7a6752d2021-09-23 14:31:00 +0100858 // Pad the bottom of the workspace with search/hotseat bar sizes
Tony Wickham21970cc2021-09-15 15:44:05 -0700859 int hotseatTop = hotseatBarSizePx;
Tony Wickhama29a0462021-03-02 12:28:25 -0800860 int paddingBottom = hotseatTop + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500861 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100862
Thales Limad90faab2021-09-21 17:18:47 +0100863 padding.set(desiredWorkspaceHorizontalMarginPx,
Thales Lima83bedbf2021-10-05 17:47:39 +0100864 workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx),
Thales Limad90faab2021-09-21 17:18:47 +0100865 desiredWorkspaceHorizontalMarginPx,
Thales Lima7a6752d2021-09-23 14:31:00 +0100866 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700867 }
Winson Chungb3800242013-10-24 11:01:54 -0700868 }
869
Sunny Goyal57b22792021-05-25 14:35:01 -0700870 /**
871 * Returns the padding for hotseat view
872 */
873 public Rect getHotseatLayoutPadding(Context context) {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700874 if (isVerticalBarLayout()) {
875 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700876 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
877 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700878 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700879 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
880 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700881 }
Sunny Goyal57b22792021-05-25 14:35:01 -0700882 } else if (isTaskbarPresent) {
Tony Wickham21970cc2021-09-15 15:44:05 -0700883 int hotseatHeight = workspacePadding.bottom;
Sunny Goyal57b22792021-05-25 14:35:01 -0700884 int taskbarOffset = getTaskbarOffsetY();
Tony Wickham21970cc2021-09-15 15:44:05 -0700885 int hotseatTopDiff = hotseatHeight - taskbarOffset;
Sunny Goyal57b22792021-05-25 14:35:01 -0700886
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700887 int endOffset = ApiWrapper.getHotseatEndOffset(context);
Sunny Goyal57b22792021-05-25 14:35:01 -0700888 int requiredWidth = iconSizePx * numShownHotseatIcons;
889
890 Resources res = context.getResources();
891 float taskbarIconSize = res.getDimension(R.dimen.taskbar_icon_size);
892 float taskbarIconSpacing = 2 * res.getDimension(R.dimen.taskbar_icon_spacing);
893 int maxSize = (int) (requiredWidth
894 * (taskbarIconSize + taskbarIconSpacing) / taskbarIconSize);
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700895 int hotseatSize = Math.min(maxSize, availableWidthPx - endOffset);
Sunny Goyal57b22792021-05-25 14:35:01 -0700896 int sideSpacing = (availableWidthPx - hotseatSize) / 2;
897 mHotseatPadding.set(sideSpacing, hotseatTopDiff, sideSpacing, taskbarOffset);
898
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700899 if (endOffset > sideSpacing) {
Sunny Goyal57b22792021-05-25 14:35:01 -0700900 int diff = Utilities.isRtl(context.getResources())
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700901 ? sideSpacing - endOffset
902 : endOffset - sideSpacing;
903 mHotseatPadding.left -= diff;
904 mHotseatPadding.right += diff;
Sunny Goyal57b22792021-05-25 14:35:01 -0700905 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700906 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700907 // We want the edges of the hotseat to line up with the edges of the workspace, but the
908 // icons in the hotseat are a different size, and so don't line up perfectly. To account
909 // for this, we pad the left and right of the hotseat with half of the difference of a
910 // workspace cell vs a hotseat cell.
911 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700912 float hotseatCellWidth = (float) widthPx / numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700913 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
914 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700915 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
916 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700917 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700918 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
919 + mInsets.right,
Jon Miranda0d284852021-06-22 14:50:55 -0700920 hotseatBarSizePx - hotseatCellHeightPx - hotseatBarTopPaddingPx
921 + cellLayoutBottomPaddingPx + mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700922 }
923 return mHotseatPadding;
924 }
925
Winsonfadbe8f2016-07-22 16:35:37 -0700926 /**
Sunny Goyal57b22792021-05-25 14:35:01 -0700927 * Returns the number of pixels the QSB is translated from the bottom of the screen.
928 */
929 public int getQsbOffsetY() {
930 int freeSpace = isTaskbarPresent
931 ? workspacePadding.bottom
932 : hotseatBarSizePx - hotseatCellHeightPx - hotseatQsbHeight;
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000933
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000934 if (isScalableGrid && qsbBottomMarginPx > mInsets.bottom) {
Tony Wickham21970cc2021-09-15 15:44:05 -0700935 // Note that taskbarSize = 0 unless isTaskbarPresent.
936 return Math.min(qsbBottomMarginPx + taskbarSize, freeSpace);
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000937 } else {
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000938 return (int) (freeSpace * QSB_CENTER_FACTOR)
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100939 + (isTaskbarPresent ? taskbarSize : mInsets.bottom);
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000940 }
Sunny Goyal57b22792021-05-25 14:35:01 -0700941 }
942
943 /**
944 * Returns the number of pixels the taskbar is translated from the bottom of the screen.
945 */
946 public int getTaskbarOffsetY() {
947 return (getQsbOffsetY() - taskbarSize) / 2;
948 }
949
950 /**
Winsonfadbe8f2016-07-22 16:35:37 -0700951 * @return the bounds for which the open folders should be contained within
952 */
953 public Rect getAbsoluteOpenFolderBounds() {
954 if (isVerticalBarLayout()) {
955 // Folders should only appear right of the drop target bar and left of the hotseat
956 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
957 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700958 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700959 mInsets.top + availableHeightPx);
960 } else {
961 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -0800962 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800963 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700964 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800965 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -0800966 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -0700967 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700968 }
969 }
970
Jon Miranda228877d2021-02-09 11:05:00 -0500971 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
972 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700973 }
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100974
Jon Miranda228877d2021-02-09 11:05:00 -0500975 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
976 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700977 }
978
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700979 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700980 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
981 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
982 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700983 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700984 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700985 return isLandscape && transposeLayoutWithOrientation;
986 }
987
Sunny Goyal59d086c2018-05-07 17:31:40 -0700988 /**
989 * Updates orientation information and returns true if it has changed from the previous value.
990 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700991 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700992 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700993 boolean isSeascape = DisplayController.INSTANCE.get(context)
994 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700995 if (mIsSeascape != isSeascape) {
996 mIsSeascape = isSeascape;
997 return true;
998 }
999 }
1000 return false;
1001 }
1002
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001003 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001004 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001005 }
1006
Sunny Goyal07b69292018-01-08 14:19:34 -08001007 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001008 return isVerticalBarLayout();
Winson Chungb3800242013-10-24 11:01:54 -07001009 }
1010
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001011 public int getCellContentHeight(@ContainerType int containerType) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001012 switch (containerType) {
1013 case CellLayout.WORKSPACE:
1014 return cellHeightPx;
1015 case CellLayout.FOLDER:
1016 return folderCellHeightPx;
1017 case CellLayout.HOTSEAT:
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001018 // The hotseat is the only container where the cell height is going to be
1019 // different from the content within that cell.
1020 return iconSizePx;
Sunny Goyalc13403c2016-11-18 23:44:48 -08001021 default:
1022 // ??
1023 return 0;
1024 }
1025 }
Sunny Goyal13178ac2016-04-04 16:35:22 -07001026
Jon Miranda58561d42021-03-17 10:51:54 -04001027 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001028 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -04001029 }
1030
1031 public void dump(String prefix, PrintWriter writer) {
1032 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -07001033 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -04001034
Alex Chaua02eddc2021-04-29 00:36:06 +01001035 writer.println(prefix + "\tallowRotation:" + allowRotation);
Jon Miranda58561d42021-03-17 10:51:54 -04001036 writer.println(prefix + "\tisTablet:" + isTablet);
Jon Miranda58561d42021-03-17 10:51:54 -04001037 writer.println(prefix + "\tisPhone:" + isPhone);
1038 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
1039 + transposeLayoutWithOrientation);
1040
1041 writer.println(prefix + "\tisLandscape:" + isLandscape);
1042 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
Sunny Goyal19ff7282021-04-22 10:12:54 -07001043 writer.println(prefix + "\tisTwoPanels:" + isTwoPanels);
Jon Miranda58561d42021-03-17 10:51:54 -04001044
1045 writer.println(prefix + pxToDpStr("windowX", windowX));
1046 writer.println(prefix + pxToDpStr("windowY", windowY));
1047 writer.println(prefix + pxToDpStr("widthPx", widthPx));
1048 writer.println(prefix + pxToDpStr("heightPx", heightPx));
1049
1050 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
1051 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
1052
1053 writer.println(prefix + "\taspectRatio:" + aspectRatio);
1054
1055 writer.println(prefix + "\tisScalableGrid:" + isScalableGrid);
1056
Thales Lima83bedbf2021-10-05 17:47:39 +01001057 writer.println(prefix + "\tinv.numColumns: " + inv.numColumns);
1058 writer.println(prefix + "\tinv.numRows: " + inv.numRows);
Jon Miranda58561d42021-03-17 10:51:54 -04001059
Thales Lima83bedbf2021-10-05 17:47:39 +01001060 writer.println(prefix + "\tminCellSize: " + inv.minCellSize[mTypeIndex] + "dp");
Jon Mirandaab3c6812021-06-28 15:42:28 -07001061
Jon Miranda58561d42021-03-17 10:51:54 -04001062 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
1063 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
1064
1065 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
1066 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
1067
Thales Lima83bedbf2021-10-05 17:47:39 +01001068 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Horizontal",
1069 cellLayoutBorderSpacePx.x));
1070 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Vertical",
1071 cellLayoutBorderSpacePx.y));
1072
Jon Miranda58561d42021-03-17 10:51:54 -04001073 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
1074 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
1075 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
1076
1077 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
1078 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
1079 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
1080 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
1081 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
1082 folderChildDrawablePaddingPx));
Thales Lima78d00ad2021-09-30 11:29:06 +01001083 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpaceOriginalPx",
1084 folderCellLayoutBorderSpaceOriginalPx));
1085 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Horizontal",
1086 folderCellLayoutBorderSpacePx.x));
1087 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Vertical",
1088 folderCellLayoutBorderSpacePx.y));
Jon Miranda58561d42021-03-17 10:51:54 -04001089
Jon Miranda58561d42021-03-17 10:51:54 -04001090 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
1091 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
1092 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
1093 allAppsIconDrawablePaddingPx));
1094 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001095 writer.println(prefix + "\tnumShownAllAppsColumns: " + numShownAllAppsColumns);
Jon Miranda58561d42021-03-17 10:51:54 -04001096
1097 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
1098 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
1099 writer.println(prefix + pxToDpStr("hotseatBarTopPaddingPx", hotseatBarTopPaddingPx));
1100 writer.println(prefix + pxToDpStr("hotseatBarBottomPaddingPx", hotseatBarBottomPaddingPx));
1101 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
1102 hotseatBarSidePaddingStartPx));
1103 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
1104 hotseatBarSidePaddingEndPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001105 writer.println(prefix + "\tnumShownHotseatIcons: " + numShownHotseatIcons);
Jon Miranda58561d42021-03-17 10:51:54 -04001106
1107 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
Tony Wickham635e1802021-07-22 14:28:04 -10001108 writer.println(prefix + "\tisTaskbarPresentInApps:" + isTaskbarPresentInApps);
Jon Miranda58561d42021-03-17 10:51:54 -04001109 writer.println(prefix + pxToDpStr("taskbarSize", taskbarSize));
Jon Miranda58561d42021-03-17 10:51:54 -04001110
Thales Lima83bedbf2021-10-05 17:47:39 +01001111 writer.println(prefix + pxToDpStr("desiredWorkspaceHorizontalMarginPx",
1112 desiredWorkspaceHorizontalMarginPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001113 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
1114 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
1115 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
1116 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
1117
Jon Mirandaab3c6812021-06-28 15:42:28 -07001118 writer.println(prefix + pxToDpStr("iconScale", iconScale));
1119 writer.println(prefix + pxToDpStr("cellScaleToFit ", cellScaleToFit));
Jon Miranda58561d42021-03-17 10:51:54 -04001120 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Thales Lima7a6752d2021-09-23 14:31:00 +01001121 writer.println(prefix + pxToDpStr("unscaled extraSpace", extraSpace / iconScale));
Jon Mirandac9e69fa2021-03-22 17:13:34 -04001122
1123 if (inv.devicePaddings != null) {
1124 int unscaledExtraSpace = (int) (extraSpace / iconScale);
1125 writer.println(prefix + pxToDpStr("maxEmptySpace",
1126 inv.devicePaddings.getDevicePadding(unscaledExtraSpace).getMaxEmptySpacePx()));
1127 }
Jon Miranda58561d42021-03-17 10:51:54 -04001128 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
1129 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
1130 writer.println(prefix + pxToDpStr("extraHotseatBottomPadding", extraHotseatBottomPadding));
1131 }
1132
Sunny Goyalfd58da62020-08-11 12:06:49 -07001133 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -07001134 Configuration config = new Configuration(c.getResources().getConfiguration());
1135 config.orientation = orientation;
Sunny Goyal35c7b192021-04-20 16:51:10 -07001136 config.densityDpi = info.densityDpi;
Sunny Goyal1890f672020-04-30 12:28:00 -07001137 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -07001138 }
Sunny Goyalfde55052018-02-01 14:46:13 -08001139
1140 /**
1141 * Callback when a component changes the DeviceProfile associated with it, as a result of
1142 * configuration change
1143 */
1144 public interface OnDeviceProfileChangeListener {
1145
1146 /**
1147 * Called when the device profile is reassigned. Note that for layout and measurements, it
1148 * is sufficient to listen for inset changes. Use this callback when you need to perform
1149 * a one time operation.
1150 */
1151 void onDeviceProfileChanged(DeviceProfile dp);
1152 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001153
1154 public static class Builder {
1155 private Context mContext;
1156 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -07001157 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001158
Sunny Goyal19ff7282021-04-22 10:12:54 -07001159 private WindowBounds mWindowBounds;
1160 private boolean mUseTwoPanels;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001161
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001162 private boolean mIsMultiWindowMode = false;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001163 private Boolean mTransposeLayoutWithOrientation;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001164
Sunny Goyalfd58da62020-08-11 12:06:49 -07001165 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001166 mContext = context;
1167 mInv = inv;
1168 mInfo = info;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001169 }
1170
1171 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
1172 mIsMultiWindowMode = isMultiWindowMode;
1173 return this;
1174 }
1175
Sunny Goyal19ff7282021-04-22 10:12:54 -07001176 public Builder setUseTwoPanels(boolean useTwoPanels) {
1177 mUseTwoPanels = useTwoPanels;
1178 return this;
1179 }
1180
1181
1182 public Builder setWindowBounds(WindowBounds bounds) {
1183 mWindowBounds = bounds;
Sunny Goyal0addbf02020-04-28 14:17:35 -07001184 return this;
1185 }
1186
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001187 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
1188 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
1189 return this;
1190 }
1191
1192 public DeviceProfile build() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001193 if (mWindowBounds == null) {
1194 throw new IllegalArgumentException("Window bounds not set");
1195 }
1196 if (mTransposeLayoutWithOrientation == null) {
1197 mTransposeLayoutWithOrientation = !mInfo.isTablet(mWindowBounds);
1198 }
1199 return new DeviceProfile(mContext, mInv, mInfo, mWindowBounds,
1200 mIsMultiWindowMode, mTransposeLayoutWithOrientation, mUseTwoPanels);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001201 }
1202 }
1203
Winson Chungb3800242013-10-24 11:01:54 -07001204}