blob: 6f35752601ad2f88ae483cde188f51bc3621535f [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;
Jon Miranda591e3602018-03-28 11:37:00 -070031import com.android.launcher3.graphics.IconNormalizer;
Winson1f064272016-07-18 17:18:02 -070032
Winson Chungb3800242013-10-24 11:01:54 -070033public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070034
Adam Cohen2e6da152015-05-06 11:42:25 -070035 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070036
Sunny Goyalc6205602015-05-21 20:46:33 -070037 // Device properties
38 public final boolean isTablet;
39 public final boolean isLargeTablet;
40 public final boolean isPhone;
41 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070042
Sunny Goyalc6205602015-05-21 20:46:33 -070043 // Device properties in current orientation
Sunny Goyald70e75a2018-02-22 10:07:32 -080044 private final boolean isLandscape;
45 public final boolean isMultiWindowMode;
46
Sunny Goyalc6205602015-05-21 20:46:33 -070047 public final int widthPx;
48 public final int heightPx;
49 public final int availableWidthPx;
50 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070051 /**
52 * The maximum amount of left/right workspace padding as a percentage of the screen width.
53 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
54 * 7% of the screen width can be used as right padding.
55 */
56 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070057
Jon Miranda3f9bab22017-07-28 14:50:17 -070058 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda30d0aa22017-07-25 15:55:29 -070059
Sunny Goyalc6205602015-05-21 20:46:33 -070060 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -080061 public final int desiredWorkspaceLeftRightMarginPx;
Jon Miranda28032002017-07-13 16:18:56 -070062 public final int cellLayoutPaddingLeftRightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070063 public final int cellLayoutBottomPaddingPx;
Sunny Goyalc6205602015-05-21 20:46:33 -070064 public final int edgeMarginPx;
65 public final Rect defaultWidgetPadding;
Sunny Goyal07b69292018-01-08 14:19:34 -080066 public final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070067 private final int topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070068 public float workspaceSpringLoadShrinkFactor;
69 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070070
Winson1f064272016-07-18 17:18:02 -070071 // Page indicator
Sunny Goyal07b69292018-01-08 14:19:34 -080072 public final int pageIndicatorSizePx;
Winson1f064272016-07-18 17:18:02 -070073
Sunny Goyalc6205602015-05-21 20:46:33 -070074 // Workspace icons
75 public int iconSizePx;
76 public int iconTextSizePx;
77 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070078 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070079
Adam Cohen59400422014-03-05 18:07:04 -080080 public int cellWidthPx;
81 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070082 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -080083
Sunny Goyalc6205602015-05-21 20:46:33 -070084 // Folder
Sunny Goyalc6205602015-05-21 20:46:33 -070085 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -070086 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -070087
88 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -070089 public int folderCellWidthPx;
90 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -070091
92 // Folder child
93 public int folderChildIconSizePx;
94 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070095 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -070096
Sunny Goyalc6205602015-05-21 20:46:33 -070097 // Hotseat
Sunny Goyalc6205602015-05-21 20:46:33 -070098 public int hotseatCellHeightPx;
Jon Miranda18751b62017-07-31 17:25:27 -070099 // In portrait: size = height, in landscape: size = width
100 public int hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800101 public final int hotseatBarTopPaddingPx;
102 public final int hotseatBarBottomPaddingPx;
103 public final int hotseatBarSidePaddingPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700104
Sunny Goyalc6205602015-05-21 20:46:33 -0700105 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700106 public int allAppsCellHeightPx;
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();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700120 private final Rect mHotseatPadding = new Rect();
Winson1f064272016-07-18 17:18:02 -0700121
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800122 // Icon badges
123 public BadgeRenderer mBadgeRenderer;
124
Sunny Goyalc6205602015-05-21 20:46:33 -0700125 public DeviceProfile(Context context, InvariantDeviceProfile inv,
126 Point minSize, Point maxSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800127 int width, int height, boolean isLandscape, boolean isMultiWindowMode) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700128
Adam Cohen2e6da152015-05-06 11:42:25 -0700129 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700130 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800131 this.isMultiWindowMode = isMultiWindowMode;
Winson Chungb3800242013-10-24 11:01:54 -0700132
Adam Cohen2e6da152015-05-06 11:42:25 -0700133 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700134 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700135
Sunny Goyalc6205602015-05-21 20:46:33 -0700136 // Constants from resources
137 isTablet = res.getBoolean(R.bool.is_tablet);
138 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
139 isPhone = !isTablet && !isLargeTablet;
140
141 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700142 transposeLayoutWithOrientation =
143 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700144
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700145 context = getContext(context, isVerticalBarLayout()
146 ? Configuration.ORIENTATION_LANDSCAPE
147 : Configuration.ORIENTATION_PORTRAIT);
148 res = context.getResources();
149
150
Winson Chungb3800242013-10-24 11:01:54 -0700151 ComponentName cn = new ComponentName(context.getPackageName(),
152 this.getClass().getName());
153 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
154 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Jon Miranda28032002017-07-13 16:18:56 -0700155 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700156 cellLayoutPaddingLeftRightPx =
157 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Jon Miranda18751b62017-07-31 17:25:27 -0700158 cellLayoutBottomPaddingPx =
159 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_bottom_padding);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700160 pageIndicatorSizePx = res.getDimensionPixelSize(
161 R.dimen.dynamic_grid_min_page_indicator_size);
Winson Chungb3800242013-10-24 11:01:54 -0700162 defaultPageSpacingPx =
163 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700164 topWorkspacePadding =
165 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700166 iconDrawablePaddingOriginalPx =
167 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700168 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
169 workspaceSpringLoadedBottomSpace =
170 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700171
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700172 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700173
Winson1f064272016-07-18 17:18:02 -0700174 hotseatBarTopPaddingPx =
175 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Sunny Goyald5190522017-04-24 03:06:54 -0700176 hotseatBarBottomPaddingPx =
177 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Sunny Goyal228153d2018-01-04 15:35:22 -0800178 hotseatBarSidePaddingPx =
179 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda18751b62017-07-31 17:25:27 -0700180 hotseatBarSizePx = isVerticalBarLayout()
181 ? Utilities.pxFromDp(inv.iconSize, dm)
182 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_size)
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700183 + hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx;
184
Sunny Goyalc6205602015-05-21 20:46:33 -0700185 // Determine sizes.
186 widthPx = width;
187 heightPx = height;
188 if (isLandscape) {
189 availableWidthPx = maxSize.x;
190 availableHeightPx = minSize.y;
191 } else {
192 availableWidthPx = minSize.x;
193 availableHeightPx = maxSize.y;
194 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700195
Jon Miranda2ed276e2017-06-29 11:36:34 -0700196 // Calculate all of the remaining variables.
Sunny Goyalc6205602015-05-21 20:46:33 -0700197 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700198
199 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700200 float aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda30d0aa22017-07-25 15:55:29 -0700201 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
202 if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700203 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700204 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700205 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700206 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700207 int extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700208 hotseatBarSizePx += extraSpace - pageIndicatorSizePx;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700209
Jon Miranda3f9bab22017-07-28 14:50:17 -0700210 // Recalculate the available dimensions using the new hotseat size.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700211 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700212 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800213 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800214
215 // This is done last, after iconSizePx is calculated above.
Sunny Goyal179249d2017-12-19 16:49:24 -0800216 mBadgeRenderer = new BadgeRenderer(iconSizePx);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700217 }
218
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800219 public DeviceProfile copy(Context context) {
220 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyald70e75a2018-02-22 10:07:32 -0800221 return new DeviceProfile(context, inv, size, size, widthPx, heightPx, isLandscape,
222 isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800223 }
224
Winson Chung2fda6ce2018-01-31 14:09:49 -0800225 public DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700226 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
227 // the system decor is always excluded.
228 mwSize.set(Math.min(availableWidthPx, mwSize.x), Math.min(availableHeightPx, mwSize.y));
229
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700230 // In multi-window mode, we can have widthPx = availableWidthPx
231 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
232 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800233 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800234 isLandscape, true);
Jon Miranda93e1f042016-11-11 14:13:04 -0800235
Jon Miranda0cd04572017-09-19 11:31:42 -0700236 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
237 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
238 - iconDrawablePaddingPx - profile.iconTextSizePx;
239 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
240 profile.adjustToHideWorkspaceLabels();
241 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800242
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800243 // We use these scales to measure and layout the widgets using their full invariant profile
244 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
245 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
246 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
247 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800248 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800249
Jon Miranda93e1f042016-11-11 14:13:04 -0800250 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700251 }
252
Adam Cohen63f1ec02014-08-12 09:23:13 -0700253 /**
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800254 * Inverse of {@link #getMultiWindowProfile(Context, Point)}
255 * @return device profile corresponding to the current orientation in non multi-window mode.
256 */
257 public DeviceProfile getFullScreenProfile() {
258 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
259 }
260
261 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700262 * Adjusts the profile so that the labels on the Workspace are hidden.
263 * It is important to call this method after the All Apps variables have been set.
264 */
265 private void adjustToHideWorkspaceLabels() {
266 iconTextSizePx = 0;
267 iconDrawablePaddingPx = 0;
268 cellHeightPx = iconSizePx;
269
270 // In normal cases, All Apps cell height should equal the Workspace cell height.
271 // Since we are removing labels from the Workspace, we need to manually compute the
272 // All Apps cell height.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700273 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700274 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
275 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700276 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700277 }
278
Sunny Goyalc6205602015-05-21 20:46:33 -0700279 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Miranda09660722017-06-14 14:20:14 -0700280 updateIconSize(1f, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800281
Jon Mirandabf7d8122016-11-03 15:29:29 -0700282 // Check to see if the icons fit within the available height. If not, then scale down.
283 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700284 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700285 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700286 float scale = maxHeight / usedHeight;
Jon Miranda09660722017-06-14 14:20:14 -0700287 updateIconSize(scale, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700288 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700289 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700290 }
291
Jon Miranda09660722017-06-14 14:20:14 -0700292 private void updateIconSize(float scale, Resources res, DisplayMetrics dm) {
Jon Miranda18751b62017-07-31 17:25:27 -0700293 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800294 final boolean isVerticalLayout = isVerticalBarLayout();
295 float invIconSizePx = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700296 iconSizePx = (int) (Utilities.pxFromDp(invIconSizePx, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800297 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700298 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700299
Jon Miranda18751b62017-07-31 17:25:27 -0700300 cellHeightPx = iconSizePx + iconDrawablePaddingPx
301 + Utilities.calculateTextHeight(iconTextSizePx);
Jon Miranda846455e2017-10-02 14:58:52 -0700302 int cellYPadding = (getCellSize().y - cellHeightPx) / 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800303 if (iconDrawablePaddingPx > cellYPadding && !isVerticalLayout
Sunny Goyald70e75a2018-02-22 10:07:32 -0800304 && !isMultiWindowMode) {
Jon Miranda846455e2017-10-02 14:58:52 -0700305 // Ensures that the label is closer to its corresponding icon. This is not an issue
306 // with vertical bar layout or multi-window mode since the issue is handled separately
307 // with their calls to {@link #adjustToHideWorkspaceLabels}.
308 cellHeightPx -= (iconDrawablePaddingPx - cellYPadding);
309 iconDrawablePaddingPx = cellYPadding;
310 }
311 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700312
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700313 // All apps
314 allAppsIconTextSizePx = iconTextSizePx;
Winson1f064272016-07-18 17:18:02 -0700315 allAppsIconSizePx = iconSizePx;
316 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700317 allAppsCellHeightPx = getCellSize().y;
318
Sunny Goyal07b69292018-01-08 14:19:34 -0800319 if (isVerticalLayout) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700320 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700321 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700322 }
Winson Chungb3800242013-10-24 11:01:54 -0700323
Winson Chungb3800242013-10-24 11:01:54 -0700324 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800325 if (isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700326 hotseatBarSizePx = iconSizePx;
327 }
Jon Miranda5044c352017-08-09 11:37:59 -0700328 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700329
Sunny Goyal07b69292018-01-08 14:19:34 -0800330 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700331 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700332 - pageIndicatorSizePx - topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700333 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
334 workspaceSpringLoadShrinkFactor = Math.min(
335 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
336 1 - (minRequiredHeight / expectedWorkspaceHeight));
337 } else {
338 workspaceSpringLoadShrinkFactor =
339 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
340 }
341
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700342 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700343 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
344 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700345 }
346
Jon Mirandabf7d8122016-11-03 15:29:29 -0700347 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
348 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
349 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
350 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
351
Jon Mirandac1b23992016-12-13 08:57:36 -0800352 updateFolderCellSize(1f, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700353
Jon Mirandac1b23992016-12-13 08:57:36 -0800354 // Don't let the folder get too close to the edges of the screen.
Jon Miranda72b5fd12017-06-25 18:06:23 -0700355 int folderMargin = edgeMarginPx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800356 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800357
358 // Check if the icons fit within the available height.
359 float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize;
Sunny Goyal07b69292018-01-08 14:19:34 -0800360 int maxHeight = availableHeightPx - totalWorkspacePadding.y - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800361 float scaleY = maxHeight / usedHeight;
362
363 // Check if the icons fit within the available width.
364 float usedWidth = folderCellWidthPx * inv.numFolderColumns;
Sunny Goyal07b69292018-01-08 14:19:34 -0800365 int maxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800366 float scaleX = maxWidth / usedWidth;
367
368 float scale = Math.min(scaleX, scaleY);
369 if (scale < 1f) {
370 updateFolderCellSize(scale, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700371 }
372 }
373
Jon Mirandac1b23992016-12-13 08:57:36 -0800374 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700375 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
376 folderChildTextSizePx =
377 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
378
379 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
380 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
381 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
382
Jon Mirandac1b23992016-12-13 08:57:36 -0800383 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000384 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
385 folderChildDrawablePaddingPx = Math.max(0,
386 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700387 }
388
Winson1f064272016-07-18 17:18:02 -0700389 public void updateInsets(Rect insets) {
390 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800391 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700392 }
393
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800394 public Rect getInsets() {
395 return mInsets;
396 }
397
Sunny Goyal756cd262015-08-20 12:33:21 -0700398 public Point getCellSize() {
399 Point result = new Point();
400 // Since we are only concerned with the overall padding, layout direction does
401 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700402 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700403 result.x = calculateCellWidth(availableWidthPx - padding.x
404 - cellLayoutPaddingLeftRightPx * 2, inv.numColumns);
405 result.y = calculateCellHeight(availableHeightPx - padding.y
406 - cellLayoutBottomPaddingPx, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700407 return result;
408 }
409
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700410 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800411 updateWorkspacePadding();
412 return new Point(workspacePadding.left + workspacePadding.right,
413 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700414 }
415
416 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800417 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
418 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700419 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800420 private void updateWorkspacePadding() {
421 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700422 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800423 padding.top = 0;
424 padding.bottom = edgeMarginPx;
425 padding.left = hotseatBarSidePaddingPx;
426 padding.right = hotseatBarSidePaddingPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800427 if (isSeascape()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800428 padding.left += hotseatBarSizePx;
429 padding.right += pageIndicatorSizePx;
Winson1f064272016-07-18 17:18:02 -0700430 } else {
Sunny Goyal228153d2018-01-04 15:35:22 -0800431 padding.left += pageIndicatorSizePx;
432 padding.right += hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700433 }
Winson Chungb3800242013-10-24 11:01:54 -0700434 } else {
Jon Miranda18751b62017-07-31 17:25:27 -0700435 int paddingBottom = hotseatBarSizePx + pageIndicatorSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700436 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700437 // Pad the left and right of the workspace to ensure consistent spacing
438 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700439 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800440 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800441 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700442 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800443 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal228153d2018-01-04 15:35:22 -0800444 int availablePaddingY = Math.max(0, heightPx - topWorkspacePadding - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800445 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
446 - hotseatBarBottomPaddingPx);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700447 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700448 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700449 } else {
450 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700451 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700452 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700453 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700454 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700455 }
456 }
Winson Chungb3800242013-10-24 11:01:54 -0700457 }
458
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700459 public Rect getHotseatLayoutPadding() {
460 if (isVerticalBarLayout()) {
461 if (isSeascape()) {
462 mHotseatPadding.set(
463 mInsets.left, mInsets.top, hotseatBarSidePaddingPx, mInsets.bottom);
464 } else {
465 mHotseatPadding.set(
466 hotseatBarSidePaddingPx, mInsets.top, mInsets.right, mInsets.bottom);
467 }
468 } else {
469
470 // We want the edges of the hotseat to line up with the edges of the workspace, but the
471 // icons in the hotseat are a different size, and so don't line up perfectly. To account
472 // for this, we pad the left and right of the hotseat with half of the difference of a
473 // workspace cell vs a hotseat cell.
474 float workspaceCellWidth = (float) widthPx / inv.numColumns;
475 float hotseatCellWidth = (float) widthPx / inv.numHotseatIcons;
476 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
477 mHotseatPadding.set(
478 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx,
479 hotseatBarTopPaddingPx,
480 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx,
481 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
482 }
483 return mHotseatPadding;
484 }
485
Winsonfadbe8f2016-07-22 16:35:37 -0700486 /**
487 * @return the bounds for which the open folders should be contained within
488 */
489 public Rect getAbsoluteOpenFolderBounds() {
490 if (isVerticalBarLayout()) {
491 // Folders should only appear right of the drop target bar and left of the hotseat
492 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
493 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700494 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700495 mInsets.top + availableHeightPx);
496 } else {
497 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800498 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700499 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800500 mInsets.left + availableWidthPx - edgeMarginPx,
Jon Miranda18751b62017-07-31 17:25:27 -0700501 mInsets.top + availableHeightPx - hotseatBarSizePx
Jon Miranda2ed276e2017-06-29 11:36:34 -0700502 - pageIndicatorSizePx - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700503 }
504 }
505
Adam Cohen2e6da152015-05-06 11:42:25 -0700506 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700507 return width / countX;
508 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700509 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700510 return height / countY;
511 }
512
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700513 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700514 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
515 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
516 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700517 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700518 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700519 return isLandscape && transposeLayoutWithOrientation;
520 }
521
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800522 public boolean isSeascape() {
523 // TODO: This might not hold true for multi window mode, use configuration insead.
524 return isVerticalBarLayout() && mInsets.left > mInsets.right;
525 }
526
Sunny Goyal07b69292018-01-08 14:19:34 -0800527 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700528 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700529 }
530
Sunny Goyalc13403c2016-11-18 23:44:48 -0800531 public int getCellHeight(@ContainerType int containerType) {
532 switch (containerType) {
533 case CellLayout.WORKSPACE:
534 return cellHeightPx;
535 case CellLayout.FOLDER:
536 return folderCellHeightPx;
537 case CellLayout.HOTSEAT:
538 return hotseatCellHeightPx;
539 default:
540 // ??
541 return 0;
542 }
543 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700544
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700545 private static Context getContext(Context c, int orientation) {
546 Configuration context = new Configuration(c.getResources().getConfiguration());
547 context.orientation = orientation;
548 return c.createConfigurationContext(context);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700549 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800550
551 /**
552 * Callback when a component changes the DeviceProfile associated with it, as a result of
553 * configuration change
554 */
555 public interface OnDeviceProfileChangeListener {
556
557 /**
558 * Called when the device profile is reassigned. Note that for layout and measurements, it
559 * is sufficient to listen for inset changes. Use this callback when you need to perform
560 * a one time operation.
561 */
562 void onDeviceProfileChanged(DeviceProfile dp);
563 }
Winson Chungb3800242013-10-24 11:01:54 -0700564}