blob: 9992fdeb9060d6ec0ee77b83290cd2421d5ad341 [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
Jon Miranda228877d2021-02-09 11:05:00 -050019import static com.android.launcher3.config.FeatureFlags.ENABLE_FOUR_COLUMNS;
20
Winson Chungb3800242013-10-24 11:01:54 -070021import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070022import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070023import android.content.res.Resources;
Winson Chungb3800242013-10-24 11:01:54 -070024import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080025import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070026import android.graphics.Rect;
Sunny Goyal59d086c2018-05-07 17:31:40 -070027import android.view.Surface;
Winson Chungb3800242013-10-24 11:01:54 -070028
Sunny Goyalc13403c2016-11-18 23:44:48 -080029import com.android.launcher3.CellLayout.ContainerType;
Jon Miranda228877d2021-02-09 11:05:00 -050030import com.android.launcher3.DevicePaddings.DevicePadding;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070031import com.android.launcher3.graphics.IconShape;
Tony Wickham8912b042018-11-29 15:28:53 -080032import com.android.launcher3.icons.DotRenderer;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070033import com.android.launcher3.icons.IconNormalizer;
Sunny Goyalfd58da62020-08-11 12:06:49 -070034import com.android.launcher3.util.DisplayController;
35import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb46703d2020-05-27 17:52:03 -070036import com.android.launcher3.util.WindowBounds;
Winson1f064272016-07-18 17:18:02 -070037
Winson Chungb3800242013-10-24 11:01:54 -070038public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070039
Sunny Goyal1890f672020-04-30 12:28:00 -070040 private static final float TABLET_MIN_DPS = 600;
41 private static final float LARGE_TABLET_MIN_DPS = 720;
42
43
Adam Cohen2e6da152015-05-06 11:42:25 -070044 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070045 private final Info mInfo;
Winson Chungbe876472014-05-14 14:15:20 -070046
Sunny Goyalc6205602015-05-21 20:46:33 -070047 // Device properties
48 public final boolean isTablet;
49 public final boolean isLargeTablet;
50 public final boolean isPhone;
51 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070052
Sunny Goyalc6205602015-05-21 20:46:33 -070053 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070054 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080055 public final boolean isMultiWindowMode;
56
Sunny Goyal0addbf02020-04-28 14:17:35 -070057 public final int windowX;
58 public final int windowY;
Sunny Goyalc6205602015-05-21 20:46:33 -070059 public final int widthPx;
60 public final int heightPx;
61 public final int availableWidthPx;
62 public final int availableHeightPx;
Jon Mirandabba64512019-03-27 10:54:17 -070063
64 public final float aspectRatio;
65
Tony Wickhamd6b40372015-09-23 18:37:57 -070066 /**
67 * The maximum amount of left/right workspace padding as a percentage of the screen width.
68 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
69 * 7% of the screen width can be used as right padding.
70 */
71 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070072
Jon Miranda3f9bab22017-07-28 14:50:17 -070073 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070074
Jon Miranda5eacbb72018-07-31 11:14:46 -070075 // To evenly space the icons, increase the left/right margins for tablets in portrait mode.
76 private static final int PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER = 4;
77
Sunny Goyalc6205602015-05-21 20:46:33 -070078 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -080079 public final int desiredWorkspaceLeftRightMarginPx;
Jon Miranda228877d2021-02-09 11:05:00 -050080 public final int cellLayoutBorderSpacingPx;
Jon Miranda28032002017-07-13 16:18:56 -070081 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070082 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -070083 public final int edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070084 public float workspaceSpringLoadShrinkFactor;
85 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070086
Jon Miranda228877d2021-02-09 11:05:00 -050087 public int workspaceTopPadding;
88 public int workspaceBottomPadding;
89
Tony Wickham5edf9e22020-03-27 20:06:52 -070090 // Workspace page indicator
91 public final int workspacePageIndicatorHeight;
92 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -070093
Sunny Goyalc6205602015-05-21 20:46:33 -070094 // Workspace icons
95 public int iconSizePx;
96 public int iconTextSizePx;
97 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070099
Adam Cohen59400422014-03-05 18:07:04 -0800100 public int cellWidthPx;
101 public int cellHeightPx;
Jon Miranda228877d2021-02-09 11:05:00 -0500102 public int cellYPaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700103 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800104
Sunny Goyalc6205602015-05-21 20:46:33 -0700105 // Folder
Sunny Goyalc6205602015-05-21 20:46:33 -0700106 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700107 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700108
109 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700110 public int folderCellWidthPx;
111 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700112
113 // Folder child
114 public int folderChildIconSizePx;
115 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700116 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700117
Sunny Goyalc6205602015-05-21 20:46:33 -0700118 // Hotseat
Sunny Goyalc6205602015-05-21 20:46:33 -0700119 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700120 // In portrait: size = height, in landscape: size = width
121 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800122 public final int hotseatBarTopPaddingPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700123 public int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700124 // Start is the side next to the nav bar, end is the side next to the workspace
125 public final int hotseatBarSidePaddingStartPx;
126 public final int hotseatBarSidePaddingEndPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700127
Sunny Goyalc6205602015-05-21 20:46:33 -0700128 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700129 public int allAppsCellHeightPx;
Samuel Fufaf1424a32019-10-04 15:15:19 -0700130 public int allAppsCellWidthPx;
Winson1f064272016-07-18 17:18:02 -0700131 public int allAppsIconSizePx;
132 public int allAppsIconDrawablePaddingPx;
133 public float allAppsIconTextSizePx;
134
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800135 // Widgets
136 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
137
Sunny Goyal47328fd2016-05-25 18:56:41 -0700138 // Drop Target
139 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700140
Winson1f064272016-07-18 17:18:02 -0700141 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800142 private final Rect mInsets = new Rect();
143 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700144 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800145 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700146 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700147
Tony Wickhamf34bee82018-12-03 18:11:39 -0800148 // Notification dots
Samuel Fufac96fa242019-09-26 23:06:32 -0700149 public DotRenderer mDotRendererWorkSpace;
150 public DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800151
Sunny Goyalfd58da62020-08-11 12:06:49 -0700152 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info,
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700153 Point minSize, Point maxSize, int width, int height, boolean isLandscape,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700154 boolean isMultiWindowMode, boolean transposeLayoutWithOrientation,
155 Point windowPosition) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700156
Adam Cohen2e6da152015-05-06 11:42:25 -0700157 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700158 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800159 this.isMultiWindowMode = isMultiWindowMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -0700160 windowX = windowPosition.x;
161 windowY = windowPosition.y;
Winson Chungb3800242013-10-24 11:01:54 -0700162
Jon Miranda7e183c32018-06-20 11:05:27 -0700163 // Determine sizes.
164 widthPx = width;
165 heightPx = height;
166 if (isLandscape) {
167 availableWidthPx = maxSize.x;
168 availableHeightPx = minSize.y;
169 } else {
170 availableWidthPx = minSize.x;
171 availableHeightPx = maxSize.y;
172 }
173
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700174 mInfo = info;
Adam Cohen2e6da152015-05-06 11:42:25 -0700175
Sunny Goyalc6205602015-05-21 20:46:33 -0700176 // Constants from resources
Sunny Goyal1890f672020-04-30 12:28:00 -0700177 float swDPs = Utilities.dpiFromPx(
178 Math.min(info.smallestSize.x, info.smallestSize.y), info.metrics);
Alex Chau4e2c25a2021-02-03 16:20:04 +0000179 boolean allowRotation = context.getResources().getBoolean(R.bool.allow_rotation);
180 // Tablet UI is built with assumption that simulated landscape is disabled.
181 isTablet = allowRotation && swDPs >= TABLET_MIN_DPS;
182 isLargeTablet = isTablet && swDPs >= LARGE_TABLET_MIN_DPS;
Sunny Goyalc6205602015-05-21 20:46:33 -0700183 isPhone = !isTablet && !isLargeTablet;
Jon Mirandabba64512019-03-27 10:54:17 -0700184 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda7e183c32018-06-20 11:05:27 -0700185 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700186
187 // Some more constants
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700188 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Winson Chungb3800242013-10-24 11:01:54 -0700189
Sunny Goyal1890f672020-04-30 12:28:00 -0700190 context = getContext(context, info, isVerticalBarLayout()
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700191 ? Configuration.ORIENTATION_LANDSCAPE
192 : Configuration.ORIENTATION_PORTRAIT);
Sunny Goyal1890f672020-04-30 12:28:00 -0700193 final Resources res = context.getResources();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700194
Winson Chungb3800242013-10-24 11:01:54 -0700195 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Jon Miranda28032002017-07-13 16:18:56 -0700196 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700197
Jon Miranda228877d2021-02-09 11:05:00 -0500198 cellYPaddingPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_y);
199 cellLayoutBorderSpacingPx = isVerticalBarLayout()
200 || isMultiWindowMode
201 || !ENABLE_FOUR_COLUMNS.get()
202 ? 0 : res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_border_spacing);
Jon Miranda5eacbb72018-07-31 11:14:46 -0700203 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
204 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700205 int cellLayoutPadding = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
206 if (isLandscape) {
207 cellLayoutPaddingLeftRightPx = 0;
208 cellLayoutBottomPaddingPx = cellLayoutPadding;
209 } else {
210 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
211 cellLayoutBottomPaddingPx = 0;
212 }
213
Tony Wickham5edf9e22020-03-27 20:06:52 -0700214 workspacePageIndicatorHeight = res.getDimensionPixelSize(
215 R.dimen.workspace_page_indicator_height);
216 mWorkspacePageIndicatorOverlapWorkspace =
217 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700218
Winson Chungb3800242013-10-24 11:01:54 -0700219 iconDrawablePaddingOriginalPx =
220 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700221 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
222 workspaceSpringLoadedBottomSpace =
223 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700224
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700225 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700226
Winson1f064272016-07-18 17:18:02 -0700227 hotseatBarTopPaddingPx =
228 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700229 hotseatBarBottomPaddingPx = (isTallDevice ? 0
230 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
231 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700232 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800233 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700234 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700235 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Jon Miranda228877d2021-02-09 11:05:00 -0500236 int hotseatExtraVerticalSize =
237 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700238 hotseatBarSizePx = ResourceUtils.pxFromDp(inv.iconSize, mInfo.metrics)
239 + (isVerticalBarLayout()
Sunny Goyal415f1732018-11-29 10:33:47 -0800240 ? (hotseatBarSidePaddingStartPx + hotseatBarSidePaddingEndPx)
Jon Miranda228877d2021-02-09 11:05:00 -0500241 : (hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx
242 + (ENABLE_FOUR_COLUMNS.get() ? 0 : hotseatExtraVerticalSize)));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700243
Jon Miranda2ed276e2017-06-29 11:36:34 -0700244 // Calculate all of the remaining variables.
Jon Miranda228877d2021-02-09 11:05:00 -0500245 int extraSpace = updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700246 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Miranda228877d2021-02-09 11:05:00 -0500247 if (ENABLE_FOUR_COLUMNS.get()) {
248 DevicePadding padding = inv.devicePaddings.getDevicePadding(extraSpace);
249 workspaceTopPadding = padding.getWorkspaceTopPadding(extraSpace);
250 workspaceBottomPadding = padding.getWorkspaceBottomPadding(extraSpace);
251
252 float hotseatBarBottomPadding = padding.getHotseatBottomPadding(extraSpace);
253 hotseatBarSizePx += hotseatBarBottomPadding;
254 hotseatBarBottomPaddingPx += hotseatBarBottomPadding;
255 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700256 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700257 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700258 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700259 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda228877d2021-02-09 11:05:00 -0500260 extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2
Tony Wickham5edf9e22020-03-27 20:06:52 -0700261 - workspacePageIndicatorHeight;
Jon Miranda7e183c32018-06-20 11:05:27 -0700262 hotseatBarSizePx += extraSpace;
263 hotseatBarBottomPaddingPx += extraSpace;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700264
Jon Miranda3f9bab22017-07-28 14:50:17 -0700265 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700266 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700267 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800268 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800269
270 // This is done last, after iconSizePx is calculated above.
Samuel Fufac96fa242019-09-26 23:06:32 -0700271 mDotRendererWorkSpace = new DotRenderer(iconSizePx, IconShape.getShapePath(),
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700272 IconShape.DEFAULT_PATH_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700273 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
274 new DotRenderer(allAppsIconSizePx, IconShape.getShapePath(),
275 IconShape.DEFAULT_PATH_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700276 }
277
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700278 public Builder toBuilder(Context context) {
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800279 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700280 return new Builder(context, inv, mInfo)
281 .setSizeRange(size, size)
282 .setSize(widthPx, heightPx)
Sunny Goyal0addbf02020-04-28 14:17:35 -0700283 .setWindowPosition(windowX, windowY)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700284 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800285 }
286
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700287 public DeviceProfile copy(Context context) {
288 return toBuilder(context).build();
289 }
290
291 /**
292 * TODO: Move this to the builder as part of setMultiWindowMode
293 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700294 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700295 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
296 // the system decor is always excluded.
Sunny Goyalb46703d2020-05-27 17:52:03 -0700297 Point mwSize = new Point(Math.min(availableWidthPx, windowBounds.availableSize.x),
298 Math.min(availableHeightPx, windowBounds.availableSize.y));
Jon Mirandaa11380d2017-08-24 11:30:03 -0700299
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700300 DeviceProfile profile = toBuilder(context)
301 .setSizeRange(mwSize, mwSize)
Sunny Goyalb46703d2020-05-27 17:52:03 -0700302 .setSize(windowBounds.bounds.width(), windowBounds.bounds.height())
303 .setWindowPosition(windowBounds.bounds.left, windowBounds.bounds.top)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700304 .setMultiWindowMode(true)
305 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800306
Jon Miranda0cd04572017-09-19 11:31:42 -0700307 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
308 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
309 - iconDrawablePaddingPx - profile.iconTextSizePx;
310 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
311 profile.adjustToHideWorkspaceLabels();
312 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800313
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800314 // We use these scales to measure and layout the widgets using their full invariant profile
315 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
316 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
317 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
318 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800319 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800320
Jon Miranda93e1f042016-11-11 14:13:04 -0800321 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700322 }
323
Adam Cohen63f1ec02014-08-12 09:23:13 -0700324 /**
Sunny Goyalb46703d2020-05-27 17:52:03 -0700325 * Inverse of {@link #getMultiWindowProfile(Context, WindowBounds)}
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800326 * @return device profile corresponding to the current orientation in non multi-window mode.
327 */
328 public DeviceProfile getFullScreenProfile() {
329 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
330 }
331
332 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700333 * Adjusts the profile so that the labels on the Workspace are hidden.
334 * It is important to call this method after the All Apps variables have been set.
335 */
336 private void adjustToHideWorkspaceLabels() {
337 iconTextSizePx = 0;
338 iconDrawablePaddingPx = 0;
339 cellHeightPx = iconSizePx;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700340 autoResizeAllAppsCells();
341 }
Jon Miranda1091e532017-07-21 13:31:50 -0700342
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700343 /**
344 * Re-computes the all-apps cell size to be independent of workspace
345 */
346 public void autoResizeAllAppsCells() {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700347 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700348 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
349 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700350 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700351 }
352
Jon Miranda228877d2021-02-09 11:05:00 -0500353 /**
354 * Returns the amount of extra (or unused) vertical space.
355 */
356 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700357 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800358
Jon Mirandabf7d8122016-11-03 15:29:29 -0700359 // Check to see if the icons fit within the available height. If not, then scale down.
Jon Miranda228877d2021-02-09 11:05:00 -0500360 float usedHeight = (cellHeightPx * inv.numRows)
361 + (cellLayoutBorderSpacingPx * (inv.numRows - 1));
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700362 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Jon Miranda228877d2021-02-09 11:05:00 -0500363 float extraHeight = Math.max(0, maxHeight - usedHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700364 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700365 float scale = maxHeight / usedHeight;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700366 updateIconSize(scale, res);
Jon Miranda228877d2021-02-09 11:05:00 -0500367 extraHeight = 0;
Winson Chungb3800242013-10-24 11:01:54 -0700368 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700369 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500370 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700371 }
372
Jon Miranda6f7e9702019-09-16 14:44:14 -0700373 /**
374 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
375 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
376 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
377 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800378 public void updateIconSize(float scale, Resources res) {
Jon Miranda18751b62017-07-31 17:25:27 -0700379 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800380 final boolean isVerticalLayout = isVerticalBarLayout();
Jon Miranda6f7e9702019-09-16 14:44:14 -0700381 float invIconSizeDp = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700382 iconSizePx = Math.max(1, (int) (ResourceUtils.pxFromDp(invIconSizeDp, mInfo.metrics)
383 * scale));
384 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, mInfo.metrics) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700385 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700386
Jon Miranda228877d2021-02-09 11:05:00 -0500387 if (ENABLE_FOUR_COLUMNS.get()) {
388 cellHeightPx = iconSizePx + iconDrawablePaddingPx
389 + Utilities.calculateTextHeight(iconTextSizePx)
390 + (cellYPaddingPx * 2);
391 } else {
392 cellYPaddingPx = 0;
393 cellHeightPx = iconSizePx + iconDrawablePaddingPx
394 + Utilities.calculateTextHeight(iconTextSizePx);
395 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
396 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
397 && !isMultiWindowMode) {
398 // Ensures that the label is closer to its corresponding icon. This is not an issue
399 // with vertical bar layout or multi-window mode since the issue is handled
400 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
401 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
402 iconDrawablePaddingPx = cellPaddingY;
403 }
Jon Miranda846455e2017-10-02 14:58:52 -0700404 }
405 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700406
Sunny Goyalae190ff2020-04-14 00:19:01 +0000407 // All apps
408 if (allAppsHasDifferentNumColumns()) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700409 allAppsIconSizePx = ResourceUtils.pxFromDp(inv.allAppsIconSize, mInfo.metrics);
410 allAppsIconTextSizePx = Utilities.pxFromSp(inv.allAppsIconTextSize, mInfo.metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000411 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda35359442020-07-09 14:54:23 -0700412 // We use 4 below to ensure labels are closer to their corresponding icon.
413 allAppsCellHeightPx = Math.round(allAppsIconSizePx + allAppsIconTextSizePx
414 + (4 * allAppsIconDrawablePaddingPx));
Sunny Goyalae190ff2020-04-14 00:19:01 +0000415 } else {
416 allAppsIconSizePx = iconSizePx;
417 allAppsIconTextSizePx = iconTextSizePx;
418 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
419 allAppsCellHeightPx = getCellSize().y;
420 }
421 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700422
Jon Miranda6f7e9702019-09-16 14:44:14 -0700423 if (isVerticalBarLayout()) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700424 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700425 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700426 }
Winson Chungb3800242013-10-24 11:01:54 -0700427
Winson Chungb3800242013-10-24 11:01:54 -0700428 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800429 if (isVerticalLayout) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700430 hotseatBarSizePx = iconSizePx + hotseatBarSidePaddingStartPx
431 + hotseatBarSidePaddingEndPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700432 }
Jon Miranda5044c352017-08-09 11:37:59 -0700433 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700434
Sunny Goyal07b69292018-01-08 14:19:34 -0800435 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700436 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700437 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700438 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
439 workspaceSpringLoadShrinkFactor = Math.min(
440 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
441 1 - (minRequiredHeight / expectedWorkspaceHeight));
442 } else {
443 workspaceSpringLoadShrinkFactor =
444 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
445 }
446
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700447 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700448 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
449 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700450 }
451
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700452 private void updateAvailableFolderCellDimensions(Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700453 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
454 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
455 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
456
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700457 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700458
Jon Mirandac1b23992016-12-13 08:57:36 -0800459 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700460 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800461 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800462
463 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500464 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
465 + ((inv.numFolderRows - 1) * cellLayoutBorderSpacingPx);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800466 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
467 - folderMargin;
468 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800469
470 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500471 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
472 + ((inv.numFolderColumns - 1) * cellLayoutBorderSpacingPx);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800473 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin;
474 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800475
476 float scale = Math.min(scaleX, scaleY);
477 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700478 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700479 }
480 }
481
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700482 private void updateFolderCellSize(float scale, Resources res) {
483 folderChildIconSizePx = (int) (ResourceUtils.pxFromDp(inv.iconSize, mInfo.metrics) * scale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700484 folderChildTextSizePx =
485 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
486
487 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
488 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
489 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
490
Jon Mirandac1b23992016-12-13 08:57:36 -0800491 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000492 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
493 folderChildDrawablePaddingPx = Math.max(0,
494 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700495 }
496
Winson1f064272016-07-18 17:18:02 -0700497 public void updateInsets(Rect insets) {
498 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800499 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700500 }
501
Sunny Goyalae6e3182019-04-30 12:04:37 -0700502 /**
503 * The current device insets. This is generally same as the insets being dispatched to
504 * {@link Insettable} elements, but can differ if the element is using a different profile.
505 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800506 public Rect getInsets() {
507 return mInsets;
508 }
509
Sunny Goyal756cd262015-08-20 12:33:21 -0700510 public Point getCellSize() {
Jon Miranda6f7e9702019-09-16 14:44:14 -0700511 return getCellSize(inv.numColumns, inv.numRows);
512 }
513
514 private Point getCellSize(int numColumns, int numRows) {
Sunny Goyal756cd262015-08-20 12:33:21 -0700515 Point result = new Point();
516 // Since we are only concerned with the overall padding, layout direction does
517 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700518 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700519 result.x = calculateCellWidth(availableWidthPx - padding.x
Jon Miranda228877d2021-02-09 11:05:00 -0500520 - cellLayoutPaddingLeftRightPx * 2, cellLayoutBorderSpacingPx, numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700521 result.y = calculateCellHeight(availableHeightPx - padding.y
Jon Miranda228877d2021-02-09 11:05:00 -0500522 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacingPx, numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700523 return result;
524 }
525
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700526 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800527 updateWorkspacePadding();
528 return new Point(workspacePadding.left + workspacePadding.right,
529 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700530 }
531
532 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800533 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
534 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700535 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800536 private void updateWorkspacePadding() {
537 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700538 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800539 padding.top = 0;
540 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800541 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700542 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700543 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700544 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700545 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700546 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700547 }
Winson Chungb3800242013-10-24 11:01:54 -0700548 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700549 int paddingBottom = hotseatBarSizePx + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500550 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700551 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700552 // Pad the left and right of the workspace to ensure consistent spacing
553 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700554 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800555 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800556 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700557 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800558 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700559 int availablePaddingY = Math.max(0, heightPx - edgeMarginPx - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800560 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
561 - hotseatBarBottomPaddingPx);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700562 padding.set(availablePaddingX / 2, edgeMarginPx + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700563 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700564 } else {
565 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700566 padding.set(desiredWorkspaceLeftRightMarginPx,
Jon Miranda228877d2021-02-09 11:05:00 -0500567 workspaceTopPadding + edgeMarginPx,
Winson1f064272016-07-18 17:18:02 -0700568 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700569 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700570 }
571 }
Winson Chungb3800242013-10-24 11:01:54 -0700572 }
573
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700574 public Rect getHotseatLayoutPadding() {
575 if (isVerticalBarLayout()) {
576 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700577 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
578 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700579 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700580 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
581 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700582 }
583 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700584 // We want the edges of the hotseat to line up with the edges of the workspace, but the
585 // icons in the hotseat are a different size, and so don't line up perfectly. To account
586 // for this, we pad the left and right of the hotseat with half of the difference of a
587 // workspace cell vs a hotseat cell.
588 float workspaceCellWidth = (float) widthPx / inv.numColumns;
589 float hotseatCellWidth = (float) widthPx / inv.numHotseatIcons;
590 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
591 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700592 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
593 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700594 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700595 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
596 + mInsets.right,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700597 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
598 }
599 return mHotseatPadding;
600 }
601
Winsonfadbe8f2016-07-22 16:35:37 -0700602 /**
603 * @return the bounds for which the open folders should be contained within
604 */
605 public Rect getAbsoluteOpenFolderBounds() {
606 if (isVerticalBarLayout()) {
607 // Folders should only appear right of the drop target bar and left of the hotseat
608 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
609 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700610 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700611 mInsets.top + availableHeightPx);
612 } else {
613 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800614 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700615 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800616 mInsets.left + availableWidthPx - edgeMarginPx,
Jon Miranda18751b62017-07-31 17:25:27 -0700617 mInsets.top + availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700618 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700619 }
620 }
621
Jon Miranda228877d2021-02-09 11:05:00 -0500622 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
623 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700624 }
Jon Miranda228877d2021-02-09 11:05:00 -0500625 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
626 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700627 }
628
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700629 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700630 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
631 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
632 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700633 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700634 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700635 return isLandscape && transposeLayoutWithOrientation;
636 }
637
Sunny Goyal59d086c2018-05-07 17:31:40 -0700638 /**
Jon Miranda6f7e9702019-09-16 14:44:14 -0700639 * Returns true when the number of workspace columns and all apps columns differs.
640 */
641 private boolean allAppsHasDifferentNumColumns() {
642 return inv.numAllAppsColumns != inv.numColumns;
643 }
644
645 /**
Sunny Goyal59d086c2018-05-07 17:31:40 -0700646 * Updates orientation information and returns true if it has changed from the previous value.
647 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700648 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700649 if (isVerticalBarLayout()) {
Schneider Victor-tulias954bb632021-01-27 14:03:51 -0800650 // Check an up-to-date info.
Alex Chaud25e87a2021-02-12 12:12:56 +0000651 DisplayController.Info displayInfo = DisplayController.getDefaultDisplay(context)
652 .createInfoForContext(context);
653 if (displayInfo == null) {
654 return false;
655 }
656
657 boolean isSeascape = displayInfo.rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700658 if (mIsSeascape != isSeascape) {
659 mIsSeascape = isSeascape;
660 return true;
661 }
662 }
663 return false;
664 }
665
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800666 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700667 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800668 }
669
Sunny Goyal07b69292018-01-08 14:19:34 -0800670 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700671 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700672 }
673
Sunny Goyalc13403c2016-11-18 23:44:48 -0800674 public int getCellHeight(@ContainerType int containerType) {
675 switch (containerType) {
676 case CellLayout.WORKSPACE:
677 return cellHeightPx;
678 case CellLayout.FOLDER:
679 return folderCellHeightPx;
680 case CellLayout.HOTSEAT:
681 return hotseatCellHeightPx;
682 default:
683 // ??
684 return 0;
685 }
686 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700687
Sunny Goyalfd58da62020-08-11 12:06:49 -0700688 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -0700689 Configuration config = new Configuration(c.getResources().getConfiguration());
690 config.orientation = orientation;
691 config.densityDpi = info.metrics.densityDpi;
692 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700693 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800694
695 /**
696 * Callback when a component changes the DeviceProfile associated with it, as a result of
697 * configuration change
698 */
699 public interface OnDeviceProfileChangeListener {
700
701 /**
702 * Called when the device profile is reassigned. Note that for layout and measurements, it
703 * is sufficient to listen for inset changes. Use this callback when you need to perform
704 * a one time operation.
705 */
706 void onDeviceProfileChanged(DeviceProfile dp);
707 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700708
709 public static class Builder {
710 private Context mContext;
711 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -0700712 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700713
Sunny Goyal0addbf02020-04-28 14:17:35 -0700714 private final Point mWindowPosition = new Point();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700715 private Point mMinSize, mMaxSize;
716 private int mWidth, mHeight;
717
718 private boolean mIsLandscape;
719 private boolean mIsMultiWindowMode = false;
720 private boolean mTransposeLayoutWithOrientation;
721
Sunny Goyalfd58da62020-08-11 12:06:49 -0700722 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700723 mContext = context;
724 mInv = inv;
725 mInfo = info;
726 mTransposeLayoutWithOrientation = context.getResources()
727 .getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
728 }
729
730 public Builder setSizeRange(Point minSize, Point maxSize) {
731 mMinSize = minSize;
732 mMaxSize = maxSize;
733 return this;
734 }
735
736 public Builder setSize(int width, int height) {
737 mWidth = width;
738 mHeight = height;
739 mIsLandscape = mWidth > mHeight;
740 return this;
741 }
742
743 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
744 mIsMultiWindowMode = isMultiWindowMode;
745 return this;
746 }
747
Sunny Goyal0addbf02020-04-28 14:17:35 -0700748 /**
749 * Sets the window position if not full-screen
750 */
751 public Builder setWindowPosition(int x, int y) {
752 mWindowPosition.set(x, y);
753 return this;
754 }
755
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700756 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
757 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
758 return this;
759 }
760
761 public DeviceProfile build() {
762 return new DeviceProfile(mContext, mInv, mInfo, mMinSize, mMaxSize,
763 mWidth, mHeight, mIsLandscape, mIsMultiWindowMode,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700764 mTransposeLayoutWithOrientation, mWindowPosition);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700765 }
766 }
767
Winson Chungb3800242013-10-24 11:01:54 -0700768}