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