blob: b297658b4d70509ae2e84ea5fe5b83ae67b09659 [file] [log] [blame]
Winson Chungb3800242013-10-24 11:01:54 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
19import android.appwidget.AppWidgetHostView;
20import android.content.ComponentName;
21import android.content.Context;
Winson Chungb3800242013-10-24 11:01:54 -070022import android.content.res.Resources;
23import android.graphics.Paint;
24import android.graphics.Paint.FontMetrics;
25import android.graphics.Point;
Winson Chungb3800242013-10-24 11:01:54 -070026import android.graphics.Rect;
27import android.util.DisplayMetrics;
Winson Chungb3800242013-10-24 11:01:54 -070028import android.view.Gravity;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080030import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.view.ViewGroup.LayoutParams;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -070032import android.view.ViewGroup.MarginLayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070033import android.widget.FrameLayout;
Adam Cohen24ce0b32014-01-14 16:18:14 -080034import android.widget.LinearLayout;
Winson Chungb3800242013-10-24 11:01:54 -070035
Winson Chungb3800242013-10-24 11:01:54 -070036public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070037
Adam Cohen2e6da152015-05-06 11:42:25 -070038 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070039
Sunny Goyalc6205602015-05-21 20:46:33 -070040 // Device properties
41 public final boolean isTablet;
42 public final boolean isLargeTablet;
43 public final boolean isPhone;
44 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070045
Sunny Goyalc6205602015-05-21 20:46:33 -070046 // Device properties in current orientation
47 public final boolean isLandscape;
48 public final int widthPx;
49 public final int heightPx;
50 public final int availableWidthPx;
51 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070052 /**
53 * The maximum amount of left/right workspace padding as a percentage of the screen width.
54 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
55 * 7% of the screen width can be used as right padding.
56 */
57 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070058
Sunny Goyalc6205602015-05-21 20:46:33 -070059 // Overview mode
60 private final int overviewModeMinIconZoneHeightPx;
61 private final int overviewModeMaxIconZoneHeightPx;
62 private final int overviewModeBarItemWidthPx;
63 private final int overviewModeBarSpacerWidthPx;
64 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070065
Sunny Goyalc6205602015-05-21 20:46:33 -070066 // Workspace
67 private int desiredWorkspaceLeftRightMarginPx;
68 public final int edgeMarginPx;
69 public final Rect defaultWidgetPadding;
70 private final int pageIndicatorHeightPx;
71 private final int defaultPageSpacingPx;
72 private float dragViewScale;
73
74 // Workspace icons
75 public int iconSizePx;
76 public int iconTextSizePx;
77 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070078 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070079
Adam Cohen59400422014-03-05 18:07:04 -080080 public int cellWidthPx;
81 public int cellHeightPx;
82
Sunny Goyalc6205602015-05-21 20:46:33 -070083 // Folder
84 public int folderBackgroundOffset;
85 public int folderIconSizePx;
86 public int folderCellWidthPx;
87 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070088
Sunny Goyalc6205602015-05-21 20:46:33 -070089 // Hotseat
90 public int hotseatCellWidthPx;
91 public int hotseatCellHeightPx;
92 public int hotseatIconSizePx;
93 private int hotseatBarHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -070094
Sunny Goyalc6205602015-05-21 20:46:33 -070095 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -070096 public int allAppsNumCols;
97 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -070098 public int allAppsButtonVisualSize;
99 public final int allAppsIconSizePx;
100 public final int allAppsIconTextSizePx;
Winson Chung59a488a2013-12-10 12:32:14 -0800101
Sunny Goyalc6205602015-05-21 20:46:33 -0700102 // QSB
103 private int searchBarSpaceWidthPx;
104 private int searchBarSpaceHeightPx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700105
Sunny Goyalc6205602015-05-21 20:46:33 -0700106 public DeviceProfile(Context context, InvariantDeviceProfile inv,
107 Point minSize, Point maxSize,
108 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700109
Adam Cohen2e6da152015-05-06 11:42:25 -0700110 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700111 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700112
Adam Cohen2e6da152015-05-06 11:42:25 -0700113 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700114 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700115
Sunny Goyalc6205602015-05-21 20:46:33 -0700116 // Constants from resources
117 isTablet = res.getBoolean(R.bool.is_tablet);
118 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
119 isPhone = !isTablet && !isLargeTablet;
120
121 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700122 transposeLayoutWithOrientation =
123 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700124
125 ComponentName cn = new ComponentName(context.getPackageName(),
126 this.getClass().getName());
127 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
128 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
129 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
130 pageIndicatorHeightPx =
131 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
132 defaultPageSpacingPx =
133 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700134 overviewModeMinIconZoneHeightPx =
135 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
136 overviewModeMaxIconZoneHeightPx =
137 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800138 overviewModeBarItemWidthPx =
139 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
140 overviewModeBarSpacerWidthPx =
141 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700142 overviewModeIconZoneRatio =
143 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700144 iconDrawablePaddingOriginalPx =
145 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700146
Adam Cohen2e6da152015-05-06 11:42:25 -0700147 // AllApps uses the original non-scaled icon text size
148 allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
149
150 // AllApps uses the original non-scaled icon size
151 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700152
Sunny Goyalc6205602015-05-21 20:46:33 -0700153 // Determine sizes.
154 widthPx = width;
155 heightPx = height;
156 if (isLandscape) {
157 availableWidthPx = maxSize.x;
158 availableHeightPx = minSize.y;
159 } else {
160 availableWidthPx = minSize.x;
161 availableHeightPx = maxSize.y;
162 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700163
Winson Chungb3800242013-10-24 11:01:54 -0700164 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700165 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700166 computeAllAppsButtonSize(context);
167 }
168
169 /**
170 * Determine the exact visual footprint of the all apps button, taking into account scaling
171 * and internal padding of the drawable.
172 */
173 private void computeAllAppsButtonSize(Context context) {
174 Resources res = context.getResources();
175 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700176 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
177 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700178 }
179
Sunny Goyalc6205602015-05-21 20:46:33 -0700180 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700181 // Check to see if the icons fit in the new available height. If not, then we need to
182 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700183 float scale = 1f;
184 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700185 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700186 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800187
Sunny Goyalc6205602015-05-21 20:46:33 -0700188 // We only care about the top and bottom workspace padding, which is not affected by RTL.
189 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700190 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
191 if (usedHeight > maxHeight) {
192 scale = maxHeight / usedHeight;
193 drawablePadding = 0;
194 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700195 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700196 }
197
Winson Chung93f98ea2015-03-10 16:28:47 -0700198 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700199 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700200 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
201 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700202 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700203 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700204
205 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800206 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700207 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Sunny Goyal594d76d2014-11-06 10:12:54 -0800208 searchBarSpaceHeightPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700209 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Winson Chungb3800242013-10-24 11:01:54 -0700210
211 // Calculate the actual text height
212 Paint textPaint = new Paint();
213 textPaint.setTextSize(iconTextSizePx);
214 FontMetrics fm = textPaint.getFontMetrics();
215 cellWidthPx = iconSizePx;
216 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700217 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800218 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700219
220 // Hotseat
221 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
222 hotseatCellWidthPx = iconSizePx;
223 hotseatCellHeightPx = iconSizePx;
224
225 // Folder
226 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
227 folderCellHeightPx = cellHeightPx + edgeMarginPx;
228 folderBackgroundOffset = -edgeMarginPx;
229 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700230 }
231
Winson Chungef7f8742015-06-04 17:18:17 -0700232 /**
233 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
234 */
235 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700236 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700237 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700238 int allAppsCellWidthGap =
239 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700240 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700241 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700242 (allAppsIconSizePx + allAppsCellWidthGap);
243 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700244 allAppsNumCols = numAppsCols;
245 allAppsNumPredictiveCols = numPredictiveAppCols;
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();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700260 if (isVerticalBarLayout()) {
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
Winson Chung4f3bfad2015-07-13 11:14:30 -0700272 int width = getCurrentWidth();
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();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700294 if (isVerticalBarLayout()) {
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;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700308 int width = getCurrentWidth();
309 int height = getCurrentHeight();
Winson Chung59a488a2013-12-10 12:32:14 -0800310 int paddingTop = searchBarBounds.bottom;
311 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700312 // The amount of screen space available for left/right padding.
313 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
314 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
315 availablePaddingX = (int) Math.min(availablePaddingX,
316 width * MAX_HORIZONTAL_PADDING_PERCENT);
317 int availablePaddingY = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700318 - (int) (2 * inv.numRows * cellHeightPx));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700319 padding.set(availablePaddingX / 2, paddingTop + availablePaddingY / 2,
320 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700321 } else {
322 // Pad the top and bottom of the workspace with search/hotseat bar sizes
323 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
324 searchBarBounds.bottom,
325 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
326 hotseatBarHeightPx + pageIndicatorHeightPx);
327 }
328 }
329 return padding;
330 }
331
Sunny Goyalc6205602015-05-21 20:46:33 -0700332 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700333 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700334 // In landscape mode the page spacing is set to the default.
335 return defaultPageSpacingPx;
336 } else {
337 // In portrait, we want the pages spaced such that there is no
338 // overhang of the previous / next page into the current page viewport.
339 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700340 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700341 }
342 }
343
Winson Chung4f7eb402015-07-10 18:00:48 -0700344 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700345 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
346 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
347 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700348 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700349 }
350
351 // The rect returned will be extended to below the system ui that covers the workspace
352 Rect getHotseatRect() {
353 if (isVerticalBarLayout()) {
354 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
355 Integer.MAX_VALUE, availableHeightPx);
356 } else {
357 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
358 availableWidthPx, Integer.MAX_VALUE);
359 }
360 }
361
Adam Cohen2e6da152015-05-06 11:42:25 -0700362 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700363 return width / countX;
364 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700365 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700366 return height / countY;
367 }
368
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700369 /**
370 * When {@code true}, hotseat is on the bottom row when in landscape mode.
371 * If {@code false}, hotseat is on the right column when in landscape mode.
372 */
Winson Chungb3800242013-10-24 11:01:54 -0700373 boolean isVerticalBarLayout() {
374 return isLandscape && transposeLayoutWithOrientation;
375 }
376
377 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700378 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700379 }
380
Sunny Goyalc6205602015-05-21 20:46:33 -0700381 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800382 int visibleChildren = 0;
383 for (int i = 0; i < parent.getChildCount(); i++) {
384 if (parent.getChildAt(i).getVisibility() != View.GONE) {
385 visibleChildren++;
386 }
387 }
388 return visibleChildren;
389 }
390
Winson Chungb3800242013-10-24 11:01:54 -0700391 public void layout(Launcher launcher) {
392 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700393 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700394 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700395
396 // Layout the search bar space
Winson Chung006ee262015-08-03 14:40:11 -0700397 View searchBar = launcher.getSearchDropTargetBar();
Winson Chungb3800242013-10-24 11:01:54 -0700398 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
399 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700400 // Vertical search bar space -- The search bar is fixed in the layout to be on the left
401 // of the screen regardless of RTL
Sunny Goyald9cc7802015-04-23 14:47:19 -0700402 lp.gravity = Gravity.LEFT;
Winson Chungb3800242013-10-24 11:01:54 -0700403 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800404
405 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
406 targets.setOrientation(LinearLayout.VERTICAL);
Sunny Goyald9cc7802015-04-23 14:47:19 -0700407 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
408 targetsLp.gravity = Gravity.TOP;
409 targetsLp.height = LayoutParams.WRAP_CONTENT;
410
Winson Chungb3800242013-10-24 11:01:54 -0700411 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800412 // Horizontal search bar space
Sunny Goyald9cc7802015-04-23 14:47:19 -0700413 lp.gravity = Gravity.TOP;
Winson Chungb3800242013-10-24 11:01:54 -0700414 lp.height = searchBarSpaceHeightPx;
Sunny Goyald9cc7802015-04-23 14:47:19 -0700415
416 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
417 targets.getLayoutParams().width = searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700418 }
419 searchBar.setLayoutParams(lp);
420
Winson Chungb3800242013-10-24 11:01:54 -0700421 // Layout the workspace
422 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
423 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
424 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700425 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700426 workspace.setLayoutParams(lp);
427 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700428 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700429
430 // Layout the hotseat
431 View hotseat = launcher.findViewById(R.id.hotseat);
432 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700433 // We want the edges of the hotseat to line up with the edges of the workspace, but the
434 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
435 // this, we pad the left and right of the hotseat with half of the difference of a workspace
436 // cell vs a hotseat cell.
437 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
438 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
439 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700440 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700441 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
442 // screen regardless of RTL
443 lp.gravity = Gravity.RIGHT;
Winson Chungb3800242013-10-24 11:01:54 -0700444 lp.width = hotseatBarHeightPx;
445 lp.height = LayoutParams.MATCH_PARENT;
446 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700447 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800448 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700449 lp.gravity = Gravity.BOTTOM;
450 lp.width = LayoutParams.MATCH_PARENT;
451 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700452 hotseat.findViewById(R.id.layout).setPadding(
453 hotseatAdjustment + padding.left, 0,
454 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700455 } else {
456 // For phones, layout the hotseat without any bottom margin
457 // to ensure that we have space for the folders
458 lp.gravity = Gravity.BOTTOM;
459 lp.width = LayoutParams.MATCH_PARENT;
460 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700461 hotseat.findViewById(R.id.layout).setPadding(
462 hotseatAdjustment + padding.left, 0,
463 hotseatAdjustment + padding.right, 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) {
Winson Chung4f7eb402015-07-10 18:00:48 -0700487 int overviewButtonBarHeight = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700488 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 Chung4f7eb402015-07-10 18:00:48 -0700496 lp.height = overviewButtonBarHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700497 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 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700524
525 private int getCurrentWidth() {
526 return isLandscape
527 ? Math.max(widthPx, heightPx)
528 : Math.min(widthPx, heightPx);
529 }
530
531 private int getCurrentHeight() {
532 return isLandscape
533 ? Math.min(widthPx, heightPx)
534 : Math.max(widthPx, heightPx);
535 }
Winson Chungb3800242013-10-24 11:01:54 -0700536}