blob: 61c4b1a22de178fb130bbbffbce85450ce4e6fc8 [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
Winson Chungb3800242013-10-24 11:01:54 -070036public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070037
Adam Cohen2e6da152015-05-06 11:42:25 -070038 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070039
Sunny Goyalc6205602015-05-21 20:46:33 -070040 // Device properties
41 public final boolean isTablet;
42 public final boolean isLargeTablet;
43 public final boolean isPhone;
44 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070045
Sunny Goyalc6205602015-05-21 20:46:33 -070046 // Device properties in current orientation
47 public final boolean isLandscape;
48 public final int widthPx;
49 public final int heightPx;
50 public final int availableWidthPx;
51 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070052 /**
53 * The maximum amount of left/right workspace padding as a percentage of the screen width.
54 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
55 * 7% of the screen width can be used as right padding.
56 */
57 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070058
Sunny Goyalc6205602015-05-21 20:46:33 -070059 // Overview mode
60 private final int overviewModeMinIconZoneHeightPx;
61 private final int overviewModeMaxIconZoneHeightPx;
62 private final int overviewModeBarItemWidthPx;
63 private final int overviewModeBarSpacerWidthPx;
64 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070065
Sunny Goyalc6205602015-05-21 20:46:33 -070066 // Workspace
67 private int desiredWorkspaceLeftRightMarginPx;
68 public final int edgeMarginPx;
69 public final Rect defaultWidgetPadding;
70 private final int pageIndicatorHeightPx;
71 private final int defaultPageSpacingPx;
72 private float dragViewScale;
73
74 // Workspace icons
75 public int iconSizePx;
76 public int iconTextSizePx;
77 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070078 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070079
Adam Cohen59400422014-03-05 18:07:04 -080080 public int cellWidthPx;
81 public int cellHeightPx;
82
Sunny Goyalc6205602015-05-21 20:46:33 -070083 // Folder
84 public int folderBackgroundOffset;
85 public int folderIconSizePx;
86 public int folderCellWidthPx;
87 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070088
Sunny Goyalc6205602015-05-21 20:46:33 -070089 // Hotseat
90 public int hotseatCellWidthPx;
91 public int hotseatCellHeightPx;
92 public int hotseatIconSizePx;
Tony Wickhama3c74d12015-10-23 11:43:47 -070093 private int normalHotseatBarHeightPx, shortHotseatBarHeightPx;
Tony Wickham55616cd2015-09-23 14:55:17 -070094 private int hotseatBarHeightPx; // One of the above.
Adam Cohen2e6da152015-05-06 11:42:25 -070095
Sunny Goyalc6205602015-05-21 20:46:33 -070096 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097 public int allAppsNumCols;
98 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -070099 public int allAppsButtonVisualSize;
100 public final int allAppsIconSizePx;
Sunny Goyal383e0a22015-12-10 12:28:21 -0800101 public final float allAppsIconTextSizeSp;
Winson Chung59a488a2013-12-10 12:32:14 -0800102
Sunny Goyalc6205602015-05-21 20:46:33 -0700103 // QSB
Tony Wickhama3c74d12015-10-23 11:43:47 -0700104 private int searchBarWidgetInternalPaddingTop, searchBarWidgetInternalPaddingBottom;
105 private int searchBarTopPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800106 private int tallSearchBarNegativeTopPaddingPx, normalSearchBarTopExtraPaddingPx;
107 private int searchBarTopExtraPaddingPx; // One of the above.
Tony Wickhama3c74d12015-10-23 11:43:47 -0700108 private int normalSearchBarBottomPaddingPx, tallSearchBarBottomPaddingPx;
109 private int searchBarBottomPaddingPx; // One of the above.
110 private int normalSearchBarSpaceHeightPx, tallSearchBarSpaceHeightPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700111 private int searchBarSpaceHeightPx; // One of the above.
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700112
Sunny Goyalc6205602015-05-21 20:46:33 -0700113 public DeviceProfile(Context context, InvariantDeviceProfile inv,
114 Point minSize, Point maxSize,
115 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700116
Adam Cohen2e6da152015-05-06 11:42:25 -0700117 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700118 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700119
Adam Cohen2e6da152015-05-06 11:42:25 -0700120 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700121 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700122
Sunny Goyalc6205602015-05-21 20:46:33 -0700123 // Constants from resources
124 isTablet = res.getBoolean(R.bool.is_tablet);
125 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
126 isPhone = !isTablet && !isLargeTablet;
127
128 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700129 transposeLayoutWithOrientation =
130 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700131
132 ComponentName cn = new ComponentName(context.getPackageName(),
133 this.getClass().getName());
134 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
135 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
136 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
137 pageIndicatorHeightPx =
138 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
139 defaultPageSpacingPx =
140 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700141 overviewModeMinIconZoneHeightPx =
142 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
143 overviewModeMaxIconZoneHeightPx =
144 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800145 overviewModeBarItemWidthPx =
146 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
147 overviewModeBarSpacerWidthPx =
148 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700149 overviewModeIconZoneRatio =
150 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700151 iconDrawablePaddingOriginalPx =
152 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700153
Adam Cohen2e6da152015-05-06 11:42:25 -0700154 // AllApps uses the original non-scaled icon text size
Sunny Goyal383e0a22015-12-10 12:28:21 -0800155 allAppsIconTextSizeSp = inv.iconTextSize;
Adam Cohen2e6da152015-05-06 11:42:25 -0700156
157 // AllApps uses the original non-scaled icon size
158 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700159
Sunny Goyalc6205602015-05-21 20:46:33 -0700160 // Determine sizes.
161 widthPx = width;
162 heightPx = height;
163 if (isLandscape) {
164 availableWidthPx = maxSize.x;
165 availableHeightPx = minSize.y;
166 } else {
167 availableWidthPx = minSize.x;
168 availableHeightPx = maxSize.y;
169 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700170
Winson Chungb3800242013-10-24 11:01:54 -0700171 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700172 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700173 computeAllAppsButtonSize(context);
174 }
175
176 /**
177 * Determine the exact visual footprint of the all apps button, taking into account scaling
178 * and internal padding of the drawable.
179 */
180 private void computeAllAppsButtonSize(Context context) {
181 Resources res = context.getResources();
182 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700183 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
184 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700185 }
186
Sunny Goyalc6205602015-05-21 20:46:33 -0700187 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700188 // Check to see if the icons fit in the new available height. If not, then we need to
189 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700190 float scale = 1f;
191 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700192 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700193 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800194
Sunny Goyalc6205602015-05-21 20:46:33 -0700195 // We only care about the top and bottom workspace padding, which is not affected by RTL.
196 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700197 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
198 if (usedHeight > maxHeight) {
199 scale = maxHeight / usedHeight;
200 drawablePadding = 0;
201 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700202 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700203 }
204
Winson Chung93f98ea2015-03-10 16:28:47 -0700205 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700206 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700207 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800208 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700209 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700210 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700211
212 // Search Bar
Tony Wickhama3c74d12015-10-23 11:43:47 -0700213 normalSearchBarSpaceHeightPx = res.getDimensionPixelSize(
214 R.dimen.dynamic_grid_search_bar_height);
215 tallSearchBarSpaceHeightPx = res.getDimensionPixelSize(
216 R.dimen.dynamic_grid_search_bar_height_tall);
217 searchBarWidgetInternalPaddingTop = res.getDimensionPixelSize(
218 R.dimen.qsb_internal_padding_top);
219 searchBarWidgetInternalPaddingBottom = res.getDimensionPixelSize(
220 R.dimen.qsb_internal_padding_bottom);
Tony Wickham8a758062016-02-01 14:15:20 -0800221 normalSearchBarTopExtraPaddingPx = res.getDimensionPixelSize(
222 R.dimen.dynamic_grid_search_bar_extra_top_padding);
223 tallSearchBarNegativeTopPaddingPx = res.getDimensionPixelSize(
224 R.dimen.dynamic_grid_search_bar_negative_top_padding_short);
Tony Wickhama3c74d12015-10-23 11:43:47 -0700225 if (isTablet && !isVerticalBarLayout()) {
226 searchBarTopPaddingPx = searchBarWidgetInternalPaddingTop;
227 normalSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom +
228 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_bottom_padding_tablet);
229 tallSearchBarBottomPaddingPx = normalSearchBarBottomPaddingPx;
230 } else {
231 searchBarTopPaddingPx = searchBarWidgetInternalPaddingTop;
232 normalSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom +
233 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_bottom_padding);
Tony Wickham8a758062016-02-01 14:15:20 -0800234 tallSearchBarBottomPaddingPx = searchBarWidgetInternalPaddingBottom
235 + res.getDimensionPixelSize(
236 R.dimen.dynamic_grid_search_bar_bottom_negative_padding_short);
Tony Wickhama3c74d12015-10-23 11:43:47 -0700237 }
Winson Chungb3800242013-10-24 11:01:54 -0700238
239 // Calculate the actual text height
240 Paint textPaint = new Paint();
241 textPaint.setTextSize(iconTextSizePx);
242 FontMetrics fm = textPaint.getFontMetrics();
243 cellWidthPx = iconSizePx;
244 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700245 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800246 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700247
248 // Hotseat
Tony Wickhama3c74d12015-10-23 11:43:47 -0700249 normalHotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
250 shortHotseatBarHeightPx = iconSizePx + 2 * edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700251 hotseatCellWidthPx = iconSizePx;
252 hotseatCellHeightPx = iconSizePx;
253
254 // Folder
Tony Wickham6159c242015-11-13 18:07:18 +0000255 int folderCellPadding = isTablet || isLandscape ? 6 * edgeMarginPx : 3 * edgeMarginPx;
256 // Don't let the folder get too close to the edges of the screen.
257 folderCellWidthPx = Math.min(cellWidthPx + folderCellPadding,
Tony Wickhama2950482015-10-12 15:57:23 -0700258 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Winson Chungb3800242013-10-24 11:01:54 -0700259 folderCellHeightPx = cellHeightPx + edgeMarginPx;
260 folderBackgroundOffset = -edgeMarginPx;
261 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700262 }
263
Winson Chungef7f8742015-06-04 17:18:17 -0700264 /**
265 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
266 */
267 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700268 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700269 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700270 int allAppsCellWidthGap =
271 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700272 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700273 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700274 (allAppsIconSizePx + allAppsCellWidthGap);
275 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700276 allAppsNumCols = numAppsCols;
277 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700278 }
279
Tony Wickhama3c74d12015-10-23 11:43:47 -0700280 /** Returns the amount of extra space to allocate to the search bar for vertical padding. */
281 private int getSearchBarTotalVerticalPadding() {
Tony Wickham8a758062016-02-01 14:15:20 -0800282 return searchBarTopPaddingPx + searchBarTopExtraPaddingPx + searchBarBottomPaddingPx;
Tony Wickhama3c74d12015-10-23 11:43:47 -0700283 }
284
285 /** Returns the width and height of the search bar, ignoring any padding. */
286 public Point getSearchBarDimensForWidgetOpts(Resources res) {
287 Rect searchBarBounds = getSearchBarBounds(Utilities.isRtl(res));
288 if (isVerticalBarLayout()) {
289 return new Point(searchBarBounds.width(), searchBarBounds.height());
Winson Chung69e04ea2013-12-02 14:43:44 -0800290 }
Tony Wickhama3c74d12015-10-23 11:43:47 -0700291 int widgetInternalPadding = searchBarWidgetInternalPaddingTop +
292 searchBarWidgetInternalPaddingBottom;
293 return new Point(searchBarBounds.width(), searchBarSpaceHeightPx + widgetInternalPadding);
Winson Chung69e04ea2013-12-02 14:43:44 -0800294 }
295
Winson Chungb3800242013-10-24 11:01:54 -0700296 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700297 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700298 Rect bounds = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700299 if (isVerticalBarLayout()) {
Winson Chung42b3c062013-12-04 12:09:59 -0800300 if (isLayoutRtl) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700301 bounds.set(availableWidthPx - normalSearchBarSpaceHeightPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800302 availableWidthPx, availableHeightPx - edgeMarginPx);
303 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700304 bounds.set(0, edgeMarginPx, normalSearchBarSpaceHeightPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800305 availableHeightPx - edgeMarginPx);
306 }
Winson Chungb3800242013-10-24 11:01:54 -0700307 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700308 int boundsBottom = searchBarSpaceHeightPx + getSearchBarTotalVerticalPadding();
Sunny Goyalc6205602015-05-21 20:46:33 -0700309 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700310 // Pad the left and right of the workspace to ensure consistent spacing
311 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700312 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700313 // XXX: If the icon size changes across orientations, we will have to take
314 // that into account here too.
315 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700316 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Tony Wickhama3c74d12015-10-23 11:43:47 -0700317 bounds.set(edgeMarginPx + gap, 0,
318 availableWidthPx - (edgeMarginPx + gap), boundsBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700319 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800320 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700321 0,
Winson Chungb3800242013-10-24 11:01:54 -0700322 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
Tony Wickhama3c74d12015-10-23 11:43:47 -0700323 defaultWidgetPadding.right), boundsBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700324 }
325 }
326 return bounds;
327 }
328
329 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700330 Rect getWorkspacePadding(boolean isLayoutRtl) {
331 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700332 Rect padding = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700333 if (isVerticalBarLayout()) {
Winson Chungb3800242013-10-24 11:01:54 -0700334 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800335 if (isLayoutRtl) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700336 padding.set(normalHotseatBarHeightPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800337 searchBarBounds.width(), edgeMarginPx);
338 } else {
339 padding.set(searchBarBounds.width(), edgeMarginPx,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700340 normalHotseatBarHeightPx, edgeMarginPx);
Winson Chung42b3c062013-12-04 12:09:59 -0800341 }
Winson Chungb3800242013-10-24 11:01:54 -0700342 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700343 int paddingTop = searchBarBounds.bottom;
344 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700345 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700346 // Pad the left and right of the workspace to ensure consistent spacing
347 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800348 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700349 int width = getCurrentWidth();
350 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700351 // The amount of screen space available for left/right padding.
352 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
353 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
354 availablePaddingX = (int) Math.min(availablePaddingX,
355 width * MAX_HORIZONTAL_PADDING_PERCENT);
356 int availablePaddingY = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700357 - (int) (2 * inv.numRows * cellHeightPx));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700358 padding.set(availablePaddingX / 2, paddingTop + availablePaddingY / 2,
359 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700360 } else {
361 // Pad the top and bottom of the workspace with search/hotseat bar sizes
362 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700363 paddingTop,
Winson Chungb3800242013-10-24 11:01:54 -0700364 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700365 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700366 }
367 }
368 return padding;
369 }
370
Sunny Goyalc6205602015-05-21 20:46:33 -0700371 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700372 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700373 // In landscape mode the page spacing is set to the default.
374 return defaultPageSpacingPx;
375 } else {
376 // In portrait, we want the pages spaced such that there is no
377 // overhang of the previous / next page into the current page viewport.
378 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700379 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700380 }
381 }
382
Winson Chung4f7eb402015-07-10 18:00:48 -0700383 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700384 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
385 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
386 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700387 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700388 }
389
390 // The rect returned will be extended to below the system ui that covers the workspace
391 Rect getHotseatRect() {
392 if (isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700393 return new Rect(availableWidthPx - normalHotseatBarHeightPx, 0,
Winson Chungb3800242013-10-24 11:01:54 -0700394 Integer.MAX_VALUE, availableHeightPx);
395 } else {
396 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
397 availableWidthPx, Integer.MAX_VALUE);
398 }
399 }
400
Adam Cohen2e6da152015-05-06 11:42:25 -0700401 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700402 return width / countX;
403 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700404 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700405 return height / countY;
406 }
407
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700408 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700409 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
410 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
411 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700412 */
Winson Chungb3800242013-10-24 11:01:54 -0700413 boolean isVerticalBarLayout() {
414 return isLandscape && transposeLayoutWithOrientation;
415 }
416
417 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700418 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700419 }
420
Sunny Goyalc6205602015-05-21 20:46:33 -0700421 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800422 int visibleChildren = 0;
423 for (int i = 0; i < parent.getChildCount(); i++) {
424 if (parent.getChildAt(i).getVisibility() != View.GONE) {
425 visibleChildren++;
426 }
427 }
428 return visibleChildren;
429 }
430
Tony Wickhameef44322015-10-20 13:24:36 -0700431 // TODO(twickham): b/25154513
432 public void setSearchBarHeight(int searchBarHeight) {
433 if (searchBarHeight == LauncherCallbacks.SEARCH_BAR_HEIGHT_TALL) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700434 hotseatBarHeightPx = shortHotseatBarHeightPx;
435 searchBarSpaceHeightPx = tallSearchBarSpaceHeightPx;
436 searchBarBottomPaddingPx = tallSearchBarBottomPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800437 searchBarTopExtraPaddingPx = isPhone ? tallSearchBarNegativeTopPaddingPx
438 : normalSearchBarTopExtraPaddingPx;
Tony Wickhameef44322015-10-20 13:24:36 -0700439 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700440 hotseatBarHeightPx = normalHotseatBarHeightPx;
441 searchBarSpaceHeightPx = normalSearchBarSpaceHeightPx;
442 searchBarBottomPaddingPx = normalSearchBarBottomPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800443 searchBarTopExtraPaddingPx = normalSearchBarTopExtraPaddingPx;
Tony Wickhameef44322015-10-20 13:24:36 -0700444 }
445 }
446
Winson Chungb3800242013-10-24 11:01:54 -0700447 public void layout(Launcher launcher) {
448 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700449 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700450 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700451
452 // Layout the search bar space
Tony Wickhama3c74d12015-10-23 11:43:47 -0700453 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chung006ee262015-08-03 14:40:11 -0700454 View searchBar = launcher.getSearchDropTargetBar();
Winson Chungb3800242013-10-24 11:01:54 -0700455 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Tony Wickhama3c74d12015-10-23 11:43:47 -0700456 lp.width = searchBarBounds.width();
457 lp.height = searchBarBounds.height();
Tony Wickham8a758062016-02-01 14:15:20 -0800458 lp.topMargin = searchBarTopExtraPaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700459 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700460 // Vertical search bar space -- The search bar is fixed in the layout to be on the left
461 // of the screen regardless of RTL
Sunny Goyald9cc7802015-04-23 14:47:19 -0700462 lp.gravity = Gravity.LEFT;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800463
464 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
465 targets.setOrientation(LinearLayout.VERTICAL);
Sunny Goyald9cc7802015-04-23 14:47:19 -0700466 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
467 targetsLp.gravity = Gravity.TOP;
468 targetsLp.height = LayoutParams.WRAP_CONTENT;
469
Winson Chungb3800242013-10-24 11:01:54 -0700470 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800471 // Horizontal search bar space
Tony Wickhama3c74d12015-10-23 11:43:47 -0700472 lp.gravity = Gravity.TOP|Gravity.CENTER_HORIZONTAL;
Winson Chungb3800242013-10-24 11:01:54 -0700473 }
474 searchBar.setLayoutParams(lp);
475
Winson Chungb3800242013-10-24 11:01:54 -0700476 // Layout the workspace
477 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
478 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
479 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700480 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700481 workspace.setLayoutParams(lp);
482 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700483 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700484
485 // Layout the hotseat
486 View hotseat = launcher.findViewById(R.id.hotseat);
487 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700488 // We want the edges of the hotseat to line up with the edges of the workspace, but the
489 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
490 // this, we pad the left and right of the hotseat with half of the difference of a workspace
491 // cell vs a hotseat cell.
492 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
493 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
494 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700495 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700496 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
497 // screen regardless of RTL
498 lp.gravity = Gravity.RIGHT;
Tony Wickhama3c74d12015-10-23 11:43:47 -0700499 lp.width = normalHotseatBarHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700500 lp.height = LayoutParams.MATCH_PARENT;
501 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700502 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800503 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700504 lp.gravity = Gravity.BOTTOM;
505 lp.width = LayoutParams.MATCH_PARENT;
506 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700507 hotseat.findViewById(R.id.layout).setPadding(
508 hotseatAdjustment + padding.left, 0,
509 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700510 } else {
511 // For phones, layout the hotseat without any bottom margin
512 // to ensure that we have space for the folders
513 lp.gravity = Gravity.BOTTOM;
514 lp.width = LayoutParams.MATCH_PARENT;
515 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700516 hotseat.findViewById(R.id.layout).setPadding(
517 hotseatAdjustment + padding.left, 0,
518 hotseatAdjustment + padding.right, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700519 }
520 hotseat.setLayoutParams(lp);
521
522 // Layout the page indicators
523 View pageIndicator = launcher.findViewById(R.id.page_indicator);
524 if (pageIndicator != null) {
525 if (hasVerticalBarLayout) {
526 // Hide the page indicators when we have vertical search/hotseat
527 pageIndicator.setVisibility(View.GONE);
528 } else {
529 // Put the page indicators above the hotseat
530 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
531 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
532 lp.width = LayoutParams.WRAP_CONTENT;
533 lp.height = LayoutParams.WRAP_CONTENT;
534 lp.bottomMargin = hotseatBarHeightPx;
535 pageIndicator.setLayoutParams(lp);
536 }
537 }
538
Winson Chungb3800242013-10-24 11:01:54 -0700539 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800540 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700541 if (overviewMode != null) {
Winson Chung4f7eb402015-07-10 18:00:48 -0700542 int overviewButtonBarHeight = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700543 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
544 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700545
546 int visibleChildCount = getVisibleChildCount(overviewMode);
547 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
548 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
549
550 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chung4f7eb402015-07-10 18:00:48 -0700551 lp.height = overviewButtonBarHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700552 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700553
554 if (lp.width > totalItemWidth && visibleChildCount > 1) {
555 // We have enough space. Lets add some margin too.
556 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
557 View lastChild = null;
558
559 // Set margin of all visible children except the last visible child
560 for (int i = 0; i < visibleChildCount; i++) {
561 if (lastChild != null) {
562 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
563 if (isLayoutRtl) {
564 clp.leftMargin = margin;
565 } else {
566 clp.rightMargin = margin;
567 }
568 lastChild.setLayoutParams(clp);
569 lastChild = null;
570 }
571 View thisChild = overviewMode.getChildAt(i);
572 if (thisChild.getVisibility() != View.GONE) {
573 lastChild = thisChild;
574 }
575 }
576 }
Winson Chungb3800242013-10-24 11:01:54 -0700577 }
578 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700579
580 private int getCurrentWidth() {
581 return isLandscape
582 ? Math.max(widthPx, heightPx)
583 : Math.min(widthPx, heightPx);
584 }
585
586 private int getCurrentHeight() {
587 return isLandscape
588 ? Math.min(widthPx, heightPx)
589 : Math.max(widthPx, heightPx);
590 }
Winson Chungb3800242013-10-24 11:01:54 -0700591}