blob: 5bfa716eef455d566e682283fee861bbcbd18879 [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;
Winson Chungb3800242013-10-24 11:01:54 -070022import android.content.res.Resources;
23import android.graphics.Paint;
24import android.graphics.Paint.FontMetrics;
25import android.graphics.Point;
Winson Chungb3800242013-10-24 11:01:54 -070026import android.graphics.Rect;
27import android.util.DisplayMetrics;
Winson Chungb3800242013-10-24 11:01:54 -070028import android.view.Gravity;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080030import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.view.ViewGroup.LayoutParams;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -070032import android.view.ViewGroup.MarginLayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070033import android.widget.FrameLayout;
Adam Cohen24ce0b32014-01-14 16:18:14 -080034import android.widget.LinearLayout;
Winson Chungb3800242013-10-24 11:01:54 -070035
Tony Wickhame0c33232016-02-08 11:37:04 -080036import com.android.launcher3.config.FeatureFlags;
37
Winson Chungb3800242013-10-24 11:01:54 -070038public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070039
Adam Cohen2e6da152015-05-06 11:42:25 -070040 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070041
Sunny Goyalc6205602015-05-21 20:46:33 -070042 // Device properties
43 public final boolean isTablet;
44 public final boolean isLargeTablet;
45 public final boolean isPhone;
46 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070047
Sunny Goyalc6205602015-05-21 20:46:33 -070048 // Device properties in current orientation
49 public final boolean isLandscape;
50 public final int widthPx;
51 public final int heightPx;
52 public final int availableWidthPx;
53 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070054 /**
55 * The maximum amount of left/right workspace padding as a percentage of the screen width.
56 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
57 * 7% of the screen width can be used as right padding.
58 */
59 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070060
Sunny Goyalc6205602015-05-21 20:46:33 -070061 // Overview mode
62 private final int overviewModeMinIconZoneHeightPx;
63 private final int overviewModeMaxIconZoneHeightPx;
64 private final int overviewModeBarItemWidthPx;
65 private final int overviewModeBarSpacerWidthPx;
66 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070067
Sunny Goyalc6205602015-05-21 20:46:33 -070068 // Workspace
69 private int desiredWorkspaceLeftRightMarginPx;
70 public final int edgeMarginPx;
71 public final Rect defaultWidgetPadding;
72 private final int pageIndicatorHeightPx;
73 private final int defaultPageSpacingPx;
74 private float dragViewScale;
75
76 // 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;
84
Sunny Goyalc6205602015-05-21 20:46:33 -070085 // Folder
86 public int folderBackgroundOffset;
87 public int folderIconSizePx;
Adam Cohenefca0272016-02-24 19:19:06 -080088 public int folderIconPreviewPadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070089 public int folderCellWidthPx;
90 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070091
Sunny Goyalc6205602015-05-21 20:46:33 -070092 // Hotseat
93 public int hotseatCellWidthPx;
94 public int hotseatCellHeightPx;
95 public int hotseatIconSizePx;
Tony Wickhama3c74d12015-10-23 11:43:47 -070096 private int normalHotseatBarHeightPx, shortHotseatBarHeightPx;
Tony Wickham55616cd2015-09-23 14:55:17 -070097 private int hotseatBarHeightPx; // One of the above.
Adam Cohen2e6da152015-05-06 11:42:25 -070098
Sunny Goyalc6205602015-05-21 20:46:33 -070099 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700100 public int allAppsNumCols;
101 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -0700102 public int allAppsButtonVisualSize;
103 public final int allAppsIconSizePx;
Sunny Goyal383e0a22015-12-10 12:28:21 -0800104 public final float allAppsIconTextSizeSp;
Winson Chung59a488a2013-12-10 12:32:14 -0800105
Sunny Goyalc6205602015-05-21 20:46:33 -0700106 // QSB
Tony Wickhama3c74d12015-10-23 11:43:47 -0700107 private int searchBarWidgetInternalPaddingTop, searchBarWidgetInternalPaddingBottom;
108 private int searchBarTopPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800109 private int tallSearchBarNegativeTopPaddingPx, normalSearchBarTopExtraPaddingPx;
110 private int searchBarTopExtraPaddingPx; // One of the above.
Tony Wickhama3c74d12015-10-23 11:43:47 -0700111 private int normalSearchBarBottomPaddingPx, tallSearchBarBottomPaddingPx;
112 private int searchBarBottomPaddingPx; // One of the above.
113 private int normalSearchBarSpaceHeightPx, tallSearchBarSpaceHeightPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700114 private int searchBarSpaceHeightPx; // One of the above.
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700115
Sunny Goyalc6205602015-05-21 20:46:33 -0700116 public DeviceProfile(Context context, InvariantDeviceProfile inv,
117 Point minSize, Point maxSize,
118 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700119
Adam Cohen2e6da152015-05-06 11:42:25 -0700120 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700121 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700122
Adam Cohen2e6da152015-05-06 11:42:25 -0700123 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700124 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700125
Sunny Goyalc6205602015-05-21 20:46:33 -0700126 // Constants from resources
127 isTablet = res.getBoolean(R.bool.is_tablet);
128 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
129 isPhone = !isTablet && !isLargeTablet;
130
131 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700132 transposeLayoutWithOrientation =
133 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700134
135 ComponentName cn = new ComponentName(context.getPackageName(),
136 this.getClass().getName());
137 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
138 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
139 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
140 pageIndicatorHeightPx =
141 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
142 defaultPageSpacingPx =
143 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700144 overviewModeMinIconZoneHeightPx =
145 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
146 overviewModeMaxIconZoneHeightPx =
147 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800148 overviewModeBarItemWidthPx =
149 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
150 overviewModeBarSpacerWidthPx =
151 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700152 overviewModeIconZoneRatio =
153 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700154 iconDrawablePaddingOriginalPx =
155 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700156
Adam Cohen2e6da152015-05-06 11:42:25 -0700157 // AllApps uses the original non-scaled icon text size
Sunny Goyal383e0a22015-12-10 12:28:21 -0800158 allAppsIconTextSizeSp = inv.iconTextSize;
Adam Cohen2e6da152015-05-06 11:42:25 -0700159
160 // AllApps uses the original non-scaled icon size
161 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700162
Sunny Goyalc6205602015-05-21 20:46:33 -0700163 // Determine sizes.
164 widthPx = width;
165 heightPx = height;
166 if (isLandscape) {
167 availableWidthPx = maxSize.x;
168 availableHeightPx = minSize.y;
169 } else {
170 availableWidthPx = minSize.x;
171 availableHeightPx = maxSize.y;
172 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700173
Winson Chungb3800242013-10-24 11:01:54 -0700174 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700175 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700176 computeAllAppsButtonSize(context);
177 }
178
179 /**
180 * Determine the exact visual footprint of the all apps button, taking into account scaling
181 * and internal padding of the drawable.
182 */
183 private void computeAllAppsButtonSize(Context context) {
184 Resources res = context.getResources();
185 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700186 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
187 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700188 }
189
Sunny Goyalc6205602015-05-21 20:46:33 -0700190 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700191 // Check to see if the icons fit in the new available height. If not, then we need to
192 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700193 float scale = 1f;
194 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700195 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700196 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800197
Sunny Goyalc6205602015-05-21 20:46:33 -0700198 // We only care about the top and bottom workspace padding, which is not affected by RTL.
199 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700200 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
201 if (usedHeight > maxHeight) {
202 scale = maxHeight / usedHeight;
203 drawablePadding = 0;
204 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700205 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700206 }
207
Winson Chung93f98ea2015-03-10 16:28:47 -0700208 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700209 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700210 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800211 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700212 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700213 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700214
215 // Search Bar
Tony Wickhama3c74d12015-10-23 11:43:47 -0700216 normalSearchBarSpaceHeightPx = res.getDimensionPixelSize(
217 R.dimen.dynamic_grid_search_bar_height);
218 tallSearchBarSpaceHeightPx = res.getDimensionPixelSize(
219 R.dimen.dynamic_grid_search_bar_height_tall);
220 searchBarWidgetInternalPaddingTop = res.getDimensionPixelSize(
221 R.dimen.qsb_internal_padding_top);
222 searchBarWidgetInternalPaddingBottom = res.getDimensionPixelSize(
223 R.dimen.qsb_internal_padding_bottom);
Tony Wickham8a758062016-02-01 14:15:20 -0800224 normalSearchBarTopExtraPaddingPx = res.getDimensionPixelSize(
225 R.dimen.dynamic_grid_search_bar_extra_top_padding);
226 tallSearchBarNegativeTopPaddingPx = res.getDimensionPixelSize(
227 R.dimen.dynamic_grid_search_bar_negative_top_padding_short);
Tony Wickhama3c74d12015-10-23 11:43:47 -0700228 if (isTablet && !isVerticalBarLayout()) {
229 searchBarTopPaddingPx = searchBarWidgetInternalPaddingTop;
230 normalSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom +
231 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_bottom_padding_tablet);
232 tallSearchBarBottomPaddingPx = normalSearchBarBottomPaddingPx;
233 } else {
234 searchBarTopPaddingPx = searchBarWidgetInternalPaddingTop;
235 normalSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom +
236 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_bottom_padding);
Tony Wickham8a758062016-02-01 14:15:20 -0800237 tallSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom
238 + res.getDimensionPixelSize(
239 R.dimen.dynamic_grid_search_bar_bottom_negative_padding_short);
Tony Wickhama3c74d12015-10-23 11:43:47 -0700240 }
Winson Chungb3800242013-10-24 11:01:54 -0700241
242 // Calculate the actual text height
243 Paint textPaint = new Paint();
244 textPaint.setTextSize(iconTextSizePx);
245 FontMetrics fm = textPaint.getFontMetrics();
246 cellWidthPx = iconSizePx;
247 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Tony Wickhame0c33232016-02-08 11:37:04 -0800248 final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
249 : res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800250 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700251
252 // Hotseat
Tony Wickhama3c74d12015-10-23 11:43:47 -0700253 normalHotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
254 shortHotseatBarHeightPx = iconSizePx + 2 * edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700255 hotseatCellWidthPx = iconSizePx;
256 hotseatCellHeightPx = iconSizePx;
257
258 // Folder
Tony Wickham6159c242015-11-13 18:07:18 +0000259 int folderCellPadding = isTablet || isLandscape ? 6 * edgeMarginPx : 3 * edgeMarginPx;
260 // Don't let the folder get too close to the edges of the screen.
261 folderCellWidthPx = Math.min(cellWidthPx + folderCellPadding,
Tony Wickhama2950482015-10-12 15:57:23 -0700262 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Winson Chungb3800242013-10-24 11:01:54 -0700263 folderCellHeightPx = cellHeightPx + edgeMarginPx;
264 folderBackgroundOffset = -edgeMarginPx;
265 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800266 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700267 }
268
Winson Chungef7f8742015-06-04 17:18:17 -0700269 /**
270 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
271 */
272 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700273 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700274 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700275 int allAppsCellWidthGap =
276 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700277 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Sunny Goyal94f46d92016-02-16 14:56:33 -0800278 int numAppsCols = (availableAppsWidthPx + allAppsCellWidthGap - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700279 (allAppsIconSizePx + allAppsCellWidthGap);
280 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700281 allAppsNumCols = numAppsCols;
282 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700283 }
284
Tony Wickhama3c74d12015-10-23 11:43:47 -0700285 /** Returns the amount of extra space to allocate to the search bar for vertical padding. */
286 private int getSearchBarTotalVerticalPadding() {
Tony Wickham8a758062016-02-01 14:15:20 -0800287 return searchBarTopPaddingPx + searchBarTopExtraPaddingPx + searchBarBottomPaddingPx;
Tony Wickhama3c74d12015-10-23 11:43:47 -0700288 }
289
290 /** Returns the width and height of the search bar, ignoring any padding. */
291 public Point getSearchBarDimensForWidgetOpts(Resources res) {
292 Rect searchBarBounds = getSearchBarBounds(Utilities.isRtl(res));
293 if (isVerticalBarLayout()) {
294 return new Point(searchBarBounds.width(), searchBarBounds.height());
Winson Chung69e04ea2013-12-02 14:43:44 -0800295 }
Tony Wickhama3c74d12015-10-23 11:43:47 -0700296 int widgetInternalPadding = searchBarWidgetInternalPaddingTop +
297 searchBarWidgetInternalPaddingBottom;
298 return new Point(searchBarBounds.width(), searchBarSpaceHeightPx + widgetInternalPadding);
Winson Chung69e04ea2013-12-02 14:43:44 -0800299 }
300
Winson Chungb3800242013-10-24 11:01:54 -0700301 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700302 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700303 Rect bounds = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700304 if (isVerticalBarLayout()) {
Winson Chung42b3c062013-12-04 12:09:59 -0800305 if (isLayoutRtl) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700306 bounds.set(availableWidthPx - normalSearchBarSpaceHeightPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800307 availableWidthPx, availableHeightPx - edgeMarginPx);
308 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700309 bounds.set(0, edgeMarginPx, normalSearchBarSpaceHeightPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800310 availableHeightPx - edgeMarginPx);
311 }
Winson Chungb3800242013-10-24 11:01:54 -0700312 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700313 int boundsBottom = searchBarSpaceHeightPx + getSearchBarTotalVerticalPadding();
Sunny Goyalc6205602015-05-21 20:46:33 -0700314 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700315 // Pad the left and right of the workspace to ensure consistent spacing
316 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700317 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700318 // XXX: If the icon size changes across orientations, we will have to take
319 // that into account here too.
320 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700321 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Tony Wickhama3c74d12015-10-23 11:43:47 -0700322 bounds.set(edgeMarginPx + gap, 0,
323 availableWidthPx - (edgeMarginPx + gap), boundsBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700324 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800325 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700326 0,
Winson Chungb3800242013-10-24 11:01:54 -0700327 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
Tony Wickhama3c74d12015-10-23 11:43:47 -0700328 defaultWidgetPadding.right), boundsBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700329 }
330 }
331 return bounds;
332 }
333
Sunny Goyal756cd262015-08-20 12:33:21 -0700334 public Point getCellSize() {
335 Point result = new Point();
336 // Since we are only concerned with the overall padding, layout direction does
337 // not matter.
338 Rect padding = getWorkspacePadding(false /* isLayoutRtl */ );
339 result.x = calculateCellWidth(availableWidthPx - padding.left - padding.right,
340 inv.numColumns);
341 result.y = calculateCellHeight(availableHeightPx - padding.top - padding.bottom,
342 inv.numRows);
343 return result;
344 }
345
Winson Chungb3800242013-10-24 11:01:54 -0700346 /** Returns the workspace padding in the specified orientation */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800347 public Rect getWorkspacePadding(boolean isLayoutRtl) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700348 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700349 Rect padding = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700350 if (isVerticalBarLayout()) {
Winson Chungb3800242013-10-24 11:01:54 -0700351 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800352 if (isLayoutRtl) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700353 padding.set(normalHotseatBarHeightPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800354 searchBarBounds.width(), edgeMarginPx);
355 } else {
356 padding.set(searchBarBounds.width(), edgeMarginPx,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700357 normalHotseatBarHeightPx, edgeMarginPx);
Winson Chung42b3c062013-12-04 12:09:59 -0800358 }
Winson Chungb3800242013-10-24 11:01:54 -0700359 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700360 int paddingTop = searchBarBounds.bottom;
361 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700362 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700363 // Pad the left and right of the workspace to ensure consistent spacing
364 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800365 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700366 int width = getCurrentWidth();
367 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700368 // The amount of screen space available for left/right padding.
369 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
370 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
371 availablePaddingX = (int) Math.min(availablePaddingX,
372 width * MAX_HORIZONTAL_PADDING_PERCENT);
373 int availablePaddingY = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700374 - (int) (2 * inv.numRows * cellHeightPx));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700375 padding.set(availablePaddingX / 2, paddingTop + availablePaddingY / 2,
376 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700377 } else {
378 // Pad the top and bottom of the workspace with search/hotseat bar sizes
379 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700380 paddingTop,
Winson Chungb3800242013-10-24 11:01:54 -0700381 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700382 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700383 }
384 }
385 return padding;
386 }
387
Sunny Goyalc6205602015-05-21 20:46:33 -0700388 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700389 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700390 // In landscape mode the page spacing is set to the default.
391 return defaultPageSpacingPx;
392 } else {
393 // In portrait, we want the pages spaced such that there is no
394 // overhang of the previous / next page into the current page viewport.
395 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700396 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700397 }
398 }
399
Winson Chung4f7eb402015-07-10 18:00:48 -0700400 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700401 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
402 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
403 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700404 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700405 }
406
407 // The rect returned will be extended to below the system ui that covers the workspace
Sunny Goyalcf25b522015-07-09 00:01:18 -0700408 public boolean isInHotseatRect(int x, int y) {
Winson Chungb3800242013-10-24 11:01:54 -0700409 if (isVerticalBarLayout()) {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700410 return (x >= (availableWidthPx - hotseatBarHeightPx))
411 && (y >= 0) && (y <= availableHeightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700412 } else {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700413 return (x >= 0) && (x <= availableWidthPx)
414 && (y >= (availableHeightPx - hotseatBarHeightPx));
Winson Chungb3800242013-10-24 11:01:54 -0700415 }
416 }
417
Adam Cohen2e6da152015-05-06 11:42:25 -0700418 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700419 return width / countX;
420 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700421 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700422 return height / countY;
423 }
424
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700425 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700426 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
427 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
428 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700429 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700430 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700431 return isLandscape && transposeLayoutWithOrientation;
432 }
433
434 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700435 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700436 }
437
Sunny Goyalc6205602015-05-21 20:46:33 -0700438 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800439 int visibleChildren = 0;
440 for (int i = 0; i < parent.getChildCount(); i++) {
441 if (parent.getChildAt(i).getVisibility() != View.GONE) {
442 visibleChildren++;
443 }
444 }
445 return visibleChildren;
446 }
447
Tony Wickhameef44322015-10-20 13:24:36 -0700448 // TODO(twickham): b/25154513
449 public void setSearchBarHeight(int searchBarHeight) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700450 if (searchBarHeight == LauncherCallbacks.SEARCH_BAR_HEIGHT_TALL) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700451 hotseatBarHeightPx = shortHotseatBarHeightPx;
452 searchBarSpaceHeightPx = tallSearchBarSpaceHeightPx;
453 searchBarBottomPaddingPx = tallSearchBarBottomPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800454 searchBarTopExtraPaddingPx = isPhone ? tallSearchBarNegativeTopPaddingPx
455 : normalSearchBarTopExtraPaddingPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700456 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700457 hotseatBarHeightPx = normalHotseatBarHeightPx;
458 searchBarSpaceHeightPx = normalSearchBarSpaceHeightPx;
459 searchBarBottomPaddingPx = normalSearchBarBottomPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800460 searchBarTopExtraPaddingPx = normalSearchBarTopExtraPaddingPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700461 }
Tony Wickhameef44322015-10-20 13:24:36 -0700462 }
463
Jorim Jaggid017f882014-01-14 17:08:48 -0800464 public void layout(Launcher launcher) {
465 FrameLayout.LayoutParams lp;
466 boolean hasVerticalBarLayout = isVerticalBarLayout();
467 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
468
469 // Layout the search bar space
Tony Wickhama3c74d12015-10-23 11:43:47 -0700470 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chung006ee262015-08-03 14:40:11 -0700471 View searchBar = launcher.getSearchDropTargetBar();
Tony Wickham34d2c912015-09-11 09:27:58 -0700472 lp = getDropTargetBarLayoutParams(hasVerticalBarLayout, searchBar, Gravity.TOP);
Tony Wickhama3c74d12015-10-23 11:43:47 -0700473 lp.width = searchBarBounds.width();
474 lp.height = searchBarBounds.height();
Tony Wickham8a758062016-02-01 14:15:20 -0800475 lp.topMargin = searchBarTopExtraPaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700476 searchBar.setLayoutParams(lp);
477
Tony Wickham34d2c912015-09-11 09:27:58 -0700478 // Layout the app info bar space
479 View appInfoBar = launcher.getAppInfoDropTargetBar();
480 lp = getDropTargetBarLayoutParams(hasVerticalBarLayout, appInfoBar, Gravity.BOTTOM);
481 lp.bottomMargin = hotseatBarHeightPx;
482 appInfoBar.setLayoutParams(lp);
483
Winson Chungb3800242013-10-24 11:01:54 -0700484 // Layout the workspace
485 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
486 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
487 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700488 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700489 workspace.setLayoutParams(lp);
490 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700491 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700492
493 // Layout the hotseat
494 View hotseat = launcher.findViewById(R.id.hotseat);
495 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700496 // We want the edges of the hotseat to line up with the edges of the workspace, but the
497 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
498 // this, we pad the left and right of the hotseat with half of the difference of a workspace
499 // cell vs a hotseat cell.
500 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
501 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
502 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700503 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700504 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
505 // screen regardless of RTL
506 lp.gravity = Gravity.RIGHT;
Tony Wickhama3c74d12015-10-23 11:43:47 -0700507 lp.width = normalHotseatBarHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700508 lp.height = LayoutParams.MATCH_PARENT;
509 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700510 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800511 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700512 lp.gravity = Gravity.BOTTOM;
513 lp.width = LayoutParams.MATCH_PARENT;
514 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700515 hotseat.findViewById(R.id.layout).setPadding(
516 hotseatAdjustment + padding.left, 0,
517 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700518 } else {
519 // For phones, layout the hotseat without any bottom margin
520 // to ensure that we have space for the folders
521 lp.gravity = Gravity.BOTTOM;
522 lp.width = LayoutParams.MATCH_PARENT;
523 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700524 hotseat.findViewById(R.id.layout).setPadding(
525 hotseatAdjustment + padding.left, 0,
526 hotseatAdjustment + padding.right, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700527 }
528 hotseat.setLayoutParams(lp);
529
530 // Layout the page indicators
531 View pageIndicator = launcher.findViewById(R.id.page_indicator);
532 if (pageIndicator != null) {
533 if (hasVerticalBarLayout) {
534 // Hide the page indicators when we have vertical search/hotseat
535 pageIndicator.setVisibility(View.GONE);
536 } else {
537 // Put the page indicators above the hotseat
538 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
539 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
540 lp.width = LayoutParams.WRAP_CONTENT;
541 lp.height = LayoutParams.WRAP_CONTENT;
542 lp.bottomMargin = hotseatBarHeightPx;
543 pageIndicator.setLayoutParams(lp);
544 }
545 }
546
Winson Chungb3800242013-10-24 11:01:54 -0700547 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800548 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700549 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700550 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
551 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700552
553 int visibleChildCount = getVisibleChildCount(overviewMode);
554 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
555 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
556
557 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700558 lp.height = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700559 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700560
561 if (lp.width > totalItemWidth && visibleChildCount > 1) {
562 // We have enough space. Lets add some margin too.
563 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
564 View lastChild = null;
565
566 // Set margin of all visible children except the last visible child
567 for (int i = 0; i < visibleChildCount; i++) {
568 if (lastChild != null) {
569 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
570 if (isLayoutRtl) {
571 clp.leftMargin = margin;
572 } else {
573 clp.rightMargin = margin;
574 }
575 lastChild.setLayoutParams(clp);
576 lastChild = null;
577 }
578 View thisChild = overviewMode.getChildAt(i);
579 if (thisChild.getVisibility() != View.GONE) {
580 lastChild = thisChild;
581 }
582 }
583 }
Winson Chungb3800242013-10-24 11:01:54 -0700584 }
585 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700586
Tony Wickham34d2c912015-09-11 09:27:58 -0700587 private FrameLayout.LayoutParams getDropTargetBarLayoutParams(boolean hasVerticalBarLayout,
588 View dropTargetBar, int verticalGravity) {
589 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) dropTargetBar.getLayoutParams();
590 if (hasVerticalBarLayout) {
591 // Vertical drop target bar space -- The drop target bar is fixed in the layout to be on
592 // the left of the screen regardless of RTL
593 lp.gravity = Gravity.LEFT;
Tony Wickhamc1ff24c2015-10-30 10:37:43 -0700594 lp.width = normalSearchBarSpaceHeightPx;
Tony Wickham34d2c912015-09-11 09:27:58 -0700595
596 LinearLayout targets = (LinearLayout) dropTargetBar.findViewById(R.id.drag_target_bar);
597 targets.setOrientation(LinearLayout.VERTICAL);
598 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
599 targetsLp.gravity = verticalGravity;
600 targetsLp.height = LayoutParams.WRAP_CONTENT;
601 } else {
602 // Horizontal drop target bar space
Tony Wickham5ea422e2015-10-30 11:03:51 -0700603 lp.gravity = verticalGravity | Gravity.CENTER_HORIZONTAL;
Tony Wickham34d2c912015-09-11 09:27:58 -0700604 lp.height = searchBarSpaceHeightPx;
Tony Wickham34d2c912015-09-11 09:27:58 -0700605 }
606 return lp;
607 }
608
Winson Chung4f3bfad2015-07-13 11:14:30 -0700609 private int getCurrentWidth() {
610 return isLandscape
611 ? Math.max(widthPx, heightPx)
612 : Math.min(widthPx, heightPx);
613 }
614
615 private int getCurrentHeight() {
616 return isLandscape
617 ? Math.min(widthPx, heightPx)
618 : Math.max(widthPx, heightPx);
619 }
Winson Chungb3800242013-10-24 11:01:54 -0700620}