blob: 9ab56119ff6458d77836620820f3c32b540c141f [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;
34
Tony Wickhame0c33232016-02-08 11:37:04 -080035import com.android.launcher3.config.FeatureFlags;
36
Winson Chungb3800242013-10-24 11:01:54 -070037public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070038
Adam Cohen2e6da152015-05-06 11:42:25 -070039 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070040
Sunny Goyalc6205602015-05-21 20:46:33 -070041 // Device properties
42 public final boolean isTablet;
43 public final boolean isLargeTablet;
44 public final boolean isPhone;
45 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070046
Sunny Goyalc6205602015-05-21 20:46:33 -070047 // Device properties in current orientation
48 public final boolean isLandscape;
49 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
Sunny Goyalc6205602015-05-21 20:46:33 -070060 // Overview mode
61 private final int overviewModeMinIconZoneHeightPx;
62 private final int overviewModeMaxIconZoneHeightPx;
63 private final int overviewModeBarItemWidthPx;
64 private final int overviewModeBarSpacerWidthPx;
65 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070066
Sunny Goyalc6205602015-05-21 20:46:33 -070067 // Workspace
68 private int desiredWorkspaceLeftRightMarginPx;
69 public final int edgeMarginPx;
70 public final Rect defaultWidgetPadding;
71 private final int pageIndicatorHeightPx;
72 private final int defaultPageSpacingPx;
73 private float dragViewScale;
74
75 // Workspace icons
76 public int iconSizePx;
77 public int iconTextSizePx;
78 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070079 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070080
Adam Cohen59400422014-03-05 18:07:04 -080081 public int cellWidthPx;
82 public int cellHeightPx;
83
Sunny Goyalc6205602015-05-21 20:46:33 -070084 // Folder
85 public int folderBackgroundOffset;
86 public int folderIconSizePx;
Adam Cohenefca0272016-02-24 19:19:06 -080087 public int folderIconPreviewPadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070088 public int folderCellWidthPx;
89 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070090
Sunny Goyalc6205602015-05-21 20:46:33 -070091 // Hotseat
92 public int hotseatCellWidthPx;
93 public int hotseatCellHeightPx;
94 public int hotseatIconSizePx;
Tony Wickhama3c74d12015-10-23 11:43:47 -070095 private int normalHotseatBarHeightPx, shortHotseatBarHeightPx;
Tony Wickham55616cd2015-09-23 14:55:17 -070096 private int hotseatBarHeightPx; // One of the above.
Adam Cohen2e6da152015-05-06 11:42:25 -070097
Sunny Goyalc6205602015-05-21 20:46:33 -070098 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099 public int allAppsNumCols;
100 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -0700101 public int allAppsButtonVisualSize;
102 public final int allAppsIconSizePx;
Sunny Goyal383e0a22015-12-10 12:28:21 -0800103 public final float allAppsIconTextSizeSp;
Winson Chung59a488a2013-12-10 12:32:14 -0800104
Sunny Goyalc6205602015-05-21 20:46:33 -0700105 // QSB
Tony Wickhama3c74d12015-10-23 11:43:47 -0700106 private int searchBarWidgetInternalPaddingTop, searchBarWidgetInternalPaddingBottom;
107 private int searchBarTopPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800108 private int tallSearchBarNegativeTopPaddingPx, normalSearchBarTopExtraPaddingPx;
109 private int searchBarTopExtraPaddingPx; // One of the above.
Tony Wickhama3c74d12015-10-23 11:43:47 -0700110 private int normalSearchBarBottomPaddingPx, tallSearchBarBottomPaddingPx;
111 private int searchBarBottomPaddingPx; // One of the above.
112 private int normalSearchBarSpaceHeightPx, tallSearchBarSpaceHeightPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700113 private int searchBarSpaceHeightPx; // One of the above.
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700114
Sunny Goyalc6205602015-05-21 20:46:33 -0700115 public DeviceProfile(Context context, InvariantDeviceProfile inv,
116 Point minSize, Point maxSize,
117 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700118
Adam Cohen2e6da152015-05-06 11:42:25 -0700119 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700120 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700121
Adam Cohen2e6da152015-05-06 11:42:25 -0700122 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700123 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700124
Sunny Goyalc6205602015-05-21 20:46:33 -0700125 // Constants from resources
126 isTablet = res.getBoolean(R.bool.is_tablet);
127 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
128 isPhone = !isTablet && !isLargeTablet;
129
130 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700131 transposeLayoutWithOrientation =
132 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700133
134 ComponentName cn = new ComponentName(context.getPackageName(),
135 this.getClass().getName());
136 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
137 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
138 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
139 pageIndicatorHeightPx =
140 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
141 defaultPageSpacingPx =
142 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700143 overviewModeMinIconZoneHeightPx =
144 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
145 overviewModeMaxIconZoneHeightPx =
146 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800147 overviewModeBarItemWidthPx =
148 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
149 overviewModeBarSpacerWidthPx =
150 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700151 overviewModeIconZoneRatio =
152 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700153 iconDrawablePaddingOriginalPx =
154 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700155
Adam Cohen2e6da152015-05-06 11:42:25 -0700156 // AllApps uses the original non-scaled icon text size
Sunny Goyal383e0a22015-12-10 12:28:21 -0800157 allAppsIconTextSizeSp = inv.iconTextSize;
Adam Cohen2e6da152015-05-06 11:42:25 -0700158
159 // AllApps uses the original non-scaled icon size
160 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700161
Sunny Goyalc6205602015-05-21 20:46:33 -0700162 // Determine sizes.
163 widthPx = width;
164 heightPx = height;
165 if (isLandscape) {
166 availableWidthPx = maxSize.x;
167 availableHeightPx = minSize.y;
168 } else {
169 availableWidthPx = minSize.x;
170 availableHeightPx = maxSize.y;
171 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700172
Winson Chungb3800242013-10-24 11:01:54 -0700173 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700174 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700175 computeAllAppsButtonSize(context);
176 }
177
178 /**
179 * Determine the exact visual footprint of the all apps button, taking into account scaling
180 * and internal padding of the drawable.
181 */
182 private void computeAllAppsButtonSize(Context context) {
183 Resources res = context.getResources();
184 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700185 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
186 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700187 }
188
Sunny Goyalc6205602015-05-21 20:46:33 -0700189 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700190 // Check to see if the icons fit in the new available height. If not, then we need to
191 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700192 float scale = 1f;
193 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700194 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700195 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800196
Sunny Goyalc6205602015-05-21 20:46:33 -0700197 // We only care about the top and bottom workspace padding, which is not affected by RTL.
198 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700199 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
200 if (usedHeight > maxHeight) {
201 scale = maxHeight / usedHeight;
202 drawablePadding = 0;
203 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700204 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700205 }
206
Winson Chung93f98ea2015-03-10 16:28:47 -0700207 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700208 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700209 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800210 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700211 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700212 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700213
214 // Search Bar
Tony Wickhama3c74d12015-10-23 11:43:47 -0700215 normalSearchBarSpaceHeightPx = res.getDimensionPixelSize(
216 R.dimen.dynamic_grid_search_bar_height);
217 tallSearchBarSpaceHeightPx = res.getDimensionPixelSize(
218 R.dimen.dynamic_grid_search_bar_height_tall);
219 searchBarWidgetInternalPaddingTop = res.getDimensionPixelSize(
220 R.dimen.qsb_internal_padding_top);
221 searchBarWidgetInternalPaddingBottom = res.getDimensionPixelSize(
222 R.dimen.qsb_internal_padding_bottom);
Tony Wickham8a758062016-02-01 14:15:20 -0800223 normalSearchBarTopExtraPaddingPx = res.getDimensionPixelSize(
224 R.dimen.dynamic_grid_search_bar_extra_top_padding);
225 tallSearchBarNegativeTopPaddingPx = res.getDimensionPixelSize(
226 R.dimen.dynamic_grid_search_bar_negative_top_padding_short);
Tony Wickhama3c74d12015-10-23 11:43:47 -0700227 if (isTablet && !isVerticalBarLayout()) {
228 searchBarTopPaddingPx = searchBarWidgetInternalPaddingTop;
229 normalSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom +
230 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_bottom_padding_tablet);
231 tallSearchBarBottomPaddingPx = normalSearchBarBottomPaddingPx;
232 } else {
233 searchBarTopPaddingPx = searchBarWidgetInternalPaddingTop;
234 normalSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom +
235 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_bottom_padding);
Tony Wickham8a758062016-02-01 14:15:20 -0800236 tallSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom
237 + res.getDimensionPixelSize(
238 R.dimen.dynamic_grid_search_bar_bottom_negative_padding_short);
Tony Wickhama3c74d12015-10-23 11:43:47 -0700239 }
Winson Chungb3800242013-10-24 11:01:54 -0700240
241 // Calculate the actual text height
242 Paint textPaint = new Paint();
243 textPaint.setTextSize(iconTextSizePx);
244 FontMetrics fm = textPaint.getFontMetrics();
245 cellWidthPx = iconSizePx;
246 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Tony Wickhame0c33232016-02-08 11:37:04 -0800247 final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
248 : res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800249 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700250
251 // Hotseat
Tony Wickhama3c74d12015-10-23 11:43:47 -0700252 normalHotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
253 shortHotseatBarHeightPx = iconSizePx + 2 * edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700254 hotseatCellWidthPx = iconSizePx;
255 hotseatCellHeightPx = iconSizePx;
256
257 // Folder
Tony Wickham6159c242015-11-13 18:07:18 +0000258 int folderCellPadding = isTablet || isLandscape ? 6 * edgeMarginPx : 3 * edgeMarginPx;
259 // Don't let the folder get too close to the edges of the screen.
260 folderCellWidthPx = Math.min(cellWidthPx + folderCellPadding,
Tony Wickhama2950482015-10-12 15:57:23 -0700261 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Winson Chungb3800242013-10-24 11:01:54 -0700262 folderCellHeightPx = cellHeightPx + edgeMarginPx;
263 folderBackgroundOffset = -edgeMarginPx;
264 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800265 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700266 }
267
Winson Chungef7f8742015-06-04 17:18:17 -0700268 /**
269 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
270 */
271 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700272 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700273 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700274 int allAppsCellWidthGap =
275 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700276 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Sunny Goyal94f46d92016-02-16 14:56:33 -0800277 int numAppsCols = (availableAppsWidthPx + allAppsCellWidthGap - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700278 (allAppsIconSizePx + allAppsCellWidthGap);
279 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700280 allAppsNumCols = numAppsCols;
281 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700282 }
283
Tony Wickhama3c74d12015-10-23 11:43:47 -0700284 /** Returns the amount of extra space to allocate to the search bar for vertical padding. */
285 private int getSearchBarTotalVerticalPadding() {
Tony Wickham8a758062016-02-01 14:15:20 -0800286 return searchBarTopPaddingPx + searchBarTopExtraPaddingPx + searchBarBottomPaddingPx;
Tony Wickhama3c74d12015-10-23 11:43:47 -0700287 }
288
289 /** Returns the width and height of the search bar, ignoring any padding. */
290 public Point getSearchBarDimensForWidgetOpts(Resources res) {
291 Rect searchBarBounds = getSearchBarBounds(Utilities.isRtl(res));
292 if (isVerticalBarLayout()) {
293 return new Point(searchBarBounds.width(), searchBarBounds.height());
Winson Chung69e04ea2013-12-02 14:43:44 -0800294 }
Tony Wickhama3c74d12015-10-23 11:43:47 -0700295 int widgetInternalPadding = searchBarWidgetInternalPaddingTop +
296 searchBarWidgetInternalPaddingBottom;
297 return new Point(searchBarBounds.width(), searchBarSpaceHeightPx + widgetInternalPadding);
Winson Chung69e04ea2013-12-02 14:43:44 -0800298 }
299
Winson Chungb3800242013-10-24 11:01:54 -0700300 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700301 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700302 Rect bounds = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700303 if (isVerticalBarLayout()) {
Winson Chung42b3c062013-12-04 12:09:59 -0800304 if (isLayoutRtl) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700305 bounds.set(availableWidthPx - normalSearchBarSpaceHeightPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800306 availableWidthPx, availableHeightPx - edgeMarginPx);
307 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700308 bounds.set(0, edgeMarginPx, normalSearchBarSpaceHeightPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800309 availableHeightPx - edgeMarginPx);
310 }
Winson Chungb3800242013-10-24 11:01:54 -0700311 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700312 int boundsBottom = searchBarSpaceHeightPx + getSearchBarTotalVerticalPadding();
Sunny Goyalc6205602015-05-21 20:46:33 -0700313 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700314 // Pad the left and right of the workspace to ensure consistent spacing
315 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700316 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700317 // XXX: If the icon size changes across orientations, we will have to take
318 // that into account here too.
319 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700320 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Tony Wickhama3c74d12015-10-23 11:43:47 -0700321 bounds.set(edgeMarginPx + gap, 0,
322 availableWidthPx - (edgeMarginPx + gap), boundsBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700323 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800324 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700325 0,
Winson Chungb3800242013-10-24 11:01:54 -0700326 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
Tony Wickhama3c74d12015-10-23 11:43:47 -0700327 defaultWidgetPadding.right), boundsBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700328 }
329 }
330 return bounds;
331 }
332
Sunny Goyal756cd262015-08-20 12:33:21 -0700333 public Point getCellSize() {
334 Point result = new Point();
335 // Since we are only concerned with the overall padding, layout direction does
336 // not matter.
337 Rect padding = getWorkspacePadding(false /* isLayoutRtl */ );
338 result.x = calculateCellWidth(availableWidthPx - padding.left - padding.right,
339 inv.numColumns);
340 result.y = calculateCellHeight(availableHeightPx - padding.top - padding.bottom,
341 inv.numRows);
342 return result;
343 }
344
Winson Chungb3800242013-10-24 11:01:54 -0700345 /** Returns the workspace padding in the specified orientation */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800346 public Rect getWorkspacePadding(boolean isLayoutRtl) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700347 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700348 Rect padding = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700349 if (isVerticalBarLayout()) {
Winson Chungb3800242013-10-24 11:01:54 -0700350 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800351 if (isLayoutRtl) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700352 padding.set(normalHotseatBarHeightPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800353 searchBarBounds.width(), edgeMarginPx);
354 } else {
355 padding.set(searchBarBounds.width(), edgeMarginPx,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700356 normalHotseatBarHeightPx, edgeMarginPx);
Winson Chung42b3c062013-12-04 12:09:59 -0800357 }
Winson Chungb3800242013-10-24 11:01:54 -0700358 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700359 int paddingTop = searchBarBounds.bottom;
360 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700361 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700362 // Pad the left and right of the workspace to ensure consistent spacing
363 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800364 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700365 int width = getCurrentWidth();
366 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700367 // The amount of screen space available for left/right padding.
368 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
369 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
370 availablePaddingX = (int) Math.min(availablePaddingX,
371 width * MAX_HORIZONTAL_PADDING_PERCENT);
372 int availablePaddingY = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700373 - (int) (2 * inv.numRows * cellHeightPx));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700374 padding.set(availablePaddingX / 2, paddingTop + availablePaddingY / 2,
375 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700376 } else {
377 // Pad the top and bottom of the workspace with search/hotseat bar sizes
378 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700379 paddingTop,
Winson Chungb3800242013-10-24 11:01:54 -0700380 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700381 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700382 }
383 }
384 return padding;
385 }
386
Sunny Goyalc6205602015-05-21 20:46:33 -0700387 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700388 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700389 // In landscape mode the page spacing is set to the default.
390 return defaultPageSpacingPx;
391 } else {
392 // In portrait, we want the pages spaced such that there is no
393 // overhang of the previous / next page into the current page viewport.
394 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700395 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700396 }
397 }
398
Winson Chung4f7eb402015-07-10 18:00:48 -0700399 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700400 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
401 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
402 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700403 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700404 }
405
406 // The rect returned will be extended to below the system ui that covers the workspace
Sunny Goyalcf25b522015-07-09 00:01:18 -0700407 public boolean isInHotseatRect(int x, int y) {
Winson Chungb3800242013-10-24 11:01:54 -0700408 if (isVerticalBarLayout()) {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700409 return (x >= (availableWidthPx - hotseatBarHeightPx))
410 && (y >= 0) && (y <= availableHeightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700411 } else {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700412 return (x >= 0) && (x <= availableWidthPx)
413 && (y >= (availableHeightPx - hotseatBarHeightPx));
Winson Chungb3800242013-10-24 11:01:54 -0700414 }
415 }
416
Adam Cohen2e6da152015-05-06 11:42:25 -0700417 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700418 return width / countX;
419 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700420 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700421 return height / countY;
422 }
423
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700424 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700425 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
426 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
427 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700428 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700429 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700430 return isLandscape && transposeLayoutWithOrientation;
431 }
432
433 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700434 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700435 }
436
Sunny Goyalc6205602015-05-21 20:46:33 -0700437 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800438 int visibleChildren = 0;
439 for (int i = 0; i < parent.getChildCount(); i++) {
440 if (parent.getChildAt(i).getVisibility() != View.GONE) {
441 visibleChildren++;
442 }
443 }
444 return visibleChildren;
445 }
446
Tony Wickhameef44322015-10-20 13:24:36 -0700447 // TODO(twickham): b/25154513
448 public void setSearchBarHeight(int searchBarHeight) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700449 if (searchBarHeight == LauncherCallbacks.SEARCH_BAR_HEIGHT_TALL) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700450 hotseatBarHeightPx = shortHotseatBarHeightPx;
451 searchBarSpaceHeightPx = tallSearchBarSpaceHeightPx;
452 searchBarBottomPaddingPx = tallSearchBarBottomPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800453 searchBarTopExtraPaddingPx = isPhone ? tallSearchBarNegativeTopPaddingPx
454 : normalSearchBarTopExtraPaddingPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700455 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700456 hotseatBarHeightPx = normalHotseatBarHeightPx;
457 searchBarSpaceHeightPx = normalSearchBarSpaceHeightPx;
458 searchBarBottomPaddingPx = normalSearchBarBottomPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800459 searchBarTopExtraPaddingPx = normalSearchBarTopExtraPaddingPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700460 }
Tony Wickhameef44322015-10-20 13:24:36 -0700461 }
462
Jorim Jaggid017f882014-01-14 17:08:48 -0800463 public void layout(Launcher launcher) {
464 FrameLayout.LayoutParams lp;
465 boolean hasVerticalBarLayout = isVerticalBarLayout();
466 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
467
468 // Layout the search bar space
Tony Wickhama3c74d12015-10-23 11:43:47 -0700469 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chung006ee262015-08-03 14:40:11 -0700470 View searchBar = launcher.getSearchDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700471 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Tony Wickhama3c74d12015-10-23 11:43:47 -0700472 lp.width = searchBarBounds.width();
473 lp.height = searchBarBounds.height();
Tony Wickham8a758062016-02-01 14:15:20 -0800474 lp.topMargin = searchBarTopExtraPaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700475 searchBar.setLayoutParams(lp);
476
Tony Wickham34d2c912015-09-11 09:27:58 -0700477 // Layout the app info bar space
478 View appInfoBar = launcher.getAppInfoDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700479 lp = (FrameLayout.LayoutParams) appInfoBar.getLayoutParams();
Tony Wickham34d2c912015-09-11 09:27:58 -0700480 lp.bottomMargin = hotseatBarHeightPx;
481 appInfoBar.setLayoutParams(lp);
482
Winson Chungb3800242013-10-24 11:01:54 -0700483 // Layout the workspace
484 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
485 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
486 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700487 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700488 workspace.setLayoutParams(lp);
489 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700490 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700491
492 // Layout the hotseat
493 View hotseat = launcher.findViewById(R.id.hotseat);
494 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700495 // We want the edges of the hotseat to line up with the edges of the workspace, but the
496 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
497 // this, we pad the left and right of the hotseat with half of the difference of a workspace
498 // cell vs a hotseat cell.
499 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
500 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
501 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700502 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700503 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
504 // screen regardless of RTL
505 lp.gravity = Gravity.RIGHT;
Tony Wickhama3c74d12015-10-23 11:43:47 -0700506 lp.width = normalHotseatBarHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700507 lp.height = LayoutParams.MATCH_PARENT;
508 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700509 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800510 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700511 lp.gravity = Gravity.BOTTOM;
512 lp.width = LayoutParams.MATCH_PARENT;
513 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700514 hotseat.findViewById(R.id.layout).setPadding(
515 hotseatAdjustment + padding.left, 0,
516 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700517 } else {
518 // For phones, layout the hotseat without any bottom margin
519 // to ensure that we have space for the folders
520 lp.gravity = Gravity.BOTTOM;
521 lp.width = LayoutParams.MATCH_PARENT;
522 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700523 hotseat.findViewById(R.id.layout).setPadding(
524 hotseatAdjustment + padding.left, 0,
525 hotseatAdjustment + padding.right, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700526 }
527 hotseat.setLayoutParams(lp);
528
529 // Layout the page indicators
530 View pageIndicator = launcher.findViewById(R.id.page_indicator);
531 if (pageIndicator != null) {
532 if (hasVerticalBarLayout) {
533 // Hide the page indicators when we have vertical search/hotseat
534 pageIndicator.setVisibility(View.GONE);
535 } else {
536 // Put the page indicators above the hotseat
537 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
538 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
539 lp.width = LayoutParams.WRAP_CONTENT;
540 lp.height = LayoutParams.WRAP_CONTENT;
541 lp.bottomMargin = hotseatBarHeightPx;
542 pageIndicator.setLayoutParams(lp);
543 }
544 }
545
Winson Chungb3800242013-10-24 11:01:54 -0700546 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800547 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700548 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700549 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
550 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700551
552 int visibleChildCount = getVisibleChildCount(overviewMode);
553 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
554 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
555
556 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700557 lp.height = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700558 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700559
560 if (lp.width > totalItemWidth && visibleChildCount > 1) {
561 // We have enough space. Lets add some margin too.
562 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
563 View lastChild = null;
564
565 // Set margin of all visible children except the last visible child
566 for (int i = 0; i < visibleChildCount; i++) {
567 if (lastChild != null) {
568 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
569 if (isLayoutRtl) {
570 clp.leftMargin = margin;
571 } else {
572 clp.rightMargin = margin;
573 }
574 lastChild.setLayoutParams(clp);
575 lastChild = null;
576 }
577 View thisChild = overviewMode.getChildAt(i);
578 if (thisChild.getVisibility() != View.GONE) {
579 lastChild = thisChild;
580 }
581 }
582 }
Winson Chungb3800242013-10-24 11:01:54 -0700583 }
584 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700585
586 private int getCurrentWidth() {
587 return isLandscape
588 ? Math.max(widthPx, heightPx)
589 : Math.min(widthPx, heightPx);
590 }
591
592 private int getCurrentHeight() {
593 return isLandscape
594 ? Math.min(widthPx, heightPx)
595 : Math.max(widthPx, heightPx);
596 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700597
598
Sunny Goyale27815e2016-04-04 17:51:04 -0700599 public static final int getContainerPadding(Context context, int availableWidth) {
Sunny Goyal13178ac2016-04-04 16:35:22 -0700600 Resources res = context.getResources();
601
602 int maxSize = res.getDimensionPixelSize(R.dimen.container_max_width);
603 int minMargin = res.getDimensionPixelSize(R.dimen.container_min_margin);
604
605 if (maxSize > 0) {
606 return Math.max(minMargin, (availableWidth - maxSize) / 2);
607 } else {
608 return Math.max(minMargin,
609 (int) res.getFraction(R.fraction.container_margin, availableWidth, 1));
610 }
611 }
Winson Chungb3800242013-10-24 11:01:54 -0700612}