blob: d2d00c855446089e0523c9c65c42833924b5d650 [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 -070020import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
21
Jon Mirandae126d722021-02-25 10:45:20 -050022import static com.android.launcher3.ResourceUtils.pxFromDp;
Jon Miranda58561d42021-03-17 10:51:54 -040023import static com.android.launcher3.Utilities.dpiFromPx;
Jon Mirandaaf3aed02021-05-06 18:28:45 -070024import static com.android.launcher3.Utilities.pxFromSp;
Jon Miranda92c1b5d2021-07-20 13:57:16 -070025import static com.android.launcher3.folder.ClippedFolderIconLayoutRule.ICON_OVERLAP_FACTOR;
Alex Chaua02eddc2021-04-29 00:36:06 +010026import static com.android.launcher3.util.WindowManagerCompat.MIN_TABLET_WIDTH;
Jon Miranda228877d2021-02-09 11:05:00 -050027
Sunny Goyal35c7b192021-04-20 16:51:10 -070028import android.annotation.SuppressLint;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070030import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.content.res.Resources;
Sunny Goyal8b9919d2021-04-07 14:45:17 -070032import android.graphics.Path;
Winson Chungb3800242013-10-24 11:01:54 -070033import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080034import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070035import android.graphics.Rect;
Sunny Goyal35c7b192021-04-20 16:51:10 -070036import android.hardware.display.DisplayManager;
37import android.util.DisplayMetrics;
Sunny Goyal59d086c2018-05-07 17:31:40 -070038import android.view.Surface;
Tony Wickham15883892021-02-12 11:24:40 -080039import android.view.WindowInsets;
40import android.view.WindowManager;
Winson Chungb3800242013-10-24 11:01:54 -070041
Sunny Goyalc13403c2016-11-18 23:44:48 -080042import com.android.launcher3.CellLayout.ContainerType;
Jon Miranda228877d2021-02-09 11:05:00 -050043import com.android.launcher3.DevicePaddings.DevicePadding;
Tony Wickham15883892021-02-12 11:24:40 -080044import com.android.launcher3.config.FeatureFlags;
Tony Wickham8912b042018-11-29 15:28:53 -080045import com.android.launcher3.icons.DotRenderer;
Sunny Goyal8b9919d2021-04-07 14:45:17 -070046import com.android.launcher3.icons.GraphicsUtils;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070047import com.android.launcher3.icons.IconNormalizer;
Sunny Goyal57b22792021-05-25 14:35:01 -070048import com.android.launcher3.uioverrides.ApiWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070049import com.android.launcher3.util.DisplayController;
50import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb46703d2020-05-27 17:52:03 -070051import com.android.launcher3.util.WindowBounds;
Winson1f064272016-07-18 17:18:02 -070052
Jon Miranda58561d42021-03-17 10:51:54 -040053import java.io.PrintWriter;
54
Sunny Goyal35c7b192021-04-20 16:51:10 -070055@SuppressLint("NewApi")
Winson Chungb3800242013-10-24 11:01:54 -070056public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070057
Sunny Goyal8b9919d2021-04-07 14:45:17 -070058 private static final int DEFAULT_DOT_SIZE = 100;
Sunny Goyal57b22792021-05-25 14:35:01 -070059 // Ratio of empty space, qsb should take up to appear visually centered.
60 private static final float QSB_CENTER_FACTOR = .325f;
Sunny Goyal1890f672020-04-30 12:28:00 -070061
Adam Cohen2e6da152015-05-06 11:42:25 -070062 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070063 private final Info mInfo;
Sunny Goyal35c7b192021-04-20 16:51:10 -070064 private final DisplayMetrics mMetrics;
Winson Chungbe876472014-05-14 14:15:20 -070065
Sunny Goyalc6205602015-05-21 20:46:33 -070066 // Device properties
67 public final boolean isTablet;
Sunny Goyalc6205602015-05-21 20:46:33 -070068 public final boolean isPhone;
69 public final boolean transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -070070 public final boolean isTwoPanels;
Alex Chaua02eddc2021-04-29 00:36:06 +010071 public final boolean allowRotation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070072
Sunny Goyalc6205602015-05-21 20:46:33 -070073 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070074 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080075 public final boolean isMultiWindowMode;
76
Sunny Goyal0addbf02020-04-28 14:17:35 -070077 public final int windowX;
78 public final int windowY;
Sunny Goyalc6205602015-05-21 20:46:33 -070079 public final int widthPx;
80 public final int heightPx;
81 public final int availableWidthPx;
82 public final int availableHeightPx;
Jon Mirandabba64512019-03-27 10:54:17 -070083
84 public final float aspectRatio;
85
Jon Mirandae126d722021-02-25 10:45:20 -050086 public final boolean isScalableGrid;
87
Tony Wickhamd6b40372015-09-23 18:37:57 -070088 /**
89 * The maximum amount of left/right workspace padding as a percentage of the screen width.
90 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
91 * 7% of the screen width can be used as right padding.
92 */
93 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070094
Jon Miranda3f9bab22017-07-28 14:50:17 -070095 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda80cddbc2021-07-22 13:51:16 -070096 private static final float TALLER_DEVICE_ASPECT_RATIO_THRESHOLD = 2.15f;
Jon Miranda80dda302021-07-28 14:14:59 -070097 private static final float TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP = 252;
98 private static final float TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP = 268;
Jon Miranda30d0aa22017-07-25 15:55:29 -070099
Jon Miranda5eacbb72018-07-31 11:14:46 -0700100 // To evenly space the icons, increase the left/right margins for tablets in portrait mode.
101 private static final int PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER = 4;
102
Sunny Goyalc6205602015-05-21 20:46:33 -0700103 // Workspace
Jon Mirandae126d722021-02-25 10:45:20 -0500104 public final int desiredWorkspaceLeftRightOriginalPx;
105 public int desiredWorkspaceLeftRightMarginPx;
106 public final int cellLayoutBorderSpacingOriginalPx;
107 public int cellLayoutBorderSpacingPx;
Jon Miranda28032002017-07-13 16:18:56 -0700108 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700109 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700110 public final int edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700111 public float workspaceSpringLoadShrinkFactor;
112 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700113
Jon Miranda58561d42021-03-17 10:51:54 -0400114 private final int extraSpace;
Jon Miranda228877d2021-02-09 11:05:00 -0500115 public int workspaceTopPadding;
116 public int workspaceBottomPadding;
Jon Miranda58561d42021-03-17 10:51:54 -0400117 public int extraHotseatBottomPadding;
Jon Miranda228877d2021-02-09 11:05:00 -0500118
Tony Wickham5edf9e22020-03-27 20:06:52 -0700119 // Workspace page indicator
120 public final int workspacePageIndicatorHeight;
121 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -0700122
Sunny Goyalc6205602015-05-21 20:46:33 -0700123 // Workspace icons
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400124 public float iconScale;
Sunny Goyalc6205602015-05-21 20:46:33 -0700125 public int iconSizePx;
126 public int iconTextSizePx;
127 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700128 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700129
Jon Mirandaab3c6812021-06-28 15:42:28 -0700130 public float cellScaleToFit;
Adam Cohen59400422014-03-05 18:07:04 -0800131 public int cellWidthPx;
132 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700133 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800134
Jon Mirandae126d722021-02-25 10:45:20 -0500135 public int cellYPaddingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500136
Sunny Goyalc6205602015-05-21 20:46:33 -0700137 // Folder
Jon Mirandae126d722021-02-25 10:45:20 -0500138 public float folderLabelTextScale;
139 public int folderLabelTextSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700140 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700141 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700142
Jon Mirandae126d722021-02-25 10:45:20 -0500143 // Folder content
Jon Miranda823da222021-03-23 08:08:45 -0400144 public int folderCellLayoutBorderSpacingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500145 public int folderContentPaddingLeftRight;
146 public int folderContentPaddingTop;
147
Jon Mirandabf7d8122016-11-03 15:29:29 -0700148 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700149 public int folderCellWidthPx;
150 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700151
152 // Folder child
153 public int folderChildIconSizePx;
154 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700155 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700156
Sunny Goyalc6205602015-05-21 20:46:33 -0700157 // Hotseat
Jon Miranda80cddbc2021-07-22 13:51:16 -0700158 public int hotseatBarSizeExtraSpacePx;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700159 public final int numShownHotseatIcons;
Sunny Goyalc6205602015-05-21 20:46:33 -0700160 public int hotseatCellHeightPx;
Jon Miranda0d284852021-06-22 14:50:55 -0700161 private final int hotseatExtraVerticalSize;
Jon Miranda18751b62017-07-31 17:25:27 -0700162 // In portrait: size = height, in landscape: size = width
163 public int hotseatBarSizePx;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700164 public int hotseatBarTopPaddingPx;
Jon Miranda0d284852021-06-22 14:50:55 -0700165 public final int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700166 // Start is the side next to the nav bar, end is the side next to the workspace
167 public final int hotseatBarSidePaddingStartPx;
168 public final int hotseatBarSidePaddingEndPx;
Sunny Goyal57b22792021-05-25 14:35:01 -0700169 public final int hotseatQsbHeight;
Adam Cohen2e6da152015-05-06 11:42:25 -0700170
Jon Miranda8bdb2222021-06-23 18:10:10 -0700171 public final float qsbBottomMarginOriginalPx;
172 public int qsbBottomMarginPx;
173
Sunny Goyalc6205602015-05-21 20:46:33 -0700174 // All apps
Samuel Fufaee9aff92021-04-05 13:30:40 -0500175 public int allAppsOpenVerticalTranslate;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700176 public int allAppsCellHeightPx;
Samuel Fufaf1424a32019-10-04 15:15:19 -0700177 public int allAppsCellWidthPx;
Winson1f064272016-07-18 17:18:02 -0700178 public int allAppsIconSizePx;
179 public int allAppsIconDrawablePaddingPx;
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 Chaudedbc8a2021-03-17 16:53:26 +0000186 public int overviewTaskIconSizePx;
187 public int overviewTaskThumbnailTopMarginPx;
zakcohendad8a802021-05-20 09:52:26 -0700188 public final int overviewActionsMarginThreeButtonPx;
Alex Chau5fd9d492021-07-22 17:27:11 +0100189 public final int overviewActionsTopMarginGesturePx;
190 public final int overviewActionsBottomMarginGesturePx;
191 public int overviewPageSpacing;
192 public int overviewRowSpacing;
Zak Cohen334efeb2021-03-19 16:42:07 -0700193
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800194 // Widgets
195 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
196
Sunny Goyal47328fd2016-05-25 18:56:41 -0700197 // Drop Target
198 public int dropTargetBarSizePx;
Alex Chaua02eddc2021-04-29 00:36:06 +0100199 public int dropTargetDragPaddingPx;
200 public int dropTargetTextSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700201
Winson1f064272016-07-18 17:18:02 -0700202 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800203 private final Rect mInsets = new Rect();
204 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700205 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800206 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700207 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700208
Tony Wickhamf34bee82018-12-03 18:11:39 -0800209 // Notification dots
Samuel Fufac96fa242019-09-26 23:06:32 -0700210 public DotRenderer mDotRendererWorkSpace;
211 public DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800212
Tony Wickham15883892021-02-12 11:24:40 -0800213 // Taskbar
214 public boolean isTaskbarPresent;
215 public int taskbarSize;
Tony Wickham145b7fd2021-03-11 10:03:12 -0800216 // How much of the bottom inset is due to Taskbar rather than other system elements.
217 public int nonOverlappingTaskbarInset;
Tony Wickham15883892021-02-12 11:24:40 -0800218
Alex Chaua02eddc2021-04-29 00:36:06 +0100219 // DragController
220 public int flingToDeleteThresholdVelocity;
221
Sunny Goyal19ff7282021-04-22 10:12:54 -0700222 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info, WindowBounds windowBounds,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700223 boolean isMultiWindowMode, boolean transposeLayoutWithOrientation,
Sunny Goyal19ff7282021-04-22 10:12:54 -0700224 boolean useTwoPanels) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700225
Adam Cohen2e6da152015-05-06 11:42:25 -0700226 this.inv = inv;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700227 this.isLandscape = windowBounds.isLandscape();
Sunny Goyald70e75a2018-02-22 10:07:32 -0800228 this.isMultiWindowMode = isMultiWindowMode;
Jon Mirandae126d722021-02-25 10:45:20 -0500229 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700230 windowX = windowBounds.bounds.left;
231 windowY = windowBounds.bounds.top;
Winson Chungb3800242013-10-24 11:01:54 -0700232
Jon Mirandae126d722021-02-25 10:45:20 -0500233 isScalableGrid = inv.isScalable && !isVerticalBarLayout() && !isMultiWindowMode;
234
Jon Miranda7e183c32018-06-20 11:05:27 -0700235 // Determine sizes.
Sunny Goyal19ff7282021-04-22 10:12:54 -0700236 widthPx = windowBounds.bounds.width();
237 heightPx = windowBounds.bounds.height();
238 availableWidthPx = windowBounds.availableSize.x;
239 int nonFinalAvailableHeightPx = windowBounds.availableSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700240
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700241 mInfo = info;
Alex Chaua02eddc2021-04-29 00:36:06 +0100242 // If the device's pixel density was scaled (usually via settings for A11y), use the
243 // original dimensions to determine if rotation is allowed of not.
244 float originalSmallestWidth = dpiFromPx(Math.min(widthPx, heightPx), DENSITY_DEVICE_STABLE);
245 allowRotation = originalSmallestWidth >= MIN_TABLET_WIDTH;
246 // Tablet UI does not support emulated landscape.
247 isTablet = allowRotation && info.isTablet(windowBounds);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700248 isPhone = !isTablet;
249 isTwoPanels = isTablet && useTwoPanels;
Adam Cohen2e6da152015-05-06 11:42:25 -0700250
Jon Mirandabba64512019-03-27 10:54:17 -0700251 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda7e183c32018-06-20 11:05:27 -0700252 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700253
254 // Some more constants
Sunny Goyal1890f672020-04-30 12:28:00 -0700255 context = getContext(context, info, isVerticalBarLayout()
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700256 ? Configuration.ORIENTATION_LANDSCAPE
257 : Configuration.ORIENTATION_PORTRAIT);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700258 mMetrics = context.getResources().getDisplayMetrics();
Sunny Goyal1890f672020-04-30 12:28:00 -0700259 final Resources res = context.getResources();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700260
Sunny Goyal57b22792021-05-25 14:35:01 -0700261 hotseatQsbHeight = res.getDimensionPixelSize(R.dimen.qsb_widget_height);
Sunny Goyal71ef9682021-07-29 12:59:27 -0700262 isTaskbarPresent = isTablet && ApiWrapper.TASKBAR_DRAWN_IN_PROCESS
263 && FeatureFlags.ENABLE_TASKBAR.get();
Tony Wickham15883892021-02-12 11:24:40 -0800264 if (isTaskbarPresent) {
265 // Taskbar will be added later, but provides bottom insets that we should subtract
266 // from availableHeightPx.
267 taskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_size);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700268 WindowInsets windowInsets =
269 context.createWindowContext(
270 context.getSystemService(DisplayManager.class).getDisplay(mInfo.id),
271 TYPE_APPLICATION, null)
272 .getSystemService(WindowManager.class)
Tony Wickham15883892021-02-12 11:24:40 -0800273 .getCurrentWindowMetrics().getWindowInsets();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700274 nonOverlappingTaskbarInset = taskbarSize - windowInsets.getSystemWindowInsetBottom();
Tony Wickham15883892021-02-12 11:24:40 -0800275 if (nonOverlappingTaskbarInset > 0) {
276 nonFinalAvailableHeightPx -= nonOverlappingTaskbarInset;
277 }
278 }
279 availableHeightPx = nonFinalAvailableHeightPx;
280
Winson Chungb3800242013-10-24 11:01:54 -0700281 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700282
Jon Mirandae126d722021-02-25 10:45:20 -0500283 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : isScalableGrid
284 ? res.getDimensionPixelSize(R.dimen.scalable_grid_left_right_margin)
285 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
286 desiredWorkspaceLeftRightOriginalPx = desiredWorkspaceLeftRightMarginPx;
287
Samuel Fufaee9aff92021-04-05 13:30:40 -0500288
289 allAppsOpenVerticalTranslate = res.getDimensionPixelSize(
290 R.dimen.all_apps_open_vertical_translate);
291
Jon Mirandae126d722021-02-25 10:45:20 -0500292 folderLabelTextScale = res.getFloat(R.dimen.folder_label_text_scale);
293 folderContentPaddingLeftRight =
294 res.getDimensionPixelSize(R.dimen.folder_content_padding_left_right);
295 folderContentPaddingTop = res.getDimensionPixelSize(R.dimen.folder_content_padding_top);
296
Sunny Goyal35c7b192021-04-20 16:51:10 -0700297 setCellLayoutBorderSpacing(pxFromDp(inv.borderSpacing, mMetrics, 1f));
Jon Mirandae126d722021-02-25 10:45:20 -0500298 cellLayoutBorderSpacingOriginalPx = cellLayoutBorderSpacingPx;
Jon Miranda823da222021-03-23 08:08:45 -0400299 folderCellLayoutBorderSpacingPx = cellLayoutBorderSpacingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500300
Jon Miranda5eacbb72018-07-31 11:14:46 -0700301 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
302 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Jon Mirandae126d722021-02-25 10:45:20 -0500303 int cellLayoutPadding = isScalableGrid
304 ? 0
305 : res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100306
Sunny Goyal19ff7282021-04-22 10:12:54 -0700307 if (isTwoPanels) {
Steven Ng2a5c5942021-07-29 17:27:00 +0100308 cellLayoutPaddingLeftRightPx = res.getDimensionPixelSize(
309 isLandscape
310 ? R.dimen.two_panels_home_side_padding_landscape
311 : R.dimen.two_panels_home_side_padding_portrait);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100312 cellLayoutBottomPaddingPx = 0;
313 } else if (isLandscape) {
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700314 cellLayoutPaddingLeftRightPx = 0;
315 cellLayoutBottomPaddingPx = cellLayoutPadding;
316 } else {
317 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
318 cellLayoutBottomPaddingPx = 0;
319 }
320
Tony Wickham5edf9e22020-03-27 20:06:52 -0700321 workspacePageIndicatorHeight = res.getDimensionPixelSize(
322 R.dimen.workspace_page_indicator_height);
323 mWorkspacePageIndicatorOverlapWorkspace =
324 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700325
Winson Chungb3800242013-10-24 11:01:54 -0700326 iconDrawablePaddingOriginalPx =
327 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Alex Chaua02eddc2021-04-29 00:36:06 +0100328
Sunny Goyal47328fd2016-05-25 18:56:41 -0700329 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
Alex Chaua02eddc2021-04-29 00:36:06 +0100330 dropTargetDragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
331 dropTargetTextSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size);
332
Sunny Goyal47328fd2016-05-25 18:56:41 -0700333 workspaceSpringLoadedBottomSpace =
334 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700335
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700336 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700337
Sunny Goyal19ff7282021-04-22 10:12:54 -0700338 numShownHotseatIcons =
339 isTwoPanels ? inv.numDatabaseHotseatIcons : inv.numShownHotseatIcons;
340 numShownAllAppsColumns =
341 isTwoPanels ? inv.numDatabaseAllAppsColumns : inv.numAllAppsColumns;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700342 hotseatBarSizeExtraSpacePx = 0;
Winson1f064272016-07-18 17:18:02 -0700343 hotseatBarTopPaddingPx =
344 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700345 hotseatBarBottomPaddingPx = (isTallDevice ? 0
346 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
347 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700348 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800349 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700350 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700351 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Jon Miranda0d284852021-06-22 14:50:55 -0700352 hotseatExtraVerticalSize =
Jon Miranda228877d2021-02-09 11:05:00 -0500353 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size);
Jon Miranda0d284852021-06-22 14:50:55 -0700354 updateHotseatIconSize(pxFromDp(inv.iconSize, mMetrics, 1f));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700355
Jon Miranda8bdb2222021-06-23 18:10:10 -0700356 qsbBottomMarginOriginalPx = isScalableGrid
357 ? res.getDimensionPixelSize(R.dimen.scalable_grid_qsb_bottom_margin)
358 : 0;
359
Alex Chau646f8042021-07-26 11:56:22 +0100360 overviewShowAsGrid = isTablet && FeatureFlags.ENABLE_OVERVIEW_GRID.get();
Alex Chau5fd9d492021-07-22 17:27:11 +0100361 overviewTaskMarginPx = overviewShowAsGrid
362 ? res.getDimensionPixelSize(R.dimen.overview_task_margin_grid)
363 : res.getDimensionPixelSize(R.dimen.overview_task_margin);
364 overviewTaskIconSizePx = res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_size);
Alex Chaudedbc8a2021-03-17 16:53:26 +0000365 overviewTaskThumbnailTopMarginPx = overviewTaskIconSizePx + overviewTaskMarginPx * 2;
Alex Chau5fd9d492021-07-22 17:27:11 +0100366 if (overviewShowAsGrid) {
367 if (isLandscape) {
368 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
369 R.dimen.overview_actions_top_margin_gesture_grid_landscape);
370 overviewActionsBottomMarginGesturePx = res.getDimensionPixelSize(
371 R.dimen.overview_actions_bottom_margin_gesture_grid_landscape);
372 } else {
373 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
374 R.dimen.overview_actions_top_margin_gesture_grid_portrait);
375 overviewActionsBottomMarginGesturePx = res.getDimensionPixelSize(
376 R.dimen.overview_actions_bottom_margin_gesture_grid_portrait);
377 }
378 } else {
379 overviewActionsTopMarginGesturePx = res.getDimensionPixelSize(
380 R.dimen.overview_actions_margin_gesture);
381 overviewActionsBottomMarginGesturePx = overviewActionsTopMarginGesturePx;
382 }
zakcohendad8a802021-05-20 09:52:26 -0700383 overviewActionsMarginThreeButtonPx = res.getDimensionPixelSize(
Alex Chau5fd9d492021-07-22 17:27:11 +0100384 R.dimen.overview_actions_margin_three_button);
385 overviewPageSpacing = overviewShowAsGrid
386 ? res.getDimensionPixelSize(R.dimen.recents_page_spacing_grid)
387 : res.getDimensionPixelSize(R.dimen.recents_page_spacing);
388 overviewRowSpacing = isLandscape
389 ? res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing_landscape)
390 : res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing_portrait);
Zak Cohen334efeb2021-03-19 16:42:07 -0700391
Jon Miranda2ed276e2017-06-29 11:36:34 -0700392 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400393 extraSpace = updateAvailableDimensions(res);
Jon Miranda80cddbc2021-07-22 13:51:16 -0700394
Jon Miranda2ed276e2017-06-29 11:36:34 -0700395 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400396 if (isScalableGrid && inv.devicePaddings != null) {
397 // Paddings were created assuming no scaling, so we first unscale the extra space.
Jon Mirandaab3c6812021-06-28 15:42:28 -0700398 int unscaledExtraSpace = (int) (extraSpace / cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400399 DevicePadding padding = inv.devicePaddings.getDevicePadding(unscaledExtraSpace);
Jon Miranda228877d2021-02-09 11:05:00 -0500400
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400401 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
402 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
403 int paddingHotseatBottom = padding.getHotseatBottomPadding(unscaledExtraSpace);
404
Jon Mirandaab3c6812021-06-28 15:42:28 -0700405 workspaceTopPadding = Math.round(paddingWorkspaceTop * cellScaleToFit);
406 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * cellScaleToFit);
407 extraHotseatBottomPadding = Math.round(paddingHotseatBottom * cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400408
Jon Miranda58561d42021-03-17 10:51:54 -0400409 hotseatBarSizePx += extraHotseatBottomPadding;
Jon Miranda8bdb2222021-06-23 18:10:10 -0700410
Jon Mirandaab3c6812021-06-28 15:42:28 -0700411 qsbBottomMarginPx = Math.round(qsbBottomMarginOriginalPx * cellScaleToFit);
Jon Miranda228877d2021-02-09 11:05:00 -0500412 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700413 // We increase the hotseat size when there is extra space.
Jon Miranda80cddbc2021-07-22 13:51:16 -0700414
Jon Miranda80dda302021-07-28 14:14:59 -0700415 if (Float.compare(aspectRatio, TALLER_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0
416 && extraSpace >= Utilities.dpToPx(TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP)) {
417 // For taller devices, we will take a piece of the extra space from each row,
Jon Miranda80cddbc2021-07-22 13:51:16 -0700418 // and add it to the space above and below the hotseat.
Jon Miranda80dda302021-07-28 14:14:59 -0700419
420 // For devices with more extra space, we take a larger piece from each cell.
421 int piece = extraSpace < Utilities.dpToPx(TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP)
422 ? 5 : 3;
423
Jon Miranda80cddbc2021-07-22 13:51:16 -0700424 int extraSpace = ((getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2)
Jon Miranda80dda302021-07-28 14:14:59 -0700425 * inv.numRows) / piece;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700426
427 int halfExtraSpace = extraSpace / 2;
428 hotseatBarTopPaddingPx += halfExtraSpace;
429 hotseatBarSizeExtraSpacePx = halfExtraSpace;
430 } else {
431 // ie. For a display with a large aspect ratio, we can keep the icons on the
432 // workspace in portrait mode closer together by adding more height to the hotseat.
433 // Note: This calculation was created after noticing a pattern in the design spec.
434 hotseatBarSizeExtraSpacePx = getCellSize().y - iconSizePx
435 - iconDrawablePaddingPx * 2 - workspacePageIndicatorHeight;
436 }
437
438 updateHotseatIconSize(iconSizePx);
Jon Miranda30d0aa22017-07-25 15:55:29 -0700439
Jon Miranda3f9bab22017-07-28 14:50:17 -0700440 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700441 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700442 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800443 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800444
Alex Chaua02eddc2021-04-29 00:36:06 +0100445 flingToDeleteThresholdVelocity = res.getDimensionPixelSize(
446 R.dimen.drag_flingToDeleteMinVelocity);
447
Tony Wickham1237df02017-02-24 08:59:36 -0800448 // This is done last, after iconSizePx is calculated above.
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700449 Path dotPath = GraphicsUtils.getShapePath(DEFAULT_DOT_SIZE);
450 mDotRendererWorkSpace = new DotRenderer(iconSizePx, dotPath, DEFAULT_DOT_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700451 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700452 new DotRenderer(allAppsIconSizePx, dotPath, DEFAULT_DOT_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700453 }
454
Jon Miranda0d284852021-06-22 14:50:55 -0700455 private void updateHotseatIconSize(int hotseatIconSizePx) {
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700456 // Ensure there is enough space for folder icons, which have a slightly larger radius.
457 hotseatCellHeightPx = (int) Math.ceil(hotseatIconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda0d284852021-06-22 14:50:55 -0700458 if (isVerticalBarLayout()) {
459 hotseatBarSizePx = hotseatIconSizePx + hotseatBarSidePaddingStartPx
460 + hotseatBarSidePaddingEndPx;
461 } else {
462 hotseatBarSizePx = hotseatIconSizePx + hotseatBarTopPaddingPx
Jon Miranda80cddbc2021-07-22 13:51:16 -0700463 + hotseatBarBottomPaddingPx + (isScalableGrid ? 0 : hotseatExtraVerticalSize)
464 + hotseatBarSizeExtraSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700465 }
466 }
467
Jon Mirandae126d722021-02-25 10:45:20 -0500468 private void setCellLayoutBorderSpacing(int borderSpacing) {
Jon Miranda823da222021-03-23 08:08:45 -0400469 cellLayoutBorderSpacingPx = isScalableGrid ? borderSpacing : 0;
Jon Mirandae126d722021-02-25 10:45:20 -0500470 }
471
Sunny Goyal69a8eec2021-07-22 10:02:50 -0700472 public Info getDisplayInfo() {
473 return mInfo;
474 }
475
Jon Miranda611dba42021-03-02 14:12:13 -0500476 /**
477 * We inset the widget padding added by the system and instead rely on the border spacing
478 * between cells to create reliable consistency between widgets
479 */
480 public boolean shouldInsetWidgets() {
481 Rect widgetPadding = inv.defaultWidgetPadding;
482
Jon Miranda49811182021-06-03 09:52:40 -0700483 // Check all sides to ensure that the widget won't overlap into another cell, or into
484 // status bar.
485 return workspaceTopPadding > widgetPadding.top
486 && cellLayoutBorderSpacingPx > widgetPadding.left
Jon Miranda611dba42021-03-02 14:12:13 -0500487 && cellLayoutBorderSpacingPx > widgetPadding.top
488 && cellLayoutBorderSpacingPx > widgetPadding.right
489 && cellLayoutBorderSpacingPx > widgetPadding.bottom;
490 }
Jon Mirandae126d722021-02-25 10:45:20 -0500491
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700492 public Builder toBuilder(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700493 WindowBounds bounds =
494 new WindowBounds(widthPx, heightPx, availableWidthPx, availableHeightPx);
495 bounds.bounds.offsetTo(windowX, windowY);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700496 return new Builder(context, inv, mInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700497 .setWindowBounds(bounds)
498 .setUseTwoPanels(isTwoPanels)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700499 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800500 }
501
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700502 public DeviceProfile copy(Context context) {
503 return toBuilder(context).build();
504 }
505
506 /**
507 * TODO: Move this to the builder as part of setMultiWindowMode
508 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700509 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700510 DeviceProfile profile = toBuilder(context)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700511 .setWindowBounds(windowBounds)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700512 .setMultiWindowMode(true)
513 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800514
Jon Miranda941375e2021-03-31 13:10:45 -0400515 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda93e1f042016-11-11 14:13:04 -0800516
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800517 // We use these scales to measure and layout the widgets using their full invariant profile
518 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
519 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
520 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
521 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800522 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800523
Jon Miranda93e1f042016-11-11 14:13:04 -0800524 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700525 }
526
Adam Cohen63f1ec02014-08-12 09:23:13 -0700527 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400528 * Checks if there is enough space for labels on the workspace.
529 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700530 * It is important to call this method after the All Apps variables have been set.
531 */
Jon Miranda941375e2021-03-31 13:10:45 -0400532 private void hideWorkspaceLabelsIfNotEnoughSpace() {
533 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
534 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
535 - iconTextHeight;
536
537 // We want enough space so that the text is closer to its corresponding icon.
538 if (workspaceCellPaddingY < iconTextHeight) {
539 iconTextSizePx = 0;
540 iconDrawablePaddingPx = 0;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700541 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda941375e2021-03-31 13:10:45 -0400542 autoResizeAllAppsCells();
543 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700544 }
Jon Miranda1091e532017-07-21 13:31:50 -0700545
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700546 /**
547 * Re-computes the all-apps cell size to be independent of workspace
548 */
549 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400550 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
551 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700552 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400553 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700554 }
555
Jon Miranda228877d2021-02-09 11:05:00 -0500556 /**
557 * Returns the amount of extra (or unused) vertical space.
558 */
559 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700560 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800561
Jon Mirandae126d722021-02-25 10:45:20 -0500562 Point workspacePadding = getTotalWorkspacePadding();
563
564 // Check to see if the icons fit within the available height.
565 float usedHeight = getCellLayoutHeight();
566 final int maxHeight = availableHeightPx - workspacePadding.y;
Jon Miranda228877d2021-02-09 11:05:00 -0500567 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500568 float scaleY = maxHeight / usedHeight;
569 boolean shouldScale = scaleY < 1f;
570
571 float scaleX = 1f;
572 if (isScalableGrid) {
573 // We scale to fit the cellWidth and cellHeight in the available space.
574 // The benefit of scalable grids is that we can get consistent aspect ratios between
575 // devices.
576 float usedWidth = (cellWidthPx * inv.numColumns)
577 + (cellLayoutBorderSpacingPx * (inv.numColumns - 1))
578 + (desiredWorkspaceLeftRightMarginPx * 2);
579 // We do not subtract padding here, as we also scale the workspace padding if needed.
580 scaleX = availableWidthPx / usedWidth;
581 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700582 }
Jon Mirandae126d722021-02-25 10:45:20 -0500583
584 if (shouldScale) {
585 float scale = Math.min(scaleX, scaleY);
586 updateIconSize(scale, res);
587 extraHeight = Math.max(0, maxHeight - getCellLayoutHeight());
588 }
589
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700590 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500591 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700592 }
593
Jon Mirandae126d722021-02-25 10:45:20 -0500594 private int getCellLayoutHeight() {
595 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacingPx * (inv.numRows - 1));
596 }
597
Jon Miranda6f7e9702019-09-16 14:44:14 -0700598 /**
599 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
600 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
601 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
602 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800603 public void updateIconSize(float scale, Resources res) {
Jon Mirandaab3c6812021-06-28 15:42:28 -0700604 // Icon scale should never exceed 1, otherwise pixellation may occur.
605 iconScale = Math.min(1f, scale);
606 cellScaleToFit = scale;
607
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400608
Jon Miranda18751b62017-07-31 17:25:27 -0700609 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800610 final boolean isVerticalLayout = isVerticalBarLayout();
Andras Kloczl6057cb92021-04-07 13:45:17 +0200611 float invIconSizeDp = isLandscape ? inv.landscapeIconSize : inv.iconSize;
Jon Mirandaab3c6812021-06-28 15:42:28 -0700612 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, iconScale));
Andras Kloczl6057cb92021-04-07 13:45:17 +0200613 float invIconTextSizeSp = isLandscape ? inv.landscapeIconTextSize : inv.iconTextSize;
Jon Mirandaab3c6812021-06-28 15:42:28 -0700614 iconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * iconScale);
615 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * iconScale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700616
Jon Mirandae126d722021-02-25 10:45:20 -0500617 setCellLayoutBorderSpacing((int) (cellLayoutBorderSpacingOriginalPx * scale));
618
619 if (isScalableGrid) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700620 cellWidthPx = pxFromDp(inv.minCellWidth, mMetrics, scale);
621 cellHeightPx = pxFromDp(inv.minCellHeight, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500622 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
623 + Utilities.calculateTextHeight(iconTextSizePx);
624 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
625 desiredWorkspaceLeftRightMarginPx = (int) (desiredWorkspaceLeftRightOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500626 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500627 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700628 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR)
629 + iconDrawablePaddingPx
Jon Miranda228877d2021-02-09 11:05:00 -0500630 + Utilities.calculateTextHeight(iconTextSizePx);
631 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
632 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
633 && !isMultiWindowMode) {
634 // Ensures that the label is closer to its corresponding icon. This is not an issue
635 // with vertical bar layout or multi-window mode since the issue is handled
636 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
637 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
638 iconDrawablePaddingPx = cellPaddingY;
639 }
Jon Miranda846455e2017-10-02 14:58:52 -0700640 }
Jon Miranda18751b62017-07-31 17:25:27 -0700641
Sunny Goyalae190ff2020-04-14 00:19:01 +0000642 // All apps
Sunny Goyal19ff7282021-04-22 10:12:54 -0700643 if (numShownAllAppsColumns != inv.numColumns) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700644 allAppsIconSizePx = pxFromDp(inv.allAppsIconSize, mMetrics);
Jon Mirandaaf3aed02021-05-06 18:28:45 -0700645 allAppsIconTextSizePx = pxFromSp(inv.allAppsIconTextSize, mMetrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000646 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda941375e2021-03-31 13:10:45 -0400647 autoResizeAllAppsCells();
Sunny Goyalae190ff2020-04-14 00:19:01 +0000648 } else {
649 allAppsIconSizePx = iconSizePx;
650 allAppsIconTextSizePx = iconTextSizePx;
651 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
652 allAppsCellHeightPx = getCellSize().y;
653 }
654 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700655
Jon Miranda941375e2021-03-31 13:10:45 -0400656 if (isVerticalLayout) {
657 hideWorkspaceLabelsIfNotEnoughSpace();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700658 }
Winson Chungb3800242013-10-24 11:01:54 -0700659
Winson Chungb3800242013-10-24 11:01:54 -0700660 // Hotseat
Jon Miranda0d284852021-06-22 14:50:55 -0700661 updateHotseatIconSize(iconSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700662
Sunny Goyal07b69292018-01-08 14:19:34 -0800663 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700664 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700665 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700666 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
667 workspaceSpringLoadShrinkFactor = Math.min(
668 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
669 1 - (minRequiredHeight / expectedWorkspaceHeight));
670 } else {
671 workspaceSpringLoadShrinkFactor =
672 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
673 }
674
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700675 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700676 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
677 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700678 }
679
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700680 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700681 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700682
Jon Mirandae126d722021-02-25 10:45:20 -0500683 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
684
Jon Mirandac1b23992016-12-13 08:57:36 -0800685 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700686 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800687 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800688
689 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500690 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Jon Miranda823da222021-03-23 08:08:45 -0400691 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacingPx);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800692 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500693 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800694 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800695
696 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500697 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Jon Miranda823da222021-03-23 08:08:45 -0400698 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacingPx);
Jon Mirandae126d722021-02-25 10:45:20 -0500699 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
700 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800701 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800702
703 float scale = Math.min(scaleX, scaleY);
704 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700705 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700706 }
707 }
708
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700709 private void updateFolderCellSize(float scale, Resources res) {
Jon Mirandae126d722021-02-25 10:45:20 -0500710 float invIconSizeDp = isVerticalBarLayout() ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700711 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Jon Mirandaaf3aed02021-05-06 18:28:45 -0700712 folderChildTextSizePx = pxFromSp(inv.iconTextSize, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500713 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700714
715 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700716
Jon Miranda823da222021-03-23 08:08:45 -0400717 if (isScalableGrid) {
Jon Mirandaf33f5b32021-07-22 17:15:31 -0700718 int minWidth = folderChildIconSizePx + iconDrawablePaddingPx * 2;
719 int minHeight = folderChildIconSizePx + iconDrawablePaddingPx * 2 + textHeight;
720
721 folderCellWidthPx = (int) Math.max(minWidth, cellWidthPx * scale);
722 folderCellHeightPx = (int) Math.max(minHeight, cellHeightPx * scale);
Jon Miranda823da222021-03-23 08:08:45 -0400723
724 int borderSpacing = (int) (cellLayoutBorderSpacingOriginalPx * scale);
725 folderCellLayoutBorderSpacingPx = borderSpacing;
726 folderContentPaddingLeftRight = borderSpacing;
727 folderContentPaddingTop = borderSpacing;
728 } else {
729 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
730 * scale);
731 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
732 * scale);
733
734 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
735 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
736 }
737
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000738 folderChildDrawablePaddingPx = Math.max(0,
739 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700740 }
741
Winson1f064272016-07-18 17:18:02 -0700742 public void updateInsets(Rect insets) {
743 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800744 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700745 }
746
Sunny Goyalae6e3182019-04-30 12:04:37 -0700747 /**
748 * The current device insets. This is generally same as the insets being dispatched to
749 * {@link Insettable} elements, but can differ if the element is using a different profile.
750 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800751 public Rect getInsets() {
752 return mInsets;
753 }
754
Sunny Goyal756cd262015-08-20 12:33:21 -0700755 public Point getCellSize() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700756 return getCellSize(null);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700757 }
758
Sunny Goyal19ff7282021-04-22 10:12:54 -0700759 public Point getCellSize(Point result) {
760 if (result == null) {
761 result = new Point();
762 }
Sunny Goyal756cd262015-08-20 12:33:21 -0700763 // Since we are only concerned with the overall padding, layout direction does
764 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700765 Point padding = getTotalWorkspacePadding();
Steven Ng2a5c5942021-07-29 17:27:00 +0100766 // availableWidthPx is the screen width of the device. In 2 panels mode, each panel should
767 // only have half of the screen width. In addition, there is only cellLayoutPadding in the
768 // left side of the left panel and the right side of the right panel. There is no
769 // cellLayoutPadding in the middle.
770 int screenWidthPx = isTwoPanels
771 ? availableWidthPx / 2 - padding.x - cellLayoutPaddingLeftRightPx
772 : availableWidthPx - padding.x - cellLayoutPaddingLeftRightPx * 2;
773 result.x = calculateCellWidth(screenWidthPx, cellLayoutBorderSpacingPx, inv.numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700774 result.y = calculateCellHeight(availableHeightPx - padding.y
Sunny Goyal19ff7282021-04-22 10:12:54 -0700775 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacingPx, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700776 return result;
777 }
778
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700779 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800780 updateWorkspacePadding();
781 return new Point(workspacePadding.left + workspacePadding.right,
782 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700783 }
784
785 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800786 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
787 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700788 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800789 private void updateWorkspacePadding() {
790 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700791 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800792 padding.top = 0;
793 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800794 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700795 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700796 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700797 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700798 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700799 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700800 }
Winson Chungb3800242013-10-24 11:01:54 -0700801 } else {
Tony Wickhama29a0462021-03-02 12:28:25 -0800802 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
803 int paddingBottom = hotseatTop + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500804 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700805 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700806 // Pad the left and right of the workspace to ensure consistent spacing
807 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700808 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800809 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800810 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700811 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800812 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Tony Wickhama29a0462021-03-02 12:28:25 -0800813 int hotseatVerticalPadding = isTaskbarPresent ? 0
814 : hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700815 int availablePaddingY = Math.max(0, heightPx - edgeMarginPx - paddingBottom
Tony Wickhama29a0462021-03-02 12:28:25 -0800816 - (2 * inv.numRows * cellHeightPx) - hotseatVerticalPadding);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700817 padding.set(availablePaddingX / 2, edgeMarginPx + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700818 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100819
Sunny Goyal19ff7282021-04-22 10:12:54 -0700820 if (isTwoPanels) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100821 padding.set(0, padding.top, 0, padding.bottom);
822 }
Winson Chungb3800242013-10-24 11:01:54 -0700823 } else {
824 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700825 padding.set(desiredWorkspaceLeftRightMarginPx,
Jon Mirandae126d722021-02-25 10:45:20 -0500826 workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx),
Winson1f064272016-07-18 17:18:02 -0700827 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700828 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700829 }
830 }
Winson Chungb3800242013-10-24 11:01:54 -0700831 }
832
Sunny Goyal57b22792021-05-25 14:35:01 -0700833 /**
834 * Returns the padding for hotseat view
835 */
836 public Rect getHotseatLayoutPadding(Context context) {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700837 if (isVerticalBarLayout()) {
838 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700839 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
840 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700841 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700842 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
843 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700844 }
Sunny Goyal57b22792021-05-25 14:35:01 -0700845 } else if (isTaskbarPresent) {
846 int hotseatHeight = workspacePadding.bottom + taskbarSize;
847 int taskbarOffset = getTaskbarOffsetY();
848 int hotseatTopDiff = hotseatHeight - taskbarSize - taskbarOffset;
849
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700850 int endOffset = ApiWrapper.getHotseatEndOffset(context);
Sunny Goyal57b22792021-05-25 14:35:01 -0700851 int requiredWidth = iconSizePx * numShownHotseatIcons;
852
853 Resources res = context.getResources();
854 float taskbarIconSize = res.getDimension(R.dimen.taskbar_icon_size);
855 float taskbarIconSpacing = 2 * res.getDimension(R.dimen.taskbar_icon_spacing);
856 int maxSize = (int) (requiredWidth
857 * (taskbarIconSize + taskbarIconSpacing) / taskbarIconSize);
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700858 int hotseatSize = Math.min(maxSize, availableWidthPx - endOffset);
Sunny Goyal57b22792021-05-25 14:35:01 -0700859 int sideSpacing = (availableWidthPx - hotseatSize) / 2;
860 mHotseatPadding.set(sideSpacing, hotseatTopDiff, sideSpacing, taskbarOffset);
861
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700862 if (endOffset > sideSpacing) {
Sunny Goyal57b22792021-05-25 14:35:01 -0700863 int diff = Utilities.isRtl(context.getResources())
Vinit Nayak8dcbde82021-07-19 12:53:28 -0700864 ? sideSpacing - endOffset
865 : endOffset - sideSpacing;
866 mHotseatPadding.left -= diff;
867 mHotseatPadding.right += diff;
Sunny Goyal57b22792021-05-25 14:35:01 -0700868 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700869 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700870 // We want the edges of the hotseat to line up with the edges of the workspace, but the
871 // icons in the hotseat are a different size, and so don't line up perfectly. To account
872 // for this, we pad the left and right of the hotseat with half of the difference of a
873 // workspace cell vs a hotseat cell.
874 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700875 float hotseatCellWidth = (float) widthPx / numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700876 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
877 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700878 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
879 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700880 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700881 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
882 + mInsets.right,
Jon Miranda0d284852021-06-22 14:50:55 -0700883 hotseatBarSizePx - hotseatCellHeightPx - hotseatBarTopPaddingPx
884 + cellLayoutBottomPaddingPx + mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700885 }
886 return mHotseatPadding;
887 }
888
Winsonfadbe8f2016-07-22 16:35:37 -0700889 /**
Sunny Goyal57b22792021-05-25 14:35:01 -0700890 * Returns the number of pixels the QSB is translated from the bottom of the screen.
891 */
892 public int getQsbOffsetY() {
893 int freeSpace = isTaskbarPresent
894 ? workspacePadding.bottom
895 : hotseatBarSizePx - hotseatCellHeightPx - hotseatQsbHeight;
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000896
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000897 if (isScalableGrid && qsbBottomMarginPx > mInsets.bottom) {
Jonathan Mirandac2a49ee2021-07-22 00:38:43 +0000898 return Math.min(qsbBottomMarginPx, freeSpace);
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000899 } else {
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000900 return (int) (freeSpace * QSB_CENTER_FACTOR)
Jonathan Miranda8f16a772021-07-23 23:10:37 +0000901 + (isTaskbarPresent ? taskbarSize : mInsets.bottom);
Jonathan Miranda7a273e22021-06-24 21:29:10 +0000902 }
Sunny Goyal57b22792021-05-25 14:35:01 -0700903 }
904
905 /**
906 * Returns the number of pixels the taskbar is translated from the bottom of the screen.
907 */
908 public int getTaskbarOffsetY() {
909 return (getQsbOffsetY() - taskbarSize) / 2;
910 }
911
912 /**
Winsonfadbe8f2016-07-22 16:35:37 -0700913 * @return the bounds for which the open folders should be contained within
914 */
915 public Rect getAbsoluteOpenFolderBounds() {
916 if (isVerticalBarLayout()) {
917 // Folders should only appear right of the drop target bar and left of the hotseat
918 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
919 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700920 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700921 mInsets.top + availableHeightPx);
922 } else {
923 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -0800924 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800925 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700926 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800927 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -0800928 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -0700929 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700930 }
931 }
932
Jon Miranda228877d2021-02-09 11:05:00 -0500933 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
934 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700935 }
Jon Miranda228877d2021-02-09 11:05:00 -0500936 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
937 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700938 }
939
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700940 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700941 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
942 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
943 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700944 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700945 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700946 return isLandscape && transposeLayoutWithOrientation;
947 }
948
Sunny Goyal59d086c2018-05-07 17:31:40 -0700949 /**
950 * Updates orientation information and returns true if it has changed from the previous value.
951 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700952 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700953 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700954 boolean isSeascape = DisplayController.INSTANCE.get(context)
955 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700956 if (mIsSeascape != isSeascape) {
957 mIsSeascape = isSeascape;
958 return true;
959 }
960 }
961 return false;
962 }
963
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800964 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700965 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800966 }
967
Sunny Goyal07b69292018-01-08 14:19:34 -0800968 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700969 return isVerticalBarLayout();
Winson Chungb3800242013-10-24 11:01:54 -0700970 }
971
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700972 public int getCellContentHeight(@ContainerType int containerType) {
Sunny Goyalc13403c2016-11-18 23:44:48 -0800973 switch (containerType) {
974 case CellLayout.WORKSPACE:
975 return cellHeightPx;
976 case CellLayout.FOLDER:
977 return folderCellHeightPx;
978 case CellLayout.HOTSEAT:
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700979 // The hotseat is the only container where the cell height is going to be
980 // different from the content within that cell.
981 return iconSizePx;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800982 default:
983 // ??
984 return 0;
985 }
986 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700987
Jon Miranda58561d42021-03-17 10:51:54 -0400988 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700989 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -0400990 }
991
992 public void dump(String prefix, PrintWriter writer) {
993 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -0700994 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -0400995
Alex Chaua02eddc2021-04-29 00:36:06 +0100996 writer.println(prefix + "\tallowRotation:" + allowRotation);
Jon Miranda58561d42021-03-17 10:51:54 -0400997 writer.println(prefix + "\tisTablet:" + isTablet);
Jon Miranda58561d42021-03-17 10:51:54 -0400998 writer.println(prefix + "\tisPhone:" + isPhone);
999 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
1000 + transposeLayoutWithOrientation);
1001
1002 writer.println(prefix + "\tisLandscape:" + isLandscape);
1003 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
Sunny Goyal19ff7282021-04-22 10:12:54 -07001004 writer.println(prefix + "\tisTwoPanels:" + isTwoPanels);
Jon Miranda58561d42021-03-17 10:51:54 -04001005
1006 writer.println(prefix + pxToDpStr("windowX", windowX));
1007 writer.println(prefix + pxToDpStr("windowY", windowY));
1008 writer.println(prefix + pxToDpStr("widthPx", widthPx));
1009 writer.println(prefix + pxToDpStr("heightPx", heightPx));
1010
1011 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
1012 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
1013
1014 writer.println(prefix + "\taspectRatio:" + aspectRatio);
1015
1016 writer.println(prefix + "\tisScalableGrid:" + isScalableGrid);
1017
1018 writer.println(prefix + "\tinv.minCellWidth:" + inv.minCellWidth + "dp");
1019 writer.println(prefix + "\tinv.minCellHeight:" + inv.minCellHeight + "dp");
1020
Jon Mirandaab3c6812021-06-28 15:42:28 -07001021 writer.println(prefix + "\tinv.numColumns:" + inv.numColumns);
1022 writer.println(prefix + "\tinv.numRows:" + inv.numRows);
1023
Jon Miranda58561d42021-03-17 10:51:54 -04001024 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
1025 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
1026
1027 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
1028 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
1029
1030 writer.println(prefix + "\tinv.iconSize:" + inv.iconSize + "dp");
1031 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
1032 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
1033 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
1034
1035 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
1036 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
1037 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
1038 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
1039 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
1040 folderChildDrawablePaddingPx));
Jon Miranda823da222021-03-23 08:08:45 -04001041 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacingPx",
1042 folderCellLayoutBorderSpacingPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001043
1044 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacingPx",
1045 cellLayoutBorderSpacingPx));
1046 writer.println(prefix + pxToDpStr("desiredWorkspaceLeftRightMarginPx",
1047 desiredWorkspaceLeftRightMarginPx));
1048
1049 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
1050 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
1051 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
1052 allAppsIconDrawablePaddingPx));
1053 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001054 writer.println(prefix + "\tnumShownAllAppsColumns: " + numShownAllAppsColumns);
Jon Miranda58561d42021-03-17 10:51:54 -04001055
1056 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
1057 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
1058 writer.println(prefix + pxToDpStr("hotseatBarTopPaddingPx", hotseatBarTopPaddingPx));
1059 writer.println(prefix + pxToDpStr("hotseatBarBottomPaddingPx", hotseatBarBottomPaddingPx));
1060 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
1061 hotseatBarSidePaddingStartPx));
1062 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
1063 hotseatBarSidePaddingEndPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001064 writer.println(prefix + "\tnumShownHotseatIcons: " + numShownHotseatIcons);
Jon Miranda58561d42021-03-17 10:51:54 -04001065
1066 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
1067
1068 writer.println(prefix + pxToDpStr("taskbarSize", taskbarSize));
1069 writer.println(prefix + pxToDpStr("nonOverlappingTaskbarInset",
1070 nonOverlappingTaskbarInset));
1071
1072 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
1073 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
1074 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
1075 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
1076
Jon Mirandaab3c6812021-06-28 15:42:28 -07001077 writer.println(prefix + pxToDpStr("iconScale", iconScale));
1078 writer.println(prefix + pxToDpStr("cellScaleToFit ", cellScaleToFit));
Jon Miranda58561d42021-03-17 10:51:54 -04001079 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Jon Mirandac9e69fa2021-03-22 17:13:34 -04001080
1081 if (inv.devicePaddings != null) {
1082 int unscaledExtraSpace = (int) (extraSpace / iconScale);
1083 writer.println(prefix + pxToDpStr("maxEmptySpace",
1084 inv.devicePaddings.getDevicePadding(unscaledExtraSpace).getMaxEmptySpacePx()));
1085 }
Jon Miranda58561d42021-03-17 10:51:54 -04001086 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
1087 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
1088 writer.println(prefix + pxToDpStr("extraHotseatBottomPadding", extraHotseatBottomPadding));
1089 }
1090
Sunny Goyalfd58da62020-08-11 12:06:49 -07001091 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -07001092 Configuration config = new Configuration(c.getResources().getConfiguration());
1093 config.orientation = orientation;
Sunny Goyal35c7b192021-04-20 16:51:10 -07001094 config.densityDpi = info.densityDpi;
Sunny Goyal1890f672020-04-30 12:28:00 -07001095 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -07001096 }
Sunny Goyalfde55052018-02-01 14:46:13 -08001097
1098 /**
1099 * Callback when a component changes the DeviceProfile associated with it, as a result of
1100 * configuration change
1101 */
1102 public interface OnDeviceProfileChangeListener {
1103
1104 /**
1105 * Called when the device profile is reassigned. Note that for layout and measurements, it
1106 * is sufficient to listen for inset changes. Use this callback when you need to perform
1107 * a one time operation.
1108 */
1109 void onDeviceProfileChanged(DeviceProfile dp);
1110 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001111
1112 public static class Builder {
1113 private Context mContext;
1114 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -07001115 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001116
Sunny Goyal19ff7282021-04-22 10:12:54 -07001117 private WindowBounds mWindowBounds;
1118 private boolean mUseTwoPanels;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001119
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001120 private boolean mIsMultiWindowMode = false;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001121 private Boolean mTransposeLayoutWithOrientation;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001122
Sunny Goyalfd58da62020-08-11 12:06:49 -07001123 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001124 mContext = context;
1125 mInv = inv;
1126 mInfo = info;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001127 }
1128
1129 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
1130 mIsMultiWindowMode = isMultiWindowMode;
1131 return this;
1132 }
1133
Sunny Goyal19ff7282021-04-22 10:12:54 -07001134 public Builder setUseTwoPanels(boolean useTwoPanels) {
1135 mUseTwoPanels = useTwoPanels;
1136 return this;
1137 }
1138
1139
1140 public Builder setWindowBounds(WindowBounds bounds) {
1141 mWindowBounds = bounds;
Sunny Goyal0addbf02020-04-28 14:17:35 -07001142 return this;
1143 }
1144
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001145 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
1146 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
1147 return this;
1148 }
1149
1150 public DeviceProfile build() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001151 if (mWindowBounds == null) {
1152 throw new IllegalArgumentException("Window bounds not set");
1153 }
1154 if (mTransposeLayoutWithOrientation == null) {
1155 mTransposeLayoutWithOrientation = !mInfo.isTablet(mWindowBounds);
1156 }
1157 return new DeviceProfile(mContext, mInv, mInfo, mWindowBounds,
1158 mIsMultiWindowMode, mTransposeLayoutWithOrientation, mUseTwoPanels);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001159 }
1160 }
1161
Winson Chungb3800242013-10-24 11:01:54 -07001162}