blob: b38109ce84c938715e531f85114ca10f8f5f7ecb [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
Sunny Goyalc13403c2016-11-18 23:44:48 -080032import com.android.launcher3.CellLayout.ContainerType;
Tony Wickhame0c33232016-02-08 11:37:04 -080033import com.android.launcher3.config.FeatureFlags;
34
Winson1f064272016-07-18 17:18:02 -070035import java.util.ArrayList;
36
Winson Chungb3800242013-10-24 11:01:54 -070037public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070038
Winson1f064272016-07-18 17:18:02 -070039 public interface LauncherLayoutChangeListener {
40 void onLauncherLayoutChanged();
41 }
42
Adam Cohen2e6da152015-05-06 11:42:25 -070043 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070044
Sunny Goyalc6205602015-05-21 20:46:33 -070045 // Device properties
46 public final boolean isTablet;
47 public final boolean isLargeTablet;
48 public final boolean isPhone;
49 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070050
Sunny Goyalc6205602015-05-21 20:46:33 -070051 // Device properties in current orientation
52 public final boolean isLandscape;
53 public final int widthPx;
54 public final int heightPx;
55 public final int availableWidthPx;
56 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070057 /**
58 * The maximum amount of left/right workspace padding as a percentage of the screen width.
59 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
60 * 7% of the screen width can be used as right padding.
61 */
62 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070063
Sunny Goyalc6205602015-05-21 20:46:33 -070064 // Overview mode
65 private final int overviewModeMinIconZoneHeightPx;
66 private final int overviewModeMaxIconZoneHeightPx;
67 private final int overviewModeBarItemWidthPx;
68 private final int overviewModeBarSpacerWidthPx;
69 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070070
Sunny Goyalc6205602015-05-21 20:46:33 -070071 // Workspace
72 private int desiredWorkspaceLeftRightMarginPx;
73 public final int edgeMarginPx;
74 public final Rect defaultWidgetPadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070075 private final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070076 private final int topWorkspacePadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070077 private float dragViewScale;
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
Sunny Goyal47328fd2016-05-25 18:56:41 -0700127 // Drop Target
128 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700129
Winson1f064272016-07-18 17:18:02 -0700130 // Insets
131 private Rect mInsets = new Rect();
132
133 // Listeners
134 private ArrayList<LauncherLayoutChangeListener> mListeners = new ArrayList<>();
135
Sunny Goyalc6205602015-05-21 20:46:33 -0700136 public DeviceProfile(Context context, InvariantDeviceProfile inv,
137 Point minSize, Point maxSize,
138 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700139
Adam Cohen2e6da152015-05-06 11:42:25 -0700140 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700141 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700142
Adam Cohen2e6da152015-05-06 11:42:25 -0700143 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700144 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700145
Sunny Goyalc6205602015-05-21 20:46:33 -0700146 // Constants from resources
147 isTablet = res.getBoolean(R.bool.is_tablet);
148 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
149 isPhone = !isTablet && !isLargeTablet;
150
151 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700152 transposeLayoutWithOrientation =
153 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700154
155 ComponentName cn = new ComponentName(context.getPackageName(),
156 this.getClass().getName());
157 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
158 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Winson1f064272016-07-18 17:18:02 -0700159 desiredWorkspaceLeftRightMarginPx = edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700160 pageIndicatorHeightPx =
161 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
Winson1f064272016-07-18 17:18:02 -0700162 pageIndicatorLandGutterLeftNavBarPx = res.getDimensionPixelSize(
163 R.dimen.dynamic_grid_page_indicator_gutter_width_left_nav_bar);
164 pageIndicatorLandWorkspaceOffsetPx =
165 res.getDimensionPixelSize(R.dimen.all_apps_caret_workspace_offset);
166 pageIndicatorLandGutterRightNavBarPx = res.getDimensionPixelSize(
167 R.dimen.dynamic_grid_page_indicator_gutter_width_right_nav_bar);
Winson Chungb3800242013-10-24 11:01:54 -0700168 defaultPageSpacingPx =
169 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700170 topWorkspacePadding =
171 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700172 overviewModeMinIconZoneHeightPx =
173 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
174 overviewModeMaxIconZoneHeightPx =
175 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800176 overviewModeBarItemWidthPx =
177 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
178 overviewModeBarSpacerWidthPx =
179 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700180 overviewModeIconZoneRatio =
181 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700182 iconDrawablePaddingOriginalPx =
183 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700184 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
185 workspaceSpringLoadedBottomSpace =
186 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Winson1f064272016-07-18 17:18:02 -0700187 hotseatBarHeightPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height);
188 hotseatBarTopPaddingPx =
189 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Jon Mirandac1b08c52016-11-15 14:37:56 -0800190 hotseatBarBottomPaddingPx = 0;
Winson1f064272016-07-18 17:18:02 -0700191 hotseatLandGutterPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_gutter_width);
Winson Chungb3800242013-10-24 11:01:54 -0700192
Sunny Goyalc6205602015-05-21 20:46:33 -0700193 // Determine sizes.
194 widthPx = width;
195 heightPx = height;
196 if (isLandscape) {
197 availableWidthPx = maxSize.x;
198 availableHeightPx = minSize.y;
199 } else {
200 availableWidthPx = minSize.x;
201 availableHeightPx = maxSize.y;
202 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700203
Winson Chungb3800242013-10-24 11:01:54 -0700204 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700205 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700206 computeAllAppsButtonSize(context);
207 }
208
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700209 DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
210 // In multi-window mode, we can have widthPx = availableWidthPx
211 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
212 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800213 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
214 isLandscape);
215
216 // Hide labels on the workspace.
217 profile.iconTextSizePx = 0;
218 profile.cellHeightPx = profile.iconSizePx + profile.iconDrawablePaddingPx
219 + Utilities.calculateTextHeight(profile.iconTextSizePx);
220
Jon Mirandac1b08c52016-11-15 14:37:56 -0800221 // The nav bar is black so we add bottom padding to visually center hotseat icons.
222 profile.hotseatBarBottomPaddingPx = profile.hotseatBarTopPaddingPx;
223
Jon Miranda93e1f042016-11-11 14:13:04 -0800224 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700225 }
226
Winson1f064272016-07-18 17:18:02 -0700227 public void addLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
228 if (!mListeners.contains(listener)) {
229 mListeners.add(listener);
230 }
231 }
232
233 public void removeLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
234 if (mListeners.contains(listener)) {
235 mListeners.remove(listener);
236 }
237 }
238
Adam Cohen63f1ec02014-08-12 09:23:13 -0700239 /**
240 * Determine the exact visual footprint of the all apps button, taking into account scaling
241 * and internal padding of the drawable.
242 */
243 private void computeAllAppsButtonSize(Context context) {
244 Resources res = context.getResources();
245 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700246 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
247 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700248 }
249
Sunny Goyalc6205602015-05-21 20:46:33 -0700250 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700251 updateIconSize(1f, iconDrawablePaddingOriginalPx, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800252
Jon Mirandabf7d8122016-11-03 15:29:29 -0700253 // Check to see if the icons fit within the available height. If not, then scale down.
254 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700255 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700256 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700257 float scale = maxHeight / usedHeight;
258 updateIconSize(scale, 0, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700259 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700260
261 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700262 }
263
Winson Chung93f98ea2015-03-10 16:28:47 -0700264 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700265 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700266 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800267 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700268 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700269 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson1f064272016-07-18 17:18:02 -0700270 allAppsIconSizePx = iconSizePx;
271 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
272 allAppsIconTextSizePx = iconTextSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700273
Winson Chungb3800242013-10-24 11:01:54 -0700274 cellWidthPx = iconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700275 cellHeightPx = iconSizePx + iconDrawablePaddingPx
276 + Utilities.calculateTextHeight(iconTextSizePx);
Tony Wickhame0c33232016-02-08 11:37:04 -0800277 final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
278 : res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800279 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700280
281 // Hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700282 hotseatCellWidthPx = iconSizePx;
283 hotseatCellHeightPx = iconSizePx;
284
Sunny Goyal47328fd2016-05-25 18:56:41 -0700285 if (!isVerticalBarLayout()) {
286 int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
287 - pageIndicatorHeightPx - topWorkspacePadding;
288 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
289 workspaceSpringLoadShrinkFactor = Math.min(
290 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
291 1 - (minRequiredHeight / expectedWorkspaceHeight));
292 } else {
293 workspaceSpringLoadShrinkFactor =
294 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
295 }
296
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700297 // Folder icon
Winson Chungb3800242013-10-24 11:01:54 -0700298 folderBackgroundOffset = -edgeMarginPx;
299 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800300 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700301 }
302
Jon Mirandabf7d8122016-11-03 15:29:29 -0700303 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
304 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
305 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
306 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
307
308 updateFolderCellSize(1f, dm, res, folderBottomPanelSize);
309
310 // Check to see if the icons fit within the available height. If not, then scale down.
311 float usedHeight = (folderCellHeightPx * inv.numFolderRows) + folderBottomPanelSize;
312 int maxHeight = availableHeightPx - getTotalWorkspacePadding().y - (2 * edgeMarginPx);
313 if (usedHeight > maxHeight) {
314 float scale = maxHeight / usedHeight;
315 updateFolderCellSize(scale, dm, res, folderBottomPanelSize);
316 }
317 }
318
319 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res,
320 int folderBottomPanelSize) {
321 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
322 folderChildTextSizePx =
323 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
324
325 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
326 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
327 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
328
329 // Don't let the folder get too close to the edges of the screen.
330 folderCellWidthPx = Math.min(folderChildIconSizePx + 2 * cellPaddingX,
331 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
332 folderCellHeightPx = Math.min(folderChildIconSizePx + 2 * cellPaddingY + textHeight,
333 (availableHeightPx - 4 * edgeMarginPx - folderBottomPanelSize) / inv.numFolderRows);
334 folderChildDrawablePaddingPx = Math.max(0,
335 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
336 }
337
Winson1f064272016-07-18 17:18:02 -0700338 public void updateInsets(Rect insets) {
339 mInsets.set(insets);
340 }
341
Winson1f064272016-07-18 17:18:02 -0700342 public void updateAppsViewNumCols() {
343 allAppsNumCols = allAppsNumPredictiveCols = inv.numColumns;
Winson Chungb3800242013-10-24 11:01:54 -0700344 }
345
Tony Wickhama3c74d12015-10-23 11:43:47 -0700346 /** Returns the width and height of the search bar, ignoring any padding. */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700347 public Point getSearchBarDimensForWidgetOpts() {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700348 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700349 return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700350 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700351 int gap;
Sunny Goyalc6205602015-05-21 20:46:33 -0700352 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700353 // Pad the left and right of the workspace to ensure consistent spacing
354 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700355 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700356 // XXX: If the icon size changes across orientations, we will have to take
357 // that into account here too.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700358 gap = ((width - 2 * edgeMarginPx
359 - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
360 + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700361 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700362 gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
Winson Chungb3800242013-10-24 11:01:54 -0700363 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700364 return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700365 }
Winson Chungb3800242013-10-24 11:01:54 -0700366 }
367
Sunny Goyal756cd262015-08-20 12:33:21 -0700368 public Point getCellSize() {
369 Point result = new Point();
370 // Since we are only concerned with the overall padding, layout direction does
371 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700372 Point padding = getTotalWorkspacePadding();
373 result.x = calculateCellWidth(availableWidthPx - padding.x, inv.numColumns);
374 result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700375 return result;
376 }
377
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700378 public Point getTotalWorkspacePadding() {
Sunny Goyal6178f132016-07-11 17:30:03 -0700379 Rect padding = getWorkspacePadding(null);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700380 return new Point(padding.left + padding.right, padding.top + padding.bottom);
381 }
382
383 /**
384 * Returns the workspace padding in the specified orientation.
385 * Note that it assumes that while in verticalBarLayout, the nav bar is on the right, as such
386 * this value is not reliable.
387 * Use {@link #getTotalWorkspacePadding()} instead.
388 */
Sunny Goyal6178f132016-07-11 17:30:03 -0700389 public Rect getWorkspacePadding(Rect recycle) {
390 Rect padding = recycle == null ? new Rect() : recycle;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700391 if (isVerticalBarLayout()) {
Winson1f064272016-07-18 17:18:02 -0700392 if (mInsets.left > 0) {
393 padding.set(mInsets.left + pageIndicatorLandGutterLeftNavBarPx, 0,
394 hotseatBarHeightPx + hotseatLandGutterPx - mInsets.left, 2 * edgeMarginPx);
395 } else {
396 padding.set(pageIndicatorLandGutterRightNavBarPx, 0,
397 hotseatBarHeightPx + hotseatLandGutterPx, 2 * edgeMarginPx);
398 }
Winson Chungb3800242013-10-24 11:01:54 -0700399 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700400 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700401 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700402 // Pad the left and right of the workspace to ensure consistent spacing
403 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800404 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700405 int width = getCurrentWidth();
406 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700407 // The amount of screen space available for left/right padding.
408 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
409 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
410 availablePaddingX = (int) Math.min(availablePaddingX,
411 width * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700412 int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800413 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
414 - hotseatBarBottomPaddingPx);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700415 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700416 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700417 } else {
418 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700419 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700420 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700421 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700422 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700423 }
424 }
425 return padding;
426 }
427
Winsonfadbe8f2016-07-22 16:35:37 -0700428 /**
429 * @return the bounds for which the open folders should be contained within
430 */
431 public Rect getAbsoluteOpenFolderBounds() {
432 if (isVerticalBarLayout()) {
433 // Folders should only appear right of the drop target bar and left of the hotseat
434 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
435 mInsets.top,
436 mInsets.left + availableWidthPx - hotseatBarHeightPx - edgeMarginPx,
437 mInsets.top + availableHeightPx);
438 } else {
439 // Folders should only appear below the drop target bar and above the hotseat
440 return new Rect(mInsets.left,
441 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
442 mInsets.left + availableWidthPx,
443 mInsets.top + availableHeightPx - hotseatBarHeightPx - pageIndicatorHeightPx -
444 edgeMarginPx);
445 }
446 }
447
Sunny Goyal47328fd2016-05-25 18:56:41 -0700448 private int getWorkspacePageSpacing() {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700449 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700450 // In landscape mode the page spacing is set to the default.
451 return defaultPageSpacingPx;
452 } else {
453 // In portrait, we want the pages spaced such that there is no
454 // overhang of the previous / next page into the current page viewport.
455 // We assume symmetrical padding in portrait mode.
Winson11439c52016-07-20 14:25:07 -0700456 return Math.max(defaultPageSpacingPx, getWorkspacePadding(null).left + 1);
Winson Chungb3800242013-10-24 11:01:54 -0700457 }
458 }
459
Winson Chung4f7eb402015-07-10 18:00:48 -0700460 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700461 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
462 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
463 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700464 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700465 }
466
Adam Cohen2e6da152015-05-06 11:42:25 -0700467 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700468 return width / countX;
469 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700470 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700471 return height / countY;
472 }
473
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700474 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700475 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
476 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
477 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700478 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700479 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700480 return isLandscape && transposeLayoutWithOrientation;
481 }
482
483 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700484 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700485 }
486
Sunny Goyalc6205602015-05-21 20:46:33 -0700487 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800488 int visibleChildren = 0;
489 for (int i = 0; i < parent.getChildCount(); i++) {
490 if (parent.getChildAt(i).getVisibility() != View.GONE) {
491 visibleChildren++;
492 }
493 }
494 return visibleChildren;
495 }
496
Winson1f064272016-07-18 17:18:02 -0700497 public void layout(Launcher launcher, boolean notifyListeners) {
Winson Chungb3800242013-10-24 11:01:54 -0700498 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700499 boolean hasVerticalBarLayout = isVerticalBarLayout();
500
501 // Layout the search bar space
Sunny Goyal47328fd2016-05-25 18:56:41 -0700502 Point searchBarBounds = getSearchBarDimensForWidgetOpts();
503 View searchBar = launcher.getDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700504 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700505 lp.width = searchBarBounds.x;
506 lp.height = searchBarBounds.y;
Winson1f064272016-07-18 17:18:02 -0700507 lp.topMargin = mInsets.top + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700508 searchBar.setLayoutParams(lp);
509
Winson Chungb3800242013-10-24 11:01:54 -0700510 // Layout the workspace
511 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
Winson1f064272016-07-18 17:18:02 -0700512 Rect workspacePadding = getWorkspacePadding(null);
513 workspace.setPadding(workspacePadding.left, workspacePadding.top, workspacePadding.right,
514 workspacePadding.bottom);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700515 workspace.setPageSpacing(getWorkspacePageSpacing());
Winson Chungb3800242013-10-24 11:01:54 -0700516
Sunny Goyal6178f132016-07-11 17:30:03 -0700517 View qsbContainer = launcher.getQsbContainer();
518 lp = (FrameLayout.LayoutParams) qsbContainer.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700519 lp.topMargin = mInsets.top + workspacePadding.top;
Sunny Goyal6178f132016-07-11 17:30:03 -0700520 qsbContainer.setLayoutParams(lp);
521
Winson Chungb3800242013-10-24 11:01:54 -0700522 // Layout the hotseat
Winson6231ede2016-07-20 17:21:55 -0700523 Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat);
Winson Chungb3800242013-10-24 11:01:54 -0700524 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700525 // We want the edges of the hotseat to line up with the edges of the workspace, but the
526 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
527 // this, we pad the left and right of the hotseat with half of the difference of a workspace
528 // cell vs a hotseat cell.
529 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
530 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
531 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700532 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700533 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
534 // screen regardless of RTL
535 lp.gravity = Gravity.RIGHT;
Winson1f064272016-07-18 17:18:02 -0700536 lp.width = hotseatBarHeightPx + mInsets.left + mInsets.right;
Winson Chungb3800242013-10-24 11:01:54 -0700537 lp.height = LayoutParams.MATCH_PARENT;
Winson6231ede2016-07-20 17:21:55 -0700538 hotseat.getLayout().setPadding(mInsets.left, mInsets.top, mInsets.right,
539 workspacePadding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700540 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800541 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700542 lp.gravity = Gravity.BOTTOM;
543 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700544 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700545 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
546 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
Jon Mirandac1b08c52016-11-15 14:37:56 -0800547 hotseatBarBottomPaddingPx + mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700548 } else {
549 // For phones, layout the hotseat without any bottom margin
550 // to ensure that we have space for the folders
551 lp.gravity = Gravity.BOTTOM;
552 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700553 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700554 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
555 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
Jon Mirandac1b08c52016-11-15 14:37:56 -0800556 hotseatBarBottomPaddingPx + mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700557 }
558 hotseat.setLayoutParams(lp);
559
560 // Layout the page indicators
561 View pageIndicator = launcher.findViewById(R.id.page_indicator);
562 if (pageIndicator != null) {
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700563 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700564 if (isVerticalBarLayout()) {
565 if (mInsets.left > 0) {
566 lp.leftMargin = mInsets.left + pageIndicatorLandGutterLeftNavBarPx -
567 lp.width - pageIndicatorLandWorkspaceOffsetPx;
568 } else if (mInsets.right > 0) {
569 lp.leftMargin = pageIndicatorLandGutterRightNavBarPx - lp.width -
570 pageIndicatorLandWorkspaceOffsetPx;
571 }
572 lp.bottomMargin = workspacePadding.bottom;
573 } else {
Winson Chungb3800242013-10-24 11:01:54 -0700574 // Put the page indicators above the hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700575 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Winson1f064272016-07-18 17:18:02 -0700576 lp.height = pageIndicatorHeightPx;
577 lp.bottomMargin = hotseatBarHeightPx + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700578 }
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700579 pageIndicator.setLayoutParams(lp);
Winson Chungb3800242013-10-24 11:01:54 -0700580 }
581
Winson Chungb3800242013-10-24 11:01:54 -0700582 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800583 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700584 if (overviewMode != null) {
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700585 int visibleChildCount = getVisibleChildCount(overviewMode);
586 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
Jon Mirandadf3bc522016-11-01 16:55:56 -0700587 int maxWidth = totalItemWidth + (visibleChildCount - 1) * overviewModeBarSpacerWidthPx;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700588
Jon Mirandadf3bc522016-11-01 16:55:56 -0700589 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700590 lp.width = Math.min(availableWidthPx, maxWidth);
Jon Mirandadf3bc522016-11-01 16:55:56 -0700591 lp.height = getOverviewModeButtonBarHeight() + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700592 overviewMode.setLayoutParams(lp);
Winson1f064272016-07-18 17:18:02 -0700593 }
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700594
Winson1f064272016-07-18 17:18:02 -0700595 if (notifyListeners) {
596 for (int i = mListeners.size() - 1; i >= 0; i--) {
597 mListeners.get(i).onLauncherLayoutChanged();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700598 }
Winson Chungb3800242013-10-24 11:01:54 -0700599 }
600 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700601
602 private int getCurrentWidth() {
603 return isLandscape
604 ? Math.max(widthPx, heightPx)
605 : Math.min(widthPx, heightPx);
606 }
607
608 private int getCurrentHeight() {
609 return isLandscape
610 ? Math.min(widthPx, heightPx)
611 : Math.max(widthPx, heightPx);
612 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700613
Sunny Goyalc13403c2016-11-18 23:44:48 -0800614 public int getCellHeight(@ContainerType int containerType) {
615 switch (containerType) {
616 case CellLayout.WORKSPACE:
617 return cellHeightPx;
618 case CellLayout.FOLDER:
619 return folderCellHeightPx;
620 case CellLayout.HOTSEAT:
621 return hotseatCellHeightPx;
622 default:
623 // ??
624 return 0;
625 }
626 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700627
Winson1f064272016-07-18 17:18:02 -0700628 /**
629 * @return the left/right paddings for all containers.
630 */
Jon Mirandac3e4e222016-11-02 16:56:52 -0700631 public final int[] getContainerPadding() {
Winson1f064272016-07-18 17:18:02 -0700632 // No paddings for portrait phone
633 if (isPhone && !isVerticalBarLayout()) {
634 return new int[] {0, 0};
Sunny Goyal13178ac2016-04-04 16:35:22 -0700635 }
Winson1f064272016-07-18 17:18:02 -0700636
Hyunyoung Song9d96fd52016-08-23 11:06:24 -0700637 // In landscape, we match the width of the workspace
638 int padding = (pageIndicatorLandGutterRightNavBarPx +
639 hotseatBarHeightPx + hotseatLandGutterPx + mInsets.left) / 2;
Winson1f064272016-07-18 17:18:02 -0700640 return new int[]{ padding, padding };
Sunny Goyal13178ac2016-04-04 16:35:22 -0700641 }
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700642
Jon Miranda51f037d2016-11-07 08:37:20 -0800643 public boolean shouldIgnoreLongPressToOverview(float touchX) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700644 boolean inMultiWindowMode = this != inv.landscapeProfile && this != inv.portraitProfile;
Jon Miranda51f037d2016-11-07 08:37:20 -0800645 boolean touchedLhsEdge = mInsets.left == 0 && touchX < edgeMarginPx;
646 boolean touchedRhsEdge = mInsets.right == 0 && touchX > (widthPx - edgeMarginPx);
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700647 return !inMultiWindowMode && (touchedLhsEdge || touchedRhsEdge);
648 }
Winson Chungb3800242013-10-24 11:01:54 -0700649}