blob: cd5823084990b5058fe3647f7fe2db80411d3b51 [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;
88 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;
Winson Chung0f785722015-04-08 10:27:49 -0700265 }
266
Winson Chungef7f8742015-06-04 17:18:17 -0700267 /**
268 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
269 */
270 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700271 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700272 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700273 int allAppsCellWidthGap =
274 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700275 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700276 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700277 (allAppsIconSizePx + allAppsCellWidthGap);
278 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700279 allAppsNumCols = numAppsCols;
280 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700281 }
282
Tony Wickhama3c74d12015-10-23 11:43:47 -0700283 /** Returns the amount of extra space to allocate to the search bar for vertical padding. */
284 private int getSearchBarTotalVerticalPadding() {
Tony Wickham8a758062016-02-01 14:15:20 -0800285 return searchBarTopPaddingPx + searchBarTopExtraPaddingPx + searchBarBottomPaddingPx;
Tony Wickhama3c74d12015-10-23 11:43:47 -0700286 }
287
288 /** Returns the width and height of the search bar, ignoring any padding. */
289 public Point getSearchBarDimensForWidgetOpts(Resources res) {
290 Rect searchBarBounds = getSearchBarBounds(Utilities.isRtl(res));
291 if (isVerticalBarLayout()) {
292 return new Point(searchBarBounds.width(), searchBarBounds.height());
Winson Chung69e04ea2013-12-02 14:43:44 -0800293 }
Tony Wickhama3c74d12015-10-23 11:43:47 -0700294 int widgetInternalPadding = searchBarWidgetInternalPaddingTop +
295 searchBarWidgetInternalPaddingBottom;
296 return new Point(searchBarBounds.width(), searchBarSpaceHeightPx + widgetInternalPadding);
Winson Chung69e04ea2013-12-02 14:43:44 -0800297 }
298
Winson Chungb3800242013-10-24 11:01:54 -0700299 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700300 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700301 Rect bounds = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700302 if (isVerticalBarLayout()) {
Winson Chung42b3c062013-12-04 12:09:59 -0800303 if (isLayoutRtl) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700304 bounds.set(availableWidthPx - normalSearchBarSpaceHeightPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800305 availableWidthPx, availableHeightPx - edgeMarginPx);
306 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700307 bounds.set(0, edgeMarginPx, normalSearchBarSpaceHeightPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800308 availableHeightPx - edgeMarginPx);
309 }
Winson Chungb3800242013-10-24 11:01:54 -0700310 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700311 int boundsBottom = searchBarSpaceHeightPx + getSearchBarTotalVerticalPadding();
Sunny Goyalc6205602015-05-21 20:46:33 -0700312 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700313 // Pad the left and right of the workspace to ensure consistent spacing
314 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700315 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700316 // XXX: If the icon size changes across orientations, we will have to take
317 // that into account here too.
318 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700319 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Tony Wickhama3c74d12015-10-23 11:43:47 -0700320 bounds.set(edgeMarginPx + gap, 0,
321 availableWidthPx - (edgeMarginPx + gap), boundsBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700322 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800323 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700324 0,
Winson Chungb3800242013-10-24 11:01:54 -0700325 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
Tony Wickhama3c74d12015-10-23 11:43:47 -0700326 defaultWidgetPadding.right), boundsBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700327 }
328 }
329 return bounds;
330 }
331
Sunny Goyal756cd262015-08-20 12:33:21 -0700332 public Point getCellSize() {
333 Point result = new Point();
334 // Since we are only concerned with the overall padding, layout direction does
335 // not matter.
336 Rect padding = getWorkspacePadding(false /* isLayoutRtl */ );
337 result.x = calculateCellWidth(availableWidthPx - padding.left - padding.right,
338 inv.numColumns);
339 result.y = calculateCellHeight(availableHeightPx - padding.top - padding.bottom,
340 inv.numRows);
341 return result;
342 }
343
Winson Chungb3800242013-10-24 11:01:54 -0700344 /** Returns the workspace padding in the specified orientation */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800345 public Rect getWorkspacePadding(boolean isLayoutRtl) {
Sunny Goyalc6205602015-05-21 20:46:33 -0700346 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700347 Rect padding = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700348 if (isVerticalBarLayout()) {
Winson Chungb3800242013-10-24 11:01:54 -0700349 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800350 if (isLayoutRtl) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700351 padding.set(normalHotseatBarHeightPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800352 searchBarBounds.width(), edgeMarginPx);
353 } else {
354 padding.set(searchBarBounds.width(), edgeMarginPx,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700355 normalHotseatBarHeightPx, edgeMarginPx);
Winson Chung42b3c062013-12-04 12:09:59 -0800356 }
Winson Chungb3800242013-10-24 11:01:54 -0700357 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700358 int paddingTop = searchBarBounds.bottom;
359 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700360 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700361 // Pad the left and right of the workspace to ensure consistent spacing
362 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800363 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700364 int width = getCurrentWidth();
365 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700366 // The amount of screen space available for left/right padding.
367 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
368 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
369 availablePaddingX = (int) Math.min(availablePaddingX,
370 width * MAX_HORIZONTAL_PADDING_PERCENT);
371 int availablePaddingY = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700372 - (int) (2 * inv.numRows * cellHeightPx));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700373 padding.set(availablePaddingX / 2, paddingTop + availablePaddingY / 2,
374 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700375 } else {
376 // Pad the top and bottom of the workspace with search/hotseat bar sizes
377 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700378 paddingTop,
Winson Chungb3800242013-10-24 11:01:54 -0700379 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700380 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700381 }
382 }
383 return padding;
384 }
385
Sunny Goyalc6205602015-05-21 20:46:33 -0700386 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700387 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700388 // In landscape mode the page spacing is set to the default.
389 return defaultPageSpacingPx;
390 } else {
391 // In portrait, we want the pages spaced such that there is no
392 // overhang of the previous / next page into the current page viewport.
393 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700394 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700395 }
396 }
397
Winson Chung4f7eb402015-07-10 18:00:48 -0700398 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700399 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
400 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
401 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700402 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700403 }
404
405 // The rect returned will be extended to below the system ui that covers the workspace
Sunny Goyalcf25b522015-07-09 00:01:18 -0700406 public boolean isInHotseatRect(int x, int y) {
Winson Chungb3800242013-10-24 11:01:54 -0700407 if (isVerticalBarLayout()) {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700408 return (x >= (availableWidthPx - hotseatBarHeightPx))
409 && (y >= 0) && (y <= availableHeightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700410 } else {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700411 return (x >= 0) && (x <= availableWidthPx)
412 && (y >= (availableHeightPx - hotseatBarHeightPx));
Winson Chungb3800242013-10-24 11:01:54 -0700413 }
414 }
415
Adam Cohen2e6da152015-05-06 11:42:25 -0700416 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700417 return width / countX;
418 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700419 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700420 return height / countY;
421 }
422
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700423 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700424 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
425 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
426 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700427 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700428 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700429 return isLandscape && transposeLayoutWithOrientation;
430 }
431
432 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700433 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700434 }
435
Sunny Goyalc6205602015-05-21 20:46:33 -0700436 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800437 int visibleChildren = 0;
438 for (int i = 0; i < parent.getChildCount(); i++) {
439 if (parent.getChildAt(i).getVisibility() != View.GONE) {
440 visibleChildren++;
441 }
442 }
443 return visibleChildren;
444 }
445
Tony Wickhameef44322015-10-20 13:24:36 -0700446 // TODO(twickham): b/25154513
447 public void setSearchBarHeight(int searchBarHeight) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700448 if (searchBarHeight == LauncherCallbacks.SEARCH_BAR_HEIGHT_TALL) {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700449 hotseatBarHeightPx = shortHotseatBarHeightPx;
450 searchBarSpaceHeightPx = tallSearchBarSpaceHeightPx;
451 searchBarBottomPaddingPx = tallSearchBarBottomPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800452 searchBarTopExtraPaddingPx = isPhone ? tallSearchBarNegativeTopPaddingPx
453 : normalSearchBarTopExtraPaddingPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700454 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700455 hotseatBarHeightPx = normalHotseatBarHeightPx;
456 searchBarSpaceHeightPx = normalSearchBarSpaceHeightPx;
457 searchBarBottomPaddingPx = normalSearchBarBottomPaddingPx;
Tony Wickham8a758062016-02-01 14:15:20 -0800458 searchBarTopExtraPaddingPx = normalSearchBarTopExtraPaddingPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700459 }
Tony Wickhameef44322015-10-20 13:24:36 -0700460 }
461
Jorim Jaggid017f882014-01-14 17:08:48 -0800462 public void layout(Launcher launcher) {
463 FrameLayout.LayoutParams lp;
464 boolean hasVerticalBarLayout = isVerticalBarLayout();
465 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
466
467 // Layout the search bar space
Tony Wickhama3c74d12015-10-23 11:43:47 -0700468 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chung006ee262015-08-03 14:40:11 -0700469 View searchBar = launcher.getSearchDropTargetBar();
Tony Wickham34d2c912015-09-11 09:27:58 -0700470 lp = getDropTargetBarLayoutParams(hasVerticalBarLayout, searchBar, Gravity.TOP);
Tony Wickhama3c74d12015-10-23 11:43:47 -0700471 lp.width = searchBarBounds.width();
472 lp.height = searchBarBounds.height();
Tony Wickham8a758062016-02-01 14:15:20 -0800473 lp.topMargin = searchBarTopExtraPaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700474 searchBar.setLayoutParams(lp);
475
Tony Wickham34d2c912015-09-11 09:27:58 -0700476 // Layout the app info bar space
477 View appInfoBar = launcher.getAppInfoDropTargetBar();
478 lp = getDropTargetBarLayoutParams(hasVerticalBarLayout, appInfoBar, Gravity.BOTTOM);
479 lp.bottomMargin = hotseatBarHeightPx;
480 appInfoBar.setLayoutParams(lp);
481
Winson Chungb3800242013-10-24 11:01:54 -0700482 // Layout the workspace
483 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
484 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
485 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700486 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700487 workspace.setLayoutParams(lp);
488 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700489 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700490
491 // Layout the hotseat
492 View hotseat = launcher.findViewById(R.id.hotseat);
493 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700494 // We want the edges of the hotseat to line up with the edges of the workspace, but the
495 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
496 // this, we pad the left and right of the hotseat with half of the difference of a workspace
497 // cell vs a hotseat cell.
498 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
499 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
500 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700501 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700502 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
503 // screen regardless of RTL
504 lp.gravity = Gravity.RIGHT;
Tony Wickhama3c74d12015-10-23 11:43:47 -0700505 lp.width = normalHotseatBarHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700506 lp.height = LayoutParams.MATCH_PARENT;
507 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700508 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800509 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700510 lp.gravity = Gravity.BOTTOM;
511 lp.width = LayoutParams.MATCH_PARENT;
512 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700513 hotseat.findViewById(R.id.layout).setPadding(
514 hotseatAdjustment + padding.left, 0,
515 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700516 } else {
517 // For phones, layout the hotseat without any bottom margin
518 // to ensure that we have space for the folders
519 lp.gravity = Gravity.BOTTOM;
520 lp.width = LayoutParams.MATCH_PARENT;
521 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700522 hotseat.findViewById(R.id.layout).setPadding(
523 hotseatAdjustment + padding.left, 0,
524 hotseatAdjustment + padding.right, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700525 }
526 hotseat.setLayoutParams(lp);
527
528 // Layout the page indicators
529 View pageIndicator = launcher.findViewById(R.id.page_indicator);
530 if (pageIndicator != null) {
531 if (hasVerticalBarLayout) {
532 // Hide the page indicators when we have vertical search/hotseat
533 pageIndicator.setVisibility(View.GONE);
534 } else {
535 // Put the page indicators above the hotseat
536 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
537 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
538 lp.width = LayoutParams.WRAP_CONTENT;
539 lp.height = LayoutParams.WRAP_CONTENT;
540 lp.bottomMargin = hotseatBarHeightPx;
541 pageIndicator.setLayoutParams(lp);
542 }
543 }
544
Winson Chungb3800242013-10-24 11:01:54 -0700545 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800546 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700547 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700548 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
549 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700550
551 int visibleChildCount = getVisibleChildCount(overviewMode);
552 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
553 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
554
555 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700556 lp.height = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700557 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700558
559 if (lp.width > totalItemWidth && visibleChildCount > 1) {
560 // We have enough space. Lets add some margin too.
561 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
562 View lastChild = null;
563
564 // Set margin of all visible children except the last visible child
565 for (int i = 0; i < visibleChildCount; i++) {
566 if (lastChild != null) {
567 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
568 if (isLayoutRtl) {
569 clp.leftMargin = margin;
570 } else {
571 clp.rightMargin = margin;
572 }
573 lastChild.setLayoutParams(clp);
574 lastChild = null;
575 }
576 View thisChild = overviewMode.getChildAt(i);
577 if (thisChild.getVisibility() != View.GONE) {
578 lastChild = thisChild;
579 }
580 }
581 }
Winson Chungb3800242013-10-24 11:01:54 -0700582 }
583 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700584
Tony Wickham34d2c912015-09-11 09:27:58 -0700585 private FrameLayout.LayoutParams getDropTargetBarLayoutParams(boolean hasVerticalBarLayout,
586 View dropTargetBar, int verticalGravity) {
587 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) dropTargetBar.getLayoutParams();
588 if (hasVerticalBarLayout) {
589 // Vertical drop target bar space -- The drop target bar is fixed in the layout to be on
590 // the left of the screen regardless of RTL
591 lp.gravity = Gravity.LEFT;
Tony Wickhamc1ff24c2015-10-30 10:37:43 -0700592 lp.width = normalSearchBarSpaceHeightPx;
Tony Wickham34d2c912015-09-11 09:27:58 -0700593
594 LinearLayout targets = (LinearLayout) dropTargetBar.findViewById(R.id.drag_target_bar);
595 targets.setOrientation(LinearLayout.VERTICAL);
596 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
597 targetsLp.gravity = verticalGravity;
598 targetsLp.height = LayoutParams.WRAP_CONTENT;
599 } else {
600 // Horizontal drop target bar space
Tony Wickham5ea422e2015-10-30 11:03:51 -0700601 lp.gravity = verticalGravity | Gravity.CENTER_HORIZONTAL;
Tony Wickham34d2c912015-09-11 09:27:58 -0700602 lp.height = searchBarSpaceHeightPx;
Tony Wickham34d2c912015-09-11 09:27:58 -0700603 }
604 return lp;
605 }
606
Winson Chung4f3bfad2015-07-13 11:14:30 -0700607 private int getCurrentWidth() {
608 return isLandscape
609 ? Math.max(widthPx, heightPx)
610 : Math.min(widthPx, heightPx);
611 }
612
613 private int getCurrentHeight() {
614 return isLandscape
615 ? Math.min(widthPx, heightPx)
616 : Math.max(widthPx, heightPx);
617 }
Winson Chungb3800242013-10-24 11:01:54 -0700618}