blob: 20c4a5fdf978589f8e8a443fc401d001ea198f48 [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;
29import android.view.WindowManager;
Winson Chungb3800242013-10-24 11:01:54 -070030
Sunny Goyalc13403c2016-11-18 23:44:48 -080031import com.android.launcher3.CellLayout.ContainerType;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080032import com.android.launcher3.badge.BadgeRenderer;
Jon Miranda591e3602018-03-28 11:37:00 -070033import com.android.launcher3.graphics.IconNormalizer;
Winson1f064272016-07-18 17:18:02 -070034
Winson Chungb3800242013-10-24 11:01:54 -070035public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070036
Adam Cohen2e6da152015-05-06 11:42:25 -070037 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070038
Sunny Goyalc6205602015-05-21 20:46:33 -070039 // Device properties
40 public final boolean isTablet;
41 public final boolean isLargeTablet;
42 public final boolean isPhone;
43 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070044
Sunny Goyalc6205602015-05-21 20:46:33 -070045 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070046 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080047 public final boolean isMultiWindowMode;
48
Sunny Goyalc6205602015-05-21 20:46:33 -070049 public final int widthPx;
50 public final int heightPx;
51 public final int availableWidthPx;
52 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070053 /**
54 * The maximum amount of left/right workspace padding as a percentage of the screen width.
55 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
56 * 7% of the screen width can be used as right padding.
57 */
58 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070059
Jon Miranda3f9bab22017-07-28 14:50:17 -070060 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070061
Sunny Goyalc6205602015-05-21 20:46:33 -070062 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -080063 public final int desiredWorkspaceLeftRightMarginPx;
Jon Miranda28032002017-07-13 16:18:56 -070064 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070065 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -070066 public final int edgeMarginPx;
67 public final Rect defaultWidgetPadding;
Sunny Goyal07b69292018-01-08 14:19:34 -080068 public final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070069 private final int topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070070 public float workspaceSpringLoadShrinkFactor;
71 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070072
Sunny Goyal08ef6672018-05-10 11:32:00 -070073 // Drag handle
74 public final int verticalDragHandleSizePx;
Winson1f064272016-07-18 17:18:02 -070075
Sunny Goyalc6205602015-05-21 20:46:33 -070076 // Workspace icons
77 public int iconSizePx;
78 public int iconTextSizePx;
79 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070080 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070081
Adam Cohen59400422014-03-05 18:07:04 -080082 public int cellWidthPx;
83 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070084 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -080085
Sunny Goyalc6205602015-05-21 20:46:33 -070086 // Folder
Sunny Goyalc6205602015-05-21 20:46:33 -070087 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -070088 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -070089
90 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -070091 public int folderCellWidthPx;
92 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -070093
94 // Folder child
95 public int folderChildIconSizePx;
96 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070097 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -070098
Sunny Goyalc6205602015-05-21 20:46:33 -070099 // Hotseat
Sunny Goyalc6205602015-05-21 20:46:33 -0700100 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700101 // In portrait: size = height, in landscape: size = width
102 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800103 public final int hotseatBarTopPaddingPx;
104 public final int hotseatBarBottomPaddingPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700105 // Start is the side next to the nav bar, end is the side next to the workspace
106 public final int hotseatBarSidePaddingStartPx;
107 public final int hotseatBarSidePaddingEndPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700108
Sunny Goyalc6205602015-05-21 20:46:33 -0700109 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700110 public int allAppsCellHeightPx;
Winson1f064272016-07-18 17:18:02 -0700111 public int allAppsIconSizePx;
112 public int allAppsIconDrawablePaddingPx;
113 public float allAppsIconTextSizePx;
114
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800115 // Widgets
116 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
117
Sunny Goyal47328fd2016-05-25 18:56:41 -0700118 // Drop Target
119 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700120
Winson1f064272016-07-18 17:18:02 -0700121 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800122 private final Rect mInsets = new Rect();
123 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700124 private final Rect mHotseatPadding = new Rect();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700125 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700126
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800127 // Icon badges
128 public BadgeRenderer mBadgeRenderer;
129
Sunny Goyalc6205602015-05-21 20:46:33 -0700130 public DeviceProfile(Context context, InvariantDeviceProfile inv,
131 Point minSize, Point maxSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800132 int width, int height, boolean isLandscape, boolean isMultiWindowMode) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700133
Adam Cohen2e6da152015-05-06 11:42:25 -0700134 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700135 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800136 this.isMultiWindowMode = isMultiWindowMode;
Winson Chungb3800242013-10-24 11:01:54 -0700137
Adam Cohen2e6da152015-05-06 11:42:25 -0700138 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700139 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700140
Sunny Goyalc6205602015-05-21 20:46:33 -0700141 // Constants from resources
142 isTablet = res.getBoolean(R.bool.is_tablet);
143 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
144 isPhone = !isTablet && !isLargeTablet;
145
146 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700147 transposeLayoutWithOrientation =
148 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700149
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700150 context = getContext(context, isVerticalBarLayout()
151 ? Configuration.ORIENTATION_LANDSCAPE
152 : Configuration.ORIENTATION_PORTRAIT);
153 res = context.getResources();
154
155
Winson Chungb3800242013-10-24 11:01:54 -0700156 ComponentName cn = new ComponentName(context.getPackageName(),
157 this.getClass().getName());
158 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
159 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Jon Miranda28032002017-07-13 16:18:56 -0700160 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700161 cellLayoutPaddingLeftRightPx =
162 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Jon Miranda18751b62017-07-31 17:25:27 -0700163 cellLayoutBottomPaddingPx =
164 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_bottom_padding);
Sunny Goyal08ef6672018-05-10 11:32:00 -0700165 verticalDragHandleSizePx = res.getDimensionPixelSize(
166 R.dimen.vertical_drag_handle_size);
Winson Chungb3800242013-10-24 11:01:54 -0700167 defaultPageSpacingPx =
168 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700169 topWorkspacePadding =
170 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700171 iconDrawablePaddingOriginalPx =
172 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700173 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
174 workspaceSpringLoadedBottomSpace =
175 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700176
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700177 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700178
Winson1f064272016-07-18 17:18:02 -0700179 hotseatBarTopPaddingPx =
180 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Sunny Goyald5190522017-04-24 03:06:54 -0700181 hotseatBarBottomPaddingPx =
182 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700183 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800184 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700185 // Add a bit of space between nav bar and hotseat in multi-window vertical bar layout.
186 hotseatBarSidePaddingStartPx = isMultiWindowMode && isVerticalBarLayout()
187 ? edgeMarginPx : 0;
Jon Miranda18751b62017-07-31 17:25:27 -0700188 hotseatBarSizePx = isVerticalBarLayout()
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700189 ? Utilities.pxFromDp(inv.iconSize, dm) + hotseatBarSidePaddingStartPx
190 + hotseatBarSidePaddingEndPx
Jon Miranda18751b62017-07-31 17:25:27 -0700191 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_size)
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700192 + hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx;
193
Sunny Goyalc6205602015-05-21 20:46:33 -0700194 // Determine sizes.
195 widthPx = width;
196 heightPx = height;
197 if (isLandscape) {
198 availableWidthPx = maxSize.x;
199 availableHeightPx = minSize.y;
200 } else {
201 availableWidthPx = minSize.x;
202 availableHeightPx = maxSize.y;
203 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700204
Jon Miranda2ed276e2017-06-29 11:36:34 -0700205 // Calculate all of the remaining variables.
Sunny Goyalc6205602015-05-21 20:46:33 -0700206 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700207
208 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700209 float aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda30d0aa22017-07-25 15:55:29 -0700210 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
211 if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700212 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700213 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700214 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700215 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700216 int extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx;
Sunny Goyal08ef6672018-05-10 11:32:00 -0700217 hotseatBarSizePx += extraSpace - verticalDragHandleSizePx;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700218
Jon Miranda3f9bab22017-07-28 14:50:17 -0700219 // Recalculate the available dimensions using the new hotseat size.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700220 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700221 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800222 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800223
224 // This is done last, after iconSizePx is calculated above.
Sunny Goyal179249d2017-12-19 16:49:24 -0800225 mBadgeRenderer = new BadgeRenderer(iconSizePx);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700226 }
227
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800228 public DeviceProfile copy(Context context) {
229 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyald70e75a2018-02-22 10:07:32 -0800230 return new DeviceProfile(context, inv, size, size, widthPx, heightPx, isLandscape,
231 isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800232 }
233
Winson Chung2fda6ce2018-01-31 14:09:49 -0800234 public DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700235 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
236 // the system decor is always excluded.
237 mwSize.set(Math.min(availableWidthPx, mwSize.x), Math.min(availableHeightPx, mwSize.y));
238
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700239 // In multi-window mode, we can have widthPx = availableWidthPx
240 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
241 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800242 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800243 isLandscape, true);
Jon Miranda93e1f042016-11-11 14:13:04 -0800244
Jon Miranda0cd04572017-09-19 11:31:42 -0700245 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
246 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
247 - iconDrawablePaddingPx - profile.iconTextSizePx;
248 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
249 profile.adjustToHideWorkspaceLabels();
250 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800251
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800252 // We use these scales to measure and layout the widgets using their full invariant profile
253 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
254 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
255 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
256 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800257 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800258
Jon Miranda93e1f042016-11-11 14:13:04 -0800259 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700260 }
261
Adam Cohen63f1ec02014-08-12 09:23:13 -0700262 /**
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800263 * Inverse of {@link #getMultiWindowProfile(Context, Point)}
264 * @return device profile corresponding to the current orientation in non multi-window mode.
265 */
266 public DeviceProfile getFullScreenProfile() {
267 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
268 }
269
270 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700271 * Adjusts the profile so that the labels on the Workspace are hidden.
272 * It is important to call this method after the All Apps variables have been set.
273 */
274 private void adjustToHideWorkspaceLabels() {
275 iconTextSizePx = 0;
276 iconDrawablePaddingPx = 0;
277 cellHeightPx = iconSizePx;
278
279 // In normal cases, All Apps cell height should equal the Workspace cell height.
280 // Since we are removing labels from the Workspace, we need to manually compute the
281 // All Apps cell height.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700282 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700283 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
284 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700285 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700286 }
287
Sunny Goyalc6205602015-05-21 20:46:33 -0700288 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Miranda09660722017-06-14 14:20:14 -0700289 updateIconSize(1f, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800290
Jon Mirandabf7d8122016-11-03 15:29:29 -0700291 // Check to see if the icons fit within the available height. If not, then scale down.
292 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700293 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700294 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700295 float scale = maxHeight / usedHeight;
Jon Miranda09660722017-06-14 14:20:14 -0700296 updateIconSize(scale, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700297 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700298 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700299 }
300
Jon Miranda09660722017-06-14 14:20:14 -0700301 private void updateIconSize(float scale, Resources res, DisplayMetrics dm) {
Jon Miranda18751b62017-07-31 17:25:27 -0700302 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800303 final boolean isVerticalLayout = isVerticalBarLayout();
304 float invIconSizePx = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700305 iconSizePx = (int) (Utilities.pxFromDp(invIconSizePx, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800306 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700307 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700308
Jon Miranda18751b62017-07-31 17:25:27 -0700309 cellHeightPx = iconSizePx + iconDrawablePaddingPx
310 + Utilities.calculateTextHeight(iconTextSizePx);
Jon Miranda846455e2017-10-02 14:58:52 -0700311 int cellYPadding = (getCellSize().y - cellHeightPx) / 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800312 if (iconDrawablePaddingPx > cellYPadding && !isVerticalLayout
Sunny Goyald70e75a2018-02-22 10:07:32 -0800313 && !isMultiWindowMode) {
Jon Miranda846455e2017-10-02 14:58:52 -0700314 // Ensures that the label is closer to its corresponding icon. This is not an issue
315 // with vertical bar layout or multi-window mode since the issue is handled separately
316 // with their calls to {@link #adjustToHideWorkspaceLabels}.
317 cellHeightPx -= (iconDrawablePaddingPx - cellYPadding);
318 iconDrawablePaddingPx = cellYPadding;
319 }
320 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700321
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700322 // All apps
323 allAppsIconTextSizePx = iconTextSizePx;
Winson1f064272016-07-18 17:18:02 -0700324 allAppsIconSizePx = iconSizePx;
325 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700326 allAppsCellHeightPx = getCellSize().y;
327
Sunny Goyal07b69292018-01-08 14:19:34 -0800328 if (isVerticalLayout) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700329 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700330 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700331 }
Winson Chungb3800242013-10-24 11:01:54 -0700332
Winson Chungb3800242013-10-24 11:01:54 -0700333 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800334 if (isVerticalLayout) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700335 hotseatBarSizePx = iconSizePx + hotseatBarSidePaddingStartPx
336 + hotseatBarSidePaddingEndPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700337 }
Jon Miranda5044c352017-08-09 11:37:59 -0700338 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700339
Sunny Goyal07b69292018-01-08 14:19:34 -0800340 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700341 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Sunny Goyal08ef6672018-05-10 11:32:00 -0700342 - verticalDragHandleSizePx - topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700343 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
344 workspaceSpringLoadShrinkFactor = Math.min(
345 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
346 1 - (minRequiredHeight / expectedWorkspaceHeight));
347 } else {
348 workspaceSpringLoadShrinkFactor =
349 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
350 }
351
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700352 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700353 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
354 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700355 }
356
Jon Mirandabf7d8122016-11-03 15:29:29 -0700357 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
358 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
359 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
360 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
361
Jon Mirandac1b23992016-12-13 08:57:36 -0800362 updateFolderCellSize(1f, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700363
Jon Mirandac1b23992016-12-13 08:57:36 -0800364 // Don't let the folder get too close to the edges of the screen.
Jon Miranda72b5fd12017-06-25 18:06:23 -0700365 int folderMargin = edgeMarginPx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800366 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800367
368 // Check if the icons fit within the available height.
369 float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize;
Sunny Goyal07b69292018-01-08 14:19:34 -0800370 int maxHeight = availableHeightPx - totalWorkspacePadding.y - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800371 float scaleY = maxHeight / usedHeight;
372
373 // Check if the icons fit within the available width.
374 float usedWidth = folderCellWidthPx * inv.numFolderColumns;
Sunny Goyal07b69292018-01-08 14:19:34 -0800375 int maxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800376 float scaleX = maxWidth / usedWidth;
377
378 float scale = Math.min(scaleX, scaleY);
379 if (scale < 1f) {
380 updateFolderCellSize(scale, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700381 }
382 }
383
Jon Mirandac1b23992016-12-13 08:57:36 -0800384 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700385 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
386 folderChildTextSizePx =
387 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
388
389 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
390 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
391 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
392
Jon Mirandac1b23992016-12-13 08:57:36 -0800393 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000394 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
395 folderChildDrawablePaddingPx = Math.max(0,
396 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700397 }
398
Winson1f064272016-07-18 17:18:02 -0700399 public void updateInsets(Rect insets) {
400 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800401 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700402 }
403
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800404 public Rect getInsets() {
405 return mInsets;
406 }
407
Sunny Goyal756cd262015-08-20 12:33:21 -0700408 public Point getCellSize() {
409 Point result = new Point();
410 // Since we are only concerned with the overall padding, layout direction does
411 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700412 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700413 result.x = calculateCellWidth(availableWidthPx - padding.x
414 - cellLayoutPaddingLeftRightPx * 2, inv.numColumns);
415 result.y = calculateCellHeight(availableHeightPx - padding.y
416 - cellLayoutBottomPaddingPx, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700417 return result;
418 }
419
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700420 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800421 updateWorkspacePadding();
422 return new Point(workspacePadding.left + workspacePadding.right,
423 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700424 }
425
426 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800427 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
428 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700429 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800430 private void updateWorkspacePadding() {
431 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700432 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800433 padding.top = 0;
434 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800435 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700436 padding.left = hotseatBarSizePx;
437 padding.right = verticalDragHandleSizePx;
Winson1f064272016-07-18 17:18:02 -0700438 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700439 padding.left = verticalDragHandleSizePx;
440 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700441 }
Winson Chungb3800242013-10-24 11:01:54 -0700442 } else {
Sunny Goyal08ef6672018-05-10 11:32:00 -0700443 int paddingBottom = hotseatBarSizePx + verticalDragHandleSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700444 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700445 // Pad the left and right of the workspace to ensure consistent spacing
446 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700447 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800448 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800449 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700450 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800451 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal228153d2018-01-04 15:35:22 -0800452 int availablePaddingY = Math.max(0, heightPx - topWorkspacePadding - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800453 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
454 - hotseatBarBottomPaddingPx);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700455 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700456 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700457 } else {
458 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700459 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700460 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700461 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700462 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700463 }
464 }
Winson Chungb3800242013-10-24 11:01:54 -0700465 }
466
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700467 public Rect getHotseatLayoutPadding() {
468 if (isVerticalBarLayout()) {
469 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700470 mHotseatPadding.set(mInsets.left + hotseatBarSidePaddingStartPx,
471 mInsets.top, hotseatBarSidePaddingEndPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700472 } else {
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700473 mHotseatPadding.set(hotseatBarSidePaddingEndPx, mInsets.top,
474 mInsets.right + hotseatBarSidePaddingStartPx, mInsets.bottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700475 }
476 } else {
477
478 // We want the edges of the hotseat to line up with the edges of the workspace, but the
479 // icons in the hotseat are a different size, and so don't line up perfectly. To account
480 // for this, we pad the left and right of the hotseat with half of the difference of a
481 // workspace cell vs a hotseat cell.
482 float workspaceCellWidth = (float) widthPx / inv.numColumns;
483 float hotseatCellWidth = (float) widthPx / inv.numHotseatIcons;
484 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
485 mHotseatPadding.set(
486 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx,
487 hotseatBarTopPaddingPx,
488 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx,
489 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
490 }
491 return mHotseatPadding;
492 }
493
Winsonfadbe8f2016-07-22 16:35:37 -0700494 /**
495 * @return the bounds for which the open folders should be contained within
496 */
497 public Rect getAbsoluteOpenFolderBounds() {
498 if (isVerticalBarLayout()) {
499 // Folders should only appear right of the drop target bar and left of the hotseat
500 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
501 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700502 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700503 mInsets.top + availableHeightPx);
504 } else {
505 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800506 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700507 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800508 mInsets.left + availableWidthPx - edgeMarginPx,
Jon Miranda18751b62017-07-31 17:25:27 -0700509 mInsets.top + availableHeightPx - hotseatBarSizePx
Sunny Goyal08ef6672018-05-10 11:32:00 -0700510 - verticalDragHandleSizePx - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700511 }
512 }
513
Adam Cohen2e6da152015-05-06 11:42:25 -0700514 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700515 return width / countX;
516 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700517 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700518 return height / countY;
519 }
520
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700521 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700522 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
523 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
524 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700525 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700526 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700527 return isLandscape && transposeLayoutWithOrientation;
528 }
529
Sunny Goyal59d086c2018-05-07 17:31:40 -0700530 /**
531 * Updates orientation information and returns true if it has changed from the previous value.
532 */
533 public boolean updateIsSeascape(WindowManager wm) {
534 if (isVerticalBarLayout()) {
535 boolean isSeascape = wm.getDefaultDisplay().getRotation() == Surface.ROTATION_270;
536 if (mIsSeascape != isSeascape) {
537 mIsSeascape = isSeascape;
538 return true;
539 }
540 }
541 return false;
542 }
543
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800544 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700545 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800546 }
547
Sunny Goyal07b69292018-01-08 14:19:34 -0800548 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700549 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700550 }
551
Sunny Goyalc13403c2016-11-18 23:44:48 -0800552 public int getCellHeight(@ContainerType int containerType) {
553 switch (containerType) {
554 case CellLayout.WORKSPACE:
555 return cellHeightPx;
556 case CellLayout.FOLDER:
557 return folderCellHeightPx;
558 case CellLayout.HOTSEAT:
559 return hotseatCellHeightPx;
560 default:
561 // ??
562 return 0;
563 }
564 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700565
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700566 private static Context getContext(Context c, int orientation) {
567 Configuration context = new Configuration(c.getResources().getConfiguration());
568 context.orientation = orientation;
569 return c.createConfigurationContext(context);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700570 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800571
572 /**
573 * Callback when a component changes the DeviceProfile associated with it, as a result of
574 * configuration change
575 */
576 public interface OnDeviceProfileChangeListener {
577
578 /**
579 * Called when the device profile is reassigned. Note that for layout and measurements, it
580 * is sufficient to listen for inset changes. Use this callback when you need to perform
581 * a one time operation.
582 */
583 void onDeviceProfileChanged(DeviceProfile dp);
584 }
Winson Chungb3800242013-10-24 11:01:54 -0700585}