blob: 2b130e5a71f59178d523ca524cab913e52e7a79d [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;
34
Tony Wickhame0c33232016-02-08 11:37:04 -080035import com.android.launcher3.config.FeatureFlags;
36
Winson Chungb3800242013-10-24 11:01:54 -070037public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070038
Adam Cohen2e6da152015-05-06 11:42:25 -070039 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070040
Sunny Goyalc6205602015-05-21 20:46:33 -070041 // Device properties
42 public final boolean isTablet;
43 public final boolean isLargeTablet;
44 public final boolean isPhone;
45 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070046
Sunny Goyalc6205602015-05-21 20:46:33 -070047 // Device properties in current orientation
48 public final boolean isLandscape;
49 public final int widthPx;
50 public final int heightPx;
51 public final int availableWidthPx;
52 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070053 /**
54 * The maximum amount of left/right workspace padding as a percentage of the screen width.
55 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
56 * 7% of the screen width can be used as right padding.
57 */
58 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070059
Sunny Goyalc6205602015-05-21 20:46:33 -070060 // Overview mode
61 private final int overviewModeMinIconZoneHeightPx;
62 private final int overviewModeMaxIconZoneHeightPx;
63 private final int overviewModeBarItemWidthPx;
64 private final int overviewModeBarSpacerWidthPx;
65 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070066
Sunny Goyalc6205602015-05-21 20:46:33 -070067 // Workspace
68 private int desiredWorkspaceLeftRightMarginPx;
69 public final int edgeMarginPx;
70 public final Rect defaultWidgetPadding;
71 private final int pageIndicatorHeightPx;
72 private final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070073 private final int topWorkspacePadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070074 private float dragViewScale;
Sunny Goyal47328fd2016-05-25 18:56:41 -070075 public float workspaceSpringLoadShrinkFactor;
76 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070077
78 // Workspace icons
79 public int iconSizePx;
80 public int iconTextSizePx;
81 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070082 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070083
Adam Cohen59400422014-03-05 18:07:04 -080084 public int cellWidthPx;
85 public int cellHeightPx;
86
Sunny Goyalc6205602015-05-21 20:46:33 -070087 // Folder
88 public int folderBackgroundOffset;
89 public int folderIconSizePx;
Adam Cohenefca0272016-02-24 19:19:06 -080090 public int folderIconPreviewPadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070091 public int folderCellWidthPx;
92 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070093
Sunny Goyalc6205602015-05-21 20:46:33 -070094 // Hotseat
95 public int hotseatCellWidthPx;
96 public int hotseatCellHeightPx;
97 public int hotseatIconSizePx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070098 private int hotseatBarHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -070099
Sunny Goyalc6205602015-05-21 20:46:33 -0700100 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700101 public int allAppsNumCols;
102 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -0700103 public int allAppsButtonVisualSize;
104 public final int allAppsIconSizePx;
Sunny Goyal383e0a22015-12-10 12:28:21 -0800105 public final float allAppsIconTextSizeSp;
Winson Chung59a488a2013-12-10 12:32:14 -0800106
Sunny Goyal47328fd2016-05-25 18:56:41 -0700107 // Drop Target
108 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700109
Sunny Goyalc6205602015-05-21 20:46:33 -0700110 public DeviceProfile(Context context, InvariantDeviceProfile inv,
111 Point minSize, Point maxSize,
112 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700113
Adam Cohen2e6da152015-05-06 11:42:25 -0700114 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700115 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700116
Adam Cohen2e6da152015-05-06 11:42:25 -0700117 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700118 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700119
Sunny Goyalc6205602015-05-21 20:46:33 -0700120 // Constants from resources
121 isTablet = res.getBoolean(R.bool.is_tablet);
122 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
123 isPhone = !isTablet && !isLargeTablet;
124
125 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700126 transposeLayoutWithOrientation =
127 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700128
129 ComponentName cn = new ComponentName(context.getPackageName(),
130 this.getClass().getName());
131 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
132 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
133 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
134 pageIndicatorHeightPx =
135 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
136 defaultPageSpacingPx =
137 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700138 topWorkspacePadding =
139 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700140 overviewModeMinIconZoneHeightPx =
141 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
142 overviewModeMaxIconZoneHeightPx =
143 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800144 overviewModeBarItemWidthPx =
145 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
146 overviewModeBarSpacerWidthPx =
147 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700148 overviewModeIconZoneRatio =
149 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700150 iconDrawablePaddingOriginalPx =
151 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700152 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
153 workspaceSpringLoadedBottomSpace =
154 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Winson Chungb3800242013-10-24 11:01:54 -0700155
Adam Cohen2e6da152015-05-06 11:42:25 -0700156 // AllApps uses the original non-scaled icon text size
Sunny Goyal383e0a22015-12-10 12:28:21 -0800157 allAppsIconTextSizeSp = inv.iconTextSize;
Adam Cohen2e6da152015-05-06 11:42:25 -0700158
159 // AllApps uses the original non-scaled icon size
160 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700161
Sunny Goyalc6205602015-05-21 20:46:33 -0700162 // Determine sizes.
163 widthPx = width;
164 heightPx = height;
165 if (isLandscape) {
166 availableWidthPx = maxSize.x;
167 availableHeightPx = minSize.y;
168 } else {
169 availableWidthPx = minSize.x;
170 availableHeightPx = maxSize.y;
171 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700172
Winson Chungb3800242013-10-24 11:01:54 -0700173 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700174 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700175 computeAllAppsButtonSize(context);
176 }
177
178 /**
179 * Determine the exact visual footprint of the all apps button, taking into account scaling
180 * and internal padding of the drawable.
181 */
182 private void computeAllAppsButtonSize(Context context) {
183 Resources res = context.getResources();
184 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700185 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
186 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700187 }
188
Sunny Goyalc6205602015-05-21 20:46:33 -0700189 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700190 // Check to see if the icons fit in the new available height. If not, then we need to
191 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700192 float scale = 1f;
193 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700194 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700195 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800196
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700197 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700198 if (usedHeight > maxHeight) {
199 scale = maxHeight / usedHeight;
200 drawablePadding = 0;
201 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700202 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700203 }
204
Winson Chung93f98ea2015-03-10 16:28:47 -0700205 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700206 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700207 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800208 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700209 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700210 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700211
Winson Chungb3800242013-10-24 11:01:54 -0700212 // Calculate the actual text height
213 Paint textPaint = new Paint();
214 textPaint.setTextSize(iconTextSizePx);
215 FontMetrics fm = textPaint.getFontMetrics();
216 cellWidthPx = iconSizePx;
217 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Tony Wickhame0c33232016-02-08 11:37:04 -0800218 final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
219 : res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800220 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700221
222 // Hotseat
Sunny Goyal47328fd2016-05-25 18:56:41 -0700223 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700224 hotseatCellWidthPx = iconSizePx;
225 hotseatCellHeightPx = iconSizePx;
226
Sunny Goyal47328fd2016-05-25 18:56:41 -0700227 if (!isVerticalBarLayout()) {
228 int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
229 - pageIndicatorHeightPx - topWorkspacePadding;
230 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
231 workspaceSpringLoadShrinkFactor = Math.min(
232 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
233 1 - (minRequiredHeight / expectedWorkspaceHeight));
234 } else {
235 workspaceSpringLoadShrinkFactor =
236 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
237 }
238
Winson Chungb3800242013-10-24 11:01:54 -0700239 // Folder
Tony Wickham6159c242015-11-13 18:07:18 +0000240 int folderCellPadding = isTablet || isLandscape ? 6 * edgeMarginPx : 3 * edgeMarginPx;
241 // Don't let the folder get too close to the edges of the screen.
242 folderCellWidthPx = Math.min(cellWidthPx + folderCellPadding,
Tony Wickhama2950482015-10-12 15:57:23 -0700243 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Winson Chungb3800242013-10-24 11:01:54 -0700244 folderCellHeightPx = cellHeightPx + edgeMarginPx;
245 folderBackgroundOffset = -edgeMarginPx;
246 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800247 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700248 }
249
Winson Chungef7f8742015-06-04 17:18:17 -0700250 /**
251 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
252 */
253 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700254 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700255 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700256 int allAppsCellWidthGap =
257 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700258 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Sunny Goyal94f46d92016-02-16 14:56:33 -0800259 int numAppsCols = (availableAppsWidthPx + allAppsCellWidthGap - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700260 (allAppsIconSizePx + allAppsCellWidthGap);
261 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700262 allAppsNumCols = numAppsCols;
263 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700264 }
265
Tony Wickhama3c74d12015-10-23 11:43:47 -0700266 /** Returns the width and height of the search bar, ignoring any padding. */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700267 public Point getSearchBarDimensForWidgetOpts() {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700268 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700269 return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700270 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700271 int gap;
Sunny Goyalc6205602015-05-21 20:46:33 -0700272 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700273 // Pad the left and right of the workspace to ensure consistent spacing
274 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700275 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700276 // XXX: If the icon size changes across orientations, we will have to take
277 // that into account here too.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700278 gap = ((width - 2 * edgeMarginPx
279 - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
280 + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700281 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700282 gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
Winson Chungb3800242013-10-24 11:01:54 -0700283 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700284 return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700285 }
Winson Chungb3800242013-10-24 11:01:54 -0700286 }
287
Sunny Goyal756cd262015-08-20 12:33:21 -0700288 public Point getCellSize() {
289 Point result = new Point();
290 // Since we are only concerned with the overall padding, layout direction does
291 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700292 Point padding = getTotalWorkspacePadding();
293 result.x = calculateCellWidth(availableWidthPx - padding.x, inv.numColumns);
294 result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700295 return result;
296 }
297
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700298 public Point getTotalWorkspacePadding() {
Sunny Goyal6178f132016-07-11 17:30:03 -0700299 Rect padding = getWorkspacePadding(null);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700300 return new Point(padding.left + padding.right, padding.top + padding.bottom);
301 }
302
303 /**
304 * Returns the workspace padding in the specified orientation.
305 * Note that it assumes that while in verticalBarLayout, the nav bar is on the right, as such
306 * this value is not reliable.
307 * Use {@link #getTotalWorkspacePadding()} instead.
308 */
Sunny Goyal6178f132016-07-11 17:30:03 -0700309 public Rect getWorkspacePadding(Rect recycle) {
310 Rect padding = recycle == null ? new Rect() : recycle;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700311 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700312 // in case of isVerticalBarLayout, the hotseat is always on the right and the drop
313 // target bar is on the left, independent of the layout direction.
314 padding.set(dropTargetBarSizePx, edgeMarginPx, hotseatBarHeightPx, edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700315 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700316 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700317 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700318 // Pad the left and right of the workspace to ensure consistent spacing
319 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800320 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700321 int width = getCurrentWidth();
322 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700323 // The amount of screen space available for left/right padding.
324 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
325 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
326 availablePaddingX = (int) Math.min(availablePaddingX,
327 width * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700328 int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700329 - (int) (2 * inv.numRows * cellHeightPx));
Sunny Goyal47328fd2016-05-25 18:56:41 -0700330 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700331 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700332 } else {
333 // Pad the top and bottom of the workspace with search/hotseat bar sizes
334 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700335 topWorkspacePadding,
Winson Chungb3800242013-10-24 11:01:54 -0700336 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700337 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700338 }
339 }
340 return padding;
341 }
342
Sunny Goyal47328fd2016-05-25 18:56:41 -0700343 private int getWorkspacePageSpacing() {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700344 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700345 // In landscape mode the page spacing is set to the default.
346 return defaultPageSpacingPx;
347 } else {
348 // In portrait, we want the pages spaced such that there is no
349 // overhang of the previous / next page into the current page viewport.
350 // We assume symmetrical padding in portrait mode.
Sunny Goyal6178f132016-07-11 17:30:03 -0700351 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(null).left);
Winson Chungb3800242013-10-24 11:01:54 -0700352 }
353 }
354
Winson Chung4f7eb402015-07-10 18:00:48 -0700355 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700356 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
357 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
358 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700359 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700360 }
361
Adam Cohen2e6da152015-05-06 11:42:25 -0700362 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700363 return width / countX;
364 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700365 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700366 return height / countY;
367 }
368
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700369 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700370 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
371 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
372 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700373 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700374 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700375 return isLandscape && transposeLayoutWithOrientation;
376 }
377
378 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700379 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700380 }
381
Sunny Goyalc6205602015-05-21 20:46:33 -0700382 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800383 int visibleChildren = 0;
384 for (int i = 0; i < parent.getChildCount(); i++) {
385 if (parent.getChildAt(i).getVisibility() != View.GONE) {
386 visibleChildren++;
387 }
388 }
389 return visibleChildren;
390 }
391
Winson Chungb3800242013-10-24 11:01:54 -0700392 public void layout(Launcher launcher) {
393 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700394 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700395 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700396
397 // Layout the search bar space
Sunny Goyal47328fd2016-05-25 18:56:41 -0700398 Point searchBarBounds = getSearchBarDimensForWidgetOpts();
399 View searchBar = launcher.getDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700400 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700401 lp.width = searchBarBounds.x;
402 lp.height = searchBarBounds.y;
403 lp.topMargin = edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700404 searchBar.setLayoutParams(lp);
405
Winson Chungb3800242013-10-24 11:01:54 -0700406 // Layout the workspace
407 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -0700408 Rect padding = getWorkspacePadding(null);
Winson Chungb3800242013-10-24 11:01:54 -0700409 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700410 workspace.setPageSpacing(getWorkspacePageSpacing());
Winson Chungb3800242013-10-24 11:01:54 -0700411
Sunny Goyal6178f132016-07-11 17:30:03 -0700412 View qsbContainer = launcher.getQsbContainer();
413 lp = (FrameLayout.LayoutParams) qsbContainer.getLayoutParams();
414 lp.topMargin = padding.top;
415 qsbContainer.setLayoutParams(lp);
416
Winson Chungb3800242013-10-24 11:01:54 -0700417 // Layout the hotseat
418 View hotseat = launcher.findViewById(R.id.hotseat);
419 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700420 // We want the edges of the hotseat to line up with the edges of the workspace, but the
421 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
422 // this, we pad the left and right of the hotseat with half of the difference of a workspace
423 // cell vs a hotseat cell.
424 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
425 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
426 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700427 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700428 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
429 // screen regardless of RTL
430 lp.gravity = Gravity.RIGHT;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700431 lp.width = hotseatBarHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700432 lp.height = LayoutParams.MATCH_PARENT;
433 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700434 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800435 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700436 lp.gravity = Gravity.BOTTOM;
437 lp.width = LayoutParams.MATCH_PARENT;
438 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700439 hotseat.findViewById(R.id.layout).setPadding(
440 hotseatAdjustment + padding.left, 0,
441 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700442 } else {
443 // For phones, layout the hotseat without any bottom margin
444 // to ensure that we have space for the folders
445 lp.gravity = Gravity.BOTTOM;
446 lp.width = LayoutParams.MATCH_PARENT;
447 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700448 hotseat.findViewById(R.id.layout).setPadding(
449 hotseatAdjustment + padding.left, 0,
450 hotseatAdjustment + padding.right, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700451 }
452 hotseat.setLayoutParams(lp);
453
454 // Layout the page indicators
455 View pageIndicator = launcher.findViewById(R.id.page_indicator);
456 if (pageIndicator != null) {
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700457 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
458 if (!hasVerticalBarLayout) {
Winson Chungb3800242013-10-24 11:01:54 -0700459 // Put the page indicators above the hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700460 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
461 lp.width = LayoutParams.WRAP_CONTENT;
Winson Chungb3800242013-10-24 11:01:54 -0700462 lp.bottomMargin = hotseatBarHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700463 }
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700464 pageIndicator.setLayoutParams(lp);
Winson Chungb3800242013-10-24 11:01:54 -0700465 }
466
Winson Chungb3800242013-10-24 11:01:54 -0700467 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800468 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700469 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700470 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
471 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700472
473 int visibleChildCount = getVisibleChildCount(overviewMode);
474 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
475 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
476
477 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700478 lp.height = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700479 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700480
481 if (lp.width > totalItemWidth && visibleChildCount > 1) {
482 // We have enough space. Lets add some margin too.
483 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
484 View lastChild = null;
485
486 // Set margin of all visible children except the last visible child
487 for (int i = 0; i < visibleChildCount; i++) {
488 if (lastChild != null) {
489 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
490 if (isLayoutRtl) {
491 clp.leftMargin = margin;
492 } else {
493 clp.rightMargin = margin;
494 }
495 lastChild.setLayoutParams(clp);
496 lastChild = null;
497 }
498 View thisChild = overviewMode.getChildAt(i);
499 if (thisChild.getVisibility() != View.GONE) {
500 lastChild = thisChild;
501 }
502 }
503 }
Winson Chungb3800242013-10-24 11:01:54 -0700504 }
505 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700506
507 private int getCurrentWidth() {
508 return isLandscape
509 ? Math.max(widthPx, heightPx)
510 : Math.min(widthPx, heightPx);
511 }
512
513 private int getCurrentHeight() {
514 return isLandscape
515 ? Math.min(widthPx, heightPx)
516 : Math.max(widthPx, heightPx);
517 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700518
519
Sunny Goyale27815e2016-04-04 17:51:04 -0700520 public static final int getContainerPadding(Context context, int availableWidth) {
Sunny Goyal13178ac2016-04-04 16:35:22 -0700521 Resources res = context.getResources();
522
523 int maxSize = res.getDimensionPixelSize(R.dimen.container_max_width);
524 int minMargin = res.getDimensionPixelSize(R.dimen.container_min_margin);
525
526 if (maxSize > 0) {
527 return Math.max(minMargin, (availableWidth - maxSize) / 2);
528 } else {
529 return Math.max(minMargin,
530 (int) res.getFraction(R.fraction.container_margin, availableWidth, 1));
531 }
532 }
Winson Chungb3800242013-10-24 11:01:54 -0700533}