blob: 8ab58b9c02407217ae5ae227e96d24dbe2543c65 [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;
22import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.graphics.Paint;
25import android.graphics.Paint.FontMetrics;
26import android.graphics.Point;
27import android.graphics.PointF;
28import android.graphics.Rect;
29import android.util.DisplayMetrics;
Adam Cohen2e6da152015-05-06 11:42:25 -070030import android.util.TypedValue;
Winson Chungb3800242013-10-24 11:01:54 -070031import android.view.Display;
32import android.view.Gravity;
33import android.view.Surface;
34import android.view.View;
Jorim Jaggid017f882014-01-14 17:08:48 -080035import android.view.ViewGroup;
Winson Chungb3800242013-10-24 11:01:54 -070036import android.view.ViewGroup.LayoutParams;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -070037import android.view.ViewGroup.MarginLayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070038import android.view.WindowManager;
39import android.widget.FrameLayout;
Adam Cohen24ce0b32014-01-14 16:18:14 -080040import android.widget.LinearLayout;
Winson Chungb3800242013-10-24 11:01:54 -070041
Adam Cohen091440a2015-03-18 14:16:05 -070042import com.android.launcher3.util.Thunk;
43
Winson Chungb3800242013-10-24 11:01:54 -070044import java.util.ArrayList;
45import java.util.Collections;
46import java.util.Comparator;
47
Winson Chungb3800242013-10-24 11:01:54 -070048public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070049
Winson Chungb3800242013-10-24 11:01:54 -070050 public static interface DeviceProfileCallbacks {
51 public void onAvailableSizeChanged(DeviceProfile grid);
52 }
53
Adam Cohen2e6da152015-05-06 11:42:25 -070054 public final InvariantDeviceProfile inv;
Winson Chungb3800242013-10-24 11:01:54 -070055 private int iconDrawablePaddingOriginalPx;
Winson Chungbe876472014-05-14 14:15:20 -070056
Winson Chungb3800242013-10-24 11:01:54 -070057 boolean isLandscape;
Hyunyoung Song4b69f2c2015-05-11 14:55:07 -070058 public boolean isTablet;
59 public boolean isLargeTablet;
Hyunyoung Songada50982015-04-10 14:35:23 -070060 public boolean isLayoutRtl;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070061
Winson Chungb3800242013-10-24 11:01:54 -070062 boolean transposeLayoutWithOrientation;
Winson Chungb3800242013-10-24 11:01:54 -070063 int desiredWorkspaceLeftRightMarginPx;
Adam Cohen59400422014-03-05 18:07:04 -080064 public int edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -070065 Rect defaultWidgetPadding;
66
67 int widthPx;
68 int heightPx;
Adam Cohen59400422014-03-05 18:07:04 -080069 public int availableWidthPx;
70 public int availableHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070071 int defaultPageSpacingPx;
72
73 int overviewModeMinIconZoneHeightPx;
74 int overviewModeMaxIconZoneHeightPx;
Jorim Jaggid017f882014-01-14 17:08:48 -080075 int overviewModeBarItemWidthPx;
76 int overviewModeBarSpacerWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -070077 float overviewModeIconZoneRatio;
78 float overviewModeScaleFactor;
79
Adam Cohen59400422014-03-05 18:07:04 -080080 public int cellWidthPx;
81 public int cellHeightPx;
82
Winson Chungb3800242013-10-24 11:01:54 -070083 int iconDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -070084 int allAppsCellWidthPx;
85 int allAppsCellHeightPx;
86 int allAppsCellPaddingPx;
87 int folderBackgroundOffset;
88 int folderIconSizePx;
89 int folderCellWidthPx;
90 int folderCellHeightPx;
91 int hotseatCellWidthPx;
92 int hotseatCellHeightPx;
93 int hotseatIconSizePx;
94 int hotseatBarHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070095 int allAppsNumRows;
96 int allAppsNumCols;
Winson Chung93f98ea2015-03-10 16:28:47 -070097 int appsViewNumCols;
Winson Chung208ed752015-05-12 19:05:30 -070098 int appsViewNumPredictiveCols;
Winson Chungb3800242013-10-24 11:01:54 -070099 int searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700100 int searchBarSpaceHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700101 int pageIndicatorHeightPx;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700102 int allAppsButtonVisualSize;
Winson Chungb3800242013-10-24 11:01:54 -0700103
Adam Cohen2e6da152015-05-06 11:42:25 -0700104 int iconSizePx;
105 public int iconTextSizePx;
106 int allAppsIconSizePx;
107 int allAppsIconTextSizePx;
108
Winson Chung59a488a2013-12-10 12:32:14 -0800109 float dragViewScale;
110
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700111 int allAppsShortEdgeCount = -1;
112 int allAppsLongEdgeCount = -1;
113
Winson Chungb3800242013-10-24 11:01:54 -0700114 private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>();
115
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 public DeviceProfile(Context context, InvariantDeviceProfile inv) {
117 // Determine the dynamic grid properties
118 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
119 Display display = wm.getDefaultDisplay();
Winson Chungb3800242013-10-24 11:01:54 -0700120
Adam Cohen2e6da152015-05-06 11:42:25 -0700121 Point realSize = new Point();
122 display.getRealSize(realSize);
123 DisplayMetrics dm = new DisplayMetrics();
124 display.getMetrics(dm);
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700125
Adam Cohen2e6da152015-05-06 11:42:25 -0700126 this.inv = inv;
127 init(context, realSize.x, realSize.y, dm.widthPixels, dm.heightPixels);
Winson Chungb3800242013-10-24 11:01:54 -0700128 }
129
Adam Cohen2e6da152015-05-06 11:42:25 -0700130 private void init(Context context, int wPx, int hPx, int awPx, int ahPx) {
131 Resources res = context.getResources();
Winson Chungb3800242013-10-24 11:01:54 -0700132 DisplayMetrics dm = res.getDisplayMetrics();
Adam Cohen2e6da152015-05-06 11:42:25 -0700133
Winson Chungb3800242013-10-24 11:01:54 -0700134 transposeLayoutWithOrientation =
135 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chungb3800242013-10-24 11:01:54 -0700136
137 ComponentName cn = new ComponentName(context.getPackageName(),
138 this.getClass().getName());
139 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
140 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
141 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
142 pageIndicatorHeightPx =
143 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
144 defaultPageSpacingPx =
145 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
146 allAppsCellPaddingPx =
147 res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_padding);
148 overviewModeMinIconZoneHeightPx =
149 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
150 overviewModeMaxIconZoneHeightPx =
151 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800152 overviewModeBarItemWidthPx =
153 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
154 overviewModeBarSpacerWidthPx =
155 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700156 overviewModeIconZoneRatio =
157 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
158 overviewModeScaleFactor =
159 res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f;
160
Winson Chungb3800242013-10-24 11:01:54 -0700161 iconDrawablePaddingOriginalPx =
162 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chungb3800242013-10-24 11:01:54 -0700163
Adam Cohen2e6da152015-05-06 11:42:25 -0700164 // AllApps uses the original non-scaled icon text size
165 allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
166
167 // AllApps uses the original non-scaled icon size
168 allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
Winson Chungb3800242013-10-24 11:01:54 -0700169
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700170 // If the partner customization apk contains any grid overrides, apply them
171 applyPartnerDeviceProfileOverrides(context, dm);
172
Winson Chungb3800242013-10-24 11:01:54 -0700173 // Calculate the remaining vars
174 updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx);
175 updateAvailableDimensions(context);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700176 computeAllAppsButtonSize(context);
177 }
178
179 /**
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700180 * Apply any Partner customization grid overrides.
181 *
182 * Currently we support: all apps row / column count.
183 */
184 private void applyPartnerDeviceProfileOverrides(Context ctx, DisplayMetrics dm) {
185 Partner p = Partner.get(ctx.getPackageManager());
186 if (p != null) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700187 p.applyDeviceProfileOverrides(this);
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700188 }
189 }
190
191 /**
Adam Cohen63f1ec02014-08-12 09:23:13 -0700192 * Determine the exact visual footprint of the all apps button, taking into account scaling
193 * and internal padding of the drawable.
194 */
195 private void computeAllAppsButtonSize(Context context) {
196 Resources res = context.getResources();
197 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
198 LauncherAppState app = LauncherAppState.getInstance();
199 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding));
Winson Chungb3800242013-10-24 11:01:54 -0700200 }
201
202 void addCallback(DeviceProfileCallbacks cb) {
203 mCallbacks.add(cb);
204 cb.onAvailableSizeChanged(this);
205 }
206 void removeCallback(DeviceProfileCallbacks cb) {
207 mCallbacks.remove(cb);
208 }
209
210 private int getDeviceOrientation(Context context) {
211 WindowManager windowManager = (WindowManager)
212 context.getSystemService(Context.WINDOW_SERVICE);
213 Resources resources = context.getResources();
214 DisplayMetrics dm = resources.getDisplayMetrics();
215 Configuration config = resources.getConfiguration();
216 int rotation = windowManager.getDefaultDisplay().getRotation();
217
218 boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) &&
219 (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180);
220 boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) &&
221 (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
222 if (isLandscape || isRotatedPortrait) {
223 return CellLayout.LANDSCAPE;
224 } else {
225 return CellLayout.PORTRAIT;
226 }
227 }
228
229 private void updateAvailableDimensions(Context context) {
230 WindowManager windowManager = (WindowManager)
231 context.getSystemService(Context.WINDOW_SERVICE);
232 Display display = windowManager.getDefaultDisplay();
233 Resources resources = context.getResources();
234 DisplayMetrics dm = resources.getDisplayMetrics();
235 Configuration config = resources.getConfiguration();
236
237 // There are three possible configurations that the dynamic grid accounts for, portrait,
238 // landscape with the nav bar at the bottom, and landscape with the nav bar at the side.
239 // To prevent waiting for fitSystemWindows(), we make the observation that in landscape,
240 // the height is the smallest height (either with the nav bar at the bottom or to the
241 // side) and otherwise, the height is simply the largest possible height for a portrait
242 // device.
243 Point size = new Point();
244 Point smallestSize = new Point();
245 Point largestSize = new Point();
246 display.getSize(size);
247 display.getCurrentSizeRange(smallestSize, largestSize);
248 availableWidthPx = size.x;
249 if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
250 availableHeightPx = smallestSize.y;
251 } else {
252 availableHeightPx = largestSize.y;
253 }
254
255 // Check to see if the icons fit in the new available height. If not, then we need to
256 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700257 float scale = 1f;
258 int drawablePadding = iconDrawablePaddingOriginalPx;
259 updateIconSize(1f, drawablePadding, resources, dm);
Adam Cohen2e6da152015-05-06 11:42:25 -0700260 float usedHeight = (cellHeightPx * inv.numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800261
262 Rect workspacePadding = getWorkspacePadding();
Winson Chungb3800242013-10-24 11:01:54 -0700263 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
264 if (usedHeight > maxHeight) {
265 scale = maxHeight / usedHeight;
266 drawablePadding = 0;
267 }
268 updateIconSize(scale, drawablePadding, resources, dm);
269
270 // Make the callbacks
271 for (DeviceProfileCallbacks cb : mCallbacks) {
272 cb.onAvailableSizeChanged(this);
273 }
274 }
275
Winson Chung93f98ea2015-03-10 16:28:47 -0700276 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700277 DisplayMetrics dm) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700278 iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
279 iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700280 iconDrawablePaddingPx = drawablePadding;
Adam Cohen2e6da152015-05-06 11:42:25 -0700281 hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);
Winson Chungb3800242013-10-24 11:01:54 -0700282
283 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800284 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700285 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Sunny Goyal594d76d2014-11-06 10:12:54 -0800286 searchBarSpaceHeightPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700287 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Winson Chungb3800242013-10-24 11:01:54 -0700288
289 // Calculate the actual text height
290 Paint textPaint = new Paint();
291 textPaint.setTextSize(iconTextSizePx);
292 FontMetrics fm = textPaint.getFontMetrics();
293 cellWidthPx = iconSizePx;
294 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700295 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800296 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700297
298 // Hotseat
299 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
300 hotseatCellWidthPx = iconSizePx;
301 hotseatCellHeightPx = iconSizePx;
302
303 // Folder
304 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
305 folderCellHeightPx = cellHeightPx + edgeMarginPx;
306 folderBackgroundOffset = -edgeMarginPx;
307 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
308
309 // All Apps
Winson Chungb3800242013-10-24 11:01:54 -0700310 allAppsCellWidthPx = allAppsIconSizePx;
Winson Chung208ed752015-05-12 19:05:30 -0700311 allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + allAppsIconTextSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700312 int maxLongEdgeCellCount =
Winson Chung93f98ea2015-03-10 16:28:47 -0700313 res.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count);
Winson Chungb3800242013-10-24 11:01:54 -0700314 int maxShortEdgeCellCount =
Winson Chung93f98ea2015-03-10 16:28:47 -0700315 res.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count);
Winson Chungb3800242013-10-24 11:01:54 -0700316 int minEdgeCellCount =
Winson Chung93f98ea2015-03-10 16:28:47 -0700317 res.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count);
Winson Chungb3800242013-10-24 11:01:54 -0700318 int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount);
319 int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount);
320
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700321 if (allAppsShortEdgeCount > 0 && allAppsLongEdgeCount > 0) {
322 allAppsNumRows = isLandscape ? allAppsShortEdgeCount : allAppsLongEdgeCount;
323 allAppsNumCols = isLandscape ? allAppsLongEdgeCount : allAppsShortEdgeCount;
324 } else {
325 allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx) /
326 (allAppsCellHeightPx + allAppsCellPaddingPx);
327 allAppsNumRows = Math.max(minEdgeCellCount, Math.min(maxRows, allAppsNumRows));
Winson Chung93f98ea2015-03-10 16:28:47 -0700328 allAppsNumCols = (availableWidthPx) / (allAppsCellWidthPx + allAppsCellPaddingPx);
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700329 allAppsNumCols = Math.max(minEdgeCellCount, Math.min(maxCols, allAppsNumCols));
330 }
Winson Chung93f98ea2015-03-10 16:28:47 -0700331
Winson Chung0f785722015-04-08 10:27:49 -0700332 int appsContainerViewWidthPx = res.getDimensionPixelSize(R.dimen.apps_container_width);
333 updateAppsViewNumCols(res, appsContainerViewWidthPx);
334 }
335
336 public boolean updateAppsViewNumCols(Resources res, int containerWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700337 int appsViewLeftMarginPx =
338 res.getDimensionPixelSize(R.dimen.apps_grid_view_start_margin);
Winson Chung0f785722015-04-08 10:27:49 -0700339 int availableAppsWidthPx = (containerWidth > 0) ? containerWidth : availableWidthPx;
Winson Chung208ed752015-05-12 19:05:30 -0700340 int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chungbd808532015-04-08 11:07:53 -0700341 (allAppsCellWidthPx + 2 * allAppsCellPaddingPx);
Winson Chungc2af7f92015-05-14 13:15:29 -0700342 int numPredictiveAppCols = isPhone() ? 4 : numAppsCols;
Winson Chung208ed752015-05-12 19:05:30 -0700343 if ((numAppsCols != appsViewNumCols) ||
344 (numPredictiveAppCols != appsViewNumPredictiveCols)) {
345 appsViewNumCols = numAppsCols;
346 appsViewNumPredictiveCols = numPredictiveAppCols;
Winson Chung0f785722015-04-08 10:27:49 -0700347 return true;
348 }
349 return false;
Winson Chungb3800242013-10-24 11:01:54 -0700350 }
351
352 void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx,
353 int awPx, int ahPx) {
Winson Chung42b3c062013-12-04 12:09:59 -0800354 Configuration configuration = resources.getConfiguration();
355 isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
Winson Chungb3800242013-10-24 11:01:54 -0700356 isTablet = resources.getBoolean(R.bool.is_tablet);
357 isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
Sunny Goyal70660032015-05-14 00:07:08 -0700358 isLayoutRtl = Utilities.isRtl(resources);
Winson Chungb3800242013-10-24 11:01:54 -0700359 widthPx = wPx;
360 heightPx = hPx;
361 availableWidthPx = awPx;
362 availableHeightPx = ahPx;
363
364 updateAvailableDimensions(context);
365 }
366
Winson Chung69e04ea2013-12-02 14:43:44 -0800367 /** Returns the search bar top offset */
368 int getSearchBarTopOffset() {
369 if (isTablet() && !isVerticalBarLayout()) {
370 return 4 * edgeMarginPx;
371 } else {
372 return 2 * edgeMarginPx;
373 }
374 }
375
Winson Chungb3800242013-10-24 11:01:54 -0700376 /** Returns the search bar bounds in the current orientation */
377 Rect getSearchBarBounds() {
378 return getSearchBarBounds(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
379 }
380 /** Returns the search bar bounds in the specified orientation */
381 Rect getSearchBarBounds(int orientation) {
382 Rect bounds = new Rect();
383 if (orientation == CellLayout.LANDSCAPE &&
384 transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800385 if (isLayoutRtl) {
386 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
387 availableWidthPx, availableHeightPx - edgeMarginPx);
388 } else {
389 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
390 availableHeightPx - edgeMarginPx);
391 }
Winson Chungb3800242013-10-24 11:01:54 -0700392 } else {
393 if (isTablet()) {
394 // Pad the left and right of the workspace to ensure consistent spacing
395 // between all icons
396 int width = (orientation == CellLayout.LANDSCAPE)
397 ? Math.max(widthPx, heightPx)
398 : Math.min(widthPx, heightPx);
399 // XXX: If the icon size changes across orientations, we will have to take
400 // that into account here too.
401 int gap = (int) ((width - 2 * edgeMarginPx -
Adam Cohen2e6da152015-05-06 11:42:25 -0700402 (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800403 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
404 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700405 searchBarSpaceHeightPx);
406 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800407 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
408 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700409 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
410 defaultWidgetPadding.right), searchBarSpaceHeightPx);
411 }
412 }
413 return bounds;
414 }
415
Winson Chunga6945242014-01-08 14:04:34 -0800416 /** Returns the bounds of the workspace page indicators. */
417 Rect getWorkspacePageIndicatorBounds(Rect insets) {
418 Rect workspacePadding = getWorkspacePadding();
Winson Chung205cd772014-01-15 14:31:59 -0800419 if (isLandscape && transposeLayoutWithOrientation) {
420 if (isLayoutRtl) {
421 return new Rect(workspacePadding.left, workspacePadding.top,
422 workspacePadding.left + pageIndicatorHeightPx,
423 heightPx - workspacePadding.bottom - insets.bottom);
424 } else {
425 int pageIndicatorLeft = widthPx - workspacePadding.right;
426 return new Rect(pageIndicatorLeft, workspacePadding.top,
427 pageIndicatorLeft + pageIndicatorHeightPx,
428 heightPx - workspacePadding.bottom - insets.bottom);
429 }
430 } else {
431 int pageIndicatorTop = heightPx - insets.bottom - workspacePadding.bottom;
432 return new Rect(workspacePadding.left, pageIndicatorTop,
433 widthPx - workspacePadding.right, pageIndicatorTop + pageIndicatorHeightPx);
434 }
Winson Chunga6945242014-01-08 14:04:34 -0800435 }
436
Adam Cohen59400422014-03-05 18:07:04 -0800437 public int getWorkspaceGridHeight() {
438 Rect p = getWorkspacePadding();
439 return availableHeightPx - p.top - p.bottom;
440 }
441
442 public int getWorkspaceGridWidth() {
443 Rect p = getWorkspacePadding();
444 return availableWidthPx - p.left - p.right;
445 }
446
Winson Chungb3800242013-10-24 11:01:54 -0700447 /** Returns the workspace padding in the specified orientation */
448 Rect getWorkspacePadding() {
449 return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
450 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700451
Winson Chungb3800242013-10-24 11:01:54 -0700452 Rect getWorkspacePadding(int orientation) {
453 Rect searchBarBounds = getSearchBarBounds(orientation);
454 Rect padding = new Rect();
455 if (orientation == CellLayout.LANDSCAPE &&
456 transposeLayoutWithOrientation) {
457 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800458 if (isLayoutRtl) {
459 padding.set(hotseatBarHeightPx, edgeMarginPx,
460 searchBarBounds.width(), edgeMarginPx);
461 } else {
462 padding.set(searchBarBounds.width(), edgeMarginPx,
463 hotseatBarHeightPx, edgeMarginPx);
464 }
Winson Chungb3800242013-10-24 11:01:54 -0700465 } else {
466 if (isTablet()) {
467 // Pad the left and right of the workspace to ensure consistent spacing
468 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800469 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chungb3800242013-10-24 11:01:54 -0700470 int width = (orientation == CellLayout.LANDSCAPE)
471 ? Math.max(widthPx, heightPx)
472 : Math.min(widthPx, heightPx);
Winson Chung59a488a2013-12-10 12:32:14 -0800473 int height = (orientation != CellLayout.LANDSCAPE)
474 ? Math.max(widthPx, heightPx)
475 : Math.min(widthPx, heightPx);
476 int paddingTop = searchBarBounds.bottom;
477 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
Adam Cohen2e6da152015-05-06 11:42:25 -0700478 int availableWidth = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) +
479 (inv.numColumns * gapScale * cellWidthPx)));
Winson Chung59a488a2013-12-10 12:32:14 -0800480 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
Adam Cohen2e6da152015-05-06 11:42:25 -0700481 - (int) (2 * inv.numRows * cellHeightPx));
Winson Chung59a488a2013-12-10 12:32:14 -0800482 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
483 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700484 } else {
485 // Pad the top and bottom of the workspace with search/hotseat bar sizes
486 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
487 searchBarBounds.bottom,
488 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
489 hotseatBarHeightPx + pageIndicatorHeightPx);
490 }
491 }
492 return padding;
493 }
494
495 int getWorkspacePageSpacing(int orientation) {
Winson Chung59a488a2013-12-10 12:32:14 -0800496 if ((orientation == CellLayout.LANDSCAPE &&
497 transposeLayoutWithOrientation) || isLargeTablet()) {
Winson Chungb3800242013-10-24 11:01:54 -0700498 // In landscape mode the page spacing is set to the default.
499 return defaultPageSpacingPx;
500 } else {
501 // In portrait, we want the pages spaced such that there is no
502 // overhang of the previous / next page into the current page viewport.
503 // We assume symmetrical padding in portrait mode.
Adam Cohenefb31e32014-01-16 16:07:50 -0800504 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding().left);
Winson Chungb3800242013-10-24 11:01:54 -0700505 }
506 }
507
508 Rect getOverviewModeButtonBarRect() {
509 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
510 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
511 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
512 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
513 }
514
515 float getOverviewModeScale() {
516 Rect workspacePadding = getWorkspacePadding();
517 Rect overviewBar = getOverviewModeButtonBarRect();
518 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
519 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
520 }
521
522 // The rect returned will be extended to below the system ui that covers the workspace
523 Rect getHotseatRect() {
524 if (isVerticalBarLayout()) {
525 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
526 Integer.MAX_VALUE, availableHeightPx);
527 } else {
528 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
529 availableWidthPx, Integer.MAX_VALUE);
530 }
531 }
532
Adam Cohen2e6da152015-05-06 11:42:25 -0700533 public static int calculateCellWidth(int width, int countX) {
Winson Chungb3800242013-10-24 11:01:54 -0700534 return width / countX;
535 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700536 public static int calculateCellHeight(int height, int countY) {
Winson Chungb3800242013-10-24 11:01:54 -0700537 return height / countY;
538 }
539
540 boolean isPhone() {
541 return !isTablet && !isLargeTablet;
542 }
543 boolean isTablet() {
544 return isTablet;
545 }
546 boolean isLargeTablet() {
547 return isLargeTablet;
548 }
549
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700550 /**
551 * When {@code true}, hotseat is on the bottom row when in landscape mode.
552 * If {@code false}, hotseat is on the right column when in landscape mode.
553 */
Winson Chungb3800242013-10-24 11:01:54 -0700554 boolean isVerticalBarLayout() {
555 return isLandscape && transposeLayoutWithOrientation;
556 }
557
558 boolean shouldFadeAdjacentWorkspaceScreens() {
559 return isVerticalBarLayout() || isLargeTablet();
560 }
561
Jorim Jaggid017f882014-01-14 17:08:48 -0800562 int getVisibleChildCount(ViewGroup parent) {
563 int visibleChildren = 0;
564 for (int i = 0; i < parent.getChildCount(); i++) {
565 if (parent.getChildAt(i).getVisibility() != View.GONE) {
566 visibleChildren++;
567 }
568 }
569 return visibleChildren;
570 }
571
Winson Chungb3800242013-10-24 11:01:54 -0700572 public void layout(Launcher launcher) {
573 FrameLayout.LayoutParams lp;
Winson Chungb3800242013-10-24 11:01:54 -0700574 boolean hasVerticalBarLayout = isVerticalBarLayout();
575
576 // Layout the search bar space
577 View searchBar = launcher.getSearchBar();
578 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
579 if (hasVerticalBarLayout) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800580 // Vertical search bar space
Sunny Goyald9cc7802015-04-23 14:47:19 -0700581 lp.gravity = Gravity.LEFT;
Winson Chungb3800242013-10-24 11:01:54 -0700582 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800583
584 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
585 targets.setOrientation(LinearLayout.VERTICAL);
Sunny Goyald9cc7802015-04-23 14:47:19 -0700586 FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams();
587 targetsLp.gravity = Gravity.TOP;
588 targetsLp.height = LayoutParams.WRAP_CONTENT;
589
Winson Chungb3800242013-10-24 11:01:54 -0700590 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800591 // Horizontal search bar space
Sunny Goyald9cc7802015-04-23 14:47:19 -0700592 lp.gravity = Gravity.TOP;
Winson Chungb3800242013-10-24 11:01:54 -0700593 lp.height = searchBarSpaceHeightPx;
Sunny Goyald9cc7802015-04-23 14:47:19 -0700594
595 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
596 targets.getLayoutParams().width = searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700597 }
598 searchBar.setLayoutParams(lp);
599
Winson Chungb3800242013-10-24 11:01:54 -0700600 // Layout the workspace
601 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
602 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
603 lp.gravity = Gravity.CENTER;
604 int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
605 Rect padding = getWorkspacePadding(orientation);
606 workspace.setLayoutParams(lp);
607 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
608 workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
609
610 // Layout the hotseat
611 View hotseat = launcher.findViewById(R.id.hotseat);
612 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
613 if (hasVerticalBarLayout) {
614 // Vertical hotseat
Winson Chung42b3c062013-12-04 12:09:59 -0800615 lp.gravity = Gravity.END;
Winson Chungb3800242013-10-24 11:01:54 -0700616 lp.width = hotseatBarHeightPx;
617 lp.height = LayoutParams.MATCH_PARENT;
618 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
619 } else if (isTablet()) {
Winson Chung59a488a2013-12-10 12:32:14 -0800620 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700621 lp.gravity = Gravity.BOTTOM;
622 lp.width = LayoutParams.MATCH_PARENT;
623 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800624 hotseat.setPadding(edgeMarginPx + padding.left, 0,
625 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700626 2 * edgeMarginPx);
627 } else {
628 // For phones, layout the hotseat without any bottom margin
629 // to ensure that we have space for the folders
630 lp.gravity = Gravity.BOTTOM;
631 lp.width = LayoutParams.MATCH_PARENT;
632 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000633 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
634 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700635 }
636 hotseat.setLayoutParams(lp);
637
638 // Layout the page indicators
639 View pageIndicator = launcher.findViewById(R.id.page_indicator);
640 if (pageIndicator != null) {
641 if (hasVerticalBarLayout) {
642 // Hide the page indicators when we have vertical search/hotseat
643 pageIndicator.setVisibility(View.GONE);
644 } else {
645 // Put the page indicators above the hotseat
646 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
647 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
648 lp.width = LayoutParams.WRAP_CONTENT;
649 lp.height = LayoutParams.WRAP_CONTENT;
650 lp.bottomMargin = hotseatBarHeightPx;
651 pageIndicator.setLayoutParams(lp);
652 }
653 }
654
Winson Chungb3800242013-10-24 11:01:54 -0700655 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800656 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700657 if (overviewMode != null) {
658 Rect r = getOverviewModeButtonBarRect();
659 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
660 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700661
662 int visibleChildCount = getVisibleChildCount(overviewMode);
663 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
664 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
665
666 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chungb3800242013-10-24 11:01:54 -0700667 lp.height = r.height();
668 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700669
670 if (lp.width > totalItemWidth && visibleChildCount > 1) {
671 // We have enough space. Lets add some margin too.
672 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
673 View lastChild = null;
674
675 // Set margin of all visible children except the last visible child
676 for (int i = 0; i < visibleChildCount; i++) {
677 if (lastChild != null) {
678 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
679 if (isLayoutRtl) {
680 clp.leftMargin = margin;
681 } else {
682 clp.rightMargin = margin;
683 }
684 lastChild.setLayoutParams(clp);
685 lastChild = null;
686 }
687 View thisChild = overviewMode.getChildAt(i);
688 if (thisChild.getVisibility() != View.GONE) {
689 lastChild = thisChild;
690 }
691 }
692 }
Winson Chungb3800242013-10-24 11:01:54 -0700693 }
694 }
695}