blob: 0b60b322755c1433c5b6a80767213310eecd19bf [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;
Zak Cohen334efeb2021-03-19 16:42:07 -0700196
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800197 // Widgets
198 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
199
Sunny Goyal47328fd2016-05-25 18:56:41 -0700200 // Drop Target
201 public int dropTargetBarSizePx;
Alex Chaua02eddc2021-04-29 00:36:06 +0100202 public int dropTargetDragPaddingPx;
203 public int dropTargetTextSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700204
Winson1f064272016-07-18 17:18:02 -0700205 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800206 private final Rect mInsets = new Rect();
207 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700208 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800209 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700210 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700211
Tony Wickhamf34bee82018-12-03 18:11:39 -0800212 // Notification dots
Samuel Fufac96fa242019-09-26 23:06:32 -0700213 public DotRenderer mDotRendererWorkSpace;
214 public DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800215
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100216 // Taskbar
Tony Wickham15883892021-02-12 11:24:40 -0800217 public boolean isTaskbarPresent;
Tony Wickham635e1802021-07-22 14:28:04 -1000218 // Whether Taskbar will inset the bottom of apps by taskbarSize.
219 public boolean isTaskbarPresentInApps;
Tony Wickham15883892021-02-12 11:24:40 -0800220 public int taskbarSize;
221
Alex Chaua02eddc2021-04-29 00:36:06 +0100222 // DragController
223 public int flingToDeleteThresholdVelocity;
224
Vinit Nayak17c4b332021-08-05 12:54:58 -0700225 /** TODO: Once we fully migrate to staged split, remove "isMultiWindowMode" */
Sunny Goyal19ff7282021-04-22 10:12:54 -0700226 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info, WindowBounds windowBounds,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700227 boolean isMultiWindowMode, boolean transposeLayoutWithOrientation,
Sunny Goyal19ff7282021-04-22 10:12:54 -0700228 boolean useTwoPanels) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700229
Adam Cohen2e6da152015-05-06 11:42:25 -0700230 this.inv = inv;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700231 this.isLandscape = windowBounds.isLandscape();
Sunny Goyald70e75a2018-02-22 10:07:32 -0800232 this.isMultiWindowMode = isMultiWindowMode;
Jon Mirandae126d722021-02-25 10:45:20 -0500233 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700234 windowX = windowBounds.bounds.left;
235 windowY = windowBounds.bounds.top;
Winson Chungb3800242013-10-24 11:01:54 -0700236
Jon Mirandae126d722021-02-25 10:45:20 -0500237 isScalableGrid = inv.isScalable && !isVerticalBarLayout() && !isMultiWindowMode;
238
Jon Miranda7e183c32018-06-20 11:05:27 -0700239 // Determine sizes.
Sunny Goyal19ff7282021-04-22 10:12:54 -0700240 widthPx = windowBounds.bounds.width();
241 heightPx = windowBounds.bounds.height();
242 availableWidthPx = windowBounds.availableSize.x;
Tony Wickham21970cc2021-09-15 15:44:05 -0700243 availableHeightPx = windowBounds.availableSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700244
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700245 mInfo = info;
Alex Chaua02eddc2021-04-29 00:36:06 +0100246 // If the device's pixel density was scaled (usually via settings for A11y), use the
247 // original dimensions to determine if rotation is allowed of not.
248 float originalSmallestWidth = dpiFromPx(Math.min(widthPx, heightPx), DENSITY_DEVICE_STABLE);
249 allowRotation = originalSmallestWidth >= MIN_TABLET_WIDTH;
250 // Tablet UI does not support emulated landscape.
251 isTablet = allowRotation && info.isTablet(windowBounds);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700252 isPhone = !isTablet;
Alex Chaub1c36ce2021-08-25 11:54:08 +0100253 isTwoPanels = isTablet && useTwoPanels
254 && (isLandscape || FeatureFlags.ENABLE_TWO_PANEL_HOME_IN_PORTRAIT.get());
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);
Zak Cohen334efeb2021-03-19 16:42:07 -0700402
Jon Miranda2ed276e2017-06-29 11:36:34 -0700403 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400404 extraSpace = updateAvailableDimensions(res);
Jon Miranda80cddbc2021-07-22 13:51:16 -0700405
Jon Miranda2ed276e2017-06-29 11:36:34 -0700406 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400407 if (isScalableGrid && inv.devicePaddings != null) {
408 // Paddings were created assuming no scaling, so we first unscale the extra space.
Jon Mirandaab3c6812021-06-28 15:42:28 -0700409 int unscaledExtraSpace = (int) (extraSpace / cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400410 DevicePadding padding = inv.devicePaddings.getDevicePadding(unscaledExtraSpace);
Jon Miranda228877d2021-02-09 11:05:00 -0500411
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400412 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
413 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
414 int paddingHotseatBottom = padding.getHotseatBottomPadding(unscaledExtraSpace);
415
Jon Mirandaab3c6812021-06-28 15:42:28 -0700416 workspaceTopPadding = Math.round(paddingWorkspaceTop * cellScaleToFit);
417 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * cellScaleToFit);
418 extraHotseatBottomPadding = Math.round(paddingHotseatBottom * cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400419
Jon Miranda58561d42021-03-17 10:51:54 -0400420 hotseatBarSizePx += extraHotseatBottomPadding;
Jon Miranda8bdb2222021-06-23 18:10:10 -0700421
Jon Mirandaab3c6812021-06-28 15:42:28 -0700422 qsbBottomMarginPx = Math.round(qsbBottomMarginOriginalPx * cellScaleToFit);
Jon Miranda228877d2021-02-09 11:05:00 -0500423 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700424 // We increase the hotseat size when there is extra space.
Jon Miranda80cddbc2021-07-22 13:51:16 -0700425
Jon Miranda80dda302021-07-28 14:14:59 -0700426 if (Float.compare(aspectRatio, TALLER_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0
427 && extraSpace >= Utilities.dpToPx(TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP)) {
428 // For taller devices, we will take a piece of the extra space from each row,
Jon Miranda80cddbc2021-07-22 13:51:16 -0700429 // and add it to the space above and below the hotseat.
Jon Miranda80dda302021-07-28 14:14:59 -0700430
431 // For devices with more extra space, we take a larger piece from each cell.
432 int piece = extraSpace < Utilities.dpToPx(TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP)
Jon Mirandaa56cb842021-08-03 17:26:44 -0700433 ? 7 : 5;
Jon Miranda80dda302021-07-28 14:14:59 -0700434
Jon Miranda80cddbc2021-07-22 13:51:16 -0700435 int extraSpace = ((getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2)
Jon Miranda80dda302021-07-28 14:14:59 -0700436 * inv.numRows) / piece;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700437
Jon Mirandaa56cb842021-08-03 17:26:44 -0700438 workspaceTopPadding = extraSpace / 8;
439 int halfLeftOver = (extraSpace - workspaceTopPadding) / 2;
440 hotseatBarTopPaddingPx += halfLeftOver;
441 hotseatBarSizeExtraSpacePx = halfLeftOver;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700442 } else {
443 // ie. For a display with a large aspect ratio, we can keep the icons on the
444 // workspace in portrait mode closer together by adding more height to the hotseat.
445 // Note: This calculation was created after noticing a pattern in the design spec.
446 hotseatBarSizeExtraSpacePx = getCellSize().y - iconSizePx
447 - iconDrawablePaddingPx * 2 - workspacePageIndicatorHeight;
448 }
449
450 updateHotseatIconSize(iconSizePx);
Jon Miranda30d0aa22017-07-25 15:55:29 -0700451
Jon Miranda3f9bab22017-07-28 14:50:17 -0700452 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700453 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700454 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800455 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800456
Alex Chaua02eddc2021-04-29 00:36:06 +0100457 flingToDeleteThresholdVelocity = res.getDimensionPixelSize(
458 R.dimen.drag_flingToDeleteMinVelocity);
459
Tony Wickham1237df02017-02-24 08:59:36 -0800460 // This is done last, after iconSizePx is calculated above.
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700461 Path dotPath = GraphicsUtils.getShapePath(DEFAULT_DOT_SIZE);
462 mDotRendererWorkSpace = new DotRenderer(iconSizePx, dotPath, DEFAULT_DOT_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700463 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700464 new DotRenderer(allAppsIconSizePx, dotPath, DEFAULT_DOT_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700465 }
466
Thales Limad90faab2021-09-21 17:18:47 +0100467 private int getHorizontalMarginPx(InvariantDeviceProfile idp, Resources res) {
468 if (isVerticalBarLayout()) {
469 return 0;
470 }
471
Thales Lima83bedbf2021-10-05 17:47:39 +0100472 return isScalableGrid
473 ? pxFromDp(idp.horizontalMargin[mTypeIndex], mMetrics)
474 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
Thales Limad90faab2021-09-21 17:18:47 +0100475 }
476
Jon Miranda0d284852021-06-22 14:50:55 -0700477 private void updateHotseatIconSize(int hotseatIconSizePx) {
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700478 // Ensure there is enough space for folder icons, which have a slightly larger radius.
479 hotseatCellHeightPx = (int) Math.ceil(hotseatIconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda0d284852021-06-22 14:50:55 -0700480 if (isVerticalBarLayout()) {
481 hotseatBarSizePx = hotseatIconSizePx + hotseatBarSidePaddingStartPx
482 + hotseatBarSidePaddingEndPx;
483 } else {
484 hotseatBarSizePx = hotseatIconSizePx + hotseatBarTopPaddingPx
Jon Miranda80cddbc2021-07-22 13:51:16 -0700485 + hotseatBarBottomPaddingPx + (isScalableGrid ? 0 : hotseatExtraVerticalSize)
486 + hotseatBarSizeExtraSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700487 }
488 }
489
Thales Lima78d00ad2021-09-30 11:29:06 +0100490 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp) {
491 if (!isScalableGrid) {
492 return new Point(0, 0);
493 }
494
Thales Lima83bedbf2021-10-05 17:47:39 +0100495 int horizontalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].x, mMetrics);
496 int verticalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].y, mMetrics);
Thales Lima78d00ad2021-09-30 11:29:06 +0100497
498 return new Point(horizontalSpacePx, verticalSpacePx);
499 }
500
501 private Point getCellLayoutBorderSpaceScaled(InvariantDeviceProfile idp, float scale) {
502 Point original = getCellLayoutBorderSpace(idp);
503 return new Point((int) (original.x * scale), (int) (original.y * scale));
Jon Mirandae126d722021-02-25 10:45:20 -0500504 }
505
Sunny Goyal69a8eec2021-07-22 10:02:50 -0700506 public Info getDisplayInfo() {
507 return mInfo;
508 }
509
Jon Miranda611dba42021-03-02 14:12:13 -0500510 /**
511 * We inset the widget padding added by the system and instead rely on the border spacing
512 * between cells to create reliable consistency between widgets
513 */
514 public boolean shouldInsetWidgets() {
515 Rect widgetPadding = inv.defaultWidgetPadding;
516
Jon Miranda49811182021-06-03 09:52:40 -0700517 // Check all sides to ensure that the widget won't overlap into another cell, or into
518 // status bar.
519 return workspaceTopPadding > widgetPadding.top
Thales Lima78d00ad2021-09-30 11:29:06 +0100520 && cellLayoutBorderSpacePx.x > widgetPadding.left
521 && cellLayoutBorderSpacePx.y > widgetPadding.top
522 && cellLayoutBorderSpacePx.x > widgetPadding.right
523 && cellLayoutBorderSpacePx.y > widgetPadding.bottom;
Jon Miranda611dba42021-03-02 14:12:13 -0500524 }
Jon Mirandae126d722021-02-25 10:45:20 -0500525
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700526 public Builder toBuilder(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700527 WindowBounds bounds =
528 new WindowBounds(widthPx, heightPx, availableWidthPx, availableHeightPx);
529 bounds.bounds.offsetTo(windowX, windowY);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700530 return new Builder(context, inv, mInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700531 .setWindowBounds(bounds)
532 .setUseTwoPanels(isTwoPanels)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700533 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800534 }
535
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700536 public DeviceProfile copy(Context context) {
537 return toBuilder(context).build();
538 }
539
540 /**
541 * TODO: Move this to the builder as part of setMultiWindowMode
542 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700543 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700544 DeviceProfile profile = toBuilder(context)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700545 .setWindowBounds(windowBounds)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700546 .setMultiWindowMode(true)
547 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800548
Jon Miranda941375e2021-03-31 13:10:45 -0400549 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda93e1f042016-11-11 14:13:04 -0800550
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800551 // We use these scales to measure and layout the widgets using their full invariant profile
552 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
553 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
554 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
555 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800556 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800557
Jon Miranda93e1f042016-11-11 14:13:04 -0800558 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700559 }
560
Adam Cohen63f1ec02014-08-12 09:23:13 -0700561 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400562 * Checks if there is enough space for labels on the workspace.
563 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700564 * It is important to call this method after the All Apps variables have been set.
565 */
Jon Miranda941375e2021-03-31 13:10:45 -0400566 private void hideWorkspaceLabelsIfNotEnoughSpace() {
567 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
568 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
569 - iconTextHeight;
570
571 // We want enough space so that the text is closer to its corresponding icon.
572 if (workspaceCellPaddingY < iconTextHeight) {
573 iconTextSizePx = 0;
574 iconDrawablePaddingPx = 0;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700575 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda941375e2021-03-31 13:10:45 -0400576 autoResizeAllAppsCells();
577 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700578 }
Jon Miranda1091e532017-07-21 13:31:50 -0700579
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700580 /**
581 * Re-computes the all-apps cell size to be independent of workspace
582 */
583 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400584 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
585 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700586 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400587 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700588 }
589
sfufa@google.comde013292021-09-21 18:22:44 -0700590 private void updateAllAppsWidth() {
591 if (isTwoPanels) {
592 int usedWidth = (allAppsCellWidthPx * numShownAllAppsColumns)
Thales Lima83bedbf2021-10-05 17:47:39 +0100593 + (allAppsCellSpacePx.x * (numShownAllAppsColumns + 1));
sfufa@google.comde013292021-09-21 18:22:44 -0700594 allAppsLeftRightPadding = Math.max(1, (availableWidthPx - usedWidth) / 2);
595 } else {
596 allAppsLeftRightPadding =
Thales Limad90faab2021-09-21 17:18:47 +0100597 desiredWorkspaceHorizontalMarginPx + cellLayoutPaddingLeftRightPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700598 }
599 }
600
Jon Miranda228877d2021-02-09 11:05:00 -0500601 /**
602 * Returns the amount of extra (or unused) vertical space.
603 */
604 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700605 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800606
Jon Mirandae126d722021-02-25 10:45:20 -0500607 Point workspacePadding = getTotalWorkspacePadding();
608
609 // Check to see if the icons fit within the available height.
610 float usedHeight = getCellLayoutHeight();
611 final int maxHeight = availableHeightPx - workspacePadding.y;
Jon Miranda228877d2021-02-09 11:05:00 -0500612 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500613 float scaleY = maxHeight / usedHeight;
614 boolean shouldScale = scaleY < 1f;
615
616 float scaleX = 1f;
617 if (isScalableGrid) {
618 // We scale to fit the cellWidth and cellHeight in the available space.
619 // The benefit of scalable grids is that we can get consistent aspect ratios between
620 // devices.
Thales Limad1df5fc2021-09-03 18:37:19 +0100621 int numColumns = isTwoPanels ? inv.numColumns * 2 : inv.numColumns;
622 float usedWidth = (cellWidthPx * numColumns)
Thales Lima78d00ad2021-09-30 11:29:06 +0100623 + (cellLayoutBorderSpacePx.x * (numColumns - 1))
Thales Limad90faab2021-09-21 17:18:47 +0100624 + (desiredWorkspaceHorizontalMarginPx * 2);
Jon Mirandae126d722021-02-25 10:45:20 -0500625 // We do not subtract padding here, as we also scale the workspace padding if needed.
626 scaleX = availableWidthPx / usedWidth;
627 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700628 }
Jon Mirandae126d722021-02-25 10:45:20 -0500629
630 if (shouldScale) {
631 float scale = Math.min(scaleX, scaleY);
632 updateIconSize(scale, res);
633 extraHeight = Math.max(0, maxHeight - getCellLayoutHeight());
634 }
635
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700636 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500637 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700638 }
639
Jon Mirandae126d722021-02-25 10:45:20 -0500640 private int getCellLayoutHeight() {
Thales Lima78d00ad2021-09-30 11:29:06 +0100641 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacePx.y * (inv.numRows - 1));
Jon Mirandae126d722021-02-25 10:45:20 -0500642 }
643
Jon Miranda6f7e9702019-09-16 14:44:14 -0700644 /**
645 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
646 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
647 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
648 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800649 public void updateIconSize(float scale, Resources res) {
Jon Mirandaab3c6812021-06-28 15:42:28 -0700650 // Icon scale should never exceed 1, otherwise pixellation may occur.
651 iconScale = Math.min(1f, scale);
652 cellScaleToFit = scale;
653
Jon Miranda18751b62017-07-31 17:25:27 -0700654 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800655 final boolean isVerticalLayout = isVerticalBarLayout();
Thales Lima83bedbf2021-10-05 17:47:39 +0100656 float invIconSizeDp = inv.iconSize[mTypeIndex];
657 float invIconTextSizeSp = inv.iconTextSize[mTypeIndex];
Andras Kloczl30fe9152021-08-05 18:02:29 +0200658
Jon Mirandaab3c6812021-06-28 15:42:28 -0700659 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, iconScale));
Jon Mirandaab3c6812021-06-28 15:42:28 -0700660 iconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * iconScale);
661 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * iconScale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700662
Thales Lima78d00ad2021-09-30 11:29:06 +0100663 cellLayoutBorderSpacePx = getCellLayoutBorderSpaceScaled(inv, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500664
665 if (isScalableGrid) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100666 cellWidthPx = pxFromDp(inv.minCellSize[mTypeIndex].x, mMetrics, scale);
667 cellHeightPx = pxFromDp(inv.minCellSize[mTypeIndex].y, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500668 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
669 + Utilities.calculateTextHeight(iconTextSizePx);
670 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
Thales Limad90faab2021-09-21 17:18:47 +0100671 desiredWorkspaceHorizontalMarginPx =
672 (int) (desiredWorkspaceHorizontalMarginOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500673 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500674 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700675 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR)
676 + iconDrawablePaddingPx
Jon Miranda228877d2021-02-09 11:05:00 -0500677 + Utilities.calculateTextHeight(iconTextSizePx);
678 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
679 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
680 && !isMultiWindowMode) {
681 // Ensures that the label is closer to its corresponding icon. This is not an issue
682 // with vertical bar layout or multi-window mode since the issue is handled
683 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
684 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
685 iconDrawablePaddingPx = cellPaddingY;
686 }
Jon Miranda846455e2017-10-02 14:58:52 -0700687 }
Jon Miranda18751b62017-07-31 17:25:27 -0700688
Sunny Goyalae190ff2020-04-14 00:19:01 +0000689 // All apps
Sunny Goyal19ff7282021-04-22 10:12:54 -0700690 if (numShownAllAppsColumns != inv.numColumns) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100691 allAppsIconSizePx =
692 pxFromDp(inv.iconSize[InvariantDeviceProfile.INDEX_ALL_APPS], mMetrics);
693 allAppsIconTextSizePx =
694 pxFromSp(inv.iconTextSize[InvariantDeviceProfile.INDEX_ALL_APPS], mMetrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000695 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda941375e2021-03-31 13:10:45 -0400696 autoResizeAllAppsCells();
Sunny Goyalae190ff2020-04-14 00:19:01 +0000697 } else {
698 allAppsIconSizePx = iconSizePx;
699 allAppsIconTextSizePx = iconTextSizePx;
700 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
701 allAppsCellHeightPx = getCellSize().y;
702 }
703 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700704 updateAllAppsWidth();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700705
Jon Miranda941375e2021-03-31 13:10:45 -0400706 if (isVerticalLayout) {
707 hideWorkspaceLabelsIfNotEnoughSpace();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700708 }
Winson Chungb3800242013-10-24 11:01:54 -0700709
Winson Chungb3800242013-10-24 11:01:54 -0700710 // Hotseat
Jon Miranda0d284852021-06-22 14:50:55 -0700711 updateHotseatIconSize(iconSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700712
Sunny Goyal07b69292018-01-08 14:19:34 -0800713 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700714 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700715 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700716 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
717 workspaceSpringLoadShrinkFactor = Math.min(
718 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
719 1 - (minRequiredHeight / expectedWorkspaceHeight));
720 } else {
721 workspaceSpringLoadShrinkFactor =
722 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
723 }
724
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700725 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700726 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
727 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700728 }
729
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700730 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700731 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700732
Jon Mirandae126d722021-02-25 10:45:20 -0500733 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
734
Jon Mirandac1b23992016-12-13 08:57:36 -0800735 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700736 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800737 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800738
739 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500740 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Thales Lima78d00ad2021-09-30 11:29:06 +0100741 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacePx.y);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800742 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500743 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800744 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800745
746 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500747 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Thales Lima78d00ad2021-09-30 11:29:06 +0100748 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacePx.x);
Jon Mirandae126d722021-02-25 10:45:20 -0500749 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
750 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800751 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800752
753 float scale = Math.min(scaleX, scaleY);
754 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700755 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700756 }
757 }
758
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700759 private void updateFolderCellSize(float scale, Resources res) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100760 float invIconSizeDp = isVerticalBarLayout()
761 ? inv.iconSize[InvariantDeviceProfile.INDEX_LANDSCAPE]
762 : inv.iconSize[InvariantDeviceProfile.INDEX_DEFAULT];
Sunny Goyal35c7b192021-04-20 16:51:10 -0700763 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Thales Lima83bedbf2021-10-05 17:47:39 +0100764 folderChildTextSizePx =
765 pxFromSp(inv.iconTextSize[InvariantDeviceProfile.INDEX_DEFAULT], mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500766 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700767
768 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700769
Jon Miranda823da222021-03-23 08:08:45 -0400770 if (isScalableGrid) {
Jon Mirandaf33f5b32021-07-22 17:15:31 -0700771 int minWidth = folderChildIconSizePx + iconDrawablePaddingPx * 2;
772 int minHeight = folderChildIconSizePx + iconDrawablePaddingPx * 2 + textHeight;
773
774 folderCellWidthPx = (int) Math.max(minWidth, cellWidthPx * scale);
775 folderCellHeightPx = (int) Math.max(minHeight, cellHeightPx * scale);
Jon Miranda823da222021-03-23 08:08:45 -0400776
Thales Lima78d00ad2021-09-30 11:29:06 +0100777 int scaledSpace = (int) (folderCellLayoutBorderSpaceOriginalPx * scale);
778 folderCellLayoutBorderSpacePx = new Point(scaledSpace, scaledSpace);
779 folderContentPaddingLeftRight = scaledSpace;
780 folderContentPaddingTop = scaledSpace;
Jon Miranda823da222021-03-23 08:08:45 -0400781 } else {
782 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
783 * scale);
784 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
785 * scale);
786
787 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
788 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
789 }
790
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000791 folderChildDrawablePaddingPx = Math.max(0,
792 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700793 }
794
Winson1f064272016-07-18 17:18:02 -0700795 public void updateInsets(Rect insets) {
796 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800797 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700798 }
799
Sunny Goyalae6e3182019-04-30 12:04:37 -0700800 /**
801 * The current device insets. This is generally same as the insets being dispatched to
802 * {@link Insettable} elements, but can differ if the element is using a different profile.
803 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800804 public Rect getInsets() {
805 return mInsets;
806 }
807
Sunny Goyal756cd262015-08-20 12:33:21 -0700808 public Point getCellSize() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700809 return getCellSize(null);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700810 }
811
Sunny Goyal19ff7282021-04-22 10:12:54 -0700812 public Point getCellSize(Point result) {
813 if (result == null) {
814 result = new Point();
815 }
Thales Limad1df5fc2021-09-03 18:37:19 +0100816
Sunny Goyal756cd262015-08-20 12:33:21 -0700817 // Since we are only concerned with the overall padding, layout direction does
818 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700819 Point padding = getTotalWorkspacePadding();
Thales Limad1df5fc2021-09-03 18:37:19 +0100820
821 int numColumns = isTwoPanels ? inv.numColumns * 2 : inv.numColumns;
Thales Lima7a6752d2021-09-23 14:31:00 +0100822 int cellLayoutTotalPadding =
823 isTwoPanels ? 4 * cellLayoutPaddingLeftRightPx : 2 * cellLayoutPaddingLeftRightPx;
824 int screenWidthPx = availableWidthPx - padding.x - cellLayoutTotalPadding;
Thales Lima78d00ad2021-09-30 11:29:06 +0100825 result.x = calculateCellWidth(screenWidthPx, cellLayoutBorderSpacePx.x, numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700826 result.y = calculateCellHeight(availableHeightPx - padding.y
Thales Lima78d00ad2021-09-30 11:29:06 +0100827 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacePx.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700828 return result;
829 }
830
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700831 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800832 updateWorkspacePadding();
833 return new Point(workspacePadding.left + workspacePadding.right,
834 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700835 }
836
837 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800838 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
839 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700840 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800841 private void updateWorkspacePadding() {
842 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700843 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800844 padding.top = 0;
845 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800846 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700847 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700848 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700849 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700850 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700851 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700852 }
Winson Chungb3800242013-10-24 11:01:54 -0700853 } else {
Thales Lima7a6752d2021-09-23 14:31:00 +0100854 // Pad the bottom of the workspace with search/hotseat bar sizes
Tony Wickham21970cc2021-09-15 15:44:05 -0700855 int hotseatTop = hotseatBarSizePx;
Tony Wickhama29a0462021-03-02 12:28:25 -0800856 int paddingBottom = hotseatTop + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500857 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100858
Thales Limad90faab2021-09-21 17:18:47 +0100859 padding.set(desiredWorkspaceHorizontalMarginPx,
Thales Lima83bedbf2021-10-05 17:47:39 +0100860 workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx),
Thales Limad90faab2021-09-21 17:18:47 +0100861 desiredWorkspaceHorizontalMarginPx,
Thales Lima7a6752d2021-09-23 14:31:00 +0100862 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700863 }
Winson Chungb3800242013-10-24 11:01:54 -0700864 }
865
Sunny Goyal57b22792021-05-25 14:35:01 -0700866 /**
867 * Returns the padding for hotseat view
868 */
869 public Rect getHotseatLayoutPadding(Context context) {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700870 if (isVerticalBarLayout()) {
871 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700872 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
873 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700874 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700875 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
876 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700877 }
Sunny Goyal57b22792021-05-25 14:35:01 -0700878 } else if (isTaskbarPresent) {
Tony Wickham21970cc2021-09-15 15:44:05 -0700879 int hotseatHeight = workspacePadding.bottom;
Sunny Goyal57b22792021-05-25 14:35:01 -0700880 int taskbarOffset = getTaskbarOffsetY();
Tony Wickham21970cc2021-09-15 15:44:05 -0700881 int hotseatTopDiff = hotseatHeight - taskbarOffset;
Sunny Goyal57b22792021-05-25 14:35:01 -0700882
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700883 int endOffset = ApiWrapper.getHotseatEndOffset(context);
Sunny Goyal57b22792021-05-25 14:35:01 -0700884 int requiredWidth = iconSizePx * numShownHotseatIcons;
885
886 Resources res = context.getResources();
887 float taskbarIconSize = res.getDimension(R.dimen.taskbar_icon_size);
888 float taskbarIconSpacing = 2 * res.getDimension(R.dimen.taskbar_icon_spacing);
889 int maxSize = (int) (requiredWidth
890 * (taskbarIconSize + taskbarIconSpacing) / taskbarIconSize);
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700891 int hotseatSize = Math.min(maxSize, availableWidthPx - endOffset);
Sunny Goyal57b22792021-05-25 14:35:01 -0700892 int sideSpacing = (availableWidthPx - hotseatSize) / 2;
893 mHotseatPadding.set(sideSpacing, hotseatTopDiff, sideSpacing, taskbarOffset);
894
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700895 if (endOffset > sideSpacing) {
Sunny Goyal57b22792021-05-25 14:35:01 -0700896 int diff = Utilities.isRtl(context.getResources())
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700897 ? sideSpacing - endOffset
898 : endOffset - sideSpacing;
899 mHotseatPadding.left -= diff;
900 mHotseatPadding.right += diff;
Sunny Goyal57b22792021-05-25 14:35:01 -0700901 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700902 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700903 // We want the edges of the hotseat to line up with the edges of the workspace, but the
904 // icons in the hotseat are a different size, and so don't line up perfectly. To account
905 // for this, we pad the left and right of the hotseat with half of the difference of a
906 // workspace cell vs a hotseat cell.
907 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700908 float hotseatCellWidth = (float) widthPx / numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700909 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
910 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700911 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
912 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700913 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700914 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
915 + mInsets.right,
Jon Miranda0d284852021-06-22 14:50:55 -0700916 hotseatBarSizePx - hotseatCellHeightPx - hotseatBarTopPaddingPx
917 + cellLayoutBottomPaddingPx + mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700918 }
919 return mHotseatPadding;
920 }
921
Winsonfadbe8f2016-07-22 16:35:37 -0700922 /**
Sunny Goyal57b22792021-05-25 14:35:01 -0700923 * Returns the number of pixels the QSB is translated from the bottom of the screen.
924 */
925 public int getQsbOffsetY() {
926 int freeSpace = isTaskbarPresent
927 ? workspacePadding.bottom
928 : hotseatBarSizePx - hotseatCellHeightPx - hotseatQsbHeight;
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000929
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000930 if (isScalableGrid && qsbBottomMarginPx > mInsets.bottom) {
Tony Wickham21970cc2021-09-15 15:44:05 -0700931 // Note that taskbarSize = 0 unless isTaskbarPresent.
932 return Math.min(qsbBottomMarginPx + taskbarSize, freeSpace);
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000933 } else {
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000934 return (int) (freeSpace * QSB_CENTER_FACTOR)
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100935 + (isTaskbarPresent ? taskbarSize : mInsets.bottom);
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000936 }
Sunny Goyal57b22792021-05-25 14:35:01 -0700937 }
938
939 /**
940 * Returns the number of pixels the taskbar is translated from the bottom of the screen.
941 */
942 public int getTaskbarOffsetY() {
943 return (getQsbOffsetY() - taskbarSize) / 2;
944 }
945
946 /**
Winsonfadbe8f2016-07-22 16:35:37 -0700947 * @return the bounds for which the open folders should be contained within
948 */
949 public Rect getAbsoluteOpenFolderBounds() {
950 if (isVerticalBarLayout()) {
951 // Folders should only appear right of the drop target bar and left of the hotseat
952 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
953 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700954 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700955 mInsets.top + availableHeightPx);
956 } else {
957 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -0800958 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800959 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700960 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800961 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -0800962 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -0700963 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700964 }
965 }
966
Jon Miranda228877d2021-02-09 11:05:00 -0500967 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
968 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700969 }
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100970
Jon Miranda228877d2021-02-09 11:05:00 -0500971 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
972 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700973 }
974
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700975 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700976 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
977 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
978 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700979 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700980 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700981 return isLandscape && transposeLayoutWithOrientation;
982 }
983
Sunny Goyal59d086c2018-05-07 17:31:40 -0700984 /**
985 * Updates orientation information and returns true if it has changed from the previous value.
986 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700987 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700988 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700989 boolean isSeascape = DisplayController.INSTANCE.get(context)
990 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700991 if (mIsSeascape != isSeascape) {
992 mIsSeascape = isSeascape;
993 return true;
994 }
995 }
996 return false;
997 }
998
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800999 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001000 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001001 }
1002
Sunny Goyal07b69292018-01-08 14:19:34 -08001003 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001004 return isVerticalBarLayout();
Winson Chungb3800242013-10-24 11:01:54 -07001005 }
1006
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001007 public int getCellContentHeight(@ContainerType int containerType) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001008 switch (containerType) {
1009 case CellLayout.WORKSPACE:
1010 return cellHeightPx;
1011 case CellLayout.FOLDER:
1012 return folderCellHeightPx;
1013 case CellLayout.HOTSEAT:
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001014 // The hotseat is the only container where the cell height is going to be
1015 // different from the content within that cell.
1016 return iconSizePx;
Sunny Goyalc13403c2016-11-18 23:44:48 -08001017 default:
1018 // ??
1019 return 0;
1020 }
1021 }
Sunny Goyal13178ac2016-04-04 16:35:22 -07001022
Jon Miranda58561d42021-03-17 10:51:54 -04001023 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001024 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -04001025 }
1026
1027 public void dump(String prefix, PrintWriter writer) {
1028 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -07001029 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -04001030
Alex Chaua02eddc2021-04-29 00:36:06 +01001031 writer.println(prefix + "\tallowRotation:" + allowRotation);
Jon Miranda58561d42021-03-17 10:51:54 -04001032 writer.println(prefix + "\tisTablet:" + isTablet);
Jon Miranda58561d42021-03-17 10:51:54 -04001033 writer.println(prefix + "\tisPhone:" + isPhone);
1034 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
1035 + transposeLayoutWithOrientation);
1036
1037 writer.println(prefix + "\tisLandscape:" + isLandscape);
1038 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
Sunny Goyal19ff7282021-04-22 10:12:54 -07001039 writer.println(prefix + "\tisTwoPanels:" + isTwoPanels);
Jon Miranda58561d42021-03-17 10:51:54 -04001040
1041 writer.println(prefix + pxToDpStr("windowX", windowX));
1042 writer.println(prefix + pxToDpStr("windowY", windowY));
1043 writer.println(prefix + pxToDpStr("widthPx", widthPx));
1044 writer.println(prefix + pxToDpStr("heightPx", heightPx));
1045
1046 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
1047 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
1048
1049 writer.println(prefix + "\taspectRatio:" + aspectRatio);
1050
1051 writer.println(prefix + "\tisScalableGrid:" + isScalableGrid);
1052
Thales Lima83bedbf2021-10-05 17:47:39 +01001053 writer.println(prefix + "\tinv.numColumns: " + inv.numColumns);
1054 writer.println(prefix + "\tinv.numRows: " + inv.numRows);
Jon Miranda58561d42021-03-17 10:51:54 -04001055
Thales Lima83bedbf2021-10-05 17:47:39 +01001056 writer.println(prefix + "\tminCellSize: " + inv.minCellSize[mTypeIndex] + "dp");
Jon Mirandaab3c6812021-06-28 15:42:28 -07001057
Jon Miranda58561d42021-03-17 10:51:54 -04001058 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
1059 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
1060
1061 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
1062 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
1063
Thales Lima83bedbf2021-10-05 17:47:39 +01001064 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Horizontal",
1065 cellLayoutBorderSpacePx.x));
1066 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Vertical",
1067 cellLayoutBorderSpacePx.y));
1068
Jon Miranda58561d42021-03-17 10:51:54 -04001069 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
1070 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
1071 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
1072
1073 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
1074 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
1075 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
1076 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
1077 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
1078 folderChildDrawablePaddingPx));
Thales Lima78d00ad2021-09-30 11:29:06 +01001079 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpaceOriginalPx",
1080 folderCellLayoutBorderSpaceOriginalPx));
1081 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Horizontal",
1082 folderCellLayoutBorderSpacePx.x));
1083 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Vertical",
1084 folderCellLayoutBorderSpacePx.y));
Jon Miranda58561d42021-03-17 10:51:54 -04001085
Jon Miranda58561d42021-03-17 10:51:54 -04001086 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
1087 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
1088 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
1089 allAppsIconDrawablePaddingPx));
1090 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001091 writer.println(prefix + "\tnumShownAllAppsColumns: " + numShownAllAppsColumns);
Jon Miranda58561d42021-03-17 10:51:54 -04001092
1093 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
1094 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
1095 writer.println(prefix + pxToDpStr("hotseatBarTopPaddingPx", hotseatBarTopPaddingPx));
1096 writer.println(prefix + pxToDpStr("hotseatBarBottomPaddingPx", hotseatBarBottomPaddingPx));
1097 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
1098 hotseatBarSidePaddingStartPx));
1099 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
1100 hotseatBarSidePaddingEndPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001101 writer.println(prefix + "\tnumShownHotseatIcons: " + numShownHotseatIcons);
Jon Miranda58561d42021-03-17 10:51:54 -04001102
1103 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
Tony Wickham635e1802021-07-22 14:28:04 -10001104 writer.println(prefix + "\tisTaskbarPresentInApps:" + isTaskbarPresentInApps);
Jon Miranda58561d42021-03-17 10:51:54 -04001105 writer.println(prefix + pxToDpStr("taskbarSize", taskbarSize));
Jon Miranda58561d42021-03-17 10:51:54 -04001106
Thales Lima83bedbf2021-10-05 17:47:39 +01001107 writer.println(prefix + pxToDpStr("desiredWorkspaceHorizontalMarginPx",
1108 desiredWorkspaceHorizontalMarginPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001109 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
1110 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
1111 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
1112 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
1113
Jon Mirandaab3c6812021-06-28 15:42:28 -07001114 writer.println(prefix + pxToDpStr("iconScale", iconScale));
1115 writer.println(prefix + pxToDpStr("cellScaleToFit ", cellScaleToFit));
Jon Miranda58561d42021-03-17 10:51:54 -04001116 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Thales Lima7a6752d2021-09-23 14:31:00 +01001117 writer.println(prefix + pxToDpStr("unscaled extraSpace", extraSpace / iconScale));
Jon Mirandac9e69fa2021-03-22 17:13:34 -04001118
1119 if (inv.devicePaddings != null) {
1120 int unscaledExtraSpace = (int) (extraSpace / iconScale);
1121 writer.println(prefix + pxToDpStr("maxEmptySpace",
1122 inv.devicePaddings.getDevicePadding(unscaledExtraSpace).getMaxEmptySpacePx()));
1123 }
Jon Miranda58561d42021-03-17 10:51:54 -04001124 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
1125 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
1126 writer.println(prefix + pxToDpStr("extraHotseatBottomPadding", extraHotseatBottomPadding));
1127 }
1128
Sunny Goyalfd58da62020-08-11 12:06:49 -07001129 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -07001130 Configuration config = new Configuration(c.getResources().getConfiguration());
1131 config.orientation = orientation;
Sunny Goyal35c7b192021-04-20 16:51:10 -07001132 config.densityDpi = info.densityDpi;
Sunny Goyal1890f672020-04-30 12:28:00 -07001133 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -07001134 }
Sunny Goyalfde55052018-02-01 14:46:13 -08001135
1136 /**
1137 * Callback when a component changes the DeviceProfile associated with it, as a result of
1138 * configuration change
1139 */
1140 public interface OnDeviceProfileChangeListener {
1141
1142 /**
1143 * Called when the device profile is reassigned. Note that for layout and measurements, it
1144 * is sufficient to listen for inset changes. Use this callback when you need to perform
1145 * a one time operation.
1146 */
1147 void onDeviceProfileChanged(DeviceProfile dp);
1148 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001149
1150 public static class Builder {
1151 private Context mContext;
1152 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -07001153 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001154
Sunny Goyal19ff7282021-04-22 10:12:54 -07001155 private WindowBounds mWindowBounds;
1156 private boolean mUseTwoPanels;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001157
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001158 private boolean mIsMultiWindowMode = false;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001159 private Boolean mTransposeLayoutWithOrientation;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001160
Sunny Goyalfd58da62020-08-11 12:06:49 -07001161 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001162 mContext = context;
1163 mInv = inv;
1164 mInfo = info;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001165 }
1166
1167 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
1168 mIsMultiWindowMode = isMultiWindowMode;
1169 return this;
1170 }
1171
Sunny Goyal19ff7282021-04-22 10:12:54 -07001172 public Builder setUseTwoPanels(boolean useTwoPanels) {
1173 mUseTwoPanels = useTwoPanels;
1174 return this;
1175 }
1176
1177
1178 public Builder setWindowBounds(WindowBounds bounds) {
1179 mWindowBounds = bounds;
Sunny Goyal0addbf02020-04-28 14:17:35 -07001180 return this;
1181 }
1182
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001183 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
1184 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
1185 return this;
1186 }
1187
1188 public DeviceProfile build() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001189 if (mWindowBounds == null) {
1190 throw new IllegalArgumentException("Window bounds not set");
1191 }
1192 if (mTransposeLayoutWithOrientation == null) {
1193 mTransposeLayoutWithOrientation = !mInfo.isTablet(mWindowBounds);
1194 }
1195 return new DeviceProfile(mContext, mInv, mInfo, mWindowBounds,
1196 mIsMultiWindowMode, mTransposeLayoutWithOrientation, mUseTwoPanels);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001197 }
1198 }
1199
Winson Chungb3800242013-10-24 11:01:54 -07001200}