blob: 90cc384e86b7283c41ee6c3881727bf408ec3a27 [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 Mirandae126d722021-02-25 10:45:20 -050019import static com.android.launcher3.ResourceUtils.pxFromDp;
Jon Miranda228877d2021-02-09 11:05:00 -050020
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
Jon Mirandae126d722021-02-25 10:45:20 -050069 public final boolean isScalableGrid;
70
Tony Wickhamd6b40372015-09-23 18:37:57 -070071 /**
72 * The maximum amount of left/right workspace padding as a percentage of the screen width.
73 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
74 * 7% of the screen width can be used as right padding.
75 */
76 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070077
Jon Miranda3f9bab22017-07-28 14:50:17 -070078 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070079
Jon Miranda5eacbb72018-07-31 11:14:46 -070080 // To evenly space the icons, increase the left/right margins for tablets in portrait mode.
81 private static final int PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER = 4;
82
Sunny Goyalc6205602015-05-21 20:46:33 -070083 // Workspace
Jon Mirandae126d722021-02-25 10:45:20 -050084 public final int desiredWorkspaceLeftRightOriginalPx;
85 public int desiredWorkspaceLeftRightMarginPx;
86 public final int cellLayoutBorderSpacingOriginalPx;
87 public int cellLayoutBorderSpacingPx;
Jon Miranda28032002017-07-13 16:18:56 -070088 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070089 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -070090 public final int edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070091 public float workspaceSpringLoadShrinkFactor;
92 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070093
Jon Miranda228877d2021-02-09 11:05:00 -050094 public int workspaceTopPadding;
95 public int workspaceBottomPadding;
96
Tony Wickham5edf9e22020-03-27 20:06:52 -070097 // Workspace page indicator
98 public final int workspacePageIndicatorHeight;
99 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -0700100
Sunny Goyalc6205602015-05-21 20:46:33 -0700101 // Workspace icons
102 public int iconSizePx;
103 public int iconTextSizePx;
104 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700105 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700106
Adam Cohen59400422014-03-05 18:07:04 -0800107 public int cellWidthPx;
108 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700109 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800110
Jon Mirandae126d722021-02-25 10:45:20 -0500111 public int cellYPaddingPx;
112 public int cellYPaddingOriginalPx;
113
Sunny Goyalc6205602015-05-21 20:46:33 -0700114 // Folder
Jon Mirandae126d722021-02-25 10:45:20 -0500115 public float folderLabelTextScale;
116 public int folderLabelTextSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700117 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700118 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700119
Jon Mirandae126d722021-02-25 10:45:20 -0500120 // Folder content
121 public int folderContentPaddingLeftRight;
122 public int folderContentPaddingTop;
123
Jon Mirandabf7d8122016-11-03 15:29:29 -0700124 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700125 public int folderCellWidthPx;
126 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700127
128 // Folder child
129 public int folderChildIconSizePx;
130 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700131 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700132
Sunny Goyalc6205602015-05-21 20:46:33 -0700133 // Hotseat
Sunny Goyalc6205602015-05-21 20:46:33 -0700134 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700135 // In portrait: size = height, in landscape: size = width
136 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800137 public final int hotseatBarTopPaddingPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700138 public int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700139 // Start is the side next to the nav bar, end is the side next to the workspace
140 public final int hotseatBarSidePaddingStartPx;
141 public final int hotseatBarSidePaddingEndPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700142
Sunny Goyalc6205602015-05-21 20:46:33 -0700143 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700144 public int allAppsCellHeightPx;
Samuel Fufaf1424a32019-10-04 15:15:19 -0700145 public int allAppsCellWidthPx;
Winson1f064272016-07-18 17:18:02 -0700146 public int allAppsIconSizePx;
147 public int allAppsIconDrawablePaddingPx;
148 public float allAppsIconTextSizePx;
149
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800150 // Widgets
151 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
152
Sunny Goyal47328fd2016-05-25 18:56:41 -0700153 // Drop Target
154 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700155
Winson1f064272016-07-18 17:18:02 -0700156 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800157 private final Rect mInsets = new Rect();
158 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700159 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800160 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700161 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700162
Tony Wickhamf34bee82018-12-03 18:11:39 -0800163 // Notification dots
Samuel Fufac96fa242019-09-26 23:06:32 -0700164 public DotRenderer mDotRendererWorkSpace;
165 public DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800166
Tony Wickham15883892021-02-12 11:24:40 -0800167 // Taskbar
168 public boolean isTaskbarPresent;
169 public int taskbarSize;
Tony Wickham145b7fd2021-03-11 10:03:12 -0800170 // How much of the bottom inset is due to Taskbar rather than other system elements.
171 public int nonOverlappingTaskbarInset;
Tony Wickham15883892021-02-12 11:24:40 -0800172
Sunny Goyalfd58da62020-08-11 12:06:49 -0700173 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info,
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700174 Point minSize, Point maxSize, int width, int height, boolean isLandscape,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700175 boolean isMultiWindowMode, boolean transposeLayoutWithOrientation,
176 Point windowPosition) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700177
Adam Cohen2e6da152015-05-06 11:42:25 -0700178 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700179 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800180 this.isMultiWindowMode = isMultiWindowMode;
Jon Mirandae126d722021-02-25 10:45:20 -0500181 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Sunny Goyal0addbf02020-04-28 14:17:35 -0700182 windowX = windowPosition.x;
183 windowY = windowPosition.y;
Winson Chungb3800242013-10-24 11:01:54 -0700184
Jon Mirandae126d722021-02-25 10:45:20 -0500185 isScalableGrid = inv.isScalable && !isVerticalBarLayout() && !isMultiWindowMode;
186
Jon Miranda7e183c32018-06-20 11:05:27 -0700187 // Determine sizes.
188 widthPx = width;
189 heightPx = height;
Tony Wickham15883892021-02-12 11:24:40 -0800190 int nonFinalAvailableHeightPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700191 if (isLandscape) {
192 availableWidthPx = maxSize.x;
Tony Wickham15883892021-02-12 11:24:40 -0800193 nonFinalAvailableHeightPx = minSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700194 } else {
195 availableWidthPx = minSize.x;
Tony Wickham15883892021-02-12 11:24:40 -0800196 nonFinalAvailableHeightPx = maxSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700197 }
198
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700199 mInfo = info;
Adam Cohen2e6da152015-05-06 11:42:25 -0700200
Sunny Goyalc6205602015-05-21 20:46:33 -0700201 // Constants from resources
Sunny Goyal1890f672020-04-30 12:28:00 -0700202 float swDPs = Utilities.dpiFromPx(
203 Math.min(info.smallestSize.x, info.smallestSize.y), info.metrics);
Alex Chau4e2c25a2021-02-03 16:20:04 +0000204 boolean allowRotation = context.getResources().getBoolean(R.bool.allow_rotation);
205 // Tablet UI is built with assumption that simulated landscape is disabled.
206 isTablet = allowRotation && swDPs >= TABLET_MIN_DPS;
207 isLargeTablet = isTablet && swDPs >= LARGE_TABLET_MIN_DPS;
Sunny Goyalc6205602015-05-21 20:46:33 -0700208 isPhone = !isTablet && !isLargeTablet;
Jon Mirandabba64512019-03-27 10:54:17 -0700209 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda7e183c32018-06-20 11:05:27 -0700210 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700211
212 // Some more constants
Sunny Goyal1890f672020-04-30 12:28:00 -0700213 context = getContext(context, info, isVerticalBarLayout()
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700214 ? Configuration.ORIENTATION_LANDSCAPE
215 : Configuration.ORIENTATION_PORTRAIT);
Sunny Goyal1890f672020-04-30 12:28:00 -0700216 final Resources res = context.getResources();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700217
Tony Wickham15883892021-02-12 11:24:40 -0800218 isTaskbarPresent = isTablet && FeatureFlags.ENABLE_TASKBAR.get();
219 if (isTaskbarPresent) {
220 // Taskbar will be added later, but provides bottom insets that we should subtract
221 // from availableHeightPx.
222 taskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_size);
223 WindowInsets windowInsets = DisplayController.INSTANCE.get(context).getHolder(mInfo.id)
224 .getDisplayContext().getSystemService(WindowManager.class)
225 .getCurrentWindowMetrics().getWindowInsets();
Tony Wickham145b7fd2021-03-11 10:03:12 -0800226 nonOverlappingTaskbarInset =
Tony Wickham15883892021-02-12 11:24:40 -0800227 taskbarSize - windowInsets.getSystemWindowInsetBottom();
228 if (nonOverlappingTaskbarInset > 0) {
229 nonFinalAvailableHeightPx -= nonOverlappingTaskbarInset;
230 }
231 }
232 availableHeightPx = nonFinalAvailableHeightPx;
233
Winson Chungb3800242013-10-24 11:01:54 -0700234 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700235
Jon Mirandae126d722021-02-25 10:45:20 -0500236 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : isScalableGrid
237 ? res.getDimensionPixelSize(R.dimen.scalable_grid_left_right_margin)
238 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
239 desiredWorkspaceLeftRightOriginalPx = desiredWorkspaceLeftRightMarginPx;
240
241 folderLabelTextScale = res.getFloat(R.dimen.folder_label_text_scale);
242 folderContentPaddingLeftRight =
243 res.getDimensionPixelSize(R.dimen.folder_content_padding_left_right);
244 folderContentPaddingTop = res.getDimensionPixelSize(R.dimen.folder_content_padding_top);
245
246 setCellLayoutBorderSpacing(pxFromDp(inv.borderSpacing, mInfo.metrics, 1f));
247 cellLayoutBorderSpacingOriginalPx = cellLayoutBorderSpacingPx;
248
Jon Miranda5eacbb72018-07-31 11:14:46 -0700249 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
250 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Jon Mirandae126d722021-02-25 10:45:20 -0500251 int cellLayoutPadding = isScalableGrid
252 ? 0
253 : res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700254 if (isLandscape) {
255 cellLayoutPaddingLeftRightPx = 0;
256 cellLayoutBottomPaddingPx = cellLayoutPadding;
257 } else {
258 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
259 cellLayoutBottomPaddingPx = 0;
260 }
261
Tony Wickham5edf9e22020-03-27 20:06:52 -0700262 workspacePageIndicatorHeight = res.getDimensionPixelSize(
263 R.dimen.workspace_page_indicator_height);
264 mWorkspacePageIndicatorOverlapWorkspace =
265 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700266
Winson Chungb3800242013-10-24 11:01:54 -0700267 iconDrawablePaddingOriginalPx =
268 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700269 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
270 workspaceSpringLoadedBottomSpace =
271 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700272
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700273 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700274
Winson1f064272016-07-18 17:18:02 -0700275 hotseatBarTopPaddingPx =
276 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700277 hotseatBarBottomPaddingPx = (isTallDevice ? 0
278 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
279 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700280 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800281 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700282 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700283 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Jon Miranda228877d2021-02-09 11:05:00 -0500284 int hotseatExtraVerticalSize =
285 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size);
Jon Mirandae126d722021-02-25 10:45:20 -0500286 hotseatBarSizePx = pxFromDp(inv.iconSize, mInfo.metrics, 1f)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700287 + (isVerticalBarLayout()
Sunny Goyal415f1732018-11-29 10:33:47 -0800288 ? (hotseatBarSidePaddingStartPx + hotseatBarSidePaddingEndPx)
Jon Miranda228877d2021-02-09 11:05:00 -0500289 : (hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx
Jon Mirandae126d722021-02-25 10:45:20 -0500290 + (isScalableGrid ? 0 : hotseatExtraVerticalSize)));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700291
Jon Miranda2ed276e2017-06-29 11:36:34 -0700292 // Calculate all of the remaining variables.
Jon Miranda228877d2021-02-09 11:05:00 -0500293 int extraSpace = updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700294 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandae126d722021-02-25 10:45:20 -0500295 if (isScalableGrid) {
Jon Miranda228877d2021-02-09 11:05:00 -0500296 DevicePadding padding = inv.devicePaddings.getDevicePadding(extraSpace);
297 workspaceTopPadding = padding.getWorkspaceTopPadding(extraSpace);
298 workspaceBottomPadding = padding.getWorkspaceBottomPadding(extraSpace);
299
300 float hotseatBarBottomPadding = padding.getHotseatBottomPadding(extraSpace);
301 hotseatBarSizePx += hotseatBarBottomPadding;
302 hotseatBarBottomPaddingPx += hotseatBarBottomPadding;
303 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700304 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700305 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700306 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700307 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda228877d2021-02-09 11:05:00 -0500308 extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2
Tony Wickham5edf9e22020-03-27 20:06:52 -0700309 - workspacePageIndicatorHeight;
Jon Miranda7e183c32018-06-20 11:05:27 -0700310 hotseatBarSizePx += extraSpace;
311 hotseatBarBottomPaddingPx += extraSpace;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700312
Jon Miranda3f9bab22017-07-28 14:50:17 -0700313 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700314 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700315 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800316 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800317
318 // This is done last, after iconSizePx is calculated above.
Samuel Fufac96fa242019-09-26 23:06:32 -0700319 mDotRendererWorkSpace = new DotRenderer(iconSizePx, IconShape.getShapePath(),
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700320 IconShape.DEFAULT_PATH_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700321 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
322 new DotRenderer(allAppsIconSizePx, IconShape.getShapePath(),
323 IconShape.DEFAULT_PATH_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700324 }
325
Jon Mirandae126d722021-02-25 10:45:20 -0500326 private void setCellLayoutBorderSpacing(int borderSpacing) {
327 if (isScalableGrid) {
328 cellLayoutBorderSpacingPx = borderSpacing;
329 folderContentPaddingLeftRight = borderSpacing;
330 folderContentPaddingTop = borderSpacing;
331 } else {
332 cellLayoutBorderSpacingPx = 0;
333 }
334 }
335
Jon Miranda611dba42021-03-02 14:12:13 -0500336 /**
337 * We inset the widget padding added by the system and instead rely on the border spacing
338 * between cells to create reliable consistency between widgets
339 */
340 public boolean shouldInsetWidgets() {
341 Rect widgetPadding = inv.defaultWidgetPadding;
342
343 // Check all sides to ensure that the widget won't overlap into another cell.
344 return cellLayoutBorderSpacingPx > widgetPadding.left
345 && cellLayoutBorderSpacingPx > widgetPadding.top
346 && cellLayoutBorderSpacingPx > widgetPadding.right
347 && cellLayoutBorderSpacingPx > widgetPadding.bottom;
348 }
Jon Mirandae126d722021-02-25 10:45:20 -0500349
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700350 public Builder toBuilder(Context context) {
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800351 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700352 return new Builder(context, inv, mInfo)
353 .setSizeRange(size, size)
354 .setSize(widthPx, heightPx)
Sunny Goyal0addbf02020-04-28 14:17:35 -0700355 .setWindowPosition(windowX, windowY)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700356 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800357 }
358
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700359 public DeviceProfile copy(Context context) {
360 return toBuilder(context).build();
361 }
362
363 /**
364 * TODO: Move this to the builder as part of setMultiWindowMode
365 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700366 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700367 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
368 // the system decor is always excluded.
Sunny Goyalb46703d2020-05-27 17:52:03 -0700369 Point mwSize = new Point(Math.min(availableWidthPx, windowBounds.availableSize.x),
370 Math.min(availableHeightPx, windowBounds.availableSize.y));
Jon Mirandaa11380d2017-08-24 11:30:03 -0700371
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700372 DeviceProfile profile = toBuilder(context)
373 .setSizeRange(mwSize, mwSize)
Sunny Goyalb46703d2020-05-27 17:52:03 -0700374 .setSize(windowBounds.bounds.width(), windowBounds.bounds.height())
375 .setWindowPosition(windowBounds.bounds.left, windowBounds.bounds.top)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700376 .setMultiWindowMode(true)
377 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800378
Jon Miranda0cd04572017-09-19 11:31:42 -0700379 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
380 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
381 - iconDrawablePaddingPx - profile.iconTextSizePx;
382 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
383 profile.adjustToHideWorkspaceLabels();
384 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800385
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800386 // We use these scales to measure and layout the widgets using their full invariant profile
387 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
388 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
389 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
390 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800391 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800392
Jon Miranda93e1f042016-11-11 14:13:04 -0800393 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700394 }
395
Adam Cohen63f1ec02014-08-12 09:23:13 -0700396 /**
Sunny Goyalb46703d2020-05-27 17:52:03 -0700397 * Inverse of {@link #getMultiWindowProfile(Context, WindowBounds)}
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800398 * @return device profile corresponding to the current orientation in non multi-window mode.
399 */
400 public DeviceProfile getFullScreenProfile() {
401 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
402 }
403
404 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700405 * Adjusts the profile so that the labels on the Workspace are hidden.
406 * It is important to call this method after the All Apps variables have been set.
407 */
408 private void adjustToHideWorkspaceLabels() {
409 iconTextSizePx = 0;
410 iconDrawablePaddingPx = 0;
411 cellHeightPx = iconSizePx;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700412 autoResizeAllAppsCells();
413 }
Jon Miranda1091e532017-07-21 13:31:50 -0700414
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700415 /**
416 * Re-computes the all-apps cell size to be independent of workspace
417 */
418 public void autoResizeAllAppsCells() {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700419 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700420 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
421 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700422 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700423 }
424
Jon Miranda228877d2021-02-09 11:05:00 -0500425 /**
426 * Returns the amount of extra (or unused) vertical space.
427 */
428 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700429 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800430
Jon Mirandae126d722021-02-25 10:45:20 -0500431 Point workspacePadding = getTotalWorkspacePadding();
432
433 // Check to see if the icons fit within the available height.
434 float usedHeight = getCellLayoutHeight();
435 final int maxHeight = availableHeightPx - workspacePadding.y;
Jon Miranda228877d2021-02-09 11:05:00 -0500436 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500437 float scaleY = maxHeight / usedHeight;
438 boolean shouldScale = scaleY < 1f;
439
440 float scaleX = 1f;
441 if (isScalableGrid) {
442 // We scale to fit the cellWidth and cellHeight in the available space.
443 // The benefit of scalable grids is that we can get consistent aspect ratios between
444 // devices.
445 float usedWidth = (cellWidthPx * inv.numColumns)
446 + (cellLayoutBorderSpacingPx * (inv.numColumns - 1))
447 + (desiredWorkspaceLeftRightMarginPx * 2);
448 // We do not subtract padding here, as we also scale the workspace padding if needed.
449 scaleX = availableWidthPx / usedWidth;
450 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700451 }
Jon Mirandae126d722021-02-25 10:45:20 -0500452
453 if (shouldScale) {
454 float scale = Math.min(scaleX, scaleY);
455 updateIconSize(scale, res);
456 extraHeight = Math.max(0, maxHeight - getCellLayoutHeight());
457 }
458
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700459 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500460 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700461 }
462
Jon Mirandae126d722021-02-25 10:45:20 -0500463 private int getCellLayoutHeight() {
464 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacingPx * (inv.numRows - 1));
465 }
466
Jon Miranda6f7e9702019-09-16 14:44:14 -0700467 /**
468 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
469 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
470 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
471 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800472 public void updateIconSize(float scale, Resources res) {
Jon Miranda18751b62017-07-31 17:25:27 -0700473 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800474 final boolean isVerticalLayout = isVerticalBarLayout();
Jon Miranda6f7e9702019-09-16 14:44:14 -0700475 float invIconSizeDp = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500476 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mInfo.metrics, scale));
Jon Miranda378c11f2021-03-10 10:28:50 -0500477 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, mInfo.metrics) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700478 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700479
Jon Mirandae126d722021-02-25 10:45:20 -0500480 setCellLayoutBorderSpacing((int) (cellLayoutBorderSpacingOriginalPx * scale));
481
482 if (isScalableGrid) {
483 cellWidthPx = pxFromDp(inv.minCellWidth, mInfo.metrics, scale);
484 cellHeightPx = pxFromDp(inv.minCellHeight, mInfo.metrics, scale);
485 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
486 + Utilities.calculateTextHeight(iconTextSizePx);
487 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
488 desiredWorkspaceLeftRightMarginPx = (int) (desiredWorkspaceLeftRightOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500489 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500490 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda228877d2021-02-09 11:05:00 -0500491 cellHeightPx = iconSizePx + iconDrawablePaddingPx
492 + Utilities.calculateTextHeight(iconTextSizePx);
493 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
494 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
495 && !isMultiWindowMode) {
496 // Ensures that the label is closer to its corresponding icon. This is not an issue
497 // with vertical bar layout or multi-window mode since the issue is handled
498 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
499 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
500 iconDrawablePaddingPx = cellPaddingY;
501 }
Jon Miranda846455e2017-10-02 14:58:52 -0700502 }
Jon Miranda18751b62017-07-31 17:25:27 -0700503
Sunny Goyalae190ff2020-04-14 00:19:01 +0000504 // All apps
505 if (allAppsHasDifferentNumColumns()) {
Jon Mirandae126d722021-02-25 10:45:20 -0500506 allAppsIconSizePx = pxFromDp(inv.allAppsIconSize, mInfo.metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700507 allAppsIconTextSizePx = Utilities.pxFromSp(inv.allAppsIconTextSize, mInfo.metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000508 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda35359442020-07-09 14:54:23 -0700509 // We use 4 below to ensure labels are closer to their corresponding icon.
510 allAppsCellHeightPx = Math.round(allAppsIconSizePx + allAppsIconTextSizePx
511 + (4 * allAppsIconDrawablePaddingPx));
Sunny Goyalae190ff2020-04-14 00:19:01 +0000512 } else {
513 allAppsIconSizePx = iconSizePx;
514 allAppsIconTextSizePx = iconTextSizePx;
515 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
516 allAppsCellHeightPx = getCellSize().y;
517 }
518 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700519
Jon Miranda6f7e9702019-09-16 14:44:14 -0700520 if (isVerticalBarLayout()) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700521 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700522 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700523 }
Winson Chungb3800242013-10-24 11:01:54 -0700524
Winson Chungb3800242013-10-24 11:01:54 -0700525 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800526 if (isVerticalLayout) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700527 hotseatBarSizePx = iconSizePx + hotseatBarSidePaddingStartPx
528 + hotseatBarSidePaddingEndPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700529 }
Jon Miranda5044c352017-08-09 11:37:59 -0700530 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700531
Sunny Goyal07b69292018-01-08 14:19:34 -0800532 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700533 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700534 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700535 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
536 workspaceSpringLoadShrinkFactor = Math.min(
537 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
538 1 - (minRequiredHeight / expectedWorkspaceHeight));
539 } else {
540 workspaceSpringLoadShrinkFactor =
541 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
542 }
543
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700544 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700545 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
546 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700547 }
548
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700549 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700550 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700551
Jon Mirandae126d722021-02-25 10:45:20 -0500552 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
553
Jon Mirandac1b23992016-12-13 08:57:36 -0800554 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700555 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800556 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800557
558 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500559 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
560 + ((inv.numFolderRows - 1) * cellLayoutBorderSpacingPx);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800561 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500562 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800563 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800564
565 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500566 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
567 + ((inv.numFolderColumns - 1) * cellLayoutBorderSpacingPx);
Jon Mirandae126d722021-02-25 10:45:20 -0500568 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
569 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800570 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800571
572 float scale = Math.min(scaleX, scaleY);
573 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700574 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700575 }
576 }
577
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700578 private void updateFolderCellSize(float scale, Resources res) {
Jon Mirandae126d722021-02-25 10:45:20 -0500579 float invIconSizeDp = isVerticalBarLayout() ? inv.landscapeIconSize : inv.iconSize;
580 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mInfo.metrics, scale));
581 folderChildTextSizePx = pxFromDp(inv.iconTextSize, mInfo.metrics, scale);
582 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700583
584 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
585 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
586 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
587
Jon Mirandac1b23992016-12-13 08:57:36 -0800588 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000589 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
590 folderChildDrawablePaddingPx = Math.max(0,
591 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700592 }
593
Winson1f064272016-07-18 17:18:02 -0700594 public void updateInsets(Rect insets) {
595 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800596 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700597 }
598
Sunny Goyalae6e3182019-04-30 12:04:37 -0700599 /**
600 * The current device insets. This is generally same as the insets being dispatched to
601 * {@link Insettable} elements, but can differ if the element is using a different profile.
602 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800603 public Rect getInsets() {
604 return mInsets;
605 }
606
Sunny Goyal756cd262015-08-20 12:33:21 -0700607 public Point getCellSize() {
Jon Miranda6f7e9702019-09-16 14:44:14 -0700608 return getCellSize(inv.numColumns, inv.numRows);
609 }
610
611 private Point getCellSize(int numColumns, int numRows) {
Sunny Goyal756cd262015-08-20 12:33:21 -0700612 Point result = new Point();
613 // Since we are only concerned with the overall padding, layout direction does
614 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700615 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700616 result.x = calculateCellWidth(availableWidthPx - padding.x
Jon Miranda228877d2021-02-09 11:05:00 -0500617 - cellLayoutPaddingLeftRightPx * 2, cellLayoutBorderSpacingPx, numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700618 result.y = calculateCellHeight(availableHeightPx - padding.y
Jon Miranda228877d2021-02-09 11:05:00 -0500619 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacingPx, numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700620 return result;
621 }
622
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700623 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800624 updateWorkspacePadding();
625 return new Point(workspacePadding.left + workspacePadding.right,
626 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700627 }
628
629 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800630 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
631 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700632 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800633 private void updateWorkspacePadding() {
634 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700635 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800636 padding.top = 0;
637 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800638 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700639 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700640 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700641 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700642 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700643 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700644 }
Winson Chungb3800242013-10-24 11:01:54 -0700645 } else {
Tony Wickhama29a0462021-03-02 12:28:25 -0800646 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
647 int paddingBottom = hotseatTop + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500648 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700649 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700650 // Pad the left and right of the workspace to ensure consistent spacing
651 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700652 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800653 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800654 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700655 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800656 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Tony Wickhama29a0462021-03-02 12:28:25 -0800657 int hotseatVerticalPadding = isTaskbarPresent ? 0
658 : hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700659 int availablePaddingY = Math.max(0, heightPx - edgeMarginPx - paddingBottom
Tony Wickhama29a0462021-03-02 12:28:25 -0800660 - (2 * inv.numRows * cellHeightPx) - hotseatVerticalPadding);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700661 padding.set(availablePaddingX / 2, edgeMarginPx + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700662 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700663 } else {
664 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700665 padding.set(desiredWorkspaceLeftRightMarginPx,
Jon Mirandae126d722021-02-25 10:45:20 -0500666 workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx),
Winson1f064272016-07-18 17:18:02 -0700667 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700668 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700669 }
670 }
Winson Chungb3800242013-10-24 11:01:54 -0700671 }
672
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700673 public Rect getHotseatLayoutPadding() {
674 if (isVerticalBarLayout()) {
675 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700676 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
677 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700678 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700679 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
680 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700681 }
682 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700683 // We want the edges of the hotseat to line up with the edges of the workspace, but the
684 // icons in the hotseat are a different size, and so don't line up perfectly. To account
685 // for this, we pad the left and right of the hotseat with half of the difference of a
686 // workspace cell vs a hotseat cell.
687 float workspaceCellWidth = (float) widthPx / inv.numColumns;
688 float hotseatCellWidth = (float) widthPx / inv.numHotseatIcons;
689 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
690 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700691 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
692 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700693 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700694 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
695 + mInsets.right,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700696 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
697 }
698 return mHotseatPadding;
699 }
700
Winsonfadbe8f2016-07-22 16:35:37 -0700701 /**
702 * @return the bounds for which the open folders should be contained within
703 */
704 public Rect getAbsoluteOpenFolderBounds() {
705 if (isVerticalBarLayout()) {
706 // Folders should only appear right of the drop target bar and left of the hotseat
707 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
708 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700709 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700710 mInsets.top + availableHeightPx);
711 } else {
712 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -0800713 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800714 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700715 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800716 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -0800717 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -0700718 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700719 }
720 }
721
Jon Miranda228877d2021-02-09 11:05:00 -0500722 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
723 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700724 }
Jon Miranda228877d2021-02-09 11:05:00 -0500725 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
726 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700727 }
728
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700729 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700730 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
731 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
732 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700733 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700734 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700735 return isLandscape && transposeLayoutWithOrientation;
736 }
737
Sunny Goyal59d086c2018-05-07 17:31:40 -0700738 /**
Jon Miranda6f7e9702019-09-16 14:44:14 -0700739 * Returns true when the number of workspace columns and all apps columns differs.
740 */
741 private boolean allAppsHasDifferentNumColumns() {
742 return inv.numAllAppsColumns != inv.numColumns;
743 }
744
745 /**
Sunny Goyal59d086c2018-05-07 17:31:40 -0700746 * Updates orientation information and returns true if it has changed from the previous value.
747 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700748 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700749 if (isVerticalBarLayout()) {
Schneider Victor-tulias954bb632021-01-27 14:03:51 -0800750 // Check an up-to-date info.
Alex Chaud25e87a2021-02-12 12:12:56 +0000751 DisplayController.Info displayInfo = DisplayController.getDefaultDisplay(context)
752 .createInfoForContext(context);
753 if (displayInfo == null) {
754 return false;
755 }
756
757 boolean isSeascape = displayInfo.rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700758 if (mIsSeascape != isSeascape) {
759 mIsSeascape = isSeascape;
760 return true;
761 }
762 }
763 return false;
764 }
765
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800766 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700767 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800768 }
769
Sunny Goyal07b69292018-01-08 14:19:34 -0800770 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700771 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700772 }
773
Sunny Goyalc13403c2016-11-18 23:44:48 -0800774 public int getCellHeight(@ContainerType int containerType) {
775 switch (containerType) {
776 case CellLayout.WORKSPACE:
777 return cellHeightPx;
778 case CellLayout.FOLDER:
779 return folderCellHeightPx;
780 case CellLayout.HOTSEAT:
781 return hotseatCellHeightPx;
782 default:
783 // ??
784 return 0;
785 }
786 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700787
Sunny Goyalfd58da62020-08-11 12:06:49 -0700788 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -0700789 Configuration config = new Configuration(c.getResources().getConfiguration());
790 config.orientation = orientation;
791 config.densityDpi = info.metrics.densityDpi;
792 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700793 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800794
795 /**
796 * Callback when a component changes the DeviceProfile associated with it, as a result of
797 * configuration change
798 */
799 public interface OnDeviceProfileChangeListener {
800
801 /**
802 * Called when the device profile is reassigned. Note that for layout and measurements, it
803 * is sufficient to listen for inset changes. Use this callback when you need to perform
804 * a one time operation.
805 */
806 void onDeviceProfileChanged(DeviceProfile dp);
807 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700808
809 public static class Builder {
810 private Context mContext;
811 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -0700812 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700813
Sunny Goyal0addbf02020-04-28 14:17:35 -0700814 private final Point mWindowPosition = new Point();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700815 private Point mMinSize, mMaxSize;
816 private int mWidth, mHeight;
817
818 private boolean mIsLandscape;
819 private boolean mIsMultiWindowMode = false;
820 private boolean mTransposeLayoutWithOrientation;
821
Sunny Goyalfd58da62020-08-11 12:06:49 -0700822 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700823 mContext = context;
824 mInv = inv;
825 mInfo = info;
826 mTransposeLayoutWithOrientation = context.getResources()
827 .getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
828 }
829
830 public Builder setSizeRange(Point minSize, Point maxSize) {
831 mMinSize = minSize;
832 mMaxSize = maxSize;
833 return this;
834 }
835
836 public Builder setSize(int width, int height) {
837 mWidth = width;
838 mHeight = height;
839 mIsLandscape = mWidth > mHeight;
840 return this;
841 }
842
843 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
844 mIsMultiWindowMode = isMultiWindowMode;
845 return this;
846 }
847
Sunny Goyal0addbf02020-04-28 14:17:35 -0700848 /**
849 * Sets the window position if not full-screen
850 */
851 public Builder setWindowPosition(int x, int y) {
852 mWindowPosition.set(x, y);
853 return this;
854 }
855
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700856 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
857 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
858 return this;
859 }
860
861 public DeviceProfile build() {
862 return new DeviceProfile(mContext, mInv, mInfo, mMinSize, mMaxSize,
863 mWidth, mHeight, mIsLandscape, mIsMultiWindowMode,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700864 mTransposeLayoutWithOrientation, mWindowPosition);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700865 }
866 }
867
Winson Chungb3800242013-10-24 11:01:54 -0700868}