blob: b6c394cdbc5a937a0b6bd59bb9d17b3693fdb316 [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;
30import android.view.Display;
31import android.view.Gravity;
32import android.view.Surface;
33import android.view.View;
34import android.view.ViewGroup.LayoutParams;
35import android.view.WindowManager;
36import android.widget.FrameLayout;
37
38import java.util.ArrayList;
39import java.util.Collections;
40import java.util.Comparator;
41
42
43class DeviceProfileQuery {
44 float widthDps;
45 float heightDps;
46 float value;
47 PointF dimens;
48
49 DeviceProfileQuery(float w, float h, float v) {
50 widthDps = w;
51 heightDps = h;
52 value = v;
53 dimens = new PointF(w, h);
54 }
55}
56
57public class DeviceProfile {
58 public static interface DeviceProfileCallbacks {
59 public void onAvailableSizeChanged(DeviceProfile grid);
60 }
61
62 String name;
63 float minWidthDps;
64 float minHeightDps;
65 float numRows;
66 float numColumns;
67 float numHotseatIcons;
68 private float iconSize;
69 private float iconTextSize;
70 private int iconDrawablePaddingOriginalPx;
71 private float hotseatIconSize;
72
73 boolean isLandscape;
74 boolean isTablet;
75 boolean isLargeTablet;
Winson Chung42b3c062013-12-04 12:09:59 -080076 boolean isLayoutRtl;
Winson Chungb3800242013-10-24 11:01:54 -070077 boolean transposeLayoutWithOrientation;
78
79 int desiredWorkspaceLeftRightMarginPx;
80 int edgeMarginPx;
81 Rect defaultWidgetPadding;
82
83 int widthPx;
84 int heightPx;
85 int availableWidthPx;
86 int availableHeightPx;
87 int defaultPageSpacingPx;
88
89 int overviewModeMinIconZoneHeightPx;
90 int overviewModeMaxIconZoneHeightPx;
91 int overviewModeMaxBarWidthPx;
92 float overviewModeIconZoneRatio;
93 float overviewModeScaleFactor;
94
95 int iconSizePx;
96 int iconTextSizePx;
97 int iconDrawablePaddingPx;
98 int cellWidthPx;
99 int cellHeightPx;
100 int allAppsIconSizePx;
101 int allAppsIconTextSizePx;
102 int allAppsCellWidthPx;
103 int allAppsCellHeightPx;
104 int allAppsCellPaddingPx;
105 int folderBackgroundOffset;
106 int folderIconSizePx;
107 int folderCellWidthPx;
108 int folderCellHeightPx;
109 int hotseatCellWidthPx;
110 int hotseatCellHeightPx;
111 int hotseatIconSizePx;
112 int hotseatBarHeightPx;
113 int hotseatAllAppsRank;
114 int allAppsNumRows;
115 int allAppsNumCols;
116 int searchBarSpaceWidthPx;
117 int searchBarSpaceMaxWidthPx;
118 int searchBarSpaceHeightPx;
119 int searchBarHeightPx;
120 int pageIndicatorHeightPx;
121
Winson Chung59a488a2013-12-10 12:32:14 -0800122 float dragViewScale;
123
Winson Chungb3800242013-10-24 11:01:54 -0700124 private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>();
125
126 DeviceProfile(String n, float w, float h, float r, float c,
127 float is, float its, float hs, float his) {
128 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
129 if (!AppsCustomizePagedView.DISABLE_ALL_APPS && hs % 2 == 0) {
130 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
131 }
132
133 name = n;
134 minWidthDps = w;
135 minHeightDps = h;
136 numRows = r;
137 numColumns = c;
138 iconSize = is;
139 iconTextSize = its;
140 numHotseatIcons = hs;
141 hotseatIconSize = his;
142 }
143
144 DeviceProfile(Context context,
145 ArrayList<DeviceProfile> profiles,
146 float minWidth, float minHeight,
147 int wPx, int hPx,
148 int awPx, int ahPx,
149 Resources res) {
150 DisplayMetrics dm = res.getDisplayMetrics();
151 ArrayList<DeviceProfileQuery> points =
152 new ArrayList<DeviceProfileQuery>();
153 transposeLayoutWithOrientation =
154 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
155 minWidthDps = minWidth;
156 minHeightDps = minHeight;
157
158 ComponentName cn = new ComponentName(context.getPackageName(),
159 this.getClass().getName());
160 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
161 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
162 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
163 pageIndicatorHeightPx =
164 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
165 defaultPageSpacingPx =
166 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
167 allAppsCellPaddingPx =
168 res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_padding);
169 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);
173 overviewModeMaxBarWidthPx =
174 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_max_width);
175 overviewModeIconZoneRatio =
176 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
177 overviewModeScaleFactor =
178 res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f;
179
180 // Interpolate the rows
181 for (DeviceProfile p : profiles) {
182 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numRows));
183 }
184 numRows = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
185 // Interpolate the columns
186 points.clear();
187 for (DeviceProfile p : profiles) {
188 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numColumns));
189 }
190 numColumns = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
191 // Interpolate the hotseat length
192 points.clear();
193 for (DeviceProfile p : profiles) {
194 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numHotseatIcons));
195 }
196 numHotseatIcons = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
197 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
198
199 // Interpolate the icon size
200 points.clear();
201 for (DeviceProfile p : profiles) {
202 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconSize));
203 }
204 iconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
205 // AllApps uses the original non-scaled icon size
206 allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm);
207
208 // Interpolate the icon text size
209 points.clear();
210 for (DeviceProfile p : profiles) {
211 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconTextSize));
212 }
213 iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points);
214 iconDrawablePaddingOriginalPx =
215 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
216 // AllApps uses the original non-scaled icon text size
217 allAppsIconTextSizePx = DynamicGrid.pxFromDp(iconTextSize, dm);
218
219 // Interpolate the hotseat icon size
220 points.clear();
221 for (DeviceProfile p : profiles) {
222 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.hotseatIconSize));
223 }
224 // Hotseat
225 hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
226
227 // Calculate the remaining vars
228 updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx);
229 updateAvailableDimensions(context);
230 }
231
232 void addCallback(DeviceProfileCallbacks cb) {
233 mCallbacks.add(cb);
234 cb.onAvailableSizeChanged(this);
235 }
236 void removeCallback(DeviceProfileCallbacks cb) {
237 mCallbacks.remove(cb);
238 }
239
240 private int getDeviceOrientation(Context context) {
241 WindowManager windowManager = (WindowManager)
242 context.getSystemService(Context.WINDOW_SERVICE);
243 Resources resources = context.getResources();
244 DisplayMetrics dm = resources.getDisplayMetrics();
245 Configuration config = resources.getConfiguration();
246 int rotation = windowManager.getDefaultDisplay().getRotation();
247
248 boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) &&
249 (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180);
250 boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) &&
251 (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
252 if (isLandscape || isRotatedPortrait) {
253 return CellLayout.LANDSCAPE;
254 } else {
255 return CellLayout.PORTRAIT;
256 }
257 }
258
259 private void updateAvailableDimensions(Context context) {
260 WindowManager windowManager = (WindowManager)
261 context.getSystemService(Context.WINDOW_SERVICE);
262 Display display = windowManager.getDefaultDisplay();
263 Resources resources = context.getResources();
264 DisplayMetrics dm = resources.getDisplayMetrics();
265 Configuration config = resources.getConfiguration();
266
267 // There are three possible configurations that the dynamic grid accounts for, portrait,
268 // landscape with the nav bar at the bottom, and landscape with the nav bar at the side.
269 // To prevent waiting for fitSystemWindows(), we make the observation that in landscape,
270 // the height is the smallest height (either with the nav bar at the bottom or to the
271 // side) and otherwise, the height is simply the largest possible height for a portrait
272 // device.
273 Point size = new Point();
274 Point smallestSize = new Point();
275 Point largestSize = new Point();
276 display.getSize(size);
277 display.getCurrentSizeRange(smallestSize, largestSize);
278 availableWidthPx = size.x;
279 if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
280 availableHeightPx = smallestSize.y;
281 } else {
282 availableHeightPx = largestSize.y;
283 }
284
285 // Check to see if the icons fit in the new available height. If not, then we need to
286 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700287 float scale = 1f;
288 int drawablePadding = iconDrawablePaddingOriginalPx;
289 updateIconSize(1f, drawablePadding, resources, dm);
290 float usedHeight = (cellHeightPx * numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800291
292 Rect workspacePadding = getWorkspacePadding();
Winson Chungb3800242013-10-24 11:01:54 -0700293 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
294 if (usedHeight > maxHeight) {
295 scale = maxHeight / usedHeight;
296 drawablePadding = 0;
297 }
298 updateIconSize(scale, drawablePadding, resources, dm);
299
300 // Make the callbacks
301 for (DeviceProfileCallbacks cb : mCallbacks) {
302 cb.onAvailableSizeChanged(this);
303 }
304 }
305
306 private void updateIconSize(float scale, int drawablePadding, Resources resources,
307 DisplayMetrics dm) {
308 iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale);
309 iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale);
310 iconDrawablePaddingPx = drawablePadding;
311 hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale);
312
313 // Search Bar
314 searchBarSpaceMaxWidthPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width);
315 searchBarHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
316 searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
Winson Chung69e04ea2013-12-02 14:43:44 -0800317 searchBarSpaceHeightPx = searchBarHeightPx + getSearchBarTopOffset();
Winson Chungb3800242013-10-24 11:01:54 -0700318
319 // Calculate the actual text height
320 Paint textPaint = new Paint();
321 textPaint.setTextSize(iconTextSizePx);
322 FontMetrics fm = textPaint.getFontMetrics();
323 cellWidthPx = iconSizePx;
324 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung59a488a2013-12-10 12:32:14 -0800325 final float scaleDps = resources.getDimensionPixelSize(R.dimen.dragViewScale);
326 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700327
328 // Hotseat
329 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
330 hotseatCellWidthPx = iconSizePx;
331 hotseatCellHeightPx = iconSizePx;
332
333 // Folder
334 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
335 folderCellHeightPx = cellHeightPx + edgeMarginPx;
336 folderBackgroundOffset = -edgeMarginPx;
337 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
338
339 // All Apps
340 Rect padding = getWorkspacePadding(isLandscape ?
341 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
342 int pageIndicatorOffset =
343 resources.getDimensionPixelSize(R.dimen.apps_customize_page_indicator_offset);
344 allAppsCellWidthPx = allAppsIconSizePx;
345 allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + iconTextSizePx;
346 int maxLongEdgeCellCount =
347 resources.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count);
348 int maxShortEdgeCellCount =
349 resources.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count);
350 int minEdgeCellCount =
351 resources.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count);
352 int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount);
353 int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount);
354
355 allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx) /
356 (allAppsCellHeightPx + allAppsCellPaddingPx);
357 allAppsNumRows = Math.max(minEdgeCellCount, Math.min(maxRows, allAppsNumRows));
358 allAppsNumCols = (availableWidthPx) /
359 (allAppsCellWidthPx + allAppsCellPaddingPx);
360 allAppsNumCols = Math.max(minEdgeCellCount, Math.min(maxCols, allAppsNumCols));
361 }
362
363 void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx,
364 int awPx, int ahPx) {
Winson Chung42b3c062013-12-04 12:09:59 -0800365 Configuration configuration = resources.getConfiguration();
366 isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
Winson Chungb3800242013-10-24 11:01:54 -0700367 isTablet = resources.getBoolean(R.bool.is_tablet);
368 isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
Winson Chung42b3c062013-12-04 12:09:59 -0800369 isLayoutRtl = (configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
Winson Chungb3800242013-10-24 11:01:54 -0700370 widthPx = wPx;
371 heightPx = hPx;
372 availableWidthPx = awPx;
373 availableHeightPx = ahPx;
374
375 updateAvailableDimensions(context);
376 }
377
378 private float dist(PointF p0, PointF p1) {
379 return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) +
380 (p1.y-p0.y)*(p1.y-p0.y));
381 }
382
383 private float weight(PointF a, PointF b,
384 float pow) {
385 float d = dist(a, b);
386 if (d == 0f) {
387 return Float.POSITIVE_INFINITY;
388 }
389 return (float) (1f / Math.pow(d, pow));
390 }
391
392 private float invDistWeightedInterpolate(float width, float height,
393 ArrayList<DeviceProfileQuery> points) {
394 float sum = 0;
395 float weights = 0;
396 float pow = 5;
397 float kNearestNeighbors = 3;
398 final PointF xy = new PointF(width, height);
399
400 ArrayList<DeviceProfileQuery> pointsByNearness = points;
401 Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
402 public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
403 return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
404 }
405 });
406
407 for (int i = 0; i < pointsByNearness.size(); ++i) {
408 DeviceProfileQuery p = pointsByNearness.get(i);
409 if (i < kNearestNeighbors) {
410 float w = weight(xy, p.dimens, pow);
411 if (w == Float.POSITIVE_INFINITY) {
412 return p.value;
413 }
414 weights += w;
415 }
416 }
417
418 for (int i = 0; i < pointsByNearness.size(); ++i) {
419 DeviceProfileQuery p = pointsByNearness.get(i);
420 if (i < kNearestNeighbors) {
421 float w = weight(xy, p.dimens, pow);
422 sum += w * p.value / weights;
423 }
424 }
425
426 return sum;
427 }
428
Winson Chung69e04ea2013-12-02 14:43:44 -0800429 /** Returns the search bar top offset */
430 int getSearchBarTopOffset() {
431 if (isTablet() && !isVerticalBarLayout()) {
432 return 4 * edgeMarginPx;
433 } else {
434 return 2 * edgeMarginPx;
435 }
436 }
437
Winson Chungb3800242013-10-24 11:01:54 -0700438 /** Returns the search bar bounds in the current orientation */
439 Rect getSearchBarBounds() {
440 return getSearchBarBounds(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
441 }
442 /** Returns the search bar bounds in the specified orientation */
443 Rect getSearchBarBounds(int orientation) {
444 Rect bounds = new Rect();
445 if (orientation == CellLayout.LANDSCAPE &&
446 transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800447 if (isLayoutRtl) {
448 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
449 availableWidthPx, availableHeightPx - edgeMarginPx);
450 } else {
451 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
452 availableHeightPx - edgeMarginPx);
453 }
Winson Chungb3800242013-10-24 11:01:54 -0700454 } else {
455 if (isTablet()) {
456 // Pad the left and right of the workspace to ensure consistent spacing
457 // between all icons
458 int width = (orientation == CellLayout.LANDSCAPE)
459 ? Math.max(widthPx, heightPx)
460 : Math.min(widthPx, heightPx);
461 // XXX: If the icon size changes across orientations, we will have to take
462 // that into account here too.
463 int gap = (int) ((width - 2 * edgeMarginPx -
464 (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800465 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
466 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700467 searchBarSpaceHeightPx);
468 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800469 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
470 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700471 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
472 defaultWidgetPadding.right), searchBarSpaceHeightPx);
473 }
474 }
475 return bounds;
476 }
477
Winson Chunga6945242014-01-08 14:04:34 -0800478 /** Returns the bounds of the workspace page indicators. */
479 Rect getWorkspacePageIndicatorBounds(Rect insets) {
480 Rect workspacePadding = getWorkspacePadding();
481 int pageIndicatorTop = heightPx - insets.bottom - workspacePadding.bottom;
482 return new Rect(workspacePadding.left, pageIndicatorTop,
483 widthPx - workspacePadding.right, pageIndicatorTop + pageIndicatorHeightPx);
484 }
485
Winson Chungb3800242013-10-24 11:01:54 -0700486 /** Returns the workspace padding in the specified orientation */
487 Rect getWorkspacePadding() {
488 return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
489 }
490 Rect getWorkspacePadding(int orientation) {
491 Rect searchBarBounds = getSearchBarBounds(orientation);
492 Rect padding = new Rect();
493 if (orientation == CellLayout.LANDSCAPE &&
494 transposeLayoutWithOrientation) {
495 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800496 if (isLayoutRtl) {
497 padding.set(hotseatBarHeightPx, edgeMarginPx,
498 searchBarBounds.width(), edgeMarginPx);
499 } else {
500 padding.set(searchBarBounds.width(), edgeMarginPx,
501 hotseatBarHeightPx, edgeMarginPx);
502 }
Winson Chungb3800242013-10-24 11:01:54 -0700503 } else {
504 if (isTablet()) {
505 // Pad the left and right of the workspace to ensure consistent spacing
506 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800507 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chungb3800242013-10-24 11:01:54 -0700508 int width = (orientation == CellLayout.LANDSCAPE)
509 ? Math.max(widthPx, heightPx)
510 : Math.min(widthPx, heightPx);
Winson Chung59a488a2013-12-10 12:32:14 -0800511 int height = (orientation != CellLayout.LANDSCAPE)
512 ? Math.max(widthPx, heightPx)
513 : Math.min(widthPx, heightPx);
514 int paddingTop = searchBarBounds.bottom;
515 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
516 int availableWidth = Math.max(0, width - (int) ((numColumns * cellWidthPx) +
517 (numColumns * gapScale * cellWidthPx)));
518 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
519 - (int) (2 * numRows * cellHeightPx));
520 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
521 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700522 } else {
523 // Pad the top and bottom of the workspace with search/hotseat bar sizes
524 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
525 searchBarBounds.bottom,
526 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
527 hotseatBarHeightPx + pageIndicatorHeightPx);
528 }
529 }
530 return padding;
531 }
532
533 int getWorkspacePageSpacing(int orientation) {
Winson Chung59a488a2013-12-10 12:32:14 -0800534 if ((orientation == CellLayout.LANDSCAPE &&
535 transposeLayoutWithOrientation) || isLargeTablet()) {
Winson Chungb3800242013-10-24 11:01:54 -0700536 // In landscape mode the page spacing is set to the default.
537 return defaultPageSpacingPx;
538 } else {
539 // In portrait, we want the pages spaced such that there is no
540 // overhang of the previous / next page into the current page viewport.
541 // We assume symmetrical padding in portrait mode.
542 return 2 * getWorkspacePadding().left;
543 }
544 }
545
546 Rect getOverviewModeButtonBarRect() {
547 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
548 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
549 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
550 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
551 }
552
553 float getOverviewModeScale() {
554 Rect workspacePadding = getWorkspacePadding();
555 Rect overviewBar = getOverviewModeButtonBarRect();
556 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
557 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
558 }
559
560 // The rect returned will be extended to below the system ui that covers the workspace
561 Rect getHotseatRect() {
562 if (isVerticalBarLayout()) {
563 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
564 Integer.MAX_VALUE, availableHeightPx);
565 } else {
566 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
567 availableWidthPx, Integer.MAX_VALUE);
568 }
569 }
570
571 int calculateCellWidth(int width, int countX) {
572 return width / countX;
573 }
574 int calculateCellHeight(int height, int countY) {
575 return height / countY;
576 }
577
578 boolean isPhone() {
579 return !isTablet && !isLargeTablet;
580 }
581 boolean isTablet() {
582 return isTablet;
583 }
584 boolean isLargeTablet() {
585 return isLargeTablet;
586 }
587
588 boolean isVerticalBarLayout() {
589 return isLandscape && transposeLayoutWithOrientation;
590 }
591
592 boolean shouldFadeAdjacentWorkspaceScreens() {
593 return isVerticalBarLayout() || isLargeTablet();
594 }
595
596 public void layout(Launcher launcher) {
597 FrameLayout.LayoutParams lp;
598 Resources res = launcher.getResources();
599 boolean hasVerticalBarLayout = isVerticalBarLayout();
600
601 // Layout the search bar space
602 View searchBar = launcher.getSearchBar();
603 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
604 if (hasVerticalBarLayout) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800605 // Vertical search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700606 lp.gravity = Gravity.TOP | Gravity.LEFT;
607 lp.width = searchBarSpaceHeightPx;
608 lp.height = LayoutParams.MATCH_PARENT;
609 searchBar.setPadding(
610 0, 2 * edgeMarginPx, 0,
611 2 * edgeMarginPx);
612 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800613 // Horizontal search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700614 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
615 lp.width = searchBarSpaceWidthPx;
616 lp.height = searchBarSpaceHeightPx;
617 searchBar.setPadding(
618 2 * edgeMarginPx,
Winson Chung69e04ea2013-12-02 14:43:44 -0800619 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700620 2 * edgeMarginPx, 0);
621 }
622 searchBar.setLayoutParams(lp);
623
624 // Layout the search bar
625 View qsbBar = launcher.getQsbBar();
626 LayoutParams vglp = qsbBar.getLayoutParams();
627 vglp.width = LayoutParams.MATCH_PARENT;
628 vglp.height = LayoutParams.MATCH_PARENT;
629 qsbBar.setLayoutParams(vglp);
630
631 // Layout the voice proxy
632 View voiceButtonProxy = launcher.findViewById(R.id.voice_button_proxy);
633 if (voiceButtonProxy != null) {
634 if (hasVerticalBarLayout) {
635 // TODO: MOVE THIS INTO SEARCH BAR MEASURE
636 } else {
637 lp = (FrameLayout.LayoutParams) voiceButtonProxy.getLayoutParams();
638 lp.gravity = Gravity.TOP | Gravity.END;
639 lp.width = (widthPx - searchBarSpaceWidthPx) / 2 +
640 2 * iconSizePx;
641 lp.height = searchBarSpaceHeightPx;
642 }
643 }
644
645 // Layout the workspace
646 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
647 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
648 lp.gravity = Gravity.CENTER;
649 int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
650 Rect padding = getWorkspacePadding(orientation);
651 workspace.setLayoutParams(lp);
652 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
653 workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
654
655 // Layout the hotseat
656 View hotseat = launcher.findViewById(R.id.hotseat);
657 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
658 if (hasVerticalBarLayout) {
659 // Vertical hotseat
Winson Chung42b3c062013-12-04 12:09:59 -0800660 lp.gravity = Gravity.END;
Winson Chungb3800242013-10-24 11:01:54 -0700661 lp.width = hotseatBarHeightPx;
662 lp.height = LayoutParams.MATCH_PARENT;
663 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
664 } else if (isTablet()) {
Winson Chung59a488a2013-12-10 12:32:14 -0800665 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700666 lp.gravity = Gravity.BOTTOM;
667 lp.width = LayoutParams.MATCH_PARENT;
668 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800669 hotseat.setPadding(edgeMarginPx + padding.left, 0,
670 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700671 2 * edgeMarginPx);
672 } else {
673 // For phones, layout the hotseat without any bottom margin
674 // to ensure that we have space for the folders
675 lp.gravity = Gravity.BOTTOM;
676 lp.width = LayoutParams.MATCH_PARENT;
677 lp.height = hotseatBarHeightPx;
678 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
679 2 * edgeMarginPx, 0);
680 }
681 hotseat.setLayoutParams(lp);
682
683 // Layout the page indicators
684 View pageIndicator = launcher.findViewById(R.id.page_indicator);
685 if (pageIndicator != null) {
686 if (hasVerticalBarLayout) {
687 // Hide the page indicators when we have vertical search/hotseat
688 pageIndicator.setVisibility(View.GONE);
689 } else {
690 // Put the page indicators above the hotseat
691 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
692 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
693 lp.width = LayoutParams.WRAP_CONTENT;
694 lp.height = LayoutParams.WRAP_CONTENT;
695 lp.bottomMargin = hotseatBarHeightPx;
696 pageIndicator.setLayoutParams(lp);
697 }
698 }
699
700 // Layout AllApps
701 AppsCustomizeTabHost host = (AppsCustomizeTabHost)
702 launcher.findViewById(R.id.apps_customize_pane);
703 if (host != null) {
704 // Center the all apps page indicator
705 int pageIndicatorHeight = (int) (pageIndicatorHeightPx * Math.min(1f,
706 (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX)));
707 pageIndicator = host.findViewById(R.id.apps_customize_page_indicator);
708 if (pageIndicator != null) {
709 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
710 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
711 lp.width = LayoutParams.WRAP_CONTENT;
712 lp.height = pageIndicatorHeight;
713 pageIndicator.setLayoutParams(lp);
714 }
715
716 AppsCustomizePagedView pagedView = (AppsCustomizePagedView)
717 host.findViewById(R.id.apps_customize_pane_content);
718 padding = new Rect();
719 if (pagedView != null) {
720 // Constrain the dimensions of all apps so that it does not span the full width
721 int paddingLR = (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) /
722 (2 * (allAppsNumCols + 1));
723 int paddingTB = (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows)) /
724 (2 * (allAppsNumRows + 1));
725 paddingLR = Math.min(paddingLR, (int)((paddingLR + paddingTB) * 0.75f));
726 paddingTB = Math.min(paddingTB, (int)((paddingLR + paddingTB) * 0.75f));
727 int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR));
728 int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 2;
Winson Chung495f44d2013-12-04 12:51:53 -0800729 // Only adjust the side paddings on landscape phones, or tablets
730 if ((isTablet() || isLandscape) && gridPaddingLR > (allAppsCellWidthPx / 4)) {
Winson Chungb3800242013-10-24 11:01:54 -0700731 padding.left = padding.right = gridPaddingLR;
732 }
733 // The icons are centered, so we can't just offset by the page indicator height
734 // because the empty space will actually be pageIndicatorHeight + paddingTB
735 padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB);
736 pagedView.setAllAppsPadding(padding);
737 pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight);
738 }
739 }
740
741 // Layout the Overview Mode
742 View overviewMode = launcher.getOverviewPanel();
743 if (overviewMode != null) {
744 Rect r = getOverviewModeButtonBarRect();
745 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
746 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
747 lp.width = Math.min(availableWidthPx, overviewModeMaxBarWidthPx);
748 lp.height = r.height();
749 overviewMode.setLayoutParams(lp);
750 }
751 }
752}