blob: ac86f0b74a725f7cedaadfa2471a8c2b6f8237a6 [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;
23import android.graphics.Paint;
24import android.graphics.Paint.FontMetrics;
25import android.graphics.Point;
Winson Chungb3800242013-10-24 11:01:54 -070026import android.graphics.Rect;
27import android.util.DisplayMetrics;
Winson Chungb3800242013-10-24 11:01:54 -070028import android.view.Gravity;
Winson Chungb3800242013-10-24 11:01:54 -070029import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080030import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.view.ViewGroup.LayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070032import android.widget.FrameLayout;
33
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 Goyalc6205602015-05-21 20:46:33 -070078 private float dragViewScale;
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;
Sunny Goyalc6205602015-05-21 20:46:33 -0700101 public int folderCellWidthPx;
102 public int folderCellHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700103
Sunny Goyalc6205602015-05-21 20:46:33 -0700104 // Hotseat
105 public int hotseatCellWidthPx;
106 public int hotseatCellHeightPx;
107 public int hotseatIconSizePx;
Sunny Goyal47328fd2016-05-25 18:56:41 -0700108 private int hotseatBarHeightPx;
Winson1f064272016-07-18 17:18:02 -0700109 private int hotseatBarTopPaddingPx;
110 private int hotseatLandGutterPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700111
Sunny Goyalc6205602015-05-21 20:46:33 -0700112 // All apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700113 public int allAppsNumCols;
114 public int allAppsNumPredictiveCols;
Sunny Goyalc6205602015-05-21 20:46:33 -0700115 public int allAppsButtonVisualSize;
Winson1f064272016-07-18 17:18:02 -0700116 public int allAppsIconSizePx;
117 public int allAppsIconDrawablePaddingPx;
118 public float allAppsIconTextSizePx;
119
120 // Containers
121 private final int containerLeftPaddingPx;
122 private final int containerRightPaddingPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800123
Sunny Goyal47328fd2016-05-25 18:56:41 -0700124 // Drop Target
125 public int dropTargetBarSizePx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700126
Winson1f064272016-07-18 17:18:02 -0700127 // Insets
128 private Rect mInsets = new Rect();
129
130 // Listeners
131 private ArrayList<LauncherLayoutChangeListener> mListeners = new ArrayList<>();
132
Sunny Goyalc6205602015-05-21 20:46:33 -0700133 public DeviceProfile(Context context, InvariantDeviceProfile inv,
134 Point minSize, Point maxSize,
135 int width, int height, boolean isLandscape) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700136
Adam Cohen2e6da152015-05-06 11:42:25 -0700137 this.inv = inv;
Sunny Goyalc6205602015-05-21 20:46:33 -0700138 this.isLandscape = isLandscape;
Winson Chungb3800242013-10-24 11:01:54 -0700139
Adam Cohen2e6da152015-05-06 11:42:25 -0700140 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700141 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700142
Sunny Goyalc6205602015-05-21 20:46:33 -0700143 // Constants from resources
144 isTablet = res.getBoolean(R.bool.is_tablet);
145 isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
146 isPhone = !isTablet && !isLargeTablet;
147
148 // Some more constants
Winson Chungb3800242013-10-24 11:01:54 -0700149 transposeLayoutWithOrientation =
150 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700151
152 ComponentName cn = new ComponentName(context.getPackageName(),
153 this.getClass().getName());
154 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
155 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Winson1f064272016-07-18 17:18:02 -0700156 desiredWorkspaceLeftRightMarginPx = edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700157 pageIndicatorHeightPx =
158 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
Winson1f064272016-07-18 17:18:02 -0700159 pageIndicatorLandGutterLeftNavBarPx = res.getDimensionPixelSize(
160 R.dimen.dynamic_grid_page_indicator_gutter_width_left_nav_bar);
161 pageIndicatorLandWorkspaceOffsetPx =
162 res.getDimensionPixelSize(R.dimen.all_apps_caret_workspace_offset);
163 pageIndicatorLandGutterRightNavBarPx = res.getDimensionPixelSize(
164 R.dimen.dynamic_grid_page_indicator_gutter_width_right_nav_bar);
Winson Chungb3800242013-10-24 11:01:54 -0700165 defaultPageSpacingPx =
166 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700167 topWorkspacePadding =
168 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700169 overviewModeMinIconZoneHeightPx =
170 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
171 overviewModeMaxIconZoneHeightPx =
172 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800173 overviewModeBarItemWidthPx =
174 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
175 overviewModeBarSpacerWidthPx =
176 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700177 overviewModeIconZoneRatio =
178 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
Winson Chungb3800242013-10-24 11:01:54 -0700179 iconDrawablePaddingOriginalPx =
180 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700181 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
182 workspaceSpringLoadedBottomSpace =
183 res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
Winson1f064272016-07-18 17:18:02 -0700184 hotseatBarHeightPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height);
185 hotseatBarTopPaddingPx =
186 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
187 hotseatLandGutterPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_gutter_width);
188 containerLeftPaddingPx =
189 res.getDimensionPixelSize(R.dimen.dynamic_grid_container_land_left_padding);
190 containerRightPaddingPx =
191 res.getDimensionPixelSize(R.dimen.dynamic_grid_container_land_right_padding);
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
Winson1f064272016-07-18 17:18:02 -0700209 public void addLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
210 if (!mListeners.contains(listener)) {
211 mListeners.add(listener);
212 }
213 }
214
215 public void removeLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) {
216 if (mListeners.contains(listener)) {
217 mListeners.remove(listener);
218 }
219 }
220
Adam Cohen63f1ec02014-08-12 09:23:13 -0700221 /**
222 * Determine the exact visual footprint of the all apps button, taking into account scaling
223 * and internal padding of the drawable.
224 */
225 private void computeAllAppsButtonSize(Context context) {
226 Resources res = context.getResources();
227 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700228 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)) - context.getResources()
229 .getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
Winson Chungb3800242013-10-24 11:01:54 -0700230 }
231
Sunny Goyalc6205602015-05-21 20:46:33 -0700232 private void updateAvailableDimensions(DisplayMetrics dm, Resources res) {
Winson Chungb3800242013-10-24 11:01:54 -0700233 // Check to see if the icons fit in the new available height. If not, then we need to
234 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700235 float scale = 1f;
236 int drawablePadding = iconDrawablePaddingOriginalPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700237 updateIconSize(1f, drawablePadding, res, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700238 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800239
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700240 int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y);
Winson Chungb3800242013-10-24 11:01:54 -0700241 if (usedHeight > maxHeight) {
242 scale = maxHeight / usedHeight;
243 drawablePadding = 0;
244 }
Sunny Goyalc6205602015-05-21 20:46:33 -0700245 updateIconSize(scale, drawablePadding, res, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700246 }
247
Winson Chung93f98ea2015-03-10 16:28:47 -0700248 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700249 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700250 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
Sunny Goyala50a4192015-12-11 09:50:49 -0800251 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700252 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700253 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson1f064272016-07-18 17:18:02 -0700254 allAppsIconSizePx = iconSizePx;
255 allAppsIconDrawablePaddingPx = iconDrawablePaddingPx;
256 allAppsIconTextSizePx = iconTextSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700257
Winson Chungb3800242013-10-24 11:01:54 -0700258 // Calculate the actual text height
259 Paint textPaint = new Paint();
260 textPaint.setTextSize(iconTextSizePx);
261 FontMetrics fm = textPaint.getFontMetrics();
262 cellWidthPx = iconSizePx;
263 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Tony Wickhame0c33232016-02-08 11:37:04 -0800264 final float scaleDps = !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND ? 0f
265 : res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800266 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700267
268 // Hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700269 hotseatCellWidthPx = iconSizePx;
270 hotseatCellHeightPx = iconSizePx;
271
Sunny Goyal47328fd2016-05-25 18:56:41 -0700272 if (!isVerticalBarLayout()) {
273 int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx
274 - pageIndicatorHeightPx - topWorkspacePadding;
275 float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace;
276 workspaceSpringLoadShrinkFactor = Math.min(
277 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f,
278 1 - (minRequiredHeight / expectedWorkspaceHeight));
279 } else {
280 workspaceSpringLoadShrinkFactor =
281 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
282 }
283
Winson Chungb3800242013-10-24 11:01:54 -0700284 // Folder
Tony Wickham6159c242015-11-13 18:07:18 +0000285 int folderCellPadding = isTablet || isLandscape ? 6 * edgeMarginPx : 3 * edgeMarginPx;
286 // Don't let the folder get too close to the edges of the screen.
287 folderCellWidthPx = Math.min(cellWidthPx + folderCellPadding,
Tony Wickhama2950482015-10-12 15:57:23 -0700288 (availableWidthPx - 4 * edgeMarginPx) / inv.numFolderColumns);
Winson Chungb3800242013-10-24 11:01:54 -0700289 folderCellHeightPx = cellHeightPx + edgeMarginPx;
290 folderBackgroundOffset = -edgeMarginPx;
291 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Adam Cohenefca0272016-02-24 19:19:06 -0800292 folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Winson Chung0f785722015-04-08 10:27:49 -0700293 }
294
Winson1f064272016-07-18 17:18:02 -0700295 public void updateInsets(Rect insets) {
296 mInsets.set(insets);
297 }
298
Winson Chungef7f8742015-06-04 17:18:17 -0700299 /**
300 * @param recyclerViewWidth the available width of the AllAppsRecyclerView
301 */
Winson1f064272016-07-18 17:18:02 -0700302 public void updateAppsViewNumCols() {
303 allAppsNumCols = allAppsNumPredictiveCols = inv.numColumns;
Winson Chungb3800242013-10-24 11:01:54 -0700304 }
305
Tony Wickhama3c74d12015-10-23 11:43:47 -0700306 /** Returns the width and height of the search bar, ignoring any padding. */
Sunny Goyal47328fd2016-05-25 18:56:41 -0700307 public Point getSearchBarDimensForWidgetOpts() {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700308 if (isVerticalBarLayout()) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700309 return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx);
Winson Chungb3800242013-10-24 11:01:54 -0700310 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700311 int gap;
Sunny Goyalc6205602015-05-21 20:46:33 -0700312 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700313 // Pad the left and right of the workspace to ensure consistent spacing
314 // between all icons
Winson Chung4f3bfad2015-07-13 11:14:30 -0700315 int width = getCurrentWidth();
Winson Chungb3800242013-10-24 11:01:54 -0700316 // XXX: If the icon size changes across orientations, we will have to take
317 // that into account here too.
Sunny Goyal47328fd2016-05-25 18:56:41 -0700318 gap = ((width - 2 * edgeMarginPx
319 - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)))
320 + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700321 } else {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700322 gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right;
Winson Chungb3800242013-10-24 11:01:54 -0700323 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700324 return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700325 }
Winson Chungb3800242013-10-24 11:01:54 -0700326 }
327
Sunny Goyal756cd262015-08-20 12:33:21 -0700328 public Point getCellSize() {
329 Point result = new Point();
330 // Since we are only concerned with the overall padding, layout direction does
331 // not matter.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700332 Point padding = getTotalWorkspacePadding();
333 result.x = calculateCellWidth(availableWidthPx - padding.x, inv.numColumns);
334 result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows);
Sunny Goyal756cd262015-08-20 12:33:21 -0700335 return result;
336 }
337
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700338 public Point getTotalWorkspacePadding() {
Sunny Goyal6178f132016-07-11 17:30:03 -0700339 Rect padding = getWorkspacePadding(null);
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700340 return new Point(padding.left + padding.right, padding.top + padding.bottom);
341 }
342
343 /**
344 * Returns the workspace padding in the specified orientation.
345 * Note that it assumes that while in verticalBarLayout, the nav bar is on the right, as such
346 * this value is not reliable.
347 * Use {@link #getTotalWorkspacePadding()} instead.
348 */
Sunny Goyal6178f132016-07-11 17:30:03 -0700349 public Rect getWorkspacePadding(Rect recycle) {
350 Rect padding = recycle == null ? new Rect() : recycle;
Tony Wickham3a3517f2015-10-06 11:27:04 -0700351 if (isVerticalBarLayout()) {
Winson1f064272016-07-18 17:18:02 -0700352 if (mInsets.left > 0) {
353 padding.set(mInsets.left + pageIndicatorLandGutterLeftNavBarPx, 0,
354 hotseatBarHeightPx + hotseatLandGutterPx - mInsets.left, 2 * edgeMarginPx);
355 } else {
356 padding.set(pageIndicatorLandGutterRightNavBarPx, 0,
357 hotseatBarHeightPx + hotseatLandGutterPx, 2 * edgeMarginPx);
358 }
Winson Chungb3800242013-10-24 11:01:54 -0700359 } else {
Tony Wickhama3c74d12015-10-23 11:43:47 -0700360 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700361 if (isTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700362 // Pad the left and right of the workspace to ensure consistent spacing
363 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800364 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chung4f3bfad2015-07-13 11:14:30 -0700365 int width = getCurrentWidth();
366 int height = getCurrentHeight();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700367 // The amount of screen space available for left/right padding.
368 int availablePaddingX = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
369 ((inv.numColumns - 1) * gapScale * cellWidthPx)));
370 availablePaddingX = (int) Math.min(availablePaddingX,
371 width * MAX_HORIZONTAL_PADDING_PERCENT);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700372 int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700373 - (int) (2 * inv.numRows * cellHeightPx));
Sunny Goyal47328fd2016-05-25 18:56:41 -0700374 padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700375 availablePaddingX / 2, paddingBottom + availablePaddingY / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700376 } else {
377 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson1f064272016-07-18 17:18:02 -0700378 padding.set(desiredWorkspaceLeftRightMarginPx,
Sunny Goyal47328fd2016-05-25 18:56:41 -0700379 topWorkspacePadding,
Winson1f064272016-07-18 17:18:02 -0700380 desiredWorkspaceLeftRightMarginPx,
381 hotseatBarHeightPx + pageIndicatorHeightPx);
Winson Chungb3800242013-10-24 11:01:54 -0700382 }
383 }
384 return padding;
385 }
386
Sunny Goyal47328fd2016-05-25 18:56:41 -0700387 private int getWorkspacePageSpacing() {
Tony Wickham3a3517f2015-10-06 11:27:04 -0700388 if (isVerticalBarLayout() || isLargeTablet) {
Winson Chungb3800242013-10-24 11:01:54 -0700389 // In landscape mode the page spacing is set to the default.
390 return defaultPageSpacingPx;
391 } else {
392 // In portrait, we want the pages spaced such that there is no
393 // overhang of the previous / next page into the current page viewport.
394 // We assume symmetrical padding in portrait mode.
Sunny Goyal6178f132016-07-11 17:30:03 -0700395 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(null).left);
Winson Chungb3800242013-10-24 11:01:54 -0700396 }
397 }
398
Winson Chung4f7eb402015-07-10 18:00:48 -0700399 int getOverviewModeButtonBarHeight() {
Winson Chungb3800242013-10-24 11:01:54 -0700400 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
401 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
402 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
Winson Chung4f7eb402015-07-10 18:00:48 -0700403 return zoneHeight;
Winson Chungb3800242013-10-24 11:01:54 -0700404 }
405
Adam Cohen2e6da152015-05-06 11:42:25 -0700406 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700407 return width / countX;
408 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700409 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700410 return height / countY;
411 }
412
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700413 /**
Tony Wickham55616cd2015-09-23 14:55:17 -0700414 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
415 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
416 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700417 */
Tony Wickhamab946a12015-09-16 15:38:16 -0700418 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -0700419 return isLandscape && transposeLayoutWithOrientation;
420 }
421
422 boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyalc6205602015-05-21 20:46:33 -0700423 return isVerticalBarLayout() || isLargeTablet;
Winson Chungb3800242013-10-24 11:01:54 -0700424 }
425
Sunny Goyalc6205602015-05-21 20:46:33 -0700426 private int getVisibleChildCount(ViewGroup parent) {
Jorim Jaggid017f882014-01-14 17:08:48 -0800427 int visibleChildren = 0;
428 for (int i = 0; i < parent.getChildCount(); i++) {
429 if (parent.getChildAt(i).getVisibility() != View.GONE) {
430 visibleChildren++;
431 }
432 }
433 return visibleChildren;
434 }
435
Winson1f064272016-07-18 17:18:02 -0700436 public void layout(Launcher launcher, boolean notifyListeners) {
Winson Chungb3800242013-10-24 11:01:54 -0700437 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700438 boolean hasVerticalBarLayout = isVerticalBarLayout();
Sunny Goyalc6205602015-05-21 20:46:33 -0700439 final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources());
Winson Chungb3800242013-10-24 11:01:54 -0700440
441 // Layout the search bar space
Sunny Goyal47328fd2016-05-25 18:56:41 -0700442 Point searchBarBounds = getSearchBarDimensForWidgetOpts();
443 View searchBar = launcher.getDropTargetBar();
Sunny Goyalf37a2142016-03-24 17:28:25 -0700444 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700445 lp.width = searchBarBounds.x;
446 lp.height = searchBarBounds.y;
Winson1f064272016-07-18 17:18:02 -0700447 lp.topMargin = mInsets.top + edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -0700448 searchBar.setLayoutParams(lp);
449
Winson Chungb3800242013-10-24 11:01:54 -0700450 // Layout the workspace
451 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
Winson1f064272016-07-18 17:18:02 -0700452 Rect workspacePadding = getWorkspacePadding(null);
453 workspace.setPadding(workspacePadding.left, workspacePadding.top, workspacePadding.right,
454 workspacePadding.bottom);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700455 workspace.setPageSpacing(getWorkspacePageSpacing());
Winson Chungb3800242013-10-24 11:01:54 -0700456
Sunny Goyal6178f132016-07-11 17:30:03 -0700457 View qsbContainer = launcher.getQsbContainer();
458 lp = (FrameLayout.LayoutParams) qsbContainer.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700459 lp.topMargin = mInsets.top + workspacePadding.top;
Sunny Goyal6178f132016-07-11 17:30:03 -0700460 qsbContainer.setLayoutParams(lp);
461
Winson Chungb3800242013-10-24 11:01:54 -0700462 // Layout the hotseat
463 View hotseat = launcher.findViewById(R.id.hotseat);
464 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
Tony Wickhamd6b40372015-09-23 18:37:57 -0700465 // We want the edges of the hotseat to line up with the edges of the workspace, but the
466 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
467 // this, we pad the left and right of the hotseat with half of the difference of a workspace
468 // cell vs a hotseat cell.
469 float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns;
470 float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons;
471 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700472 if (hasVerticalBarLayout) {
Winson Chungf8c6f882015-06-03 11:27:26 -0700473 // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the
474 // screen regardless of RTL
475 lp.gravity = Gravity.RIGHT;
Winson1f064272016-07-18 17:18:02 -0700476 lp.width = hotseatBarHeightPx + mInsets.left + mInsets.right;
Winson Chungb3800242013-10-24 11:01:54 -0700477 lp.height = LayoutParams.MATCH_PARENT;
478 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Winson1f064272016-07-18 17:18:02 -0700479 hotseat.setPadding(mInsets.left, 0, mInsets.right, 0);
Sunny Goyalc6205602015-05-21 20:46:33 -0700480 } else if (isTablet) {
Winson Chung59a488a2013-12-10 12:32:14 -0800481 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700482 lp.gravity = Gravity.BOTTOM;
483 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700484 lp.height = hotseatBarHeightPx + mInsets.bottom;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700485 hotseat.findViewById(R.id.layout).setPadding(
Winson1f064272016-07-18 17:18:02 -0700486 hotseatAdjustment + workspacePadding.left, 0,
487 hotseatAdjustment + workspacePadding.right, 2 * edgeMarginPx);
488 hotseat.setPadding(0, hotseatBarTopPaddingPx, 0, mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700489 } else {
490 // For phones, layout the hotseat without any bottom margin
491 // to ensure that we have space for the folders
492 lp.gravity = Gravity.BOTTOM;
493 lp.width = LayoutParams.MATCH_PARENT;
Winson1f064272016-07-18 17:18:02 -0700494 lp.height = hotseatBarHeightPx + mInsets.bottom;
Tony Wickhamd6b40372015-09-23 18:37:57 -0700495 hotseat.findViewById(R.id.layout).setPadding(
Winson1f064272016-07-18 17:18:02 -0700496 hotseatAdjustment + workspacePadding.left, 0,
497 hotseatAdjustment + workspacePadding.right, 0);
498 hotseat.setPadding(0, hotseatBarTopPaddingPx, 0, mInsets.bottom);
Winson Chungb3800242013-10-24 11:01:54 -0700499 }
500 hotseat.setLayoutParams(lp);
501
502 // Layout the page indicators
503 View pageIndicator = launcher.findViewById(R.id.page_indicator);
504 if (pageIndicator != null) {
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700505 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700506 if (isVerticalBarLayout()) {
507 if (mInsets.left > 0) {
508 lp.leftMargin = mInsets.left + pageIndicatorLandGutterLeftNavBarPx -
509 lp.width - pageIndicatorLandWorkspaceOffsetPx;
510 } else if (mInsets.right > 0) {
511 lp.leftMargin = pageIndicatorLandGutterRightNavBarPx - lp.width -
512 pageIndicatorLandWorkspaceOffsetPx;
513 }
514 lp.bottomMargin = workspacePadding.bottom;
515 } else {
Winson Chungb3800242013-10-24 11:01:54 -0700516 // Put the page indicators above the hotseat
Winson Chungb3800242013-10-24 11:01:54 -0700517 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
518 lp.width = LayoutParams.WRAP_CONTENT;
Winson1f064272016-07-18 17:18:02 -0700519 lp.height = pageIndicatorHeightPx;
520 lp.bottomMargin = hotseatBarHeightPx + mInsets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -0700521 }
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700522 pageIndicator.setLayoutParams(lp);
Winson Chungb3800242013-10-24 11:01:54 -0700523 }
524
Winson Chungb3800242013-10-24 11:01:54 -0700525 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800526 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700527 if (overviewMode != null) {
Winson Chungb3800242013-10-24 11:01:54 -0700528 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
Winson1f064272016-07-18 17:18:02 -0700529 lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700530
531 int visibleChildCount = getVisibleChildCount(overviewMode);
532 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
533 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
534
535 lp.width = Math.min(availableWidthPx, maxWidth);
Tony Wickham34d2c912015-09-11 09:27:58 -0700536 lp.height = getOverviewModeButtonBarHeight();
Winson1f064272016-07-18 17:18:02 -0700537 // Center the overview buttons on the workspace page
538 lp.leftMargin = workspacePadding.left + (availableWidthPx -
539 workspacePadding.left - workspacePadding.right - lp.width) / 2;
Winson Chungb3800242013-10-24 11:01:54 -0700540 overviewMode.setLayoutParams(lp);
Winson1f064272016-07-18 17:18:02 -0700541 }
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700542
Winson1f064272016-07-18 17:18:02 -0700543 if (notifyListeners) {
544 for (int i = mListeners.size() - 1; i >= 0; i--) {
545 mListeners.get(i).onLauncherLayoutChanged();
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700546 }
Winson Chungb3800242013-10-24 11:01:54 -0700547 }
548 }
Winson Chung4f3bfad2015-07-13 11:14:30 -0700549
550 private int getCurrentWidth() {
551 return isLandscape
552 ? Math.max(widthPx, heightPx)
553 : Math.min(widthPx, heightPx);
554 }
555
556 private int getCurrentHeight() {
557 return isLandscape
558 ? Math.min(widthPx, heightPx)
559 : Math.max(widthPx, heightPx);
560 }
Sunny Goyal13178ac2016-04-04 16:35:22 -0700561
562
Winson1f064272016-07-18 17:18:02 -0700563 /**
564 * @return the left/right paddings for all containers.
565 */
566 public final int[] getContainerPadding(Context context) {
Sunny Goyal13178ac2016-04-04 16:35:22 -0700567 Resources res = context.getResources();
568
Winson1f064272016-07-18 17:18:02 -0700569 // No paddings for portrait phone
570 if (isPhone && !isVerticalBarLayout()) {
571 return new int[] {0, 0};
Sunny Goyal13178ac2016-04-04 16:35:22 -0700572 }
Winson1f064272016-07-18 17:18:02 -0700573
574 // In landscape, we just match the vertical display width
575 int containerWidth = heightPx;
576 int padding = (availableWidthPx - containerWidth) / 2;
577 return new int[]{ padding, padding };
Sunny Goyal13178ac2016-04-04 16:35:22 -0700578 }
Winson Chungb3800242013-10-24 11:01:54 -0700579}