blob: 66cf2dfea80848f319197bf330c6dac39bf864d3 [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;
Winson Chungb3800242013-10-24 11:01:54 -070059
Sunny Goyalc6205602015-05-21 20:46:33 -070060 // Workspace
61 private int desiredWorkspaceLeftRightMarginPx;
62 public final int edgeMarginPx;
63 public final Rect defaultWidgetPadding;
64 private final int pageIndicatorHeightPx;
65 private final int defaultPageSpacingPx;
66 private float dragViewScale;
67
68 // Workspace icons
69 public int iconSizePx;
70 public int iconTextSizePx;
71 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070072 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070073
Adam Cohen59400422014-03-05 18:07:04 -080074 public int cellWidthPx;
75 public int cellHeightPx;
76
Sunny Goyalc6205602015-05-21 20:46:33 -070077 // Folder
78 public int folderBackgroundOffset;
79 public int folderIconSizePx;
80 public int folderCellWidthPx;
81 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070082
Sunny Goyalc6205602015-05-21 20:46:33 -070083 // Hotseat
84 public int hotseatCellWidthPx;
85 public int hotseatCellHeightPx;
86 public int hotseatIconSizePx;
87 private int hotseatBarHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -070088
Sunny Goyalc6205602015-05-21 20:46:33 -070089 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -070090 public int allAppsNumCols;
91 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -070092 public int allAppsButtonVisualSize;
93 public final int allAppsIconSizePx;
94 public final int allAppsIconTextSizePx;
Winson Chung59a488a2013-12-10 12:32:14 -080095
Sunny Goyalc6205602015-05-21 20:46:33 -070096 // QSB
97 private int searchBarSpaceWidthPx;
98 private int searchBarSpaceHeightPx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -070099
Sunny Goyalc6205602015-05-21 20:46:33 -0700100 public DeviceProfile(Context context, InvariantDeviceProfile inv,
101 Point minSize, Point maxSize,
102 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700103
Adam Cohen2e6da152015-05-06 11:42:25 -0700104 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700105 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700106
Adam Cohen2e6da152015-05-06 11:42:25 -0700107 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700108 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700109
Sunny Goyalc6205602015-05-21 20:46:33 -0700110 // Constants from resources
111 isTablet = res.getBoolean(R.bool.is_tablet);
112 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
113 isPhone = !isTablet && !isLargeTablet;
114
115 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700116 transposeLayoutWithOrientation =
117 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700118
119 ComponentName cn = new ComponentName(context.getPackageName(),
120 this.getClass().getName());
121 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
122 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
123 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
124 pageIndicatorHeightPx =
125 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
126 defaultPageSpacingPx =
127 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700128 overviewModeMinIconZoneHeightPx =
129 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
130 overviewModeMaxIconZoneHeightPx =
131 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800132 overviewModeBarItemWidthPx =
133 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
134 overviewModeBarSpacerWidthPx =
135 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700136 overviewModeIconZoneRatio =
137 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700138 iconDrawablePaddingOriginalPx =
139 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700140
Adam Cohen2e6da152015-05-06 11:42:25 -0700141 // AllApps uses the original non-scaled icon text size
142 allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
143
144 // AllApps uses the original non-scaled icon size
145 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700146
Sunny Goyalc6205602015-05-21 20:46:33 -0700147 // Determine sizes.
148 widthPx = width;
149 heightPx = height;
150 if (isLandscape) {
151 availableWidthPx = maxSize.x;
152 availableHeightPx = minSize.y;
153 } else {
154 availableWidthPx = minSize.x;
155 availableHeightPx = maxSize.y;
156 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700157
Winson Chungb3800242013-10-24 11:01:54 -0700158 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700159 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700160 computeAllAppsButtonSize(context);
161 }
162
163 /**
164 * Determine the exact visual footprint of the all apps button, taking into account scaling
165 * and internal padding of the drawable.
166 */
167 private void computeAllAppsButtonSize(Context context) {
168 Resources res = context.getResources();
169 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700170 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding));
Winson Chungb3800242013-10-24 11:01:54 -0700171 }
172
Sunny Goyalc6205602015-05-21 20:46:33 -0700173 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700174 // Check to see if the icons fit in the new available height. If not, then we need to
175 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700176 float scale = 1f;
177 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700178 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700179 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800180
Sunny Goyalc6205602015-05-21 20:46:33 -0700181 // We only care about the top and bottom workspace padding, which is not affected by RTL.
182 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700183 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
184 if (usedHeight > maxHeight) {
185 scale = maxHeight / usedHeight;
186 drawablePadding = 0;
187 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700188 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700189 }
190
Winson Chung93f98ea2015-03-10 16:28:47 -0700191 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700192 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700193 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
194 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700195 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700196 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700197
198 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800199 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700200 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Sunny Goyal594d76d2014-11-06 10:12:54 -0800201 searchBarSpaceHeightPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700202 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Winson Chungb3800242013-10-24 11:01:54 -0700203
204 // Calculate the actual text height
205 Paint textPaint = new Paint();
206 textPaint.setTextSize(iconTextSizePx);
207 FontMetrics fm = textPaint.getFontMetrics();
208 cellWidthPx = iconSizePx;
209 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700210 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800211 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700212
213 // Hotseat
214 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
215 hotseatCellWidthPx = iconSizePx;
216 hotseatCellHeightPx = iconSizePx;
217
218 // Folder
219 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
220 folderCellHeightPx = cellHeightPx + edgeMarginPx;
221 folderBackgroundOffset = -edgeMarginPx;
222 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700223 }
224
Winson Chungef7f8742015-06-04 17:18:17 -0700225 /**
226 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
227 */
228 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700229 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700230 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700231 int allAppsCellWidthGap =
232 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700233 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700234 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700235 (allAppsIconSizePx + allAppsCellWidthGap);
236 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700237 allAppsNumCols = numAppsCols;
238 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700239 }
240
Winson Chung69e04ea2013-12-02 14:43:44 -0800241 /** Returns the search bar top offset */
Sunny Goyalc6205602015-05-21 20:46:33 -0700242 private int getSearchBarTopOffset() {
243 if (isTablet && !isVerticalBarLayout()) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800244 return 4 * edgeMarginPx;
245 } else {
246 return 2 * edgeMarginPx;
247 }
248 }
249
Winson Chungb3800242013-10-24 11:01:54 -0700250 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700251 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700252 Rect bounds = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700253 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800254 if (isLayoutRtl) {
255 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
256 availableWidthPx, availableHeightPx - edgeMarginPx);
257 } else {
258 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
259 availableHeightPx - edgeMarginPx);
260 }
Winson Chungb3800242013-10-24 11:01:54 -0700261 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700262 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700263 // Pad the left and right of the workspace to ensure consistent spacing
264 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700265 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700266 // XXX: If the icon size changes across orientations, we will have to take
267 // that into account here too.
268 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700269 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800270 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
271 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700272 searchBarSpaceHeightPx);
273 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800274 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
275 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700276 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
277 defaultWidgetPadding.right), searchBarSpaceHeightPx);
278 }
279 }
280 return bounds;
281 }
282
Sunny Goyal756cd262015-08-20 12:33:21 -0700283 public Point getCellSize() {
284 Point result = new Point();
285 // Since we are only concerned with the overall padding, layout direction does
286 // not matter.
287 Rect padding = getWorkspacePadding(false /* isLayoutRtl */ );
288 result.x = calculateCellWidth(availableWidthPx - padding.left - padding.right,
289 inv.numColumns);
290 result.y = calculateCellHeight(availableHeightPx - padding.top - padding.bottom,
291 inv.numRows);
292 return result;
293 }
294
Winson Chungb3800242013-10-24 11:01:54 -0700295 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700296 Rect getWorkspacePadding(boolean isLayoutRtl) {
297 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700298 Rect padding = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700299 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chungb3800242013-10-24 11:01:54 -0700300 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800301 if (isLayoutRtl) {
302 padding.set(hotseatBarHeightPx, edgeMarginPx,
303 searchBarBounds.width(), edgeMarginPx);
304 } else {
305 padding.set(searchBarBounds.width(), edgeMarginPx,
306 hotseatBarHeightPx, edgeMarginPx);
307 }
Winson Chungb3800242013-10-24 11:01:54 -0700308 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700309 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700310 // Pad the left and right of the workspace to ensure consistent spacing
311 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800312 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700313 int width = getCurrentWidth();
314 int height = getCurrentHeight();
Winson Chung59a488a2013-12-10 12:32:14 -0800315 int paddingTop = searchBarBounds.bottom;
316 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700317 int availableWidth = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
318 (inv.numColumns * gapScale * cellWidthPx)));
Winson Chung59a488a2013-12-10 12:32:14 -0800319 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700320 - (int) (2 * inv.numRows * cellHeightPx));
Winson Chung59a488a2013-12-10 12:32:14 -0800321 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
322 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700323 } else {
324 // Pad the top and bottom of the workspace with search/hotseat bar sizes
325 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
326 searchBarBounds.bottom,
327 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
328 hotseatBarHeightPx + pageIndicatorHeightPx);
329 }
330 }
331 return padding;
332 }
333
Sunny Goyalc6205602015-05-21 20:46:33 -0700334 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
335 if ((isLandscape && transposeLayoutWithOrientation) || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700336 // In landscape mode the page spacing is set to the default.
337 return defaultPageSpacingPx;
338 } else {
339 // In portrait, we want the pages spaced such that there is no
340 // overhang of the previous / next page into the current page viewport.
341 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700342 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700343 }
344 }
345
Winson Chung4f7eb402015-07-10 18:00:48 -0700346 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700347 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
348 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
349 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700350 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700351 }
352
353 // The rect returned will be extended to below the system ui that covers the workspace
Sunny Goyalcf25b522015-07-09 00:01:18 -0700354 public boolean isInHotseatRect(int x, int y) {
Winson Chungb3800242013-10-24 11:01:54 -0700355 if (isVerticalBarLayout()) {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700356 return (x >= (availableWidthPx - hotseatBarHeightPx))
357 && (y >= 0) && (y <= availableHeightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700358 } else {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700359 return (x >= 0) && (x <= availableWidthPx)
360 && (y >= (availableHeightPx - hotseatBarHeightPx));
Winson Chungb3800242013-10-24 11:01:54 -0700361 }
362 }
363
Adam Cohen2e6da152015-05-06 11:42:25 -0700364 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700365 return width / countX;
366 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700367 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700368 return height / countY;
369 }
370
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700371 /**
372 * When {@code true}, hotseat is on the bottom row when in landscape mode.
373 * If {@code false}, hotseat is on the right column when in landscape mode.
374 */
Winson Chungb3800242013-10-24 11:01:54 -0700375 boolean isVerticalBarLayout() {
376 return isLandscape && transposeLayoutWithOrientation;
377 }
378
379 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700380 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700381 }
382
Sunny Goyalc6205602015-05-21 20:46:33 -0700383 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800384 int visibleChildren = 0;
385 for (int i = 0; i < parent.getChildCount(); i++) {
386 if (parent.getChildAt(i).getVisibility() != View.GONE) {
387 visibleChildren++;
388 }
389 }
390 return visibleChildren;
391 }
392
Winson Chungb3800242013-10-24 11:01:54 -0700393 public void layout(Launcher launcher) {
394 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700395 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700396 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700397
398 // Layout the search bar space
Winson Chung006ee262015-08-03 14:40:11 -0700399 View searchBar = launcher.getSearchDropTargetBar();
Tony Wickham34d2c912015-09-11 09:27:58 -0700400 lp = getDropTargetBarLayoutParams(hasVerticalBarLayout, searchBar, Gravity.TOP);
Winson Chungb3800242013-10-24 11:01:54 -0700401 searchBar.setLayoutParams(lp);
402
Tony Wickham34d2c912015-09-11 09:27:58 -0700403 // Layout the app info bar space
404 View appInfoBar = launcher.getAppInfoDropTargetBar();
405 lp = getDropTargetBarLayoutParams(hasVerticalBarLayout, appInfoBar, Gravity.BOTTOM);
406 lp.bottomMargin = hotseatBarHeightPx;
407 appInfoBar.setLayoutParams(lp);
408
Winson Chungb3800242013-10-24 11:01:54 -0700409 // Layout the workspace
410 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
411 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
412 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700413 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700414 workspace.setLayoutParams(lp);
415 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700416 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700417
418 // Layout the hotseat
419 View hotseat = launcher.findViewById(R.id.hotseat);
420 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
421 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700422 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
423 // screen regardless of RTL
424 lp.gravity = Gravity.RIGHT;
Winson Chungb3800242013-10-24 11:01:54 -0700425 lp.width = hotseatBarHeightPx;
426 lp.height = LayoutParams.MATCH_PARENT;
427 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700428 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800429 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700430 lp.gravity = Gravity.BOTTOM;
431 lp.width = LayoutParams.MATCH_PARENT;
432 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800433 hotseat.setPadding(edgeMarginPx + padding.left, 0,
434 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700435 2 * edgeMarginPx);
436 } else {
437 // For phones, layout the hotseat without any bottom margin
438 // to ensure that we have space for the folders
439 lp.gravity = Gravity.BOTTOM;
440 lp.width = LayoutParams.MATCH_PARENT;
441 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000442 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
443 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700444 }
445 hotseat.setLayoutParams(lp);
446
447 // Layout the page indicators
448 View pageIndicator = launcher.findViewById(R.id.page_indicator);
449 if (pageIndicator != null) {
450 if (hasVerticalBarLayout) {
451 // Hide the page indicators when we have vertical search/hotseat
452 pageIndicator.setVisibility(View.GONE);
453 } else {
454 // Put the page indicators above the hotseat
455 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
456 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
457 lp.width = LayoutParams.WRAP_CONTENT;
458 lp.height = LayoutParams.WRAP_CONTENT;
459 lp.bottomMargin = hotseatBarHeightPx;
460 pageIndicator.setLayoutParams(lp);
461 }
462 }
463
Winson Chungb3800242013-10-24 11:01:54 -0700464 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800465 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700466 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700467 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
468 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700469
470 int visibleChildCount = getVisibleChildCount(overviewMode);
471 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
472 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
473
474 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700475 lp.height = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700476 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700477
478 if (lp.width > totalItemWidth && visibleChildCount > 1) {
479 // We have enough space. Lets add some margin too.
480 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
481 View lastChild = null;
482
483 // Set margin of all visible children except the last visible child
484 for (int i = 0; i < visibleChildCount; i++) {
485 if (lastChild != null) {
486 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
487 if (isLayoutRtl) {
488 clp.leftMargin = margin;
489 } else {
490 clp.rightMargin = margin;
491 }
492 lastChild.setLayoutParams(clp);
493 lastChild = null;
494 }
495 View thisChild = overviewMode.getChildAt(i);
496 if (thisChild.getVisibility() != View.GONE) {
497 lastChild = thisChild;
498 }
499 }
500 }
Winson Chungb3800242013-10-24 11:01:54 -0700501 }
502 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700503
Tony Wickham34d2c912015-09-11 09:27:58 -0700504 private FrameLayout.LayoutParams getDropTargetBarLayoutParams(boolean hasVerticalBarLayout,
505 View dropTargetBar, int verticalGravity) {
506 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) dropTargetBar.getLayoutParams();
507 if (hasVerticalBarLayout) {
508 // Vertical drop target bar space -- The drop target bar is fixed in the layout to be on
509 // the left of the screen regardless of RTL
510 lp.gravity = Gravity.LEFT;
511 lp.width = searchBarSpaceHeightPx;
512
513 LinearLayout targets = (LinearLayout) dropTargetBar.findViewById(R.id.drag_target_bar);
514 targets.setOrientation(LinearLayout.VERTICAL);
515 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
516 targetsLp.gravity = verticalGravity;
517 targetsLp.height = LayoutParams.WRAP_CONTENT;
518 } else {
519 // Horizontal drop target bar space
520 lp.gravity = verticalGravity;
521 lp.height = searchBarSpaceHeightPx;
522
523 LinearLayout targets = (LinearLayout) dropTargetBar.findViewById(R.id.drag_target_bar);
524 targets.getLayoutParams().width = searchBarSpaceWidthPx;
525 }
526 return lp;
527 }
528
Winson Chung4f3bfad2015-07-13 11:14:30 -0700529 private int getCurrentWidth() {
530 return isLandscape
531 ? Math.max(widthPx, heightPx)
532 : Math.min(widthPx, heightPx);
533 }
534
535 private int getCurrentHeight() {
536 return isLandscape
537 ? Math.min(widthPx, heightPx)
538 : Math.max(widthPx, heightPx);
539 }
Winson Chungb3800242013-10-24 11:01:54 -0700540}