blob: e5ad24341b039eed2a1680edd9d2ee998e692e11 [file] [log] [blame]
Winson Chungb3800242013-10-24 11:01:54 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Alex Chaua02eddc2021-04-29 00:36:06 +010019import static android.util.DisplayMetrics.DENSITY_DEVICE_STABLE;
Sunny Goyal35c7b192021-04-20 16:51:10 -070020import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
21
Jon Mirandae126d722021-02-25 10:45:20 -050022import static com.android.launcher3.ResourceUtils.pxFromDp;
Jon Miranda58561d42021-03-17 10:51:54 -040023import static com.android.launcher3.Utilities.dpiFromPx;
Alex Chaua02eddc2021-04-29 00:36:06 +010024import static com.android.launcher3.util.WindowManagerCompat.MIN_TABLET_WIDTH;
Jon Miranda228877d2021-02-09 11:05:00 -050025
Sunny Goyal35c7b192021-04-20 16:51:10 -070026import android.annotation.SuppressLint;
Winson Chungb3800242013-10-24 11:01:54 -070027import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070028import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.content.res.Resources;
Sunny Goyal8b9919d2021-04-07 14:45:17 -070030import android.graphics.Path;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080032import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070033import android.graphics.Rect;
Sunny Goyal35c7b192021-04-20 16:51:10 -070034import android.hardware.display.DisplayManager;
35import android.util.DisplayMetrics;
Sunny Goyal59d086c2018-05-07 17:31:40 -070036import android.view.Surface;
Tony Wickham15883892021-02-12 11:24:40 -080037import android.view.WindowInsets;
38import android.view.WindowManager;
Winson Chungb3800242013-10-24 11:01:54 -070039
Sunny Goyalc13403c2016-11-18 23:44:48 -080040import com.android.launcher3.CellLayout.ContainerType;
Jon Miranda228877d2021-02-09 11:05:00 -050041import com.android.launcher3.DevicePaddings.DevicePadding;
Tony Wickham15883892021-02-12 11:24:40 -080042import com.android.launcher3.config.FeatureFlags;
Tony Wickham8912b042018-11-29 15:28:53 -080043import com.android.launcher3.icons.DotRenderer;
Sunny Goyal8b9919d2021-04-07 14:45:17 -070044import com.android.launcher3.icons.GraphicsUtils;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070045import com.android.launcher3.icons.IconNormalizer;
Sunny Goyalfd58da62020-08-11 12:06:49 -070046import com.android.launcher3.util.DisplayController;
47import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb46703d2020-05-27 17:52:03 -070048import com.android.launcher3.util.WindowBounds;
Winson1f064272016-07-18 17:18:02 -070049
Jon Miranda58561d42021-03-17 10:51:54 -040050import java.io.PrintWriter;
51
Sunny Goyal35c7b192021-04-20 16:51:10 -070052@SuppressLint("NewApi")
Winson Chungb3800242013-10-24 11:01:54 -070053public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070054
Sunny Goyal8b9919d2021-04-07 14:45:17 -070055 private static final int DEFAULT_DOT_SIZE = 100;
Sunny Goyal1890f672020-04-30 12:28:00 -070056
Adam Cohen2e6da152015-05-06 11:42:25 -070057 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070058 private final Info mInfo;
Sunny Goyal35c7b192021-04-20 16:51:10 -070059 private final DisplayMetrics mMetrics;
Winson Chungbe876472014-05-14 14:15:20 -070060
Sunny Goyalc6205602015-05-21 20:46:33 -070061 // Device properties
62 public final boolean isTablet;
Sunny Goyalc6205602015-05-21 20:46:33 -070063 public final boolean isPhone;
64 public final boolean transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -070065 public final boolean isTwoPanels;
Alex Chaua02eddc2021-04-29 00:36:06 +010066 public final boolean allowRotation;
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;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700165 public final int numShownAllAppsColumns;
Winson1f064272016-07-18 17:18:02 -0700166 public float allAppsIconTextSizePx;
167
Zak Cohen334efeb2021-03-19 16:42:07 -0700168 // Overview
Zak Cohen334efeb2021-03-19 16:42:07 -0700169 public int overviewTaskMarginPx;
Alex Chaudedbc8a2021-03-17 16:53:26 +0000170 public int overviewTaskIconSizePx;
171 public int overviewTaskThumbnailTopMarginPx;
Zak Cohen334efeb2021-03-19 16:42:07 -0700172
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800173 // Widgets
174 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
175
Sunny Goyal47328fd2016-05-25 18:56:41 -0700176 // Drop Target
177 public int dropTargetBarSizePx;
Alex Chaua02eddc2021-04-29 00:36:06 +0100178 public int dropTargetDragPaddingPx;
179 public int dropTargetTextSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700180
Winson1f064272016-07-18 17:18:02 -0700181 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800182 private final Rect mInsets = new Rect();
183 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700184 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800185 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700186 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700187
Tony Wickhamf34bee82018-12-03 18:11:39 -0800188 // Notification dots
Samuel Fufac96fa242019-09-26 23:06:32 -0700189 public DotRenderer mDotRendererWorkSpace;
190 public DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800191
Tony Wickham15883892021-02-12 11:24:40 -0800192 // Taskbar
193 public boolean isTaskbarPresent;
194 public int taskbarSize;
Tony Wickham145b7fd2021-03-11 10:03:12 -0800195 // How much of the bottom inset is due to Taskbar rather than other system elements.
196 public int nonOverlappingTaskbarInset;
Tony Wickham15883892021-02-12 11:24:40 -0800197
Alex Chaua02eddc2021-04-29 00:36:06 +0100198 // DragController
199 public int flingToDeleteThresholdVelocity;
200
Sunny Goyal19ff7282021-04-22 10:12:54 -0700201 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info, WindowBounds windowBounds,
Sunny Goyal0addbf02020-04-28 14:17:35 -0700202 boolean isMultiWindowMode, boolean transposeLayoutWithOrientation,
Sunny Goyal19ff7282021-04-22 10:12:54 -0700203 boolean useTwoPanels) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700204
Adam Cohen2e6da152015-05-06 11:42:25 -0700205 this.inv = inv;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700206 this.isLandscape = windowBounds.isLandscape();
Sunny Goyald70e75a2018-02-22 10:07:32 -0800207 this.isMultiWindowMode = isMultiWindowMode;
Jon Mirandae126d722021-02-25 10:45:20 -0500208 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700209 windowX = windowBounds.bounds.left;
210 windowY = windowBounds.bounds.top;
Winson Chungb3800242013-10-24 11:01:54 -0700211
Jon Mirandae126d722021-02-25 10:45:20 -0500212 isScalableGrid = inv.isScalable && !isVerticalBarLayout() && !isMultiWindowMode;
213
Jon Miranda7e183c32018-06-20 11:05:27 -0700214 // Determine sizes.
Sunny Goyal19ff7282021-04-22 10:12:54 -0700215 widthPx = windowBounds.bounds.width();
216 heightPx = windowBounds.bounds.height();
217 availableWidthPx = windowBounds.availableSize.x;
218 int nonFinalAvailableHeightPx = windowBounds.availableSize.y;
Jon Miranda7e183c32018-06-20 11:05:27 -0700219
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700220 mInfo = info;
Alex Chaua02eddc2021-04-29 00:36:06 +0100221 // If the device's pixel density was scaled (usually via settings for A11y), use the
222 // original dimensions to determine if rotation is allowed of not.
223 float originalSmallestWidth = dpiFromPx(Math.min(widthPx, heightPx), DENSITY_DEVICE_STABLE);
224 allowRotation = originalSmallestWidth >= MIN_TABLET_WIDTH;
225 // Tablet UI does not support emulated landscape.
226 isTablet = allowRotation && info.isTablet(windowBounds);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700227 isPhone = !isTablet;
228 isTwoPanels = isTablet && useTwoPanels;
Adam Cohen2e6da152015-05-06 11:42:25 -0700229
Jon Mirandabba64512019-03-27 10:54:17 -0700230 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda7e183c32018-06-20 11:05:27 -0700231 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700232
233 // Some more constants
Sunny Goyal1890f672020-04-30 12:28:00 -0700234 context = getContext(context, info, isVerticalBarLayout()
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700235 ? Configuration.ORIENTATION_LANDSCAPE
236 : Configuration.ORIENTATION_PORTRAIT);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700237 mMetrics = context.getResources().getDisplayMetrics();
Sunny Goyal1890f672020-04-30 12:28:00 -0700238 final Resources res = context.getResources();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700239
Tony Wickham15883892021-02-12 11:24:40 -0800240 isTaskbarPresent = isTablet && FeatureFlags.ENABLE_TASKBAR.get();
241 if (isTaskbarPresent) {
242 // Taskbar will be added later, but provides bottom insets that we should subtract
243 // from availableHeightPx.
244 taskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_size);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700245 WindowInsets windowInsets =
246 context.createWindowContext(
247 context.getSystemService(DisplayManager.class).getDisplay(mInfo.id),
248 TYPE_APPLICATION, null)
249 .getSystemService(WindowManager.class)
Tony Wickham15883892021-02-12 11:24:40 -0800250 .getCurrentWindowMetrics().getWindowInsets();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700251 nonOverlappingTaskbarInset = taskbarSize - windowInsets.getSystemWindowInsetBottom();
Tony Wickham15883892021-02-12 11:24:40 -0800252 if (nonOverlappingTaskbarInset > 0) {
253 nonFinalAvailableHeightPx -= nonOverlappingTaskbarInset;
254 }
255 }
256 availableHeightPx = nonFinalAvailableHeightPx;
257
Winson Chungb3800242013-10-24 11:01:54 -0700258 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700259
Jon Mirandae126d722021-02-25 10:45:20 -0500260 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : isScalableGrid
261 ? res.getDimensionPixelSize(R.dimen.scalable_grid_left_right_margin)
262 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
263 desiredWorkspaceLeftRightOriginalPx = desiredWorkspaceLeftRightMarginPx;
264
Samuel Fufaee9aff92021-04-05 13:30:40 -0500265
266 allAppsOpenVerticalTranslate = res.getDimensionPixelSize(
267 R.dimen.all_apps_open_vertical_translate);
268
Jon Mirandae126d722021-02-25 10:45:20 -0500269 folderLabelTextScale = res.getFloat(R.dimen.folder_label_text_scale);
270 folderContentPaddingLeftRight =
271 res.getDimensionPixelSize(R.dimen.folder_content_padding_left_right);
272 folderContentPaddingTop = res.getDimensionPixelSize(R.dimen.folder_content_padding_top);
273
Sunny Goyal35c7b192021-04-20 16:51:10 -0700274 setCellLayoutBorderSpacing(pxFromDp(inv.borderSpacing, mMetrics, 1f));
Jon Mirandae126d722021-02-25 10:45:20 -0500275 cellLayoutBorderSpacingOriginalPx = cellLayoutBorderSpacingPx;
Jon Miranda823da222021-03-23 08:08:45 -0400276 folderCellLayoutBorderSpacingPx = cellLayoutBorderSpacingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500277
Jon Miranda5eacbb72018-07-31 11:14:46 -0700278 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
279 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Jon Mirandae126d722021-02-25 10:45:20 -0500280 int cellLayoutPadding = isScalableGrid
281 ? 0
282 : res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100283
Sunny Goyal19ff7282021-04-22 10:12:54 -0700284 if (isTwoPanels) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100285 cellLayoutPaddingLeftRightPx =
286 res.getDimensionPixelSize(R.dimen.two_panel_home_side_padding);
287 cellLayoutBottomPaddingPx = 0;
288 } else if (isLandscape) {
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700289 cellLayoutPaddingLeftRightPx = 0;
290 cellLayoutBottomPaddingPx = cellLayoutPadding;
291 } else {
292 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
293 cellLayoutBottomPaddingPx = 0;
294 }
295
Tony Wickham5edf9e22020-03-27 20:06:52 -0700296 workspacePageIndicatorHeight = res.getDimensionPixelSize(
297 R.dimen.workspace_page_indicator_height);
298 mWorkspacePageIndicatorOverlapWorkspace =
299 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700300
Winson Chungb3800242013-10-24 11:01:54 -0700301 iconDrawablePaddingOriginalPx =
302 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Alex Chaua02eddc2021-04-29 00:36:06 +0100303
Sunny Goyal47328fd2016-05-25 18:56:41 -0700304 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
Alex Chaua02eddc2021-04-29 00:36:06 +0100305 dropTargetDragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
306 dropTargetTextSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size);
307
Sunny Goyal47328fd2016-05-25 18:56:41 -0700308 workspaceSpringLoadedBottomSpace =
309 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700310
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700311 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700312
Sunny Goyal19ff7282021-04-22 10:12:54 -0700313 numShownHotseatIcons =
314 isTwoPanels ? inv.numDatabaseHotseatIcons : inv.numShownHotseatIcons;
315 numShownAllAppsColumns =
316 isTwoPanels ? inv.numDatabaseAllAppsColumns : inv.numAllAppsColumns;
Winson1f064272016-07-18 17:18:02 -0700317 hotseatBarTopPaddingPx =
318 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700319 hotseatBarBottomPaddingPx = (isTallDevice ? 0
320 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
321 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700322 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800323 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700324 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700325 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Jon Miranda228877d2021-02-09 11:05:00 -0500326 int hotseatExtraVerticalSize =
327 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700328 hotseatBarSizePx = pxFromDp(inv.iconSize, mMetrics, 1f)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700329 + (isVerticalBarLayout()
Sunny Goyal415f1732018-11-29 10:33:47 -0800330 ? (hotseatBarSidePaddingStartPx + hotseatBarSidePaddingEndPx)
Jon Miranda228877d2021-02-09 11:05:00 -0500331 : (hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx
Jon Mirandae126d722021-02-25 10:45:20 -0500332 + (isScalableGrid ? 0 : hotseatExtraVerticalSize)));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700333
Zak Cohen334efeb2021-03-19 16:42:07 -0700334 overviewTaskMarginPx = res.getDimensionPixelSize(R.dimen.overview_task_margin);
Alex Chaudedbc8a2021-03-17 16:53:26 +0000335 overviewTaskIconSizePx =
336 isTablet && FeatureFlags.ENABLE_OVERVIEW_GRID.get() ? res.getDimensionPixelSize(
337 R.dimen.task_thumbnail_icon_size_grid) : res.getDimensionPixelSize(
338 R.dimen.task_thumbnail_icon_size);
339 overviewTaskThumbnailTopMarginPx = overviewTaskIconSizePx + overviewTaskMarginPx * 2;
Zak Cohen334efeb2021-03-19 16:42:07 -0700340
Jon Miranda2ed276e2017-06-29 11:36:34 -0700341 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400342 extraSpace = updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700343 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400344 if (isScalableGrid && inv.devicePaddings != null) {
345 // Paddings were created assuming no scaling, so we first unscale the extra space.
346 int unscaledExtraSpace = (int) (extraSpace / iconScale);
347 DevicePadding padding = inv.devicePaddings.getDevicePadding(unscaledExtraSpace);
Jon Miranda228877d2021-02-09 11:05:00 -0500348
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400349 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
350 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
351 int paddingHotseatBottom = padding.getHotseatBottomPadding(unscaledExtraSpace);
352
353 workspaceTopPadding = Math.round(paddingWorkspaceTop * iconScale);
354 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * iconScale);
355 extraHotseatBottomPadding = Math.round(paddingHotseatBottom * iconScale);
356
Jon Miranda58561d42021-03-17 10:51:54 -0400357 hotseatBarSizePx += extraHotseatBottomPadding;
358 hotseatBarBottomPaddingPx += extraHotseatBottomPadding;
Jon Miranda228877d2021-02-09 11:05:00 -0500359 } else if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700360 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700361 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700362 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700363 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda58561d42021-03-17 10:51:54 -0400364 int extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2
Tony Wickham5edf9e22020-03-27 20:06:52 -0700365 - workspacePageIndicatorHeight;
Jon Miranda7e183c32018-06-20 11:05:27 -0700366 hotseatBarSizePx += extraSpace;
367 hotseatBarBottomPaddingPx += extraSpace;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700368
Jon Miranda3f9bab22017-07-28 14:50:17 -0700369 // Recalculate the available dimensions using the new hotseat size.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700370 updateAvailableDimensions(res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700371 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800372 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800373
Alex Chaua02eddc2021-04-29 00:36:06 +0100374 flingToDeleteThresholdVelocity = res.getDimensionPixelSize(
375 R.dimen.drag_flingToDeleteMinVelocity);
376
Tony Wickham1237df02017-02-24 08:59:36 -0800377 // This is done last, after iconSizePx is calculated above.
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700378 Path dotPath = GraphicsUtils.getShapePath(DEFAULT_DOT_SIZE);
379 mDotRendererWorkSpace = new DotRenderer(iconSizePx, dotPath, DEFAULT_DOT_SIZE);
Samuel Fufac96fa242019-09-26 23:06:32 -0700380 mDotRendererAllApps = iconSizePx == allAppsIconSizePx ? mDotRendererWorkSpace :
Sunny Goyal8b9919d2021-04-07 14:45:17 -0700381 new DotRenderer(allAppsIconSizePx, dotPath, DEFAULT_DOT_SIZE);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700382 }
383
Jon Mirandae126d722021-02-25 10:45:20 -0500384 private void setCellLayoutBorderSpacing(int borderSpacing) {
Jon Miranda823da222021-03-23 08:08:45 -0400385 cellLayoutBorderSpacingPx = isScalableGrid ? borderSpacing : 0;
Jon Mirandae126d722021-02-25 10:45:20 -0500386 }
387
Jon Miranda611dba42021-03-02 14:12:13 -0500388 /**
389 * We inset the widget padding added by the system and instead rely on the border spacing
390 * between cells to create reliable consistency between widgets
391 */
392 public boolean shouldInsetWidgets() {
393 Rect widgetPadding = inv.defaultWidgetPadding;
394
395 // Check all sides to ensure that the widget won't overlap into another cell.
396 return cellLayoutBorderSpacingPx > widgetPadding.left
397 && cellLayoutBorderSpacingPx > widgetPadding.top
398 && cellLayoutBorderSpacingPx > widgetPadding.right
399 && cellLayoutBorderSpacingPx > widgetPadding.bottom;
400 }
Jon Mirandae126d722021-02-25 10:45:20 -0500401
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700402 public Builder toBuilder(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700403 WindowBounds bounds =
404 new WindowBounds(widthPx, heightPx, availableWidthPx, availableHeightPx);
405 bounds.bounds.offsetTo(windowX, windowY);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700406 return new Builder(context, inv, mInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700407 .setWindowBounds(bounds)
408 .setUseTwoPanels(isTwoPanels)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700409 .setMultiWindowMode(isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800410 }
411
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700412 public DeviceProfile copy(Context context) {
413 return toBuilder(context).build();
414 }
415
416 /**
417 * TODO: Move this to the builder as part of setMultiWindowMode
418 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700419 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700420 DeviceProfile profile = toBuilder(context)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700421 .setWindowBounds(windowBounds)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700422 .setMultiWindowMode(true)
423 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800424
Jon Miranda941375e2021-03-31 13:10:45 -0400425 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda93e1f042016-11-11 14:13:04 -0800426
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800427 // We use these scales to measure and layout the widgets using their full invariant profile
428 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
429 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
430 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
431 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800432 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800433
Jon Miranda93e1f042016-11-11 14:13:04 -0800434 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700435 }
436
Adam Cohen63f1ec02014-08-12 09:23:13 -0700437 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400438 * Checks if there is enough space for labels on the workspace.
439 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700440 * It is important to call this method after the All Apps variables have been set.
441 */
Jon Miranda941375e2021-03-31 13:10:45 -0400442 private void hideWorkspaceLabelsIfNotEnoughSpace() {
443 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
444 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
445 - iconTextHeight;
446
447 // We want enough space so that the text is closer to its corresponding icon.
448 if (workspaceCellPaddingY < iconTextHeight) {
449 iconTextSizePx = 0;
450 iconDrawablePaddingPx = 0;
451 cellHeightPx = iconSizePx;
452 autoResizeAllAppsCells();
453 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700454 }
Jon Miranda1091e532017-07-21 13:31:50 -0700455
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700456 /**
457 * Re-computes the all-apps cell size to be independent of workspace
458 */
459 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400460 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
461 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700462 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400463 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700464 }
465
Jon Miranda228877d2021-02-09 11:05:00 -0500466 /**
467 * Returns the amount of extra (or unused) vertical space.
468 */
469 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700470 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800471
Jon Mirandae126d722021-02-25 10:45:20 -0500472 Point workspacePadding = getTotalWorkspacePadding();
473
474 // Check to see if the icons fit within the available height.
475 float usedHeight = getCellLayoutHeight();
476 final int maxHeight = availableHeightPx - workspacePadding.y;
Jon Miranda228877d2021-02-09 11:05:00 -0500477 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500478 float scaleY = maxHeight / usedHeight;
479 boolean shouldScale = scaleY < 1f;
480
481 float scaleX = 1f;
482 if (isScalableGrid) {
483 // We scale to fit the cellWidth and cellHeight in the available space.
484 // The benefit of scalable grids is that we can get consistent aspect ratios between
485 // devices.
486 float usedWidth = (cellWidthPx * inv.numColumns)
487 + (cellLayoutBorderSpacingPx * (inv.numColumns - 1))
488 + (desiredWorkspaceLeftRightMarginPx * 2);
489 // We do not subtract padding here, as we also scale the workspace padding if needed.
490 scaleX = availableWidthPx / usedWidth;
491 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700492 }
Jon Mirandae126d722021-02-25 10:45:20 -0500493
494 if (shouldScale) {
495 float scale = Math.min(scaleX, scaleY);
496 updateIconSize(scale, res);
497 extraHeight = Math.max(0, maxHeight - getCellLayoutHeight());
498 }
499
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700500 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500501 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700502 }
503
Jon Mirandae126d722021-02-25 10:45:20 -0500504 private int getCellLayoutHeight() {
505 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacingPx * (inv.numRows - 1));
506 }
507
Jon Miranda6f7e9702019-09-16 14:44:14 -0700508 /**
509 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
510 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
511 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
512 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800513 public void updateIconSize(float scale, Resources res) {
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400514 iconScale = scale;
515
Jon Miranda18751b62017-07-31 17:25:27 -0700516 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800517 final boolean isVerticalLayout = isVerticalBarLayout();
Andras Kloczl6057cb92021-04-07 13:45:17 +0200518 float invIconSizeDp = isLandscape ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700519 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Andras Kloczl6057cb92021-04-07 13:45:17 +0200520 float invIconTextSizeSp = isLandscape ? inv.landscapeIconTextSize : inv.iconTextSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700521 iconTextSizePx = (int) (Utilities.pxFromSp(invIconTextSizeSp, mMetrics) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700522 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700523
Jon Mirandae126d722021-02-25 10:45:20 -0500524 setCellLayoutBorderSpacing((int) (cellLayoutBorderSpacingOriginalPx * scale));
525
526 if (isScalableGrid) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700527 cellWidthPx = pxFromDp(inv.minCellWidth, mMetrics, scale);
528 cellHeightPx = pxFromDp(inv.minCellHeight, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500529 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
530 + Utilities.calculateTextHeight(iconTextSizePx);
531 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
532 desiredWorkspaceLeftRightMarginPx = (int) (desiredWorkspaceLeftRightOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500533 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500534 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda228877d2021-02-09 11:05:00 -0500535 cellHeightPx = iconSizePx + iconDrawablePaddingPx
536 + Utilities.calculateTextHeight(iconTextSizePx);
537 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
538 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
539 && !isMultiWindowMode) {
540 // Ensures that the label is closer to its corresponding icon. This is not an issue
541 // with vertical bar layout or multi-window mode since the issue is handled
542 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
543 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
544 iconDrawablePaddingPx = cellPaddingY;
545 }
Jon Miranda846455e2017-10-02 14:58:52 -0700546 }
Jon Miranda18751b62017-07-31 17:25:27 -0700547
Sunny Goyalae190ff2020-04-14 00:19:01 +0000548 // All apps
Sunny Goyal19ff7282021-04-22 10:12:54 -0700549 if (numShownAllAppsColumns != inv.numColumns) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700550 allAppsIconSizePx = pxFromDp(inv.allAppsIconSize, mMetrics);
551 allAppsIconTextSizePx = Utilities.pxFromSp(inv.allAppsIconTextSize, mMetrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000552 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Jon Miranda941375e2021-03-31 13:10:45 -0400553 autoResizeAllAppsCells();
Sunny Goyalae190ff2020-04-14 00:19:01 +0000554 } else {
555 allAppsIconSizePx = iconSizePx;
556 allAppsIconTextSizePx = iconTextSizePx;
557 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
558 allAppsCellHeightPx = getCellSize().y;
559 }
560 allAppsCellWidthPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700561
Jon Miranda941375e2021-03-31 13:10:45 -0400562 if (isVerticalLayout) {
563 hideWorkspaceLabelsIfNotEnoughSpace();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700564 }
Winson Chungb3800242013-10-24 11:01:54 -0700565
Winson Chungb3800242013-10-24 11:01:54 -0700566 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800567 if (isVerticalLayout) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700568 hotseatBarSizePx = iconSizePx + hotseatBarSidePaddingStartPx
569 + hotseatBarSidePaddingEndPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700570 }
Jon Miranda5044c352017-08-09 11:37:59 -0700571 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700572
Sunny Goyal07b69292018-01-08 14:19:34 -0800573 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700574 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Tony Wickham5edf9e22020-03-27 20:06:52 -0700575 - workspacePageIndicatorHeight - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700576 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
577 workspaceSpringLoadShrinkFactor = Math.min(
578 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
579 1 - (minRequiredHeight / expectedWorkspaceHeight));
580 } else {
581 workspaceSpringLoadShrinkFactor =
582 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
583 }
584
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700585 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700586 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
587 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700588 }
589
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700590 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700591 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700592
Jon Mirandae126d722021-02-25 10:45:20 -0500593 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
594
Jon Mirandac1b23992016-12-13 08:57:36 -0800595 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700596 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800597 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800598
599 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500600 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Jon Miranda823da222021-03-23 08:08:45 -0400601 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacingPx);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800602 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500603 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800604 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800605
606 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500607 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Jon Miranda823da222021-03-23 08:08:45 -0400608 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacingPx);
Jon Mirandae126d722021-02-25 10:45:20 -0500609 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
610 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800611 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800612
613 float scale = Math.min(scaleX, scaleY);
614 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700615 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700616 }
617 }
618
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700619 private void updateFolderCellSize(float scale, Resources res) {
Jon Mirandae126d722021-02-25 10:45:20 -0500620 float invIconSizeDp = isVerticalBarLayout() ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700621 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
622 folderChildTextSizePx = pxFromDp(inv.iconTextSize, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500623 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700624
625 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700626
Jon Miranda823da222021-03-23 08:08:45 -0400627 if (isScalableGrid) {
628 folderCellWidthPx = (int) (cellWidthPx * scale);
629 folderCellHeightPx = (int) (cellHeightPx * scale);
630
631 int borderSpacing = (int) (cellLayoutBorderSpacingOriginalPx * scale);
632 folderCellLayoutBorderSpacingPx = borderSpacing;
633 folderContentPaddingLeftRight = borderSpacing;
634 folderContentPaddingTop = borderSpacing;
635 } else {
636 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
637 * scale);
638 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
639 * scale);
640
641 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
642 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
643 }
644
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000645 folderChildDrawablePaddingPx = Math.max(0,
646 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700647 }
648
Winson1f064272016-07-18 17:18:02 -0700649 public void updateInsets(Rect insets) {
650 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800651 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700652 }
653
Sunny Goyalae6e3182019-04-30 12:04:37 -0700654 /**
655 * The current device insets. This is generally same as the insets being dispatched to
656 * {@link Insettable} elements, but can differ if the element is using a different profile.
657 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800658 public Rect getInsets() {
659 return mInsets;
660 }
661
Sunny Goyal756cd262015-08-20 12:33:21 -0700662 public Point getCellSize() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700663 return getCellSize(null);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700664 }
665
Sunny Goyal19ff7282021-04-22 10:12:54 -0700666 public Point getCellSize(Point result) {
667 if (result == null) {
668 result = new Point();
669 }
Sunny Goyal756cd262015-08-20 12:33:21 -0700670 // Since we are only concerned with the overall padding, layout direction does
671 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700672 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700673 result.x = calculateCellWidth(availableWidthPx - padding.x
Sunny Goyal19ff7282021-04-22 10:12:54 -0700674 - cellLayoutPaddingLeftRightPx * 2, cellLayoutBorderSpacingPx, inv.numColumns);
Jon Miranda18751b62017-07-31 17:25:27 -0700675 result.y = calculateCellHeight(availableHeightPx - padding.y
Sunny Goyal19ff7282021-04-22 10:12:54 -0700676 - cellLayoutBottomPaddingPx, cellLayoutBorderSpacingPx, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700677 return result;
678 }
679
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700680 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800681 updateWorkspacePadding();
682 return new Point(workspacePadding.left + workspacePadding.right,
683 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700684 }
685
686 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800687 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
688 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700689 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800690 private void updateWorkspacePadding() {
691 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700692 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800693 padding.top = 0;
694 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800695 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700696 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -0700697 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -0700698 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -0700699 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700700 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700701 }
Winson Chungb3800242013-10-24 11:01:54 -0700702 } else {
Tony Wickhama29a0462021-03-02 12:28:25 -0800703 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
704 int paddingBottom = hotseatTop + workspacePageIndicatorHeight
Jon Miranda228877d2021-02-09 11:05:00 -0500705 + workspaceBottomPadding - mWorkspacePageIndicatorOverlapWorkspace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700706 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700707 // Pad the left and right of the workspace to ensure consistent spacing
708 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700709 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800710 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800711 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700712 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800713 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Tony Wickhama29a0462021-03-02 12:28:25 -0800714 int hotseatVerticalPadding = isTaskbarPresent ? 0
715 : hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700716 int availablePaddingY = Math.max(0, heightPx - edgeMarginPx - paddingBottom
Tony Wickhama29a0462021-03-02 12:28:25 -0800717 - (2 * inv.numRows * cellHeightPx) - hotseatVerticalPadding);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700718 padding.set(availablePaddingX / 2, edgeMarginPx + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700719 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100720
Sunny Goyal19ff7282021-04-22 10:12:54 -0700721 if (isTwoPanels) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100722 padding.set(0, padding.top, 0, padding.bottom);
723 }
Winson Chungb3800242013-10-24 11:01:54 -0700724 } else {
725 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700726 padding.set(desiredWorkspaceLeftRightMarginPx,
Jon Mirandae126d722021-02-25 10:45:20 -0500727 workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx),
Winson1f064272016-07-18 17:18:02 -0700728 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700729 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700730 }
731 }
Winson Chungb3800242013-10-24 11:01:54 -0700732 }
733
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700734 public Rect getHotseatLayoutPadding() {
735 if (isVerticalBarLayout()) {
736 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700737 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
738 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700739 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700740 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
741 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700742 }
743 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700744 // We want the edges of the hotseat to line up with the edges of the workspace, but the
745 // icons in the hotseat are a different size, and so don't line up perfectly. To account
746 // for this, we pad the left and right of the hotseat with half of the difference of a
747 // workspace cell vs a hotseat cell.
748 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700749 float hotseatCellWidth = (float) widthPx / numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700750 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
751 mHotseatPadding.set(
Sunny Goyal786940a2020-06-02 02:31:31 -0700752 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx
753 + mInsets.left,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700754 hotseatBarTopPaddingPx,
Sunny Goyal786940a2020-06-02 02:31:31 -0700755 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx
756 + mInsets.right,
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700757 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
758 }
759 return mHotseatPadding;
760 }
761
Winsonfadbe8f2016-07-22 16:35:37 -0700762 /**
763 * @return the bounds for which the open folders should be contained within
764 */
765 public Rect getAbsoluteOpenFolderBounds() {
766 if (isVerticalBarLayout()) {
767 // Folders should only appear right of the drop target bar and left of the hotseat
768 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
769 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700770 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700771 mInsets.top + availableHeightPx);
772 } else {
773 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -0800774 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800775 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700776 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800777 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -0800778 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -0700779 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700780 }
781 }
782
Jon Miranda228877d2021-02-09 11:05:00 -0500783 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
784 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -0700785 }
Jon Miranda228877d2021-02-09 11:05:00 -0500786 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
787 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -0700788 }
789
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700790 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700791 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
792 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
793 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700794 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700795 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700796 return isLandscape && transposeLayoutWithOrientation;
797 }
798
Sunny Goyal59d086c2018-05-07 17:31:40 -0700799 /**
800 * Updates orientation information and returns true if it has changed from the previous value.
801 */
Winson Chung13c1c2c2019-09-06 11:46:19 -0700802 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700803 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700804 boolean isSeascape = DisplayController.INSTANCE.get(context)
805 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -0700806 if (mIsSeascape != isSeascape) {
807 mIsSeascape = isSeascape;
808 return true;
809 }
810 }
811 return false;
812 }
813
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800814 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700815 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800816 }
817
Sunny Goyal07b69292018-01-08 14:19:34 -0800818 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700819 return isVerticalBarLayout();
Winson Chungb3800242013-10-24 11:01:54 -0700820 }
821
Sunny Goyalc13403c2016-11-18 23:44:48 -0800822 public int getCellHeight(@ContainerType int containerType) {
823 switch (containerType) {
824 case CellLayout.WORKSPACE:
825 return cellHeightPx;
826 case CellLayout.FOLDER:
827 return folderCellHeightPx;
828 case CellLayout.HOTSEAT:
829 return hotseatCellHeightPx;
830 default:
831 // ??
832 return 0;
833 }
834 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700835
Jon Miranda58561d42021-03-17 10:51:54 -0400836 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700837 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -0400838 }
839
840 public void dump(String prefix, PrintWriter writer) {
841 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -0700842 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -0400843
Alex Chaua02eddc2021-04-29 00:36:06 +0100844 writer.println(prefix + "\tallowRotation:" + allowRotation);
Jon Miranda58561d42021-03-17 10:51:54 -0400845 writer.println(prefix + "\tisTablet:" + isTablet);
Jon Miranda58561d42021-03-17 10:51:54 -0400846 writer.println(prefix + "\tisPhone:" + isPhone);
847 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
848 + transposeLayoutWithOrientation);
849
850 writer.println(prefix + "\tisLandscape:" + isLandscape);
851 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700852 writer.println(prefix + "\tisTwoPanels:" + isTwoPanels);
Jon Miranda58561d42021-03-17 10:51:54 -0400853
854 writer.println(prefix + pxToDpStr("windowX", windowX));
855 writer.println(prefix + pxToDpStr("windowY", windowY));
856 writer.println(prefix + pxToDpStr("widthPx", widthPx));
857 writer.println(prefix + pxToDpStr("heightPx", heightPx));
858
859 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
860 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
861
862 writer.println(prefix + "\taspectRatio:" + aspectRatio);
863
864 writer.println(prefix + "\tisScalableGrid:" + isScalableGrid);
865
866 writer.println(prefix + "\tinv.minCellWidth:" + inv.minCellWidth + "dp");
867 writer.println(prefix + "\tinv.minCellHeight:" + inv.minCellHeight + "dp");
868
869 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
870 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
871
872 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
873 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
874
875 writer.println(prefix + "\tinv.iconSize:" + inv.iconSize + "dp");
876 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
877 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
878 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
879
880 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
881 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
882 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
883 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
884 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
885 folderChildDrawablePaddingPx));
Jon Miranda823da222021-03-23 08:08:45 -0400886 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacingPx",
887 folderCellLayoutBorderSpacingPx));
Jon Miranda58561d42021-03-17 10:51:54 -0400888
889 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacingPx",
890 cellLayoutBorderSpacingPx));
891 writer.println(prefix + pxToDpStr("desiredWorkspaceLeftRightMarginPx",
892 desiredWorkspaceLeftRightMarginPx));
893
894 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
895 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
896 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
897 allAppsIconDrawablePaddingPx));
898 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700899 writer.println(prefix + "\tnumShownAllAppsColumns: " + numShownAllAppsColumns);
Jon Miranda58561d42021-03-17 10:51:54 -0400900
901 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
902 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
903 writer.println(prefix + pxToDpStr("hotseatBarTopPaddingPx", hotseatBarTopPaddingPx));
904 writer.println(prefix + pxToDpStr("hotseatBarBottomPaddingPx", hotseatBarBottomPaddingPx));
905 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
906 hotseatBarSidePaddingStartPx));
907 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
908 hotseatBarSidePaddingEndPx));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700909 writer.println(prefix + "\tnumShownHotseatIcons: " + numShownHotseatIcons);
Jon Miranda58561d42021-03-17 10:51:54 -0400910
911 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
912
913 writer.println(prefix + pxToDpStr("taskbarSize", taskbarSize));
914 writer.println(prefix + pxToDpStr("nonOverlappingTaskbarInset",
915 nonOverlappingTaskbarInset));
916
917 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
918 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
919 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
920 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
921
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400922 writer.println(prefix + pxToDpStr("scaleToFit", iconScale));
Jon Miranda58561d42021-03-17 10:51:54 -0400923 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400924
925 if (inv.devicePaddings != null) {
926 int unscaledExtraSpace = (int) (extraSpace / iconScale);
927 writer.println(prefix + pxToDpStr("maxEmptySpace",
928 inv.devicePaddings.getDevicePadding(unscaledExtraSpace).getMaxEmptySpacePx()));
929 }
Jon Miranda58561d42021-03-17 10:51:54 -0400930 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
931 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
932 writer.println(prefix + pxToDpStr("extraHotseatBottomPadding", extraHotseatBottomPadding));
933 }
934
Sunny Goyalfd58da62020-08-11 12:06:49 -0700935 private static Context getContext(Context c, Info info, int orientation) {
Sunny Goyal1890f672020-04-30 12:28:00 -0700936 Configuration config = new Configuration(c.getResources().getConfiguration());
937 config.orientation = orientation;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700938 config.densityDpi = info.densityDpi;
Sunny Goyal1890f672020-04-30 12:28:00 -0700939 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700940 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800941
942 /**
943 * Callback when a component changes the DeviceProfile associated with it, as a result of
944 * configuration change
945 */
946 public interface OnDeviceProfileChangeListener {
947
948 /**
949 * Called when the device profile is reassigned. Note that for layout and measurements, it
950 * is sufficient to listen for inset changes. Use this callback when you need to perform
951 * a one time operation.
952 */
953 void onDeviceProfileChanged(DeviceProfile dp);
954 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700955
956 public static class Builder {
957 private Context mContext;
958 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -0700959 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700960
Sunny Goyal19ff7282021-04-22 10:12:54 -0700961 private WindowBounds mWindowBounds;
962 private boolean mUseTwoPanels;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700963
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700964 private boolean mIsMultiWindowMode = false;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700965 private Boolean mTransposeLayoutWithOrientation;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700966
Sunny Goyalfd58da62020-08-11 12:06:49 -0700967 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700968 mContext = context;
969 mInv = inv;
970 mInfo = info;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700971 }
972
973 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
974 mIsMultiWindowMode = isMultiWindowMode;
975 return this;
976 }
977
Sunny Goyal19ff7282021-04-22 10:12:54 -0700978 public Builder setUseTwoPanels(boolean useTwoPanels) {
979 mUseTwoPanels = useTwoPanels;
980 return this;
981 }
982
983
984 public Builder setWindowBounds(WindowBounds bounds) {
985 mWindowBounds = bounds;
Sunny Goyal0addbf02020-04-28 14:17:35 -0700986 return this;
987 }
988
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700989 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
990 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
991 return this;
992 }
993
994 public DeviceProfile build() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700995 if (mWindowBounds == null) {
996 throw new IllegalArgumentException("Window bounds not set");
997 }
998 if (mTransposeLayoutWithOrientation == null) {
999 mTransposeLayoutWithOrientation = !mInfo.isTablet(mWindowBounds);
1000 }
1001 return new DeviceProfile(mContext, mInv, mInfo, mWindowBounds,
1002 mIsMultiWindowMode, mTransposeLayoutWithOrientation, mUseTwoPanels);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001003 }
1004 }
1005
Winson Chungb3800242013-10-24 11:01:54 -07001006}