blob: a6fc0f34c034b157834305112e4d4be11953ebc2 [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
Sunny Goyal35c7b192021-04-20 16:51:10 -070019import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
20
Jon Mirandae126d722021-02-25 10:45:20 -050021import static com.android.launcher3.ResourceUtils.pxFromDp;
Jon Miranda58561d42021-03-17 10:51:54 -040022import static com.android.launcher3.Utilities.dpiFromPx;
Jon Miranda228877d2021-02-09 11:05:00 -050023
Sunny Goyal35c7b192021-04-20 16:51:10 -070024import android.annotation.SuppressLint;
Winson Chungb3800242013-10-24 11:01:54 -070025import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070026import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070027import android.content.res.Resources;
Winson Chungb3800242013-10-24 11:01:54 -070028import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080029import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070030import android.graphics.Rect;
Sunny Goyal35c7b192021-04-20 16:51:10 -070031import android.hardware.display.DisplayManager;
32import android.util.DisplayMetrics;
Winson Chung8e64bba2021-04-20 13:26:26 -070033import android.util.Log;
Sunny Goyal59d086c2018-05-07 17:31:40 -070034import android.view.Surface;
Tony Wickham15883892021-02-12 11:24:40 -080035import android.view.WindowInsets;
36import android.view.WindowManager;
Winson Chungb3800242013-10-24 11:01:54 -070037
Sunny Goyalc13403c2016-11-18 23:44:48 -080038import com.android.launcher3.CellLayout.ContainerType;
Jon Miranda228877d2021-02-09 11:05:00 -050039import com.android.launcher3.DevicePaddings.DevicePadding;
Tony Wickham15883892021-02-12 11:24:40 -080040import com.android.launcher3.config.FeatureFlags;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070041import com.android.launcher3.graphics.IconShape;
Tony Wickham8912b042018-11-29 15:28:53 -080042import com.android.launcher3.icons.DotRenderer;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070043import com.android.launcher3.icons.IconNormalizer;
Winson Chung8e64bba2021-04-20 13:26:26 -070044import com.android.launcher3.testing.TestProtocol;
Sunny Goyalfd58da62020-08-11 12:06:49 -070045import com.android.launcher3.util.DisplayController;
46import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb46703d2020-05-27 17:52:03 -070047import com.android.launcher3.util.WindowBounds;
Winson1f064272016-07-18 17:18:02 -070048
Jon Miranda58561d42021-03-17 10:51:54 -040049import java.io.PrintWriter;
50
Sunny Goyal35c7b192021-04-20 16:51:10 -070051@SuppressLint("NewApi")
Winson Chungb3800242013-10-24 11:01:54 -070052public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070053
Sunny Goyal1890f672020-04-30 12:28:00 -070054 private static final float TABLET_MIN_DPS = 600;
55 private static final float LARGE_TABLET_MIN_DPS = 720;
56
57
Adam Cohen2e6da152015-05-06 11:42:25 -070058 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070059 private final Info mInfo;
Sunny Goyal35c7b192021-04-20 16:51:10 -070060 private final DisplayMetrics mMetrics;
Winson Chungbe876472014-05-14 14:15:20 -070061
Sunny Goyalc6205602015-05-21 20:46:33 -070062 // Device properties
63 public final boolean isTablet;
64 public final boolean isLargeTablet;
65 public final boolean isPhone;
66 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070067
Sunny Goyalc6205602015-05-21 20:46:33 -070068 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070069 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080070 public final boolean isMultiWindowMode;
71
Sunny Goyal0addbf02020-04-28 14:17:35 -070072 public final int windowX;
73 public final int windowY;
Sunny Goyalc6205602015-05-21 20:46:33 -070074 public final int widthPx;
75 public final int heightPx;
76 public final int availableWidthPx;
77 public final int availableHeightPx;
Jon Mirandabba64512019-03-27 10:54:17 -070078
79 public final float aspectRatio;
80
Jon Mirandae126d722021-02-25 10:45:20 -050081 public final boolean isScalableGrid;
82
Tony Wickhamd6b40372015-09-23 18:37:57 -070083 /**
84 * The maximum amount of left/right workspace padding as a percentage of the screen width.
85 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
86 * 7% of the screen width can be used as right padding.
87 */
88 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070089
Jon Miranda3f9bab22017-07-28 14:50:17 -070090 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070091
Jon Miranda5eacbb72018-07-31 11:14:46 -070092 // To evenly space the icons, increase the left/right margins for tablets in portrait mode.
93 private static final int PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER = 4;
94
Sunny Goyalc6205602015-05-21 20:46:33 -070095 // Workspace
Jon Mirandae126d722021-02-25 10:45:20 -050096 public final int desiredWorkspaceLeftRightOriginalPx;
97 public int desiredWorkspaceLeftRightMarginPx;
98 public final int cellLayoutBorderSpacingOriginalPx;
99 public int cellLayoutBorderSpacingPx;
Jon Miranda28032002017-07-13 16:18:56 -0700100 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700101 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700102 public final int edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700103 public float workspaceSpringLoadShrinkFactor;
104 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700105
Jon Miranda58561d42021-03-17 10:51:54 -0400106 private final int extraSpace;
Jon Miranda228877d2021-02-09 11:05:00 -0500107 public int workspaceTopPadding;
108 public int workspaceBottomPadding;
Jon Miranda58561d42021-03-17 10:51:54 -0400109 public int extraHotseatBottomPadding;
Jon Miranda228877d2021-02-09 11:05:00 -0500110
Tony Wickham5edf9e22020-03-27 20:06:52 -0700111 // Workspace page indicator
112 public final int workspacePageIndicatorHeight;
113 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -0700114
Sunny Goyalc6205602015-05-21 20:46:33 -0700115 // Workspace icons
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400116 public float iconScale;
Sunny Goyalc6205602015-05-21 20:46:33 -0700117 public int iconSizePx;
118 public int iconTextSizePx;
119 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700120 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700121
Adam Cohen59400422014-03-05 18:07:04 -0800122 public int cellWidthPx;
123 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700124 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800125
Jon Mirandae126d722021-02-25 10:45:20 -0500126 public int cellYPaddingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500127
Sunny Goyalc6205602015-05-21 20:46:33 -0700128 // Folder
Jon Mirandae126d722021-02-25 10:45:20 -0500129 public float folderLabelTextScale;
130 public int folderLabelTextSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700131 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700132 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700133
Jon Mirandae126d722021-02-25 10:45:20 -0500134 // Folder content
Jon Miranda823da222021-03-23 08:08:45 -0400135 public int folderCellLayoutBorderSpacingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500136 public int folderContentPaddingLeftRight;
137 public int folderContentPaddingTop;
138
Jon Mirandabf7d8122016-11-03 15:29:29 -0700139 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700140 public int folderCellWidthPx;
141 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700142
143 // Folder child
144 public int folderChildIconSizePx;
145 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700146 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700147
Sunny Goyalc6205602015-05-21 20:46:33 -0700148 // Hotseat
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700149 public final int numShownHotseatIcons;
Sunny Goyalc6205602015-05-21 20:46:33 -0700150 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700151 // In portrait: size = height, in landscape: size = width
152 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800153 public final int hotseatBarTopPaddingPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700154 public int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700155 // Start is the side next to the nav bar, end is the side next to the workspace
156 public final int hotseatBarSidePaddingStartPx;
157 public final int hotseatBarSidePaddingEndPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700158
Sunny Goyalc6205602015-05-21 20:46:33 -0700159 // All apps
Samuel Fufaee9aff92021-04-05 13:30:40 -0500160 public int allAppsOpenVerticalTranslate;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700161 public int allAppsCellHeightPx;
Samuel Fufaf1424a32019-10-04 15:15:19 -0700162 public int allAppsCellWidthPx;
Winson1f064272016-07-18 17:18:02 -0700163 public int allAppsIconSizePx;
164 public int allAppsIconDrawablePaddingPx;
165 public float allAppsIconTextSizePx;
166
Zak Cohen334efeb2021-03-19 16:42:07 -0700167 // Overview
Zak Cohen334efeb2021-03-19 16:42:07 -0700168 public int overviewTaskMarginPx;
Alex Chaudedbc8a2021-03-17 16:53:26 +0000169 public int overviewTaskIconSizePx;
170 public int overviewTaskThumbnailTopMarginPx;
Zak Cohen334efeb2021-03-19 16:42:07 -0700171
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800172 // Widgets
173 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
174
Sunny Goyal47328fd2016-05-25 18:56:41 -0700175 // Drop Target
176 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700177
Winson1f064272016-07-18 17:18:02 -0700178 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800179 private final Rect mInsets = new Rect();
180 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700181 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800182 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700183 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700184
Tony Wickhamf34bee82018-12-03 18:11:39 -0800185 // Notification dots
Samuel Fufac96fa242019-09-26 23:06:32 -0700186 public DotRenderer mDotRendererWorkSpace;
187 public DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800188
Tony Wickham15883892021-02-12 11:24:40 -0800189 // Taskbar
190 public boolean isTaskbarPresent;
191 public int taskbarSize;
Tony Wickham145b7fd2021-03-11 10:03:12 -0800192 // How much of the bottom inset is due to Taskbar rather than other system elements.
193 public int nonOverlappingTaskbarInset;
Tony Wickham15883892021-02-12 11:24:40 -0800194
Sunny Goyalfd58da62020-08-11 12:06:49 -0700195 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info,
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700196 Point minSize, Point maxSize, int width, int height, boolean isLandscape,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700197 boolean isMultiWindowMode, boolean transposeLayoutWithOrientation,
198 Point windowPosition) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700199
Adam Cohen2e6da152015-05-06 11:42:25 -0700200 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700201 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800202 this.isMultiWindowMode = isMultiWindowMode;
Jon Mirandae126d722021-02-25 10:45:20 -0500203 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Sunny Goyal0addbf02020-04-28 14:17:35 -0700204 windowX = windowPosition.x;
205 windowY = windowPosition.y;
Winson Chungb3800242013-10-24 11:01:54 -0700206
Jon Mirandae126d722021-02-25 10:45:20 -0500207 isScalableGrid = inv.isScalable && !isVerticalBarLayout() && !isMultiWindowMode;
208
Jon Miranda7e183c32018-06-20 11:05:27 -0700209 // Determine sizes.
210 widthPx = width;
211 heightPx = height;
Tony Wickham15883892021-02-12 11:24:40 -0800212 int nonFinalAvailableHeightPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700213 if (isLandscape) {
214 availableWidthPx = maxSize.x;
Tony Wickham15883892021-02-12 11:24:40 -0800215 nonFinalAvailableHeightPx = minSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700216 } else {
217 availableWidthPx = minSize.x;
Tony Wickham15883892021-02-12 11:24:40 -0800218 nonFinalAvailableHeightPx = maxSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700219 }
220
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700221 mInfo = info;
Adam Cohen2e6da152015-05-06 11:42:25 -0700222
Sunny Goyalc6205602015-05-21 20:46:33 -0700223 // Constants from resources
Sunny Goyal35c7b192021-04-20 16:51:10 -0700224 float swDPs = dpiFromPx(Math.min(info.smallestSize.x, info.smallestSize.y),
225 info.densityDpi);
Alex Chau4e2c25a2021-02-03 16:20:04 +0000226 boolean allowRotation = context.getResources().getBoolean(R.bool.allow_rotation);
227 // Tablet UI is built with assumption that simulated landscape is disabled.
228 isTablet = allowRotation && swDPs >= TABLET_MIN_DPS;
229 isLargeTablet = isTablet && swDPs >= LARGE_TABLET_MIN_DPS;
Sunny Goyalc6205602015-05-21 20:46:33 -0700230 isPhone = !isTablet && !isLargeTablet;
Jon Mirandabba64512019-03-27 10:54:17 -0700231 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda7e183c32018-06-20 11:05:27 -0700232 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700233
234 // Some more constants
Sunny Goyal1890f672020-04-30 12:28:00 -0700235 context = getContext(context, info, isVerticalBarLayout()
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700236 ? Configuration.ORIENTATION_LANDSCAPE
237 : Configuration.ORIENTATION_PORTRAIT);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700238 mMetrics = context.getResources().getDisplayMetrics();
Sunny Goyal1890f672020-04-30 12:28:00 -0700239 final Resources res = context.getResources();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700240
Tony Wickham15883892021-02-12 11:24:40 -0800241 isTaskbarPresent = isTablet && FeatureFlags.ENABLE_TASKBAR.get();
242 if (isTaskbarPresent) {
243 // Taskbar will be added later, but provides bottom insets that we should subtract
244 // from availableHeightPx.
245 taskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_size);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700246 WindowInsets windowInsets =
247 context.createWindowContext(
248 context.getSystemService(DisplayManager.class).getDisplay(mInfo.id),
249 TYPE_APPLICATION, null)
250 .getSystemService(WindowManager.class)
Tony Wickham15883892021-02-12 11:24:40 -0800251 .getCurrentWindowMetrics().getWindowInsets();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700252 nonOverlappingTaskbarInset = taskbarSize - windowInsets.getSystemWindowInsetBottom();
Tony Wickham15883892021-02-12 11:24:40 -0800253 if (nonOverlappingTaskbarInset > 0) {
254 nonFinalAvailableHeightPx -= nonOverlappingTaskbarInset;
255 }
256 }
257 availableHeightPx = nonFinalAvailableHeightPx;
258
Winson Chungb3800242013-10-24 11:01:54 -0700259 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700260
Jon Mirandae126d722021-02-25 10:45:20 -0500261 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : isScalableGrid
262 ? res.getDimensionPixelSize(R.dimen.scalable_grid_left_right_margin)
263 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
264 desiredWorkspaceLeftRightOriginalPx = desiredWorkspaceLeftRightMarginPx;
265
Samuel Fufaee9aff92021-04-05 13:30:40 -0500266
267 allAppsOpenVerticalTranslate = res.getDimensionPixelSize(
268 R.dimen.all_apps_open_vertical_translate);
269
Jon Mirandae126d722021-02-25 10:45:20 -0500270 folderLabelTextScale = res.getFloat(R.dimen.folder_label_text_scale);
271 folderContentPaddingLeftRight =
272 res.getDimensionPixelSize(R.dimen.folder_content_padding_left_right);
273 folderContentPaddingTop = res.getDimensionPixelSize(R.dimen.folder_content_padding_top);
274
Sunny Goyal35c7b192021-04-20 16:51:10 -0700275 setCellLayoutBorderSpacing(pxFromDp(inv.borderSpacing, mMetrics, 1f));
Jon Mirandae126d722021-02-25 10:45:20 -0500276 cellLayoutBorderSpacingOriginalPx = cellLayoutBorderSpacingPx;
Jon Miranda823da222021-03-23 08:08:45 -0400277 folderCellLayoutBorderSpacingPx = cellLayoutBorderSpacingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500278
Jon Miranda5eacbb72018-07-31 11:14:46 -0700279 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
280 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Jon Mirandae126d722021-02-25 10:45:20 -0500281 int cellLayoutPadding = isScalableGrid
282 ? 0
283 : res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100284
285 if (FeatureFlags.ENABLE_TWO_PANEL_HOME.get() && isTablet) {
286 cellLayoutPaddingLeftRightPx =
287 res.getDimensionPixelSize(R.dimen.two_panel_home_side_padding);
288 cellLayoutBottomPaddingPx = 0;
289 } else if (isLandscape) {
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700290 cellLayoutPaddingLeftRightPx = 0;
291 cellLayoutBottomPaddingPx = cellLayoutPadding;
292 } else {
293 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
294 cellLayoutBottomPaddingPx = 0;
295 }
296
Tony Wickham5edf9e22020-03-27 20:06:52 -0700297 workspacePageIndicatorHeight = res.getDimensionPixelSize(
298 R.dimen.workspace_page_indicator_height);
299 mWorkspacePageIndicatorOverlapWorkspace =
300 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700301
Winson Chungb3800242013-10-24 11:01:54 -0700302 iconDrawablePaddingOriginalPx =
303 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700304 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
305 workspaceSpringLoadedBottomSpace =
306 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700307
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700308 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700309
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700310 numShownHotseatIcons = inv.numShownHotseatIcons;
Winson1f064272016-07-18 17:18:02 -0700311 hotseatBarTopPaddingPx =
312 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700313 hotseatBarBottomPaddingPx = (isTallDevice ? 0
314 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
315 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700316 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800317 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700318 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700319 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Jon Miranda228877d2021-02-09 11:05:00 -0500320 int hotseatExtraVerticalSize =
321 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700322 hotseatBarSizePx = pxFromDp(inv.iconSize, mMetrics, 1f)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700323 + (isVerticalBarLayout()
Sunny Goyal415f1732018-11-29 10:33:47 -0800324 ? (hotseatBarSidePaddingStartPx + hotseatBarSidePaddingEndPx)
Jon Miranda228877d2021-02-09 11:05:00 -0500325 : (hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx
Jon Mirandae126d722021-02-25 10:45:20 -0500326 + (isScalableGrid ? 0 : hotseatExtraVerticalSize)));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700327
Zak Cohen334efeb2021-03-19 16:42:07 -0700328 overviewTaskMarginPx = res.getDimensionPixelSize(R.dimen.overview_task_margin);
Alex Chaudedbc8a2021-03-17 16:53:26 +0000329 overviewTaskIconSizePx =
330 isTablet && FeatureFlags.ENABLE_OVERVIEW_GRID.get() ? res.getDimensionPixelSize(
331 R.dimen.task_thumbnail_icon_size_grid) : res.getDimensionPixelSize(
332 R.dimen.task_thumbnail_icon_size);
333 overviewTaskThumbnailTopMarginPx = overviewTaskIconSizePx + overviewTaskMarginPx * 2;
Zak Cohen334efeb2021-03-19 16:42:07 -0700334
Jon Miranda2ed276e2017-06-29 11:36:34 -0700335 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400336 extraSpace = updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700337 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400338 if (isScalableGrid && inv.devicePaddings != null) {
339 // Paddings were created assuming no scaling, so we first unscale the extra space.
340 int unscaledExtraSpace = (int) (extraSpace / iconScale);
341 DevicePadding padding = inv.devicePaddings.getDevicePadding(unscaledExtraSpace);
Jon Miranda228877d2021-02-09 11:05:00 -0500342
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400343 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
344 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
345 int paddingHotseatBottom = padding.getHotseatBottomPadding(unscaledExtraSpace);
346
347 workspaceTopPadding = Math.round(paddingWorkspaceTop * iconScale);
348 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * iconScale);
349 extraHotseatBottomPadding = Math.round(paddingHotseatBottom * iconScale);
350
Jon Miranda58561d42021-03-17 10:51:54 -0400351 hotseatBarSizePx += extraHotseatBottomPadding;
352 hotseatBarBottomPaddingPx += extraHotseatBottomPadding;
Jon Miranda228877d2021-02-09 11:05:00 -0500353 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700354 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700355 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700356 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700357 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda58561d42021-03-17 10:51:54 -0400358 int extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2
Tony Wickham5edf9e22020-03-27 20:06:52 -0700359 - workspacePageIndicatorHeight;
Jon Miranda7e183c32018-06-20 11:05:27 -0700360 hotseatBarSizePx += extraSpace;
361 hotseatBarBottomPaddingPx += extraSpace;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700362
Jon Miranda3f9bab22017-07-28 14:50:17 -0700363 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700364 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700365 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800366 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800367
368 // This is done last, after iconSizePx is calculated above.
Samuel Fufac96fa242019-09-26 23:06:32 -0700369 mDotRendererWorkSpace = new DotRenderer(iconSizePx, IconShape.getShapePath(),
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700370 IconShape.DEFAULT_PATH_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700371 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
372 new DotRenderer(allAppsIconSizePx, IconShape.getShapePath(),
373 IconShape.DEFAULT_PATH_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700374 }
375
Jon Mirandae126d722021-02-25 10:45:20 -0500376 private void setCellLayoutBorderSpacing(int borderSpacing) {
Jon Miranda823da222021-03-23 08:08:45 -0400377 cellLayoutBorderSpacingPx = isScalableGrid ? borderSpacing : 0;
Jon Mirandae126d722021-02-25 10:45:20 -0500378 }
379
Jon Miranda611dba42021-03-02 14:12:13 -0500380 /**
381 * We inset the widget padding added by the system and instead rely on the border spacing
382 * between cells to create reliable consistency between widgets
383 */
384 public boolean shouldInsetWidgets() {
385 Rect widgetPadding = inv.defaultWidgetPadding;
386
387 // Check all sides to ensure that the widget won't overlap into another cell.
388 return cellLayoutBorderSpacingPx > widgetPadding.left
389 && cellLayoutBorderSpacingPx > widgetPadding.top
390 && cellLayoutBorderSpacingPx > widgetPadding.right
391 && cellLayoutBorderSpacingPx > widgetPadding.bottom;
392 }
Jon Mirandae126d722021-02-25 10:45:20 -0500393
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700394 public Builder toBuilder(Context context) {
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800395 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700396 return new Builder(context, inv, mInfo)
397 .setSizeRange(size, size)
398 .setSize(widthPx, heightPx)
Sunny Goyal0addbf02020-04-28 14:17:35 -0700399 .setWindowPosition(windowX, windowY)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700400 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800401 }
402
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700403 public DeviceProfile copy(Context context) {
404 return toBuilder(context).build();
405 }
406
407 /**
408 * TODO: Move this to the builder as part of setMultiWindowMode
409 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700410 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700411 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
412 // the system decor is always excluded.
Sunny Goyalb46703d2020-05-27 17:52:03 -0700413 Point mwSize = new Point(Math.min(availableWidthPx, windowBounds.availableSize.x),
414 Math.min(availableHeightPx, windowBounds.availableSize.y));
Jon Mirandaa11380d2017-08-24 11:30:03 -0700415
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700416 DeviceProfile profile = toBuilder(context)
417 .setSizeRange(mwSize, mwSize)
Sunny Goyalb46703d2020-05-27 17:52:03 -0700418 .setSize(windowBounds.bounds.width(), windowBounds.bounds.height())
419 .setWindowPosition(windowBounds.bounds.left, windowBounds.bounds.top)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700420 .setMultiWindowMode(true)
421 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800422
Jon Miranda941375e2021-03-31 13:10:45 -0400423 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda93e1f042016-11-11 14:13:04 -0800424
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800425 // We use these scales to measure and layout the widgets using their full invariant profile
426 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
427 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
428 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
429 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800430 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800431
Jon Miranda93e1f042016-11-11 14:13:04 -0800432 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700433 }
434
Adam Cohen63f1ec02014-08-12 09:23:13 -0700435 /**
Sunny Goyalb46703d2020-05-27 17:52:03 -0700436 * Inverse of {@link #getMultiWindowProfile(Context, WindowBounds)}
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800437 * @return device profile corresponding to the current orientation in non multi-window mode.
438 */
439 public DeviceProfile getFullScreenProfile() {
440 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
441 }
442
443 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400444 * Checks if there is enough space for labels on the workspace.
445 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700446 * It is important to call this method after the All Apps variables have been set.
447 */
Jon Miranda941375e2021-03-31 13:10:45 -0400448 private void hideWorkspaceLabelsIfNotEnoughSpace() {
449 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
450 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
451 - iconTextHeight;
452
453 // We want enough space so that the text is closer to its corresponding icon.
454 if (workspaceCellPaddingY < iconTextHeight) {
455 iconTextSizePx = 0;
456 iconDrawablePaddingPx = 0;
457 cellHeightPx = iconSizePx;
458 autoResizeAllAppsCells();
459 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700460 }
Jon Miranda1091e532017-07-21 13:31:50 -0700461
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700462 /**
463 * Re-computes the all-apps cell size to be independent of workspace
464 */
465 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400466 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
467 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700468 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400469 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700470 }
471
Jon Miranda228877d2021-02-09 11:05:00 -0500472 /**
473 * Returns the amount of extra (or unused) vertical space.
474 */
475 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700476 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800477
Jon Mirandae126d722021-02-25 10:45:20 -0500478 Point workspacePadding = getTotalWorkspacePadding();
479
480 // Check to see if the icons fit within the available height.
481 float usedHeight = getCellLayoutHeight();
482 final int maxHeight = availableHeightPx - workspacePadding.y;
Jon Miranda228877d2021-02-09 11:05:00 -0500483 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500484 float scaleY = maxHeight / usedHeight;
485 boolean shouldScale = scaleY < 1f;
486
487 float scaleX = 1f;
488 if (isScalableGrid) {
489 // We scale to fit the cellWidth and cellHeight in the available space.
490 // The benefit of scalable grids is that we can get consistent aspect ratios between
491 // devices.
492 float usedWidth = (cellWidthPx * inv.numColumns)
493 + (cellLayoutBorderSpacingPx * (inv.numColumns - 1))
494 + (desiredWorkspaceLeftRightMarginPx * 2);
495 // We do not subtract padding here, as we also scale the workspace padding if needed.
496 scaleX = availableWidthPx / usedWidth;
497 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700498 }
Jon Mirandae126d722021-02-25 10:45:20 -0500499
500 if (shouldScale) {
501 float scale = Math.min(scaleX, scaleY);
502 updateIconSize(scale, res);
503 extraHeight = Math.max(0, maxHeight - getCellLayoutHeight());
504 }
505
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700506 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500507 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700508 }
509
Jon Mirandae126d722021-02-25 10:45:20 -0500510 private int getCellLayoutHeight() {
511 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacingPx * (inv.numRows - 1));
512 }
513
Jon Miranda6f7e9702019-09-16 14:44:14 -0700514 /**
515 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
516 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
517 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
518 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800519 public void updateIconSize(float scale, Resources res) {
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400520 iconScale = scale;
521
Jon Miranda18751b62017-07-31 17:25:27 -0700522 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800523 final boolean isVerticalLayout = isVerticalBarLayout();
Andras Kloczl6057cb92021-04-07 13:45:17 +0200524 float invIconSizeDp = isLandscape ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700525 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Andras Kloczl6057cb92021-04-07 13:45:17 +0200526 float invIconTextSizeSp = isLandscape ? inv.landscapeIconTextSize : inv.iconTextSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700527 iconTextSizePx = (int) (Utilities.pxFromSp(invIconTextSizeSp, mMetrics) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700528 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700529
Jon Mirandae126d722021-02-25 10:45:20 -0500530 setCellLayoutBorderSpacing((int) (cellLayoutBorderSpacingOriginalPx * scale));
531
532 if (isScalableGrid) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700533 cellWidthPx = pxFromDp(inv.minCellWidth, mMetrics, scale);
534 cellHeightPx = pxFromDp(inv.minCellHeight, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500535 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
536 + Utilities.calculateTextHeight(iconTextSizePx);
537 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
538 desiredWorkspaceLeftRightMarginPx = (int) (desiredWorkspaceLeftRightOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500539 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500540 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda228877d2021-02-09 11:05:00 -0500541 cellHeightPx = iconSizePx + iconDrawablePaddingPx
542 + Utilities.calculateTextHeight(iconTextSizePx);
543 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
544 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
545 && !isMultiWindowMode) {
546 // Ensures that the label is closer to its corresponding icon. This is not an issue
547 // with vertical bar layout or multi-window mode since the issue is handled
548 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
549 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
550 iconDrawablePaddingPx = cellPaddingY;
551 }
Jon Miranda846455e2017-10-02 14:58:52 -0700552 }
Jon Miranda18751b62017-07-31 17:25:27 -0700553
Sunny Goyalae190ff2020-04-14 00:19:01 +0000554 // All apps
555 if (allAppsHasDifferentNumColumns()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700556 allAppsIconSizePx = pxFromDp(inv.allAppsIconSize, mMetrics);
557 allAppsIconTextSizePx = Utilities.pxFromSp(inv.allAppsIconTextSize, mMetrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000558 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda941375e2021-03-31 13:10:45 -0400559 autoResizeAllAppsCells();
Sunny Goyalae190ff2020-04-14 00:19:01 +0000560 } else {
561 allAppsIconSizePx = iconSizePx;
562 allAppsIconTextSizePx = iconTextSizePx;
563 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
564 allAppsCellHeightPx = getCellSize().y;
565 }
566 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700567
Jon Miranda941375e2021-03-31 13:10:45 -0400568 if (isVerticalLayout) {
569 hideWorkspaceLabelsIfNotEnoughSpace();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700570 }
Winson Chungb3800242013-10-24 11:01:54 -0700571
Winson Chungb3800242013-10-24 11:01:54 -0700572 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800573 if (isVerticalLayout) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700574 hotseatBarSizePx = iconSizePx + hotseatBarSidePaddingStartPx
575 + hotseatBarSidePaddingEndPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700576 }
Jon Miranda5044c352017-08-09 11:37:59 -0700577 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700578
Sunny Goyal07b69292018-01-08 14:19:34 -0800579 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700580 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700581 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700582 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
583 workspaceSpringLoadShrinkFactor = Math.min(
584 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
585 1 - (minRequiredHeight / expectedWorkspaceHeight));
586 } else {
587 workspaceSpringLoadShrinkFactor =
588 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
589 }
590
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700591 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700592 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
593 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700594 }
595
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700596 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700597 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700598
Jon Mirandae126d722021-02-25 10:45:20 -0500599 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
600
Jon Mirandac1b23992016-12-13 08:57:36 -0800601 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700602 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800603 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800604
605 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500606 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Jon Miranda823da222021-03-23 08:08:45 -0400607 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacingPx);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800608 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500609 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800610 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800611
612 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500613 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Jon Miranda823da222021-03-23 08:08:45 -0400614 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacingPx);
Jon Mirandae126d722021-02-25 10:45:20 -0500615 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
616 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800617 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800618
619 float scale = Math.min(scaleX, scaleY);
620 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700621 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700622 }
623 }
624
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700625 private void updateFolderCellSize(float scale, Resources res) {
Jon Mirandae126d722021-02-25 10:45:20 -0500626 float invIconSizeDp = isVerticalBarLayout() ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700627 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
628 folderChildTextSizePx = pxFromDp(inv.iconTextSize, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500629 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700630
631 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700632
Jon Miranda823da222021-03-23 08:08:45 -0400633 if (isScalableGrid) {
634 folderCellWidthPx = (int) (cellWidthPx * scale);
635 folderCellHeightPx = (int) (cellHeightPx * scale);
636
637 int borderSpacing = (int) (cellLayoutBorderSpacingOriginalPx * scale);
638 folderCellLayoutBorderSpacingPx = borderSpacing;
639 folderContentPaddingLeftRight = borderSpacing;
640 folderContentPaddingTop = borderSpacing;
641 } else {
642 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
643 * scale);
644 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
645 * scale);
646
647 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
648 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
649 }
650
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000651 folderChildDrawablePaddingPx = Math.max(0,
652 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700653 }
654
Winson1f064272016-07-18 17:18:02 -0700655 public void updateInsets(Rect insets) {
656 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800657 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700658 }
659
Sunny Goyalae6e3182019-04-30 12:04:37 -0700660 /**
661 * The current device insets. This is generally same as the insets being dispatched to
662 * {@link Insettable} elements, but can differ if the element is using a different profile.
663 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800664 public Rect getInsets() {
665 return mInsets;
666 }
667
Sunny Goyal756cd262015-08-20 12:33:21 -0700668 public Point getCellSize() {
Jon Miranda6f7e9702019-09-16 14:44:14 -0700669 return getCellSize(inv.numColumns, inv.numRows);
670 }
671
672 private Point getCellSize(int numColumns, int numRows) {
Sunny Goyal756cd262015-08-20 12:33:21 -0700673 Point result = new Point();
674 // Since we are only concerned with the overall padding, layout direction does
675 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700676 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700677 result.x = calculateCellWidth(availableWidthPx - padding.x
Jon Miranda228877d2021-02-09 11:05:00 -0500678 - cellLayoutPaddingLeftRightPx * 2, cellLayoutBorderSpacingPx, numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700679 result.y = calculateCellHeight(availableHeightPx - padding.y
Jon Miranda228877d2021-02-09 11:05:00 -0500680 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacingPx, numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700681 return result;
682 }
683
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700684 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800685 updateWorkspacePadding();
686 return new Point(workspacePadding.left + workspacePadding.right,
687 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700688 }
689
690 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800691 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
692 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700693 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800694 private void updateWorkspacePadding() {
695 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700696 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800697 padding.top = 0;
698 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800699 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700700 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700701 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700702 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700703 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700704 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700705 }
Winson Chungb3800242013-10-24 11:01:54 -0700706 } else {
Tony Wickhama29a0462021-03-02 12:28:25 -0800707 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
708 int paddingBottom = hotseatTop + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500709 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700710 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700711 // Pad the left and right of the workspace to ensure consistent spacing
712 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700713 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800714 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800715 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700716 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800717 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Tony Wickhama29a0462021-03-02 12:28:25 -0800718 int hotseatVerticalPadding = isTaskbarPresent ? 0
719 : hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700720 int availablePaddingY = Math.max(0, heightPx - edgeMarginPx - paddingBottom
Tony Wickhama29a0462021-03-02 12:28:25 -0800721 - (2 * inv.numRows * cellHeightPx) - hotseatVerticalPadding);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700722 padding.set(availablePaddingX / 2, edgeMarginPx + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700723 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100724
725 if (FeatureFlags.ENABLE_TWO_PANEL_HOME.get()) {
726 padding.set(0, padding.top, 0, padding.bottom);
727 }
Winson Chungb3800242013-10-24 11:01:54 -0700728 } else {
729 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700730 padding.set(desiredWorkspaceLeftRightMarginPx,
Jon Mirandae126d722021-02-25 10:45:20 -0500731 workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx),
Winson1f064272016-07-18 17:18:02 -0700732 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700733 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700734 }
735 }
Winson Chungb3800242013-10-24 11:01:54 -0700736 }
737
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700738 public Rect getHotseatLayoutPadding() {
739 if (isVerticalBarLayout()) {
740 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700741 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
742 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700743 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700744 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
745 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700746 }
747 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700748 // We want the edges of the hotseat to line up with the edges of the workspace, but the
749 // icons in the hotseat are a different size, and so don't line up perfectly. To account
750 // for this, we pad the left and right of the hotseat with half of the difference of a
751 // workspace cell vs a hotseat cell.
752 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700753 float hotseatCellWidth = (float) widthPx / inv.numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700754 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
755 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700756 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
757 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700758 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700759 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
760 + mInsets.right,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700761 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
762 }
763 return mHotseatPadding;
764 }
765
Winsonfadbe8f2016-07-22 16:35:37 -0700766 /**
767 * @return the bounds for which the open folders should be contained within
768 */
769 public Rect getAbsoluteOpenFolderBounds() {
770 if (isVerticalBarLayout()) {
771 // Folders should only appear right of the drop target bar and left of the hotseat
772 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
773 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700774 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700775 mInsets.top + availableHeightPx);
776 } else {
777 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -0800778 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800779 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700780 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800781 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -0800782 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -0700783 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700784 }
785 }
786
Jon Miranda228877d2021-02-09 11:05:00 -0500787 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
788 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700789 }
Jon Miranda228877d2021-02-09 11:05:00 -0500790 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
791 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700792 }
793
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700794 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700795 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
796 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
797 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700798 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700799 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700800 return isLandscape && transposeLayoutWithOrientation;
801 }
802
Sunny Goyal59d086c2018-05-07 17:31:40 -0700803 /**
Jon Miranda6f7e9702019-09-16 14:44:14 -0700804 * Returns true when the number of workspace columns and all apps columns differs.
805 */
806 private boolean allAppsHasDifferentNumColumns() {
807 return inv.numAllAppsColumns != inv.numColumns;
808 }
809
810 /**
Sunny Goyal59d086c2018-05-07 17:31:40 -0700811 * Updates orientation information and returns true if it has changed from the previous value.
812 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700813 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700814 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700815 boolean isSeascape = DisplayController.INSTANCE.get(context)
816 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700817 if (mIsSeascape != isSeascape) {
818 mIsSeascape = isSeascape;
819 return true;
820 }
821 }
822 return false;
823 }
824
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800825 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700826 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800827 }
828
Sunny Goyal07b69292018-01-08 14:19:34 -0800829 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700830 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700831 }
832
Sunny Goyalc13403c2016-11-18 23:44:48 -0800833 public int getCellHeight(@ContainerType int containerType) {
834 switch (containerType) {
835 case CellLayout.WORKSPACE:
836 return cellHeightPx;
837 case CellLayout.FOLDER:
838 return folderCellHeightPx;
839 case CellLayout.HOTSEAT:
840 return hotseatCellHeightPx;
841 default:
842 // ??
843 return 0;
844 }
845 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700846
Jon Miranda58561d42021-03-17 10:51:54 -0400847 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700848 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -0400849 }
850
851 public void dump(String prefix, PrintWriter writer) {
852 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -0700853 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -0400854
855 writer.println(prefix + "\tisTablet:" + isTablet);
856 writer.println(prefix + "\tisLargeTablet:" + isLargeTablet);
857 writer.println(prefix + "\tisPhone:" + isPhone);
858 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
859 + transposeLayoutWithOrientation);
860
861 writer.println(prefix + "\tisLandscape:" + isLandscape);
862 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
863
864 writer.println(prefix + pxToDpStr("windowX", windowX));
865 writer.println(prefix + pxToDpStr("windowY", windowY));
866 writer.println(prefix + pxToDpStr("widthPx", widthPx));
867 writer.println(prefix + pxToDpStr("heightPx", heightPx));
868
869 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
870 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
871
872 writer.println(prefix + "\taspectRatio:" + aspectRatio);
873
874 writer.println(prefix + "\tisScalableGrid:" + isScalableGrid);
875
876 writer.println(prefix + "\tinv.minCellWidth:" + inv.minCellWidth + "dp");
877 writer.println(prefix + "\tinv.minCellHeight:" + inv.minCellHeight + "dp");
878
879 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
880 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
881
882 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
883 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
884
885 writer.println(prefix + "\tinv.iconSize:" + inv.iconSize + "dp");
886 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
887 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
888 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
889
890 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
891 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
892 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
893 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
894 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
895 folderChildDrawablePaddingPx));
Jon Miranda823da222021-03-23 08:08:45 -0400896 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacingPx",
897 folderCellLayoutBorderSpacingPx));
Jon Miranda58561d42021-03-17 10:51:54 -0400898
899 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacingPx",
900 cellLayoutBorderSpacingPx));
901 writer.println(prefix + pxToDpStr("desiredWorkspaceLeftRightMarginPx",
902 desiredWorkspaceLeftRightMarginPx));
903
904 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
905 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
906 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
907 allAppsIconDrawablePaddingPx));
908 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
909
910 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
911 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
912 writer.println(prefix + pxToDpStr("hotseatBarTopPaddingPx", hotseatBarTopPaddingPx));
913 writer.println(prefix + pxToDpStr("hotseatBarBottomPaddingPx", hotseatBarBottomPaddingPx));
914 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
915 hotseatBarSidePaddingStartPx));
916 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
917 hotseatBarSidePaddingEndPx));
918
919 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
920
921 writer.println(prefix + pxToDpStr("taskbarSize", taskbarSize));
922 writer.println(prefix + pxToDpStr("nonOverlappingTaskbarInset",
923 nonOverlappingTaskbarInset));
924
925 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
926 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
927 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
928 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
929
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400930 writer.println(prefix + pxToDpStr("scaleToFit", iconScale));
Jon Miranda58561d42021-03-17 10:51:54 -0400931 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400932
933 if (inv.devicePaddings != null) {
934 int unscaledExtraSpace = (int) (extraSpace / iconScale);
935 writer.println(prefix + pxToDpStr("maxEmptySpace",
936 inv.devicePaddings.getDevicePadding(unscaledExtraSpace).getMaxEmptySpacePx()));
937 }
Jon Miranda58561d42021-03-17 10:51:54 -0400938 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
939 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
940 writer.println(prefix + pxToDpStr("extraHotseatBottomPadding", extraHotseatBottomPadding));
941 }
942
Sunny Goyalfd58da62020-08-11 12:06:49 -0700943 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -0700944 Configuration config = new Configuration(c.getResources().getConfiguration());
945 config.orientation = orientation;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700946 config.densityDpi = info.densityDpi;
Sunny Goyal1890f672020-04-30 12:28:00 -0700947 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700948 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800949
950 /**
951 * Callback when a component changes the DeviceProfile associated with it, as a result of
952 * configuration change
953 */
954 public interface OnDeviceProfileChangeListener {
955
956 /**
957 * Called when the device profile is reassigned. Note that for layout and measurements, it
958 * is sufficient to listen for inset changes. Use this callback when you need to perform
959 * a one time operation.
960 */
961 void onDeviceProfileChanged(DeviceProfile dp);
962 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700963
964 public static class Builder {
965 private Context mContext;
966 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -0700967 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700968
Sunny Goyal0addbf02020-04-28 14:17:35 -0700969 private final Point mWindowPosition = new Point();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700970 private Point mMinSize, mMaxSize;
971 private int mWidth, mHeight;
972
973 private boolean mIsLandscape;
974 private boolean mIsMultiWindowMode = false;
975 private boolean mTransposeLayoutWithOrientation;
976
Sunny Goyalfd58da62020-08-11 12:06:49 -0700977 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700978 mContext = context;
979 mInv = inv;
980 mInfo = info;
981 mTransposeLayoutWithOrientation = context.getResources()
982 .getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung8e64bba2021-04-20 13:26:26 -0700983 if (TestProtocol.sDebugTracing) {
984 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED,
985 "transposeLayout=" + mTransposeLayoutWithOrientation);
986 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700987 }
988
989 public Builder setSizeRange(Point minSize, Point maxSize) {
990 mMinSize = minSize;
991 mMaxSize = maxSize;
992 return this;
993 }
994
995 public Builder setSize(int width, int height) {
996 mWidth = width;
997 mHeight = height;
998 mIsLandscape = mWidth > mHeight;
Winson Chung8e64bba2021-04-20 13:26:26 -0700999 if (TestProtocol.sDebugTracing) {
1000 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED,
1001 "isLandscape=" + mIsLandscape + " w=" + mWidth + " h=" + mHeight);
1002 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001003 return this;
1004 }
1005
1006 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
1007 mIsMultiWindowMode = isMultiWindowMode;
1008 return this;
1009 }
1010
Sunny Goyal0addbf02020-04-28 14:17:35 -07001011 /**
1012 * Sets the window position if not full-screen
1013 */
1014 public Builder setWindowPosition(int x, int y) {
1015 mWindowPosition.set(x, y);
1016 return this;
1017 }
1018
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001019 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
1020 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
1021 return this;
1022 }
1023
1024 public DeviceProfile build() {
1025 return new DeviceProfile(mContext, mInv, mInfo, mMinSize, mMaxSize,
1026 mWidth, mHeight, mIsLandscape, mIsMultiWindowMode,
Sunny Goyal0addbf02020-04-28 14:17:35 -07001027 mTransposeLayoutWithOrientation, mWindowPosition);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001028 }
1029 }
1030
Winson Chungb3800242013-10-24 11:01:54 -07001031}