blob: e64ea90160b6c7b5fcfe8c29508273edd19f7e88 [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 Chau90125822021-11-17 14:16:58 +0000254 isTwoPanels = isTablet && useTwoPanels;
Adam Cohen2e6da152015-05-06 11:42:25 -0700255
Jon Mirandabba64512019-03-27 10:54:17 -0700256 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda7e183c32018-06-20 11:05:27 -0700257 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700258
259 // Some more constants
Sunny Goyal1890f672020-04-30 12:28:00 -0700260 context = getContext(context, info, isVerticalBarLayout()
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700261 ? Configuration.ORIENTATION_LANDSCAPE
262 : Configuration.ORIENTATION_PORTRAIT);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700263 mMetrics = context.getResources().getDisplayMetrics();
Sunny Goyal1890f672020-04-30 12:28:00 -0700264 final Resources res = context.getResources();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700265
Thales Lima83bedbf2021-10-05 17:47:39 +0100266 if (isTwoPanels) {
267 if (isLandscape) {
268 mTypeIndex = InvariantDeviceProfile.INDEX_TWO_PANEL_LANDSCAPE;
269 } else {
270 mTypeIndex = InvariantDeviceProfile.INDEX_TWO_PANEL_PORTRAIT;
271 }
272 } else {
273 if (isLandscape) {
274 mTypeIndex = InvariantDeviceProfile.INDEX_LANDSCAPE;
275 } else {
276 mTypeIndex = InvariantDeviceProfile.INDEX_DEFAULT;
277 }
278 }
279
Sunny Goyal57b22792021-05-25 14:35:01 -0700280 hotseatQsbHeight = res.getDimensionPixelSize(R.dimen.qsb_widget_height);
Sunny Goyal71ef9682021-07-29 12:59:27 -0700281 isTaskbarPresent = isTablet && ApiWrapper.TASKBAR_DRAWN_IN_PROCESS
282 && FeatureFlags.ENABLE_TASKBAR.get();
Tony Wickham15883892021-02-12 11:24:40 -0800283 if (isTaskbarPresent) {
Tony Wickham15883892021-02-12 11:24:40 -0800284 taskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_size);
Tony Wickham15883892021-02-12 11:24:40 -0800285 }
Tony Wickham15883892021-02-12 11:24:40 -0800286
Winson Chungb3800242013-10-24 11:01:54 -0700287 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700288
Thales Limad90faab2021-09-21 17:18:47 +0100289 desiredWorkspaceHorizontalMarginPx = getHorizontalMarginPx(inv, res);
290 desiredWorkspaceHorizontalMarginOriginalPx = desiredWorkspaceHorizontalMarginPx;
Samuel Fufaee9aff92021-04-05 13:30:40 -0500291
292 allAppsOpenVerticalTranslate = res.getDimensionPixelSize(
293 R.dimen.all_apps_open_vertical_translate);
294
Jon Mirandae126d722021-02-25 10:45:20 -0500295 folderLabelTextScale = res.getFloat(R.dimen.folder_label_text_scale);
296 folderContentPaddingLeftRight =
297 res.getDimensionPixelSize(R.dimen.folder_content_padding_left_right);
298 folderContentPaddingTop = res.getDimensionPixelSize(R.dimen.folder_content_padding_top);
299
Thales Lima78d00ad2021-09-30 11:29:06 +0100300 cellLayoutBorderSpacePx = getCellLayoutBorderSpace(inv);
Thales Lima83bedbf2021-10-05 17:47:39 +0100301 allAppsCellSpacePx = new Point(
302 pxFromDp(inv.borderSpaces[InvariantDeviceProfile.INDEX_ALL_APPS].x, mMetrics, 1f),
303 pxFromDp(inv.borderSpaces[InvariantDeviceProfile.INDEX_ALL_APPS].y, mMetrics, 1f));
Thales Lima78d00ad2021-09-30 11:29:06 +0100304 cellLayoutBorderSpaceOriginalPx = new Point(cellLayoutBorderSpacePx);
305 folderCellLayoutBorderSpaceOriginalPx = pxFromDp(inv.folderBorderSpace, mMetrics, 1f);
306 folderCellLayoutBorderSpacePx = new Point(folderCellLayoutBorderSpaceOriginalPx,
307 folderCellLayoutBorderSpaceOriginalPx);
Jon Mirandae126d722021-02-25 10:45:20 -0500308
Jon Miranda5eacbb72018-07-31 11:14:46 -0700309 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
310 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Jon Mirandae126d722021-02-25 10:45:20 -0500311 int cellLayoutPadding = isScalableGrid
312 ? 0
313 : res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100314
Sunny Goyal19ff7282021-04-22 10:12:54 -0700315 if (isTwoPanels) {
Thales Lima7a6752d2021-09-23 14:31:00 +0100316 cellLayoutPaddingLeftRightPx = 0;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100317 cellLayoutBottomPaddingPx = 0;
318 } else if (isLandscape) {
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700319 cellLayoutPaddingLeftRightPx = 0;
320 cellLayoutBottomPaddingPx = cellLayoutPadding;
321 } else {
322 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
323 cellLayoutBottomPaddingPx = 0;
324 }
325
Tony Wickham5edf9e22020-03-27 20:06:52 -0700326 workspacePageIndicatorHeight = res.getDimensionPixelSize(
327 R.dimen.workspace_page_indicator_height);
328 mWorkspacePageIndicatorOverlapWorkspace =
329 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700330
Winson Chungb3800242013-10-24 11:01:54 -0700331 iconDrawablePaddingOriginalPx =
332 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Alex Chaua02eddc2021-04-29 00:36:06 +0100333
Sunny Goyal47328fd2016-05-25 18:56:41 -0700334 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
Alex Chaua02eddc2021-04-29 00:36:06 +0100335 dropTargetDragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
336 dropTargetTextSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size);
337
Sunny Goyal47328fd2016-05-25 18:56:41 -0700338 workspaceSpringLoadedBottomSpace =
339 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700340
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700341 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700342
Sunny Goyal19ff7282021-04-22 10:12:54 -0700343 numShownHotseatIcons =
344 isTwoPanels ? inv.numDatabaseHotseatIcons : inv.numShownHotseatIcons;
345 numShownAllAppsColumns =
346 isTwoPanels ? inv.numDatabaseAllAppsColumns : inv.numAllAppsColumns;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700347 hotseatBarSizeExtraSpacePx = 0;
Winson1f064272016-07-18 17:18:02 -0700348 hotseatBarTopPaddingPx =
349 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700350 hotseatBarBottomPaddingPx = (isTallDevice ? 0
351 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
352 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700353 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800354 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700355 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700356 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Jon Miranda0d284852021-06-22 14:50:55 -0700357 hotseatExtraVerticalSize =
Jon Miranda228877d2021-02-09 11:05:00 -0500358 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size);
Thales Lima83bedbf2021-10-05 17:47:39 +0100359 updateHotseatIconSize(
360 pxFromDp(inv.iconSize[InvariantDeviceProfile.INDEX_DEFAULT], mMetrics, 1f));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700361
Jon Miranda8bdb2222021-06-23 18:10:10 -0700362 qsbBottomMarginOriginalPx = isScalableGrid
363 ? res.getDimensionPixelSize(R.dimen.scalable_grid_qsb_bottom_margin)
364 : 0;
365
Alex Chau646f8042021-07-26 11:56:22 +0100366 overviewShowAsGrid = isTablet && FeatureFlags.ENABLE_OVERVIEW_GRID.get();
Alex Chau5fd9d492021-07-22 17:27:11 +0100367 overviewTaskMarginPx = overviewShowAsGrid
Alex Chaueaa6d682021-08-17 12:56:15 +0100368 ? res.getDimensionPixelSize(R.dimen.overview_task_margin_focused)
Alex Chau5fd9d492021-07-22 17:27:11 +0100369 : res.getDimensionPixelSize(R.dimen.overview_task_margin);
Alex Chaueaa6d682021-08-17 12:56:15 +0100370 overviewTaskMarginGridPx = res.getDimensionPixelSize(R.dimen.overview_task_margin_grid);
Alex Chau5fd9d492021-07-22 17:27:11 +0100371 overviewTaskIconSizePx = res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_size);
Alex Chauac9df382021-08-02 19:08:41 +0100372 overviewTaskIconDrawableSizePx =
373 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size);
374 overviewTaskIconDrawableSizeGridPx =
375 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size_grid);
Alex Chaudedbc8a2021-03-17 16:53:26 +0000376 overviewTaskThumbnailTopMarginPx = overviewTaskIconSizePx + overviewTaskMarginPx * 2;
Alex Chau5fd9d492021-07-22 17:27:11 +0100377 if (overviewShowAsGrid) {
378 if (isLandscape) {
379 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
380 R.dimen.overview_actions_top_margin_gesture_grid_landscape);
381 overviewActionsBottomMarginGesturePx = res.getDimensionPixelSize(
382 R.dimen.overview_actions_bottom_margin_gesture_grid_landscape);
383 } else {
384 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
385 R.dimen.overview_actions_top_margin_gesture_grid_portrait);
386 overviewActionsBottomMarginGesturePx = res.getDimensionPixelSize(
387 R.dimen.overview_actions_bottom_margin_gesture_grid_portrait);
388 }
389 } else {
390 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
391 R.dimen.overview_actions_margin_gesture);
392 overviewActionsBottomMarginGesturePx = overviewActionsTopMarginGesturePx;
393 }
zakcohendad8a802021-05-20 09:52:26 -0700394 overviewActionsMarginThreeButtonPx = res.getDimensionPixelSize(
Alex Chau5fd9d492021-07-22 17:27:11 +0100395 R.dimen.overview_actions_margin_three_button);
396 overviewPageSpacing = overviewShowAsGrid
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100397 ? res.getDimensionPixelSize(R.dimen.recents_page_spacing_grid)
398 : res.getDimensionPixelSize(R.dimen.recents_page_spacing);
Alex Chau5fd9d492021-07-22 17:27:11 +0100399 overviewRowSpacing = isLandscape
400 ? res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing_landscape)
401 : res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing_portrait);
Alex Chau56bd2572021-11-03 18:48:18 +0000402 overviewGridSideMargin = isLandscape
403 ? res.getDimensionPixelSize(R.dimen.overview_grid_side_margin_landscape)
404 : res.getDimensionPixelSize(R.dimen.overview_grid_side_margin_portrait);
Zak Cohen334efeb2021-03-19 16:42:07 -0700405
Jon Miranda2ed276e2017-06-29 11:36:34 -0700406 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400407 extraSpace = updateAvailableDimensions(res);
Jon Miranda80cddbc2021-07-22 13:51:16 -0700408
Jon Miranda2ed276e2017-06-29 11:36:34 -0700409 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400410 if (isScalableGrid && inv.devicePaddings != null) {
411 // Paddings were created assuming no scaling, so we first unscale the extra space.
Jon Mirandaab3c6812021-06-28 15:42:28 -0700412 int unscaledExtraSpace = (int) (extraSpace / cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400413 DevicePadding padding = inv.devicePaddings.getDevicePadding(unscaledExtraSpace);
Jon Miranda228877d2021-02-09 11:05:00 -0500414
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400415 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
416 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
417 int paddingHotseatBottom = padding.getHotseatBottomPadding(unscaledExtraSpace);
418
Jon Mirandaab3c6812021-06-28 15:42:28 -0700419 workspaceTopPadding = Math.round(paddingWorkspaceTop * cellScaleToFit);
420 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * cellScaleToFit);
421 extraHotseatBottomPadding = Math.round(paddingHotseatBottom * cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400422
Jon Miranda58561d42021-03-17 10:51:54 -0400423 hotseatBarSizePx += extraHotseatBottomPadding;
Jon Miranda8bdb2222021-06-23 18:10:10 -0700424
Jon Mirandaab3c6812021-06-28 15:42:28 -0700425 qsbBottomMarginPx = Math.round(qsbBottomMarginOriginalPx * cellScaleToFit);
Jon Miranda228877d2021-02-09 11:05:00 -0500426 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700427 // We increase the hotseat size when there is extra space.
Jon Miranda80cddbc2021-07-22 13:51:16 -0700428
Jon Miranda80dda302021-07-28 14:14:59 -0700429 if (Float.compare(aspectRatio, TALLER_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0
430 && extraSpace >= Utilities.dpToPx(TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP)) {
431 // For taller devices, we will take a piece of the extra space from each row,
Jon Miranda80cddbc2021-07-22 13:51:16 -0700432 // and add it to the space above and below the hotseat.
Jon Miranda80dda302021-07-28 14:14:59 -0700433
434 // For devices with more extra space, we take a larger piece from each cell.
435 int piece = extraSpace < Utilities.dpToPx(TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP)
Jon Mirandaa56cb842021-08-03 17:26:44 -0700436 ? 7 : 5;
Jon Miranda80dda302021-07-28 14:14:59 -0700437
Jon Miranda80cddbc2021-07-22 13:51:16 -0700438 int extraSpace = ((getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2)
Jon Miranda80dda302021-07-28 14:14:59 -0700439 * inv.numRows) / piece;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700440
Jon Mirandaa56cb842021-08-03 17:26:44 -0700441 workspaceTopPadding = extraSpace / 8;
442 int halfLeftOver = (extraSpace - workspaceTopPadding) / 2;
443 hotseatBarTopPaddingPx += halfLeftOver;
444 hotseatBarSizeExtraSpacePx = halfLeftOver;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700445 } else {
446 // ie. For a display with a large aspect ratio, we can keep the icons on the
447 // workspace in portrait mode closer together by adding more height to the hotseat.
448 // Note: This calculation was created after noticing a pattern in the design spec.
449 hotseatBarSizeExtraSpacePx = getCellSize().y - iconSizePx
450 - iconDrawablePaddingPx * 2 - workspacePageIndicatorHeight;
451 }
452
453 updateHotseatIconSize(iconSizePx);
Jon Miranda30d0aa22017-07-25 15:55:29 -0700454
Jon Miranda3f9bab22017-07-28 14:50:17 -0700455 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700456 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700457 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800458 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800459
Alex Chaua02eddc2021-04-29 00:36:06 +0100460 flingToDeleteThresholdVelocity = res.getDimensionPixelSize(
461 R.dimen.drag_flingToDeleteMinVelocity);
462
Tony Wickham1237df02017-02-24 08:59:36 -0800463 // This is done last, after iconSizePx is calculated above.
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700464 Path dotPath = GraphicsUtils.getShapePath(DEFAULT_DOT_SIZE);
465 mDotRendererWorkSpace = new DotRenderer(iconSizePx, dotPath, DEFAULT_DOT_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700466 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700467 new DotRenderer(allAppsIconSizePx, dotPath, DEFAULT_DOT_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700468 }
469
Thales Limad90faab2021-09-21 17:18:47 +0100470 private int getHorizontalMarginPx(InvariantDeviceProfile idp, Resources res) {
471 if (isVerticalBarLayout()) {
472 return 0;
473 }
474
Thales Lima83bedbf2021-10-05 17:47:39 +0100475 return isScalableGrid
476 ? pxFromDp(idp.horizontalMargin[mTypeIndex], mMetrics)
477 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
Thales Limad90faab2021-09-21 17:18:47 +0100478 }
479
Jon Miranda0d284852021-06-22 14:50:55 -0700480 private void updateHotseatIconSize(int hotseatIconSizePx) {
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700481 // Ensure there is enough space for folder icons, which have a slightly larger radius.
482 hotseatCellHeightPx = (int) Math.ceil(hotseatIconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda0d284852021-06-22 14:50:55 -0700483 if (isVerticalBarLayout()) {
484 hotseatBarSizePx = hotseatIconSizePx + hotseatBarSidePaddingStartPx
485 + hotseatBarSidePaddingEndPx;
486 } else {
487 hotseatBarSizePx = hotseatIconSizePx + hotseatBarTopPaddingPx
Jon Miranda80cddbc2021-07-22 13:51:16 -0700488 + hotseatBarBottomPaddingPx + (isScalableGrid ? 0 : hotseatExtraVerticalSize)
489 + hotseatBarSizeExtraSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700490 }
491 }
492
Thales Lima78d00ad2021-09-30 11:29:06 +0100493 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp) {
494 if (!isScalableGrid) {
495 return new Point(0, 0);
496 }
497
Thales Lima83bedbf2021-10-05 17:47:39 +0100498 int horizontalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].x, mMetrics);
499 int verticalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].y, mMetrics);
Thales Lima78d00ad2021-09-30 11:29:06 +0100500
501 return new Point(horizontalSpacePx, verticalSpacePx);
502 }
503
504 private Point getCellLayoutBorderSpaceScaled(InvariantDeviceProfile idp, float scale) {
505 Point original = getCellLayoutBorderSpace(idp);
506 return new Point((int) (original.x * scale), (int) (original.y * scale));
Jon Mirandae126d722021-02-25 10:45:20 -0500507 }
508
Sunny Goyal69a8eec2021-07-22 10:02:50 -0700509 public Info getDisplayInfo() {
510 return mInfo;
511 }
512
Jon Miranda611dba42021-03-02 14:12:13 -0500513 /**
514 * We inset the widget padding added by the system and instead rely on the border spacing
515 * between cells to create reliable consistency between widgets
516 */
517 public boolean shouldInsetWidgets() {
518 Rect widgetPadding = inv.defaultWidgetPadding;
519
Jon Miranda49811182021-06-03 09:52:40 -0700520 // Check all sides to ensure that the widget won't overlap into another cell, or into
521 // status bar.
522 return workspaceTopPadding > widgetPadding.top
Thales Lima78d00ad2021-09-30 11:29:06 +0100523 && cellLayoutBorderSpacePx.x > widgetPadding.left
524 && cellLayoutBorderSpacePx.y > widgetPadding.top
525 && cellLayoutBorderSpacePx.x > widgetPadding.right
526 && cellLayoutBorderSpacePx.y > widgetPadding.bottom;
Jon Miranda611dba42021-03-02 14:12:13 -0500527 }
Jon Mirandae126d722021-02-25 10:45:20 -0500528
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700529 public Builder toBuilder(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700530 WindowBounds bounds =
531 new WindowBounds(widthPx, heightPx, availableWidthPx, availableHeightPx);
532 bounds.bounds.offsetTo(windowX, windowY);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700533 return new Builder(context, inv, mInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700534 .setWindowBounds(bounds)
535 .setUseTwoPanels(isTwoPanels)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700536 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800537 }
538
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700539 public DeviceProfile copy(Context context) {
540 return toBuilder(context).build();
541 }
542
543 /**
544 * TODO: Move this to the builder as part of setMultiWindowMode
545 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700546 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700547 DeviceProfile profile = toBuilder(context)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700548 .setWindowBounds(windowBounds)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700549 .setMultiWindowMode(true)
550 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800551
Jon Miranda941375e2021-03-31 13:10:45 -0400552 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda93e1f042016-11-11 14:13:04 -0800553
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800554 // We use these scales to measure and layout the widgets using their full invariant profile
555 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
556 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
557 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
558 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800559 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800560
Jon Miranda93e1f042016-11-11 14:13:04 -0800561 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700562 }
563
Adam Cohen63f1ec02014-08-12 09:23:13 -0700564 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400565 * Checks if there is enough space for labels on the workspace.
566 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700567 * It is important to call this method after the All Apps variables have been set.
568 */
Jon Miranda941375e2021-03-31 13:10:45 -0400569 private void hideWorkspaceLabelsIfNotEnoughSpace() {
570 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
571 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
572 - iconTextHeight;
573
574 // We want enough space so that the text is closer to its corresponding icon.
575 if (workspaceCellPaddingY < iconTextHeight) {
576 iconTextSizePx = 0;
577 iconDrawablePaddingPx = 0;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700578 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda941375e2021-03-31 13:10:45 -0400579 autoResizeAllAppsCells();
580 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700581 }
Jon Miranda1091e532017-07-21 13:31:50 -0700582
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700583 /**
584 * Re-computes the all-apps cell size to be independent of workspace
585 */
586 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400587 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
588 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700589 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400590 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700591 }
592
sfufa@google.comde013292021-09-21 18:22:44 -0700593 private void updateAllAppsWidth() {
594 if (isTwoPanels) {
595 int usedWidth = (allAppsCellWidthPx * numShownAllAppsColumns)
Thales Lima83bedbf2021-10-05 17:47:39 +0100596 + (allAppsCellSpacePx.x * (numShownAllAppsColumns + 1));
sfufa@google.comde013292021-09-21 18:22:44 -0700597 allAppsLeftRightPadding = Math.max(1, (availableWidthPx - usedWidth) / 2);
598 } else {
599 allAppsLeftRightPadding =
Thales Limad90faab2021-09-21 17:18:47 +0100600 desiredWorkspaceHorizontalMarginPx + cellLayoutPaddingLeftRightPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700601 }
602 }
603
Jon Miranda228877d2021-02-09 11:05:00 -0500604 /**
605 * Returns the amount of extra (or unused) vertical space.
606 */
607 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700608 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800609
Jon Mirandae126d722021-02-25 10:45:20 -0500610 Point workspacePadding = getTotalWorkspacePadding();
611
612 // Check to see if the icons fit within the available height.
613 float usedHeight = getCellLayoutHeight();
614 final int maxHeight = availableHeightPx - workspacePadding.y;
Jon Miranda228877d2021-02-09 11:05:00 -0500615 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500616 float scaleY = maxHeight / usedHeight;
617 boolean shouldScale = scaleY < 1f;
618
619 float scaleX = 1f;
620 if (isScalableGrid) {
621 // We scale to fit the cellWidth and cellHeight in the available space.
622 // The benefit of scalable grids is that we can get consistent aspect ratios between
623 // devices.
Thales Limad1df5fc2021-09-03 18:37:19 +0100624 int numColumns = isTwoPanels ? inv.numColumns * 2 : inv.numColumns;
625 float usedWidth = (cellWidthPx * numColumns)
Thales Lima78d00ad2021-09-30 11:29:06 +0100626 + (cellLayoutBorderSpacePx.x * (numColumns - 1))
Thales Limad90faab2021-09-21 17:18:47 +0100627 + (desiredWorkspaceHorizontalMarginPx * 2);
Jon Mirandae126d722021-02-25 10:45:20 -0500628 // We do not subtract padding here, as we also scale the workspace padding if needed.
629 scaleX = availableWidthPx / usedWidth;
630 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700631 }
Jon Mirandae126d722021-02-25 10:45:20 -0500632
633 if (shouldScale) {
634 float scale = Math.min(scaleX, scaleY);
635 updateIconSize(scale, res);
636 extraHeight = Math.max(0, maxHeight - getCellLayoutHeight());
637 }
638
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700639 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500640 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700641 }
642
Jon Mirandae126d722021-02-25 10:45:20 -0500643 private int getCellLayoutHeight() {
Thales Lima78d00ad2021-09-30 11:29:06 +0100644 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacePx.y * (inv.numRows - 1));
Jon Mirandae126d722021-02-25 10:45:20 -0500645 }
646
Jon Miranda6f7e9702019-09-16 14:44:14 -0700647 /**
648 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
649 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
650 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
651 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800652 public void updateIconSize(float scale, Resources res) {
Jon Mirandaab3c6812021-06-28 15:42:28 -0700653 // Icon scale should never exceed 1, otherwise pixellation may occur.
654 iconScale = Math.min(1f, scale);
655 cellScaleToFit = scale;
656
Jon Miranda18751b62017-07-31 17:25:27 -0700657 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800658 final boolean isVerticalLayout = isVerticalBarLayout();
Thales Lima83bedbf2021-10-05 17:47:39 +0100659 float invIconSizeDp = inv.iconSize[mTypeIndex];
660 float invIconTextSizeSp = inv.iconTextSize[mTypeIndex];
Andras Kloczl30fe9152021-08-05 18:02:29 +0200661
Jon Mirandaab3c6812021-06-28 15:42:28 -0700662 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, iconScale));
Jon Mirandaab3c6812021-06-28 15:42:28 -0700663 iconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * iconScale);
664 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * iconScale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700665
Thales Lima78d00ad2021-09-30 11:29:06 +0100666 cellLayoutBorderSpacePx = getCellLayoutBorderSpaceScaled(inv, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500667
668 if (isScalableGrid) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100669 cellWidthPx = pxFromDp(inv.minCellSize[mTypeIndex].x, mMetrics, scale);
670 cellHeightPx = pxFromDp(inv.minCellSize[mTypeIndex].y, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500671 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
672 + Utilities.calculateTextHeight(iconTextSizePx);
673 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
Thales Limad90faab2021-09-21 17:18:47 +0100674 desiredWorkspaceHorizontalMarginPx =
675 (int) (desiredWorkspaceHorizontalMarginOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500676 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500677 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700678 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR)
679 + iconDrawablePaddingPx
Jon Miranda228877d2021-02-09 11:05:00 -0500680 + Utilities.calculateTextHeight(iconTextSizePx);
681 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
682 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
683 && !isMultiWindowMode) {
684 // Ensures that the label is closer to its corresponding icon. This is not an issue
685 // with vertical bar layout or multi-window mode since the issue is handled
686 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
687 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
688 iconDrawablePaddingPx = cellPaddingY;
689 }
Jon Miranda846455e2017-10-02 14:58:52 -0700690 }
Jon Miranda18751b62017-07-31 17:25:27 -0700691
Sunny Goyalae190ff2020-04-14 00:19:01 +0000692 // All apps
Sunny Goyal19ff7282021-04-22 10:12:54 -0700693 if (numShownAllAppsColumns != inv.numColumns) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100694 allAppsIconSizePx =
695 pxFromDp(inv.iconSize[InvariantDeviceProfile.INDEX_ALL_APPS], mMetrics);
696 allAppsIconTextSizePx =
697 pxFromSp(inv.iconTextSize[InvariantDeviceProfile.INDEX_ALL_APPS], mMetrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000698 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda941375e2021-03-31 13:10:45 -0400699 autoResizeAllAppsCells();
Sunny Goyalae190ff2020-04-14 00:19:01 +0000700 } else {
701 allAppsIconSizePx = iconSizePx;
702 allAppsIconTextSizePx = iconTextSizePx;
703 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
704 allAppsCellHeightPx = getCellSize().y;
705 }
706 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700707 updateAllAppsWidth();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700708
Jon Miranda941375e2021-03-31 13:10:45 -0400709 if (isVerticalLayout) {
710 hideWorkspaceLabelsIfNotEnoughSpace();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700711 }
Winson Chungb3800242013-10-24 11:01:54 -0700712
Winson Chungb3800242013-10-24 11:01:54 -0700713 // Hotseat
Jon Miranda0d284852021-06-22 14:50:55 -0700714 updateHotseatIconSize(iconSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700715
Sunny Goyal07b69292018-01-08 14:19:34 -0800716 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700717 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700718 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700719 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
720 workspaceSpringLoadShrinkFactor = Math.min(
721 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
722 1 - (minRequiredHeight / expectedWorkspaceHeight));
723 } else {
724 workspaceSpringLoadShrinkFactor =
725 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
726 }
727
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700728 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700729 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
730 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700731 }
732
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700733 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700734 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700735
Jon Mirandae126d722021-02-25 10:45:20 -0500736 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
737
Jon Mirandac1b23992016-12-13 08:57:36 -0800738 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700739 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800740 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800741
742 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500743 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Thales Lima78d00ad2021-09-30 11:29:06 +0100744 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacePx.y);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800745 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500746 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800747 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800748
749 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500750 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Thales Lima78d00ad2021-09-30 11:29:06 +0100751 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacePx.x);
Jon Mirandae126d722021-02-25 10:45:20 -0500752 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
753 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800754 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800755
756 float scale = Math.min(scaleX, scaleY);
757 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700758 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700759 }
760 }
761
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700762 private void updateFolderCellSize(float scale, Resources res) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100763 float invIconSizeDp = isVerticalBarLayout()
764 ? inv.iconSize[InvariantDeviceProfile.INDEX_LANDSCAPE]
765 : inv.iconSize[InvariantDeviceProfile.INDEX_DEFAULT];
Sunny Goyal35c7b192021-04-20 16:51:10 -0700766 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Thales Lima83bedbf2021-10-05 17:47:39 +0100767 folderChildTextSizePx =
768 pxFromSp(inv.iconTextSize[InvariantDeviceProfile.INDEX_DEFAULT], mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500769 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700770
771 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700772
Jon Miranda823da222021-03-23 08:08:45 -0400773 if (isScalableGrid) {
Jon Mirandaf33f5b32021-07-22 17:15:31 -0700774 int minWidth = folderChildIconSizePx + iconDrawablePaddingPx * 2;
775 int minHeight = folderChildIconSizePx + iconDrawablePaddingPx * 2 + textHeight;
776
777 folderCellWidthPx = (int) Math.max(minWidth, cellWidthPx * scale);
778 folderCellHeightPx = (int) Math.max(minHeight, cellHeightPx * scale);
Jon Miranda823da222021-03-23 08:08:45 -0400779
Thales Lima78d00ad2021-09-30 11:29:06 +0100780 int scaledSpace = (int) (folderCellLayoutBorderSpaceOriginalPx * scale);
781 folderCellLayoutBorderSpacePx = new Point(scaledSpace, scaledSpace);
782 folderContentPaddingLeftRight = scaledSpace;
783 folderContentPaddingTop = scaledSpace;
Jon Miranda823da222021-03-23 08:08:45 -0400784 } else {
785 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
786 * scale);
787 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
788 * scale);
789
790 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
791 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
792 }
793
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000794 folderChildDrawablePaddingPx = Math.max(0,
795 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700796 }
797
Winson1f064272016-07-18 17:18:02 -0700798 public void updateInsets(Rect insets) {
799 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800800 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700801 }
802
Sunny Goyalae6e3182019-04-30 12:04:37 -0700803 /**
804 * The current device insets. This is generally same as the insets being dispatched to
805 * {@link Insettable} elements, but can differ if the element is using a different profile.
806 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800807 public Rect getInsets() {
808 return mInsets;
809 }
810
Sunny Goyal756cd262015-08-20 12:33:21 -0700811 public Point getCellSize() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700812 return getCellSize(null);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700813 }
814
Sunny Goyal19ff7282021-04-22 10:12:54 -0700815 public Point getCellSize(Point result) {
816 if (result == null) {
817 result = new Point();
818 }
Thales Limad1df5fc2021-09-03 18:37:19 +0100819
Sunny Goyal756cd262015-08-20 12:33:21 -0700820 // Since we are only concerned with the overall padding, layout direction does
821 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700822 Point padding = getTotalWorkspacePadding();
Thales Limad1df5fc2021-09-03 18:37:19 +0100823
824 int numColumns = isTwoPanels ? inv.numColumns * 2 : inv.numColumns;
Thales Lima7a6752d2021-09-23 14:31:00 +0100825 int cellLayoutTotalPadding =
826 isTwoPanels ? 4 * cellLayoutPaddingLeftRightPx : 2 * cellLayoutPaddingLeftRightPx;
827 int screenWidthPx = availableWidthPx - padding.x - cellLayoutTotalPadding;
Thales Lima78d00ad2021-09-30 11:29:06 +0100828 result.x = calculateCellWidth(screenWidthPx, cellLayoutBorderSpacePx.x, numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700829 result.y = calculateCellHeight(availableHeightPx - padding.y
Thales Lima78d00ad2021-09-30 11:29:06 +0100830 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacePx.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700831 return result;
832 }
833
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700834 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800835 updateWorkspacePadding();
836 return new Point(workspacePadding.left + workspacePadding.right,
837 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700838 }
839
840 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800841 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
842 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700843 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800844 private void updateWorkspacePadding() {
845 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700846 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800847 padding.top = 0;
848 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800849 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700850 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700851 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700852 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700853 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700854 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700855 }
Winson Chungb3800242013-10-24 11:01:54 -0700856 } else {
Thales Lima7a6752d2021-09-23 14:31:00 +0100857 // Pad the bottom of the workspace with search/hotseat bar sizes
Tony Wickham21970cc2021-09-15 15:44:05 -0700858 int hotseatTop = hotseatBarSizePx;
Tony Wickhama29a0462021-03-02 12:28:25 -0800859 int paddingBottom = hotseatTop + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500860 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100861
Thales Limad90faab2021-09-21 17:18:47 +0100862 padding.set(desiredWorkspaceHorizontalMarginPx,
Thales Lima83bedbf2021-10-05 17:47:39 +0100863 workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx),
Thales Limad90faab2021-09-21 17:18:47 +0100864 desiredWorkspaceHorizontalMarginPx,
Thales Lima7a6752d2021-09-23 14:31:00 +0100865 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700866 }
Winson Chungb3800242013-10-24 11:01:54 -0700867 }
868
Sunny Goyal57b22792021-05-25 14:35:01 -0700869 /**
870 * Returns the padding for hotseat view
871 */
872 public Rect getHotseatLayoutPadding(Context context) {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700873 if (isVerticalBarLayout()) {
874 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700875 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
876 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700877 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700878 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
879 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700880 }
Sunny Goyal57b22792021-05-25 14:35:01 -0700881 } else if (isTaskbarPresent) {
Tony Wickham21970cc2021-09-15 15:44:05 -0700882 int hotseatHeight = workspacePadding.bottom;
Sunny Goyal57b22792021-05-25 14:35:01 -0700883 int taskbarOffset = getTaskbarOffsetY();
Tony Wickham21970cc2021-09-15 15:44:05 -0700884 int hotseatTopDiff = hotseatHeight - taskbarOffset;
Sunny Goyal57b22792021-05-25 14:35:01 -0700885
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700886 int endOffset = ApiWrapper.getHotseatEndOffset(context);
Sunny Goyal57b22792021-05-25 14:35:01 -0700887 int requiredWidth = iconSizePx * numShownHotseatIcons;
888
889 Resources res = context.getResources();
890 float taskbarIconSize = res.getDimension(R.dimen.taskbar_icon_size);
891 float taskbarIconSpacing = 2 * res.getDimension(R.dimen.taskbar_icon_spacing);
892 int maxSize = (int) (requiredWidth
893 * (taskbarIconSize + taskbarIconSpacing) / taskbarIconSize);
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700894 int hotseatSize = Math.min(maxSize, availableWidthPx - endOffset);
Sunny Goyal57b22792021-05-25 14:35:01 -0700895 int sideSpacing = (availableWidthPx - hotseatSize) / 2;
896 mHotseatPadding.set(sideSpacing, hotseatTopDiff, sideSpacing, taskbarOffset);
897
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700898 if (endOffset > sideSpacing) {
Sunny Goyal57b22792021-05-25 14:35:01 -0700899 int diff = Utilities.isRtl(context.getResources())
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700900 ? sideSpacing - endOffset
901 : endOffset - sideSpacing;
902 mHotseatPadding.left -= diff;
903 mHotseatPadding.right += diff;
Sunny Goyal57b22792021-05-25 14:35:01 -0700904 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700905 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700906 // We want the edges of the hotseat to line up with the edges of the workspace, but the
907 // icons in the hotseat are a different size, and so don't line up perfectly. To account
908 // for this, we pad the left and right of the hotseat with half of the difference of a
909 // workspace cell vs a hotseat cell.
910 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700911 float hotseatCellWidth = (float) widthPx / numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700912 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
913 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700914 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
915 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700916 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700917 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
918 + mInsets.right,
Jon Miranda0d284852021-06-22 14:50:55 -0700919 hotseatBarSizePx - hotseatCellHeightPx - hotseatBarTopPaddingPx
920 + cellLayoutBottomPaddingPx + mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700921 }
922 return mHotseatPadding;
923 }
924
Winsonfadbe8f2016-07-22 16:35:37 -0700925 /**
Sunny Goyal57b22792021-05-25 14:35:01 -0700926 * Returns the number of pixels the QSB is translated from the bottom of the screen.
927 */
928 public int getQsbOffsetY() {
929 int freeSpace = isTaskbarPresent
930 ? workspacePadding.bottom
931 : hotseatBarSizePx - hotseatCellHeightPx - hotseatQsbHeight;
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000932
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000933 if (isScalableGrid && qsbBottomMarginPx > mInsets.bottom) {
Tony Wickham21970cc2021-09-15 15:44:05 -0700934 // Note that taskbarSize = 0 unless isTaskbarPresent.
935 return Math.min(qsbBottomMarginPx + taskbarSize, freeSpace);
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000936 } else {
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000937 return (int) (freeSpace * QSB_CENTER_FACTOR)
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100938 + (isTaskbarPresent ? taskbarSize : mInsets.bottom);
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000939 }
Sunny Goyal57b22792021-05-25 14:35:01 -0700940 }
941
942 /**
943 * Returns the number of pixels the taskbar is translated from the bottom of the screen.
944 */
945 public int getTaskbarOffsetY() {
946 return (getQsbOffsetY() - taskbarSize) / 2;
947 }
948
949 /**
Winsonfadbe8f2016-07-22 16:35:37 -0700950 * @return the bounds for which the open folders should be contained within
951 */
952 public Rect getAbsoluteOpenFolderBounds() {
953 if (isVerticalBarLayout()) {
954 // Folders should only appear right of the drop target bar and left of the hotseat
955 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
956 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700957 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700958 mInsets.top + availableHeightPx);
959 } else {
960 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -0800961 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800962 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700963 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800964 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -0800965 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -0700966 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700967 }
968 }
969
Jon Miranda228877d2021-02-09 11:05:00 -0500970 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
971 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700972 }
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100973
Jon Miranda228877d2021-02-09 11:05:00 -0500974 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
975 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700976 }
977
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700978 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700979 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
980 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
981 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700982 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700983 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700984 return isLandscape && transposeLayoutWithOrientation;
985 }
986
Sunny Goyal59d086c2018-05-07 17:31:40 -0700987 /**
988 * Updates orientation information and returns true if it has changed from the previous value.
989 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700990 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700991 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700992 boolean isSeascape = DisplayController.INSTANCE.get(context)
993 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700994 if (mIsSeascape != isSeascape) {
995 mIsSeascape = isSeascape;
996 return true;
997 }
998 }
999 return false;
1000 }
1001
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001002 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001003 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001004 }
1005
Sunny Goyal07b69292018-01-08 14:19:34 -08001006 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001007 return isVerticalBarLayout();
Winson Chungb3800242013-10-24 11:01:54 -07001008 }
1009
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001010 public int getCellContentHeight(@ContainerType int containerType) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001011 switch (containerType) {
1012 case CellLayout.WORKSPACE:
1013 return cellHeightPx;
1014 case CellLayout.FOLDER:
1015 return folderCellHeightPx;
1016 case CellLayout.HOTSEAT:
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001017 // The hotseat is the only container where the cell height is going to be
1018 // different from the content within that cell.
1019 return iconSizePx;
Sunny Goyalc13403c2016-11-18 23:44:48 -08001020 default:
1021 // ??
1022 return 0;
1023 }
1024 }
Sunny Goyal13178ac2016-04-04 16:35:22 -07001025
Jon Miranda58561d42021-03-17 10:51:54 -04001026 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001027 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -04001028 }
1029
1030 public void dump(String prefix, PrintWriter writer) {
1031 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -07001032 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -04001033
Alex Chaua02eddc2021-04-29 00:36:06 +01001034 writer.println(prefix + "\tallowRotation:" + allowRotation);
Jon Miranda58561d42021-03-17 10:51:54 -04001035 writer.println(prefix + "\tisTablet:" + isTablet);
Jon Miranda58561d42021-03-17 10:51:54 -04001036 writer.println(prefix + "\tisPhone:" + isPhone);
1037 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
1038 + transposeLayoutWithOrientation);
1039
1040 writer.println(prefix + "\tisLandscape:" + isLandscape);
1041 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
Sunny Goyal19ff7282021-04-22 10:12:54 -07001042 writer.println(prefix + "\tisTwoPanels:" + isTwoPanels);
Jon Miranda58561d42021-03-17 10:51:54 -04001043
1044 writer.println(prefix + pxToDpStr("windowX", windowX));
1045 writer.println(prefix + pxToDpStr("windowY", windowY));
1046 writer.println(prefix + pxToDpStr("widthPx", widthPx));
1047 writer.println(prefix + pxToDpStr("heightPx", heightPx));
1048
1049 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
1050 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
1051
1052 writer.println(prefix + "\taspectRatio:" + aspectRatio);
1053
1054 writer.println(prefix + "\tisScalableGrid:" + isScalableGrid);
1055
Thales Lima83bedbf2021-10-05 17:47:39 +01001056 writer.println(prefix + "\tinv.numColumns: " + inv.numColumns);
1057 writer.println(prefix + "\tinv.numRows: " + inv.numRows);
Jon Miranda58561d42021-03-17 10:51:54 -04001058
Thales Lima83bedbf2021-10-05 17:47:39 +01001059 writer.println(prefix + "\tminCellSize: " + inv.minCellSize[mTypeIndex] + "dp");
Jon Mirandaab3c6812021-06-28 15:42:28 -07001060
Jon Miranda58561d42021-03-17 10:51:54 -04001061 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
1062 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
1063
1064 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
1065 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
1066
Thales Lima83bedbf2021-10-05 17:47:39 +01001067 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Horizontal",
1068 cellLayoutBorderSpacePx.x));
1069 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Vertical",
1070 cellLayoutBorderSpacePx.y));
1071
Jon Miranda58561d42021-03-17 10:51:54 -04001072 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
1073 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
1074 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
1075
1076 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
1077 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
1078 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
1079 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
1080 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
1081 folderChildDrawablePaddingPx));
Thales Lima78d00ad2021-09-30 11:29:06 +01001082 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpaceOriginalPx",
1083 folderCellLayoutBorderSpaceOriginalPx));
1084 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Horizontal",
1085 folderCellLayoutBorderSpacePx.x));
1086 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Vertical",
1087 folderCellLayoutBorderSpacePx.y));
Jon Miranda58561d42021-03-17 10:51:54 -04001088
Jon Miranda58561d42021-03-17 10:51:54 -04001089 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
1090 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
1091 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
1092 allAppsIconDrawablePaddingPx));
1093 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001094 writer.println(prefix + "\tnumShownAllAppsColumns: " + numShownAllAppsColumns);
Jon Miranda58561d42021-03-17 10:51:54 -04001095
1096 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
1097 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
1098 writer.println(prefix + pxToDpStr("hotseatBarTopPaddingPx", hotseatBarTopPaddingPx));
1099 writer.println(prefix + pxToDpStr("hotseatBarBottomPaddingPx", hotseatBarBottomPaddingPx));
1100 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
1101 hotseatBarSidePaddingStartPx));
1102 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
1103 hotseatBarSidePaddingEndPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001104 writer.println(prefix + "\tnumShownHotseatIcons: " + numShownHotseatIcons);
Jon Miranda58561d42021-03-17 10:51:54 -04001105
1106 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
Tony Wickham635e1802021-07-22 14:28:04 -10001107 writer.println(prefix + "\tisTaskbarPresentInApps:" + isTaskbarPresentInApps);
Jon Miranda58561d42021-03-17 10:51:54 -04001108 writer.println(prefix + pxToDpStr("taskbarSize", taskbarSize));
Jon Miranda58561d42021-03-17 10:51:54 -04001109
Thales Lima83bedbf2021-10-05 17:47:39 +01001110 writer.println(prefix + pxToDpStr("desiredWorkspaceHorizontalMarginPx",
1111 desiredWorkspaceHorizontalMarginPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001112 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
1113 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
1114 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
1115 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
1116
Jon Mirandaab3c6812021-06-28 15:42:28 -07001117 writer.println(prefix + pxToDpStr("iconScale", iconScale));
1118 writer.println(prefix + pxToDpStr("cellScaleToFit ", cellScaleToFit));
Jon Miranda58561d42021-03-17 10:51:54 -04001119 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Thales Lima7a6752d2021-09-23 14:31:00 +01001120 writer.println(prefix + pxToDpStr("unscaled extraSpace", extraSpace / iconScale));
Jon Mirandac9e69fa2021-03-22 17:13:34 -04001121
1122 if (inv.devicePaddings != null) {
1123 int unscaledExtraSpace = (int) (extraSpace / iconScale);
1124 writer.println(prefix + pxToDpStr("maxEmptySpace",
1125 inv.devicePaddings.getDevicePadding(unscaledExtraSpace).getMaxEmptySpacePx()));
1126 }
Jon Miranda58561d42021-03-17 10:51:54 -04001127 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
1128 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
1129 writer.println(prefix + pxToDpStr("extraHotseatBottomPadding", extraHotseatBottomPadding));
1130 }
1131
Sunny Goyalfd58da62020-08-11 12:06:49 -07001132 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -07001133 Configuration config = new Configuration(c.getResources().getConfiguration());
1134 config.orientation = orientation;
Sunny Goyal35c7b192021-04-20 16:51:10 -07001135 config.densityDpi = info.densityDpi;
Sunny Goyal1890f672020-04-30 12:28:00 -07001136 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -07001137 }
Sunny Goyalfde55052018-02-01 14:46:13 -08001138
1139 /**
1140 * Callback when a component changes the DeviceProfile associated with it, as a result of
1141 * configuration change
1142 */
1143 public interface OnDeviceProfileChangeListener {
1144
1145 /**
1146 * Called when the device profile is reassigned. Note that for layout and measurements, it
1147 * is sufficient to listen for inset changes. Use this callback when you need to perform
1148 * a one time operation.
1149 */
1150 void onDeviceProfileChanged(DeviceProfile dp);
1151 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001152
1153 public static class Builder {
1154 private Context mContext;
1155 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -07001156 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001157
Sunny Goyal19ff7282021-04-22 10:12:54 -07001158 private WindowBounds mWindowBounds;
1159 private boolean mUseTwoPanels;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001160
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001161 private boolean mIsMultiWindowMode = false;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001162 private Boolean mTransposeLayoutWithOrientation;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001163
Sunny Goyalfd58da62020-08-11 12:06:49 -07001164 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001165 mContext = context;
1166 mInv = inv;
1167 mInfo = info;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001168 }
1169
1170 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
1171 mIsMultiWindowMode = isMultiWindowMode;
1172 return this;
1173 }
1174
Sunny Goyal19ff7282021-04-22 10:12:54 -07001175 public Builder setUseTwoPanels(boolean useTwoPanels) {
1176 mUseTwoPanels = useTwoPanels;
1177 return this;
1178 }
1179
1180
1181 public Builder setWindowBounds(WindowBounds bounds) {
1182 mWindowBounds = bounds;
Sunny Goyal0addbf02020-04-28 14:17:35 -07001183 return this;
1184 }
1185
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001186 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
1187 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
1188 return this;
1189 }
1190
1191 public DeviceProfile build() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001192 if (mWindowBounds == null) {
1193 throw new IllegalArgumentException("Window bounds not set");
1194 }
1195 if (mTransposeLayoutWithOrientation == null) {
1196 mTransposeLayoutWithOrientation = !mInfo.isTablet(mWindowBounds);
1197 }
1198 return new DeviceProfile(mContext, mInv, mInfo, mWindowBounds,
1199 mIsMultiWindowMode, mTransposeLayoutWithOrientation, mUseTwoPanels);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001200 }
1201 }
1202
Winson Chungb3800242013-10-24 11:01:54 -07001203}