blob: 4a550ed9a89ecb6bc6afe91d745551fa39be7823 [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 Goyalc6205602015-05-21 20:46:33 -0700197 // We only care about the top and bottom workspace padding, which is not affected by RTL.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700198 Rect workspacePadding = getWorkspacePadding();
Winson Chungb3800242013-10-24 11:01:54 -0700199 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
200 if (usedHeight > maxHeight) {
201 scale = maxHeight / usedHeight;
202 drawablePadding = 0;
203 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700204 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700205 }
206
Winson Chung93f98ea2015-03-10 16:28:47 -0700207 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700208 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700209 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800210 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700211 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700212 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700213
Winson Chungb3800242013-10-24 11:01:54 -0700214 // Calculate the actual text height
215 Paint textPaint = new Paint();
216 textPaint.setTextSize(iconTextSizePx);
217 FontMetrics fm = textPaint.getFontMetrics();
218 cellWidthPx = iconSizePx;
219 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Tony Wickhame0c33232016-02-08 11:37:04 -0800220 final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
221 : 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
Sunny Goyal47328fd2016-05-25 18:56:41 -0700225 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700226 hotseatCellWidthPx = iconSizePx;
227 hotseatCellHeightPx = iconSizePx;
228
Sunny Goyal47328fd2016-05-25 18:56:41 -0700229 if (!isVerticalBarLayout()) {
230 int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
231 - pageIndicatorHeightPx - topWorkspacePadding;
232 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
233 workspaceSpringLoadShrinkFactor = Math.min(
234 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
235 1 - (minRequiredHeight / expectedWorkspaceHeight));
236 } else {
237 workspaceSpringLoadShrinkFactor =
238 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
239 }
240
Winson Chungb3800242013-10-24 11:01:54 -0700241 // Folder
Tony Wickham6159c242015-11-13 18:07:18 +0000242 int folderCellPadding = isTablet || isLandscape ? 6 * edgeMarginPx : 3 * edgeMarginPx;
243 // Don't let the folder get too close to the edges of the screen.
244 folderCellWidthPx = Math.min(cellWidthPx + folderCellPadding,
Tony Wickhama2950482015-10-12 15:57:23 -0700245 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Winson Chungb3800242013-10-24 11:01:54 -0700246 folderCellHeightPx = cellHeightPx + edgeMarginPx;
247 folderBackgroundOffset = -edgeMarginPx;
248 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800249 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700250 }
251
Winson Chungef7f8742015-06-04 17:18:17 -0700252 /**
253 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
254 */
255 public void updateAppsViewNumCols(Resources res, int recyclerViewWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700256 int appsViewLeftMarginPx =
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700257 res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700258 int allAppsCellWidthGap =
259 res.getDimensionPixelSize(R.dimen.all_apps_icon_width_gap);
Winson Chungef7f8742015-06-04 17:18:17 -0700260 int availableAppsWidthPx = (recyclerViewWidth > 0) ? recyclerViewWidth : availableWidthPx;
Sunny Goyal94f46d92016-02-16 14:56:33 -0800261 int numAppsCols = (availableAppsWidthPx + allAppsCellWidthGap - appsViewLeftMarginPx) /
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700262 (allAppsIconSizePx + allAppsCellWidthGap);
263 int numPredictiveAppCols = Math.max(inv.minAllAppsPredictionColumns, numAppsCols);
Winson Chungef7f8742015-06-04 17:18:17 -0700264 allAppsNumCols = numAppsCols;
265 allAppsNumPredictiveCols = numPredictiveAppCols;
Winson Chungb3800242013-10-24 11:01:54 -0700266 }
267
Tony Wickhama3c74d12015-10-23 11:43:47 -0700268 /** Returns the width and height of the search bar, ignoring any padding. */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700269 public Point getSearchBarDimensForWidgetOpts() {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700270 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700271 return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700272 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700273 int gap;
Sunny Goyalc6205602015-05-21 20:46:33 -0700274 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700275 // Pad the left and right of the workspace to ensure consistent spacing
276 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700277 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700278 // XXX: If the icon size changes across orientations, we will have to take
279 // that into account here too.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700280 gap = ((width - 2 * edgeMarginPx
281 - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
282 + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700283 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700284 gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
Winson Chungb3800242013-10-24 11:01:54 -0700285 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700286 return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700287 }
Winson Chungb3800242013-10-24 11:01:54 -0700288 }
289
Sunny Goyal756cd262015-08-20 12:33:21 -0700290 public Point getCellSize() {
291 Point result = new Point();
292 // Since we are only concerned with the overall padding, layout direction does
293 // not matter.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700294 Rect padding = getWorkspacePadding();
Sunny Goyal756cd262015-08-20 12:33:21 -0700295 result.x = calculateCellWidth(availableWidthPx - padding.left - padding.right,
296 inv.numColumns);
297 result.y = calculateCellHeight(availableHeightPx - padding.top - padding.bottom,
298 inv.numRows);
299 return result;
300 }
301
Winson Chungb3800242013-10-24 11:01:54 -0700302 /** Returns the workspace padding in the specified orientation */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700303 public Rect getWorkspacePadding() {
Winson Chungb3800242013-10-24 11:01:54 -0700304 Rect padding = new Rect();
Tony Wickham3a3517f2015-10-06 11:27:04 -0700305 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700306 // in case of isVerticalBarLayout, the hotseat is always on the right and the drop
307 // target bar is on the left, independent of the layout direction.
308 padding.set(dropTargetBarSizePx, edgeMarginPx, hotseatBarHeightPx, edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700309 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700310 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700311 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700312 // Pad the left and right of the workspace to ensure consistent spacing
313 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800314 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700315 int width = getCurrentWidth();
316 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700317 // The amount of screen space available for left/right padding.
318 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
319 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
320 availablePaddingX = (int) Math.min(availablePaddingX,
321 width * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700322 int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700323 - (int) (2 * inv.numRows * cellHeightPx));
Sunny Goyal47328fd2016-05-25 18:56:41 -0700324 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700325 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700326 } else {
327 // Pad the top and bottom of the workspace with search/hotseat bar sizes
328 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700329 topWorkspacePadding,
Winson Chungb3800242013-10-24 11:01:54 -0700330 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
Tony Wickhama3c74d12015-10-23 11:43:47 -0700331 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700332 }
333 }
334 return padding;
335 }
336
Sunny Goyal47328fd2016-05-25 18:56:41 -0700337 private int getWorkspacePageSpacing() {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700338 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700339 // In landscape mode the page spacing is set to the default.
340 return defaultPageSpacingPx;
341 } else {
342 // In portrait, we want the pages spaced such that there is no
343 // overhang of the previous / next page into the current page viewport.
344 // We assume symmetrical padding in portrait mode.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700345 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding().left);
Winson Chungb3800242013-10-24 11:01:54 -0700346 }
347 }
348
Winson Chung4f7eb402015-07-10 18:00:48 -0700349 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700350 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
351 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
352 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700353 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700354 }
355
356 // The rect returned will be extended to below the system ui that covers the workspace
Sunny Goyalcf25b522015-07-09 00:01:18 -0700357 public boolean isInHotseatRect(int x, int y) {
Winson Chungb3800242013-10-24 11:01:54 -0700358 if (isVerticalBarLayout()) {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700359 return (x >= (availableWidthPx - hotseatBarHeightPx))
360 && (y >= 0) && (y <= availableHeightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700361 } else {
Sunny Goyalcf25b522015-07-09 00:01:18 -0700362 return (x >= 0) && (x <= availableWidthPx)
363 && (y >= (availableHeightPx - hotseatBarHeightPx));
Winson Chungb3800242013-10-24 11:01:54 -0700364 }
365 }
366
Adam Cohen2e6da152015-05-06 11:42:25 -0700367 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700368 return width / countX;
369 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700370 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700371 return height / countY;
372 }
373
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700374 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700375 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
376 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
377 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700378 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700379 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700380 return isLandscape && transposeLayoutWithOrientation;
381 }
382
383 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700384 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700385 }
386
Sunny Goyalc6205602015-05-21 20:46:33 -0700387 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800388 int visibleChildren = 0;
389 for (int i = 0; i < parent.getChildCount(); i++) {
390 if (parent.getChildAt(i).getVisibility() != View.GONE) {
391 visibleChildren++;
392 }
393 }
394 return visibleChildren;
395 }
396
Winson Chungb3800242013-10-24 11:01:54 -0700397 public void layout(Launcher launcher) {
398 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700399 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700400 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700401
402 // Layout the search bar space
Sunny Goyal47328fd2016-05-25 18:56:41 -0700403 Point searchBarBounds = getSearchBarDimensForWidgetOpts();
404 View searchBar = launcher.getDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700405 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700406 lp.width = searchBarBounds.x;
407 lp.height = searchBarBounds.y;
408 lp.topMargin = edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700409 searchBar.setLayoutParams(lp);
410
Winson Chungb3800242013-10-24 11:01:54 -0700411 // Layout the workspace
412 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
413 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
414 lp.gravity = Gravity.CENTER;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700415 Rect padding = getWorkspacePadding();
Winson Chungb3800242013-10-24 11:01:54 -0700416 workspace.setLayoutParams(lp);
417 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700418 workspace.setPageSpacing(getWorkspacePageSpacing());
Winson Chungb3800242013-10-24 11:01:54 -0700419
420 // Layout the hotseat
421 View hotseat = launcher.findViewById(R.id.hotseat);
422 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700423 // We want the edges of the hotseat to line up with the edges of the workspace, but the
424 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
425 // this, we pad the left and right of the hotseat with half of the difference of a workspace
426 // cell vs a hotseat cell.
427 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
428 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
429 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700430 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700431 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
432 // screen regardless of RTL
433 lp.gravity = Gravity.RIGHT;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700434 lp.width = hotseatBarHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700435 lp.height = LayoutParams.MATCH_PARENT;
436 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Sunny Goyalc6205602015-05-21 20:46:33 -0700437 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800438 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700439 lp.gravity = Gravity.BOTTOM;
440 lp.width = LayoutParams.MATCH_PARENT;
441 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700442 hotseat.findViewById(R.id.layout).setPadding(
443 hotseatAdjustment + padding.left, 0,
444 hotseatAdjustment + padding.right, 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700445 } else {
446 // For phones, layout the hotseat without any bottom margin
447 // to ensure that we have space for the folders
448 lp.gravity = Gravity.BOTTOM;
449 lp.width = LayoutParams.MATCH_PARENT;
450 lp.height = hotseatBarHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700451 hotseat.findViewById(R.id.layout).setPadding(
452 hotseatAdjustment + padding.left, 0,
453 hotseatAdjustment + padding.right, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700454 }
455 hotseat.setLayoutParams(lp);
456
457 // Layout the page indicators
458 View pageIndicator = launcher.findViewById(R.id.page_indicator);
459 if (pageIndicator != null) {
460 if (hasVerticalBarLayout) {
461 // Hide the page indicators when we have vertical search/hotseat
462 pageIndicator.setVisibility(View.GONE);
463 } else {
464 // Put the page indicators above the hotseat
465 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
466 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
467 lp.width = LayoutParams.WRAP_CONTENT;
Winson Chungb3800242013-10-24 11:01:54 -0700468 lp.bottomMargin = hotseatBarHeightPx;
469 pageIndicator.setLayoutParams(lp);
470 }
471 }
472
Winson Chungb3800242013-10-24 11:01:54 -0700473 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800474 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700475 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700476 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
477 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700478
479 int visibleChildCount = getVisibleChildCount(overviewMode);
480 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
481 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
482
483 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700484 lp.height = getOverviewModeButtonBarHeight();
Winson Chungb3800242013-10-24 11:01:54 -0700485 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700486
487 if (lp.width > totalItemWidth && visibleChildCount > 1) {
488 // We have enough space. Lets add some margin too.
489 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
490 View lastChild = null;
491
492 // Set margin of all visible children except the last visible child
493 for (int i = 0; i < visibleChildCount; i++) {
494 if (lastChild != null) {
495 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
496 if (isLayoutRtl) {
497 clp.leftMargin = margin;
498 } else {
499 clp.rightMargin = margin;
500 }
501 lastChild.setLayoutParams(clp);
502 lastChild = null;
503 }
504 View thisChild = overviewMode.getChildAt(i);
505 if (thisChild.getVisibility() != View.GONE) {
506 lastChild = thisChild;
507 }
508 }
509 }
Winson Chungb3800242013-10-24 11:01:54 -0700510 }
511 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700512
513 private int getCurrentWidth() {
514 return isLandscape
515 ? Math.max(widthPx, heightPx)
516 : Math.min(widthPx, heightPx);
517 }
518
519 private int getCurrentHeight() {
520 return isLandscape
521 ? Math.min(widthPx, heightPx)
522 : Math.max(widthPx, heightPx);
523 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700524
525
Sunny Goyale27815e2016-04-04 17:51:04 -0700526 public static final int getContainerPadding(Context context, int availableWidth) {
Sunny Goyal13178ac2016-04-04 16:35:22 -0700527 Resources res = context.getResources();
528
529 int maxSize = res.getDimensionPixelSize(R.dimen.container_max_width);
530 int minMargin = res.getDimensionPixelSize(R.dimen.container_min_margin);
531
532 if (maxSize > 0) {
533 return Math.max(minMargin, (availableWidth - maxSize) / 2);
534 } else {
535 return Math.max(minMargin,
536 (int) res.getFraction(R.fraction.container_margin, availableWidth, 1));
537 }
538 }
Winson Chungb3800242013-10-24 11:01:54 -0700539}