blob: 62b05b0d65e70f0c105562decc608b3151e78b08 [file] [log] [blame]
Winson Chungb3800242013-10-24 11:01:54 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
19import android.appwidget.AppWidgetHostView;
20import android.content.ComponentName;
21import android.content.Context;
Winson Chungb3800242013-10-24 11:01:54 -070022import android.content.res.Resources;
23import android.graphics.Paint;
24import android.graphics.Paint.FontMetrics;
25import android.graphics.Point;
Winson Chungb3800242013-10-24 11:01:54 -070026import android.graphics.Rect;
27import android.util.DisplayMetrics;
Winson Chungb3800242013-10-24 11:01:54 -070028import android.view.Gravity;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080030import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.view.ViewGroup.LayoutParams;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -070032import android.view.ViewGroup.MarginLayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070033import android.widget.FrameLayout;
Adam Cohen24ce0b32014-01-14 16:18:14 -080034import android.widget.LinearLayout;
Winson Chungb3800242013-10-24 11:01:54 -070035
Winson Chungb3800242013-10-24 11:01:54 -070036public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070037
Adam Cohen2e6da152015-05-06 11:42:25 -070038 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070039
Sunny Goyalc6205602015-05-21 20:46:33 -070040 // Device properties
41 public final boolean isTablet;
42 public final boolean isLargeTablet;
43 public final boolean isPhone;
44 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070045
Sunny Goyalc6205602015-05-21 20:46:33 -070046 // Device properties in current orientation
47 public final boolean isLandscape;
48 public final int widthPx;
49 public final int heightPx;
50 public final int availableWidthPx;
51 public final int availableHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070052
Sunny Goyalc6205602015-05-21 20:46:33 -070053 // Overview mode
54 private final int overviewModeMinIconZoneHeightPx;
55 private final int overviewModeMaxIconZoneHeightPx;
56 private final int overviewModeBarItemWidthPx;
57 private final int overviewModeBarSpacerWidthPx;
58 private final float overviewModeIconZoneRatio;
59 private final float overviewModeScaleFactor;
Winson Chungb3800242013-10-24 11:01:54 -070060
Sunny Goyalc6205602015-05-21 20:46:33 -070061 // Workspace
62 private int desiredWorkspaceLeftRightMarginPx;
63 public final int edgeMarginPx;
64 public final Rect defaultWidgetPadding;
65 private final int pageIndicatorHeightPx;
66 private final int defaultPageSpacingPx;
67 private float dragViewScale;
68
69 // Workspace icons
70 public int iconSizePx;
71 public int iconTextSizePx;
72 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070073 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070074
Adam Cohen59400422014-03-05 18:07:04 -080075 public int cellWidthPx;
76 public int cellHeightPx;
77
Sunny Goyalc6205602015-05-21 20:46:33 -070078 // Folder
79 public int folderBackgroundOffset;
80 public int folderIconSizePx;
81 public int folderCellWidthPx;
82 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070083
Sunny Goyalc6205602015-05-21 20:46:33 -070084 // Hotseat
85 public int hotseatCellWidthPx;
86 public int hotseatCellHeightPx;
87 public int hotseatIconSizePx;
88 private int hotseatBarHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -070089
Sunny Goyalc6205602015-05-21 20:46:33 -070090 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -070091 public int allAppsNumCols;
92 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -070093 public int allAppsButtonVisualSize;
94 public final int allAppsIconSizePx;
95 public final int allAppsIconTextSizePx;
Winson Chung59a488a2013-12-10 12:32:14 -080096
Sunny Goyalc6205602015-05-21 20:46:33 -070097 // QSB
98 private int searchBarSpaceWidthPx;
99 private int searchBarSpaceHeightPx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700100
Sunny Goyalc6205602015-05-21 20:46:33 -0700101 public DeviceProfile(Context context, InvariantDeviceProfile inv,
102 Point minSize, Point maxSize,
103 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700104
Adam Cohen2e6da152015-05-06 11:42:25 -0700105 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700106 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700107
Adam Cohen2e6da152015-05-06 11:42:25 -0700108 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700109 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700110
Sunny Goyalc6205602015-05-21 20:46:33 -0700111 // Constants from resources
112 isTablet = res.getBoolean(R.bool.is_tablet);
113 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
114 isPhone = !isTablet && !isLargeTablet;
115
116 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700117 transposeLayoutWithOrientation =
118 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700119
120 ComponentName cn = new ComponentName(context.getPackageName(),
121 this.getClass().getName());
122 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
123 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
124 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
125 pageIndicatorHeightPx =
126 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
127 defaultPageSpacingPx =
128 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700129 overviewModeMinIconZoneHeightPx =
130 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
131 overviewModeMaxIconZoneHeightPx =
132 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800133 overviewModeBarItemWidthPx =
134 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
135 overviewModeBarSpacerWidthPx =
136 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700137 overviewModeIconZoneRatio =
138 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
139 overviewModeScaleFactor =
140 res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700141 iconDrawablePaddingOriginalPx =
142 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700143
Adam Cohen2e6da152015-05-06 11:42:25 -0700144 // AllApps uses the original non-scaled icon text size
145 allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
146
147 // AllApps uses the original non-scaled icon size
148 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700149
Sunny Goyalc6205602015-05-21 20:46:33 -0700150 // Determine sizes.
151 widthPx = width;
152 heightPx = height;
153 if (isLandscape) {
154 availableWidthPx = maxSize.x;
155 availableHeightPx = minSize.y;
156 } else {
157 availableWidthPx = minSize.x;
158 availableHeightPx = maxSize.y;
159 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700160
Winson Chungb3800242013-10-24 11:01:54 -0700161 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700162 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700163 computeAllAppsButtonSize(context);
164 }
165
166 /**
167 * Determine the exact visual footprint of the all apps button, taking into account scaling
168 * and internal padding of the drawable.
169 */
170 private void computeAllAppsButtonSize(Context context) {
171 Resources res = context.getResources();
172 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700173 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding));
Winson Chungb3800242013-10-24 11:01:54 -0700174 }
175
Sunny Goyalc6205602015-05-21 20:46:33 -0700176 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700177 // Check to see if the icons fit in the new available height. If not, then we need to
178 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700179 float scale = 1f;
180 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700181 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700182 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800183
Sunny Goyalc6205602015-05-21 20:46:33 -0700184 // We only care about the top and bottom workspace padding, which is not affected by RTL.
185 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700186 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
187 if (usedHeight > maxHeight) {
188 scale = maxHeight / usedHeight;
189 drawablePadding = 0;
190 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700191 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700192 }
193
Winson Chung93f98ea2015-03-10 16:28:47 -0700194 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700195 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700196 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
197 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700198 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700199 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700200
201 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800202 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700203 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Sunny Goyal594d76d2014-11-06 10:12:54 -0800204 searchBarSpaceHeightPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700205 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Winson Chungb3800242013-10-24 11:01:54 -0700206
207 // Calculate the actual text height
208 Paint textPaint = new Paint();
209 textPaint.setTextSize(iconTextSizePx);
210 FontMetrics fm = textPaint.getFontMetrics();
211 cellWidthPx = iconSizePx;
212 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700213 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800214 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700215
216 // Hotseat
217 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
218 hotseatCellWidthPx = iconSizePx;
219 hotseatCellHeightPx = iconSizePx;
220
221 // Folder
222 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
223 folderCellHeightPx = cellHeightPx + edgeMarginPx;
224 folderBackgroundOffset = -edgeMarginPx;
225 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700226 }
227
Winson Chungef7f8742015-06-04 17:18:17 -0700228 /**
229 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
230 */
231 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700232 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700233 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700234 int allAppsCellWidthGap =
235 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700236 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700237 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700238 (allAppsIconSizePx + allAppsCellWidthGap);
239 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700240 allAppsNumCols = numAppsCols;
241 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700242 }
243
Winson Chung69e04ea2013-12-02 14:43:44 -0800244 /** Returns the search bar top offset */
Sunny Goyalc6205602015-05-21 20:46:33 -0700245 private int getSearchBarTopOffset() {
246 if (isTablet && !isVerticalBarLayout()) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800247 return 4 * edgeMarginPx;
248 } else {
249 return 2 * edgeMarginPx;
250 }
251 }
252
Winson Chungb3800242013-10-24 11:01:54 -0700253 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700254 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700255 Rect bounds = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700256 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800257 if (isLayoutRtl) {
258 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
259 availableWidthPx, availableHeightPx - edgeMarginPx);
260 } else {
261 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
262 availableHeightPx - edgeMarginPx);
263 }
Winson Chungb3800242013-10-24 11:01:54 -0700264 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700265 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700266 // Pad the left and right of the workspace to ensure consistent spacing
267 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700268 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700269 // XXX: If the icon size changes across orientations, we will have to take
270 // that into account here too.
271 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700272 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800273 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
274 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700275 searchBarSpaceHeightPx);
276 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800277 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
278 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700279 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
280 defaultWidgetPadding.right), searchBarSpaceHeightPx);
281 }
282 }
283 return bounds;
284 }
285
286 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700287 Rect getWorkspacePadding(boolean isLayoutRtl) {
288 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700289 Rect padding = new Rect();
Sunny Goyalc6205602015-05-21 20:46:33 -0700290 if (isLandscape && transposeLayoutWithOrientation) {
Winson Chungb3800242013-10-24 11:01:54 -0700291 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800292 if (isLayoutRtl) {
293 padding.set(hotseatBarHeightPx, edgeMarginPx,
294 searchBarBounds.width(), edgeMarginPx);
295 } else {
296 padding.set(searchBarBounds.width(), edgeMarginPx,
297 hotseatBarHeightPx, edgeMarginPx);
298 }
Winson Chungb3800242013-10-24 11:01:54 -0700299 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700300 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700301 // Pad the left and right of the workspace to ensure consistent spacing
302 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800303 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700304 int width = getCurrentWidth();
305 int height = getCurrentHeight();
Winson Chung59a488a2013-12-10 12:32:14 -0800306 int paddingTop = searchBarBounds.bottom;
307 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700308 int availableWidth = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
309 (inv.numColumns * gapScale * cellWidthPx)));
Winson Chung59a488a2013-12-10 12:32:14 -0800310 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700311 - (int) (2 * inv.numRows * cellHeightPx));
Winson Chung59a488a2013-12-10 12:32:14 -0800312 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
313 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700314 } else {
315 // Pad the top and bottom of the workspace with search/hotseat bar sizes
316 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
317 searchBarBounds.bottom,
318 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
319 hotseatBarHeightPx + pageIndicatorHeightPx);
320 }
321 }
322 return padding;
323 }
324
Sunny Goyalc6205602015-05-21 20:46:33 -0700325 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
326 if ((isLandscape && transposeLayoutWithOrientation) || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700327 // In landscape mode the page spacing is set to the default.
328 return defaultPageSpacingPx;
329 } else {
330 // In portrait, we want the pages spaced such that there is no
331 // overhang of the previous / next page into the current page viewport.
332 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700333 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700334 }
335 }
336
337 Rect getOverviewModeButtonBarRect() {
338 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
339 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
340 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
341 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
342 }
343
Sunny Goyalc6205602015-05-21 20:46:33 -0700344 public float getOverviewModeScale(boolean isLayoutRtl) {
345 Rect workspacePadding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700346 Rect overviewBar = getOverviewModeButtonBarRect();
347 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
348 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
349 }
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
397 View searchBar = launcher.getSearchBar();
398 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();
433 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700434 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
435 // screen regardless of RTL
436 lp.gravity = Gravity.RIGHT;
Winson Chungb3800242013-10-24 11:01:54 -0700437 lp.width = hotseatBarHeightPx;
438 lp.height = LayoutParams.MATCH_PARENT;
439 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700440 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800441 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700442 lp.gravity = Gravity.BOTTOM;
443 lp.width = LayoutParams.MATCH_PARENT;
444 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800445 hotseat.setPadding(edgeMarginPx + padding.left, 0,
446 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700447 2 * edgeMarginPx);
448 } else {
449 // For phones, layout the hotseat without any bottom margin
450 // to ensure that we have space for the folders
451 lp.gravity = Gravity.BOTTOM;
452 lp.width = LayoutParams.MATCH_PARENT;
453 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000454 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
455 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700456 }
457 hotseat.setLayoutParams(lp);
458
459 // Layout the page indicators
460 View pageIndicator = launcher.findViewById(R.id.page_indicator);
461 if (pageIndicator != null) {
462 if (hasVerticalBarLayout) {
463 // Hide the page indicators when we have vertical search/hotseat
464 pageIndicator.setVisibility(View.GONE);
465 } else {
466 // Put the page indicators above the hotseat
467 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
468 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
469 lp.width = LayoutParams.WRAP_CONTENT;
470 lp.height = LayoutParams.WRAP_CONTENT;
471 lp.bottomMargin = hotseatBarHeightPx;
472 pageIndicator.setLayoutParams(lp);
473 }
474 }
475
Winson Chungb3800242013-10-24 11:01:54 -0700476 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800477 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700478 if (overviewMode != null) {
479 Rect r = getOverviewModeButtonBarRect();
480 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
481 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700482
483 int visibleChildCount = getVisibleChildCount(overviewMode);
484 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
485 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
486
487 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chungb3800242013-10-24 11:01:54 -0700488 lp.height = r.height();
489 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700490
491 if (lp.width > totalItemWidth && visibleChildCount > 1) {
492 // We have enough space. Lets add some margin too.
493 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
494 View lastChild = null;
495
496 // Set margin of all visible children except the last visible child
497 for (int i = 0; i < visibleChildCount; i++) {
498 if (lastChild != null) {
499 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
500 if (isLayoutRtl) {
501 clp.leftMargin = margin;
502 } else {
503 clp.rightMargin = margin;
504 }
505 lastChild.setLayoutParams(clp);
506 lastChild = null;
507 }
508 View thisChild = overviewMode.getChildAt(i);
509 if (thisChild.getVisibility() != View.GONE) {
510 lastChild = thisChild;
511 }
512 }
513 }
Winson Chungb3800242013-10-24 11:01:54 -0700514 }
515 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700516
517 private int getCurrentWidth() {
518 return isLandscape
519 ? Math.max(widthPx, heightPx)
520 : Math.min(widthPx, heightPx);
521 }
522
523 private int getCurrentHeight() {
524 return isLandscape
525 ? Math.min(widthPx, heightPx)
526 : Math.max(widthPx, heightPx);
527 }
Winson Chungb3800242013-10-24 11:01:54 -0700528}