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