blob: c32f2d55032ab2bb5a761b15e7b5e1248ccaba34 [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 Wickham9a8d11f2017-01-11 09:53:12 -080034import com.android.launcher3.badge.BadgeRenderer;
Louis Beginb07a3552017-02-27 14:33:11 +080035import com.android.launcher3.config.FeatureFlags;
Tony Wickhame0c33232016-02-08 11:37:04 -080036
Winson1f064272016-07-18 17:18:02 -070037import java.util.ArrayList;
38
Winson Chungb3800242013-10-24 11:01:54 -070039public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070040
Winson1f064272016-07-18 17:18:02 -070041 public interface LauncherLayoutChangeListener {
42 void onLauncherLayoutChanged();
43 }
44
Adam Cohen2e6da152015-05-06 11:42:25 -070045 public final InvariantDeviceProfile inv;
Winson Chungbe876472014-05-14 14:15:20 -070046
Sunny Goyalc6205602015-05-21 20:46:33 -070047 // Device properties
48 public final boolean isTablet;
49 public final boolean isLargeTablet;
50 public final boolean isPhone;
51 public final boolean transposeLayoutWithOrientation;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070052
Sunny Goyalc6205602015-05-21 20:46:33 -070053 // Device properties in current orientation
54 public final boolean isLandscape;
55 public final int widthPx;
56 public final int heightPx;
57 public final int availableWidthPx;
58 public final int availableHeightPx;
Tony Wickhamd6b40372015-09-23 18:37:57 -070059 /**
60 * The maximum amount of left/right workspace padding as a percentage of the screen width.
61 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
62 * 7% of the screen width can be used as right padding.
63 */
64 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -070065
Sunny Goyalc6205602015-05-21 20:46:33 -070066 // Overview mode
67 private final int overviewModeMinIconZoneHeightPx;
68 private final int overviewModeMaxIconZoneHeightPx;
69 private final int overviewModeBarItemWidthPx;
70 private final int overviewModeBarSpacerWidthPx;
71 private final float overviewModeIconZoneRatio;
Winson Chungb3800242013-10-24 11:01:54 -070072
Sunny Goyalc6205602015-05-21 20:46:33 -070073 // Workspace
74 private int desiredWorkspaceLeftRightMarginPx;
75 public final int edgeMarginPx;
76 public final Rect defaultWidgetPadding;
Sunny Goyalc6205602015-05-21 20:46:33 -070077 private final int defaultPageSpacingPx;
Sunny Goyal47328fd2016-05-25 18:56:41 -070078 private final int topWorkspacePadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070079 public float workspaceSpringLoadShrinkFactor;
80 public final int workspaceSpringLoadedBottomSpace;
Sunny Goyalc6205602015-05-21 20:46:33 -070081
Winson1f064272016-07-18 17:18:02 -070082 // Page indicator
83 private final int pageIndicatorHeightPx;
84 private final int pageIndicatorLandGutterLeftNavBarPx;
85 private final int pageIndicatorLandGutterRightNavBarPx;
86 private final int pageIndicatorLandWorkspaceOffsetPx;
87
Sunny Goyalc6205602015-05-21 20:46:33 -070088 // Workspace icons
89 public int iconSizePx;
90 public int iconTextSizePx;
91 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070092 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -070093
Adam Cohen59400422014-03-05 18:07:04 -080094 public int cellWidthPx;
95 public int cellHeightPx;
96
Sunny Goyalc6205602015-05-21 20:46:33 -070097 // Folder
98 public int folderBackgroundOffset;
99 public int folderIconSizePx;
Adam Cohenefca0272016-02-24 19:19:06 -0800100 public int folderIconPreviewPadding;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700101
102 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700103 public int folderCellWidthPx;
104 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700105
106 // Folder child
107 public int folderChildIconSizePx;
108 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700109 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700110
Sunny Goyalc6205602015-05-21 20:46:33 -0700111 // Hotseat
112 public int hotseatCellWidthPx;
113 public int hotseatCellHeightPx;
114 public int hotseatIconSizePx;
Jon Mirandaaca21452016-09-26 14:01:56 -0700115 public int hotseatBarHeightPx;
Winson1f064272016-07-18 17:18:02 -0700116 private int hotseatBarTopPaddingPx;
Jon Mirandac1b08c52016-11-15 14:37:56 -0800117 private int hotseatBarBottomPaddingPx;
Winson1f064272016-07-18 17:18:02 -0700118 private int hotseatLandGutterPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700119
Sunny Goyalc6205602015-05-21 20:46:33 -0700120 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700121 public int allAppsNumCols;
122 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -0700123 public int allAppsButtonVisualSize;
Winson1f064272016-07-18 17:18:02 -0700124 public int allAppsIconSizePx;
125 public int allAppsIconDrawablePaddingPx;
126 public float allAppsIconTextSizePx;
127
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800128 // Widgets
129 public final PointF appWidgetScale = new PointF(1.0f, 1.0f);
130
Sunny Goyal47328fd2016-05-25 18:56:41 -0700131 // Drop Target
132 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700133
Winson1f064272016-07-18 17:18:02 -0700134 // Insets
135 private Rect mInsets = new Rect();
136
137 // Listeners
138 private ArrayList<LauncherLayoutChangeListener> mListeners = new ArrayList<>();
139
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800140 // Icon badges
141 public BadgeRenderer mBadgeRenderer;
142
Sunny Goyalc6205602015-05-21 20:46:33 -0700143 public DeviceProfile(Context context, InvariantDeviceProfile inv,
144 Point minSize, Point maxSize,
145 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700146
Adam Cohen2e6da152015-05-06 11:42:25 -0700147 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700148 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700149
Adam Cohen2e6da152015-05-06 11:42:25 -0700150 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700151 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700152
Sunny Goyalc6205602015-05-21 20:46:33 -0700153 // Constants from resources
154 isTablet = res.getBoolean(R.bool.is_tablet);
155 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
156 isPhone = !isTablet && !isLargeTablet;
157
158 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700159 transposeLayoutWithOrientation =
160 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700161
162 ComponentName cn = new ComponentName(context.getPackageName(),
163 this.getClass().getName());
164 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
165 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Winson1f064272016-07-18 17:18:02 -0700166 desiredWorkspaceLeftRightMarginPx = edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700167 pageIndicatorHeightPx =
168 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
Winson1f064272016-07-18 17:18:02 -0700169 pageIndicatorLandGutterLeftNavBarPx = res.getDimensionPixelSize(
170 R.dimen.dynamic_grid_page_indicator_gutter_width_left_nav_bar);
171 pageIndicatorLandWorkspaceOffsetPx =
172 res.getDimensionPixelSize(R.dimen.all_apps_caret_workspace_offset);
173 pageIndicatorLandGutterRightNavBarPx = res.getDimensionPixelSize(
174 R.dimen.dynamic_grid_page_indicator_gutter_width_right_nav_bar);
Winson Chungb3800242013-10-24 11:01:54 -0700175 defaultPageSpacingPx =
176 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700177 topWorkspacePadding =
178 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700179 overviewModeMinIconZoneHeightPx =
180 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
181 overviewModeMaxIconZoneHeightPx =
182 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800183 overviewModeBarItemWidthPx =
184 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
185 overviewModeBarSpacerWidthPx =
186 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700187 overviewModeIconZoneRatio =
188 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700189 iconDrawablePaddingOriginalPx =
190 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700191 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
192 workspaceSpringLoadedBottomSpace =
193 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Sunny Goyald5190522017-04-24 03:06:54 -0700194
Winson1f064272016-07-18 17:18:02 -0700195 hotseatBarTopPaddingPx =
196 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
Sunny Goyald5190522017-04-24 03:06:54 -0700197 hotseatBarBottomPaddingPx =
198 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding);
199 hotseatBarHeightPx = hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx +
200 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height);
201 hotseatLandGutterPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_land_gutter_width);
Winson Chungb3800242013-10-24 11:01:54 -0700202
Sunny Goyalc6205602015-05-21 20:46:33 -0700203 // Determine sizes.
204 widthPx = width;
205 heightPx = height;
206 if (isLandscape) {
207 availableWidthPx = maxSize.x;
208 availableHeightPx = minSize.y;
209 } else {
210 availableWidthPx = minSize.x;
211 availableHeightPx = maxSize.y;
212 }
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700213
Winson Chungb3800242013-10-24 11:01:54 -0700214 // Calculate the remaining vars
Sunny Goyalc6205602015-05-21 20:46:33 -0700215 updateAvailableDimensions(dm, res);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700216 computeAllAppsButtonSize(context);
Tony Wickham1237df02017-02-24 08:59:36 -0800217
218 // This is done last, after iconSizePx is calculated above.
219 mBadgeRenderer = new BadgeRenderer(context, iconSizePx);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700220 }
221
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700222 DeviceProfile getMultiWindowProfile(Context context, Point mwSize) {
223 // In multi-window mode, we can have widthPx = availableWidthPx
224 // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles'
225 // widthPx and heightPx values where it's needed.
Jon Miranda93e1f042016-11-11 14:13:04 -0800226 DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y,
227 isLandscape);
228
229 // Hide labels on the workspace.
230 profile.iconTextSizePx = 0;
231 profile.cellHeightPx = profile.iconSizePx + profile.iconDrawablePaddingPx
232 + Utilities.calculateTextHeight(profile.iconTextSizePx);
233
Jon Mirandac1b08c52016-11-15 14:37:56 -0800234 // The nav bar is black so we add bottom padding to visually center hotseat icons.
235 profile.hotseatBarBottomPaddingPx = profile.hotseatBarTopPaddingPx;
236
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800237 // We use these scales to measure and layout the widgets using their full invariant profile
238 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
239 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
240 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
241 profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);
242
Jon Miranda93e1f042016-11-11 14:13:04 -0800243 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700244 }
245
Winson1f064272016-07-18 17:18:02 -0700246 public void addLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
247 if (!mListeners.contains(listener)) {
248 mListeners.add(listener);
249 }
250 }
251
252 public void removeLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
253 if (mListeners.contains(listener)) {
254 mListeners.remove(listener);
255 }
256 }
257
Adam Cohen63f1ec02014-08-12 09:23:13 -0700258 /**
259 * Determine the exact visual footprint of the all apps button, taking into account scaling
260 * and internal padding of the drawable.
261 */
262 private void computeAllAppsButtonSize(Context context) {
263 Resources res = context.getResources();
264 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700265 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
266 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700267 }
268
Sunny Goyalc6205602015-05-21 20:46:33 -0700269 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700270 updateIconSize(1f, iconDrawablePaddingOriginalPx, res, dm);
Winson Chung59a488a2013-12-10 12:32:14 -0800271
Jon Mirandabf7d8122016-11-03 15:29:29 -0700272 // Check to see if the icons fit within the available height. If not, then scale down.
273 float usedHeight = (cellHeightPx * inv.numRows);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700274 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700275 if (usedHeight > maxHeight) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700276 float scale = maxHeight / usedHeight;
277 updateIconSize(scale, 0, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700278 }
Jon Mirandabf7d8122016-11-03 15:29:29 -0700279
280 updateAvailableFolderCellDimensions(dm, res);
Winson Chungb3800242013-10-24 11:01:54 -0700281 }
282
Winson Chung93f98ea2015-03-10 16:28:47 -0700283 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700284 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700285 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800286 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700287 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700288 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson1f064272016-07-18 17:18:02 -0700289 allAppsIconSizePx = iconSizePx;
290 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
291 allAppsIconTextSizePx = iconTextSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700292
Winson Chungb3800242013-10-24 11:01:54 -0700293 cellWidthPx = iconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700294 cellHeightPx = iconSizePx + iconDrawablePaddingPx
295 + Utilities.calculateTextHeight(iconTextSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700296
297 // Hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700298 hotseatCellWidthPx = iconSizePx;
299 hotseatCellHeightPx = iconSizePx;
300
Sunny Goyal47328fd2016-05-25 18:56:41 -0700301 if (!isVerticalBarLayout()) {
302 int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
303 - pageIndicatorHeightPx - topWorkspacePadding;
304 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
305 workspaceSpringLoadShrinkFactor = Math.min(
306 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
307 1 - (minRequiredHeight / expectedWorkspaceHeight));
308 } else {
309 workspaceSpringLoadShrinkFactor =
310 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
311 }
312
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700313 // Folder icon
Winson Chungb3800242013-10-24 11:01:54 -0700314 folderBackgroundOffset = -edgeMarginPx;
315 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800316 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700317 }
318
Jon Mirandabf7d8122016-11-03 15:29:29 -0700319 private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) {
320 int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top)
321 + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom)
322 + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size));
323
Jon Mirandac1b23992016-12-13 08:57:36 -0800324 updateFolderCellSize(1f, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700325
Jon Mirandac1b23992016-12-13 08:57:36 -0800326 // Don't let the folder get too close to the edges of the screen.
327 int folderMargin = 4 * edgeMarginPx;
328
329 // Check if the icons fit within the available height.
330 float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize;
331 int maxHeight = availableHeightPx - getTotalWorkspacePadding().y - folderMargin;
332 float scaleY = maxHeight / usedHeight;
333
334 // Check if the icons fit within the available width.
335 float usedWidth = folderCellWidthPx * inv.numFolderColumns;
336 int maxWidth = availableWidthPx - getTotalWorkspacePadding().x - folderMargin;
337 float scaleX = maxWidth / usedWidth;
338
339 float scale = Math.min(scaleX, scaleY);
340 if (scale < 1f) {
341 updateFolderCellSize(scale, dm, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700342 }
343 }
344
Jon Mirandac1b23992016-12-13 08:57:36 -0800345 private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700346 folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
347 folderChildTextSizePx =
348 (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale);
349
350 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
351 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale);
352 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale);
353
Jon Mirandac1b23992016-12-13 08:57:36 -0800354 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
355 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700356 folderChildDrawablePaddingPx = Math.max(0,
357 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
358 }
359
Winson1f064272016-07-18 17:18:02 -0700360 public void updateInsets(Rect insets) {
361 mInsets.set(insets);
362 }
363
Winson1f064272016-07-18 17:18:02 -0700364 public void updateAppsViewNumCols() {
365 allAppsNumCols = allAppsNumPredictiveCols = inv.numColumns;
Winson Chungb3800242013-10-24 11:01:54 -0700366 }
367
Tony Wickhama3c74d12015-10-23 11:43:47 -0700368 /** Returns the width and height of the search bar, ignoring any padding. */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700369 public Point getSearchBarDimensForWidgetOpts() {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700370 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700371 return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700372 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700373 int gap;
Sunny Goyalc6205602015-05-21 20:46:33 -0700374 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700375 // Pad the left and right of the workspace to ensure consistent spacing
376 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700377 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700378 // XXX: If the icon size changes across orientations, we will have to take
379 // that into account here too.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700380 gap = ((width - 2 * edgeMarginPx
381 - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
382 + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700383 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700384 gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
Winson Chungb3800242013-10-24 11:01:54 -0700385 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700386 return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700387 }
Winson Chungb3800242013-10-24 11:01:54 -0700388 }
389
Sunny Goyal756cd262015-08-20 12:33:21 -0700390 public Point getCellSize() {
391 Point result = new Point();
392 // Since we are only concerned with the overall padding, layout direction does
393 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700394 Point padding = getTotalWorkspacePadding();
395 result.x = calculateCellWidth(availableWidthPx - padding.x, inv.numColumns);
396 result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700397 return result;
398 }
399
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700400 public Point getTotalWorkspacePadding() {
Sunny Goyal6178f132016-07-11 17:30:03 -0700401 Rect padding = getWorkspacePadding(null);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700402 return new Point(padding.left + padding.right, padding.top + padding.bottom);
403 }
404
405 /**
406 * Returns the workspace padding in the specified orientation.
407 * Note that it assumes that while in verticalBarLayout, the nav bar is on the right, as such
408 * this value is not reliable.
409 * Use {@link #getTotalWorkspacePadding()} instead.
410 */
Sunny Goyal6178f132016-07-11 17:30:03 -0700411 public Rect getWorkspacePadding(Rect recycle) {
412 Rect padding = recycle == null ? new Rect() : recycle;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700413 if (isVerticalBarLayout()) {
Winson1f064272016-07-18 17:18:02 -0700414 if (mInsets.left > 0) {
415 padding.set(mInsets.left + pageIndicatorLandGutterLeftNavBarPx, 0,
416 hotseatBarHeightPx + hotseatLandGutterPx - mInsets.left, 2 * edgeMarginPx);
417 } else {
418 padding.set(pageIndicatorLandGutterRightNavBarPx, 0,
419 hotseatBarHeightPx + hotseatLandGutterPx, 2 * edgeMarginPx);
420 }
Winson Chungb3800242013-10-24 11:01:54 -0700421 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700422 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700423 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700424 // Pad the left and right of the workspace to ensure consistent spacing
425 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700426 int width = getCurrentWidth();
427 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700428 // The amount of screen space available for left/right padding.
Sunny Goyalf5440cb2016-12-14 15:13:00 -0800429 int availablePaddingX = Math.max(0, width - ((inv.numColumns * cellWidthPx) +
430 ((inv.numColumns - 1) * cellWidthPx)));
Tony Wickhamd6b40372015-09-23 18:37:57 -0700431 availablePaddingX = (int) Math.min(availablePaddingX,
432 width * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700433 int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
Jon Mirandac1b08c52016-11-15 14:37:56 -0800434 - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx
435 - hotseatBarBottomPaddingPx);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700436 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700437 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700438 } else {
439 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700440 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700441 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700442 desiredWorkspaceLeftRightMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -0700443 paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -0700444 }
445 }
446 return padding;
447 }
448
Winsonfadbe8f2016-07-22 16:35:37 -0700449 /**
450 * @return the bounds for which the open folders should be contained within
451 */
452 public Rect getAbsoluteOpenFolderBounds() {
453 if (isVerticalBarLayout()) {
454 // Folders should only appear right of the drop target bar and left of the hotseat
455 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
456 mInsets.top,
457 mInsets.left + availableWidthPx - hotseatBarHeightPx - edgeMarginPx,
458 mInsets.top + availableHeightPx);
459 } else {
460 // Folders should only appear below the drop target bar and above the hotseat
461 return new Rect(mInsets.left,
462 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
463 mInsets.left + availableWidthPx,
464 mInsets.top + availableHeightPx - hotseatBarHeightPx - pageIndicatorHeightPx -
465 edgeMarginPx);
466 }
467 }
468
Sunny Goyal47328fd2016-05-25 18:56:41 -0700469 private int getWorkspacePageSpacing() {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700470 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700471 // In landscape mode the page spacing is set to the default.
472 return defaultPageSpacingPx;
473 } else {
474 // In portrait, we want the pages spaced such that there is no
475 // overhang of the previous / next page into the current page viewport.
476 // We assume symmetrical padding in portrait mode.
Winson11439c52016-07-20 14:25:07 -0700477 return Math.max(defaultPageSpacingPx, getWorkspacePadding(null).left + 1);
Winson Chungb3800242013-10-24 11:01:54 -0700478 }
479 }
480
Winson Chung4f7eb402015-07-10 18:00:48 -0700481 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700482 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
483 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
484 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700485 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700486 }
487
Adam Cohen2e6da152015-05-06 11:42:25 -0700488 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700489 return width / countX;
490 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700491 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700492 return height / countY;
493 }
494
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700495 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700496 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
497 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
498 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700499 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700500 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700501 return isLandscape && transposeLayoutWithOrientation;
502 }
503
504 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700505 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700506 }
507
Sunny Goyalc6205602015-05-21 20:46:33 -0700508 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800509 int visibleChildren = 0;
510 for (int i = 0; i < parent.getChildCount(); i++) {
511 if (parent.getChildAt(i).getVisibility() != View.GONE) {
512 visibleChildren++;
513 }
514 }
515 return visibleChildren;
516 }
517
Winson1f064272016-07-18 17:18:02 -0700518 public void layout(Launcher launcher, boolean notifyListeners) {
Winson Chungb3800242013-10-24 11:01:54 -0700519 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700520 boolean hasVerticalBarLayout = isVerticalBarLayout();
521
522 // Layout the search bar space
Sunny Goyal47328fd2016-05-25 18:56:41 -0700523 Point searchBarBounds = getSearchBarDimensForWidgetOpts();
524 View searchBar = launcher.getDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700525 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700526 lp.width = searchBarBounds.x;
527 lp.height = searchBarBounds.y;
Winson1f064272016-07-18 17:18:02 -0700528 lp.topMargin = mInsets.top + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700529 searchBar.setLayoutParams(lp);
530
Winson Chungb3800242013-10-24 11:01:54 -0700531 // Layout the workspace
532 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
Winson1f064272016-07-18 17:18:02 -0700533 Rect workspacePadding = getWorkspacePadding(null);
534 workspace.setPadding(workspacePadding.left, workspacePadding.top, workspacePadding.right,
535 workspacePadding.bottom);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700536 workspace.setPageSpacing(getWorkspacePageSpacing());
Winson Chungb3800242013-10-24 11:01:54 -0700537
Louis Beginb07a3552017-02-27 14:33:11 +0800538 // Only display when enabled
539 if (FeatureFlags.QSB_ON_FIRST_SCREEN) {
540 View qsbContainer = launcher.getQsbContainer();
541 lp = (FrameLayout.LayoutParams) qsbContainer.getLayoutParams();
542 lp.topMargin = mInsets.top + workspacePadding.top;
543 qsbContainer.setLayoutParams(lp);
544 }
Sunny Goyal6178f132016-07-11 17:30:03 -0700545
Winson Chungb3800242013-10-24 11:01:54 -0700546 // Layout the hotseat
Winson6231ede2016-07-20 17:21:55 -0700547 Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat);
Winson Chungb3800242013-10-24 11:01:54 -0700548 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700549 // We want the edges of the hotseat to line up with the edges of the workspace, but the
550 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
551 // this, we pad the left and right of the hotseat with half of the difference of a workspace
552 // cell vs a hotseat cell.
553 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
554 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
555 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700556 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700557 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
558 // screen regardless of RTL
559 lp.gravity = Gravity.RIGHT;
Winson1f064272016-07-18 17:18:02 -0700560 lp.width = hotseatBarHeightPx + mInsets.left + mInsets.right;
Winson Chungb3800242013-10-24 11:01:54 -0700561 lp.height = LayoutParams.MATCH_PARENT;
Winson6231ede2016-07-20 17:21:55 -0700562 hotseat.getLayout().setPadding(mInsets.left, mInsets.top, mInsets.right,
563 workspacePadding.bottom);
Sunny Goyalc6205602015-05-21 20:46:33 -0700564 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800565 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700566 lp.gravity = Gravity.BOTTOM;
567 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700568 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700569 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
570 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
Jon Mirandac1b08c52016-11-15 14:37:56 -0800571 hotseatBarBottomPaddingPx + mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700572 } else {
573 // For phones, layout the hotseat without any bottom margin
574 // to ensure that we have space for the folders
575 lp.gravity = Gravity.BOTTOM;
576 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700577 lp.height = hotseatBarHeightPx + mInsets.bottom;
Winson6231ede2016-07-20 17:21:55 -0700578 hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left,
579 hotseatBarTopPaddingPx, hotseatAdjustment + workspacePadding.right,
Jon Mirandac1b08c52016-11-15 14:37:56 -0800580 hotseatBarBottomPaddingPx + mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700581 }
582 hotseat.setLayoutParams(lp);
583
584 // Layout the page indicators
585 View pageIndicator = launcher.findViewById(R.id.page_indicator);
586 if (pageIndicator != null) {
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700587 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700588 if (isVerticalBarLayout()) {
589 if (mInsets.left > 0) {
590 lp.leftMargin = mInsets.left + pageIndicatorLandGutterLeftNavBarPx -
591 lp.width - pageIndicatorLandWorkspaceOffsetPx;
592 } else if (mInsets.right > 0) {
593 lp.leftMargin = pageIndicatorLandGutterRightNavBarPx - lp.width -
594 pageIndicatorLandWorkspaceOffsetPx;
595 }
596 lp.bottomMargin = workspacePadding.bottom;
597 } else {
Winson Chungb3800242013-10-24 11:01:54 -0700598 // Put the page indicators above the hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700599 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Winson1f064272016-07-18 17:18:02 -0700600 lp.height = pageIndicatorHeightPx;
601 lp.bottomMargin = hotseatBarHeightPx + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700602 }
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700603 pageIndicator.setLayoutParams(lp);
Winson Chungb3800242013-10-24 11:01:54 -0700604 }
605
Winson Chungb3800242013-10-24 11:01:54 -0700606 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800607 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700608 if (overviewMode != null) {
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700609 int visibleChildCount = getVisibleChildCount(overviewMode);
610 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
Jon Mirandadf3bc522016-11-01 16:55:56 -0700611 int maxWidth = totalItemWidth + (visibleChildCount - 1) * overviewModeBarSpacerWidthPx;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700612
Jon Mirandadf3bc522016-11-01 16:55:56 -0700613 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700614 lp.width = Math.min(availableWidthPx, maxWidth);
Jon Mirandadf3bc522016-11-01 16:55:56 -0700615 lp.height = getOverviewModeButtonBarHeight() + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700616 overviewMode.setLayoutParams(lp);
Winson1f064272016-07-18 17:18:02 -0700617 }
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700618
Winson1f064272016-07-18 17:18:02 -0700619 if (notifyListeners) {
620 for (int i = mListeners.size() - 1; i >= 0; i--) {
621 mListeners.get(i).onLauncherLayoutChanged();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700622 }
Winson Chungb3800242013-10-24 11:01:54 -0700623 }
624 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700625
626 private int getCurrentWidth() {
627 return isLandscape
628 ? Math.max(widthPx, heightPx)
629 : Math.min(widthPx, heightPx);
630 }
631
632 private int getCurrentHeight() {
633 return isLandscape
634 ? Math.min(widthPx, heightPx)
635 : Math.max(widthPx, heightPx);
636 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700637
Sunny Goyalc13403c2016-11-18 23:44:48 -0800638 public int getCellHeight(@ContainerType int containerType) {
639 switch (containerType) {
640 case CellLayout.WORKSPACE:
641 return cellHeightPx;
642 case CellLayout.FOLDER:
643 return folderCellHeightPx;
644 case CellLayout.HOTSEAT:
645 return hotseatCellHeightPx;
646 default:
647 // ??
648 return 0;
649 }
650 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700651
Winson1f064272016-07-18 17:18:02 -0700652 /**
653 * @return the left/right paddings for all containers.
654 */
Jon Mirandac3e4e222016-11-02 16:56:52 -0700655 public final int[] getContainerPadding() {
Winson1f064272016-07-18 17:18:02 -0700656 // No paddings for portrait phone
657 if (isPhone && !isVerticalBarLayout()) {
658 return new int[] {0, 0};
Sunny Goyal13178ac2016-04-04 16:35:22 -0700659 }
Winson1f064272016-07-18 17:18:02 -0700660
Hyunyoung Song9d96fd52016-08-23 11:06:24 -0700661 // In landscape, we match the width of the workspace
662 int padding = (pageIndicatorLandGutterRightNavBarPx +
663 hotseatBarHeightPx + hotseatLandGutterPx + mInsets.left) / 2;
Winson1f064272016-07-18 17:18:02 -0700664 return new int[]{ padding, padding };
Sunny Goyal13178ac2016-04-04 16:35:22 -0700665 }
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700666
Jon Miranda51f037d2016-11-07 08:37:20 -0800667 public boolean shouldIgnoreLongPressToOverview(float touchX) {
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700668 boolean inMultiWindowMode = this != inv.landscapeProfile && this != inv.portraitProfile;
Jon Miranda51f037d2016-11-07 08:37:20 -0800669 boolean touchedLhsEdge = mInsets.left == 0 && touchX < edgeMarginPx;
670 boolean touchedRhsEdge = mInsets.right == 0 && touchX > (widthPx - edgeMarginPx);
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700671 return !inMultiWindowMode && (touchedLhsEdge || touchedRhsEdge);
672 }
Winson Chungb3800242013-10-24 11:01:54 -0700673}