blob: 7ce0446766230d7e13ad913d9f152584b8244967 [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();
486 int pageIndicatorTop = heightPx - insets.bottom - workspacePadding.bottom;
487 return new Rect(workspacePadding.left, pageIndicatorTop,
488 widthPx - workspacePadding.right, pageIndicatorTop + pageIndicatorHeightPx);
489 }
490
Winson Chungb3800242013-10-24 11:01:54 -0700491 /** Returns the workspace padding in the specified orientation */
492 Rect getWorkspacePadding() {
493 return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
494 }
495 Rect getWorkspacePadding(int orientation) {
496 Rect searchBarBounds = getSearchBarBounds(orientation);
497 Rect padding = new Rect();
498 if (orientation == CellLayout.LANDSCAPE &&
499 transposeLayoutWithOrientation) {
500 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800501 if (isLayoutRtl) {
502 padding.set(hotseatBarHeightPx, edgeMarginPx,
503 searchBarBounds.width(), edgeMarginPx);
504 } else {
505 padding.set(searchBarBounds.width(), edgeMarginPx,
506 hotseatBarHeightPx, edgeMarginPx);
507 }
Winson Chungb3800242013-10-24 11:01:54 -0700508 } else {
509 if (isTablet()) {
510 // Pad the left and right of the workspace to ensure consistent spacing
511 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800512 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chungb3800242013-10-24 11:01:54 -0700513 int width = (orientation == CellLayout.LANDSCAPE)
514 ? Math.max(widthPx, heightPx)
515 : Math.min(widthPx, heightPx);
Winson Chung59a488a2013-12-10 12:32:14 -0800516 int height = (orientation != CellLayout.LANDSCAPE)
517 ? Math.max(widthPx, heightPx)
518 : Math.min(widthPx, heightPx);
519 int paddingTop = searchBarBounds.bottom;
520 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
521 int availableWidth = Math.max(0, width - (int) ((numColumns * cellWidthPx) +
522 (numColumns * gapScale * cellWidthPx)));
523 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
524 - (int) (2 * numRows * cellHeightPx));
525 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
526 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700527 } else {
528 // Pad the top and bottom of the workspace with search/hotseat bar sizes
529 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
530 searchBarBounds.bottom,
531 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
532 hotseatBarHeightPx + pageIndicatorHeightPx);
533 }
534 }
535 return padding;
536 }
537
538 int getWorkspacePageSpacing(int orientation) {
Winson Chung59a488a2013-12-10 12:32:14 -0800539 if ((orientation == CellLayout.LANDSCAPE &&
540 transposeLayoutWithOrientation) || isLargeTablet()) {
Winson Chungb3800242013-10-24 11:01:54 -0700541 // In landscape mode the page spacing is set to the default.
542 return defaultPageSpacingPx;
543 } else {
544 // In portrait, we want the pages spaced such that there is no
545 // overhang of the previous / next page into the current page viewport.
546 // We assume symmetrical padding in portrait mode.
547 return 2 * getWorkspacePadding().left;
548 }
549 }
550
551 Rect getOverviewModeButtonBarRect() {
552 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
553 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
554 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
555 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
556 }
557
558 float getOverviewModeScale() {
559 Rect workspacePadding = getWorkspacePadding();
560 Rect overviewBar = getOverviewModeButtonBarRect();
561 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
562 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
563 }
564
565 // The rect returned will be extended to below the system ui that covers the workspace
566 Rect getHotseatRect() {
567 if (isVerticalBarLayout()) {
568 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
569 Integer.MAX_VALUE, availableHeightPx);
570 } else {
571 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
572 availableWidthPx, Integer.MAX_VALUE);
573 }
574 }
575
576 int calculateCellWidth(int width, int countX) {
577 return width / countX;
578 }
579 int calculateCellHeight(int height, int countY) {
580 return height / countY;
581 }
582
583 boolean isPhone() {
584 return !isTablet && !isLargeTablet;
585 }
586 boolean isTablet() {
587 return isTablet;
588 }
589 boolean isLargeTablet() {
590 return isLargeTablet;
591 }
592
593 boolean isVerticalBarLayout() {
594 return isLandscape && transposeLayoutWithOrientation;
595 }
596
597 boolean shouldFadeAdjacentWorkspaceScreens() {
598 return isVerticalBarLayout() || isLargeTablet();
599 }
600
Jorim Jaggid017f882014-01-14 17:08:48 -0800601 int getVisibleChildCount(ViewGroup parent) {
602 int visibleChildren = 0;
603 for (int i = 0; i < parent.getChildCount(); i++) {
604 if (parent.getChildAt(i).getVisibility() != View.GONE) {
605 visibleChildren++;
606 }
607 }
608 return visibleChildren;
609 }
610
611 int calculateOverviewModeWidth(int visibleChildCount) {
612 return visibleChildCount * overviewModeBarItemWidthPx +
613 (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
614 }
615
Winson Chungb3800242013-10-24 11:01:54 -0700616 public void layout(Launcher launcher) {
617 FrameLayout.LayoutParams lp;
618 Resources res = launcher.getResources();
619 boolean hasVerticalBarLayout = isVerticalBarLayout();
620
621 // Layout the search bar space
622 View searchBar = launcher.getSearchBar();
623 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
624 if (hasVerticalBarLayout) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800625 // Vertical search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700626 lp.gravity = Gravity.TOP | Gravity.LEFT;
627 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800628 lp.height = LayoutParams.WRAP_CONTENT;
Winson Chungb3800242013-10-24 11:01:54 -0700629 searchBar.setPadding(
630 0, 2 * edgeMarginPx, 0,
631 2 * edgeMarginPx);
Adam Cohen24ce0b32014-01-14 16:18:14 -0800632
633 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
634 targets.setOrientation(LinearLayout.VERTICAL);
Winson Chungb3800242013-10-24 11:01:54 -0700635 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800636 // Horizontal search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700637 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
638 lp.width = searchBarSpaceWidthPx;
639 lp.height = searchBarSpaceHeightPx;
640 searchBar.setPadding(
641 2 * edgeMarginPx,
Winson Chung69e04ea2013-12-02 14:43:44 -0800642 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700643 2 * edgeMarginPx, 0);
644 }
645 searchBar.setLayoutParams(lp);
646
Winson Chungb3800242013-10-24 11:01:54 -0700647 // Layout the voice proxy
648 View voiceButtonProxy = launcher.findViewById(R.id.voice_button_proxy);
649 if (voiceButtonProxy != null) {
650 if (hasVerticalBarLayout) {
651 // TODO: MOVE THIS INTO SEARCH BAR MEASURE
652 } else {
653 lp = (FrameLayout.LayoutParams) voiceButtonProxy.getLayoutParams();
654 lp.gravity = Gravity.TOP | Gravity.END;
655 lp.width = (widthPx - searchBarSpaceWidthPx) / 2 +
656 2 * iconSizePx;
657 lp.height = searchBarSpaceHeightPx;
658 }
659 }
660
661 // Layout the workspace
662 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
663 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
664 lp.gravity = Gravity.CENTER;
665 int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
666 Rect padding = getWorkspacePadding(orientation);
667 workspace.setLayoutParams(lp);
668 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
669 workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
670
671 // Layout the hotseat
672 View hotseat = launcher.findViewById(R.id.hotseat);
673 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
674 if (hasVerticalBarLayout) {
675 // Vertical hotseat
Winson Chung42b3c062013-12-04 12:09:59 -0800676 lp.gravity = Gravity.END;
Winson Chungb3800242013-10-24 11:01:54 -0700677 lp.width = hotseatBarHeightPx;
678 lp.height = LayoutParams.MATCH_PARENT;
679 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
680 } else if (isTablet()) {
Winson Chung59a488a2013-12-10 12:32:14 -0800681 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700682 lp.gravity = Gravity.BOTTOM;
683 lp.width = LayoutParams.MATCH_PARENT;
684 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800685 hotseat.setPadding(edgeMarginPx + padding.left, 0,
686 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700687 2 * edgeMarginPx);
688 } else {
689 // For phones, layout the hotseat without any bottom margin
690 // to ensure that we have space for the folders
691 lp.gravity = Gravity.BOTTOM;
692 lp.width = LayoutParams.MATCH_PARENT;
693 lp.height = hotseatBarHeightPx;
694 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
695 2 * edgeMarginPx, 0);
696 }
697 hotseat.setLayoutParams(lp);
698
699 // Layout the page indicators
700 View pageIndicator = launcher.findViewById(R.id.page_indicator);
701 if (pageIndicator != null) {
702 if (hasVerticalBarLayout) {
703 // Hide the page indicators when we have vertical search/hotseat
704 pageIndicator.setVisibility(View.GONE);
705 } else {
706 // Put the page indicators above the hotseat
707 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
708 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
709 lp.width = LayoutParams.WRAP_CONTENT;
710 lp.height = LayoutParams.WRAP_CONTENT;
711 lp.bottomMargin = hotseatBarHeightPx;
712 pageIndicator.setLayoutParams(lp);
713 }
714 }
715
716 // Layout AllApps
717 AppsCustomizeTabHost host = (AppsCustomizeTabHost)
718 launcher.findViewById(R.id.apps_customize_pane);
719 if (host != null) {
720 // Center the all apps page indicator
721 int pageIndicatorHeight = (int) (pageIndicatorHeightPx * Math.min(1f,
722 (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX)));
723 pageIndicator = host.findViewById(R.id.apps_customize_page_indicator);
724 if (pageIndicator != null) {
725 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
726 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
727 lp.width = LayoutParams.WRAP_CONTENT;
728 lp.height = pageIndicatorHeight;
729 pageIndicator.setLayoutParams(lp);
730 }
731
732 AppsCustomizePagedView pagedView = (AppsCustomizePagedView)
733 host.findViewById(R.id.apps_customize_pane_content);
734 padding = new Rect();
735 if (pagedView != null) {
736 // Constrain the dimensions of all apps so that it does not span the full width
737 int paddingLR = (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) /
738 (2 * (allAppsNumCols + 1));
739 int paddingTB = (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows)) /
740 (2 * (allAppsNumRows + 1));
741 paddingLR = Math.min(paddingLR, (int)((paddingLR + paddingTB) * 0.75f));
742 paddingTB = Math.min(paddingTB, (int)((paddingLR + paddingTB) * 0.75f));
743 int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR));
744 int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 2;
Winson Chung495f44d2013-12-04 12:51:53 -0800745 // Only adjust the side paddings on landscape phones, or tablets
746 if ((isTablet() || isLandscape) && gridPaddingLR > (allAppsCellWidthPx / 4)) {
Winson Chungb3800242013-10-24 11:01:54 -0700747 padding.left = padding.right = gridPaddingLR;
748 }
749 // The icons are centered, so we can't just offset by the page indicator height
750 // because the empty space will actually be pageIndicatorHeight + paddingTB
751 padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB);
752 pagedView.setAllAppsPadding(padding);
753 pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight);
754 }
755 }
756
757 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800758 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700759 if (overviewMode != null) {
760 Rect r = getOverviewModeButtonBarRect();
761 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
762 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Jorim Jaggid017f882014-01-14 17:08:48 -0800763 lp.width = Math.min(availableWidthPx,
764 calculateOverviewModeWidth(getVisibleChildCount(overviewMode)));
Winson Chungb3800242013-10-24 11:01:54 -0700765 lp.height = r.height();
766 overviewMode.setLayoutParams(lp);
767 }
768 }
769}