blob: 6397e14ac038c1149db1a853ecf3431dfc4a9048 [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
Winson Chungb3800242013-10-24 11:01:54 -070019import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070020import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070021import android.content.res.Resources;
Winson Chungb3800242013-10-24 11:01:54 -070022import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080023import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070024import android.graphics.Rect;
25import android.util.DisplayMetrics;
Sunny Goyal59d086c2018-05-07 17:31:40 -070026import android.view.Surface;
Jon Miranda0128d3f2019-02-14 09:19:31 -080027import android.view.View;
Sunny Goyal59d086c2018-05-07 17:31:40 -070028import android.view.WindowManager;
Winson Chungb3800242013-10-24 11:01:54 -070029
Sunny Goyalc13403c2016-11-18 23:44:48 -080030import com.android.launcher3.CellLayout.ContainerType;
Tony Wickham8912b042018-11-29 15:28:53 -080031import com.android.launcher3.icons.DotRenderer;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070032import com.android.launcher3.icons.IconNormalizer;
Winson1f064272016-07-18 17:18:02 -070033
Jon Miranda0128d3f2019-02-14 09:19:31 -080034import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT;
35
Winson Chungb3800242013-10-24 11:01:54 -070036public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070037
Adam Cohen2e6da152015-05-06 11:42:25 -070038 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070039
Sunny Goyalc6205602015-05-21 20:46:33 -070040 // Device properties
41 public final boolean isTablet;
42 public final boolean isLargeTablet;
43 public final boolean isPhone;
44 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070045
Sunny Goyalc6205602015-05-21 20:46:33 -070046 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070047 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080048 public final boolean isMultiWindowMode;
49
Sunny Goyalc6205602015-05-21 20:46:33 -070050 public final int widthPx;
51 public final int heightPx;
52 public final int availableWidthPx;
53 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070054 /**
55 * The maximum amount of left/right workspace padding as a percentage of the screen width.
56 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
57 * 7% of the screen width can be used as right padding.
58 */
59 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070060
Jon Miranda3f9bab22017-07-28 14:50:17 -070061 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070062
Jon Miranda5eacbb72018-07-31 11:14:46 -070063 // To evenly space the icons, increase the left/right margins for tablets in portrait mode.
64 private static final int PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER = 4;
65
Sunny Goyalc6205602015-05-21 20:46:33 -070066 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -080067 public final int desiredWorkspaceLeftRightMarginPx;
Jon Miranda28032002017-07-13 16:18:56 -070068 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070069 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -070070 public final int edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070071 public float workspaceSpringLoadShrinkFactor;
72 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070073
Sunny Goyal08ef6672018-05-10 11:32:00 -070074 // Drag handle
75 public final int verticalDragHandleSizePx;
Jon Miranda7e183c32018-06-20 11:05:27 -070076 private final int verticalDragHandleOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -070077
Sunny Goyalc6205602015-05-21 20:46:33 -070078 // Workspace icons
79 public int iconSizePx;
80 public int iconTextSizePx;
81 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070082 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070083
Adam Cohen59400422014-03-05 18:07:04 -080084 public int cellWidthPx;
85 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070086 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -080087
Sunny Goyalc6205602015-05-21 20:46:33 -070088 // Folder
Sunny Goyalc6205602015-05-21 20:46:33 -070089 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -070090 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -070091
92 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -070093 public int folderCellWidthPx;
94 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -070095
96 // Folder child
97 public int folderChildIconSizePx;
98 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070099 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700100
Miranda Kephart1a359a22019-02-11 13:07:04 -0500101 // Hints
102 public int chipHintHeightPx;
103 public int chipHintBottomMarginPx;
104
Sunny Goyalc6205602015-05-21 20:46:33 -0700105 // Hotseat
Sunny Goyalc6205602015-05-21 20:46:33 -0700106 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700107 // In portrait: size = height, in landscape: size = width
108 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800109 public final int hotseatBarTopPaddingPx;
Jon Miranda7e183c32018-06-20 11:05:27 -0700110 public int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700111 // Start is the side next to the nav bar, end is the side next to the workspace
112 public final int hotseatBarSidePaddingStartPx;
113 public final int hotseatBarSidePaddingEndPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700114
Sunny Goyalc6205602015-05-21 20:46:33 -0700115 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700116 public int allAppsCellHeightPx;
Winson1f064272016-07-18 17:18:02 -0700117 public int allAppsIconSizePx;
118 public int allAppsIconDrawablePaddingPx;
119 public float allAppsIconTextSizePx;
120
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800121 // Widgets
122 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
123
Sunny Goyal47328fd2016-05-25 18:56:41 -0700124 // Drop Target
125 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700126
Winson1f064272016-07-18 17:18:02 -0700127 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800128 private final Rect mInsets = new Rect();
129 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700130 private final Rect mHotseatPadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800131 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700132 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700133
Tony Wickhamf34bee82018-12-03 18:11:39 -0800134 // Notification dots
Tony Wickham8912b042018-11-29 15:28:53 -0800135 public DotRenderer mDotRenderer;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800136
Sunny Goyalc6205602015-05-21 20:46:33 -0700137 public DeviceProfile(Context context, InvariantDeviceProfile inv,
138 Point minSize, Point maxSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800139 int width, int height, boolean isLandscape, boolean isMultiWindowMode) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700140
Adam Cohen2e6da152015-05-06 11:42:25 -0700141 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700142 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800143 this.isMultiWindowMode = isMultiWindowMode;
Winson Chungb3800242013-10-24 11:01:54 -0700144
Jon Miranda7e183c32018-06-20 11:05:27 -0700145 // Determine sizes.
146 widthPx = width;
147 heightPx = height;
148 if (isLandscape) {
149 availableWidthPx = maxSize.x;
150 availableHeightPx = minSize.y;
151 } else {
152 availableWidthPx = minSize.x;
153 availableHeightPx = maxSize.y;
154 }
155
Adam Cohen2e6da152015-05-06 11:42:25 -0700156 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700157 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700158
Sunny Goyalc6205602015-05-21 20:46:33 -0700159 // Constants from resources
160 isTablet = res.getBoolean(R.bool.is_tablet);
161 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
162 isPhone = !isTablet && !isLargeTablet;
Jon Miranda7e183c32018-06-20 11:05:27 -0700163 float aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
164 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
Sunny Goyalc6205602015-05-21 20:46:33 -0700165
166 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700167 transposeLayoutWithOrientation =
168 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700169
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700170 context = getContext(context, isVerticalBarLayout()
171 ? Configuration.ORIENTATION_LANDSCAPE
172 : Configuration.ORIENTATION_PORTRAIT);
173 res = context.getResources();
174
Winson Chungb3800242013-10-24 11:01:54 -0700175 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Jon Miranda28032002017-07-13 16:18:56 -0700176 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700177
Jon Miranda5eacbb72018-07-31 11:14:46 -0700178 int cellLayoutPaddingLeftRightMultiplier = !isVerticalBarLayout() && isTablet
179 ? PORTRAIT_TABLET_LEFT_RIGHT_PADDING_MULTIPLIER : 1;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700180 int cellLayoutPadding = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
181 if (isLandscape) {
182 cellLayoutPaddingLeftRightPx = 0;
183 cellLayoutBottomPaddingPx = cellLayoutPadding;
184 } else {
185 cellLayoutPaddingLeftRightPx = cellLayoutPaddingLeftRightMultiplier * cellLayoutPadding;
186 cellLayoutBottomPaddingPx = 0;
187 }
188
Sunny Goyal08ef6672018-05-10 11:32:00 -0700189 verticalDragHandleSizePx = res.getDimensionPixelSize(
190 R.dimen.vertical_drag_handle_size);
Jon Miranda7e183c32018-06-20 11:05:27 -0700191 verticalDragHandleOverlapWorkspace =
192 res.getDimensionPixelSize(R.dimen.vertical_drag_handle_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700193
Winson Chungb3800242013-10-24 11:01:54 -0700194 iconDrawablePaddingOriginalPx =
195 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700196 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
197 workspaceSpringLoadedBottomSpace =
198 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700199
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700200 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700201
Miranda Kephart1a359a22019-02-11 13:07:04 -0500202 chipHintHeightPx = res.getDimensionPixelSize(R.dimen.chip_hint_height);
203 chipHintBottomMarginPx = res.getDimensionPixelSize(R.dimen.chip_hint_bottom_margin);
204
Winson1f064272016-07-18 17:18:02 -0700205 hotseatBarTopPaddingPx =
206 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Miranda7e183c32018-06-20 11:05:27 -0700207 hotseatBarBottomPaddingPx = (isTallDevice ? 0
208 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_non_tall_padding))
209 + res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700210 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800211 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700212 // Add a bit of space between nav bar and hotseat in multi-window vertical bar layout.
213 hotseatBarSidePaddingStartPx = isMultiWindowMode && isVerticalBarLayout()
214 ? edgeMarginPx : 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800215 hotseatBarSizePx = Utilities.pxFromDp(inv.iconSize, dm) + (isVerticalBarLayout()
216 ? (hotseatBarSidePaddingStartPx + hotseatBarSidePaddingEndPx)
217 : (res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_extra_vertical_size)
218 + hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx));
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700219
Jon Miranda2ed276e2017-06-29 11:36:34 -0700220 // Calculate all of the remaining variables.
Sunny Goyalc6205602015-05-21 20:46:33 -0700221 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700222
223 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700224 if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700225 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700226 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700227 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700228 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda7e183c32018-06-20 11:05:27 -0700229 int extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx * 2
230 - verticalDragHandleSizePx;
231 hotseatBarSizePx += extraSpace;
232 hotseatBarBottomPaddingPx += extraSpace;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700233
Jon Miranda3f9bab22017-07-28 14:50:17 -0700234 // Recalculate the available dimensions using the new hotseat size.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700235 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700236 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800237 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800238
239 // This is done last, after iconSizePx is calculated above.
Tony Wickham8912b042018-11-29 15:28:53 -0800240 mDotRenderer = new DotRenderer(iconSizePx);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700241 }
242
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800243 public DeviceProfile copy(Context context) {
244 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyald70e75a2018-02-22 10:07:32 -0800245 return new DeviceProfile(context, inv, size, size, widthPx, heightPx, isLandscape,
246 isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800247 }
248
Winson Chung2fda6ce2018-01-31 14:09:49 -0800249 public DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700250 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
251 // the system decor is always excluded.
252 mwSize.set(Math.min(availableWidthPx, mwSize.x), Math.min(availableHeightPx, mwSize.y));
253
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700254 // In multi-window mode, we can have widthPx = availableWidthPx
255 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
256 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800257 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800258 isLandscape, true);
Jon Miranda93e1f042016-11-11 14:13:04 -0800259
Jon Miranda0cd04572017-09-19 11:31:42 -0700260 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
261 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
262 - iconDrawablePaddingPx - profile.iconTextSizePx;
263 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
264 profile.adjustToHideWorkspaceLabels();
265 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800266
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800267 // We use these scales to measure and layout the widgets using their full invariant profile
268 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
269 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
270 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
271 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800272 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800273
Jon Miranda93e1f042016-11-11 14:13:04 -0800274 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700275 }
276
Adam Cohen63f1ec02014-08-12 09:23:13 -0700277 /**
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800278 * Inverse of {@link #getMultiWindowProfile(Context, Point)}
279 * @return device profile corresponding to the current orientation in non multi-window mode.
280 */
281 public DeviceProfile getFullScreenProfile() {
282 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
283 }
284
285 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700286 * Adjusts the profile so that the labels on the Workspace are hidden.
287 * It is important to call this method after the All Apps variables have been set.
288 */
289 private void adjustToHideWorkspaceLabels() {
290 iconTextSizePx = 0;
291 iconDrawablePaddingPx = 0;
292 cellHeightPx = iconSizePx;
293
294 // In normal cases, All Apps cell height should equal the Workspace cell height.
295 // Since we are removing labels from the Workspace, we need to manually compute the
296 // All Apps cell height.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700297 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700298 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
299 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700300 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700301 }
302
Sunny Goyalc6205602015-05-21 20:46:33 -0700303 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Miranda09660722017-06-14 14:20:14 -0700304 updateIconSize(1f, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800305
Jon Mirandabf7d8122016-11-03 15:29:29 -0700306 // Check to see if the icons fit within the available height. If not, then scale down.
307 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700308 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700309 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700310 float scale = maxHeight / usedHeight;
Jon Miranda09660722017-06-14 14:20:14 -0700311 updateIconSize(scale, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700312 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700313 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700314 }
315
Jon Miranda09660722017-06-14 14:20:14 -0700316 private void updateIconSize(float scale, Resources res, DisplayMetrics dm) {
Jon Miranda18751b62017-07-31 17:25:27 -0700317 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800318 final boolean isVerticalLayout = isVerticalBarLayout();
319 float invIconSizePx = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Sunny Goyal238124e2019-02-20 23:08:36 -0800320 iconSizePx = Math.max(1, (int) (Utilities.pxFromDp(invIconSizePx, dm) * scale));
Sunny Goyala50a4192015-12-11 09:50:49 -0800321 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700322 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700323
Jon Miranda18751b62017-07-31 17:25:27 -0700324 cellHeightPx = iconSizePx + iconDrawablePaddingPx
325 + Utilities.calculateTextHeight(iconTextSizePx);
Jon Miranda846455e2017-10-02 14:58:52 -0700326 int cellYPadding = (getCellSize().y - cellHeightPx) / 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800327 if (iconDrawablePaddingPx > cellYPadding && !isVerticalLayout
Sunny Goyald70e75a2018-02-22 10:07:32 -0800328 && !isMultiWindowMode) {
Jon Miranda846455e2017-10-02 14:58:52 -0700329 // Ensures that the label is closer to its corresponding icon. This is not an issue
330 // with vertical bar layout or multi-window mode since the issue is handled separately
331 // with their calls to {@link #adjustToHideWorkspaceLabels}.
332 cellHeightPx -= (iconDrawablePaddingPx - cellYPadding);
333 iconDrawablePaddingPx = cellYPadding;
334 }
335 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700336
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700337 // All apps
338 allAppsIconTextSizePx = iconTextSizePx;
Winson1f064272016-07-18 17:18:02 -0700339 allAppsIconSizePx = iconSizePx;
340 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700341 allAppsCellHeightPx = getCellSize().y;
342
Sunny Goyal07b69292018-01-08 14:19:34 -0800343 if (isVerticalLayout) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700344 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700345 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700346 }
Winson Chungb3800242013-10-24 11:01:54 -0700347
Winson Chungb3800242013-10-24 11:01:54 -0700348 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800349 if (isVerticalLayout) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700350 hotseatBarSizePx = iconSizePx + hotseatBarSidePaddingStartPx
351 + hotseatBarSidePaddingEndPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700352 }
Jon Miranda5044c352017-08-09 11:37:59 -0700353 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700354
Sunny Goyal07b69292018-01-08 14:19:34 -0800355 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700356 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700357 - verticalDragHandleSizePx - edgeMarginPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700358 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
359 workspaceSpringLoadShrinkFactor = Math.min(
360 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
361 1 - (minRequiredHeight / expectedWorkspaceHeight));
362 } else {
363 workspaceSpringLoadShrinkFactor =
364 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
365 }
366
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700367 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700368 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
369 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700370 }
371
Jon Mirandabf7d8122016-11-03 15:29:29 -0700372 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
373 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
374 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
375 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
376
Jon Mirandac1b23992016-12-13 08:57:36 -0800377 updateFolderCellSize(1f, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700378
Jon Mirandac1b23992016-12-13 08:57:36 -0800379 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700380 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800381 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800382
383 // Check if the icons fit within the available height.
384 float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize;
Sunny Goyal07b69292018-01-08 14:19:34 -0800385 int maxHeight = availableHeightPx - totalWorkspacePadding.y - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800386 float scaleY = maxHeight / usedHeight;
387
388 // Check if the icons fit within the available width.
389 float usedWidth = folderCellWidthPx * inv.numFolderColumns;
Sunny Goyal07b69292018-01-08 14:19:34 -0800390 int maxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800391 float scaleX = maxWidth / usedWidth;
392
393 float scale = Math.min(scaleX, scaleY);
394 if (scale < 1f) {
395 updateFolderCellSize(scale, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700396 }
397 }
398
Jon Mirandac1b23992016-12-13 08:57:36 -0800399 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700400 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
401 folderChildTextSizePx =
402 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
403
404 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
405 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
406 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
407
Jon Mirandac1b23992016-12-13 08:57:36 -0800408 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000409 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
410 folderChildDrawablePaddingPx = Math.max(0,
411 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700412 }
413
Winson1f064272016-07-18 17:18:02 -0700414 public void updateInsets(Rect insets) {
415 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800416 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700417 }
418
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800419 public Rect getInsets() {
420 return mInsets;
421 }
422
Sunny Goyal756cd262015-08-20 12:33:21 -0700423 public Point getCellSize() {
424 Point result = new Point();
425 // Since we are only concerned with the overall padding, layout direction does
426 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700427 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700428 result.x = calculateCellWidth(availableWidthPx - padding.x
429 - cellLayoutPaddingLeftRightPx * 2, inv.numColumns);
430 result.y = calculateCellHeight(availableHeightPx - padding.y
431 - cellLayoutBottomPaddingPx, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700432 return result;
433 }
434
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700435 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800436 updateWorkspacePadding();
437 return new Point(workspacePadding.left + workspacePadding.right,
438 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700439 }
440
441 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800442 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
443 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700444 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800445 private void updateWorkspacePadding() {
446 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700447 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800448 padding.top = 0;
449 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800450 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700451 padding.left = hotseatBarSizePx;
452 padding.right = verticalDragHandleSizePx;
Winson1f064272016-07-18 17:18:02 -0700453 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700454 padding.left = verticalDragHandleSizePx;
455 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700456 }
Winson Chungb3800242013-10-24 11:01:54 -0700457 } else {
Jon Miranda7e183c32018-06-20 11:05:27 -0700458 int paddingBottom = hotseatBarSizePx + verticalDragHandleSizePx
459 - verticalDragHandleOverlapWorkspace;
Sunny Goyalc6205602015-05-21 20:46:33 -0700460 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700461 // Pad the left and right of the workspace to ensure consistent spacing
462 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700463 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800464 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800465 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700466 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800467 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700468 int availablePaddingY = Math.max(0, heightPx - edgeMarginPx - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800469 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
470 - hotseatBarBottomPaddingPx);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700471 padding.set(availablePaddingX / 2, edgeMarginPx + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700472 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700473 } else {
474 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700475 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700476 edgeMarginPx,
Winson1f064272016-07-18 17:18:02 -0700477 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700478 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700479 }
480 }
Winson Chungb3800242013-10-24 11:01:54 -0700481 }
482
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700483 public Rect getHotseatLayoutPadding() {
484 if (isVerticalBarLayout()) {
485 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700486 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
487 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700488 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700489 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
490 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700491 }
492 } else {
493
494 // We want the edges of the hotseat to line up with the edges of the workspace, but the
495 // icons in the hotseat are a different size, and so don't line up perfectly. To account
496 // for this, we pad the left and right of the hotseat with half of the difference of a
497 // workspace cell vs a hotseat cell.
498 float workspaceCellWidth = (float) widthPx / inv.numColumns;
499 float hotseatCellWidth = (float) widthPx / inv.numHotseatIcons;
500 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
501 mHotseatPadding.set(
502 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx,
503 hotseatBarTopPaddingPx,
504 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx,
505 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
506 }
507 return mHotseatPadding;
508 }
509
Winsonfadbe8f2016-07-22 16:35:37 -0700510 /**
511 * @return the bounds for which the open folders should be contained within
512 */
513 public Rect getAbsoluteOpenFolderBounds() {
514 if (isVerticalBarLayout()) {
515 // Folders should only appear right of the drop target bar and left of the hotseat
516 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
517 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700518 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700519 mInsets.top + availableHeightPx);
520 } else {
521 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800522 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700523 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800524 mInsets.left + availableWidthPx - edgeMarginPx,
Jon Miranda18751b62017-07-31 17:25:27 -0700525 mInsets.top + availableHeightPx - hotseatBarSizePx
Sunny Goyal08ef6672018-05-10 11:32:00 -0700526 - verticalDragHandleSizePx - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700527 }
528 }
529
Adam Cohen2e6da152015-05-06 11:42:25 -0700530 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700531 return width / countX;
532 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700533 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700534 return height / countY;
535 }
536
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700537 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700538 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
539 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
540 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700541 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700542 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700543 return isLandscape && transposeLayoutWithOrientation;
544 }
545
Sunny Goyal59d086c2018-05-07 17:31:40 -0700546 /**
547 * Updates orientation information and returns true if it has changed from the previous value.
548 */
549 public boolean updateIsSeascape(WindowManager wm) {
550 if (isVerticalBarLayout()) {
551 boolean isSeascape = wm.getDefaultDisplay().getRotation() == Surface.ROTATION_270;
552 if (mIsSeascape != isSeascape) {
553 mIsSeascape = isSeascape;
554 return true;
555 }
556 }
557 return false;
558 }
559
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800560 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700561 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800562 }
563
Sunny Goyal07b69292018-01-08 14:19:34 -0800564 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700565 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700566 }
567
Sunny Goyalc13403c2016-11-18 23:44:48 -0800568 public int getCellHeight(@ContainerType int containerType) {
569 switch (containerType) {
570 case CellLayout.WORKSPACE:
571 return cellHeightPx;
572 case CellLayout.FOLDER:
573 return folderCellHeightPx;
574 case CellLayout.HOTSEAT:
575 return hotseatCellHeightPx;
576 default:
577 // ??
578 return 0;
579 }
580 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700581
Jon Miranda0128d3f2019-02-14 09:19:31 -0800582 /**
583 * Gets an item's location on the home screen. This is useful if the home screen
584 * is animating, otherwise use {@link View#getLocationOnScreen(int[])}.
Jon Miranda0128d3f2019-02-14 09:19:31 -0800585 * @param pageDiff The page difference relative to the current page.
586 */
587 public void getItemLocation(int cellX, int cellY, int spanX, int spanY, int container,
588 int pageDiff, Rect outBounds) {
589 outBounds.setEmpty();
Jon Miranda0128d3f2019-02-14 09:19:31 -0800590 if (container == CONTAINER_HOTSEAT) {
Jon Miranda0bd63d12019-03-06 17:29:29 -0800591 final int actualHotseatCellHeight;
592 if (isVerticalBarLayout()) {
593 actualHotseatCellHeight = availableHeightPx / inv.numRows;
594 if (mIsSeascape) {
595 outBounds.left = mHotseatPadding.left;
596 } else {
597 outBounds.left = availableWidthPx - hotseatBarSizePx + mHotseatPadding.left;
598 }
599 outBounds.right = outBounds.left + iconSizePx;
600 outBounds.top = mHotseatPadding.top
601 + actualHotseatCellHeight * (inv.numRows - cellX - 1);
602 outBounds.bottom = outBounds.top + actualHotseatCellHeight;
603 } else {
604 actualHotseatCellHeight = hotseatBarSizePx - hotseatBarBottomPaddingPx
605 - hotseatBarTopPaddingPx;
606 outBounds.left = mInsets.left + workspacePadding.left + cellLayoutPaddingLeftRightPx
607 + (cellX * getCellSize().x);
608 outBounds.right = outBounds.left + getCellSize().x;
609 outBounds.top = mInsets.top + availableHeightPx - hotseatBarSizePx;
610 outBounds.bottom = outBounds.top + actualHotseatCellHeight;
611 }
Jon Miranda0128d3f2019-02-14 09:19:31 -0800612 } else {
Jon Miranda0bd63d12019-03-06 17:29:29 -0800613 outBounds.left = mInsets.left + workspacePadding.left + cellLayoutPaddingLeftRightPx
614 + (cellX * getCellSize().x) + (pageDiff * availableWidthPx);
615 outBounds.right = outBounds.left + (getCellSize().x * spanX);
616 outBounds.top = mInsets.top + workspacePadding.top + (cellY * getCellSize().y);
Jon Miranda0128d3f2019-02-14 09:19:31 -0800617 outBounds.bottom = outBounds.top + (getCellSize().y * spanY);
Jon Miranda0128d3f2019-02-14 09:19:31 -0800618 }
Jon Miranda0128d3f2019-02-14 09:19:31 -0800619 }
620
Jon Miranda1a49fb52019-02-19 15:34:41 -0800621 public float getAspectRatioWithInsets() {
622 int w = widthPx - mInsets.left - mInsets.right;
623 int h = heightPx - mInsets.top - mInsets.bottom;
624 return ((float) Math.max(w, h)) / Math.min(w, h);
625 }
626
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700627 private static Context getContext(Context c, int orientation) {
628 Configuration context = new Configuration(c.getResources().getConfiguration());
629 context.orientation = orientation;
630 return c.createConfigurationContext(context);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700631 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800632
633 /**
634 * Callback when a component changes the DeviceProfile associated with it, as a result of
635 * configuration change
636 */
637 public interface OnDeviceProfileChangeListener {
638
639 /**
640 * Called when the device profile is reassigned. Note that for layout and measurements, it
641 * is sufficient to listen for inset changes. Use this callback when you need to perform
642 * a one time operation.
643 */
644 void onDeviceProfileChanged(DeviceProfile dp);
645 }
Winson Chungb3800242013-10-24 11:01:54 -0700646}