blob: 1c0743cbc17b85cc979a25c196e9fda26dc814dc [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 Chung4f7eb402015-07-10 18:00:48 -070028import android.util.Size;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.view.Gravity;
Winson Chungb3800242013-10-24 11:01:54 -070030import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080031import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070032import android.view.ViewGroup.LayoutParams;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -070033import android.view.ViewGroup.MarginLayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070034import android.widget.FrameLayout;
Adam Cohen24ce0b32014-01-14 16:18:14 -080035import android.widget.LinearLayout;
Winson Chungb3800242013-10-24 11:01:54 -070036
Winson Chungb3800242013-10-24 11:01:54 -070037public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070038
Adam Cohen2e6da152015-05-06 11:42:25 -070039 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070040
Sunny Goyalc6205602015-05-21 20:46:33 -070041 // Device properties
42 public final boolean isTablet;
43 public final boolean isLargeTablet;
44 public final boolean isPhone;
45 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070046
Sunny Goyalc6205602015-05-21 20:46:33 -070047 // Device properties in current orientation
48 public final boolean isLandscape;
49 public final int widthPx;
50 public final int heightPx;
51 public final int availableWidthPx;
52 public final int availableHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070053
Sunny Goyalc6205602015-05-21 20:46:33 -070054 // Overview mode
55 private final int overviewModeMinIconZoneHeightPx;
56 private final int overviewModeMaxIconZoneHeightPx;
57 private final int overviewModeBarItemWidthPx;
58 private final int overviewModeBarSpacerWidthPx;
59 private final float overviewModeIconZoneRatio;
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;
Winson Chungb3800242013-10-24 11:01:54 -0700139 iconDrawablePaddingOriginalPx =
140 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700141
Adam Cohen2e6da152015-05-06 11:42:25 -0700142 // AllApps uses the original non-scaled icon text size
143 allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
144
145 // AllApps uses the original non-scaled icon size
146 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700147
Sunny Goyalc6205602015-05-21 20:46:33 -0700148 // Determine sizes.
149 widthPx = width;
150 heightPx = height;
151 if (isLandscape) {
152 availableWidthPx = maxSize.x;
153 availableHeightPx = minSize.y;
154 } else {
155 availableWidthPx = minSize.x;
156 availableHeightPx = maxSize.y;
157 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700158
Winson Chungb3800242013-10-24 11:01:54 -0700159 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700160 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700161 computeAllAppsButtonSize(context);
162 }
163
164 /**
165 * Determine the exact visual footprint of the all apps button, taking into account scaling
166 * and internal padding of the drawable.
167 */
168 private void computeAllAppsButtonSize(Context context) {
169 Resources res = context.getResources();
170 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700171 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding));
Winson Chungb3800242013-10-24 11:01:54 -0700172 }
173
Sunny Goyalc6205602015-05-21 20:46:33 -0700174 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700175 // Check to see if the icons fit in the new available height. If not, then we need to
176 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700177 float scale = 1f;
178 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700179 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700180 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800181
Sunny Goyalc6205602015-05-21 20:46:33 -0700182 // We only care about the top and bottom workspace padding, which is not affected by RTL.
183 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700184 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
185 if (usedHeight > maxHeight) {
186 scale = maxHeight / usedHeight;
187 drawablePadding = 0;
188 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700189 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700190 }
191
Winson Chung93f98ea2015-03-10 16:28:47 -0700192 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700193 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700194 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
195 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700196 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700197 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700198
199 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800200 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700201 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Sunny Goyal594d76d2014-11-06 10:12:54 -0800202 searchBarSpaceHeightPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700203 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Winson Chungb3800242013-10-24 11:01:54 -0700204
205 // Calculate the actual text height
206 Paint textPaint = new Paint();
207 textPaint.setTextSize(iconTextSizePx);
208 FontMetrics fm = textPaint.getFontMetrics();
209 cellWidthPx = iconSizePx;
210 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700211 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800212 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700213
214 // Hotseat
215 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
216 hotseatCellWidthPx = iconSizePx;
217 hotseatCellHeightPx = iconSizePx;
218
219 // Folder
220 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
221 folderCellHeightPx = cellHeightPx + edgeMarginPx;
222 folderBackgroundOffset = -edgeMarginPx;
223 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700224 }
225
Winson Chungef7f8742015-06-04 17:18:17 -0700226 /**
227 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
228 */
229 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700230 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700231 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700232 int allAppsCellWidthGap =
233 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700234 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700235 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700236 (allAppsIconSizePx + allAppsCellWidthGap);
237 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700238 allAppsNumCols = numAppsCols;
239 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700240 }
241
Winson Chung69e04ea2013-12-02 14:43:44 -0800242 /** Returns the search bar top offset */
Sunny Goyalc6205602015-05-21 20:46:33 -0700243 private int getSearchBarTopOffset() {
244 if (isTablet && !isVerticalBarLayout()) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800245 return 4 * edgeMarginPx;
246 } else {
247 return 2 * edgeMarginPx;
248 }
249 }
250
Winson Chungb3800242013-10-24 11:01:54 -0700251 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700252 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700253 Rect bounds = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700254 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800255 if (isLayoutRtl) {
256 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
257 availableWidthPx, availableHeightPx - edgeMarginPx);
258 } else {
259 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
260 availableHeightPx - edgeMarginPx);
261 }
Winson Chungb3800242013-10-24 11:01:54 -0700262 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700263 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700264 // Pad the left and right of the workspace to ensure consistent spacing
265 // between all icons
Sunny Goyalc6205602015-05-21 20:46:33 -0700266 int width = isLandscape ? Math.max(widthPx, heightPx) : Math.min(widthPx, heightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700267 // XXX: If the icon size changes across orientations, we will have to take
268 // that into account here too.
269 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700270 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800271 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
272 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700273 searchBarSpaceHeightPx);
274 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800275 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
276 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700277 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
278 defaultWidgetPadding.right), searchBarSpaceHeightPx);
279 }
280 }
281 return bounds;
282 }
283
284 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700285 Rect getWorkspacePadding(boolean isLayoutRtl) {
286 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700287 Rect padding = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700288 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chungb3800242013-10-24 11:01:54 -0700289 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800290 if (isLayoutRtl) {
291 padding.set(hotseatBarHeightPx, edgeMarginPx,
292 searchBarBounds.width(), edgeMarginPx);
293 } else {
294 padding.set(searchBarBounds.width(), edgeMarginPx,
295 hotseatBarHeightPx, edgeMarginPx);
296 }
Winson Chungb3800242013-10-24 11:01:54 -0700297 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700298 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700299 // Pad the left and right of the workspace to ensure consistent spacing
300 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800301 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Sunny Goyalc6205602015-05-21 20:46:33 -0700302 int width = isLandscape
Winson Chungb3800242013-10-24 11:01:54 -0700303 ? Math.max(widthPx, heightPx)
304 : Math.min(widthPx, heightPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700305 int height = isLandscape
Winson Chung59a488a2013-12-10 12:32:14 -0800306 ? Math.max(widthPx, heightPx)
307 : Math.min(widthPx, heightPx);
308 int paddingTop = searchBarBounds.bottom;
309 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700310 int availableWidth = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
311 (inv.numColumns * gapScale * cellWidthPx)));
Winson Chung59a488a2013-12-10 12:32:14 -0800312 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700313 - (int) (2 * inv.numRows * cellHeightPx));
Winson Chung59a488a2013-12-10 12:32:14 -0800314 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
315 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700316 } else {
317 // Pad the top and bottom of the workspace with search/hotseat bar sizes
318 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
319 searchBarBounds.bottom,
320 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
321 hotseatBarHeightPx + pageIndicatorHeightPx);
322 }
323 }
324 return padding;
325 }
326
Sunny Goyalc6205602015-05-21 20:46:33 -0700327 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
328 if ((isLandscape && transposeLayoutWithOrientation) || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700329 // In landscape mode the page spacing is set to the default.
330 return defaultPageSpacingPx;
331 } else {
332 // In portrait, we want the pages spaced such that there is no
333 // overhang of the previous / next page into the current page viewport.
334 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700335 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700336 }
337 }
338
Winson Chung4f7eb402015-07-10 18:00:48 -0700339 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700340 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
341 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
342 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700343 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700344 }
345
346 // The rect returned will be extended to below the system ui that covers the workspace
347 Rect getHotseatRect() {
348 if (isVerticalBarLayout()) {
349 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
350 Integer.MAX_VALUE, availableHeightPx);
351 } else {
352 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
353 availableWidthPx, Integer.MAX_VALUE);
354 }
355 }
356
Adam Cohen2e6da152015-05-06 11:42:25 -0700357 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700358 return width / countX;
359 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700360 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700361 return height / countY;
362 }
363
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700364 /**
365 * When {@code true}, hotseat is on the bottom row when in landscape mode.
366 * If {@code false}, hotseat is on the right column when in landscape mode.
367 */
Winson Chungb3800242013-10-24 11:01:54 -0700368 boolean isVerticalBarLayout() {
369 return isLandscape && transposeLayoutWithOrientation;
370 }
371
372 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700373 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700374 }
375
Sunny Goyalc6205602015-05-21 20:46:33 -0700376 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800377 int visibleChildren = 0;
378 for (int i = 0; i < parent.getChildCount(); i++) {
379 if (parent.getChildAt(i).getVisibility() != View.GONE) {
380 visibleChildren++;
381 }
382 }
383 return visibleChildren;
384 }
385
Winson Chungb3800242013-10-24 11:01:54 -0700386 public void layout(Launcher launcher) {
387 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700388 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700389 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700390
391 // Layout the search bar space
392 View searchBar = launcher.getSearchBar();
393 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
394 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700395 // Vertical search bar space -- The search bar is fixed in the layout to be on the left
396 // of the screen regardless of RTL
Sunny Goyald9cc7802015-04-23 14:47:19 -0700397 lp.gravity = Gravity.LEFT;
Winson Chungb3800242013-10-24 11:01:54 -0700398 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800399
400 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
401 targets.setOrientation(LinearLayout.VERTICAL);
Sunny Goyald9cc7802015-04-23 14:47:19 -0700402 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
403 targetsLp.gravity = Gravity.TOP;
404 targetsLp.height = LayoutParams.WRAP_CONTENT;
405
Winson Chungb3800242013-10-24 11:01:54 -0700406 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800407 // Horizontal search bar space
Sunny Goyald9cc7802015-04-23 14:47:19 -0700408 lp.gravity = Gravity.TOP;
Winson Chungb3800242013-10-24 11:01:54 -0700409 lp.height = searchBarSpaceHeightPx;
Sunny Goyald9cc7802015-04-23 14:47:19 -0700410
411 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
412 targets.getLayoutParams().width = searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700413 }
414 searchBar.setLayoutParams(lp);
415
Winson Chungb3800242013-10-24 11:01:54 -0700416 // Layout the workspace
417 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
418 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
419 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700420 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700421 workspace.setLayoutParams(lp);
422 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700423 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700424
425 // Layout the hotseat
426 View hotseat = launcher.findViewById(R.id.hotseat);
427 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
428 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700429 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
430 // screen regardless of RTL
431 lp.gravity = Gravity.RIGHT;
Winson Chungb3800242013-10-24 11:01:54 -0700432 lp.width = hotseatBarHeightPx;
433 lp.height = LayoutParams.MATCH_PARENT;
434 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700435 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800436 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700437 lp.gravity = Gravity.BOTTOM;
438 lp.width = LayoutParams.MATCH_PARENT;
439 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800440 hotseat.setPadding(edgeMarginPx + padding.left, 0,
441 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700442 2 * edgeMarginPx);
443 } else {
444 // For phones, layout the hotseat without any bottom margin
445 // to ensure that we have space for the folders
446 lp.gravity = Gravity.BOTTOM;
447 lp.width = LayoutParams.MATCH_PARENT;
448 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000449 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
450 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700451 }
452 hotseat.setLayoutParams(lp);
453
454 // Layout the page indicators
455 View pageIndicator = launcher.findViewById(R.id.page_indicator);
456 if (pageIndicator != null) {
457 if (hasVerticalBarLayout) {
458 // Hide the page indicators when we have vertical search/hotseat
459 pageIndicator.setVisibility(View.GONE);
460 } else {
461 // Put the page indicators above the hotseat
462 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
463 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
464 lp.width = LayoutParams.WRAP_CONTENT;
465 lp.height = LayoutParams.WRAP_CONTENT;
466 lp.bottomMargin = hotseatBarHeightPx;
467 pageIndicator.setLayoutParams(lp);
468 }
469 }
470
Winson Chungb3800242013-10-24 11:01:54 -0700471 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800472 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700473 if (overviewMode != null) {
Winson Chung4f7eb402015-07-10 18:00:48 -0700474 int overviewButtonBarHeight = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700475 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
476 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700477
478 int visibleChildCount = getVisibleChildCount(overviewMode);
479 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
480 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
481
482 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chung4f7eb402015-07-10 18:00:48 -0700483 lp.height = overviewButtonBarHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700484 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700485
486 if (lp.width > totalItemWidth && visibleChildCount > 1) {
487 // We have enough space. Lets add some margin too.
488 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
489 View lastChild = null;
490
491 // Set margin of all visible children except the last visible child
492 for (int i = 0; i < visibleChildCount; i++) {
493 if (lastChild != null) {
494 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
495 if (isLayoutRtl) {
496 clp.leftMargin = margin;
497 } else {
498 clp.rightMargin = margin;
499 }
500 lastChild.setLayoutParams(clp);
501 lastChild = null;
502 }
503 View thisChild = overviewMode.getChildAt(i);
504 if (thisChild.getVisibility() != View.GONE) {
505 lastChild = thisChild;
506 }
507 }
508 }
Winson Chungb3800242013-10-24 11:01:54 -0700509 }
510 }
511}