blob: cc21920b05d4f060d26819b61c24ed54873820f5 [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;
Jon Mirandabf7d8122016-11-03 15:29:29 -070099
100 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700101 public int folderCellWidthPx;
102 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700103
104 // Folder child
105 public int folderChildIconSizePx;
106 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700107 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700108
Sunny Goyalc6205602015-05-21 20:46:33 -0700109 // Hotseat
110 public int hotseatCellWidthPx;
111 public int hotseatCellHeightPx;
112 public int hotseatIconSizePx;
Jon Mirandaaca21452016-09-26 14:01:56 -0700113 public int hotseatBarHeightPx;
Winson1f064272016-07-18 17:18:02 -0700114 private int hotseatBarTopPaddingPx;
115 private int hotseatLandGutterPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700116
Sunny Goyalc6205602015-05-21 20:46:33 -0700117 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700118 public int allAppsNumCols;
119 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -0700120 public int allAppsButtonVisualSize;
Winson1f064272016-07-18 17:18:02 -0700121 public int allAppsIconSizePx;
122 public int allAppsIconDrawablePaddingPx;
123 public float allAppsIconTextSizePx;
124
Sunny Goyal47328fd2016-05-25 18:56:41 -0700125 // Drop Target
126 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700127
Winson1f064272016-07-18 17:18:02 -0700128 // Insets
129 private Rect mInsets = new Rect();
130
131 // Listeners
132 private ArrayList<LauncherLayoutChangeListener> mListeners = new ArrayList<>();
133
Sunny Goyalc6205602015-05-21 20:46:33 -0700134 public DeviceProfile(Context context, InvariantDeviceProfile inv,
135 Point minSize, Point maxSize,
136 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700137
Adam Cohen2e6da152015-05-06 11:42:25 -0700138 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700139 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700140
Adam Cohen2e6da152015-05-06 11:42:25 -0700141 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700142 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700143
Sunny Goyalc6205602015-05-21 20:46:33 -0700144 // Constants from resources
145 isTablet = res.getBoolean(R.bool.is_tablet);
146 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
147 isPhone = !isTablet && !isLargeTablet;
148
149 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700150 transposeLayoutWithOrientation =
151 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700152
153 ComponentName cn = new ComponentName(context.getPackageName(),
154 this.getClass().getName());
155 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
156 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Winson1f064272016-07-18 17:18:02 -0700157 desiredWorkspaceLeftRightMarginPx = edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700158 pageIndicatorHeightPx =
159 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
Winson1f064272016-07-18 17:18:02 -0700160 pageIndicatorLandGutterLeftNavBarPx = res.getDimensionPixelSize(
161 R.dimen.dynamic_grid_page_indicator_gutter_width_left_nav_bar);
162 pageIndicatorLandWorkspaceOffsetPx =
163 res.getDimensionPixelSize(R.dimen.all_apps_caret_workspace_offset);
164 pageIndicatorLandGutterRightNavBarPx = res.getDimensionPixelSize(
165 R.dimen.dynamic_grid_page_indicator_gutter_width_right_nav_bar);
Winson Chungb3800242013-10-24 11:01:54 -0700166 defaultPageSpacingPx =
167 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700168 topWorkspacePadding =
169 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700170 overviewModeMinIconZoneHeightPx =
171 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
172 overviewModeMaxIconZoneHeightPx =
173 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800174 overviewModeBarItemWidthPx =
175 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
176 overviewModeBarSpacerWidthPx =
177 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700178 overviewModeIconZoneRatio =
179 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700180 iconDrawablePaddingOriginalPx =
181 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700182 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
183 workspaceSpringLoadedBottomSpace =
184 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Winson1f064272016-07-18 17:18:02 -0700185 hotseatBarHeightPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height);
186 hotseatBarTopPaddingPx =
187 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
188 hotseatLandGutterPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_gutter_width);
Winson Chungb3800242013-10-24 11:01:54 -0700189
Sunny Goyalc6205602015-05-21 20:46:33 -0700190 // Determine sizes.
191 widthPx = width;
192 heightPx = height;
193 if (isLandscape) {
194 availableWidthPx = maxSize.x;
195 availableHeightPx = minSize.y;
196 } else {
197 availableWidthPx = minSize.x;
198 availableHeightPx = maxSize.y;
199 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700200
Winson Chungb3800242013-10-24 11:01:54 -0700201 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700202 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700203 computeAllAppsButtonSize(context);
204 }
205
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700206 DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
207 // In multi-window mode, we can have widthPx = availableWidthPx
208 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
209 // widthPx and heightPx values where it's needed.
210 return new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y, isLandscape);
211 }
212
Winson1f064272016-07-18 17:18:02 -0700213 public void addLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
214 if (!mListeners.contains(listener)) {
215 mListeners.add(listener);
216 }
217 }
218
219 public void removeLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
220 if (mListeners.contains(listener)) {
221 mListeners.remove(listener);
222 }
223 }
224
Adam Cohen63f1ec02014-08-12 09:23:13 -0700225 /**
226 * Determine the exact visual footprint of the all apps button, taking into account scaling
227 * and internal padding of the drawable.
228 */
229 private void computeAllAppsButtonSize(Context context) {
230 Resources res = context.getResources();
231 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700232 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
233 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700234 }
235
Sunny Goyalc6205602015-05-21 20:46:33 -0700236 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700237 updateIconSize(1f, iconDrawablePaddingOriginalPx, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800238
Jon Mirandabf7d8122016-11-03 15:29:29 -0700239 // Check to see if the icons fit within the available height. If not, then scale down.
240 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700241 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700242 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700243 float scale = maxHeight / usedHeight;
244 updateIconSize(scale, 0, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700245 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700246
247 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700248 }
249
Winson Chung93f98ea2015-03-10 16:28:47 -0700250 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700251 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700252 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800253 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700254 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700255 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson1f064272016-07-18 17:18:02 -0700256 allAppsIconSizePx = iconSizePx;
257 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
258 allAppsIconTextSizePx = iconTextSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700259
Winson Chungb3800242013-10-24 11:01:54 -0700260 cellWidthPx = iconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700261 cellHeightPx = iconSizePx + iconDrawablePaddingPx
262 + Utilities.calculateTextHeight(iconTextSizePx);
Tony Wickhame0c33232016-02-08 11:37:04 -0800263 final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
264 : res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800265 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700266
267 // Hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700268 hotseatCellWidthPx = iconSizePx;
269 hotseatCellHeightPx = iconSizePx;
270
Sunny Goyal47328fd2016-05-25 18:56:41 -0700271 if (!isVerticalBarLayout()) {
272 int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
273 - pageIndicatorHeightPx - topWorkspacePadding;
274 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
275 workspaceSpringLoadShrinkFactor = Math.min(
276 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
277 1 - (minRequiredHeight / expectedWorkspaceHeight));
278 } else {
279 workspaceSpringLoadShrinkFactor =
280 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
281 }
282
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700283 // Folder icon
Winson Chungb3800242013-10-24 11:01:54 -0700284 folderBackgroundOffset = -edgeMarginPx;
285 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800286 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700287 }
288
Jon Mirandabf7d8122016-11-03 15:29:29 -0700289 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
290 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
291 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
292 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
293
294 updateFolderCellSize(1f, dm, res, folderBottomPanelSize);
295
296 // Check to see if the icons fit within the available height. If not, then scale down.
297 float usedHeight = (folderCellHeightPx * inv.numFolderRows) + folderBottomPanelSize;
298 int maxHeight = availableHeightPx - getTotalWorkspacePadding().y - (2 * edgeMarginPx);
299 if (usedHeight > maxHeight) {
300 float scale = maxHeight / usedHeight;
301 updateFolderCellSize(scale, dm, res, folderBottomPanelSize);
302 }
303 }
304
305 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res,
306 int folderBottomPanelSize) {
307 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
308 folderChildTextSizePx =
309 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
310
311 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
312 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
313 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
314
315 // Don't let the folder get too close to the edges of the screen.
316 folderCellWidthPx = Math.min(folderChildIconSizePx + 2 * cellPaddingX,
317 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
318 folderCellHeightPx = Math.min(folderChildIconSizePx + 2 * cellPaddingY + textHeight,
319 (availableHeightPx - 4 * edgeMarginPx - folderBottomPanelSize) / inv.numFolderRows);
320 folderChildDrawablePaddingPx = Math.max(0,
321 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
322 }
323
Winson1f064272016-07-18 17:18:02 -0700324 public void updateInsets(Rect insets) {
325 mInsets.set(insets);
326 }
327
Winson1f064272016-07-18 17:18:02 -0700328 public void updateAppsViewNumCols() {
329 allAppsNumCols = allAppsNumPredictiveCols = inv.numColumns;
Winson Chungb3800242013-10-24 11:01:54 -0700330 }
331
Tony Wickhama3c74d12015-10-23 11:43:47 -0700332 /** Returns the width and height of the search bar, ignoring any padding. */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700333 public Point getSearchBarDimensForWidgetOpts() {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700334 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700335 return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700336 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700337 int gap;
Sunny Goyalc6205602015-05-21 20:46:33 -0700338 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700339 // Pad the left and right of the workspace to ensure consistent spacing
340 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700341 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700342 // XXX: If the icon size changes across orientations, we will have to take
343 // that into account here too.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700344 gap = ((width - 2 * edgeMarginPx
345 - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
346 + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700347 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700348 gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
Winson Chungb3800242013-10-24 11:01:54 -0700349 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700350 return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700351 }
Winson Chungb3800242013-10-24 11:01:54 -0700352 }
353
Sunny Goyal756cd262015-08-20 12:33:21 -0700354 public Point getCellSize() {
355 Point result = new Point();
356 // Since we are only concerned with the overall padding, layout direction does
357 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700358 Point padding = getTotalWorkspacePadding();
359 result.x = calculateCellWidth(availableWidthPx - padding.x, inv.numColumns);
360 result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700361 return result;
362 }
363
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700364 public Point getTotalWorkspacePadding() {
Sunny Goyal6178f132016-07-11 17:30:03 -0700365 Rect padding = getWorkspacePadding(null);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700366 return new Point(padding.left + padding.right, padding.top + padding.bottom);
367 }
368
369 /**
370 * Returns the workspace padding in the specified orientation.
371 * Note that it assumes that while in verticalBarLayout, the nav bar is on the right, as such
372 * this value is not reliable.
373 * Use {@link #getTotalWorkspacePadding()} instead.
374 */
Sunny Goyal6178f132016-07-11 17:30:03 -0700375 public Rect getWorkspacePadding(Rect recycle) {
376 Rect padding = recycle == null ? new Rect() : recycle;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700377 if (isVerticalBarLayout()) {
Winson1f064272016-07-18 17:18:02 -0700378 if (mInsets.left > 0) {
379 padding.set(mInsets.left + pageIndicatorLandGutterLeftNavBarPx, 0,
380 hotseatBarHeightPx + hotseatLandGutterPx - mInsets.left, 2 * edgeMarginPx);
381 } else {
382 padding.set(pageIndicatorLandGutterRightNavBarPx, 0,
383 hotseatBarHeightPx + hotseatLandGutterPx, 2 * edgeMarginPx);
384 }
Winson Chungb3800242013-10-24 11:01:54 -0700385 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700386 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700387 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700388 // Pad the left and right of the workspace to ensure consistent spacing
389 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800390 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700391 int width = getCurrentWidth();
392 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700393 // The amount of screen space available for left/right padding.
394 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
395 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
396 availablePaddingX = (int) Math.min(availablePaddingX,
397 width * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700398 int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700399 - (int) (2 * inv.numRows * cellHeightPx));
Sunny Goyal47328fd2016-05-25 18:56:41 -0700400 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700401 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700402 } else {
403 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700404 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700405 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700406 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700407 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700408 }
409 }
410 return padding;
411 }
412
Winsonfadbe8f2016-07-22 16:35:37 -0700413 /**
414 * @return the bounds for which the open folders should be contained within
415 */
416 public Rect getAbsoluteOpenFolderBounds() {
417 if (isVerticalBarLayout()) {
418 // Folders should only appear right of the drop target bar and left of the hotseat
419 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
420 mInsets.top,
421 mInsets.left + availableWidthPx - hotseatBarHeightPx - edgeMarginPx,
422 mInsets.top + availableHeightPx);
423 } else {
424 // Folders should only appear below the drop target bar and above the hotseat
425 return new Rect(mInsets.left,
426 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
427 mInsets.left + availableWidthPx,
428 mInsets.top + availableHeightPx - hotseatBarHeightPx - pageIndicatorHeightPx -
429 edgeMarginPx);
430 }
431 }
432
Sunny Goyal47328fd2016-05-25 18:56:41 -0700433 private int getWorkspacePageSpacing() {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700434 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700435 // In landscape mode the page spacing is set to the default.
436 return defaultPageSpacingPx;
437 } else {
438 // In portrait, we want the pages spaced such that there is no
439 // overhang of the previous / next page into the current page viewport.
440 // We assume symmetrical padding in portrait mode.
Winson11439c52016-07-20 14:25:07 -0700441 return Math.max(defaultPageSpacingPx, getWorkspacePadding(null).left + 1);
Winson Chungb3800242013-10-24 11:01:54 -0700442 }
443 }
444
Winson Chung4f7eb402015-07-10 18:00:48 -0700445 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700446 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
447 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
448 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700449 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700450 }
451
Adam Cohen2e6da152015-05-06 11:42:25 -0700452 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700453 return width / countX;
454 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700455 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700456 return height / countY;
457 }
458
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700459 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700460 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
461 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
462 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700463 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700464 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700465 return isLandscape && transposeLayoutWithOrientation;
466 }
467
468 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700469 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700470 }
471
Sunny Goyalc6205602015-05-21 20:46:33 -0700472 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800473 int visibleChildren = 0;
474 for (int i = 0; i < parent.getChildCount(); i++) {
475 if (parent.getChildAt(i).getVisibility() != View.GONE) {
476 visibleChildren++;
477 }
478 }
479 return visibleChildren;
480 }
481
Winson1f064272016-07-18 17:18:02 -0700482 public void layout(Launcher launcher, boolean notifyListeners) {
Winson Chungb3800242013-10-24 11:01:54 -0700483 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700484 boolean hasVerticalBarLayout = isVerticalBarLayout();
485
486 // Layout the search bar space
Sunny Goyal47328fd2016-05-25 18:56:41 -0700487 Point searchBarBounds = getSearchBarDimensForWidgetOpts();
488 View searchBar = launcher.getDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700489 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700490 lp.width = searchBarBounds.x;
491 lp.height = searchBarBounds.y;
Winson1f064272016-07-18 17:18:02 -0700492 lp.topMargin = mInsets.top + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700493 searchBar.setLayoutParams(lp);
494
Winson Chungb3800242013-10-24 11:01:54 -0700495 // Layout the workspace
496 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
Winson1f064272016-07-18 17:18:02 -0700497 Rect workspacePadding = getWorkspacePadding(null);
498 workspace.setPadding(workspacePadding.left, workspacePadding.top, workspacePadding.right,
499 workspacePadding.bottom);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700500 workspace.setPageSpacing(getWorkspacePageSpacing());
Winson Chungb3800242013-10-24 11:01:54 -0700501
Sunny Goyal6178f132016-07-11 17:30:03 -0700502 View qsbContainer = launcher.getQsbContainer();
503 lp = (FrameLayout.LayoutParams) qsbContainer.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700504 lp.topMargin = mInsets.top + workspacePadding.top;
Sunny Goyal6178f132016-07-11 17:30:03 -0700505 qsbContainer.setLayoutParams(lp);
506
Winson Chungb3800242013-10-24 11:01:54 -0700507 // Layout the hotseat
Winson6231ede2016-07-20 17:21:55 -0700508 Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat);
Winson Chungb3800242013-10-24 11:01:54 -0700509 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700510 // We want the edges of the hotseat to line up with the edges of the workspace, but the
511 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
512 // this, we pad the left and right of the hotseat with half of the difference of a workspace
513 // cell vs a hotseat cell.
514 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
515 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
516 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700517 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700518 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
519 // screen regardless of RTL
520 lp.gravity = Gravity.RIGHT;
Winson1f064272016-07-18 17:18:02 -0700521 lp.width = hotseatBarHeightPx + mInsets.left + mInsets.right;
Winson Chungb3800242013-10-24 11:01:54 -0700522 lp.height = LayoutParams.MATCH_PARENT;
Winson6231ede2016-07-20 17:21:55 -0700523 hotseat.getLayout().setPadding(mInsets.left, mInsets.top, mInsets.right,
524 workspacePadding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700525 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800526 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700527 lp.gravity = Gravity.BOTTOM;
528 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700529 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700530 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
531 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
532 mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700533 } else {
534 // For phones, layout the hotseat without any bottom margin
535 // to ensure that we have space for the folders
536 lp.gravity = Gravity.BOTTOM;
537 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700538 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700539 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
540 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
541 mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700542 }
543 hotseat.setLayoutParams(lp);
544
545 // Layout the page indicators
546 View pageIndicator = launcher.findViewById(R.id.page_indicator);
547 if (pageIndicator != null) {
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700548 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700549 if (isVerticalBarLayout()) {
550 if (mInsets.left > 0) {
551 lp.leftMargin = mInsets.left + pageIndicatorLandGutterLeftNavBarPx -
552 lp.width - pageIndicatorLandWorkspaceOffsetPx;
553 } else if (mInsets.right > 0) {
554 lp.leftMargin = pageIndicatorLandGutterRightNavBarPx - lp.width -
555 pageIndicatorLandWorkspaceOffsetPx;
556 }
557 lp.bottomMargin = workspacePadding.bottom;
558 } else {
Winson Chungb3800242013-10-24 11:01:54 -0700559 // Put the page indicators above the hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700560 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Winson1f064272016-07-18 17:18:02 -0700561 lp.height = pageIndicatorHeightPx;
562 lp.bottomMargin = hotseatBarHeightPx + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700563 }
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700564 pageIndicator.setLayoutParams(lp);
Winson Chungb3800242013-10-24 11:01:54 -0700565 }
566
Winson Chungb3800242013-10-24 11:01:54 -0700567 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800568 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700569 if (overviewMode != null) {
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700570 int visibleChildCount = getVisibleChildCount(overviewMode);
571 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
Jon Mirandadf3bc522016-11-01 16:55:56 -0700572 int maxWidth = totalItemWidth + (visibleChildCount - 1) * overviewModeBarSpacerWidthPx;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700573
Jon Mirandadf3bc522016-11-01 16:55:56 -0700574 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700575 lp.width = Math.min(availableWidthPx, maxWidth);
Jon Mirandadf3bc522016-11-01 16:55:56 -0700576 lp.height = getOverviewModeButtonBarHeight() + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700577 overviewMode.setLayoutParams(lp);
Winson1f064272016-07-18 17:18:02 -0700578 }
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700579
Winson1f064272016-07-18 17:18:02 -0700580 if (notifyListeners) {
581 for (int i = mListeners.size() - 1; i >= 0; i--) {
582 mListeners.get(i).onLauncherLayoutChanged();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700583 }
Winson Chungb3800242013-10-24 11:01:54 -0700584 }
585 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700586
587 private int getCurrentWidth() {
588 return isLandscape
589 ? Math.max(widthPx, heightPx)
590 : Math.min(widthPx, heightPx);
591 }
592
593 private int getCurrentHeight() {
594 return isLandscape
595 ? Math.min(widthPx, heightPx)
596 : Math.max(widthPx, heightPx);
597 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700598
599
Winson1f064272016-07-18 17:18:02 -0700600 /**
601 * @return the left/right paddings for all containers.
602 */
Jon Mirandac3e4e222016-11-02 16:56:52 -0700603 public final int[] getContainerPadding() {
Winson1f064272016-07-18 17:18:02 -0700604 // No paddings for portrait phone
605 if (isPhone && !isVerticalBarLayout()) {
606 return new int[] {0, 0};
Sunny Goyal13178ac2016-04-04 16:35:22 -0700607 }
Winson1f064272016-07-18 17:18:02 -0700608
Hyunyoung Song9d96fd52016-08-23 11:06:24 -0700609 // In landscape, we match the width of the workspace
610 int padding = (pageIndicatorLandGutterRightNavBarPx +
611 hotseatBarHeightPx + hotseatLandGutterPx + mInsets.left) / 2;
Winson1f064272016-07-18 17:18:02 -0700612 return new int[]{ padding, padding };
Sunny Goyal13178ac2016-04-04 16:35:22 -0700613 }
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700614
Jon Miranda51f037d2016-11-07 08:37:20 -0800615 public boolean shouldIgnoreLongPressToOverview(float touchX) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700616 boolean inMultiWindowMode = this != inv.landscapeProfile && this != inv.portraitProfile;
Jon Miranda51f037d2016-11-07 08:37:20 -0800617 boolean touchedLhsEdge = mInsets.left == 0 && touchX < edgeMarginPx;
618 boolean touchedRhsEdge = mInsets.right == 0 && touchX > (widthPx - edgeMarginPx);
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700619 return !inMultiWindowMode && (touchedLhsEdge || touchedRhsEdge);
620 }
Winson Chungb3800242013-10-24 11:01:54 -0700621}