blob: 4f9920c5b04acf5aad95fc0f3cc0930ce06ce56d [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
Winson1f064272016-07-18 17:18:02 -070073 // Page indicator
Sunny Goyal07b69292018-01-08 14:19:34 -080074 public final int pageIndicatorSizePx;
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;
105 public final int hotseatBarSidePaddingPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700106
Sunny Goyalc6205602015-05-21 20:46:33 -0700107 // All apps
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700108 public int allAppsCellHeightPx;
Winson1f064272016-07-18 17:18:02 -0700109 public int allAppsIconSizePx;
110 public int allAppsIconDrawablePaddingPx;
111 public float allAppsIconTextSizePx;
112
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800113 // Widgets
114 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
115
Sunny Goyal47328fd2016-05-25 18:56:41 -0700116 // Drop Target
117 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700118
Winson1f064272016-07-18 17:18:02 -0700119 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800120 private final Rect mInsets = new Rect();
121 public final Rect workspacePadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700122 private final Rect mHotseatPadding = new Rect();
Sunny Goyal59d086c2018-05-07 17:31:40 -0700123 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700124
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800125 // Icon badges
126 public BadgeRenderer mBadgeRenderer;
127
Sunny Goyalc6205602015-05-21 20:46:33 -0700128 public DeviceProfile(Context context, InvariantDeviceProfile inv,
129 Point minSize, Point maxSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800130 int width, int height, boolean isLandscape, boolean isMultiWindowMode) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700131
Adam Cohen2e6da152015-05-06 11:42:25 -0700132 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700133 this.isLandscape = isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -0800134 this.isMultiWindowMode = isMultiWindowMode;
Winson Chungb3800242013-10-24 11:01:54 -0700135
Adam Cohen2e6da152015-05-06 11:42:25 -0700136 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700137 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700138
Sunny Goyalc6205602015-05-21 20:46:33 -0700139 // Constants from resources
140 isTablet = res.getBoolean(R.bool.is_tablet);
141 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
142 isPhone = !isTablet && !isLargeTablet;
143
144 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700145 transposeLayoutWithOrientation =
146 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700147
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700148 context = getContext(context, isVerticalBarLayout()
149 ? Configuration.ORIENTATION_LANDSCAPE
150 : Configuration.ORIENTATION_PORTRAIT);
151 res = context.getResources();
152
153
Winson Chungb3800242013-10-24 11:01:54 -0700154 ComponentName cn = new ComponentName(context.getPackageName(),
155 this.getClass().getName());
156 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
157 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Jon Miranda28032002017-07-13 16:18:56 -0700158 desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700159 cellLayoutPaddingLeftRightPx =
160 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_padding);
Jon Miranda18751b62017-07-31 17:25:27 -0700161 cellLayoutBottomPaddingPx =
162 res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_layout_bottom_padding);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700163 pageIndicatorSizePx = res.getDimensionPixelSize(
164 R.dimen.dynamic_grid_min_page_indicator_size);
Winson Chungb3800242013-10-24 11:01:54 -0700165 defaultPageSpacingPx =
166 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700167 topWorkspacePadding =
168 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700169 iconDrawablePaddingOriginalPx =
170 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700171 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
172 workspaceSpringLoadedBottomSpace =
173 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700174
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700175 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700176
Winson1f064272016-07-18 17:18:02 -0700177 hotseatBarTopPaddingPx =
178 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Sunny Goyald5190522017-04-24 03:06:54 -0700179 hotseatBarBottomPaddingPx =
180 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
Sunny Goyal228153d2018-01-04 15:35:22 -0800181 hotseatBarSidePaddingPx =
182 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda18751b62017-07-31 17:25:27 -0700183 hotseatBarSizePx = isVerticalBarLayout()
184 ? Utilities.pxFromDp(inv.iconSize, dm)
185 : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_size)
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700186 + hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx;
187
Sunny Goyalc6205602015-05-21 20:46:33 -0700188 // Determine sizes.
189 widthPx = width;
190 heightPx = height;
191 if (isLandscape) {
192 availableWidthPx = maxSize.x;
193 availableHeightPx = minSize.y;
194 } else {
195 availableWidthPx = minSize.x;
196 availableHeightPx = maxSize.y;
197 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700198
Jon Miranda2ed276e2017-06-29 11:36:34 -0700199 // Calculate all of the remaining variables.
Sunny Goyalc6205602015-05-21 20:46:33 -0700200 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700201
202 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700203 float aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
Jon Miranda30d0aa22017-07-25 15:55:29 -0700204 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
205 if (!isVerticalBarLayout() && isPhone && isTallDevice) {
Jon Miranda3f9bab22017-07-28 14:50:17 -0700206 // We increase the hotseat size when there is extra space.
Jon Miranda2ed276e2017-06-29 11:36:34 -0700207 // ie. For a display with a large aspect ratio, we can keep the icons on the workspace
Jon Miranda3f9bab22017-07-28 14:50:17 -0700208 // in portrait mode closer together by adding more height to the hotseat.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700209 // Note: This calculation was created after noticing a pattern in the design spec.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700210 int extraSpace = getCellSize().y - iconSizePx - iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700211 hotseatBarSizePx += extraSpace - pageIndicatorSizePx;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700212
Jon Miranda3f9bab22017-07-28 14:50:17 -0700213 // Recalculate the available dimensions using the new hotseat size.
Jon Miranda30d0aa22017-07-25 15:55:29 -0700214 updateAvailableDimensions(dm, res);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700215 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800216 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800217
218 // This is done last, after iconSizePx is calculated above.
Sunny Goyal179249d2017-12-19 16:49:24 -0800219 mBadgeRenderer = new BadgeRenderer(iconSizePx);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700220 }
221
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800222 public DeviceProfile copy(Context context) {
223 Point size = new Point(availableWidthPx, availableHeightPx);
Sunny Goyald70e75a2018-02-22 10:07:32 -0800224 return new DeviceProfile(context, inv, size, size, widthPx, heightPx, isLandscape,
225 isMultiWindowMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800226 }
227
Winson Chung2fda6ce2018-01-31 14:09:49 -0800228 public DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
Jon Mirandaa11380d2017-08-24 11:30:03 -0700229 // We take the minimum sizes of this profile and it's multi-window variant to ensure that
230 // the system decor is always excluded.
231 mwSize.set(Math.min(availableWidthPx, mwSize.x), Math.min(availableHeightPx, mwSize.y));
232
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700233 // In multi-window mode, we can have widthPx = availableWidthPx
234 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
235 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800236 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800237 isLandscape, true);
Jon Miranda93e1f042016-11-11 14:13:04 -0800238
Jon Miranda0cd04572017-09-19 11:31:42 -0700239 // If there isn't enough vertical cell padding with the labels displayed, hide the labels.
240 float workspaceCellPaddingY = profile.getCellSize().y - profile.iconSizePx
241 - iconDrawablePaddingPx - profile.iconTextSizePx;
242 if (workspaceCellPaddingY < profile.iconDrawablePaddingPx * 2) {
243 profile.adjustToHideWorkspaceLabels();
244 }
Jon Miranda93e1f042016-11-11 14:13:04 -0800245
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800246 // We use these scales to measure and layout the widgets using their full invariant profile
247 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
248 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
249 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
250 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
Sunny Goyal07b69292018-01-08 14:19:34 -0800251 profile.updateWorkspacePadding();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800252
Jon Miranda93e1f042016-11-11 14:13:04 -0800253 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700254 }
255
Adam Cohen63f1ec02014-08-12 09:23:13 -0700256 /**
Sunny Goyal2e2e2b42018-02-27 16:52:55 -0800257 * Inverse of {@link #getMultiWindowProfile(Context, Point)}
258 * @return device profile corresponding to the current orientation in non multi-window mode.
259 */
260 public DeviceProfile getFullScreenProfile() {
261 return isLandscape ? inv.landscapeProfile : inv.portraitProfile;
262 }
263
264 /**
Jon Miranda1091e532017-07-21 13:31:50 -0700265 * Adjusts the profile so that the labels on the Workspace are hidden.
266 * It is important to call this method after the All Apps variables have been set.
267 */
268 private void adjustToHideWorkspaceLabels() {
269 iconTextSizePx = 0;
270 iconDrawablePaddingPx = 0;
271 cellHeightPx = iconSizePx;
272
273 // In normal cases, All Apps cell height should equal the Workspace cell height.
274 // Since we are removing labels from the Workspace, we need to manually compute the
275 // All Apps cell height.
Jon Miranda3f9bab22017-07-28 14:50:17 -0700276 int topBottomPadding = allAppsIconDrawablePaddingPx * (isVerticalBarLayout() ? 2 : 1);
Jon Miranda1091e532017-07-21 13:31:50 -0700277 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
278 + Utilities.calculateTextHeight(allAppsIconTextSizePx)
Jon Miranda3f9bab22017-07-28 14:50:17 -0700279 + topBottomPadding * 2;
Jon Miranda1091e532017-07-21 13:31:50 -0700280 }
281
Sunny Goyalc6205602015-05-21 20:46:33 -0700282 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Miranda09660722017-06-14 14:20:14 -0700283 updateIconSize(1f, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800284
Jon Mirandabf7d8122016-11-03 15:29:29 -0700285 // Check to see if the icons fit within the available height. If not, then scale down.
286 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700287 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700288 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700289 float scale = maxHeight / usedHeight;
Jon Miranda09660722017-06-14 14:20:14 -0700290 updateIconSize(scale, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700291 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700292 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700293 }
294
Jon Miranda09660722017-06-14 14:20:14 -0700295 private void updateIconSize(float scale, Resources res, DisplayMetrics dm) {
Jon Miranda18751b62017-07-31 17:25:27 -0700296 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800297 final boolean isVerticalLayout = isVerticalBarLayout();
298 float invIconSizePx = isVerticalLayout ? inv.landscapeIconSize : inv.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700299 iconSizePx = (int) (Utilities.pxFromDp(invIconSizePx, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800300 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Jon Miranda09660722017-06-14 14:20:14 -0700301 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700302
Jon Miranda18751b62017-07-31 17:25:27 -0700303 cellHeightPx = iconSizePx + iconDrawablePaddingPx
304 + Utilities.calculateTextHeight(iconTextSizePx);
Jon Miranda846455e2017-10-02 14:58:52 -0700305 int cellYPadding = (getCellSize().y - cellHeightPx) / 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800306 if (iconDrawablePaddingPx > cellYPadding && !isVerticalLayout
Sunny Goyald70e75a2018-02-22 10:07:32 -0800307 && !isMultiWindowMode) {
Jon Miranda846455e2017-10-02 14:58:52 -0700308 // Ensures that the label is closer to its corresponding icon. This is not an issue
309 // with vertical bar layout or multi-window mode since the issue is handled separately
310 // with their calls to {@link #adjustToHideWorkspaceLabels}.
311 cellHeightPx -= (iconDrawablePaddingPx - cellYPadding);
312 iconDrawablePaddingPx = cellYPadding;
313 }
314 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda18751b62017-07-31 17:25:27 -0700315
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700316 // All apps
317 allAppsIconTextSizePx = iconTextSizePx;
Winson1f064272016-07-18 17:18:02 -0700318 allAppsIconSizePx = iconSizePx;
319 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700320 allAppsCellHeightPx = getCellSize().y;
321
Sunny Goyal07b69292018-01-08 14:19:34 -0800322 if (isVerticalLayout) {
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700323 // Always hide the Workspace text with vertical bar layout.
Jon Miranda1091e532017-07-21 13:31:50 -0700324 adjustToHideWorkspaceLabels();
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700325 }
Winson Chungb3800242013-10-24 11:01:54 -0700326
Winson Chungb3800242013-10-24 11:01:54 -0700327 // Hotseat
Sunny Goyal07b69292018-01-08 14:19:34 -0800328 if (isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700329 hotseatBarSizePx = iconSizePx;
330 }
Jon Miranda5044c352017-08-09 11:37:59 -0700331 hotseatCellHeightPx = iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700332
Sunny Goyal07b69292018-01-08 14:19:34 -0800333 if (!isVerticalLayout) {
Jon Miranda18751b62017-07-31 17:25:27 -0700334 int expectedWorkspaceHeight = availableHeightPx - hotseatBarSizePx
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700335 - pageIndicatorSizePx - topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700336 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
337 workspaceSpringLoadShrinkFactor = Math.min(
338 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
339 1 - (minRequiredHeight / expectedWorkspaceHeight));
340 } else {
341 workspaceSpringLoadShrinkFactor =
342 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
343 }
344
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700345 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700346 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
347 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700348 }
349
Jon Mirandabf7d8122016-11-03 15:29:29 -0700350 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
351 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
352 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
353 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
354
Jon Mirandac1b23992016-12-13 08:57:36 -0800355 updateFolderCellSize(1f, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700356
Jon Mirandac1b23992016-12-13 08:57:36 -0800357 // Don't let the folder get too close to the edges of the screen.
Jon Miranda72b5fd12017-06-25 18:06:23 -0700358 int folderMargin = edgeMarginPx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800359 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800360
361 // Check if the icons fit within the available height.
362 float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize;
Sunny Goyal07b69292018-01-08 14:19:34 -0800363 int maxHeight = availableHeightPx - totalWorkspacePadding.y - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800364 float scaleY = maxHeight / usedHeight;
365
366 // Check if the icons fit within the available width.
367 float usedWidth = folderCellWidthPx * inv.numFolderColumns;
Sunny Goyal07b69292018-01-08 14:19:34 -0800368 int maxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin;
Jon Mirandac1b23992016-12-13 08:57:36 -0800369 float scaleX = maxWidth / usedWidth;
370
371 float scale = Math.min(scaleX, scaleY);
372 if (scale < 1f) {
373 updateFolderCellSize(scale, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700374 }
375 }
376
Jon Mirandac1b23992016-12-13 08:57:36 -0800377 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700378 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
379 folderChildTextSizePx =
380 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
381
382 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
383 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
384 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
385
Jon Mirandac1b23992016-12-13 08:57:36 -0800386 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000387 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
388 folderChildDrawablePaddingPx = Math.max(0,
389 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700390 }
391
Winson1f064272016-07-18 17:18:02 -0700392 public void updateInsets(Rect insets) {
393 mInsets.set(insets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800394 updateWorkspacePadding();
Winson1f064272016-07-18 17:18:02 -0700395 }
396
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800397 public Rect getInsets() {
398 return mInsets;
399 }
400
Sunny Goyal756cd262015-08-20 12:33:21 -0700401 public Point getCellSize() {
402 Point result = new Point();
403 // Since we are only concerned with the overall padding, layout direction does
404 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700405 Point padding = getTotalWorkspacePadding();
Jon Miranda18751b62017-07-31 17:25:27 -0700406 result.x = calculateCellWidth(availableWidthPx - padding.x
407 - cellLayoutPaddingLeftRightPx * 2, inv.numColumns);
408 result.y = calculateCellHeight(availableHeightPx - padding.y
409 - cellLayoutBottomPaddingPx, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700410 return result;
411 }
412
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700413 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -0800414 updateWorkspacePadding();
415 return new Point(workspacePadding.left + workspacePadding.right,
416 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700417 }
418
419 /**
Sunny Goyal07b69292018-01-08 14:19:34 -0800420 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
421 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700422 */
Sunny Goyal07b69292018-01-08 14:19:34 -0800423 private void updateWorkspacePadding() {
424 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700425 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800426 padding.top = 0;
427 padding.bottom = edgeMarginPx;
428 padding.left = hotseatBarSidePaddingPx;
429 padding.right = hotseatBarSidePaddingPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800430 if (isSeascape()) {
Sunny Goyal228153d2018-01-04 15:35:22 -0800431 padding.left += hotseatBarSizePx;
432 padding.right += pageIndicatorSizePx;
Winson1f064272016-07-18 17:18:02 -0700433 } else {
Sunny Goyal228153d2018-01-04 15:35:22 -0800434 padding.left += pageIndicatorSizePx;
435 padding.right += hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -0700436 }
Winson Chungb3800242013-10-24 11:01:54 -0700437 } else {
Jon Miranda18751b62017-07-31 17:25:27 -0700438 int paddingBottom = hotseatBarSizePx + pageIndicatorSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700439 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700440 // Pad the left and right of the workspace to ensure consistent spacing
441 // between all icons
Tony Wickhamd6b40372015-09-23 18:37:57 -0700442 // The amount of screen space available for left/right padding.
Sunny Goyal228153d2018-01-04 15:35:22 -0800443 int availablePaddingX = Math.max(0, widthPx - ((inv.numColumns * cellWidthPx) +
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800444 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700445 availablePaddingX = (int) Math.min(availablePaddingX,
Sunny Goyal07b69292018-01-08 14:19:34 -0800446 widthPx * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal228153d2018-01-04 15:35:22 -0800447 int availablePaddingY = Math.max(0, heightPx - topWorkspacePadding - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800448 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
449 - hotseatBarBottomPaddingPx);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700450 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700451 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700452 } else {
453 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700454 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700455 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700456 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700457 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700458 }
459 }
Winson Chungb3800242013-10-24 11:01:54 -0700460 }
461
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700462 public Rect getHotseatLayoutPadding() {
463 if (isVerticalBarLayout()) {
464 if (isSeascape()) {
465 mHotseatPadding.set(
466 mInsets.left, mInsets.top, hotseatBarSidePaddingPx, mInsets.bottom);
467 } else {
468 mHotseatPadding.set(
469 hotseatBarSidePaddingPx, mInsets.top, mInsets.right, mInsets.bottom);
470 }
471 } else {
472
473 // We want the edges of the hotseat to line up with the edges of the workspace, but the
474 // icons in the hotseat are a different size, and so don't line up perfectly. To account
475 // for this, we pad the left and right of the hotseat with half of the difference of a
476 // workspace cell vs a hotseat cell.
477 float workspaceCellWidth = (float) widthPx / inv.numColumns;
478 float hotseatCellWidth = (float) widthPx / inv.numHotseatIcons;
479 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
480 mHotseatPadding.set(
481 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingLeftRightPx,
482 hotseatBarTopPaddingPx,
483 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx,
484 hotseatBarBottomPaddingPx + mInsets.bottom + cellLayoutBottomPaddingPx);
485 }
486 return mHotseatPadding;
487 }
488
Winsonfadbe8f2016-07-22 16:35:37 -0700489 /**
490 * @return the bounds for which the open folders should be contained within
491 */
492 public Rect getAbsoluteOpenFolderBounds() {
493 if (isVerticalBarLayout()) {
494 // Folders should only appear right of the drop target bar and left of the hotseat
495 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
496 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -0700497 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700498 mInsets.top + availableHeightPx);
499 } else {
500 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800501 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700502 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -0800503 mInsets.left + availableWidthPx - edgeMarginPx,
Jon Miranda18751b62017-07-31 17:25:27 -0700504 mInsets.top + availableHeightPx - hotseatBarSizePx
Jon Miranda2ed276e2017-06-29 11:36:34 -0700505 - pageIndicatorSizePx - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -0700506 }
507 }
508
Adam Cohen2e6da152015-05-06 11:42:25 -0700509 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700510 return width / countX;
511 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700512 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700513 return height / countY;
514 }
515
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700516 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700517 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
518 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
519 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700520 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700521 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700522 return isLandscape && transposeLayoutWithOrientation;
523 }
524
Sunny Goyal59d086c2018-05-07 17:31:40 -0700525 /**
526 * Updates orientation information and returns true if it has changed from the previous value.
527 */
528 public boolean updateIsSeascape(WindowManager wm) {
529 if (isVerticalBarLayout()) {
530 boolean isSeascape = wm.getDefaultDisplay().getRotation() == Surface.ROTATION_270;
531 if (mIsSeascape != isSeascape) {
532 mIsSeascape = isSeascape;
533 return true;
534 }
535 }
536 return false;
537 }
538
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800539 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700540 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800541 }
542
Sunny Goyal07b69292018-01-08 14:19:34 -0800543 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700544 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700545 }
546
Sunny Goyalc13403c2016-11-18 23:44:48 -0800547 public int getCellHeight(@ContainerType int containerType) {
548 switch (containerType) {
549 case CellLayout.WORKSPACE:
550 return cellHeightPx;
551 case CellLayout.FOLDER:
552 return folderCellHeightPx;
553 case CellLayout.HOTSEAT:
554 return hotseatCellHeightPx;
555 default:
556 // ??
557 return 0;
558 }
559 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700560
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700561 private static Context getContext(Context c, int orientation) {
562 Configuration context = new Configuration(c.getResources().getConfiguration());
563 context.orientation = orientation;
564 return c.createConfigurationContext(context);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700565 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800566
567 /**
568 * Callback when a component changes the DeviceProfile associated with it, as a result of
569 * configuration change
570 */
571 public interface OnDeviceProfileChangeListener {
572
573 /**
574 * Called when the device profile is reassigned. Note that for layout and measurements, it
575 * is sufficient to listen for inset changes. Use this callback when you need to perform
576 * a one time operation.
577 */
578 void onDeviceProfileChanged(DeviceProfile dp);
579 }
Winson Chungb3800242013-10-24 11:01:54 -0700580}