blob: 4ee8a5115bf8678623aa8098281b0f5261a0f85d [file] [log] [blame]
Winson Chungb3800242013-10-24 11:01:54 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
19import android.appwidget.AppWidgetHostView;
20import android.content.ComponentName;
21import android.content.Context;
Winson Chungb3800242013-10-24 11:01:54 -070022import android.content.res.Resources;
23import android.graphics.Paint;
24import android.graphics.Paint.FontMetrics;
25import android.graphics.Point;
Winson Chungb3800242013-10-24 11:01:54 -070026import android.graphics.Rect;
27import android.util.DisplayMetrics;
Winson Chungb3800242013-10-24 11:01:54 -070028import android.view.Gravity;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080030import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.view.ViewGroup.LayoutParams;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -070032import android.view.ViewGroup.MarginLayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070033import android.widget.FrameLayout;
Adam Cohen24ce0b32014-01-14 16:18:14 -080034import android.widget.LinearLayout;
Winson Chungb3800242013-10-24 11:01:54 -070035
Winson Chungb3800242013-10-24 11:01:54 -070036public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070037
Adam Cohen2e6da152015-05-06 11:42:25 -070038 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070039
Sunny Goyalc6205602015-05-21 20:46:33 -070040 // Device properties
41 public final boolean isTablet;
42 public final boolean isLargeTablet;
43 public final boolean isPhone;
44 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070045
Sunny Goyalc6205602015-05-21 20:46:33 -070046 // Device properties in current orientation
47 public final boolean isLandscape;
48 public final int widthPx;
49 public final int heightPx;
50 public final int availableWidthPx;
51 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070052 /**
53 * The maximum amount of left/right workspace padding as a percentage of the screen width.
54 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
55 * 7% of the screen width can be used as right padding.
56 */
57 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070058
Sunny Goyalc6205602015-05-21 20:46:33 -070059 // Overview mode
60 private final int overviewModeMinIconZoneHeightPx;
61 private final int overviewModeMaxIconZoneHeightPx;
62 private final int overviewModeBarItemWidthPx;
63 private final int overviewModeBarSpacerWidthPx;
64 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070065
Sunny Goyalc6205602015-05-21 20:46:33 -070066 // Workspace
67 private int desiredWorkspaceLeftRightMarginPx;
68 public final int edgeMarginPx;
69 public final Rect defaultWidgetPadding;
70 private final int pageIndicatorHeightPx;
71 private final int defaultPageSpacingPx;
72 private float dragViewScale;
73
74 // Workspace icons
75 public int iconSizePx;
76 public int iconTextSizePx;
77 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070078 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070079
Adam Cohen59400422014-03-05 18:07:04 -080080 public int cellWidthPx;
81 public int cellHeightPx;
82
Sunny Goyalc6205602015-05-21 20:46:33 -070083 // Folder
84 public int folderBackgroundOffset;
85 public int folderIconSizePx;
86 public int folderCellWidthPx;
87 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070088
Sunny Goyalc6205602015-05-21 20:46:33 -070089 // Hotseat
90 public int hotseatCellWidthPx;
91 public int hotseatCellHeightPx;
92 public int hotseatIconSizePx;
Tony Wickham55616cd2015-09-23 14:55:17 -070093 private int hotseatBarHeightNormalPx, hotseatBarHeightShortPx;
94 private int hotseatBarHeightPx; // One of the above.
Adam Cohen2e6da152015-05-06 11:42:25 -070095
Sunny Goyalc6205602015-05-21 20:46:33 -070096 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097 public int allAppsNumCols;
98 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -070099 public int allAppsButtonVisualSize;
100 public final int allAppsIconSizePx;
101 public final int allAppsIconTextSizePx;
Winson Chung59a488a2013-12-10 12:32:14 -0800102
Sunny Goyalc6205602015-05-21 20:46:33 -0700103 // QSB
104 private int searchBarSpaceWidthPx;
Tony Wickham55616cd2015-09-23 14:55:17 -0700105 private int searchBarSpaceHeightNormalPx, searchBarSpaceHeightTallPx;
106 private int searchBarSpaceHeightPx; // One of the above.
107 private int searchBarHeight = LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700108
Sunny Goyalc6205602015-05-21 20:46:33 -0700109 public DeviceProfile(Context context, InvariantDeviceProfile inv,
110 Point minSize, Point maxSize,
111 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700112
Adam Cohen2e6da152015-05-06 11:42:25 -0700113 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700114 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700115
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700117 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700118
Sunny Goyalc6205602015-05-21 20:46:33 -0700119 // Constants from resources
120 isTablet = res.getBoolean(R.bool.is_tablet);
121 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
122 isPhone = !isTablet && !isLargeTablet;
123
124 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700125 transposeLayoutWithOrientation =
126 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700127
128 ComponentName cn = new ComponentName(context.getPackageName(),
129 this.getClass().getName());
130 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
131 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
132 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
133 pageIndicatorHeightPx =
134 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
135 defaultPageSpacingPx =
136 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700137 overviewModeMinIconZoneHeightPx =
138 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
139 overviewModeMaxIconZoneHeightPx =
140 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800141 overviewModeBarItemWidthPx =
142 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
143 overviewModeBarSpacerWidthPx =
144 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700145 overviewModeIconZoneRatio =
146 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700147 iconDrawablePaddingOriginalPx =
148 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700149
Adam Cohen2e6da152015-05-06 11:42:25 -0700150 // AllApps uses the original non-scaled icon text size
151 allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
152
153 // AllApps uses the original non-scaled icon size
154 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700155
Sunny Goyalc6205602015-05-21 20:46:33 -0700156 // Determine sizes.
157 widthPx = width;
158 heightPx = height;
159 if (isLandscape) {
160 availableWidthPx = maxSize.x;
161 availableHeightPx = minSize.y;
162 } else {
163 availableWidthPx = minSize.x;
164 availableHeightPx = maxSize.y;
165 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700166
Winson Chungb3800242013-10-24 11:01:54 -0700167 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700168 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700169 computeAllAppsButtonSize(context);
170 }
171
172 /**
173 * Determine the exact visual footprint of the all apps button, taking into account scaling
174 * and internal padding of the drawable.
175 */
176 private void computeAllAppsButtonSize(Context context) {
177 Resources res = context.getResources();
178 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700179 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
180 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700181 }
182
Sunny Goyalc6205602015-05-21 20:46:33 -0700183 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700184 // Check to see if the icons fit in the new available height. If not, then we need to
185 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700186 float scale = 1f;
187 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700188 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700189 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800190
Sunny Goyalc6205602015-05-21 20:46:33 -0700191 // We only care about the top and bottom workspace padding, which is not affected by RTL.
192 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700193 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
194 if (usedHeight > maxHeight) {
195 scale = maxHeight / usedHeight;
196 drawablePadding = 0;
197 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700198 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700199 }
200
Winson Chung93f98ea2015-03-10 16:28:47 -0700201 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700202 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700203 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
204 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700205 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700206 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700207
208 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800209 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700210 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Tony Wickham55616cd2015-09-23 14:55:17 -0700211 searchBarSpaceHeightNormalPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700212 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Tony Wickham55616cd2015-09-23 14:55:17 -0700213 searchBarSpaceHeightTallPx = getSearchBarTopOffset()
214 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height_tall);
Winson Chungb3800242013-10-24 11:01:54 -0700215
216 // Calculate the actual text height
217 Paint textPaint = new Paint();
218 textPaint.setTextSize(iconTextSizePx);
219 FontMetrics fm = textPaint.getFontMetrics();
220 cellWidthPx = iconSizePx;
221 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700222 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800223 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700224
225 // Hotseat
Tony Wickham55616cd2015-09-23 14:55:17 -0700226 hotseatBarHeightNormalPx = iconSizePx + 4 * edgeMarginPx;
227 hotseatBarHeightShortPx = iconSizePx + 2 * edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700228 hotseatCellWidthPx = iconSizePx;
229 hotseatCellHeightPx = iconSizePx;
230
231 // Folder
Tony Wickhama2950482015-10-12 15:57:23 -0700232 folderCellWidthPx = Math.min(cellWidthPx + 6 * edgeMarginPx,
233 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Winson Chungb3800242013-10-24 11:01:54 -0700234 folderCellHeightPx = cellHeightPx + edgeMarginPx;
235 folderBackgroundOffset = -edgeMarginPx;
236 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700237 }
238
Winson Chungef7f8742015-06-04 17:18:17 -0700239 /**
240 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
241 */
242 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700243 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700244 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700245 int allAppsCellWidthGap =
246 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700247 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700248 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700249 (allAppsIconSizePx + allAppsCellWidthGap);
250 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700251 allAppsNumCols = numAppsCols;
252 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700253 }
254
Winson Chung69e04ea2013-12-02 14:43:44 -0800255 /** Returns the search bar top offset */
Sunny Goyalc6205602015-05-21 20:46:33 -0700256 private int getSearchBarTopOffset() {
257 if (isTablet && !isVerticalBarLayout()) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800258 return 4 * edgeMarginPx;
259 } else {
260 return 2 * edgeMarginPx;
261 }
262 }
263
Winson Chungb3800242013-10-24 11:01:54 -0700264 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700265 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700266 Rect bounds = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700267 if (isVerticalBarLayout()) {
Winson Chung42b3c062013-12-04 12:09:59 -0800268 if (isLayoutRtl) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700269 bounds.set(availableWidthPx - searchBarSpaceHeightNormalPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800270 availableWidthPx, availableHeightPx - edgeMarginPx);
271 } else {
Tony Wickham55616cd2015-09-23 14:55:17 -0700272 bounds.set(0, edgeMarginPx, searchBarSpaceHeightNormalPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800273 availableHeightPx - edgeMarginPx);
274 }
Winson Chungb3800242013-10-24 11:01:54 -0700275 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700276 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700277 // Pad the left and right of the workspace to ensure consistent spacing
278 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700279 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700280 // XXX: If the icon size changes across orientations, we will have to take
281 // that into account here too.
282 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700283 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800284 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
285 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700286 searchBarSpaceHeightPx);
287 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800288 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
289 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700290 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
291 defaultWidgetPadding.right), searchBarSpaceHeightPx);
292 }
293 }
294 return bounds;
295 }
296
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 public Point getCellSize() {
298 Point result = new Point();
299 // Since we are only concerned with the overall padding, layout direction does
300 // not matter.
301 Rect padding = getWorkspacePadding(false /* isLayoutRtl */ );
302 result.x = calculateCellWidth(availableWidthPx - padding.left - padding.right,
303 inv.numColumns);
304 result.y = calculateCellHeight(availableHeightPx - padding.top - padding.bottom,
305 inv.numRows);
306 return result;
307 }
308
Winson Chungb3800242013-10-24 11:01:54 -0700309 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700310 Rect getWorkspacePadding(boolean isLayoutRtl) {
311 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700312 Rect padding = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700313 if (isVerticalBarLayout()) {
Winson Chungb3800242013-10-24 11:01:54 -0700314 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800315 if (isLayoutRtl) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700316 padding.set(hotseatBarHeightNormalPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800317 searchBarBounds.width(), edgeMarginPx);
318 } else {
319 padding.set(searchBarBounds.width(), edgeMarginPx,
Tony Wickham55616cd2015-09-23 14:55:17 -0700320 hotseatBarHeightNormalPx, edgeMarginPx);
Winson Chung42b3c062013-12-04 12:09:59 -0800321 }
Winson Chungb3800242013-10-24 11:01:54 -0700322 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700323 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700324 // Pad the left and right of the workspace to ensure consistent spacing
325 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800326 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700327 int width = getCurrentWidth();
328 int height = getCurrentHeight();
Winson Chung59a488a2013-12-10 12:32:14 -0800329 int paddingTop = searchBarBounds.bottom;
330 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700331 // The amount of screen space available for left/right padding.
332 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
333 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
334 availablePaddingX = (int) Math.min(availablePaddingX,
335 width * MAX_HORIZONTAL_PADDING_PERCENT);
336 int availablePaddingY = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700337 - (int) (2 * inv.numRows * cellHeightPx));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700338 padding.set(availablePaddingX / 2, paddingTop + availablePaddingY / 2,
339 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700340 } else {
341 // Pad the top and bottom of the workspace with search/hotseat bar sizes
342 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
343 searchBarBounds.bottom,
344 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
345 hotseatBarHeightPx + pageIndicatorHeightPx);
346 }
347 }
348 return padding;
349 }
350
Sunny Goyalc6205602015-05-21 20:46:33 -0700351 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700352 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700353 // In landscape mode the page spacing is set to the default.
354 return defaultPageSpacingPx;
355 } else {
356 // In portrait, we want the pages spaced such that there is no
357 // overhang of the previous / next page into the current page viewport.
358 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700359 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700360 }
361 }
362
Winson Chung4f7eb402015-07-10 18:00:48 -0700363 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700364 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
365 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
366 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700367 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700368 }
369
370 // The rect returned will be extended to below the system ui that covers the workspace
Sunny Goyalcf25b522015-07-09 00:01:18 -0700371 public boolean isInHotseatRect(int x, int y) {
Winson Chungb3800242013-10-24 11:01:54 -0700372 if (isVerticalBarLayout()) {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700373 return (x >= (availableWidthPx - hotseatBarHeightPx))
374 && (y >= 0) && (y <= availableHeightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700375 } else {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700376 return (x >= 0) && (x <= availableWidthPx)
377 && (y >= (availableHeightPx - hotseatBarHeightPx));
Winson Chungb3800242013-10-24 11:01:54 -0700378 }
379 }
380
Adam Cohen2e6da152015-05-06 11:42:25 -0700381 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700382 return width / countX;
383 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700384 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700385 return height / countY;
386 }
387
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700388 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700389 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
390 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
391 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700392 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700393 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700394 return isLandscape && transposeLayoutWithOrientation;
395 }
396
397 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700398 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700399 }
400
Sunny Goyalc6205602015-05-21 20:46:33 -0700401 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800402 int visibleChildren = 0;
403 for (int i = 0; i < parent.getChildCount(); i++) {
404 if (parent.getChildAt(i).getVisibility() != View.GONE) {
405 visibleChildren++;
406 }
407 }
408 return visibleChildren;
409 }
410
Winson Chungb3800242013-10-24 11:01:54 -0700411 public void layout(Launcher launcher) {
412 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700413 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700414 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700415
416 // Layout the search bar space
Tony Wickham55616cd2015-09-23 14:55:17 -0700417 searchBarHeight = launcher.getSearchBarHeight();
418 if (searchBarHeight == LauncherCallbacks.SEARCH_BAR_HEIGHT_TALL) {
419 hotseatBarHeightPx = hotseatBarHeightShortPx;
420 searchBarSpaceHeightPx = searchBarSpaceHeightTallPx;
421 } else {
422 hotseatBarHeightPx = hotseatBarHeightNormalPx;
423 searchBarSpaceHeightPx = searchBarSpaceHeightNormalPx;
424 }
Winson Chung006ee262015-08-03 14:40:11 -0700425 View searchBar = launcher.getSearchDropTargetBar();
Tony Wickham34d2c912015-09-11 09:27:58 -0700426 lp = getDropTargetBarLayoutParams(hasVerticalBarLayout, searchBar, Gravity.TOP);
Winson Chungb3800242013-10-24 11:01:54 -0700427 searchBar.setLayoutParams(lp);
428
Tony Wickham34d2c912015-09-11 09:27:58 -0700429 // Layout the app info bar space
430 View appInfoBar = launcher.getAppInfoDropTargetBar();
431 lp = getDropTargetBarLayoutParams(hasVerticalBarLayout, appInfoBar, Gravity.BOTTOM);
432 lp.bottomMargin = hotseatBarHeightPx;
433 appInfoBar.setLayoutParams(lp);
434
Winson Chungb3800242013-10-24 11:01:54 -0700435 // Layout the workspace
436 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
437 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
438 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700439 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700440 workspace.setLayoutParams(lp);
441 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700442 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700443
444 // Layout the hotseat
445 View hotseat = launcher.findViewById(R.id.hotseat);
446 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700447 // We want the edges of the hotseat to line up with the edges of the workspace, but the
448 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
449 // this, we pad the left and right of the hotseat with half of the difference of a workspace
450 // cell vs a hotseat cell.
451 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
452 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
453 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700454 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700455 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
456 // screen regardless of RTL
457 lp.gravity = Gravity.RIGHT;
Tony Wickham55616cd2015-09-23 14:55:17 -0700458 lp.width = hotseatBarHeightNormalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700459 lp.height = LayoutParams.MATCH_PARENT;
460 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700461 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800462 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700463 lp.gravity = Gravity.BOTTOM;
464 lp.width = LayoutParams.MATCH_PARENT;
465 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700466 hotseat.findViewById(R.id.layout).setPadding(
467 hotseatAdjustment + padding.left, 0,
468 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700469 } else {
470 // For phones, layout the hotseat without any bottom margin
471 // to ensure that we have space for the folders
472 lp.gravity = Gravity.BOTTOM;
473 lp.width = LayoutParams.MATCH_PARENT;
474 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700475 hotseat.findViewById(R.id.layout).setPadding(
476 hotseatAdjustment + padding.left, 0,
477 hotseatAdjustment + padding.right, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700478 }
479 hotseat.setLayoutParams(lp);
480
481 // Layout the page indicators
482 View pageIndicator = launcher.findViewById(R.id.page_indicator);
483 if (pageIndicator != null) {
484 if (hasVerticalBarLayout) {
485 // Hide the page indicators when we have vertical search/hotseat
486 pageIndicator.setVisibility(View.GONE);
487 } else {
488 // Put the page indicators above the hotseat
489 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
490 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
491 lp.width = LayoutParams.WRAP_CONTENT;
492 lp.height = LayoutParams.WRAP_CONTENT;
493 lp.bottomMargin = hotseatBarHeightPx;
494 pageIndicator.setLayoutParams(lp);
495 }
496 }
497
Winson Chungb3800242013-10-24 11:01:54 -0700498 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800499 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700500 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700501 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
502 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700503
504 int visibleChildCount = getVisibleChildCount(overviewMode);
505 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
506 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
507
508 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700509 lp.height = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700510 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700511
512 if (lp.width > totalItemWidth && visibleChildCount > 1) {
513 // We have enough space. Lets add some margin too.
514 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
515 View lastChild = null;
516
517 // Set margin of all visible children except the last visible child
518 for (int i = 0; i < visibleChildCount; i++) {
519 if (lastChild != null) {
520 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
521 if (isLayoutRtl) {
522 clp.leftMargin = margin;
523 } else {
524 clp.rightMargin = margin;
525 }
526 lastChild.setLayoutParams(clp);
527 lastChild = null;
528 }
529 View thisChild = overviewMode.getChildAt(i);
530 if (thisChild.getVisibility() != View.GONE) {
531 lastChild = thisChild;
532 }
533 }
534 }
Winson Chungb3800242013-10-24 11:01:54 -0700535 }
536 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700537
Tony Wickham34d2c912015-09-11 09:27:58 -0700538 private FrameLayout.LayoutParams getDropTargetBarLayoutParams(boolean hasVerticalBarLayout,
539 View dropTargetBar, int verticalGravity) {
540 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) dropTargetBar.getLayoutParams();
541 if (hasVerticalBarLayout) {
542 // Vertical drop target bar space -- The drop target bar is fixed in the layout to be on
543 // the left of the screen regardless of RTL
544 lp.gravity = Gravity.LEFT;
545 lp.width = searchBarSpaceHeightPx;
546
547 LinearLayout targets = (LinearLayout) dropTargetBar.findViewById(R.id.drag_target_bar);
548 targets.setOrientation(LinearLayout.VERTICAL);
549 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
550 targetsLp.gravity = verticalGravity;
551 targetsLp.height = LayoutParams.WRAP_CONTENT;
552 } else {
553 // Horizontal drop target bar space
554 lp.gravity = verticalGravity;
555 lp.height = searchBarSpaceHeightPx;
556
557 LinearLayout targets = (LinearLayout) dropTargetBar.findViewById(R.id.drag_target_bar);
558 targets.getLayoutParams().width = searchBarSpaceWidthPx;
559 }
560 return lp;
561 }
562
Winson Chung4f3bfad2015-07-13 11:14:30 -0700563 private int getCurrentWidth() {
564 return isLandscape
565 ? Math.max(widthPx, heightPx)
566 : Math.min(widthPx, heightPx);
567 }
568
569 private int getCurrentHeight() {
570 return isLandscape
571 ? Math.min(widthPx, heightPx)
572 : Math.max(widthPx, heightPx);
573 }
Winson Chungb3800242013-10-24 11:01:54 -0700574}