blob: f2b463801880387412a79c41ffe6b7714368fc0b [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;
Tony Wickham15883892021-02-12 11:24:40 -080028import android.view.WindowInsets;
29import android.view.WindowManager;
Winson Chungb3800242013-10-24 11:01:54 -070030
Sunny Goyalc13403c2016-11-18 23:44:48 -080031import com.android.launcher3.CellLayout.ContainerType;
Jon Miranda228877d2021-02-09 11:05:00 -050032import com.android.launcher3.DevicePaddings.DevicePadding;
Tony Wickham15883892021-02-12 11:24:40 -080033import com.android.launcher3.config.FeatureFlags;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070034import com.android.launcher3.graphics.IconShape;
Tony Wickham8912b042018-11-29 15:28:53 -080035import com.android.launcher3.icons.DotRenderer;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070036import com.android.launcher3.icons.IconNormalizer;
Sunny Goyalfd58da62020-08-11 12:06:49 -070037import com.android.launcher3.util.DisplayController;
38import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb46703d2020-05-27 17:52:03 -070039import com.android.launcher3.util.WindowBounds;
Winson1f064272016-07-18 17:18:02 -070040
Winson Chungb3800242013-10-24 11:01:54 -070041public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070042
Sunny Goyal1890f672020-04-30 12:28:00 -070043 private static final float TABLET_MIN_DPS = 600;
44 private static final float LARGE_TABLET_MIN_DPS = 720;
45
46
Adam Cohen2e6da152015-05-06 11:42:25 -070047 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070048 private final Info mInfo;
Winson Chungbe876472014-05-14 14:15:20 -070049
Sunny Goyalc6205602015-05-21 20:46:33 -070050 // Device properties
51 public final boolean isTablet;
52 public final boolean isLargeTablet;
53 public final boolean isPhone;
54 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070055
Sunny Goyalc6205602015-05-21 20:46:33 -070056 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070057 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080058 public final boolean isMultiWindowMode;
59
Sunny Goyal0addbf02020-04-28 14:17:35 -070060 public final int windowX;
61 public final int windowY;
Sunny Goyalc6205602015-05-21 20:46:33 -070062 public final int widthPx;
63 public final int heightPx;
64 public final int availableWidthPx;
65 public final int availableHeightPx;
Jon Mirandabba64512019-03-27 10:54:17 -070066
67 public final float aspectRatio;
68
Tony Wickhamd6b40372015-09-23 18:37:57 -070069 /**
70 * The maximum amount of left/right workspace padding as a percentage of the screen width.
71 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
72 * 7% of the screen width can be used as right padding.
73 */
74 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070075
Jon Miranda3f9bab22017-07-28 14:50:17 -070076 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070077
Jon Miranda5eacbb72018-07-31 11:14:46 -070078 // To evenly space the icons, increase the left/right margins for tablets in portrait mode.
79 private static final int PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER = 4;
80
Sunny Goyalc6205602015-05-21 20:46:33 -070081 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -080082 public final int desiredWorkspaceLeftRightMarginPx;
Jon Miranda228877d2021-02-09 11:05:00 -050083 public final int cellLayoutBorderSpacingPx;
Jon Miranda28032002017-07-13 16:18:56 -070084 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070085 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -070086 public final int edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070087 public float workspaceSpringLoadShrinkFactor;
88 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070089
Jon Miranda228877d2021-02-09 11:05:00 -050090 public int workspaceTopPadding;
91 public int workspaceBottomPadding;
92
Tony Wickham5edf9e22020-03-27 20:06:52 -070093 // Workspace page indicator
94 public final int workspacePageIndicatorHeight;
95 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -070096
Sunny Goyalc6205602015-05-21 20:46:33 -070097 // Workspace icons
98 public int iconSizePx;
99 public int iconTextSizePx;
100 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700101 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700102
Adam Cohen59400422014-03-05 18:07:04 -0800103 public int cellWidthPx;
104 public int cellHeightPx;
Jon Miranda228877d2021-02-09 11:05:00 -0500105 public int cellYPaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700106 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800107
Sunny Goyalc6205602015-05-21 20:46:33 -0700108 // Folder
Sunny Goyalc6205602015-05-21 20:46:33 -0700109 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700110 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700111
112 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700113 public int folderCellWidthPx;
114 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700115
116 // Folder child
117 public int folderChildIconSizePx;
118 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700119 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700120
Sunny Goyalc6205602015-05-21 20:46:33 -0700121 // Hotseat
Sunny Goyalc6205602015-05-21 20:46:33 -0700122 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700123 // In portrait: size = height, in landscape: size = width
124 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800125 public final int hotseatBarTopPaddingPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700126 public int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700127 // Start is the side next to the nav bar, end is the side next to the workspace
128 public final int hotseatBarSidePaddingStartPx;
129 public final int hotseatBarSidePaddingEndPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700130
Sunny Goyalc6205602015-05-21 20:46:33 -0700131 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700132 public int allAppsCellHeightPx;
Samuel Fufaf1424a32019-10-04 15:15:19 -0700133 public int allAppsCellWidthPx;
Winson1f064272016-07-18 17:18:02 -0700134 public int allAppsIconSizePx;
135 public int allAppsIconDrawablePaddingPx;
136 public float allAppsIconTextSizePx;
137
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800138 // Widgets
139 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
140
Sunny Goyal47328fd2016-05-25 18:56:41 -0700141 // Drop Target
142 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700143
Winson1f064272016-07-18 17:18:02 -0700144 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800145 private final Rect mInsets = new Rect();
146 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700147 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800148 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700149 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700150
Tony Wickhamf34bee82018-12-03 18:11:39 -0800151 // Notification dots
Samuel Fufac96fa242019-09-26 23:06:32 -0700152 public DotRenderer mDotRendererWorkSpace;
153 public DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800154
Tony Wickham15883892021-02-12 11:24:40 -0800155 // Taskbar
156 public boolean isTaskbarPresent;
157 public int taskbarSize;
158
Sunny Goyalfd58da62020-08-11 12:06:49 -0700159 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info,
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700160 Point minSize, Point maxSize, int width, int height, boolean isLandscape,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700161 boolean isMultiWindowMode, boolean transposeLayoutWithOrientation,
162 Point windowPosition) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700163
Adam Cohen2e6da152015-05-06 11:42:25 -0700164 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700165 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800166 this.isMultiWindowMode = isMultiWindowMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -0700167 windowX = windowPosition.x;
168 windowY = windowPosition.y;
Winson Chungb3800242013-10-24 11:01:54 -0700169
Jon Miranda7e183c32018-06-20 11:05:27 -0700170 // Determine sizes.
171 widthPx = width;
172 heightPx = height;
Tony Wickham15883892021-02-12 11:24:40 -0800173 int nonFinalAvailableHeightPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700174 if (isLandscape) {
175 availableWidthPx = maxSize.x;
Tony Wickham15883892021-02-12 11:24:40 -0800176 nonFinalAvailableHeightPx = minSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700177 } else {
178 availableWidthPx = minSize.x;
Tony Wickham15883892021-02-12 11:24:40 -0800179 nonFinalAvailableHeightPx = maxSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700180 }
181
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700182 mInfo = info;
Adam Cohen2e6da152015-05-06 11:42:25 -0700183
Sunny Goyalc6205602015-05-21 20:46:33 -0700184 // Constants from resources
Sunny Goyal1890f672020-04-30 12:28:00 -0700185 float swDPs = Utilities.dpiFromPx(
186 Math.min(info.smallestSize.x, info.smallestSize.y), info.metrics);
Alex Chau4e2c25a2021-02-03 16:20:04 +0000187 boolean allowRotation = context.getResources().getBoolean(R.bool.allow_rotation);
188 // Tablet UI is built with assumption that simulated landscape is disabled.
189 isTablet = allowRotation && swDPs >= TABLET_MIN_DPS;
190 isLargeTablet = isTablet && swDPs >= LARGE_TABLET_MIN_DPS;
Sunny Goyalc6205602015-05-21 20:46:33 -0700191 isPhone = !isTablet && !isLargeTablet;
Jon Mirandabba64512019-03-27 10:54:17 -0700192 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda7e183c32018-06-20 11:05:27 -0700193 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700194
195 // Some more constants
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700196 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Winson Chungb3800242013-10-24 11:01:54 -0700197
Sunny Goyal1890f672020-04-30 12:28:00 -0700198 context = getContext(context, info, isVerticalBarLayout()
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700199 ? Configuration.ORIENTATION_LANDSCAPE
200 : Configuration.ORIENTATION_PORTRAIT);
Sunny Goyal1890f672020-04-30 12:28:00 -0700201 final Resources res = context.getResources();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700202
Tony Wickham15883892021-02-12 11:24:40 -0800203 isTaskbarPresent = isTablet && FeatureFlags.ENABLE_TASKBAR.get();
204 if (isTaskbarPresent) {
205 // Taskbar will be added later, but provides bottom insets that we should subtract
206 // from availableHeightPx.
207 taskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_size);
208 WindowInsets windowInsets = DisplayController.INSTANCE.get(context).getHolder(mInfo.id)
209 .getDisplayContext().getSystemService(WindowManager.class)
210 .getCurrentWindowMetrics().getWindowInsets();
211 int nonOverlappingTaskbarInset =
212 taskbarSize - windowInsets.getSystemWindowInsetBottom();
213 if (nonOverlappingTaskbarInset > 0) {
214 nonFinalAvailableHeightPx -= nonOverlappingTaskbarInset;
215 }
216 }
217 availableHeightPx = nonFinalAvailableHeightPx;
218
Winson Chungb3800242013-10-24 11:01:54 -0700219 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Jon Miranda28032002017-07-13 16:18:56 -0700220 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700221
Jon Miranda228877d2021-02-09 11:05:00 -0500222 cellYPaddingPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_y);
223 cellLayoutBorderSpacingPx = isVerticalBarLayout()
224 || isMultiWindowMode
225 || !ENABLE_FOUR_COLUMNS.get()
226 ? 0 : res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_border_spacing);
Jon Miranda5eacbb72018-07-31 11:14:46 -0700227 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
228 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700229 int cellLayoutPadding = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
230 if (isLandscape) {
231 cellLayoutPaddingLeftRightPx = 0;
232 cellLayoutBottomPaddingPx = cellLayoutPadding;
233 } else {
234 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
235 cellLayoutBottomPaddingPx = 0;
236 }
237
Tony Wickham5edf9e22020-03-27 20:06:52 -0700238 workspacePageIndicatorHeight = res.getDimensionPixelSize(
239 R.dimen.workspace_page_indicator_height);
240 mWorkspacePageIndicatorOverlapWorkspace =
241 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700242
Winson Chungb3800242013-10-24 11:01:54 -0700243 iconDrawablePaddingOriginalPx =
244 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700245 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
246 workspaceSpringLoadedBottomSpace =
247 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700248
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700249 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700250
Winson1f064272016-07-18 17:18:02 -0700251 hotseatBarTopPaddingPx =
252 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700253 hotseatBarBottomPaddingPx = (isTallDevice ? 0
254 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
255 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700256 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800257 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700258 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700259 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Jon Miranda228877d2021-02-09 11:05:00 -0500260 int hotseatExtraVerticalSize =
261 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700262 hotseatBarSizePx = ResourceUtils.pxFromDp(inv.iconSize, mInfo.metrics)
263 + (isVerticalBarLayout()
Sunny Goyal415f1732018-11-29 10:33:47 -0800264 ? (hotseatBarSidePaddingStartPx + hotseatBarSidePaddingEndPx)
Jon Miranda228877d2021-02-09 11:05:00 -0500265 : (hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx
266 + (ENABLE_FOUR_COLUMNS.get() ? 0 : hotseatExtraVerticalSize)));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700267
Jon Miranda2ed276e2017-06-29 11:36:34 -0700268 // Calculate all of the remaining variables.
Jon Miranda228877d2021-02-09 11:05:00 -0500269 int extraSpace = updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700270 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Miranda228877d2021-02-09 11:05:00 -0500271 if (ENABLE_FOUR_COLUMNS.get()) {
272 DevicePadding padding = inv.devicePaddings.getDevicePadding(extraSpace);
273 workspaceTopPadding = padding.getWorkspaceTopPadding(extraSpace);
274 workspaceBottomPadding = padding.getWorkspaceBottomPadding(extraSpace);
275
276 float hotseatBarBottomPadding = padding.getHotseatBottomPadding(extraSpace);
277 hotseatBarSizePx += hotseatBarBottomPadding;
278 hotseatBarBottomPaddingPx += hotseatBarBottomPadding;
279 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700280 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700281 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700282 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700283 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda228877d2021-02-09 11:05:00 -0500284 extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2
Tony Wickham5edf9e22020-03-27 20:06:52 -0700285 - workspacePageIndicatorHeight;
Jon Miranda7e183c32018-06-20 11:05:27 -0700286 hotseatBarSizePx += extraSpace;
287 hotseatBarBottomPaddingPx += extraSpace;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700288
Jon Miranda3f9bab22017-07-28 14:50:17 -0700289 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700290 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700291 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800292 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800293
294 // This is done last, after iconSizePx is calculated above.
Samuel Fufac96fa242019-09-26 23:06:32 -0700295 mDotRendererWorkSpace = new DotRenderer(iconSizePx, IconShape.getShapePath(),
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700296 IconShape.DEFAULT_PATH_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700297 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
298 new DotRenderer(allAppsIconSizePx, IconShape.getShapePath(),
299 IconShape.DEFAULT_PATH_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700300 }
301
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700302 public Builder toBuilder(Context context) {
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800303 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700304 return new Builder(context, inv, mInfo)
305 .setSizeRange(size, size)
306 .setSize(widthPx, heightPx)
Sunny Goyal0addbf02020-04-28 14:17:35 -0700307 .setWindowPosition(windowX, windowY)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700308 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800309 }
310
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700311 public DeviceProfile copy(Context context) {
312 return toBuilder(context).build();
313 }
314
315 /**
316 * TODO: Move this to the builder as part of setMultiWindowMode
317 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700318 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700319 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
320 // the system decor is always excluded.
Sunny Goyalb46703d2020-05-27 17:52:03 -0700321 Point mwSize = new Point(Math.min(availableWidthPx, windowBounds.availableSize.x),
322 Math.min(availableHeightPx, windowBounds.availableSize.y));
Jon Mirandaa11380d2017-08-24 11:30:03 -0700323
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700324 DeviceProfile profile = toBuilder(context)
325 .setSizeRange(mwSize, mwSize)
Sunny Goyalb46703d2020-05-27 17:52:03 -0700326 .setSize(windowBounds.bounds.width(), windowBounds.bounds.height())
327 .setWindowPosition(windowBounds.bounds.left, windowBounds.bounds.top)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700328 .setMultiWindowMode(true)
329 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800330
Jon Miranda0cd04572017-09-19 11:31:42 -0700331 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
332 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
333 - iconDrawablePaddingPx - profile.iconTextSizePx;
334 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
335 profile.adjustToHideWorkspaceLabels();
336 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800337
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800338 // We use these scales to measure and layout the widgets using their full invariant profile
339 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
340 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
341 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
342 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800343 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800344
Jon Miranda93e1f042016-11-11 14:13:04 -0800345 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700346 }
347
Adam Cohen63f1ec02014-08-12 09:23:13 -0700348 /**
Sunny Goyalb46703d2020-05-27 17:52:03 -0700349 * Inverse of {@link #getMultiWindowProfile(Context, WindowBounds)}
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800350 * @return device profile corresponding to the current orientation in non multi-window mode.
351 */
352 public DeviceProfile getFullScreenProfile() {
353 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
354 }
355
356 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700357 * Adjusts the profile so that the labels on the Workspace are hidden.
358 * It is important to call this method after the All Apps variables have been set.
359 */
360 private void adjustToHideWorkspaceLabels() {
361 iconTextSizePx = 0;
362 iconDrawablePaddingPx = 0;
363 cellHeightPx = iconSizePx;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700364 autoResizeAllAppsCells();
365 }
Jon Miranda1091e532017-07-21 13:31:50 -0700366
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700367 /**
368 * Re-computes the all-apps cell size to be independent of workspace
369 */
370 public void autoResizeAllAppsCells() {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700371 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700372 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
373 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700374 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700375 }
376
Jon Miranda228877d2021-02-09 11:05:00 -0500377 /**
378 * Returns the amount of extra (or unused) vertical space.
379 */
380 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700381 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800382
Jon Mirandabf7d8122016-11-03 15:29:29 -0700383 // Check to see if the icons fit within the available height. If not, then scale down.
Jon Miranda228877d2021-02-09 11:05:00 -0500384 float usedHeight = (cellHeightPx * inv.numRows)
385 + (cellLayoutBorderSpacingPx * (inv.numRows - 1));
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700386 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Jon Miranda228877d2021-02-09 11:05:00 -0500387 float extraHeight = Math.max(0, maxHeight - usedHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700388 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700389 float scale = maxHeight / usedHeight;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700390 updateIconSize(scale, res);
Jon Miranda228877d2021-02-09 11:05:00 -0500391 extraHeight = 0;
Winson Chungb3800242013-10-24 11:01:54 -0700392 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700393 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500394 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700395 }
396
Jon Miranda6f7e9702019-09-16 14:44:14 -0700397 /**
398 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
399 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
400 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
401 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800402 public void updateIconSize(float scale, Resources res) {
Jon Miranda18751b62017-07-31 17:25:27 -0700403 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800404 final boolean isVerticalLayout = isVerticalBarLayout();
Jon Miranda6f7e9702019-09-16 14:44:14 -0700405 float invIconSizeDp = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700406 iconSizePx = Math.max(1, (int) (ResourceUtils.pxFromDp(invIconSizeDp, mInfo.metrics)
407 * scale));
408 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, mInfo.metrics) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700409 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700410
Jon Miranda228877d2021-02-09 11:05:00 -0500411 if (ENABLE_FOUR_COLUMNS.get()) {
412 cellHeightPx = iconSizePx + iconDrawablePaddingPx
413 + Utilities.calculateTextHeight(iconTextSizePx)
414 + (cellYPaddingPx * 2);
415 } else {
416 cellYPaddingPx = 0;
417 cellHeightPx = iconSizePx + iconDrawablePaddingPx
418 + Utilities.calculateTextHeight(iconTextSizePx);
419 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
420 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
421 && !isMultiWindowMode) {
422 // Ensures that the label is closer to its corresponding icon. This is not an issue
423 // with vertical bar layout or multi-window mode since the issue is handled
424 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
425 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
426 iconDrawablePaddingPx = cellPaddingY;
427 }
Jon Miranda846455e2017-10-02 14:58:52 -0700428 }
429 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700430
Sunny Goyalae190ff2020-04-14 00:19:01 +0000431 // All apps
432 if (allAppsHasDifferentNumColumns()) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700433 allAppsIconSizePx = ResourceUtils.pxFromDp(inv.allAppsIconSize, mInfo.metrics);
434 allAppsIconTextSizePx = Utilities.pxFromSp(inv.allAppsIconTextSize, mInfo.metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000435 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda35359442020-07-09 14:54:23 -0700436 // We use 4 below to ensure labels are closer to their corresponding icon.
437 allAppsCellHeightPx = Math.round(allAppsIconSizePx + allAppsIconTextSizePx
438 + (4 * allAppsIconDrawablePaddingPx));
Sunny Goyalae190ff2020-04-14 00:19:01 +0000439 } else {
440 allAppsIconSizePx = iconSizePx;
441 allAppsIconTextSizePx = iconTextSizePx;
442 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
443 allAppsCellHeightPx = getCellSize().y;
444 }
445 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700446
Jon Miranda6f7e9702019-09-16 14:44:14 -0700447 if (isVerticalBarLayout()) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700448 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700449 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700450 }
Winson Chungb3800242013-10-24 11:01:54 -0700451
Winson Chungb3800242013-10-24 11:01:54 -0700452 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800453 if (isVerticalLayout) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700454 hotseatBarSizePx = iconSizePx + hotseatBarSidePaddingStartPx
455 + hotseatBarSidePaddingEndPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700456 }
Jon Miranda5044c352017-08-09 11:37:59 -0700457 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700458
Sunny Goyal07b69292018-01-08 14:19:34 -0800459 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700460 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700461 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700462 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
463 workspaceSpringLoadShrinkFactor = Math.min(
464 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
465 1 - (minRequiredHeight / expectedWorkspaceHeight));
466 } else {
467 workspaceSpringLoadShrinkFactor =
468 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
469 }
470
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700471 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700472 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
473 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700474 }
475
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700476 private void updateAvailableFolderCellDimensions(Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700477 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
478 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
479 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
480
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700481 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700482
Jon Mirandac1b23992016-12-13 08:57:36 -0800483 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700484 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800485 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800486
487 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500488 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
489 + ((inv.numFolderRows - 1) * cellLayoutBorderSpacingPx);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800490 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
491 - folderMargin;
492 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800493
494 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500495 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
496 + ((inv.numFolderColumns - 1) * cellLayoutBorderSpacingPx);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800497 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin;
498 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800499
500 float scale = Math.min(scaleX, scaleY);
501 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700502 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700503 }
504 }
505
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700506 private void updateFolderCellSize(float scale, Resources res) {
507 folderChildIconSizePx = (int) (ResourceUtils.pxFromDp(inv.iconSize, mInfo.metrics) * scale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700508 folderChildTextSizePx =
509 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
510
511 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
512 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
513 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
514
Jon Mirandac1b23992016-12-13 08:57:36 -0800515 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000516 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
517 folderChildDrawablePaddingPx = Math.max(0,
518 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700519 }
520
Winson1f064272016-07-18 17:18:02 -0700521 public void updateInsets(Rect insets) {
522 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800523 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700524 }
525
Sunny Goyalae6e3182019-04-30 12:04:37 -0700526 /**
527 * The current device insets. This is generally same as the insets being dispatched to
528 * {@link Insettable} elements, but can differ if the element is using a different profile.
529 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800530 public Rect getInsets() {
531 return mInsets;
532 }
533
Sunny Goyal756cd262015-08-20 12:33:21 -0700534 public Point getCellSize() {
Jon Miranda6f7e9702019-09-16 14:44:14 -0700535 return getCellSize(inv.numColumns, inv.numRows);
536 }
537
538 private Point getCellSize(int numColumns, int numRows) {
Sunny Goyal756cd262015-08-20 12:33:21 -0700539 Point result = new Point();
540 // Since we are only concerned with the overall padding, layout direction does
541 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700542 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700543 result.x = calculateCellWidth(availableWidthPx - padding.x
Jon Miranda228877d2021-02-09 11:05:00 -0500544 - cellLayoutPaddingLeftRightPx * 2, cellLayoutBorderSpacingPx, numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700545 result.y = calculateCellHeight(availableHeightPx - padding.y
Jon Miranda228877d2021-02-09 11:05:00 -0500546 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacingPx, numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700547 return result;
548 }
549
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700550 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800551 updateWorkspacePadding();
552 return new Point(workspacePadding.left + workspacePadding.right,
553 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700554 }
555
556 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800557 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
558 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700559 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800560 private void updateWorkspacePadding() {
561 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700562 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800563 padding.top = 0;
564 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800565 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700566 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700567 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700568 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700569 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700570 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700571 }
Winson Chungb3800242013-10-24 11:01:54 -0700572 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700573 int paddingBottom = hotseatBarSizePx + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500574 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700575 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700576 // Pad the left and right of the workspace to ensure consistent spacing
577 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700578 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800579 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800580 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700581 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800582 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700583 int availablePaddingY = Math.max(0, heightPx - edgeMarginPx - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800584 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
585 - hotseatBarBottomPaddingPx);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700586 padding.set(availablePaddingX / 2, edgeMarginPx + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700587 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700588 } else {
589 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700590 padding.set(desiredWorkspaceLeftRightMarginPx,
Jon Miranda228877d2021-02-09 11:05:00 -0500591 workspaceTopPadding + edgeMarginPx,
Winson1f064272016-07-18 17:18:02 -0700592 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700593 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700594 }
595 }
Winson Chungb3800242013-10-24 11:01:54 -0700596 }
597
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700598 public Rect getHotseatLayoutPadding() {
599 if (isVerticalBarLayout()) {
600 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700601 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
602 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700603 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700604 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
605 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700606 }
607 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700608 // We want the edges of the hotseat to line up with the edges of the workspace, but the
609 // icons in the hotseat are a different size, and so don't line up perfectly. To account
610 // for this, we pad the left and right of the hotseat with half of the difference of a
611 // workspace cell vs a hotseat cell.
612 float workspaceCellWidth = (float) widthPx / inv.numColumns;
613 float hotseatCellWidth = (float) widthPx / inv.numHotseatIcons;
614 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
615 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700616 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
617 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700618 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700619 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
620 + mInsets.right,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700621 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
622 }
623 return mHotseatPadding;
624 }
625
Winsonfadbe8f2016-07-22 16:35:37 -0700626 /**
627 * @return the bounds for which the open folders should be contained within
628 */
629 public Rect getAbsoluteOpenFolderBounds() {
630 if (isVerticalBarLayout()) {
631 // Folders should only appear right of the drop target bar and left of the hotseat
632 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
633 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700634 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700635 mInsets.top + availableHeightPx);
636 } else {
637 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800638 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700639 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800640 mInsets.left + availableWidthPx - edgeMarginPx,
Jon Miranda18751b62017-07-31 17:25:27 -0700641 mInsets.top + availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700642 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700643 }
644 }
645
Jon Miranda228877d2021-02-09 11:05:00 -0500646 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
647 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700648 }
Jon Miranda228877d2021-02-09 11:05:00 -0500649 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
650 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700651 }
652
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700653 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700654 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
655 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
656 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700657 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700658 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700659 return isLandscape && transposeLayoutWithOrientation;
660 }
661
Sunny Goyal59d086c2018-05-07 17:31:40 -0700662 /**
Jon Miranda6f7e9702019-09-16 14:44:14 -0700663 * Returns true when the number of workspace columns and all apps columns differs.
664 */
665 private boolean allAppsHasDifferentNumColumns() {
666 return inv.numAllAppsColumns != inv.numColumns;
667 }
668
669 /**
Sunny Goyal59d086c2018-05-07 17:31:40 -0700670 * Updates orientation information and returns true if it has changed from the previous value.
671 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700672 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700673 if (isVerticalBarLayout()) {
Schneider Victor-tulias954bb632021-01-27 14:03:51 -0800674 // Check an up-to-date info.
675 boolean isSeascape = DisplayController.getDefaultDisplay(context)
676 .createInfoForContext(context).rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700677 if (mIsSeascape != isSeascape) {
678 mIsSeascape = isSeascape;
679 return true;
680 }
681 }
682 return false;
683 }
684
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800685 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700686 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800687 }
688
Sunny Goyal07b69292018-01-08 14:19:34 -0800689 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700690 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700691 }
692
Sunny Goyalc13403c2016-11-18 23:44:48 -0800693 public int getCellHeight(@ContainerType int containerType) {
694 switch (containerType) {
695 case CellLayout.WORKSPACE:
696 return cellHeightPx;
697 case CellLayout.FOLDER:
698 return folderCellHeightPx;
699 case CellLayout.HOTSEAT:
700 return hotseatCellHeightPx;
701 default:
702 // ??
703 return 0;
704 }
705 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700706
Sunny Goyalfd58da62020-08-11 12:06:49 -0700707 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -0700708 Configuration config = new Configuration(c.getResources().getConfiguration());
709 config.orientation = orientation;
710 config.densityDpi = info.metrics.densityDpi;
711 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700712 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800713
714 /**
715 * Callback when a component changes the DeviceProfile associated with it, as a result of
716 * configuration change
717 */
718 public interface OnDeviceProfileChangeListener {
719
720 /**
721 * Called when the device profile is reassigned. Note that for layout and measurements, it
722 * is sufficient to listen for inset changes. Use this callback when you need to perform
723 * a one time operation.
724 */
725 void onDeviceProfileChanged(DeviceProfile dp);
726 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700727
728 public static class Builder {
729 private Context mContext;
730 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -0700731 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700732
Sunny Goyal0addbf02020-04-28 14:17:35 -0700733 private final Point mWindowPosition = new Point();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700734 private Point mMinSize, mMaxSize;
735 private int mWidth, mHeight;
736
737 private boolean mIsLandscape;
738 private boolean mIsMultiWindowMode = false;
739 private boolean mTransposeLayoutWithOrientation;
740
Sunny Goyalfd58da62020-08-11 12:06:49 -0700741 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700742 mContext = context;
743 mInv = inv;
744 mInfo = info;
745 mTransposeLayoutWithOrientation = context.getResources()
746 .getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
747 }
748
749 public Builder setSizeRange(Point minSize, Point maxSize) {
750 mMinSize = minSize;
751 mMaxSize = maxSize;
752 return this;
753 }
754
755 public Builder setSize(int width, int height) {
756 mWidth = width;
757 mHeight = height;
758 mIsLandscape = mWidth > mHeight;
759 return this;
760 }
761
762 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
763 mIsMultiWindowMode = isMultiWindowMode;
764 return this;
765 }
766
Sunny Goyal0addbf02020-04-28 14:17:35 -0700767 /**
768 * Sets the window position if not full-screen
769 */
770 public Builder setWindowPosition(int x, int y) {
771 mWindowPosition.set(x, y);
772 return this;
773 }
774
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700775 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
776 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
777 return this;
778 }
779
780 public DeviceProfile build() {
781 return new DeviceProfile(mContext, mInv, mInfo, mMinSize, mMaxSize,
782 mWidth, mHeight, mIsLandscape, mIsMultiWindowMode,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700783 mTransposeLayoutWithOrientation, mWindowPosition);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700784 }
785 }
786
Winson Chungb3800242013-10-24 11:01:54 -0700787}