blob: 82be409ddd3d7c8405aebd162c0df2e8f247fc22 [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;
226
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700227 updateAppsViewNumCols(res, 0);
Winson Chung0f785722015-04-08 10:27:49 -0700228 }
229
230 public boolean updateAppsViewNumCols(Resources res, int containerWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700231 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700232 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
233 int allAppsCellPaddingPx =
234 res.getDimensionPixelSize(R.dimen.all_apps_icon_left_right_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700235 int availableAppsWidthPx = (containerWidth > 0) ? containerWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700236 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700237 (allAppsIconSizePx + 2 * allAppsCellPaddingPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700238 int numPredictiveAppCols = isPhone ? 4 : numAppsCols;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700239 if ((numAppsCols != allAppsNumCols) ||
240 (numPredictiveAppCols != allAppsNumPredictiveCols)) {
241 allAppsNumCols = numAppsCols;
242 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chung0f785722015-04-08 10:27:49 -0700243 return true;
244 }
245 return false;
Winson Chungb3800242013-10-24 11:01:54 -0700246 }
247
Winson Chung69e04ea2013-12-02 14:43:44 -0800248 /** Returns the search bar top offset */
Sunny Goyalc6205602015-05-21 20:46:33 -0700249 private int getSearchBarTopOffset() {
250 if (isTablet && !isVerticalBarLayout()) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800251 return 4 * edgeMarginPx;
252 } else {
253 return 2 * edgeMarginPx;
254 }
255 }
256
Winson Chungb3800242013-10-24 11:01:54 -0700257 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700258 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700259 Rect bounds = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700260 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800261 if (isLayoutRtl) {
262 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
263 availableWidthPx, availableHeightPx - edgeMarginPx);
264 } else {
265 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
266 availableHeightPx - edgeMarginPx);
267 }
Winson Chungb3800242013-10-24 11:01:54 -0700268 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700269 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700270 // Pad the left and right of the workspace to ensure consistent spacing
271 // between all icons
Sunny Goyalc6205602015-05-21 20:46:33 -0700272 int width = isLandscape ? Math.max(widthPx, heightPx) : Math.min(widthPx, heightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700273 // XXX: If the icon size changes across orientations, we will have to take
274 // that into account here too.
275 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700276 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800277 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
278 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700279 searchBarSpaceHeightPx);
280 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800281 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
282 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700283 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
284 defaultWidgetPadding.right), searchBarSpaceHeightPx);
285 }
286 }
287 return bounds;
288 }
289
290 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700291 Rect getWorkspacePadding(boolean isLayoutRtl) {
292 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700293 Rect padding = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700294 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chungb3800242013-10-24 11:01:54 -0700295 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800296 if (isLayoutRtl) {
297 padding.set(hotseatBarHeightPx, edgeMarginPx,
298 searchBarBounds.width(), edgeMarginPx);
299 } else {
300 padding.set(searchBarBounds.width(), edgeMarginPx,
301 hotseatBarHeightPx, edgeMarginPx);
302 }
Winson Chungb3800242013-10-24 11:01:54 -0700303 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700304 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700305 // Pad the left and right of the workspace to ensure consistent spacing
306 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800307 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Sunny Goyalc6205602015-05-21 20:46:33 -0700308 int width = isLandscape
Winson Chungb3800242013-10-24 11:01:54 -0700309 ? Math.max(widthPx, heightPx)
310 : Math.min(widthPx, heightPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700311 int height = isLandscape
Winson Chung59a488a2013-12-10 12:32:14 -0800312 ? Math.max(widthPx, heightPx)
313 : Math.min(widthPx, heightPx);
314 int paddingTop = searchBarBounds.bottom;
315 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700316 int availableWidth = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
317 (inv.numColumns * gapScale * cellWidthPx)));
Winson Chung59a488a2013-12-10 12:32:14 -0800318 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700319 - (int) (2 * inv.numRows * cellHeightPx));
Winson Chung59a488a2013-12-10 12:32:14 -0800320 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
321 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700322 } else {
323 // Pad the top and bottom of the workspace with search/hotseat bar sizes
324 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
325 searchBarBounds.bottom,
326 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
327 hotseatBarHeightPx + pageIndicatorHeightPx);
328 }
329 }
330 return padding;
331 }
332
Sunny Goyalc6205602015-05-21 20:46:33 -0700333 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
334 if ((isLandscape && transposeLayoutWithOrientation) || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700335 // In landscape mode the page spacing is set to the default.
336 return defaultPageSpacingPx;
337 } else {
338 // In portrait, we want the pages spaced such that there is no
339 // overhang of the previous / next page into the current page viewport.
340 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700341 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700342 }
343 }
344
345 Rect getOverviewModeButtonBarRect() {
346 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
347 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
348 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
349 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
350 }
351
Sunny Goyalc6205602015-05-21 20:46:33 -0700352 public float getOverviewModeScale(boolean isLayoutRtl) {
353 Rect workspacePadding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700354 Rect overviewBar = getOverviewModeButtonBarRect();
355 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
356 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
357 }
358
359 // The rect returned will be extended to below the system ui that covers the workspace
360 Rect getHotseatRect() {
361 if (isVerticalBarLayout()) {
362 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
363 Integer.MAX_VALUE, availableHeightPx);
364 } else {
365 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
366 availableWidthPx, Integer.MAX_VALUE);
367 }
368 }
369
Adam Cohen2e6da152015-05-06 11:42:25 -0700370 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700371 return width / countX;
372 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700373 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700374 return height / countY;
375 }
376
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700377 /**
378 * When {@code true}, hotseat is on the bottom row when in landscape mode.
379 * If {@code false}, hotseat is on the right column when in landscape mode.
380 */
Winson Chungb3800242013-10-24 11:01:54 -0700381 boolean isVerticalBarLayout() {
382 return isLandscape && transposeLayoutWithOrientation;
383 }
384
385 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700386 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700387 }
388
Sunny Goyalc6205602015-05-21 20:46:33 -0700389 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800390 int visibleChildren = 0;
391 for (int i = 0; i < parent.getChildCount(); i++) {
392 if (parent.getChildAt(i).getVisibility() != View.GONE) {
393 visibleChildren++;
394 }
395 }
396 return visibleChildren;
397 }
398
Winson Chungb3800242013-10-24 11:01:54 -0700399 public void layout(Launcher launcher) {
400 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700401 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700402 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700403
404 // Layout the search bar space
405 View searchBar = launcher.getSearchBar();
406 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
407 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700408 // Vertical search bar space -- The search bar is fixed in the layout to be on the left
409 // of the screen regardless of RTL
Sunny Goyald9cc7802015-04-23 14:47:19 -0700410 lp.gravity = Gravity.LEFT;
Winson Chungb3800242013-10-24 11:01:54 -0700411 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800412
413 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
414 targets.setOrientation(LinearLayout.VERTICAL);
Sunny Goyald9cc7802015-04-23 14:47:19 -0700415 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
416 targetsLp.gravity = Gravity.TOP;
417 targetsLp.height = LayoutParams.WRAP_CONTENT;
418
Winson Chungb3800242013-10-24 11:01:54 -0700419 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800420 // Horizontal search bar space
Sunny Goyald9cc7802015-04-23 14:47:19 -0700421 lp.gravity = Gravity.TOP;
Winson Chungb3800242013-10-24 11:01:54 -0700422 lp.height = searchBarSpaceHeightPx;
Sunny Goyald9cc7802015-04-23 14:47:19 -0700423
424 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
425 targets.getLayoutParams().width = searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700426 }
427 searchBar.setLayoutParams(lp);
428
Winson Chungb3800242013-10-24 11:01:54 -0700429 // Layout the workspace
430 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
431 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
432 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700433 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700434 workspace.setLayoutParams(lp);
435 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700436 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700437
438 // Layout the hotseat
439 View hotseat = launcher.findViewById(R.id.hotseat);
440 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
441 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700442 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
443 // screen regardless of RTL
444 lp.gravity = Gravity.RIGHT;
Winson Chungb3800242013-10-24 11:01:54 -0700445 lp.width = hotseatBarHeightPx;
446 lp.height = LayoutParams.MATCH_PARENT;
447 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700448 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800449 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700450 lp.gravity = Gravity.BOTTOM;
451 lp.width = LayoutParams.MATCH_PARENT;
452 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800453 hotseat.setPadding(edgeMarginPx + padding.left, 0,
454 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700455 2 * edgeMarginPx);
456 } else {
457 // For phones, layout the hotseat without any bottom margin
458 // to ensure that we have space for the folders
459 lp.gravity = Gravity.BOTTOM;
460 lp.width = LayoutParams.MATCH_PARENT;
461 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000462 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
463 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700464 }
465 hotseat.setLayoutParams(lp);
466
467 // Layout the page indicators
468 View pageIndicator = launcher.findViewById(R.id.page_indicator);
469 if (pageIndicator != null) {
470 if (hasVerticalBarLayout) {
471 // Hide the page indicators when we have vertical search/hotseat
472 pageIndicator.setVisibility(View.GONE);
473 } else {
474 // Put the page indicators above the hotseat
475 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
476 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
477 lp.width = LayoutParams.WRAP_CONTENT;
478 lp.height = LayoutParams.WRAP_CONTENT;
479 lp.bottomMargin = hotseatBarHeightPx;
480 pageIndicator.setLayoutParams(lp);
481 }
482 }
483
Winson Chungb3800242013-10-24 11:01:54 -0700484 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800485 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700486 if (overviewMode != null) {
487 Rect r = getOverviewModeButtonBarRect();
488 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
489 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700490
491 int visibleChildCount = getVisibleChildCount(overviewMode);
492 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
493 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
494
495 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chungb3800242013-10-24 11:01:54 -0700496 lp.height = r.height();
497 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700498
499 if (lp.width > totalItemWidth && visibleChildCount > 1) {
500 // We have enough space. Lets add some margin too.
501 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
502 View lastChild = null;
503
504 // Set margin of all visible children except the last visible child
505 for (int i = 0; i < visibleChildCount; i++) {
506 if (lastChild != null) {
507 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
508 if (isLayoutRtl) {
509 clp.leftMargin = margin;
510 } else {
511 clp.rightMargin = margin;
512 }
513 lastChild.setLayoutParams(clp);
514 lastChild = null;
515 }
516 View thisChild = overviewMode.getChildAt(i);
517 if (thisChild.getVisibility() != View.GONE) {
518 lastChild = thisChild;
519 }
520 }
521 }
Winson Chungb3800242013-10-24 11:01:54 -0700522 }
523 }
524}