blob: 3b054c2be8a8fe5318aa11b449519a8d1a759893 [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
19import android.appwidget.AppWidgetHostView;
20import android.content.ComponentName;
21import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070022import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070023import android.content.res.Resources;
Winson Chungb3800242013-10-24 11:01:54 -070024import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080025import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070026import android.graphics.Rect;
27import android.util.DisplayMetrics;
Sunny Goyal59d086c2018-05-07 17:31:40 -070028import android.view.Surface;
Jon Miranda0128d3f2019-02-14 09:19:31 -080029import android.view.View;
Sunny Goyal59d086c2018-05-07 17:31:40 -070030import android.view.WindowManager;
Winson Chungb3800242013-10-24 11:01:54 -070031
Sunny Goyalc13403c2016-11-18 23:44:48 -080032import com.android.launcher3.CellLayout.ContainerType;
Tony Wickham8912b042018-11-29 15:28:53 -080033import com.android.launcher3.icons.DotRenderer;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070034import com.android.launcher3.icons.IconNormalizer;
Winson1f064272016-07-18 17:18:02 -070035
Jon Miranda0128d3f2019-02-14 09:19:31 -080036import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT;
37
Winson Chungb3800242013-10-24 11:01:54 -070038public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070039
Adam Cohen2e6da152015-05-06 11:42:25 -070040 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070041
Sunny Goyalc6205602015-05-21 20:46:33 -070042 // Device properties
43 public final boolean isTablet;
44 public final boolean isLargeTablet;
45 public final boolean isPhone;
46 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070047
Sunny Goyalc6205602015-05-21 20:46:33 -070048 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070049 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080050 public final boolean isMultiWindowMode;
51
Sunny Goyalc6205602015-05-21 20:46:33 -070052 public final int widthPx;
53 public final int heightPx;
54 public final int availableWidthPx;
55 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070056 /**
57 * The maximum amount of left/right workspace padding as a percentage of the screen width.
58 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
59 * 7% of the screen width can be used as right padding.
60 */
61 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070062
Jon Miranda3f9bab22017-07-28 14:50:17 -070063 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070064
Jon Miranda5eacbb72018-07-31 11:14:46 -070065 // To evenly space the icons, increase the left/right margins for tablets in portrait mode.
66 private static final int PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER = 4;
67
Sunny Goyalc6205602015-05-21 20:46:33 -070068 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -080069 public final int desiredWorkspaceLeftRightMarginPx;
Jon Miranda28032002017-07-13 16:18:56 -070070 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070071 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -070072 public final int edgeMarginPx;
73 public final Rect defaultWidgetPadding;
Sunny Goyal07b69292018-01-08 14:19:34 -080074 public final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070075 private final int topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070076 public float workspaceSpringLoadShrinkFactor;
77 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070078
Sunny Goyal08ef6672018-05-10 11:32:00 -070079 // Drag handle
80 public final int verticalDragHandleSizePx;
Jon Miranda7e183c32018-06-20 11:05:27 -070081 private final int verticalDragHandleOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -070082
Sunny Goyalc6205602015-05-21 20:46:33 -070083 // Workspace icons
84 public int iconSizePx;
85 public int iconTextSizePx;
86 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070087 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070088
Adam Cohen59400422014-03-05 18:07:04 -080089 public int cellWidthPx;
90 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070091 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -080092
Sunny Goyalc6205602015-05-21 20:46:33 -070093 // Folder
Sunny Goyalc6205602015-05-21 20:46:33 -070094 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -070095 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -070096
97 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -070098 public int folderCellWidthPx;
99 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700100
101 // Folder child
102 public int folderChildIconSizePx;
103 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700104 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700105
Miranda Kephart1a359a22019-02-11 13:07:04 -0500106 // Hints
107 public int chipHintHeightPx;
108 public int chipHintBottomMarginPx;
109
Sunny Goyalc6205602015-05-21 20:46:33 -0700110 // Hotseat
Sunny Goyalc6205602015-05-21 20:46:33 -0700111 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700112 // In portrait: size = height, in landscape: size = width
113 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800114 public final int hotseatBarTopPaddingPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700115 public int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700116 // Start is the side next to the nav bar, end is the side next to the workspace
117 public final int hotseatBarSidePaddingStartPx;
118 public final int hotseatBarSidePaddingEndPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700119
Sunny Goyalc6205602015-05-21 20:46:33 -0700120 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700121 public int allAppsCellHeightPx;
Winson1f064272016-07-18 17:18:02 -0700122 public int allAppsIconSizePx;
123 public int allAppsIconDrawablePaddingPx;
124 public float allAppsIconTextSizePx;
125
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800126 // Widgets
127 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
128
Sunny Goyal47328fd2016-05-25 18:56:41 -0700129 // Drop Target
130 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700131
Winson1f064272016-07-18 17:18:02 -0700132 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800133 private final Rect mInsets = new Rect();
134 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700135 private final Rect mHotseatPadding = new Rect();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700136 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700137
Tony Wickhamf34bee82018-12-03 18:11:39 -0800138 // Notification dots
Tony Wickham8912b042018-11-29 15:28:53 -0800139 public DotRenderer mDotRenderer;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800140
Sunny Goyalc6205602015-05-21 20:46:33 -0700141 public DeviceProfile(Context context, InvariantDeviceProfile inv,
142 Point minSize, Point maxSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800143 int width, int height, boolean isLandscape, boolean isMultiWindowMode) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700144
Adam Cohen2e6da152015-05-06 11:42:25 -0700145 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700146 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800147 this.isMultiWindowMode = isMultiWindowMode;
Winson Chungb3800242013-10-24 11:01:54 -0700148
Jon Miranda7e183c32018-06-20 11:05:27 -0700149 // Determine sizes.
150 widthPx = width;
151 heightPx = height;
152 if (isLandscape) {
153 availableWidthPx = maxSize.x;
154 availableHeightPx = minSize.y;
155 } else {
156 availableWidthPx = minSize.x;
157 availableHeightPx = maxSize.y;
158 }
159
Adam Cohen2e6da152015-05-06 11:42:25 -0700160 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700161 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700162
Sunny Goyalc6205602015-05-21 20:46:33 -0700163 // Constants from resources
164 isTablet = res.getBoolean(R.bool.is_tablet);
165 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
166 isPhone = !isTablet && !isLargeTablet;
Jon Miranda7e183c32018-06-20 11:05:27 -0700167 float aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
168 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700169
170 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700171 transposeLayoutWithOrientation =
172 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700173
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700174 context = getContext(context, isVerticalBarLayout()
175 ? Configuration.ORIENTATION_LANDSCAPE
176 : Configuration.ORIENTATION_PORTRAIT);
177 res = context.getResources();
178
179
Winson Chungb3800242013-10-24 11:01:54 -0700180 ComponentName cn = new ComponentName(context.getPackageName(),
181 this.getClass().getName());
182 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
183 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Jon Miranda28032002017-07-13 16:18:56 -0700184 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx;
Jon Miranda5eacbb72018-07-31 11:14:46 -0700185 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
186 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
187 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier *
Jon Miranda30d0aa22017-07-25 15:55:29 -0700188 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Jon Miranda18751b62017-07-31 17:25:27 -0700189 cellLayoutBottomPaddingPx =
190 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_bottom_padding);
Sunny Goyal08ef6672018-05-10 11:32:00 -0700191 verticalDragHandleSizePx = res.getDimensionPixelSize(
192 R.dimen.vertical_drag_handle_size);
Jon Miranda7e183c32018-06-20 11:05:27 -0700193 verticalDragHandleOverlapWorkspace =
194 res.getDimensionPixelSize(R.dimen.vertical_drag_handle_overlap_workspace);
Winson Chungb3800242013-10-24 11:01:54 -0700195 defaultPageSpacingPx =
196 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700197 topWorkspacePadding =
198 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700199 iconDrawablePaddingOriginalPx =
200 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700201 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
202 workspaceSpringLoadedBottomSpace =
203 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700204
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700205 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700206
Miranda Kephart1a359a22019-02-11 13:07:04 -0500207 chipHintHeightPx = res.getDimensionPixelSize(R.dimen.chip_hint_height);
208 chipHintBottomMarginPx = res.getDimensionPixelSize(R.dimen.chip_hint_bottom_margin);
209
Winson1f064272016-07-18 17:18:02 -0700210 hotseatBarTopPaddingPx =
211 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700212 hotseatBarBottomPaddingPx = (isTallDevice ? 0
213 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
214 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700215 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800216 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700217 // Add a bit of space between nav bar and hotseat in multi-window vertical bar layout.
218 hotseatBarSidePaddingStartPx = isMultiWindowMode && isVerticalBarLayout()
219 ? edgeMarginPx : 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800220 hotseatBarSizePx = Utilities.pxFromDp(inv.iconSize, dm) + (isVerticalBarLayout()
221 ? (hotseatBarSidePaddingStartPx + hotseatBarSidePaddingEndPx)
222 : (res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size)
223 + hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700224
Jon Miranda2ed276e2017-06-29 11:36:34 -0700225 // Calculate all of the remaining variables.
Sunny Goyalc6205602015-05-21 20:46:33 -0700226 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700227
228 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700229 if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700230 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700231 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700232 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700233 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda7e183c32018-06-20 11:05:27 -0700234 int extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2
235 - verticalDragHandleSizePx;
236 hotseatBarSizePx += extraSpace;
237 hotseatBarBottomPaddingPx += extraSpace;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700238
Jon Miranda3f9bab22017-07-28 14:50:17 -0700239 // Recalculate the available dimensions using the new hotseat size.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700240 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700241 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800242 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800243
244 // This is done last, after iconSizePx is calculated above.
Tony Wickham8912b042018-11-29 15:28:53 -0800245 mDotRenderer = new DotRenderer(iconSizePx);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700246 }
247
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800248 public DeviceProfile copy(Context context) {
249 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyald70e75a2018-02-22 10:07:32 -0800250 return new DeviceProfile(context, inv, size, size, widthPx, heightPx, isLandscape,
251 isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800252 }
253
Winson Chung2fda6ce2018-01-31 14:09:49 -0800254 public DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700255 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
256 // the system decor is always excluded.
257 mwSize.set(Math.min(availableWidthPx, mwSize.x), Math.min(availableHeightPx, mwSize.y));
258
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700259 // In multi-window mode, we can have widthPx = availableWidthPx
260 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
261 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800262 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800263 isLandscape, true);
Jon Miranda93e1f042016-11-11 14:13:04 -0800264
Jon Miranda0cd04572017-09-19 11:31:42 -0700265 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
266 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
267 - iconDrawablePaddingPx - profile.iconTextSizePx;
268 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
269 profile.adjustToHideWorkspaceLabels();
270 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800271
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800272 // We use these scales to measure and layout the widgets using their full invariant profile
273 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
274 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
275 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
276 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800277 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800278
Jon Miranda93e1f042016-11-11 14:13:04 -0800279 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700280 }
281
Adam Cohen63f1ec02014-08-12 09:23:13 -0700282 /**
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800283 * Inverse of {@link #getMultiWindowProfile(Context, Point)}
284 * @return device profile corresponding to the current orientation in non multi-window mode.
285 */
286 public DeviceProfile getFullScreenProfile() {
287 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
288 }
289
290 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700291 * Adjusts the profile so that the labels on the Workspace are hidden.
292 * It is important to call this method after the All Apps variables have been set.
293 */
294 private void adjustToHideWorkspaceLabels() {
295 iconTextSizePx = 0;
296 iconDrawablePaddingPx = 0;
297 cellHeightPx = iconSizePx;
298
299 // In normal cases, All Apps cell height should equal the Workspace cell height.
300 // Since we are removing labels from the Workspace, we need to manually compute the
301 // All Apps cell height.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700302 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700303 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
304 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700305 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700306 }
307
Sunny Goyalc6205602015-05-21 20:46:33 -0700308 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Miranda09660722017-06-14 14:20:14 -0700309 updateIconSize(1f, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800310
Jon Mirandabf7d8122016-11-03 15:29:29 -0700311 // Check to see if the icons fit within the available height. If not, then scale down.
312 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700313 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700314 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700315 float scale = maxHeight / usedHeight;
Jon Miranda09660722017-06-14 14:20:14 -0700316 updateIconSize(scale, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700317 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700318 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700319 }
320
Jon Miranda09660722017-06-14 14:20:14 -0700321 private void updateIconSize(float scale, Resources res, DisplayMetrics dm) {
Jon Miranda18751b62017-07-31 17:25:27 -0700322 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800323 final boolean isVerticalLayout = isVerticalBarLayout();
324 float invIconSizePx = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal238124e2019-02-20 23:08:36 -0800325 iconSizePx = Math.max(1, (int) (Utilities.pxFromDp(invIconSizePx, dm) * scale));
Sunny Goyala50a4192015-12-11 09:50:49 -0800326 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700327 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700328
Jon Miranda18751b62017-07-31 17:25:27 -0700329 cellHeightPx = iconSizePx + iconDrawablePaddingPx
330 + Utilities.calculateTextHeight(iconTextSizePx);
Jon Miranda846455e2017-10-02 14:58:52 -0700331 int cellYPadding = (getCellSize().y - cellHeightPx) / 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800332 if (iconDrawablePaddingPx > cellYPadding && !isVerticalLayout
Sunny Goyald70e75a2018-02-22 10:07:32 -0800333 && !isMultiWindowMode) {
Jon Miranda846455e2017-10-02 14:58:52 -0700334 // Ensures that the label is closer to its corresponding icon. This is not an issue
335 // with vertical bar layout or multi-window mode since the issue is handled separately
336 // with their calls to {@link #adjustToHideWorkspaceLabels}.
337 cellHeightPx -= (iconDrawablePaddingPx - cellYPadding);
338 iconDrawablePaddingPx = cellYPadding;
339 }
340 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700341
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700342 // All apps
343 allAppsIconTextSizePx = iconTextSizePx;
Winson1f064272016-07-18 17:18:02 -0700344 allAppsIconSizePx = iconSizePx;
345 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700346 allAppsCellHeightPx = getCellSize().y;
347
Sunny Goyal07b69292018-01-08 14:19:34 -0800348 if (isVerticalLayout) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700349 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700350 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700351 }
Winson Chungb3800242013-10-24 11:01:54 -0700352
Winson Chungb3800242013-10-24 11:01:54 -0700353 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800354 if (isVerticalLayout) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700355 hotseatBarSizePx = iconSizePx + hotseatBarSidePaddingStartPx
356 + hotseatBarSidePaddingEndPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700357 }
Jon Miranda5044c352017-08-09 11:37:59 -0700358 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700359
Sunny Goyal07b69292018-01-08 14:19:34 -0800360 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700361 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Sunny Goyal08ef6672018-05-10 11:32:00 -0700362 - verticalDragHandleSizePx - topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700363 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
364 workspaceSpringLoadShrinkFactor = Math.min(
365 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
366 1 - (minRequiredHeight / expectedWorkspaceHeight));
367 } else {
368 workspaceSpringLoadShrinkFactor =
369 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
370 }
371
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700372 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700373 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
374 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700375 }
376
Jon Mirandabf7d8122016-11-03 15:29:29 -0700377 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
378 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
379 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
380 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
381
Jon Mirandac1b23992016-12-13 08:57:36 -0800382 updateFolderCellSize(1f, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700383
Jon Mirandac1b23992016-12-13 08:57:36 -0800384 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700385 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800386 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800387
388 // Check if the icons fit within the available height.
389 float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize;
Sunny Goyal07b69292018-01-08 14:19:34 -0800390 int maxHeight = availableHeightPx - totalWorkspacePadding.y - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800391 float scaleY = maxHeight / usedHeight;
392
393 // Check if the icons fit within the available width.
394 float usedWidth = folderCellWidthPx * inv.numFolderColumns;
Sunny Goyal07b69292018-01-08 14:19:34 -0800395 int maxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800396 float scaleX = maxWidth / usedWidth;
397
398 float scale = Math.min(scaleX, scaleY);
399 if (scale < 1f) {
400 updateFolderCellSize(scale, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700401 }
402 }
403
Jon Mirandac1b23992016-12-13 08:57:36 -0800404 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700405 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
406 folderChildTextSizePx =
407 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
408
409 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
410 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
411 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
412
Jon Mirandac1b23992016-12-13 08:57:36 -0800413 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000414 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
415 folderChildDrawablePaddingPx = Math.max(0,
416 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700417 }
418
Winson1f064272016-07-18 17:18:02 -0700419 public void updateInsets(Rect insets) {
420 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800421 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700422 }
423
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800424 public Rect getInsets() {
425 return mInsets;
426 }
427
Sunny Goyal756cd262015-08-20 12:33:21 -0700428 public Point getCellSize() {
429 Point result = new Point();
430 // Since we are only concerned with the overall padding, layout direction does
431 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700432 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700433 result.x = calculateCellWidth(availableWidthPx - padding.x
434 - cellLayoutPaddingLeftRightPx * 2, inv.numColumns);
435 result.y = calculateCellHeight(availableHeightPx - padding.y
436 - cellLayoutBottomPaddingPx, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700437 return result;
438 }
439
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700440 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800441 updateWorkspacePadding();
442 return new Point(workspacePadding.left + workspacePadding.right,
443 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700444 }
445
446 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800447 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
448 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700449 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800450 private void updateWorkspacePadding() {
451 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700452 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800453 padding.top = 0;
454 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800455 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700456 padding.left = hotseatBarSizePx;
457 padding.right = verticalDragHandleSizePx;
Winson1f064272016-07-18 17:18:02 -0700458 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700459 padding.left = verticalDragHandleSizePx;
460 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700461 }
Winson Chungb3800242013-10-24 11:01:54 -0700462 } else {
Jon Miranda7e183c32018-06-20 11:05:27 -0700463 int paddingBottom = hotseatBarSizePx + verticalDragHandleSizePx
464 - verticalDragHandleOverlapWorkspace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700465 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700466 // Pad the left and right of the workspace to ensure consistent spacing
467 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700468 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800469 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800470 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700471 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800472 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal228153d2018-01-04 15:35:22 -0800473 int availablePaddingY = Math.max(0, heightPx - topWorkspacePadding - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800474 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
475 - hotseatBarBottomPaddingPx);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700476 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700477 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700478 } else {
479 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700480 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700481 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700482 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700483 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700484 }
485 }
Winson Chungb3800242013-10-24 11:01:54 -0700486 }
487
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700488 public Rect getHotseatLayoutPadding() {
489 if (isVerticalBarLayout()) {
490 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700491 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
492 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700493 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700494 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
495 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700496 }
497 } else {
498
499 // We want the edges of the hotseat to line up with the edges of the workspace, but the
500 // icons in the hotseat are a different size, and so don't line up perfectly. To account
501 // for this, we pad the left and right of the hotseat with half of the difference of a
502 // workspace cell vs a hotseat cell.
503 float workspaceCellWidth = (float) widthPx / inv.numColumns;
504 float hotseatCellWidth = (float) widthPx / inv.numHotseatIcons;
505 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
506 mHotseatPadding.set(
507 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx,
508 hotseatBarTopPaddingPx,
509 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx,
510 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
511 }
512 return mHotseatPadding;
513 }
514
Winsonfadbe8f2016-07-22 16:35:37 -0700515 /**
516 * @return the bounds for which the open folders should be contained within
517 */
518 public Rect getAbsoluteOpenFolderBounds() {
519 if (isVerticalBarLayout()) {
520 // Folders should only appear right of the drop target bar and left of the hotseat
521 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
522 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700523 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700524 mInsets.top + availableHeightPx);
525 } else {
526 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800527 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700528 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800529 mInsets.left + availableWidthPx - edgeMarginPx,
Jon Miranda18751b62017-07-31 17:25:27 -0700530 mInsets.top + availableHeightPx - hotseatBarSizePx
Sunny Goyal08ef6672018-05-10 11:32:00 -0700531 - verticalDragHandleSizePx - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700532 }
533 }
534
Adam Cohen2e6da152015-05-06 11:42:25 -0700535 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700536 return width / countX;
537 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700538 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700539 return height / countY;
540 }
541
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700542 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700543 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
544 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
545 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700546 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700547 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700548 return isLandscape && transposeLayoutWithOrientation;
549 }
550
Sunny Goyal59d086c2018-05-07 17:31:40 -0700551 /**
552 * Updates orientation information and returns true if it has changed from the previous value.
553 */
554 public boolean updateIsSeascape(WindowManager wm) {
555 if (isVerticalBarLayout()) {
556 boolean isSeascape = wm.getDefaultDisplay().getRotation() == Surface.ROTATION_270;
557 if (mIsSeascape != isSeascape) {
558 mIsSeascape = isSeascape;
559 return true;
560 }
561 }
562 return false;
563 }
564
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800565 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700566 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800567 }
568
Sunny Goyal07b69292018-01-08 14:19:34 -0800569 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700570 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700571 }
572
Sunny Goyalc13403c2016-11-18 23:44:48 -0800573 public int getCellHeight(@ContainerType int containerType) {
574 switch (containerType) {
575 case CellLayout.WORKSPACE:
576 return cellHeightPx;
577 case CellLayout.FOLDER:
578 return folderCellHeightPx;
579 case CellLayout.HOTSEAT:
580 return hotseatCellHeightPx;
581 default:
582 // ??
583 return 0;
584 }
585 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700586
Jon Miranda0128d3f2019-02-14 09:19:31 -0800587 /**
588 * Gets an item's location on the home screen. This is useful if the home screen
589 * is animating, otherwise use {@link View#getLocationOnScreen(int[])}.
590 *
591 * TODO(b/123900446): Handle landscape mode
592 * @param pageDiff The page difference relative to the current page.
593 */
594 public void getItemLocation(int cellX, int cellY, int spanX, int spanY, int container,
595 int pageDiff, Rect outBounds) {
596 outBounds.setEmpty();
597 outBounds.left = mInsets.left
598 + workspacePadding.left + cellLayoutPaddingLeftRightPx + (cellX * getCellSize().x);
599 outBounds.top = mInsets.top;
600 if (container == CONTAINER_HOTSEAT) {
601 outBounds.top += workspacePadding.top
602 + (inv.numRows * getCellSize().y)
603 + verticalDragHandleSizePx
604 - verticalDragHandleOverlapWorkspace;
605 outBounds.bottom = outBounds.top + hotseatBarSizePx - hotseatBarBottomPaddingPx;
606 } else {
607 outBounds.top += workspacePadding.top + (cellY * getCellSize().y);
608 outBounds.bottom = outBounds.top + (getCellSize().y * spanY);
609 outBounds.left += (pageDiff) * availableWidthPx;
610 }
611 outBounds.right = outBounds.left + (getCellSize().x * spanX);
612 }
613
Jon Miranda1a49fb52019-02-19 15:34:41 -0800614 public float getAspectRatioWithInsets() {
615 int w = widthPx - mInsets.left - mInsets.right;
616 int h = heightPx - mInsets.top - mInsets.bottom;
617 return ((float) Math.max(w, h)) / Math.min(w, h);
618 }
619
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700620 private static Context getContext(Context c, int orientation) {
621 Configuration context = new Configuration(c.getResources().getConfiguration());
622 context.orientation = orientation;
623 return c.createConfigurationContext(context);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700624 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800625
626 /**
627 * Callback when a component changes the DeviceProfile associated with it, as a result of
628 * configuration change
629 */
630 public interface OnDeviceProfileChangeListener {
631
632 /**
633 * Called when the device profile is reassigned. Note that for layout and measurements, it
634 * is sufficient to listen for inset changes. Use this callback when you need to perform
635 * a one time operation.
636 */
637 void onDeviceProfileChanged(DeviceProfile dp);
638 }
Winson Chungb3800242013-10-24 11:01:54 -0700639}