blob: 0abfc677153041f9c37051d8ac5e868bd5c255a2 [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;
Tony Wickham55616cd2015-09-23 14:55:17 -070087 private int hotseatBarHeightNormalPx, hotseatBarHeightShortPx;
88 private int hotseatBarHeightPx; // One of the above.
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;
Tony Wickham55616cd2015-09-23 14:55:17 -070099 private int searchBarSpaceHeightNormalPx, searchBarSpaceHeightTallPx;
100 private int searchBarSpaceHeightPx; // One of the above.
101 private int searchBarHeight = LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700102
Sunny Goyalc6205602015-05-21 20:46:33 -0700103 public DeviceProfile(Context context, InvariantDeviceProfile inv,
104 Point minSize, Point maxSize,
105 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700106
Adam Cohen2e6da152015-05-06 11:42:25 -0700107 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700108 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700109
Adam Cohen2e6da152015-05-06 11:42:25 -0700110 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700111 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700112
Sunny Goyalc6205602015-05-21 20:46:33 -0700113 // Constants from resources
114 isTablet = res.getBoolean(R.bool.is_tablet);
115 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
116 isPhone = !isTablet && !isLargeTablet;
117
118 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700119 transposeLayoutWithOrientation =
120 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700121
122 ComponentName cn = new ComponentName(context.getPackageName(),
123 this.getClass().getName());
124 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
125 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
126 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
127 pageIndicatorHeightPx =
128 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
129 defaultPageSpacingPx =
130 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700131 overviewModeMinIconZoneHeightPx =
132 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
133 overviewModeMaxIconZoneHeightPx =
134 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800135 overviewModeBarItemWidthPx =
136 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
137 overviewModeBarSpacerWidthPx =
138 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700139 overviewModeIconZoneRatio =
140 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_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));
Tony Wickham55616cd2015-09-23 14:55:17 -0700204 searchBarSpaceHeightNormalPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700205 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Tony Wickham55616cd2015-09-23 14:55:17 -0700206 searchBarSpaceHeightTallPx = getSearchBarTopOffset()
207 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height_tall);
Winson Chungb3800242013-10-24 11:01:54 -0700208
209 // Calculate the actual text height
210 Paint textPaint = new Paint();
211 textPaint.setTextSize(iconTextSizePx);
212 FontMetrics fm = textPaint.getFontMetrics();
213 cellWidthPx = iconSizePx;
214 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700215 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800216 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700217
218 // Hotseat
Tony Wickham55616cd2015-09-23 14:55:17 -0700219 hotseatBarHeightNormalPx = iconSizePx + 4 * edgeMarginPx;
220 hotseatBarHeightShortPx = iconSizePx + 2 * edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700221 hotseatCellWidthPx = iconSizePx;
222 hotseatCellHeightPx = iconSizePx;
223
224 // Folder
225 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
226 folderCellHeightPx = cellHeightPx + edgeMarginPx;
227 folderBackgroundOffset = -edgeMarginPx;
228 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700229 }
230
Winson Chungef7f8742015-06-04 17:18:17 -0700231 /**
232 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
233 */
234 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700235 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700236 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700237 int allAppsCellWidthGap =
238 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700239 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700240 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700241 (allAppsIconSizePx + allAppsCellWidthGap);
242 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700243 allAppsNumCols = numAppsCols;
244 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700245 }
246
Winson Chung69e04ea2013-12-02 14:43:44 -0800247 /** Returns the search bar top offset */
Sunny Goyalc6205602015-05-21 20:46:33 -0700248 private int getSearchBarTopOffset() {
249 if (isTablet && !isVerticalBarLayout()) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800250 return 4 * edgeMarginPx;
251 } else {
252 return 2 * edgeMarginPx;
253 }
254 }
255
Winson Chungb3800242013-10-24 11:01:54 -0700256 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700257 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700258 Rect bounds = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700259 if (isVerticalBarLayout()) {
Winson Chung42b3c062013-12-04 12:09:59 -0800260 if (isLayoutRtl) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700261 bounds.set(availableWidthPx - searchBarSpaceHeightNormalPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800262 availableWidthPx, availableHeightPx - edgeMarginPx);
263 } else {
Tony Wickham55616cd2015-09-23 14:55:17 -0700264 bounds.set(0, edgeMarginPx, searchBarSpaceHeightNormalPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800265 availableHeightPx - edgeMarginPx);
266 }
Winson Chungb3800242013-10-24 11:01:54 -0700267 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700268 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700269 // Pad the left and right of the workspace to ensure consistent spacing
270 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700271 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700272 // XXX: If the icon size changes across orientations, we will have to take
273 // that into account here too.
274 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700275 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800276 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
277 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700278 searchBarSpaceHeightPx);
279 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800280 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
281 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700282 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
283 defaultWidgetPadding.right), searchBarSpaceHeightPx);
284 }
285 }
286 return bounds;
287 }
288
289 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700290 Rect getWorkspacePadding(boolean isLayoutRtl) {
291 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700292 Rect padding = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700293 if (isVerticalBarLayout()) {
Winson Chungb3800242013-10-24 11:01:54 -0700294 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800295 if (isLayoutRtl) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700296 padding.set(hotseatBarHeightNormalPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800297 searchBarBounds.width(), edgeMarginPx);
298 } else {
299 padding.set(searchBarBounds.width(), edgeMarginPx,
Tony Wickham55616cd2015-09-23 14:55:17 -0700300 hotseatBarHeightNormalPx, edgeMarginPx);
Winson Chung42b3c062013-12-04 12:09:59 -0800301 }
Winson Chungb3800242013-10-24 11:01:54 -0700302 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700303 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700304 // Pad the left and right of the workspace to ensure consistent spacing
305 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800306 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700307 int width = getCurrentWidth();
308 int height = getCurrentHeight();
Winson Chung59a488a2013-12-10 12:32:14 -0800309 int paddingTop = searchBarBounds.bottom;
310 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700311 int availableWidth = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
312 (inv.numColumns * gapScale * cellWidthPx)));
Winson Chung59a488a2013-12-10 12:32:14 -0800313 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700314 - (int) (2 * inv.numRows * cellHeightPx));
Winson Chung59a488a2013-12-10 12:32:14 -0800315 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
316 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700317 } else {
318 // Pad the top and bottom of the workspace with search/hotseat bar sizes
319 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
320 searchBarBounds.bottom,
321 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
322 hotseatBarHeightPx + pageIndicatorHeightPx);
323 }
324 }
325 return padding;
326 }
327
Sunny Goyalc6205602015-05-21 20:46:33 -0700328 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700329 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700330 // In landscape mode the page spacing is set to the default.
331 return defaultPageSpacingPx;
332 } else {
333 // In portrait, we want the pages spaced such that there is no
334 // overhang of the previous / next page into the current page viewport.
335 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700336 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700337 }
338 }
339
Winson Chung4f7eb402015-07-10 18:00:48 -0700340 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700341 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
342 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
343 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700344 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700345 }
346
347 // The rect returned will be extended to below the system ui that covers the workspace
348 Rect getHotseatRect() {
349 if (isVerticalBarLayout()) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700350 return new Rect(availableWidthPx - hotseatBarHeightNormalPx, 0,
Winson Chungb3800242013-10-24 11:01:54 -0700351 Integer.MAX_VALUE, availableHeightPx);
352 } else {
353 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
354 availableWidthPx, Integer.MAX_VALUE);
355 }
356 }
357
Adam Cohen2e6da152015-05-06 11:42:25 -0700358 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700359 return width / countX;
360 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700361 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700362 return height / countY;
363 }
364
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700365 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700366 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
367 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
368 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700369 */
Winson Chungb3800242013-10-24 11:01:54 -0700370 boolean isVerticalBarLayout() {
371 return isLandscape && transposeLayoutWithOrientation;
372 }
373
374 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700375 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700376 }
377
Sunny Goyalc6205602015-05-21 20:46:33 -0700378 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800379 int visibleChildren = 0;
380 for (int i = 0; i < parent.getChildCount(); i++) {
381 if (parent.getChildAt(i).getVisibility() != View.GONE) {
382 visibleChildren++;
383 }
384 }
385 return visibleChildren;
386 }
387
Winson Chungb3800242013-10-24 11:01:54 -0700388 public void layout(Launcher launcher) {
389 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700390 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700391 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700392
393 // Layout the search bar space
Winson Chung006ee262015-08-03 14:40:11 -0700394 View searchBar = launcher.getSearchDropTargetBar();
Winson Chungb3800242013-10-24 11:01:54 -0700395 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Tony Wickham55616cd2015-09-23 14:55:17 -0700396 searchBarHeight = launcher.getSearchBarHeight();
397 if (searchBarHeight == LauncherCallbacks.SEARCH_BAR_HEIGHT_TALL) {
398 hotseatBarHeightPx = hotseatBarHeightShortPx;
399 searchBarSpaceHeightPx = searchBarSpaceHeightTallPx;
400 } else {
401 hotseatBarHeightPx = hotseatBarHeightNormalPx;
402 searchBarSpaceHeightPx = searchBarSpaceHeightNormalPx;
403 }
Winson Chungb3800242013-10-24 11:01:54 -0700404 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700405 // Vertical search bar space -- The search bar is fixed in the layout to be on the left
406 // of the screen regardless of RTL
Sunny Goyald9cc7802015-04-23 14:47:19 -0700407 lp.gravity = Gravity.LEFT;
Tony Wickham55616cd2015-09-23 14:55:17 -0700408 lp.width = searchBarSpaceHeightNormalPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800409
410 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
411 targets.setOrientation(LinearLayout.VERTICAL);
Sunny Goyald9cc7802015-04-23 14:47:19 -0700412 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
413 targetsLp.gravity = Gravity.TOP;
414 targetsLp.height = LayoutParams.WRAP_CONTENT;
415
Winson Chungb3800242013-10-24 11:01:54 -0700416 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800417 // Horizontal search bar space
Sunny Goyald9cc7802015-04-23 14:47:19 -0700418 lp.gravity = Gravity.TOP;
Winson Chungb3800242013-10-24 11:01:54 -0700419 lp.height = searchBarSpaceHeightPx;
Sunny Goyald9cc7802015-04-23 14:47:19 -0700420
421 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
422 targets.getLayoutParams().width = searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700423 }
424 searchBar.setLayoutParams(lp);
425
Winson Chungb3800242013-10-24 11:01:54 -0700426 // Layout the workspace
427 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
428 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
429 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700430 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700431 workspace.setLayoutParams(lp);
432 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700433 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700434
435 // Layout the hotseat
436 View hotseat = launcher.findViewById(R.id.hotseat);
437 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
438 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700439 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
440 // screen regardless of RTL
441 lp.gravity = Gravity.RIGHT;
Tony Wickham55616cd2015-09-23 14:55:17 -0700442 lp.width = hotseatBarHeightNormalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700443 lp.height = LayoutParams.MATCH_PARENT;
444 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700445 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800446 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700447 lp.gravity = Gravity.BOTTOM;
448 lp.width = LayoutParams.MATCH_PARENT;
449 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800450 hotseat.setPadding(edgeMarginPx + padding.left, 0,
451 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700452 2 * edgeMarginPx);
453 } else {
454 // For phones, layout the hotseat without any bottom margin
455 // to ensure that we have space for the folders
456 lp.gravity = Gravity.BOTTOM;
457 lp.width = LayoutParams.MATCH_PARENT;
458 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000459 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
460 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700461 }
462 hotseat.setLayoutParams(lp);
463
464 // Layout the page indicators
465 View pageIndicator = launcher.findViewById(R.id.page_indicator);
466 if (pageIndicator != null) {
467 if (hasVerticalBarLayout) {
468 // Hide the page indicators when we have vertical search/hotseat
469 pageIndicator.setVisibility(View.GONE);
470 } else {
471 // Put the page indicators above the hotseat
472 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
473 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
474 lp.width = LayoutParams.WRAP_CONTENT;
475 lp.height = LayoutParams.WRAP_CONTENT;
476 lp.bottomMargin = hotseatBarHeightPx;
477 pageIndicator.setLayoutParams(lp);
478 }
479 }
480
Winson Chungb3800242013-10-24 11:01:54 -0700481 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800482 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700483 if (overviewMode != null) {
Winson Chung4f7eb402015-07-10 18:00:48 -0700484 int overviewButtonBarHeight = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700485 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
486 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700487
488 int visibleChildCount = getVisibleChildCount(overviewMode);
489 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
490 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
491
492 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chung4f7eb402015-07-10 18:00:48 -0700493 lp.height = overviewButtonBarHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700494 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700495
496 if (lp.width > totalItemWidth && visibleChildCount > 1) {
497 // We have enough space. Lets add some margin too.
498 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
499 View lastChild = null;
500
501 // Set margin of all visible children except the last visible child
502 for (int i = 0; i < visibleChildCount; i++) {
503 if (lastChild != null) {
504 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
505 if (isLayoutRtl) {
506 clp.leftMargin = margin;
507 } else {
508 clp.rightMargin = margin;
509 }
510 lastChild.setLayoutParams(clp);
511 lastChild = null;
512 }
513 View thisChild = overviewMode.getChildAt(i);
514 if (thisChild.getVisibility() != View.GONE) {
515 lastChild = thisChild;
516 }
517 }
518 }
Winson Chungb3800242013-10-24 11:01:54 -0700519 }
520 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700521
522 private int getCurrentWidth() {
523 return isLandscape
524 ? Math.max(widthPx, heightPx)
525 : Math.min(widthPx, heightPx);
526 }
527
528 private int getCurrentHeight() {
529 return isLandscape
530 ? Math.min(widthPx, heightPx)
531 : Math.max(widthPx, heightPx);
532 }
Winson Chungb3800242013-10-24 11:01:54 -0700533}