blob: ed6b7d7a86f1ba10812e9a602cad8da6a456b526 [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.
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700107
Sunny Goyalc6205602015-05-21 20:46:33 -0700108 public DeviceProfile(Context context, InvariantDeviceProfile inv,
109 Point minSize, Point maxSize,
110 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700111
Adam Cohen2e6da152015-05-06 11:42:25 -0700112 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700113 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700114
Adam Cohen2e6da152015-05-06 11:42:25 -0700115 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700116 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700117
Sunny Goyalc6205602015-05-21 20:46:33 -0700118 // Constants from resources
119 isTablet = res.getBoolean(R.bool.is_tablet);
120 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
121 isPhone = !isTablet && !isLargeTablet;
122
123 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700124 transposeLayoutWithOrientation =
125 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700126
127 ComponentName cn = new ComponentName(context.getPackageName(),
128 this.getClass().getName());
129 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
130 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
131 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
132 pageIndicatorHeightPx =
133 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
134 defaultPageSpacingPx =
135 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chungb3800242013-10-24 11:01:54 -0700136 overviewModeMinIconZoneHeightPx =
137 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
138 overviewModeMaxIconZoneHeightPx =
139 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800140 overviewModeBarItemWidthPx =
141 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
142 overviewModeBarSpacerWidthPx =
143 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700144 overviewModeIconZoneRatio =
145 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700146 iconDrawablePaddingOriginalPx =
147 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700148
Adam Cohen2e6da152015-05-06 11:42:25 -0700149 // AllApps uses the original non-scaled icon text size
150 allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
151
152 // AllApps uses the original non-scaled icon size
153 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700154
Sunny Goyalc6205602015-05-21 20:46:33 -0700155 // Determine sizes.
156 widthPx = width;
157 heightPx = height;
158 if (isLandscape) {
159 availableWidthPx = maxSize.x;
160 availableHeightPx = minSize.y;
161 } else {
162 availableWidthPx = minSize.x;
163 availableHeightPx = maxSize.y;
164 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700165
Winson Chungb3800242013-10-24 11:01:54 -0700166 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700167 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700168 computeAllAppsButtonSize(context);
169 }
170
171 /**
172 * Determine the exact visual footprint of the all apps button, taking into account scaling
173 * and internal padding of the drawable.
174 */
175 private void computeAllAppsButtonSize(Context context) {
176 Resources res = context.getResources();
177 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700178 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
179 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700180 }
181
Sunny Goyalc6205602015-05-21 20:46:33 -0700182 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700183 // Check to see if the icons fit in the new available height. If not, then we need to
184 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700185 float scale = 1f;
186 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700187 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700188 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800189
Sunny Goyalc6205602015-05-21 20:46:33 -0700190 // We only care about the top and bottom workspace padding, which is not affected by RTL.
191 Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */);
Winson Chungb3800242013-10-24 11:01:54 -0700192 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
193 if (usedHeight > maxHeight) {
194 scale = maxHeight / usedHeight;
195 drawablePadding = 0;
196 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700197 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700198 }
199
Winson Chung93f98ea2015-03-10 16:28:47 -0700200 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700201 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700202 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
203 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700204 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700205 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700206
207 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800208 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700209 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Tony Wickham55616cd2015-09-23 14:55:17 -0700210 searchBarSpaceHeightNormalPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700211 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Tony Wickham55616cd2015-09-23 14:55:17 -0700212 searchBarSpaceHeightTallPx = getSearchBarTopOffset()
213 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height_tall);
Winson Chungb3800242013-10-24 11:01:54 -0700214
215 // Calculate the actual text height
216 Paint textPaint = new Paint();
217 textPaint.setTextSize(iconTextSizePx);
218 FontMetrics fm = textPaint.getFontMetrics();
219 cellWidthPx = iconSizePx;
220 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700221 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800222 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700223
224 // Hotseat
Tony Wickham55616cd2015-09-23 14:55:17 -0700225 hotseatBarHeightNormalPx = iconSizePx + 4 * edgeMarginPx;
226 hotseatBarHeightShortPx = iconSizePx + 2 * edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700227 hotseatCellWidthPx = iconSizePx;
228 hotseatCellHeightPx = iconSizePx;
229
230 // Folder
Tony Wickhama2950482015-10-12 15:57:23 -0700231 folderCellWidthPx = Math.min(cellWidthPx + 6 * edgeMarginPx,
232 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Winson Chungb3800242013-10-24 11:01:54 -0700233 folderCellHeightPx = cellHeightPx + edgeMarginPx;
234 folderBackgroundOffset = -edgeMarginPx;
235 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung0f785722015-04-08 10:27:49 -0700236 }
237
Winson Chungef7f8742015-06-04 17:18:17 -0700238 /**
239 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
240 */
241 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700242 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700243 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700244 int allAppsCellWidthGap =
245 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700246 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700247 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700248 (allAppsIconSizePx + allAppsCellWidthGap);
249 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700250 allAppsNumCols = numAppsCols;
251 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700252 }
253
Winson Chung69e04ea2013-12-02 14:43:44 -0800254 /** Returns the search bar top offset */
Sunny Goyalc6205602015-05-21 20:46:33 -0700255 private int getSearchBarTopOffset() {
256 if (isTablet && !isVerticalBarLayout()) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800257 return 4 * edgeMarginPx;
258 } else {
259 return 2 * edgeMarginPx;
260 }
261 }
262
Winson Chungb3800242013-10-24 11:01:54 -0700263 /** Returns the search bar bounds in the current orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700264 public Rect getSearchBarBounds(boolean isLayoutRtl) {
Winson Chungb3800242013-10-24 11:01:54 -0700265 Rect bounds = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700266 if (isVerticalBarLayout()) {
Winson Chung42b3c062013-12-04 12:09:59 -0800267 if (isLayoutRtl) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700268 bounds.set(availableWidthPx - searchBarSpaceHeightNormalPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800269 availableWidthPx, availableHeightPx - edgeMarginPx);
270 } else {
Tony Wickham55616cd2015-09-23 14:55:17 -0700271 bounds.set(0, edgeMarginPx, searchBarSpaceHeightNormalPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800272 availableHeightPx - edgeMarginPx);
273 }
Winson Chungb3800242013-10-24 11:01:54 -0700274 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700275 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700276 // Pad the left and right of the workspace to ensure consistent spacing
277 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700278 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700279 // XXX: If the icon size changes across orientations, we will have to take
280 // that into account here too.
281 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700282 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800283 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
284 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700285 searchBarSpaceHeightPx);
286 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800287 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
288 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700289 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
290 defaultWidgetPadding.right), searchBarSpaceHeightPx);
291 }
292 }
293 return bounds;
294 }
295
296 /** Returns the workspace padding in the specified orientation */
Sunny Goyalc6205602015-05-21 20:46:33 -0700297 Rect getWorkspacePadding(boolean isLayoutRtl) {
298 Rect searchBarBounds = getSearchBarBounds(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700299 Rect padding = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700300 if (isVerticalBarLayout()) {
Winson Chungb3800242013-10-24 11:01:54 -0700301 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800302 if (isLayoutRtl) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700303 padding.set(hotseatBarHeightNormalPx, edgeMarginPx,
Winson Chung42b3c062013-12-04 12:09:59 -0800304 searchBarBounds.width(), edgeMarginPx);
305 } else {
306 padding.set(searchBarBounds.width(), edgeMarginPx,
Tony Wickham55616cd2015-09-23 14:55:17 -0700307 hotseatBarHeightNormalPx, edgeMarginPx);
Winson Chung42b3c062013-12-04 12:09:59 -0800308 }
Winson Chungb3800242013-10-24 11:01:54 -0700309 } else {
Sunny Goyalc6205602015-05-21 20:46:33 -0700310 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700311 // Pad the left and right of the workspace to ensure consistent spacing
312 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800313 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700314 int width = getCurrentWidth();
315 int height = getCurrentHeight();
Winson Chung59a488a2013-12-10 12:32:14 -0800316 int paddingTop = searchBarBounds.bottom;
317 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700318 // The amount of screen space available for left/right padding.
319 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
320 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
321 availablePaddingX = (int) Math.min(availablePaddingX,
322 width * MAX_HORIZONTAL_PADDING_PERCENT);
323 int availablePaddingY = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700324 - (int) (2 * inv.numRows * cellHeightPx));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700325 padding.set(availablePaddingX / 2, paddingTop + availablePaddingY / 2,
326 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700327 } else {
328 // Pad the top and bottom of the workspace with search/hotseat bar sizes
329 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
330 searchBarBounds.bottom,
331 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
332 hotseatBarHeightPx + pageIndicatorHeightPx);
333 }
334 }
335 return padding;
336 }
337
Sunny Goyalc6205602015-05-21 20:46:33 -0700338 private int getWorkspacePageSpacing(boolean isLayoutRtl) {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700339 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700340 // In landscape mode the page spacing is set to the default.
341 return defaultPageSpacingPx;
342 } else {
343 // In portrait, we want the pages spaced such that there is no
344 // overhang of the previous / next page into the current page viewport.
345 // We assume symmetrical padding in portrait mode.
Sunny Goyalc6205602015-05-21 20:46:33 -0700346 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left);
Winson Chungb3800242013-10-24 11:01:54 -0700347 }
348 }
349
Winson Chung4f7eb402015-07-10 18:00:48 -0700350 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700351 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
352 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
353 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700354 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700355 }
356
357 // The rect returned will be extended to below the system ui that covers the workspace
358 Rect getHotseatRect() {
359 if (isVerticalBarLayout()) {
Tony Wickham55616cd2015-09-23 14:55:17 -0700360 return new Rect(availableWidthPx - hotseatBarHeightNormalPx, 0,
Winson Chungb3800242013-10-24 11:01:54 -0700361 Integer.MAX_VALUE, availableHeightPx);
362 } else {
363 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
364 availableWidthPx, Integer.MAX_VALUE);
365 }
366 }
367
Adam Cohen2e6da152015-05-06 11:42:25 -0700368 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700369 return width / countX;
370 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700371 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700372 return height / countY;
373 }
374
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700375 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700376 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
377 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
378 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700379 */
Winson Chungb3800242013-10-24 11:01:54 -0700380 boolean isVerticalBarLayout() {
381 return isLandscape && transposeLayoutWithOrientation;
382 }
383
384 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700385 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700386 }
387
Sunny Goyalc6205602015-05-21 20:46:33 -0700388 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800389 int visibleChildren = 0;
390 for (int i = 0; i < parent.getChildCount(); i++) {
391 if (parent.getChildAt(i).getVisibility() != View.GONE) {
392 visibleChildren++;
393 }
394 }
395 return visibleChildren;
396 }
397
Tony Wickhameef44322015-10-20 13:24:36 -0700398 // TODO(twickham): b/25154513
399 public void setSearchBarHeight(int searchBarHeight) {
400 if (searchBarHeight == LauncherCallbacks.SEARCH_BAR_HEIGHT_TALL) {
401 hotseatBarHeightPx = hotseatBarHeightShortPx;
402 searchBarSpaceHeightPx = searchBarSpaceHeightTallPx;
403 } else {
404 hotseatBarHeightPx = hotseatBarHeightNormalPx;
405 searchBarSpaceHeightPx = searchBarSpaceHeightNormalPx;
406 }
407 }
408
Winson Chungb3800242013-10-24 11:01:54 -0700409 public void layout(Launcher launcher) {
410 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700411 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700412 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700413
414 // Layout the search bar space
Winson Chung006ee262015-08-03 14:40:11 -0700415 View searchBar = launcher.getSearchDropTargetBar();
Winson Chungb3800242013-10-24 11:01:54 -0700416 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
417 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700418 // Vertical search bar space -- The search bar is fixed in the layout to be on the left
419 // of the screen regardless of RTL
Sunny Goyald9cc7802015-04-23 14:47:19 -0700420 lp.gravity = Gravity.LEFT;
Tony Wickham55616cd2015-09-23 14:55:17 -0700421 lp.width = searchBarSpaceHeightNormalPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800422
423 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
424 targets.setOrientation(LinearLayout.VERTICAL);
Sunny Goyald9cc7802015-04-23 14:47:19 -0700425 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
426 targetsLp.gravity = Gravity.TOP;
427 targetsLp.height = LayoutParams.WRAP_CONTENT;
428
Winson Chungb3800242013-10-24 11:01:54 -0700429 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800430 // Horizontal search bar space
Sunny Goyald9cc7802015-04-23 14:47:19 -0700431 lp.gravity = Gravity.TOP;
Winson Chungb3800242013-10-24 11:01:54 -0700432 lp.height = searchBarSpaceHeightPx;
Sunny Goyald9cc7802015-04-23 14:47:19 -0700433
434 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
435 targets.getLayoutParams().width = searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700436 }
437 searchBar.setLayoutParams(lp);
438
Winson Chungb3800242013-10-24 11:01:54 -0700439 // Layout the workspace
440 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
441 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
442 lp.gravity = Gravity.CENTER;
Sunny Goyalc6205602015-05-21 20:46:33 -0700443 Rect padding = getWorkspacePadding(isLayoutRtl);
Winson Chungb3800242013-10-24 11:01:54 -0700444 workspace.setLayoutParams(lp);
445 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700446 workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl));
Winson Chungb3800242013-10-24 11:01:54 -0700447
448 // Layout the hotseat
449 View hotseat = launcher.findViewById(R.id.hotseat);
450 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700451 // We want the edges of the hotseat to line up with the edges of the workspace, but the
452 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
453 // this, we pad the left and right of the hotseat with half of the difference of a workspace
454 // cell vs a hotseat cell.
455 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
456 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
457 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700458 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700459 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
460 // screen regardless of RTL
461 lp.gravity = Gravity.RIGHT;
Tony Wickham55616cd2015-09-23 14:55:17 -0700462 lp.width = hotseatBarHeightNormalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700463 lp.height = LayoutParams.MATCH_PARENT;
464 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700465 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800466 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700467 lp.gravity = Gravity.BOTTOM;
468 lp.width = LayoutParams.MATCH_PARENT;
469 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700470 hotseat.findViewById(R.id.layout).setPadding(
471 hotseatAdjustment + padding.left, 0,
472 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700473 } else {
474 // For phones, layout the hotseat without any bottom margin
475 // to ensure that we have space for the folders
476 lp.gravity = Gravity.BOTTOM;
477 lp.width = LayoutParams.MATCH_PARENT;
478 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700479 hotseat.findViewById(R.id.layout).setPadding(
480 hotseatAdjustment + padding.left, 0,
481 hotseatAdjustment + padding.right, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700482 }
483 hotseat.setLayoutParams(lp);
484
485 // Layout the page indicators
486 View pageIndicator = launcher.findViewById(R.id.page_indicator);
487 if (pageIndicator != null) {
488 if (hasVerticalBarLayout) {
489 // Hide the page indicators when we have vertical search/hotseat
490 pageIndicator.setVisibility(View.GONE);
491 } else {
492 // Put the page indicators above the hotseat
493 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
494 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
495 lp.width = LayoutParams.WRAP_CONTENT;
496 lp.height = LayoutParams.WRAP_CONTENT;
497 lp.bottomMargin = hotseatBarHeightPx;
498 pageIndicator.setLayoutParams(lp);
499 }
500 }
501
Winson Chungb3800242013-10-24 11:01:54 -0700502 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800503 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700504 if (overviewMode != null) {
Winson Chung4f7eb402015-07-10 18:00:48 -0700505 int overviewButtonBarHeight = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700506 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
507 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700508
509 int visibleChildCount = getVisibleChildCount(overviewMode);
510 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
511 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
512
513 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chung4f7eb402015-07-10 18:00:48 -0700514 lp.height = overviewButtonBarHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700515 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700516
517 if (lp.width > totalItemWidth && visibleChildCount > 1) {
518 // We have enough space. Lets add some margin too.
519 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
520 View lastChild = null;
521
522 // Set margin of all visible children except the last visible child
523 for (int i = 0; i < visibleChildCount; i++) {
524 if (lastChild != null) {
525 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
526 if (isLayoutRtl) {
527 clp.leftMargin = margin;
528 } else {
529 clp.rightMargin = margin;
530 }
531 lastChild.setLayoutParams(clp);
532 lastChild = null;
533 }
534 View thisChild = overviewMode.getChildAt(i);
535 if (thisChild.getVisibility() != View.GONE) {
536 lastChild = thisChild;
537 }
538 }
539 }
Winson Chungb3800242013-10-24 11:01:54 -0700540 }
541 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700542
543 private int getCurrentWidth() {
544 return isLandscape
545 ? Math.max(widthPx, heightPx)
546 : Math.min(widthPx, heightPx);
547 }
548
549 private int getCurrentHeight() {
550 return isLandscape
551 ? Math.min(widthPx, heightPx)
552 : Math.max(widthPx, heightPx);
553 }
Winson Chungb3800242013-10-24 11:01:54 -0700554}