blob: 27afdc03dc2c8784bfb0626035de1c13eed2c745 [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;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080024import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070025import android.graphics.Rect;
26import android.util.DisplayMetrics;
Winson Chungb3800242013-10-24 11:01:54 -070027import android.view.Gravity;
Winson Chungb3800242013-10-24 11:01:54 -070028import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080029import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070030import android.view.ViewGroup.LayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.widget.FrameLayout;
32
Sunny Goyalc13403c2016-11-18 23:44:48 -080033import com.android.launcher3.CellLayout.ContainerType;
Tony Wickhame0c33232016-02-08 11:37:04 -080034import com.android.launcher3.config.FeatureFlags;
35
Winson1f064272016-07-18 17:18:02 -070036import java.util.ArrayList;
37
Winson Chungb3800242013-10-24 11:01:54 -070038public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070039
Winson1f064272016-07-18 17:18:02 -070040 public interface LauncherLayoutChangeListener {
41 void onLauncherLayoutChanged();
42 }
43
Adam Cohen2e6da152015-05-06 11:42:25 -070044 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070045
Sunny Goyalc6205602015-05-21 20:46:33 -070046 // Device properties
47 public final boolean isTablet;
48 public final boolean isLargeTablet;
49 public final boolean isPhone;
50 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070051
Sunny Goyalc6205602015-05-21 20:46:33 -070052 // Device properties in current orientation
53 public final boolean isLandscape;
54 public final int widthPx;
55 public final int heightPx;
56 public final int availableWidthPx;
57 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070058 /**
59 * The maximum amount of left/right workspace padding as a percentage of the screen width.
60 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
61 * 7% of the screen width can be used as right padding.
62 */
63 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070064
Sunny Goyalc6205602015-05-21 20:46:33 -070065 // Overview mode
66 private final int overviewModeMinIconZoneHeightPx;
67 private final int overviewModeMaxIconZoneHeightPx;
68 private final int overviewModeBarItemWidthPx;
69 private final int overviewModeBarSpacerWidthPx;
70 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070071
Sunny Goyalc6205602015-05-21 20:46:33 -070072 // Workspace
73 private int desiredWorkspaceLeftRightMarginPx;
74 public final int edgeMarginPx;
75 public final Rect defaultWidgetPadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070076 private final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070077 private final int topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070078 public float workspaceSpringLoadShrinkFactor;
79 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070080
Winson1f064272016-07-18 17:18:02 -070081 // Page indicator
82 private final int pageIndicatorHeightPx;
83 private final int pageIndicatorLandGutterLeftNavBarPx;
84 private final int pageIndicatorLandGutterRightNavBarPx;
85 private final int pageIndicatorLandWorkspaceOffsetPx;
86
Sunny Goyalc6205602015-05-21 20:46:33 -070087 // Workspace icons
88 public int iconSizePx;
89 public int iconTextSizePx;
90 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070091 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070092
Adam Cohen59400422014-03-05 18:07:04 -080093 public int cellWidthPx;
94 public int cellHeightPx;
95
Sunny Goyalc6205602015-05-21 20:46:33 -070096 // Folder
97 public int folderBackgroundOffset;
98 public int folderIconSizePx;
Adam Cohenefca0272016-02-24 19:19:06 -080099 public int folderIconPreviewPadding;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700100
101 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700102 public int folderCellWidthPx;
103 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700104
105 // Folder child
106 public int folderChildIconSizePx;
107 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700108 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700109
Sunny Goyalc6205602015-05-21 20:46:33 -0700110 // Hotseat
111 public int hotseatCellWidthPx;
112 public int hotseatCellHeightPx;
113 public int hotseatIconSizePx;
Jon Mirandaaca21452016-09-26 14:01:56 -0700114 public int hotseatBarHeightPx;
Winson1f064272016-07-18 17:18:02 -0700115 private int hotseatBarTopPaddingPx;
Jon Mirandac1b08c52016-11-15 14:37:56 -0800116 private int hotseatBarBottomPaddingPx;
Winson1f064272016-07-18 17:18:02 -0700117 private int hotseatLandGutterPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700118
Sunny Goyalc6205602015-05-21 20:46:33 -0700119 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700120 public int allAppsNumCols;
121 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -0700122 public int allAppsButtonVisualSize;
Winson1f064272016-07-18 17:18:02 -0700123 public int allAppsIconSizePx;
124 public int allAppsIconDrawablePaddingPx;
125 public float allAppsIconTextSizePx;
126
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800127 // Widgets
128 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
129
Sunny Goyal47328fd2016-05-25 18:56:41 -0700130 // Drop Target
131 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700132
Winson1f064272016-07-18 17:18:02 -0700133 // Insets
134 private Rect mInsets = new Rect();
135
136 // Listeners
137 private ArrayList<LauncherLayoutChangeListener> mListeners = new ArrayList<>();
138
Sunny Goyalc6205602015-05-21 20:46:33 -0700139 public DeviceProfile(Context context, InvariantDeviceProfile inv,
140 Point minSize, Point maxSize,
141 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700142
Adam Cohen2e6da152015-05-06 11:42:25 -0700143 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700144 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700145
Adam Cohen2e6da152015-05-06 11:42:25 -0700146 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700147 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700148
Sunny Goyalc6205602015-05-21 20:46:33 -0700149 // Constants from resources
150 isTablet = res.getBoolean(R.bool.is_tablet);
151 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
152 isPhone = !isTablet && !isLargeTablet;
153
154 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700155 transposeLayoutWithOrientation =
156 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700157
158 ComponentName cn = new ComponentName(context.getPackageName(),
159 this.getClass().getName());
160 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
161 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Winson1f064272016-07-18 17:18:02 -0700162 desiredWorkspaceLeftRightMarginPx = edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700163 pageIndicatorHeightPx =
164 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
Winson1f064272016-07-18 17:18:02 -0700165 pageIndicatorLandGutterLeftNavBarPx = res.getDimensionPixelSize(
166 R.dimen.dynamic_grid_page_indicator_gutter_width_left_nav_bar);
167 pageIndicatorLandWorkspaceOffsetPx =
168 res.getDimensionPixelSize(R.dimen.all_apps_caret_workspace_offset);
169 pageIndicatorLandGutterRightNavBarPx = res.getDimensionPixelSize(
170 R.dimen.dynamic_grid_page_indicator_gutter_width_right_nav_bar);
Winson Chungb3800242013-10-24 11:01:54 -0700171 defaultPageSpacingPx =
172 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700173 topWorkspacePadding =
174 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700175 overviewModeMinIconZoneHeightPx =
176 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
177 overviewModeMaxIconZoneHeightPx =
178 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800179 overviewModeBarItemWidthPx =
180 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
181 overviewModeBarSpacerWidthPx =
182 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700183 overviewModeIconZoneRatio =
184 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700185 iconDrawablePaddingOriginalPx =
186 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700187 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
188 workspaceSpringLoadedBottomSpace =
189 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Winson1f064272016-07-18 17:18:02 -0700190 hotseatBarHeightPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height);
191 hotseatBarTopPaddingPx =
192 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Mirandac1b08c52016-11-15 14:37:56 -0800193 hotseatBarBottomPaddingPx = 0;
Winson1f064272016-07-18 17:18:02 -0700194 hotseatLandGutterPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_gutter_width);
Winson Chungb3800242013-10-24 11:01:54 -0700195
Sunny Goyalc6205602015-05-21 20:46:33 -0700196 // Determine sizes.
197 widthPx = width;
198 heightPx = height;
199 if (isLandscape) {
200 availableWidthPx = maxSize.x;
201 availableHeightPx = minSize.y;
202 } else {
203 availableWidthPx = minSize.x;
204 availableHeightPx = maxSize.y;
205 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700206
Winson Chungb3800242013-10-24 11:01:54 -0700207 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700208 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700209 computeAllAppsButtonSize(context);
210 }
211
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700212 DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
213 // In multi-window mode, we can have widthPx = availableWidthPx
214 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
215 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800216 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
217 isLandscape);
218
219 // Hide labels on the workspace.
220 profile.iconTextSizePx = 0;
221 profile.cellHeightPx = profile.iconSizePx + profile.iconDrawablePaddingPx
222 + Utilities.calculateTextHeight(profile.iconTextSizePx);
223
Jon Mirandac1b08c52016-11-15 14:37:56 -0800224 // The nav bar is black so we add bottom padding to visually center hotseat icons.
225 profile.hotseatBarBottomPaddingPx = profile.hotseatBarTopPaddingPx;
226
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800227 // We use these scales to measure and layout the widgets using their full invariant profile
228 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
229 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
230 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
231 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
232
Jon Miranda93e1f042016-11-11 14:13:04 -0800233 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700234 }
235
Winson1f064272016-07-18 17:18:02 -0700236 public void addLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
237 if (!mListeners.contains(listener)) {
238 mListeners.add(listener);
239 }
240 }
241
242 public void removeLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
243 if (mListeners.contains(listener)) {
244 mListeners.remove(listener);
245 }
246 }
247
Adam Cohen63f1ec02014-08-12 09:23:13 -0700248 /**
249 * Determine the exact visual footprint of the all apps button, taking into account scaling
250 * and internal padding of the drawable.
251 */
252 private void computeAllAppsButtonSize(Context context) {
253 Resources res = context.getResources();
254 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700255 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
256 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700257 }
258
Sunny Goyalc6205602015-05-21 20:46:33 -0700259 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700260 updateIconSize(1f, iconDrawablePaddingOriginalPx, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800261
Jon Mirandabf7d8122016-11-03 15:29:29 -0700262 // Check to see if the icons fit within the available height. If not, then scale down.
263 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700264 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700265 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700266 float scale = maxHeight / usedHeight;
267 updateIconSize(scale, 0, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700268 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700269
270 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700271 }
272
Winson Chung93f98ea2015-03-10 16:28:47 -0700273 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700274 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700275 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800276 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700277 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700278 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson1f064272016-07-18 17:18:02 -0700279 allAppsIconSizePx = iconSizePx;
280 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
281 allAppsIconTextSizePx = iconTextSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700282
Winson Chungb3800242013-10-24 11:01:54 -0700283 cellWidthPx = iconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700284 cellHeightPx = iconSizePx + iconDrawablePaddingPx
285 + Utilities.calculateTextHeight(iconTextSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700286
287 // Hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700288 hotseatCellWidthPx = iconSizePx;
289 hotseatCellHeightPx = iconSizePx;
290
Sunny Goyal47328fd2016-05-25 18:56:41 -0700291 if (!isVerticalBarLayout()) {
292 int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
293 - pageIndicatorHeightPx - topWorkspacePadding;
294 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
295 workspaceSpringLoadShrinkFactor = Math.min(
296 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
297 1 - (minRequiredHeight / expectedWorkspaceHeight));
298 } else {
299 workspaceSpringLoadShrinkFactor =
300 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
301 }
302
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700303 // Folder icon
Winson Chungb3800242013-10-24 11:01:54 -0700304 folderBackgroundOffset = -edgeMarginPx;
305 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800306 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700307 }
308
Jon Mirandabf7d8122016-11-03 15:29:29 -0700309 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
310 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
311 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
312 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
313
Jon Mirandac1b23992016-12-13 08:57:36 -0800314 updateFolderCellSize(1f, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700315
Jon Mirandac1b23992016-12-13 08:57:36 -0800316 // Don't let the folder get too close to the edges of the screen.
317 int folderMargin = 4 * edgeMarginPx;
318
319 // Check if the icons fit within the available height.
320 float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize;
321 int maxHeight = availableHeightPx - getTotalWorkspacePadding().y - folderMargin;
322 float scaleY = maxHeight / usedHeight;
323
324 // Check if the icons fit within the available width.
325 float usedWidth = folderCellWidthPx * inv.numFolderColumns;
326 int maxWidth = availableWidthPx - getTotalWorkspacePadding().x - folderMargin;
327 float scaleX = maxWidth / usedWidth;
328
329 float scale = Math.min(scaleX, scaleY);
330 if (scale < 1f) {
331 updateFolderCellSize(scale, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700332 }
333 }
334
Jon Mirandac1b23992016-12-13 08:57:36 -0800335 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700336 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
337 folderChildTextSizePx =
338 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
339
340 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
341 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
342 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
343
Jon Mirandac1b23992016-12-13 08:57:36 -0800344 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
345 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700346 folderChildDrawablePaddingPx = Math.max(0,
347 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
348 }
349
Winson1f064272016-07-18 17:18:02 -0700350 public void updateInsets(Rect insets) {
351 mInsets.set(insets);
352 }
353
Winson1f064272016-07-18 17:18:02 -0700354 public void updateAppsViewNumCols() {
355 allAppsNumCols = allAppsNumPredictiveCols = inv.numColumns;
Winson Chungb3800242013-10-24 11:01:54 -0700356 }
357
Tony Wickhama3c74d12015-10-23 11:43:47 -0700358 /** Returns the width and height of the search bar, ignoring any padding. */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700359 public Point getSearchBarDimensForWidgetOpts() {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700360 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700361 return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700362 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700363 int gap;
Sunny Goyalc6205602015-05-21 20:46:33 -0700364 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700365 // Pad the left and right of the workspace to ensure consistent spacing
366 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700367 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700368 // XXX: If the icon size changes across orientations, we will have to take
369 // that into account here too.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700370 gap = ((width - 2 * edgeMarginPx
371 - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
372 + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700373 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700374 gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
Winson Chungb3800242013-10-24 11:01:54 -0700375 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700376 return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700377 }
Winson Chungb3800242013-10-24 11:01:54 -0700378 }
379
Sunny Goyal756cd262015-08-20 12:33:21 -0700380 public Point getCellSize() {
381 Point result = new Point();
382 // Since we are only concerned with the overall padding, layout direction does
383 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700384 Point padding = getTotalWorkspacePadding();
385 result.x = calculateCellWidth(availableWidthPx - padding.x, inv.numColumns);
386 result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700387 return result;
388 }
389
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700390 public Point getTotalWorkspacePadding() {
Sunny Goyal6178f132016-07-11 17:30:03 -0700391 Rect padding = getWorkspacePadding(null);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700392 return new Point(padding.left + padding.right, padding.top + padding.bottom);
393 }
394
395 /**
396 * Returns the workspace padding in the specified orientation.
397 * Note that it assumes that while in verticalBarLayout, the nav bar is on the right, as such
398 * this value is not reliable.
399 * Use {@link #getTotalWorkspacePadding()} instead.
400 */
Sunny Goyal6178f132016-07-11 17:30:03 -0700401 public Rect getWorkspacePadding(Rect recycle) {
402 Rect padding = recycle == null ? new Rect() : recycle;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700403 if (isVerticalBarLayout()) {
Winson1f064272016-07-18 17:18:02 -0700404 if (mInsets.left > 0) {
405 padding.set(mInsets.left + pageIndicatorLandGutterLeftNavBarPx, 0,
406 hotseatBarHeightPx + hotseatLandGutterPx - mInsets.left, 2 * edgeMarginPx);
407 } else {
408 padding.set(pageIndicatorLandGutterRightNavBarPx, 0,
409 hotseatBarHeightPx + hotseatLandGutterPx, 2 * edgeMarginPx);
410 }
Winson Chungb3800242013-10-24 11:01:54 -0700411 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700412 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700413 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700414 // Pad the left and right of the workspace to ensure consistent spacing
415 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700416 int width = getCurrentWidth();
417 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700418 // The amount of screen space available for left/right padding.
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800419 int availablePaddingX = Math.max(0, width - ((inv.numColumns * cellWidthPx) +
420 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700421 availablePaddingX = (int) Math.min(availablePaddingX,
422 width * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700423 int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800424 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
425 - hotseatBarBottomPaddingPx);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700426 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700427 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700428 } else {
429 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700430 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700431 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700432 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700433 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700434 }
435 }
436 return padding;
437 }
438
Winsonfadbe8f2016-07-22 16:35:37 -0700439 /**
440 * @return the bounds for which the open folders should be contained within
441 */
442 public Rect getAbsoluteOpenFolderBounds() {
443 if (isVerticalBarLayout()) {
444 // Folders should only appear right of the drop target bar and left of the hotseat
445 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
446 mInsets.top,
447 mInsets.left + availableWidthPx - hotseatBarHeightPx - edgeMarginPx,
448 mInsets.top + availableHeightPx);
449 } else {
450 // Folders should only appear below the drop target bar and above the hotseat
451 return new Rect(mInsets.left,
452 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
453 mInsets.left + availableWidthPx,
454 mInsets.top + availableHeightPx - hotseatBarHeightPx - pageIndicatorHeightPx -
455 edgeMarginPx);
456 }
457 }
458
Sunny Goyal47328fd2016-05-25 18:56:41 -0700459 private int getWorkspacePageSpacing() {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700460 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700461 // In landscape mode the page spacing is set to the default.
462 return defaultPageSpacingPx;
463 } else {
464 // In portrait, we want the pages spaced such that there is no
465 // overhang of the previous / next page into the current page viewport.
466 // We assume symmetrical padding in portrait mode.
Winson11439c52016-07-20 14:25:07 -0700467 return Math.max(defaultPageSpacingPx, getWorkspacePadding(null).left + 1);
Winson Chungb3800242013-10-24 11:01:54 -0700468 }
469 }
470
Winson Chung4f7eb402015-07-10 18:00:48 -0700471 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700472 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
473 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
474 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700475 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700476 }
477
Adam Cohen2e6da152015-05-06 11:42:25 -0700478 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700479 return width / countX;
480 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700481 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700482 return height / countY;
483 }
484
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700485 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700486 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
487 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
488 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700489 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700490 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700491 return isLandscape && transposeLayoutWithOrientation;
492 }
493
494 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700495 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700496 }
497
Sunny Goyalc6205602015-05-21 20:46:33 -0700498 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800499 int visibleChildren = 0;
500 for (int i = 0; i < parent.getChildCount(); i++) {
501 if (parent.getChildAt(i).getVisibility() != View.GONE) {
502 visibleChildren++;
503 }
504 }
505 return visibleChildren;
506 }
507
Winson1f064272016-07-18 17:18:02 -0700508 public void layout(Launcher launcher, boolean notifyListeners) {
Winson Chungb3800242013-10-24 11:01:54 -0700509 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700510 boolean hasVerticalBarLayout = isVerticalBarLayout();
511
512 // Layout the search bar space
Sunny Goyal47328fd2016-05-25 18:56:41 -0700513 Point searchBarBounds = getSearchBarDimensForWidgetOpts();
514 View searchBar = launcher.getDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700515 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700516 lp.width = searchBarBounds.x;
517 lp.height = searchBarBounds.y;
Winson1f064272016-07-18 17:18:02 -0700518 lp.topMargin = mInsets.top + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700519 searchBar.setLayoutParams(lp);
520
Winson Chungb3800242013-10-24 11:01:54 -0700521 // Layout the workspace
522 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
Winson1f064272016-07-18 17:18:02 -0700523 Rect workspacePadding = getWorkspacePadding(null);
524 workspace.setPadding(workspacePadding.left, workspacePadding.top, workspacePadding.right,
525 workspacePadding.bottom);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700526 workspace.setPageSpacing(getWorkspacePageSpacing());
Winson Chungb3800242013-10-24 11:01:54 -0700527
Sunny Goyal6178f132016-07-11 17:30:03 -0700528 View qsbContainer = launcher.getQsbContainer();
529 lp = (FrameLayout.LayoutParams) qsbContainer.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700530 lp.topMargin = mInsets.top + workspacePadding.top;
Sunny Goyal6178f132016-07-11 17:30:03 -0700531 qsbContainer.setLayoutParams(lp);
532
Winson Chungb3800242013-10-24 11:01:54 -0700533 // Layout the hotseat
Winson6231ede2016-07-20 17:21:55 -0700534 Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat);
Winson Chungb3800242013-10-24 11:01:54 -0700535 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700536 // We want the edges of the hotseat to line up with the edges of the workspace, but the
537 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
538 // this, we pad the left and right of the hotseat with half of the difference of a workspace
539 // cell vs a hotseat cell.
540 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
541 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
542 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700543 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700544 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
545 // screen regardless of RTL
546 lp.gravity = Gravity.RIGHT;
Winson1f064272016-07-18 17:18:02 -0700547 lp.width = hotseatBarHeightPx + mInsets.left + mInsets.right;
Winson Chungb3800242013-10-24 11:01:54 -0700548 lp.height = LayoutParams.MATCH_PARENT;
Winson6231ede2016-07-20 17:21:55 -0700549 hotseat.getLayout().setPadding(mInsets.left, mInsets.top, mInsets.right,
550 workspacePadding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700551 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800552 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700553 lp.gravity = Gravity.BOTTOM;
554 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700555 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700556 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
557 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
Jon Mirandac1b08c52016-11-15 14:37:56 -0800558 hotseatBarBottomPaddingPx + mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700559 } else {
560 // For phones, layout the hotseat without any bottom margin
561 // to ensure that we have space for the folders
562 lp.gravity = Gravity.BOTTOM;
563 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700564 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700565 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
566 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
Jon Mirandac1b08c52016-11-15 14:37:56 -0800567 hotseatBarBottomPaddingPx + mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700568 }
569 hotseat.setLayoutParams(lp);
570
571 // Layout the page indicators
572 View pageIndicator = launcher.findViewById(R.id.page_indicator);
573 if (pageIndicator != null) {
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700574 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700575 if (isVerticalBarLayout()) {
576 if (mInsets.left > 0) {
577 lp.leftMargin = mInsets.left + pageIndicatorLandGutterLeftNavBarPx -
578 lp.width - pageIndicatorLandWorkspaceOffsetPx;
579 } else if (mInsets.right > 0) {
580 lp.leftMargin = pageIndicatorLandGutterRightNavBarPx - lp.width -
581 pageIndicatorLandWorkspaceOffsetPx;
582 }
583 lp.bottomMargin = workspacePadding.bottom;
584 } else {
Winson Chungb3800242013-10-24 11:01:54 -0700585 // Put the page indicators above the hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700586 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Winson1f064272016-07-18 17:18:02 -0700587 lp.height = pageIndicatorHeightPx;
588 lp.bottomMargin = hotseatBarHeightPx + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700589 }
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700590 pageIndicator.setLayoutParams(lp);
Winson Chungb3800242013-10-24 11:01:54 -0700591 }
592
Winson Chungb3800242013-10-24 11:01:54 -0700593 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800594 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700595 if (overviewMode != null) {
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700596 int visibleChildCount = getVisibleChildCount(overviewMode);
597 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
Jon Mirandadf3bc522016-11-01 16:55:56 -0700598 int maxWidth = totalItemWidth + (visibleChildCount - 1) * overviewModeBarSpacerWidthPx;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700599
Jon Mirandadf3bc522016-11-01 16:55:56 -0700600 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700601 lp.width = Math.min(availableWidthPx, maxWidth);
Jon Mirandadf3bc522016-11-01 16:55:56 -0700602 lp.height = getOverviewModeButtonBarHeight() + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700603 overviewMode.setLayoutParams(lp);
Winson1f064272016-07-18 17:18:02 -0700604 }
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700605
Winson1f064272016-07-18 17:18:02 -0700606 if (notifyListeners) {
607 for (int i = mListeners.size() - 1; i >= 0; i--) {
608 mListeners.get(i).onLauncherLayoutChanged();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700609 }
Winson Chungb3800242013-10-24 11:01:54 -0700610 }
611 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700612
613 private int getCurrentWidth() {
614 return isLandscape
615 ? Math.max(widthPx, heightPx)
616 : Math.min(widthPx, heightPx);
617 }
618
619 private int getCurrentHeight() {
620 return isLandscape
621 ? Math.min(widthPx, heightPx)
622 : Math.max(widthPx, heightPx);
623 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700624
Sunny Goyalc13403c2016-11-18 23:44:48 -0800625 public int getCellHeight(@ContainerType int containerType) {
626 switch (containerType) {
627 case CellLayout.WORKSPACE:
628 return cellHeightPx;
629 case CellLayout.FOLDER:
630 return folderCellHeightPx;
631 case CellLayout.HOTSEAT:
632 return hotseatCellHeightPx;
633 default:
634 // ??
635 return 0;
636 }
637 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700638
Winson1f064272016-07-18 17:18:02 -0700639 /**
640 * @return the left/right paddings for all containers.
641 */
Jon Mirandac3e4e222016-11-02 16:56:52 -0700642 public final int[] getContainerPadding() {
Winson1f064272016-07-18 17:18:02 -0700643 // No paddings for portrait phone
644 if (isPhone && !isVerticalBarLayout()) {
645 return new int[] {0, 0};
Sunny Goyal13178ac2016-04-04 16:35:22 -0700646 }
Winson1f064272016-07-18 17:18:02 -0700647
Hyunyoung Song9d96fd52016-08-23 11:06:24 -0700648 // In landscape, we match the width of the workspace
649 int padding = (pageIndicatorLandGutterRightNavBarPx +
650 hotseatBarHeightPx + hotseatLandGutterPx + mInsets.left) / 2;
Winson1f064272016-07-18 17:18:02 -0700651 return new int[]{ padding, padding };
Sunny Goyal13178ac2016-04-04 16:35:22 -0700652 }
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700653
Jon Miranda51f037d2016-11-07 08:37:20 -0800654 public boolean shouldIgnoreLongPressToOverview(float touchX) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700655 boolean inMultiWindowMode = this != inv.landscapeProfile && this != inv.portraitProfile;
Jon Miranda51f037d2016-11-07 08:37:20 -0800656 boolean touchedLhsEdge = mInsets.left == 0 && touchX < edgeMarginPx;
657 boolean touchedRhsEdge = mInsets.right == 0 && touchX > (widthPx - edgeMarginPx);
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700658 return !inMultiWindowMode && (touchedLhsEdge || touchedRhsEdge);
659 }
Winson Chungb3800242013-10-24 11:01:54 -0700660}