blob: 655c21868fe8db6f0bf01562b28e6d5d743a2720 [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;
Winson Chungb3800242013-10-24 11:01:54 -070023import android.graphics.Point;
Winson Chungb3800242013-10-24 11:01:54 -070024import android.graphics.Rect;
25import android.util.DisplayMetrics;
Winson Chungb3800242013-10-24 11:01:54 -070026import android.view.Gravity;
Winson Chungb3800242013-10-24 11:01:54 -070027import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080028import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.view.ViewGroup.LayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070030import android.widget.FrameLayout;
31
Tony Wickhame0c33232016-02-08 11:37:04 -080032import com.android.launcher3.config.FeatureFlags;
33
Winson1f064272016-07-18 17:18:02 -070034import java.util.ArrayList;
35
Winson Chungb3800242013-10-24 11:01:54 -070036public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070037
Winson1f064272016-07-18 17:18:02 -070038 public interface LauncherLayoutChangeListener {
39 void onLauncherLayoutChanged();
40 }
41
Adam Cohen2e6da152015-05-06 11:42:25 -070042 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070043
Sunny Goyalc6205602015-05-21 20:46:33 -070044 // Device properties
45 public final boolean isTablet;
46 public final boolean isLargeTablet;
47 public final boolean isPhone;
48 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070049
Sunny Goyalc6205602015-05-21 20:46:33 -070050 // Device properties in current orientation
51 public final boolean isLandscape;
52 public final int widthPx;
53 public final int heightPx;
54 public final int availableWidthPx;
55 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070056 /**
57 * The maximum amount of left/right workspace padding as a percentage of the screen width.
58 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
59 * 7% of the screen width can be used as right padding.
60 */
61 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070062
Sunny Goyalc6205602015-05-21 20:46:33 -070063 // Overview mode
64 private final int overviewModeMinIconZoneHeightPx;
65 private final int overviewModeMaxIconZoneHeightPx;
66 private final int overviewModeBarItemWidthPx;
67 private final int overviewModeBarSpacerWidthPx;
68 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070069
Sunny Goyalc6205602015-05-21 20:46:33 -070070 // Workspace
71 private int desiredWorkspaceLeftRightMarginPx;
72 public final int edgeMarginPx;
73 public final Rect defaultWidgetPadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070074 private final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070075 private final int topWorkspacePadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070076 private float dragViewScale;
Sunny Goyal47328fd2016-05-25 18:56:41 -070077 public float workspaceSpringLoadShrinkFactor;
78 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070079
Winson1f064272016-07-18 17:18:02 -070080 // Page indicator
81 private final int pageIndicatorHeightPx;
82 private final int pageIndicatorLandGutterLeftNavBarPx;
83 private final int pageIndicatorLandGutterRightNavBarPx;
84 private final int pageIndicatorLandWorkspaceOffsetPx;
85
Sunny Goyalc6205602015-05-21 20:46:33 -070086 // Workspace icons
87 public int iconSizePx;
88 public int iconTextSizePx;
89 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070090 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070091
Adam Cohen59400422014-03-05 18:07:04 -080092 public int cellWidthPx;
93 public int cellHeightPx;
94
Sunny Goyalc6205602015-05-21 20:46:33 -070095 // Folder
96 public int folderBackgroundOffset;
97 public int folderIconSizePx;
Adam Cohenefca0272016-02-24 19:19:06 -080098 public int folderIconPreviewPadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070099 public int folderCellWidthPx;
100 public int folderCellHeightPx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700101 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700102
Sunny Goyalc6205602015-05-21 20:46:33 -0700103 // Hotseat
104 public int hotseatCellWidthPx;
105 public int hotseatCellHeightPx;
106 public int hotseatIconSizePx;
Jon Mirandaaca21452016-09-26 14:01:56 -0700107 public int hotseatBarHeightPx;
Winson1f064272016-07-18 17:18:02 -0700108 private int hotseatBarTopPaddingPx;
109 private int hotseatLandGutterPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700110
Sunny Goyalc6205602015-05-21 20:46:33 -0700111 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700112 public int allAppsNumCols;
113 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -0700114 public int allAppsButtonVisualSize;
Winson1f064272016-07-18 17:18:02 -0700115 public int allAppsIconSizePx;
116 public int allAppsIconDrawablePaddingPx;
117 public float allAppsIconTextSizePx;
118
119 // Containers
120 private final int containerLeftPaddingPx;
121 private final int containerRightPaddingPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800122
Sunny Goyal47328fd2016-05-25 18:56:41 -0700123 // Drop Target
124 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700125
Winson1f064272016-07-18 17:18:02 -0700126 // Insets
127 private Rect mInsets = new Rect();
128
129 // Listeners
130 private ArrayList<LauncherLayoutChangeListener> mListeners = new ArrayList<>();
131
Sunny Goyalc6205602015-05-21 20:46:33 -0700132 public DeviceProfile(Context context, InvariantDeviceProfile inv,
133 Point minSize, Point maxSize,
134 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700135
Adam Cohen2e6da152015-05-06 11:42:25 -0700136 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700137 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700138
Adam Cohen2e6da152015-05-06 11:42:25 -0700139 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700140 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700141
Sunny Goyalc6205602015-05-21 20:46:33 -0700142 // Constants from resources
143 isTablet = res.getBoolean(R.bool.is_tablet);
144 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
145 isPhone = !isTablet && !isLargeTablet;
146
147 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700148 transposeLayoutWithOrientation =
149 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700150
151 ComponentName cn = new ComponentName(context.getPackageName(),
152 this.getClass().getName());
153 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
154 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Winson1f064272016-07-18 17:18:02 -0700155 desiredWorkspaceLeftRightMarginPx = edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700156 pageIndicatorHeightPx =
157 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
Winson1f064272016-07-18 17:18:02 -0700158 pageIndicatorLandGutterLeftNavBarPx = res.getDimensionPixelSize(
159 R.dimen.dynamic_grid_page_indicator_gutter_width_left_nav_bar);
160 pageIndicatorLandWorkspaceOffsetPx =
161 res.getDimensionPixelSize(R.dimen.all_apps_caret_workspace_offset);
162 pageIndicatorLandGutterRightNavBarPx = res.getDimensionPixelSize(
163 R.dimen.dynamic_grid_page_indicator_gutter_width_right_nav_bar);
Winson Chungb3800242013-10-24 11:01:54 -0700164 defaultPageSpacingPx =
165 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700166 topWorkspacePadding =
167 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700168 overviewModeMinIconZoneHeightPx =
169 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
170 overviewModeMaxIconZoneHeightPx =
171 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800172 overviewModeBarItemWidthPx =
173 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
174 overviewModeBarSpacerWidthPx =
175 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700176 overviewModeIconZoneRatio =
177 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700178 iconDrawablePaddingOriginalPx =
179 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700180 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
181 workspaceSpringLoadedBottomSpace =
182 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Winson1f064272016-07-18 17:18:02 -0700183 hotseatBarHeightPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height);
184 hotseatBarTopPaddingPx =
185 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
186 hotseatLandGutterPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_gutter_width);
187 containerLeftPaddingPx =
188 res.getDimensionPixelSize(R.dimen.dynamic_grid_container_land_left_padding);
189 containerRightPaddingPx =
190 res.getDimensionPixelSize(R.dimen.dynamic_grid_container_land_right_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700191
Sunny Goyalc6205602015-05-21 20:46:33 -0700192 // Determine sizes.
193 widthPx = width;
194 heightPx = height;
195 if (isLandscape) {
196 availableWidthPx = maxSize.x;
197 availableHeightPx = minSize.y;
198 } else {
199 availableWidthPx = minSize.x;
200 availableHeightPx = maxSize.y;
201 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700202
Winson Chungb3800242013-10-24 11:01:54 -0700203 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700204 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700205 computeAllAppsButtonSize(context);
206 }
207
Winson1f064272016-07-18 17:18:02 -0700208 public void addLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
209 if (!mListeners.contains(listener)) {
210 mListeners.add(listener);
211 }
212 }
213
214 public void removeLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
215 if (mListeners.contains(listener)) {
216 mListeners.remove(listener);
217 }
218 }
219
Adam Cohen63f1ec02014-08-12 09:23:13 -0700220 /**
221 * Determine the exact visual footprint of the all apps button, taking into account scaling
222 * and internal padding of the drawable.
223 */
224 private void computeAllAppsButtonSize(Context context) {
225 Resources res = context.getResources();
226 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700227 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
228 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700229 }
230
Sunny Goyalc6205602015-05-21 20:46:33 -0700231 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700232 // Check to see if the icons fit in the new available height. If not, then we need to
233 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700234 float scale = 1f;
235 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700236 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700237 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800238
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700239 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700240 if (usedHeight > maxHeight) {
241 scale = maxHeight / usedHeight;
242 drawablePadding = 0;
243 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700244 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700245 }
246
Winson Chung93f98ea2015-03-10 16:28:47 -0700247 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700248 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700249 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800250 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700251 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700252 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson1f064272016-07-18 17:18:02 -0700253 allAppsIconSizePx = iconSizePx;
254 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
255 allAppsIconTextSizePx = iconTextSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700256
Winson Chungb3800242013-10-24 11:01:54 -0700257 cellWidthPx = iconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700258 cellHeightPx = iconSizePx + iconDrawablePaddingPx
259 + Utilities.calculateTextHeight(iconTextSizePx);
Tony Wickhame0c33232016-02-08 11:37:04 -0800260 final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
261 : res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800262 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700263
264 // Hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700265 hotseatCellWidthPx = iconSizePx;
266 hotseatCellHeightPx = iconSizePx;
267
Sunny Goyal47328fd2016-05-25 18:56:41 -0700268 if (!isVerticalBarLayout()) {
269 int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
270 - pageIndicatorHeightPx - topWorkspacePadding;
271 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
272 workspaceSpringLoadShrinkFactor = Math.min(
273 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
274 1 - (minRequiredHeight / expectedWorkspaceHeight));
275 } else {
276 workspaceSpringLoadShrinkFactor =
277 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
278 }
279
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700280 // Folder cell
281 int cellPaddingX = res.getDimensionPixelSize(R.dimen.folder_cell_x_padding);
282 int cellPaddingY = res.getDimensionPixelSize(R.dimen.folder_cell_y_padding);
283 final int folderChildTextSize =
284 Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_child_text_size));
285
286 final int folderBottomPanelSize =
Sunny Goyal68f240e2016-10-07 13:36:15 -0700287 res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
288 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700289 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
290
Tony Wickham6159c242015-11-13 18:07:18 +0000291 // Don't let the folder get too close to the edges of the screen.
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700292 folderCellWidthPx = Math.min(iconSizePx + 2 * cellPaddingX,
Tony Wickhama2950482015-10-12 15:57:23 -0700293 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700294 folderCellHeightPx = Math.min(iconSizePx + 3 * cellPaddingY + folderChildTextSize,
295 (availableHeightPx - 4 * edgeMarginPx - folderBottomPanelSize) / inv.numFolderRows);
296 folderChildDrawablePaddingPx = Math.max(0,
297 (folderCellHeightPx - iconSizePx - folderChildTextSize) / 3);
298
299 // Folder icon
Winson Chungb3800242013-10-24 11:01:54 -0700300 folderBackgroundOffset = -edgeMarginPx;
301 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800302 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700303 }
304
Winson1f064272016-07-18 17:18:02 -0700305 public void updateInsets(Rect insets) {
306 mInsets.set(insets);
307 }
308
Winson1f064272016-07-18 17:18:02 -0700309 public void updateAppsViewNumCols() {
310 allAppsNumCols = allAppsNumPredictiveCols = inv.numColumns;
Winson Chungb3800242013-10-24 11:01:54 -0700311 }
312
Tony Wickhama3c74d12015-10-23 11:43:47 -0700313 /** Returns the width and height of the search bar, ignoring any padding. */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700314 public Point getSearchBarDimensForWidgetOpts() {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700315 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700316 return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700317 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700318 int gap;
Sunny Goyalc6205602015-05-21 20:46:33 -0700319 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700320 // Pad the left and right of the workspace to ensure consistent spacing
321 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700322 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700323 // XXX: If the icon size changes across orientations, we will have to take
324 // that into account here too.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700325 gap = ((width - 2 * edgeMarginPx
326 - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
327 + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700328 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700329 gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
Winson Chungb3800242013-10-24 11:01:54 -0700330 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700331 return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700332 }
Winson Chungb3800242013-10-24 11:01:54 -0700333 }
334
Sunny Goyal756cd262015-08-20 12:33:21 -0700335 public Point getCellSize() {
336 Point result = new Point();
337 // Since we are only concerned with the overall padding, layout direction does
338 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700339 Point padding = getTotalWorkspacePadding();
340 result.x = calculateCellWidth(availableWidthPx - padding.x, inv.numColumns);
341 result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700342 return result;
343 }
344
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700345 public Point getTotalWorkspacePadding() {
Sunny Goyal6178f132016-07-11 17:30:03 -0700346 Rect padding = getWorkspacePadding(null);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700347 return new Point(padding.left + padding.right, padding.top + padding.bottom);
348 }
349
350 /**
351 * Returns the workspace padding in the specified orientation.
352 * Note that it assumes that while in verticalBarLayout, the nav bar is on the right, as such
353 * this value is not reliable.
354 * Use {@link #getTotalWorkspacePadding()} instead.
355 */
Sunny Goyal6178f132016-07-11 17:30:03 -0700356 public Rect getWorkspacePadding(Rect recycle) {
357 Rect padding = recycle == null ? new Rect() : recycle;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700358 if (isVerticalBarLayout()) {
Winson1f064272016-07-18 17:18:02 -0700359 if (mInsets.left > 0) {
360 padding.set(mInsets.left + pageIndicatorLandGutterLeftNavBarPx, 0,
361 hotseatBarHeightPx + hotseatLandGutterPx - mInsets.left, 2 * edgeMarginPx);
362 } else {
363 padding.set(pageIndicatorLandGutterRightNavBarPx, 0,
364 hotseatBarHeightPx + hotseatLandGutterPx, 2 * edgeMarginPx);
365 }
Winson Chungb3800242013-10-24 11:01:54 -0700366 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700367 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700368 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700369 // Pad the left and right of the workspace to ensure consistent spacing
370 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800371 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700372 int width = getCurrentWidth();
373 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700374 // The amount of screen space available for left/right padding.
375 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
376 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
377 availablePaddingX = (int) Math.min(availablePaddingX,
378 width * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700379 int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700380 - (int) (2 * inv.numRows * cellHeightPx));
Sunny Goyal47328fd2016-05-25 18:56:41 -0700381 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700382 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700383 } else {
384 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700385 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700386 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700387 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700388 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700389 }
390 }
391 return padding;
392 }
393
Winsonfadbe8f2016-07-22 16:35:37 -0700394 /**
395 * @return the bounds for which the open folders should be contained within
396 */
397 public Rect getAbsoluteOpenFolderBounds() {
398 if (isVerticalBarLayout()) {
399 // Folders should only appear right of the drop target bar and left of the hotseat
400 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
401 mInsets.top,
402 mInsets.left + availableWidthPx - hotseatBarHeightPx - edgeMarginPx,
403 mInsets.top + availableHeightPx);
404 } else {
405 // Folders should only appear below the drop target bar and above the hotseat
406 return new Rect(mInsets.left,
407 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
408 mInsets.left + availableWidthPx,
409 mInsets.top + availableHeightPx - hotseatBarHeightPx - pageIndicatorHeightPx -
410 edgeMarginPx);
411 }
412 }
413
Sunny Goyal47328fd2016-05-25 18:56:41 -0700414 private int getWorkspacePageSpacing() {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700415 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700416 // In landscape mode the page spacing is set to the default.
417 return defaultPageSpacingPx;
418 } else {
419 // In portrait, we want the pages spaced such that there is no
420 // overhang of the previous / next page into the current page viewport.
421 // We assume symmetrical padding in portrait mode.
Winson11439c52016-07-20 14:25:07 -0700422 return Math.max(defaultPageSpacingPx, getWorkspacePadding(null).left + 1);
Winson Chungb3800242013-10-24 11:01:54 -0700423 }
424 }
425
Winson Chung4f7eb402015-07-10 18:00:48 -0700426 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700427 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
428 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
429 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700430 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700431 }
432
Adam Cohen2e6da152015-05-06 11:42:25 -0700433 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700434 return width / countX;
435 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700436 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700437 return height / countY;
438 }
439
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700440 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700441 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
442 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
443 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700444 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700445 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700446 return isLandscape && transposeLayoutWithOrientation;
447 }
448
449 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700450 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700451 }
452
Sunny Goyalc6205602015-05-21 20:46:33 -0700453 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800454 int visibleChildren = 0;
455 for (int i = 0; i < parent.getChildCount(); i++) {
456 if (parent.getChildAt(i).getVisibility() != View.GONE) {
457 visibleChildren++;
458 }
459 }
460 return visibleChildren;
461 }
462
Winson1f064272016-07-18 17:18:02 -0700463 public void layout(Launcher launcher, boolean notifyListeners) {
Winson Chungb3800242013-10-24 11:01:54 -0700464 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700465 boolean hasVerticalBarLayout = isVerticalBarLayout();
466
467 // Layout the search bar space
Sunny Goyal47328fd2016-05-25 18:56:41 -0700468 Point searchBarBounds = getSearchBarDimensForWidgetOpts();
469 View searchBar = launcher.getDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700470 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700471 lp.width = searchBarBounds.x;
472 lp.height = searchBarBounds.y;
Winson1f064272016-07-18 17:18:02 -0700473 lp.topMargin = mInsets.top + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700474 searchBar.setLayoutParams(lp);
475
Winson Chungb3800242013-10-24 11:01:54 -0700476 // Layout the workspace
477 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
Winson1f064272016-07-18 17:18:02 -0700478 Rect workspacePadding = getWorkspacePadding(null);
479 workspace.setPadding(workspacePadding.left, workspacePadding.top, workspacePadding.right,
480 workspacePadding.bottom);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700481 workspace.setPageSpacing(getWorkspacePageSpacing());
Winson Chungb3800242013-10-24 11:01:54 -0700482
Sunny Goyal6178f132016-07-11 17:30:03 -0700483 View qsbContainer = launcher.getQsbContainer();
484 lp = (FrameLayout.LayoutParams) qsbContainer.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700485 lp.topMargin = mInsets.top + workspacePadding.top;
Sunny Goyal6178f132016-07-11 17:30:03 -0700486 qsbContainer.setLayoutParams(lp);
487
Winson Chungb3800242013-10-24 11:01:54 -0700488 // Layout the hotseat
Winson6231ede2016-07-20 17:21:55 -0700489 Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat);
Winson Chungb3800242013-10-24 11:01:54 -0700490 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700491 // We want the edges of the hotseat to line up with the edges of the workspace, but the
492 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
493 // this, we pad the left and right of the hotseat with half of the difference of a workspace
494 // cell vs a hotseat cell.
495 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
496 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
497 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700498 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700499 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
500 // screen regardless of RTL
501 lp.gravity = Gravity.RIGHT;
Winson1f064272016-07-18 17:18:02 -0700502 lp.width = hotseatBarHeightPx + mInsets.left + mInsets.right;
Winson Chungb3800242013-10-24 11:01:54 -0700503 lp.height = LayoutParams.MATCH_PARENT;
Winson6231ede2016-07-20 17:21:55 -0700504 hotseat.getLayout().setPadding(mInsets.left, mInsets.top, mInsets.right,
505 workspacePadding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700506 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800507 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700508 lp.gravity = Gravity.BOTTOM;
509 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700510 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700511 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
512 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
513 mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700514 } else {
515 // For phones, layout the hotseat without any bottom margin
516 // to ensure that we have space for the folders
517 lp.gravity = Gravity.BOTTOM;
518 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700519 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700520 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
521 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
522 mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700523 }
524 hotseat.setLayoutParams(lp);
525
526 // Layout the page indicators
527 View pageIndicator = launcher.findViewById(R.id.page_indicator);
528 if (pageIndicator != null) {
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700529 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700530 if (isVerticalBarLayout()) {
531 if (mInsets.left > 0) {
532 lp.leftMargin = mInsets.left + pageIndicatorLandGutterLeftNavBarPx -
533 lp.width - pageIndicatorLandWorkspaceOffsetPx;
534 } else if (mInsets.right > 0) {
535 lp.leftMargin = pageIndicatorLandGutterRightNavBarPx - lp.width -
536 pageIndicatorLandWorkspaceOffsetPx;
537 }
538 lp.bottomMargin = workspacePadding.bottom;
539 } else {
Winson Chungb3800242013-10-24 11:01:54 -0700540 // Put the page indicators above the hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700541 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Winson1f064272016-07-18 17:18:02 -0700542 lp.height = pageIndicatorHeightPx;
543 lp.bottomMargin = hotseatBarHeightPx + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700544 }
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700545 pageIndicator.setLayoutParams(lp);
Winson Chungb3800242013-10-24 11:01:54 -0700546 }
547
Winson Chungb3800242013-10-24 11:01:54 -0700548 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800549 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700550 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700551 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700552 lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700553
554 int visibleChildCount = getVisibleChildCount(overviewMode);
555 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
556 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
557
558 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700559 lp.height = getOverviewModeButtonBarHeight();
Winson1f064272016-07-18 17:18:02 -0700560 // Center the overview buttons on the workspace page
561 lp.leftMargin = workspacePadding.left + (availableWidthPx -
562 workspacePadding.left - workspacePadding.right - lp.width) / 2;
Winson Chungb3800242013-10-24 11:01:54 -0700563 overviewMode.setLayoutParams(lp);
Winson1f064272016-07-18 17:18:02 -0700564 }
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700565
Winson1f064272016-07-18 17:18:02 -0700566 if (notifyListeners) {
567 for (int i = mListeners.size() - 1; i >= 0; i--) {
568 mListeners.get(i).onLauncherLayoutChanged();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700569 }
Winson Chungb3800242013-10-24 11:01:54 -0700570 }
571 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700572
573 private int getCurrentWidth() {
574 return isLandscape
575 ? Math.max(widthPx, heightPx)
576 : Math.min(widthPx, heightPx);
577 }
578
579 private int getCurrentHeight() {
580 return isLandscape
581 ? Math.min(widthPx, heightPx)
582 : Math.max(widthPx, heightPx);
583 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700584
585
Winson1f064272016-07-18 17:18:02 -0700586 /**
587 * @return the left/right paddings for all containers.
588 */
589 public final int[] getContainerPadding(Context context) {
Sunny Goyal13178ac2016-04-04 16:35:22 -0700590 Resources res = context.getResources();
591
Winson1f064272016-07-18 17:18:02 -0700592 // No paddings for portrait phone
593 if (isPhone && !isVerticalBarLayout()) {
594 return new int[] {0, 0};
Sunny Goyal13178ac2016-04-04 16:35:22 -0700595 }
Winson1f064272016-07-18 17:18:02 -0700596
Hyunyoung Song9d96fd52016-08-23 11:06:24 -0700597 // In landscape, we match the width of the workspace
598 int padding = (pageIndicatorLandGutterRightNavBarPx +
599 hotseatBarHeightPx + hotseatLandGutterPx + mInsets.left) / 2;
Winson1f064272016-07-18 17:18:02 -0700600 return new int[]{ padding, padding };
Sunny Goyal13178ac2016-04-04 16:35:22 -0700601 }
Winson Chungb3800242013-10-24 11:01:54 -0700602}