blob: 7a6a244318cc93ecebaeaba8426f3d14189befdf [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;
Winson Chungb3800242013-10-24 11:01:54 -070028
Sunny Goyalc13403c2016-11-18 23:44:48 -080029import com.android.launcher3.CellLayout.ContainerType;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080030import com.android.launcher3.badge.BadgeRenderer;
Winson1f064272016-07-18 17:18:02 -070031
Winson Chungb3800242013-10-24 11:01:54 -070032public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070033
Adam Cohen2e6da152015-05-06 11:42:25 -070034 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070035
Sunny Goyalc6205602015-05-21 20:46:33 -070036 // Device properties
37 public final boolean isTablet;
38 public final boolean isLargeTablet;
39 public final boolean isPhone;
40 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070041
Sunny Goyalc6205602015-05-21 20:46:33 -070042 // Device properties in current orientation
43 public final boolean isLandscape;
44 public final int widthPx;
45 public final int heightPx;
46 public final int availableWidthPx;
47 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070048 /**
49 * The maximum amount of left/right workspace padding as a percentage of the screen width.
50 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
51 * 7% of the screen width can be used as right padding.
52 */
53 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070054
Jon Miranda3f9bab22017-07-28 14:50:17 -070055 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070056
Sunny Goyalc6205602015-05-21 20:46:33 -070057 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -080058 public final int desiredWorkspaceLeftRightMarginPx;
Jon Miranda28032002017-07-13 16:18:56 -070059 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070060 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -070061 public final int edgeMarginPx;
62 public final Rect defaultWidgetPadding;
Sunny Goyal07b69292018-01-08 14:19:34 -080063 public final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070064 private final int topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070065 public float workspaceSpringLoadShrinkFactor;
66 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070067
Winson1f064272016-07-18 17:18:02 -070068 // Page indicator
Sunny Goyal07b69292018-01-08 14:19:34 -080069 public final int pageIndicatorSizePx;
Winson1f064272016-07-18 17:18:02 -070070
Sunny Goyalc6205602015-05-21 20:46:33 -070071 // Workspace icons
72 public int iconSizePx;
73 public int iconTextSizePx;
74 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070075 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070076
Adam Cohen59400422014-03-05 18:07:04 -080077 public int cellWidthPx;
78 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070079 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -080080
Sunny Goyalc6205602015-05-21 20:46:33 -070081 // Folder
82 public int folderBackgroundOffset;
83 public int folderIconSizePx;
Adam Cohenefca0272016-02-24 19:19:06 -080084 public int folderIconPreviewPadding;
Jon Mirandabf7d8122016-11-03 15:29:29 -070085
86 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -070087 public int folderCellWidthPx;
88 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -070089
90 // Folder child
91 public int folderChildIconSizePx;
92 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070093 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -070094
Sunny Goyalc6205602015-05-21 20:46:33 -070095 // Hotseat
Sunny Goyalc6205602015-05-21 20:46:33 -070096 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070097 // In portrait: size = height, in landscape: size = width
98 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -080099 public final int hotseatBarTopPaddingPx;
100 public final int hotseatBarBottomPaddingPx;
101 public final int hotseatBarSidePaddingPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700102
Sunny Goyalc6205602015-05-21 20:46:33 -0700103 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700104 public int allAppsCellHeightPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700105 public int allAppsNumCols;
106 public int allAppsNumPredictiveCols;
Winson1f064272016-07-18 17:18:02 -0700107 public int allAppsIconSizePx;
108 public int allAppsIconDrawablePaddingPx;
109 public float allAppsIconTextSizePx;
110
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800111 // Widgets
112 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
113
Sunny Goyal47328fd2016-05-25 18:56:41 -0700114 // Drop Target
115 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700116
Winson1f064272016-07-18 17:18:02 -0700117 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800118 private final Rect mInsets = new Rect();
119 public final Rect workspacePadding = new Rect();
Winson1f064272016-07-18 17:18:02 -0700120
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800121 // Icon badges
122 public BadgeRenderer mBadgeRenderer;
123
Sunny Goyalc6205602015-05-21 20:46:33 -0700124 public DeviceProfile(Context context, InvariantDeviceProfile inv,
125 Point minSize, Point maxSize,
126 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700127
Adam Cohen2e6da152015-05-06 11:42:25 -0700128 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700129 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700130
Adam Cohen2e6da152015-05-06 11:42:25 -0700131 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700132 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700133
Sunny Goyalc6205602015-05-21 20:46:33 -0700134 // Constants from resources
135 isTablet = res.getBoolean(R.bool.is_tablet);
136 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
137 isPhone = !isTablet && !isLargeTablet;
138
139 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700140 transposeLayoutWithOrientation =
141 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700142
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700143 context = getContext(context, isVerticalBarLayout()
144 ? Configuration.ORIENTATION_LANDSCAPE
145 : Configuration.ORIENTATION_PORTRAIT);
146 res = context.getResources();
147
148
Winson Chungb3800242013-10-24 11:01:54 -0700149 ComponentName cn = new ComponentName(context.getPackageName(),
150 this.getClass().getName());
151 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
152 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Jon Miranda28032002017-07-13 16:18:56 -0700153 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700154 cellLayoutPaddingLeftRightPx =
155 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Jon Miranda18751b62017-07-31 17:25:27 -0700156 cellLayoutBottomPaddingPx =
157 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_bottom_padding);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700158 pageIndicatorSizePx = res.getDimensionPixelSize(
159 R.dimen.dynamic_grid_min_page_indicator_size);
Winson Chungb3800242013-10-24 11:01:54 -0700160 defaultPageSpacingPx =
161 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700162 topWorkspacePadding =
163 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700164 iconDrawablePaddingOriginalPx =
165 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700166 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
167 workspaceSpringLoadedBottomSpace =
168 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700169
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700170 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700171
Winson1f064272016-07-18 17:18:02 -0700172 hotseatBarTopPaddingPx =
173 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Sunny Goyald5190522017-04-24 03:06:54 -0700174 hotseatBarBottomPaddingPx =
175 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Sunny Goyal228153d2018-01-04 15:35:22 -0800176 hotseatBarSidePaddingPx =
177 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda18751b62017-07-31 17:25:27 -0700178 hotseatBarSizePx = isVerticalBarLayout()
179 ? Utilities.pxFromDp(inv.iconSize, dm)
180 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_size)
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700181 + hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx;
182
Sunny Goyalc6205602015-05-21 20:46:33 -0700183 // Determine sizes.
184 widthPx = width;
185 heightPx = height;
186 if (isLandscape) {
187 availableWidthPx = maxSize.x;
188 availableHeightPx = minSize.y;
189 } else {
190 availableWidthPx = minSize.x;
191 availableHeightPx = maxSize.y;
192 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700193
Jon Miranda2ed276e2017-06-29 11:36:34 -0700194 // Calculate all of the remaining variables.
Sunny Goyalc6205602015-05-21 20:46:33 -0700195 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700196
197 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700198 float aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda30d0aa22017-07-25 15:55:29 -0700199 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
200 if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700201 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700202 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700203 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700204 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700205 int extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700206 hotseatBarSizePx += extraSpace - pageIndicatorSizePx;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700207
Jon Miranda3f9bab22017-07-28 14:50:17 -0700208 // Recalculate the available dimensions using the new hotseat size.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700209 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700210 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800211 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800212
213 // This is done last, after iconSizePx is calculated above.
Sunny Goyal179249d2017-12-19 16:49:24 -0800214 mBadgeRenderer = new BadgeRenderer(iconSizePx);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700215 }
216
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800217 public DeviceProfile copy(Context context) {
218 Point size = new Point(availableWidthPx, availableHeightPx);
219 return new DeviceProfile(context, inv, size, size, widthPx, heightPx, isLandscape);
220 }
221
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700222 DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700223 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
224 // the system decor is always excluded.
225 mwSize.set(Math.min(availableWidthPx, mwSize.x), Math.min(availableHeightPx, mwSize.y));
226
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700227 // In multi-window mode, we can have widthPx = availableWidthPx
228 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
229 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800230 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
231 isLandscape);
232
Jon Miranda0cd04572017-09-19 11:31:42 -0700233 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
234 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
235 - iconDrawablePaddingPx - profile.iconTextSizePx;
236 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
237 profile.adjustToHideWorkspaceLabels();
238 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800239
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800240 // We use these scales to measure and layout the widgets using their full invariant profile
241 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
242 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
243 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
244 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800245 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800246
Jon Miranda93e1f042016-11-11 14:13:04 -0800247 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700248 }
249
Adam Cohen63f1ec02014-08-12 09:23:13 -0700250 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700251 * Adjusts the profile so that the labels on the Workspace are hidden.
252 * It is important to call this method after the All Apps variables have been set.
253 */
254 private void adjustToHideWorkspaceLabels() {
255 iconTextSizePx = 0;
256 iconDrawablePaddingPx = 0;
257 cellHeightPx = iconSizePx;
258
259 // In normal cases, All Apps cell height should equal the Workspace cell height.
260 // Since we are removing labels from the Workspace, we need to manually compute the
261 // All Apps cell height.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700262 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700263 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
264 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700265 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700266 }
267
Sunny Goyalc6205602015-05-21 20:46:33 -0700268 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Miranda09660722017-06-14 14:20:14 -0700269 updateIconSize(1f, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800270
Jon Mirandabf7d8122016-11-03 15:29:29 -0700271 // Check to see if the icons fit within the available height. If not, then scale down.
272 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700273 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700274 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700275 float scale = maxHeight / usedHeight;
Jon Miranda09660722017-06-14 14:20:14 -0700276 updateIconSize(scale, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700277 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700278 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700279 }
280
Jon Miranda09660722017-06-14 14:20:14 -0700281 private void updateIconSize(float scale, Resources res, DisplayMetrics dm) {
Jon Miranda18751b62017-07-31 17:25:27 -0700282 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800283 final boolean isVerticalLayout = isVerticalBarLayout();
284 float invIconSizePx = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700285 iconSizePx = (int) (Utilities.pxFromDp(invIconSizePx, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800286 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700287 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700288
Jon Miranda18751b62017-07-31 17:25:27 -0700289 cellHeightPx = iconSizePx + iconDrawablePaddingPx
290 + Utilities.calculateTextHeight(iconTextSizePx);
Jon Miranda846455e2017-10-02 14:58:52 -0700291 int cellYPadding = (getCellSize().y - cellHeightPx) / 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800292 if (iconDrawablePaddingPx > cellYPadding && !isVerticalLayout
Jon Miranda846455e2017-10-02 14:58:52 -0700293 && !inMultiWindowMode()) {
294 // Ensures that the label is closer to its corresponding icon. This is not an issue
295 // with vertical bar layout or multi-window mode since the issue is handled separately
296 // with their calls to {@link #adjustToHideWorkspaceLabels}.
297 cellHeightPx -= (iconDrawablePaddingPx - cellYPadding);
298 iconDrawablePaddingPx = cellYPadding;
299 }
300 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700301
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700302 // All apps
303 allAppsIconTextSizePx = iconTextSizePx;
Winson1f064272016-07-18 17:18:02 -0700304 allAppsIconSizePx = iconSizePx;
305 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700306 allAppsCellHeightPx = getCellSize().y;
307
Sunny Goyal07b69292018-01-08 14:19:34 -0800308 if (isVerticalLayout) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700309 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700310 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700311 }
Winson Chungb3800242013-10-24 11:01:54 -0700312
Winson Chungb3800242013-10-24 11:01:54 -0700313 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800314 if (isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700315 hotseatBarSizePx = iconSizePx;
316 }
Jon Miranda5044c352017-08-09 11:37:59 -0700317 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700318
Sunny Goyal07b69292018-01-08 14:19:34 -0800319 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700320 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700321 - pageIndicatorSizePx - topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700322 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
323 workspaceSpringLoadShrinkFactor = Math.min(
324 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
325 1 - (minRequiredHeight / expectedWorkspaceHeight));
326 } else {
327 workspaceSpringLoadShrinkFactor =
328 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
329 }
330
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700331 // Folder icon
Jon Miranda09660722017-06-14 14:20:14 -0700332 folderBackgroundOffset = -iconDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700333 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800334 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700335 }
336
Jon Mirandabf7d8122016-11-03 15:29:29 -0700337 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
338 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
339 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
340 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
341
Jon Mirandac1b23992016-12-13 08:57:36 -0800342 updateFolderCellSize(1f, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700343
Jon Mirandac1b23992016-12-13 08:57:36 -0800344 // Don't let the folder get too close to the edges of the screen.
Jon Miranda72b5fd12017-06-25 18:06:23 -0700345 int folderMargin = edgeMarginPx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800346 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800347
348 // Check if the icons fit within the available height.
349 float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize;
Sunny Goyal07b69292018-01-08 14:19:34 -0800350 int maxHeight = availableHeightPx - totalWorkspacePadding.y - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800351 float scaleY = maxHeight / usedHeight;
352
353 // Check if the icons fit within the available width.
354 float usedWidth = folderCellWidthPx * inv.numFolderColumns;
Sunny Goyal07b69292018-01-08 14:19:34 -0800355 int maxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800356 float scaleX = maxWidth / usedWidth;
357
358 float scale = Math.min(scaleX, scaleY);
359 if (scale < 1f) {
360 updateFolderCellSize(scale, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700361 }
362 }
363
Jon Mirandac1b23992016-12-13 08:57:36 -0800364 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700365 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
366 folderChildTextSizePx =
367 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
368
369 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
370 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
371 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
372
Jon Mirandac1b23992016-12-13 08:57:36 -0800373 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000374 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
375 folderChildDrawablePaddingPx = Math.max(0,
376 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700377 }
378
Winson1f064272016-07-18 17:18:02 -0700379 public void updateInsets(Rect insets) {
380 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800381 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700382 }
383
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800384 public Rect getInsets() {
385 return mInsets;
386 }
387
Winson1f064272016-07-18 17:18:02 -0700388 public void updateAppsViewNumCols() {
389 allAppsNumCols = allAppsNumPredictiveCols = inv.numColumns;
Winson Chungb3800242013-10-24 11:01:54 -0700390 }
391
Sunny Goyal756cd262015-08-20 12:33:21 -0700392 public Point getCellSize() {
393 Point result = new Point();
394 // Since we are only concerned with the overall padding, layout direction does
395 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700396 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700397 result.x = calculateCellWidth(availableWidthPx - padding.x
398 - cellLayoutPaddingLeftRightPx * 2, inv.numColumns);
399 result.y = calculateCellHeight(availableHeightPx - padding.y
400 - cellLayoutBottomPaddingPx, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700401 return result;
402 }
403
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700404 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800405 updateWorkspacePadding();
406 return new Point(workspacePadding.left + workspacePadding.right,
407 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700408 }
409
410 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800411 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
412 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700413 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800414 private void updateWorkspacePadding() {
415 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700416 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800417 padding.top = 0;
418 padding.bottom = edgeMarginPx;
419 padding.left = hotseatBarSidePaddingPx;
420 padding.right = hotseatBarSidePaddingPx;
421 if (mInsets.left > mInsets.right) {
422 padding.left += hotseatBarSizePx;
423 padding.right += pageIndicatorSizePx;
Winson1f064272016-07-18 17:18:02 -0700424 } else {
Sunny Goyal228153d2018-01-04 15:35:22 -0800425 padding.left += pageIndicatorSizePx;
426 padding.right += hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700427 }
Winson Chungb3800242013-10-24 11:01:54 -0700428 } else {
Jon Miranda18751b62017-07-31 17:25:27 -0700429 int paddingBottom = hotseatBarSizePx + pageIndicatorSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700430 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700431 // Pad the left and right of the workspace to ensure consistent spacing
432 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700433 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800434 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800435 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700436 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800437 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal228153d2018-01-04 15:35:22 -0800438 int availablePaddingY = Math.max(0, heightPx - topWorkspacePadding - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800439 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
440 - hotseatBarBottomPaddingPx);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700441 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700442 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700443 } else {
444 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700445 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700446 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700447 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700448 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700449 }
450 }
Winson Chungb3800242013-10-24 11:01:54 -0700451 }
452
Winsonfadbe8f2016-07-22 16:35:37 -0700453 /**
454 * @return the bounds for which the open folders should be contained within
455 */
456 public Rect getAbsoluteOpenFolderBounds() {
457 if (isVerticalBarLayout()) {
458 // Folders should only appear right of the drop target bar and left of the hotseat
459 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
460 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700461 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700462 mInsets.top + availableHeightPx);
463 } else {
464 // Folders should only appear below the drop target bar and above the hotseat
465 return new Rect(mInsets.left,
466 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
467 mInsets.left + availableWidthPx,
Jon Miranda18751b62017-07-31 17:25:27 -0700468 mInsets.top + availableHeightPx - hotseatBarSizePx
Jon Miranda2ed276e2017-06-29 11:36:34 -0700469 - pageIndicatorSizePx - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700470 }
471 }
472
Adam Cohen2e6da152015-05-06 11:42:25 -0700473 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700474 return width / countX;
475 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700476 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700477 return height / countY;
478 }
479
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700480 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700481 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
482 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
483 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700484 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700485 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700486 return isLandscape && transposeLayoutWithOrientation;
487 }
488
Sunny Goyal07b69292018-01-08 14:19:34 -0800489 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700490 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700491 }
492
Sunny Goyalc13403c2016-11-18 23:44:48 -0800493 public int getCellHeight(@ContainerType int containerType) {
494 switch (containerType) {
495 case CellLayout.WORKSPACE:
496 return cellHeightPx;
497 case CellLayout.FOLDER:
498 return folderCellHeightPx;
499 case CellLayout.HOTSEAT:
500 return hotseatCellHeightPx;
501 default:
502 // ??
503 return 0;
504 }
505 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700506
Jon Miranda846455e2017-10-02 14:58:52 -0700507 public boolean inMultiWindowMode() {
508 return this != inv.landscapeProfile && this != inv.portraitProfile;
509 }
510
Jon Miranda51f037d2016-11-07 08:37:20 -0800511 public boolean shouldIgnoreLongPressToOverview(float touchX) {
Jon Miranda51f037d2016-11-07 08:37:20 -0800512 boolean touchedLhsEdge = mInsets.left == 0 && touchX < edgeMarginPx;
513 boolean touchedRhsEdge = mInsets.right == 0 && touchX > (widthPx - edgeMarginPx);
Jon Miranda846455e2017-10-02 14:58:52 -0700514 return !inMultiWindowMode() && (touchedLhsEdge || touchedRhsEdge);
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700515 }
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700516
517 private static Context getContext(Context c, int orientation) {
518 Configuration context = new Configuration(c.getResources().getConfiguration());
519 context.orientation = orientation;
520 return c.createConfigurationContext(context);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700521 }
Winson Chungb3800242013-10-24 11:01:54 -0700522}