blob: d50b48e7507f9f9427dcd03bf85d10862060c9d7 [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;
Winson Chungb3800242013-10-24 11:01:54 -070052
Sunny Goyalc6205602015-05-21 20:46:33 -070053 // Overview mode
54 private final int overviewModeMinIconZoneHeightPx;
55 private final int overviewModeMaxIconZoneHeightPx;
56 private final int overviewModeBarItemWidthPx;
57 private final int overviewModeBarSpacerWidthPx;
58 private final float overviewModeIconZoneRatio;
59 private final float overviewModeScaleFactor;
Winson Chungb3800242013-10-24 11:01:54 -070060
Sunny Goyalc6205602015-05-21 20:46:33 -070061 // Workspace
62 private int desiredWorkspaceLeftRightMarginPx;
63 public final int edgeMarginPx;
64 public final Rect defaultWidgetPadding;
65 private final int pageIndicatorHeightPx;
66 private final int defaultPageSpacingPx;
67 private float dragViewScale;
68
69 // Workspace icons
70 public int iconSizePx;
71 public int iconTextSizePx;
72 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070073 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070074
Adam Cohen59400422014-03-05 18:07:04 -080075 public int cellWidthPx;
76 public int cellHeightPx;
77
Sunny Goyalc6205602015-05-21 20:46:33 -070078 // Folder
79 public int folderBackgroundOffset;
80 public int folderIconSizePx;
81 public int folderCellWidthPx;
82 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070083
Sunny Goyalc6205602015-05-21 20:46:33 -070084 // Hotseat
85 public int hotseatCellWidthPx;
86 public int hotseatCellHeightPx;
87 public int hotseatIconSizePx;
88 private int hotseatBarHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -070089
Sunny Goyalc6205602015-05-21 20:46:33 -070090 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -070091 public int allAppsNumCols;
92 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -070093 public int allAppsButtonVisualSize;
94 public final int allAppsIconSizePx;
95 public final int allAppsIconTextSizePx;
Winson Chung59a488a2013-12-10 12:32:14 -080096
Sunny Goyalc6205602015-05-21 20:46:33 -070097 // QSB
98 private int searchBarSpaceWidthPx;
99 private int searchBarSpaceHeightPx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700100
Sunny Goyalc6205602015-05-21 20:46:33 -0700101 public DeviceProfile(Context context, InvariantDeviceProfile inv,
102 Point minSize, Point maxSize,
103 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700104
Adam Cohen2e6da152015-05-06 11:42:25 -0700105 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700106 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700107
Adam Cohen2e6da152015-05-06 11:42:25 -0700108 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700109 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700110
Sunny Goyalc6205602015-05-21 20:46:33 -0700111 // Constants from resources
112 isTablet = res.getBoolean(R.bool.is_tablet);
113 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
114 isPhone = !isTablet && !isLargeTablet;
115
116 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700117 transposeLayoutWithOrientation =
118 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700119
120 ComponentName cn = new ComponentName(context.getPackageName(),
121 this.getClass().getName());
122 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
123 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
124 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
125 pageIndicatorHeightPx =
126 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
127 defaultPageSpacingPx =
128 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700129 overviewModeMinIconZoneHeightPx =
130 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
131 overviewModeMaxIconZoneHeightPx =
132 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800133 overviewModeBarItemWidthPx =
134 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
135 overviewModeBarSpacerWidthPx =
136 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700137 overviewModeIconZoneRatio =
138 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
139 overviewModeScaleFactor =
140 res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700141 iconDrawablePaddingOriginalPx =
142 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700143
Adam Cohen2e6da152015-05-06 11:42:25 -0700144 // AllApps uses the original non-scaled icon text size
145 allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
146
147 // AllApps uses the original non-scaled icon size
148 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700149
Sunny Goyalc6205602015-05-21 20:46:33 -0700150 // Determine sizes.
151 widthPx = width;
152 heightPx = height;
153 if (isLandscape) {
154 availableWidthPx = maxSize.x;
155 availableHeightPx = minSize.y;
156 } else {
157 availableWidthPx = minSize.x;
158 availableHeightPx = maxSize.y;
159 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700160
Winson Chungb3800242013-10-24 11:01:54 -0700161 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700162 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700163 computeAllAppsButtonSize(context);
164 }
165
166 /**
167 * Determine the exact visual footprint of the all apps button, taking into account scaling
168 * and internal padding of the drawable.
169 */
170 private void computeAllAppsButtonSize(Context context) {
171 Resources res = context.getResources();
172 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700173 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding));
Winson Chungb3800242013-10-24 11:01:54 -0700174 }
175
Sunny Goyalc6205602015-05-21 20:46:33 -0700176 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700177 // Check to see if the icons fit in the new available height. If not, then we need to
178 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700179 float scale = 1f;
180 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700181 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700182 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800183
Sunny Goyalc6205602015-05-21 20:46:33 -0700184 // We only care about the top and bottom workspace padding, which is not affected by RTL.
185 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700186 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
187 if (usedHeight > maxHeight) {
188 scale = maxHeight / usedHeight;
189 drawablePadding = 0;
190 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700191 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700192 }
193
Winson Chung93f98ea2015-03-10 16:28:47 -0700194 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700195 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700196 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
197 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700198 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700199 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700200
201 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800202 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700203 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Sunny Goyal594d76d2014-11-06 10:12:54 -0800204 searchBarSpaceHeightPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700205 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Winson Chungb3800242013-10-24 11:01:54 -0700206
207 // Calculate the actual text height
208 Paint textPaint = new Paint();
209 textPaint.setTextSize(iconTextSizePx);
210 FontMetrics fm = textPaint.getFontMetrics();
211 cellWidthPx = iconSizePx;
212 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700213 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800214 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700215
216 // Hotseat
217 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
218 hotseatCellWidthPx = iconSizePx;
219 hotseatCellHeightPx = iconSizePx;
220
221 // Folder
222 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
223 folderCellHeightPx = cellHeightPx + edgeMarginPx;
224 folderBackgroundOffset = -edgeMarginPx;
225 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700226 }
227
Winson Chungef7f8742015-06-04 17:18:17 -0700228 /**
229 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
230 */
231 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700232 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700233 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700234 int allAppsCellWidthGap =
235 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700236 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700237 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700238 (allAppsIconSizePx + allAppsCellWidthGap);
239 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700240 allAppsNumCols = numAppsCols;
241 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700242 }
243
Winson Chung69e04ea2013-12-02 14:43:44 -0800244 /** Returns the search bar top offset */
Sunny Goyalc6205602015-05-21 20:46:33 -0700245 private int getSearchBarTopOffset() {
246 if (isTablet && !isVerticalBarLayout()) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800247 return 4 * edgeMarginPx;
248 } else {
249 return 2 * edgeMarginPx;
250 }
251 }
252
Winson Chungb3800242013-10-24 11:01:54 -0700253 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700254 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700255 Rect bounds = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700256 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800257 if (isLayoutRtl) {
258 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
259 availableWidthPx, availableHeightPx - edgeMarginPx);
260 } else {
261 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
262 availableHeightPx - edgeMarginPx);
263 }
Winson Chungb3800242013-10-24 11:01:54 -0700264 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700265 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700266 // Pad the left and right of the workspace to ensure consistent spacing
267 // between all icons
Sunny Goyalc6205602015-05-21 20:46:33 -0700268 int width = isLandscape ? Math.max(widthPx, heightPx) : Math.min(widthPx, heightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700269 // XXX: If the icon size changes across orientations, we will have to take
270 // that into account here too.
271 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700272 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800273 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
274 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700275 searchBarSpaceHeightPx);
276 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800277 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
278 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700279 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
280 defaultWidgetPadding.right), searchBarSpaceHeightPx);
281 }
282 }
283 return bounds;
284 }
285
286 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700287 Rect getWorkspacePadding(boolean isLayoutRtl) {
288 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700289 Rect padding = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700290 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chungb3800242013-10-24 11:01:54 -0700291 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800292 if (isLayoutRtl) {
293 padding.set(hotseatBarHeightPx, edgeMarginPx,
294 searchBarBounds.width(), edgeMarginPx);
295 } else {
296 padding.set(searchBarBounds.width(), edgeMarginPx,
297 hotseatBarHeightPx, edgeMarginPx);
298 }
Winson Chungb3800242013-10-24 11:01:54 -0700299 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700300 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700301 // Pad the left and right of the workspace to ensure consistent spacing
302 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800303 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Sunny Goyalc6205602015-05-21 20:46:33 -0700304 int width = isLandscape
Winson Chungb3800242013-10-24 11:01:54 -0700305 ? Math.max(widthPx, heightPx)
306 : Math.min(widthPx, heightPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700307 int height = isLandscape
Winson Chung59a488a2013-12-10 12:32:14 -0800308 ? Math.max(widthPx, heightPx)
309 : Math.min(widthPx, heightPx);
310 int paddingTop = searchBarBounds.bottom;
311 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700312 int availableWidth = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
313 (inv.numColumns * gapScale * cellWidthPx)));
Winson Chung59a488a2013-12-10 12:32:14 -0800314 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700315 - (int) (2 * inv.numRows * cellHeightPx));
Winson Chung59a488a2013-12-10 12:32:14 -0800316 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
317 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700318 } else {
319 // Pad the top and bottom of the workspace with search/hotseat bar sizes
320 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
321 searchBarBounds.bottom,
322 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
323 hotseatBarHeightPx + pageIndicatorHeightPx);
324 }
325 }
326 return padding;
327 }
328
Sunny Goyalc6205602015-05-21 20:46:33 -0700329 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
330 if ((isLandscape && transposeLayoutWithOrientation) || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700331 // In landscape mode the page spacing is set to the default.
332 return defaultPageSpacingPx;
333 } else {
334 // In portrait, we want the pages spaced such that there is no
335 // overhang of the previous / next page into the current page viewport.
336 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700337 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700338 }
339 }
340
341 Rect getOverviewModeButtonBarRect() {
342 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
343 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
344 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
345 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
346 }
347
Sunny Goyalc6205602015-05-21 20:46:33 -0700348 public float getOverviewModeScale(boolean isLayoutRtl) {
349 Rect workspacePadding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700350 Rect overviewBar = getOverviewModeButtonBarRect();
351 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
352 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
353 }
354
355 // The rect returned will be extended to below the system ui that covers the workspace
Sunny Goyalcf25b522015-07-09 00:01:18 -0700356 public boolean isInHotseatRect(int x, int y) {
Winson Chungb3800242013-10-24 11:01:54 -0700357 if (isVerticalBarLayout()) {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700358 return (x >= (availableWidthPx - hotseatBarHeightPx))
359 && (y >= 0) && (y <= availableHeightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700360 } else {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700361 return (x >= 0) && (x <= availableWidthPx)
362 && (y >= (availableHeightPx - hotseatBarHeightPx));
Winson Chungb3800242013-10-24 11:01:54 -0700363 }
364 }
365
Adam Cohen2e6da152015-05-06 11:42:25 -0700366 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700367 return width / countX;
368 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700369 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700370 return height / countY;
371 }
372
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700373 /**
374 * When {@code true}, hotseat is on the bottom row when in landscape mode.
375 * If {@code false}, hotseat is on the right column when in landscape mode.
376 */
Winson Chungb3800242013-10-24 11:01:54 -0700377 boolean isVerticalBarLayout() {
378 return isLandscape && transposeLayoutWithOrientation;
379 }
380
381 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700382 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700383 }
384
Sunny Goyalc6205602015-05-21 20:46:33 -0700385 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800386 int visibleChildren = 0;
387 for (int i = 0; i < parent.getChildCount(); i++) {
388 if (parent.getChildAt(i).getVisibility() != View.GONE) {
389 visibleChildren++;
390 }
391 }
392 return visibleChildren;
393 }
394
Winson Chungb3800242013-10-24 11:01:54 -0700395 public void layout(Launcher launcher) {
396 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700397 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700398 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700399
400 // Layout the search bar space
401 View searchBar = launcher.getSearchBar();
402 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
403 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700404 // Vertical search bar space -- The search bar is fixed in the layout to be on the left
405 // of the screen regardless of RTL
Sunny Goyald9cc7802015-04-23 14:47:19 -0700406 lp.gravity = Gravity.LEFT;
Winson Chungb3800242013-10-24 11:01:54 -0700407 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800408
409 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
410 targets.setOrientation(LinearLayout.VERTICAL);
Sunny Goyald9cc7802015-04-23 14:47:19 -0700411 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
412 targetsLp.gravity = Gravity.TOP;
413 targetsLp.height = LayoutParams.WRAP_CONTENT;
414
Winson Chungb3800242013-10-24 11:01:54 -0700415 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800416 // Horizontal search bar space
Sunny Goyald9cc7802015-04-23 14:47:19 -0700417 lp.gravity = Gravity.TOP;
Winson Chungb3800242013-10-24 11:01:54 -0700418 lp.height = searchBarSpaceHeightPx;
Sunny Goyald9cc7802015-04-23 14:47:19 -0700419
420 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
421 targets.getLayoutParams().width = searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700422 }
423 searchBar.setLayoutParams(lp);
424
Winson Chungb3800242013-10-24 11:01:54 -0700425 // Layout the workspace
426 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
427 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
428 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700429 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700430 workspace.setLayoutParams(lp);
431 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700432 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700433
434 // Layout the hotseat
435 View hotseat = launcher.findViewById(R.id.hotseat);
436 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
437 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700438 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
439 // screen regardless of RTL
440 lp.gravity = Gravity.RIGHT;
Winson Chungb3800242013-10-24 11:01:54 -0700441 lp.width = hotseatBarHeightPx;
442 lp.height = LayoutParams.MATCH_PARENT;
443 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700444 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800445 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700446 lp.gravity = Gravity.BOTTOM;
447 lp.width = LayoutParams.MATCH_PARENT;
448 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800449 hotseat.setPadding(edgeMarginPx + padding.left, 0,
450 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700451 2 * edgeMarginPx);
452 } else {
453 // For phones, layout the hotseat without any bottom margin
454 // to ensure that we have space for the folders
455 lp.gravity = Gravity.BOTTOM;
456 lp.width = LayoutParams.MATCH_PARENT;
457 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000458 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
459 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700460 }
461 hotseat.setLayoutParams(lp);
462
463 // Layout the page indicators
464 View pageIndicator = launcher.findViewById(R.id.page_indicator);
465 if (pageIndicator != null) {
466 if (hasVerticalBarLayout) {
467 // Hide the page indicators when we have vertical search/hotseat
468 pageIndicator.setVisibility(View.GONE);
469 } else {
470 // Put the page indicators above the hotseat
471 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
472 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
473 lp.width = LayoutParams.WRAP_CONTENT;
474 lp.height = LayoutParams.WRAP_CONTENT;
475 lp.bottomMargin = hotseatBarHeightPx;
476 pageIndicator.setLayoutParams(lp);
477 }
478 }
479
Winson Chungb3800242013-10-24 11:01:54 -0700480 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800481 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700482 if (overviewMode != null) {
483 Rect r = getOverviewModeButtonBarRect();
484 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
485 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700486
487 int visibleChildCount = getVisibleChildCount(overviewMode);
488 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
489 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
490
491 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chungb3800242013-10-24 11:01:54 -0700492 lp.height = r.height();
493 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700494
495 if (lp.width > totalItemWidth && visibleChildCount > 1) {
496 // We have enough space. Lets add some margin too.
497 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
498 View lastChild = null;
499
500 // Set margin of all visible children except the last visible child
501 for (int i = 0; i < visibleChildCount; i++) {
502 if (lastChild != null) {
503 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
504 if (isLayoutRtl) {
505 clp.leftMargin = margin;
506 } else {
507 clp.rightMargin = margin;
508 }
509 lastChild.setLayoutParams(clp);
510 lastChild = null;
511 }
512 View thisChild = overviewMode.getChildAt(i);
513 if (thisChild.getVisibility() != View.GONE) {
514 lastChild = thisChild;
515 }
516 }
517 }
Winson Chungb3800242013-10-24 11:01:54 -0700518 }
519 }
520}