blob: 018fcfcbc61203f64317c97e01679b22e4262480 [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 {
Winson Chungbe876472014-05-14 14:15:20 -070046 DeviceProfile profile;
Winson Chungb3800242013-10-24 11:01:54 -070047 float widthDps;
48 float heightDps;
49 float value;
50 PointF dimens;
51
Winson Chungbe876472014-05-14 14:15:20 -070052 DeviceProfileQuery(DeviceProfile p, float v) {
53 widthDps = p.minWidthDps;
54 heightDps = p.minHeightDps;
Winson Chungb3800242013-10-24 11:01:54 -070055 value = v;
Winson Chungbe876472014-05-14 14:15:20 -070056 dimens = new PointF(widthDps, heightDps);
57 profile = p;
Winson Chungb3800242013-10-24 11:01:54 -070058 }
59}
60
61public class DeviceProfile {
62 public static interface DeviceProfileCallbacks {
63 public void onAvailableSizeChanged(DeviceProfile grid);
64 }
65
66 String name;
67 float minWidthDps;
68 float minHeightDps;
69 float numRows;
70 float numColumns;
71 float numHotseatIcons;
72 private float iconSize;
73 private float iconTextSize;
74 private int iconDrawablePaddingOriginalPx;
75 private float hotseatIconSize;
76
Winson Chungbe876472014-05-14 14:15:20 -070077 int defaultLayoutId;
78 int defaultNoAllAppsLayoutId;
79
Winson Chungb3800242013-10-24 11:01:54 -070080 boolean isLandscape;
81 boolean isTablet;
82 boolean isLargeTablet;
Winson Chung42b3c062013-12-04 12:09:59 -080083 boolean isLayoutRtl;
Winson Chungb3800242013-10-24 11:01:54 -070084 boolean transposeLayoutWithOrientation;
85
86 int desiredWorkspaceLeftRightMarginPx;
87 int edgeMarginPx;
88 Rect defaultWidgetPadding;
89
90 int widthPx;
91 int heightPx;
92 int availableWidthPx;
93 int availableHeightPx;
94 int defaultPageSpacingPx;
95
96 int overviewModeMinIconZoneHeightPx;
97 int overviewModeMaxIconZoneHeightPx;
Jorim Jaggid017f882014-01-14 17:08:48 -080098 int overviewModeBarItemWidthPx;
99 int overviewModeBarSpacerWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700100 float overviewModeIconZoneRatio;
101 float overviewModeScaleFactor;
102
103 int iconSizePx;
104 int iconTextSizePx;
105 int iconDrawablePaddingPx;
106 int cellWidthPx;
107 int cellHeightPx;
108 int allAppsIconSizePx;
109 int allAppsIconTextSizePx;
110 int allAppsCellWidthPx;
111 int allAppsCellHeightPx;
112 int allAppsCellPaddingPx;
113 int folderBackgroundOffset;
114 int folderIconSizePx;
115 int folderCellWidthPx;
116 int folderCellHeightPx;
117 int hotseatCellWidthPx;
118 int hotseatCellHeightPx;
119 int hotseatIconSizePx;
120 int hotseatBarHeightPx;
121 int hotseatAllAppsRank;
122 int allAppsNumRows;
123 int allAppsNumCols;
124 int searchBarSpaceWidthPx;
125 int searchBarSpaceMaxWidthPx;
126 int searchBarSpaceHeightPx;
127 int searchBarHeightPx;
128 int pageIndicatorHeightPx;
129
Winson Chung59a488a2013-12-10 12:32:14 -0800130 float dragViewScale;
131
Winson Chungb3800242013-10-24 11:01:54 -0700132 private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>();
133
134 DeviceProfile(String n, float w, float h, float r, float c,
Winson Chungbe876472014-05-14 14:15:20 -0700135 float is, float its, float hs, float his, int dlId, int dnalId) {
Winson Chungb3800242013-10-24 11:01:54 -0700136 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800137 if (!LauncherAppState.isDisableAllApps() && hs % 2 == 0) {
Winson Chungb3800242013-10-24 11:01:54 -0700138 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
139 }
140
141 name = n;
142 minWidthDps = w;
143 minHeightDps = h;
144 numRows = r;
145 numColumns = c;
146 iconSize = is;
147 iconTextSize = its;
148 numHotseatIcons = hs;
149 hotseatIconSize = his;
Winson Chungbe876472014-05-14 14:15:20 -0700150 defaultLayoutId = dlId;
151 defaultNoAllAppsLayoutId = dnalId;
Winson Chungb3800242013-10-24 11:01:54 -0700152 }
153
154 DeviceProfile(Context context,
155 ArrayList<DeviceProfile> profiles,
156 float minWidth, float minHeight,
157 int wPx, int hPx,
158 int awPx, int ahPx,
159 Resources res) {
160 DisplayMetrics dm = res.getDisplayMetrics();
161 ArrayList<DeviceProfileQuery> points =
162 new ArrayList<DeviceProfileQuery>();
163 transposeLayoutWithOrientation =
164 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
165 minWidthDps = minWidth;
166 minHeightDps = minHeight;
167
168 ComponentName cn = new ComponentName(context.getPackageName(),
169 this.getClass().getName());
170 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
171 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
172 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
173 pageIndicatorHeightPx =
174 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
175 defaultPageSpacingPx =
176 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
177 allAppsCellPaddingPx =
178 res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_padding);
179 overviewModeMinIconZoneHeightPx =
180 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
181 overviewModeMaxIconZoneHeightPx =
182 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800183 overviewModeBarItemWidthPx =
184 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
185 overviewModeBarSpacerWidthPx =
186 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700187 overviewModeIconZoneRatio =
188 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
189 overviewModeScaleFactor =
190 res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f;
191
Winson Chungbe876472014-05-14 14:15:20 -0700192 // Find the closes profile given the width/height
Winson Chungb3800242013-10-24 11:01:54 -0700193 for (DeviceProfile p : profiles) {
Winson Chungbe876472014-05-14 14:15:20 -0700194 points.add(new DeviceProfileQuery(p, 0f));
Winson Chungb3800242013-10-24 11:01:54 -0700195 }
Winson Chungbe876472014-05-14 14:15:20 -0700196 DeviceProfile closestProfile = findClosestDeviceProfile(minWidth, minHeight, points);
197
198 // Snap to the closest row count
199 numRows = closestProfile.numRows;
200
201 // Snap to the closest column count
202 numColumns = closestProfile.numColumns;
203
204 // Snap to the closest hotseat size
205 numHotseatIcons = closestProfile.numHotseatIcons;
Winson Chungb3800242013-10-24 11:01:54 -0700206 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
207
Winson Chungbe876472014-05-14 14:15:20 -0700208 // Snap to the closest default layout id
209 defaultLayoutId = closestProfile.defaultLayoutId;
210
211 // Snap to the closest default no all-apps layout id
212 defaultNoAllAppsLayoutId = closestProfile.defaultNoAllAppsLayoutId;
213
Winson Chungb3800242013-10-24 11:01:54 -0700214 // Interpolate the icon size
215 points.clear();
216 for (DeviceProfile p : profiles) {
Winson Chungbe876472014-05-14 14:15:20 -0700217 points.add(new DeviceProfileQuery(p, p.iconSize));
Winson Chungb3800242013-10-24 11:01:54 -0700218 }
219 iconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
220 // AllApps uses the original non-scaled icon size
221 allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm);
222
223 // Interpolate the icon text size
224 points.clear();
225 for (DeviceProfile p : profiles) {
Winson Chungbe876472014-05-14 14:15:20 -0700226 points.add(new DeviceProfileQuery(p, p.iconTextSize));
Winson Chungb3800242013-10-24 11:01:54 -0700227 }
228 iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points);
229 iconDrawablePaddingOriginalPx =
230 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
231 // AllApps uses the original non-scaled icon text size
232 allAppsIconTextSizePx = DynamicGrid.pxFromDp(iconTextSize, dm);
233
234 // Interpolate the hotseat icon size
235 points.clear();
236 for (DeviceProfile p : profiles) {
Winson Chungbe876472014-05-14 14:15:20 -0700237 points.add(new DeviceProfileQuery(p, p.hotseatIconSize));
Winson Chungb3800242013-10-24 11:01:54 -0700238 }
239 // Hotseat
240 hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
241
242 // Calculate the remaining vars
243 updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx);
244 updateAvailableDimensions(context);
245 }
246
247 void addCallback(DeviceProfileCallbacks cb) {
248 mCallbacks.add(cb);
249 cb.onAvailableSizeChanged(this);
250 }
251 void removeCallback(DeviceProfileCallbacks cb) {
252 mCallbacks.remove(cb);
253 }
254
255 private int getDeviceOrientation(Context context) {
256 WindowManager windowManager = (WindowManager)
257 context.getSystemService(Context.WINDOW_SERVICE);
258 Resources resources = context.getResources();
259 DisplayMetrics dm = resources.getDisplayMetrics();
260 Configuration config = resources.getConfiguration();
261 int rotation = windowManager.getDefaultDisplay().getRotation();
262
263 boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) &&
264 (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180);
265 boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) &&
266 (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
267 if (isLandscape || isRotatedPortrait) {
268 return CellLayout.LANDSCAPE;
269 } else {
270 return CellLayout.PORTRAIT;
271 }
272 }
273
274 private void updateAvailableDimensions(Context context) {
275 WindowManager windowManager = (WindowManager)
276 context.getSystemService(Context.WINDOW_SERVICE);
277 Display display = windowManager.getDefaultDisplay();
278 Resources resources = context.getResources();
279 DisplayMetrics dm = resources.getDisplayMetrics();
280 Configuration config = resources.getConfiguration();
281
282 // There are three possible configurations that the dynamic grid accounts for, portrait,
283 // landscape with the nav bar at the bottom, and landscape with the nav bar at the side.
284 // To prevent waiting for fitSystemWindows(), we make the observation that in landscape,
285 // the height is the smallest height (either with the nav bar at the bottom or to the
286 // side) and otherwise, the height is simply the largest possible height for a portrait
287 // device.
288 Point size = new Point();
289 Point smallestSize = new Point();
290 Point largestSize = new Point();
291 display.getSize(size);
292 display.getCurrentSizeRange(smallestSize, largestSize);
293 availableWidthPx = size.x;
294 if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
295 availableHeightPx = smallestSize.y;
296 } else {
297 availableHeightPx = largestSize.y;
298 }
299
300 // Check to see if the icons fit in the new available height. If not, then we need to
301 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700302 float scale = 1f;
303 int drawablePadding = iconDrawablePaddingOriginalPx;
304 updateIconSize(1f, drawablePadding, resources, dm);
305 float usedHeight = (cellHeightPx * numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800306
307 Rect workspacePadding = getWorkspacePadding();
Winson Chungb3800242013-10-24 11:01:54 -0700308 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
309 if (usedHeight > maxHeight) {
310 scale = maxHeight / usedHeight;
311 drawablePadding = 0;
312 }
313 updateIconSize(scale, drawablePadding, resources, dm);
314
315 // Make the callbacks
316 for (DeviceProfileCallbacks cb : mCallbacks) {
317 cb.onAvailableSizeChanged(this);
318 }
319 }
320
321 private void updateIconSize(float scale, int drawablePadding, Resources resources,
322 DisplayMetrics dm) {
323 iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale);
324 iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale);
325 iconDrawablePaddingPx = drawablePadding;
326 hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale);
327
328 // Search Bar
329 searchBarSpaceMaxWidthPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width);
330 searchBarHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
331 searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
Winson Chung69e04ea2013-12-02 14:43:44 -0800332 searchBarSpaceHeightPx = searchBarHeightPx + getSearchBarTopOffset();
Winson Chungb3800242013-10-24 11:01:54 -0700333
334 // Calculate the actual text height
335 Paint textPaint = new Paint();
336 textPaint.setTextSize(iconTextSizePx);
337 FontMetrics fm = textPaint.getFontMetrics();
338 cellWidthPx = iconSizePx;
339 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung59a488a2013-12-10 12:32:14 -0800340 final float scaleDps = resources.getDimensionPixelSize(R.dimen.dragViewScale);
341 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700342
343 // Hotseat
344 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
345 hotseatCellWidthPx = iconSizePx;
346 hotseatCellHeightPx = iconSizePx;
347
348 // Folder
349 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
350 folderCellHeightPx = cellHeightPx + edgeMarginPx;
351 folderBackgroundOffset = -edgeMarginPx;
352 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
353
354 // All Apps
355 Rect padding = getWorkspacePadding(isLandscape ?
356 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
357 int pageIndicatorOffset =
358 resources.getDimensionPixelSize(R.dimen.apps_customize_page_indicator_offset);
359 allAppsCellWidthPx = allAppsIconSizePx;
360 allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + iconTextSizePx;
361 int maxLongEdgeCellCount =
362 resources.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count);
363 int maxShortEdgeCellCount =
364 resources.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count);
365 int minEdgeCellCount =
366 resources.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count);
367 int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount);
368 int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount);
369
370 allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx) /
371 (allAppsCellHeightPx + allAppsCellPaddingPx);
372 allAppsNumRows = Math.max(minEdgeCellCount, Math.min(maxRows, allAppsNumRows));
373 allAppsNumCols = (availableWidthPx) /
374 (allAppsCellWidthPx + allAppsCellPaddingPx);
375 allAppsNumCols = Math.max(minEdgeCellCount, Math.min(maxCols, allAppsNumCols));
376 }
377
378 void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx,
379 int awPx, int ahPx) {
Winson Chung42b3c062013-12-04 12:09:59 -0800380 Configuration configuration = resources.getConfiguration();
381 isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
Winson Chungb3800242013-10-24 11:01:54 -0700382 isTablet = resources.getBoolean(R.bool.is_tablet);
383 isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
Winson Chung6033ceb2014-02-05 12:37:42 -0800384 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
385 isLayoutRtl = (configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
386 } else {
387 isLayoutRtl = false;
388 }
Winson Chungb3800242013-10-24 11:01:54 -0700389 widthPx = wPx;
390 heightPx = hPx;
391 availableWidthPx = awPx;
392 availableHeightPx = ahPx;
393
394 updateAvailableDimensions(context);
395 }
396
397 private float dist(PointF p0, PointF p1) {
398 return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) +
399 (p1.y-p0.y)*(p1.y-p0.y));
400 }
401
402 private float weight(PointF a, PointF b,
403 float pow) {
404 float d = dist(a, b);
405 if (d == 0f) {
406 return Float.POSITIVE_INFINITY;
407 }
408 return (float) (1f / Math.pow(d, pow));
409 }
410
Winson Chungbe876472014-05-14 14:15:20 -0700411 /** Returns the closest device profile given the width and height and a list of profiles */
412 private DeviceProfile findClosestDeviceProfile(float width, float height,
413 ArrayList<DeviceProfileQuery> points) {
414 return findClosestDeviceProfiles(width, height, points).get(0).profile;
415 }
416
417 /** Returns the closest device profiles ordered by closeness to the specified width and height */
418 private ArrayList<DeviceProfileQuery> findClosestDeviceProfiles(float width, float height,
419 ArrayList<DeviceProfileQuery> points) {
420 final PointF xy = new PointF(width, height);
421
422 // Sort the profiles by their closeness to the dimensions
423 ArrayList<DeviceProfileQuery> pointsByNearness = points;
424 Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
425 public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
426 return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
427 }
428 });
429
430 return pointsByNearness;
431 }
432
Winson Chungb3800242013-10-24 11:01:54 -0700433 private float invDistWeightedInterpolate(float width, float height,
434 ArrayList<DeviceProfileQuery> points) {
435 float sum = 0;
436 float weights = 0;
437 float pow = 5;
438 float kNearestNeighbors = 3;
439 final PointF xy = new PointF(width, height);
440
Winson Chungbe876472014-05-14 14:15:20 -0700441 ArrayList<DeviceProfileQuery> pointsByNearness = findClosestDeviceProfiles(width, height,
442 points);
Winson Chungb3800242013-10-24 11:01:54 -0700443
444 for (int i = 0; i < pointsByNearness.size(); ++i) {
445 DeviceProfileQuery p = pointsByNearness.get(i);
446 if (i < kNearestNeighbors) {
447 float w = weight(xy, p.dimens, pow);
448 if (w == Float.POSITIVE_INFINITY) {
449 return p.value;
450 }
451 weights += w;
452 }
453 }
454
455 for (int i = 0; i < pointsByNearness.size(); ++i) {
456 DeviceProfileQuery p = pointsByNearness.get(i);
457 if (i < kNearestNeighbors) {
458 float w = weight(xy, p.dimens, pow);
459 sum += w * p.value / weights;
460 }
461 }
462
463 return sum;
464 }
465
Winson Chung69e04ea2013-12-02 14:43:44 -0800466 /** Returns the search bar top offset */
467 int getSearchBarTopOffset() {
468 if (isTablet() && !isVerticalBarLayout()) {
469 return 4 * edgeMarginPx;
470 } else {
471 return 2 * edgeMarginPx;
472 }
473 }
474
Winson Chungb3800242013-10-24 11:01:54 -0700475 /** Returns the search bar bounds in the current orientation */
476 Rect getSearchBarBounds() {
477 return getSearchBarBounds(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
478 }
479 /** Returns the search bar bounds in the specified orientation */
480 Rect getSearchBarBounds(int orientation) {
481 Rect bounds = new Rect();
482 if (orientation == CellLayout.LANDSCAPE &&
483 transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800484 if (isLayoutRtl) {
485 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
486 availableWidthPx, availableHeightPx - edgeMarginPx);
487 } else {
488 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
489 availableHeightPx - edgeMarginPx);
490 }
Winson Chungb3800242013-10-24 11:01:54 -0700491 } else {
492 if (isTablet()) {
493 // Pad the left and right of the workspace to ensure consistent spacing
494 // between all icons
495 int width = (orientation == CellLayout.LANDSCAPE)
496 ? Math.max(widthPx, heightPx)
497 : Math.min(widthPx, heightPx);
498 // XXX: If the icon size changes across orientations, we will have to take
499 // that into account here too.
500 int gap = (int) ((width - 2 * edgeMarginPx -
501 (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800502 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
503 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700504 searchBarSpaceHeightPx);
505 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800506 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
507 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700508 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
509 defaultWidgetPadding.right), searchBarSpaceHeightPx);
510 }
511 }
512 return bounds;
513 }
514
Winson Chunga6945242014-01-08 14:04:34 -0800515 /** Returns the bounds of the workspace page indicators. */
516 Rect getWorkspacePageIndicatorBounds(Rect insets) {
517 Rect workspacePadding = getWorkspacePadding();
Winson Chung205cd772014-01-15 14:31:59 -0800518 if (isLandscape && transposeLayoutWithOrientation) {
519 if (isLayoutRtl) {
520 return new Rect(workspacePadding.left, workspacePadding.top,
521 workspacePadding.left + pageIndicatorHeightPx,
522 heightPx - workspacePadding.bottom - insets.bottom);
523 } else {
524 int pageIndicatorLeft = widthPx - workspacePadding.right;
525 return new Rect(pageIndicatorLeft, workspacePadding.top,
526 pageIndicatorLeft + pageIndicatorHeightPx,
527 heightPx - workspacePadding.bottom - insets.bottom);
528 }
529 } else {
530 int pageIndicatorTop = heightPx - insets.bottom - workspacePadding.bottom;
531 return new Rect(workspacePadding.left, pageIndicatorTop,
532 widthPx - workspacePadding.right, pageIndicatorTop + pageIndicatorHeightPx);
533 }
Winson Chunga6945242014-01-08 14:04:34 -0800534 }
535
Winson Chungb3800242013-10-24 11:01:54 -0700536 /** Returns the workspace padding in the specified orientation */
537 Rect getWorkspacePadding() {
538 return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
539 }
540 Rect getWorkspacePadding(int orientation) {
541 Rect searchBarBounds = getSearchBarBounds(orientation);
542 Rect padding = new Rect();
543 if (orientation == CellLayout.LANDSCAPE &&
544 transposeLayoutWithOrientation) {
545 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800546 if (isLayoutRtl) {
547 padding.set(hotseatBarHeightPx, edgeMarginPx,
548 searchBarBounds.width(), edgeMarginPx);
549 } else {
550 padding.set(searchBarBounds.width(), edgeMarginPx,
551 hotseatBarHeightPx, edgeMarginPx);
552 }
Winson Chungb3800242013-10-24 11:01:54 -0700553 } else {
554 if (isTablet()) {
555 // Pad the left and right of the workspace to ensure consistent spacing
556 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800557 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chungb3800242013-10-24 11:01:54 -0700558 int width = (orientation == CellLayout.LANDSCAPE)
559 ? Math.max(widthPx, heightPx)
560 : Math.min(widthPx, heightPx);
Winson Chung59a488a2013-12-10 12:32:14 -0800561 int height = (orientation != CellLayout.LANDSCAPE)
562 ? Math.max(widthPx, heightPx)
563 : Math.min(widthPx, heightPx);
564 int paddingTop = searchBarBounds.bottom;
565 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
566 int availableWidth = Math.max(0, width - (int) ((numColumns * cellWidthPx) +
567 (numColumns * gapScale * cellWidthPx)));
568 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
569 - (int) (2 * numRows * cellHeightPx));
570 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
571 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700572 } else {
573 // Pad the top and bottom of the workspace with search/hotseat bar sizes
574 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
575 searchBarBounds.bottom,
576 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
577 hotseatBarHeightPx + pageIndicatorHeightPx);
578 }
579 }
580 return padding;
581 }
582
583 int getWorkspacePageSpacing(int orientation) {
Winson Chung59a488a2013-12-10 12:32:14 -0800584 if ((orientation == CellLayout.LANDSCAPE &&
585 transposeLayoutWithOrientation) || isLargeTablet()) {
Winson Chungb3800242013-10-24 11:01:54 -0700586 // In landscape mode the page spacing is set to the default.
587 return defaultPageSpacingPx;
588 } else {
589 // In portrait, we want the pages spaced such that there is no
590 // overhang of the previous / next page into the current page viewport.
591 // We assume symmetrical padding in portrait mode.
Adam Cohenefb31e32014-01-16 16:07:50 -0800592 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding().left);
Winson Chungb3800242013-10-24 11:01:54 -0700593 }
594 }
595
596 Rect getOverviewModeButtonBarRect() {
597 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
598 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
599 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
600 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
601 }
602
603 float getOverviewModeScale() {
604 Rect workspacePadding = getWorkspacePadding();
605 Rect overviewBar = getOverviewModeButtonBarRect();
606 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
607 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
608 }
609
610 // The rect returned will be extended to below the system ui that covers the workspace
611 Rect getHotseatRect() {
612 if (isVerticalBarLayout()) {
613 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
614 Integer.MAX_VALUE, availableHeightPx);
615 } else {
616 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
617 availableWidthPx, Integer.MAX_VALUE);
618 }
619 }
620
621 int calculateCellWidth(int width, int countX) {
622 return width / countX;
623 }
624 int calculateCellHeight(int height, int countY) {
625 return height / countY;
626 }
627
628 boolean isPhone() {
629 return !isTablet && !isLargeTablet;
630 }
631 boolean isTablet() {
632 return isTablet;
633 }
634 boolean isLargeTablet() {
635 return isLargeTablet;
636 }
637
638 boolean isVerticalBarLayout() {
639 return isLandscape && transposeLayoutWithOrientation;
640 }
641
642 boolean shouldFadeAdjacentWorkspaceScreens() {
643 return isVerticalBarLayout() || isLargeTablet();
644 }
645
Jorim Jaggid017f882014-01-14 17:08:48 -0800646 int getVisibleChildCount(ViewGroup parent) {
647 int visibleChildren = 0;
648 for (int i = 0; i < parent.getChildCount(); i++) {
649 if (parent.getChildAt(i).getVisibility() != View.GONE) {
650 visibleChildren++;
651 }
652 }
653 return visibleChildren;
654 }
655
656 int calculateOverviewModeWidth(int visibleChildCount) {
657 return visibleChildCount * overviewModeBarItemWidthPx +
658 (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
659 }
660
Winson Chungb3800242013-10-24 11:01:54 -0700661 public void layout(Launcher launcher) {
662 FrameLayout.LayoutParams lp;
663 Resources res = launcher.getResources();
664 boolean hasVerticalBarLayout = isVerticalBarLayout();
665
666 // Layout the search bar space
667 View searchBar = launcher.getSearchBar();
668 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
669 if (hasVerticalBarLayout) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800670 // Vertical search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700671 lp.gravity = Gravity.TOP | Gravity.LEFT;
672 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800673 lp.height = LayoutParams.WRAP_CONTENT;
Winson Chungb3800242013-10-24 11:01:54 -0700674 searchBar.setPadding(
675 0, 2 * edgeMarginPx, 0,
676 2 * edgeMarginPx);
Adam Cohen24ce0b32014-01-14 16:18:14 -0800677
678 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
679 targets.setOrientation(LinearLayout.VERTICAL);
Winson Chungb3800242013-10-24 11:01:54 -0700680 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800681 // Horizontal search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700682 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
683 lp.width = searchBarSpaceWidthPx;
684 lp.height = searchBarSpaceHeightPx;
685 searchBar.setPadding(
686 2 * edgeMarginPx,
Winson Chung69e04ea2013-12-02 14:43:44 -0800687 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700688 2 * edgeMarginPx, 0);
689 }
690 searchBar.setLayoutParams(lp);
691
Winson Chungb3800242013-10-24 11:01:54 -0700692 // Layout the voice proxy
693 View voiceButtonProxy = launcher.findViewById(R.id.voice_button_proxy);
694 if (voiceButtonProxy != null) {
695 if (hasVerticalBarLayout) {
696 // TODO: MOVE THIS INTO SEARCH BAR MEASURE
697 } else {
698 lp = (FrameLayout.LayoutParams) voiceButtonProxy.getLayoutParams();
699 lp.gravity = Gravity.TOP | Gravity.END;
700 lp.width = (widthPx - searchBarSpaceWidthPx) / 2 +
701 2 * iconSizePx;
702 lp.height = searchBarSpaceHeightPx;
703 }
704 }
705
706 // Layout the workspace
707 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
708 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
709 lp.gravity = Gravity.CENTER;
710 int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
711 Rect padding = getWorkspacePadding(orientation);
712 workspace.setLayoutParams(lp);
713 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
714 workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
715
716 // Layout the hotseat
717 View hotseat = launcher.findViewById(R.id.hotseat);
718 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
719 if (hasVerticalBarLayout) {
720 // Vertical hotseat
Winson Chung42b3c062013-12-04 12:09:59 -0800721 lp.gravity = Gravity.END;
Winson Chungb3800242013-10-24 11:01:54 -0700722 lp.width = hotseatBarHeightPx;
723 lp.height = LayoutParams.MATCH_PARENT;
724 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
725 } else if (isTablet()) {
Winson Chung59a488a2013-12-10 12:32:14 -0800726 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700727 lp.gravity = Gravity.BOTTOM;
728 lp.width = LayoutParams.MATCH_PARENT;
729 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800730 hotseat.setPadding(edgeMarginPx + padding.left, 0,
731 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700732 2 * edgeMarginPx);
733 } else {
734 // For phones, layout the hotseat without any bottom margin
735 // to ensure that we have space for the folders
736 lp.gravity = Gravity.BOTTOM;
737 lp.width = LayoutParams.MATCH_PARENT;
738 lp.height = hotseatBarHeightPx;
739 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
740 2 * edgeMarginPx, 0);
741 }
742 hotseat.setLayoutParams(lp);
743
744 // Layout the page indicators
745 View pageIndicator = launcher.findViewById(R.id.page_indicator);
746 if (pageIndicator != null) {
747 if (hasVerticalBarLayout) {
748 // Hide the page indicators when we have vertical search/hotseat
749 pageIndicator.setVisibility(View.GONE);
750 } else {
751 // Put the page indicators above the hotseat
752 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
753 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
754 lp.width = LayoutParams.WRAP_CONTENT;
755 lp.height = LayoutParams.WRAP_CONTENT;
756 lp.bottomMargin = hotseatBarHeightPx;
757 pageIndicator.setLayoutParams(lp);
758 }
759 }
760
761 // Layout AllApps
762 AppsCustomizeTabHost host = (AppsCustomizeTabHost)
763 launcher.findViewById(R.id.apps_customize_pane);
764 if (host != null) {
765 // Center the all apps page indicator
766 int pageIndicatorHeight = (int) (pageIndicatorHeightPx * Math.min(1f,
767 (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX)));
768 pageIndicator = host.findViewById(R.id.apps_customize_page_indicator);
769 if (pageIndicator != null) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700770 LinearLayout.LayoutParams lllp = (LinearLayout.LayoutParams) pageIndicator.getLayoutParams();
771 lllp.width = LayoutParams.WRAP_CONTENT;
772 lllp.height = pageIndicatorHeight;
773 pageIndicator.setLayoutParams(lllp);
Winson Chungb3800242013-10-24 11:01:54 -0700774 }
775
776 AppsCustomizePagedView pagedView = (AppsCustomizePagedView)
777 host.findViewById(R.id.apps_customize_pane_content);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700778
779 FrameLayout fakePageContainer = (FrameLayout)
780 host.findViewById(R.id.fake_page_container);
781 FrameLayout fakePage = (FrameLayout) host.findViewById(R.id.fake_page);
782
Winson Chungb3800242013-10-24 11:01:54 -0700783 padding = new Rect();
784 if (pagedView != null) {
785 // Constrain the dimensions of all apps so that it does not span the full width
786 int paddingLR = (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) /
787 (2 * (allAppsNumCols + 1));
788 int paddingTB = (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows)) /
789 (2 * (allAppsNumRows + 1));
790 paddingLR = Math.min(paddingLR, (int)((paddingLR + paddingTB) * 0.75f));
791 paddingTB = Math.min(paddingTB, (int)((paddingLR + paddingTB) * 0.75f));
792 int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR));
793 int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 2;
Winson Chung495f44d2013-12-04 12:51:53 -0800794 // Only adjust the side paddings on landscape phones, or tablets
795 if ((isTablet() || isLandscape) && gridPaddingLR > (allAppsCellWidthPx / 4)) {
Winson Chungb3800242013-10-24 11:01:54 -0700796 padding.left = padding.right = gridPaddingLR;
797 }
Adam Cohen9bfdb762014-07-21 17:44:06 -0700798
Winson Chungb3800242013-10-24 11:01:54 -0700799 // The icons are centered, so we can't just offset by the page indicator height
800 // because the empty space will actually be pageIndicatorHeight + paddingTB
801 padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700802
Winson Chungb3800242013-10-24 11:01:54 -0700803 pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700804 fakePage.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen96bb7982014-07-07 11:58:56 -0700805
806 // Horizontal padding for the whole paged view
Adam Cohen9bfdb762014-07-21 17:44:06 -0700807 int pagedFixedViewPadding =
Adam Cohen96bb7982014-07-07 11:58:56 -0700808 res.getDimensionPixelSize(R.dimen.apps_customize_horizontal_padding);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700809
810 padding.left += pagedFixedViewPadding;
811 padding.right += pagedFixedViewPadding;
812
813 pagedView.setPadding(padding.left, padding.top, padding.right, padding.bottom);
814 fakePageContainer.setPadding(padding.left, padding.top, padding.right, padding.bottom);
815
Winson Chungb3800242013-10-24 11:01:54 -0700816 }
817 }
818
819 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800820 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700821 if (overviewMode != null) {
822 Rect r = getOverviewModeButtonBarRect();
823 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
824 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Jorim Jaggid017f882014-01-14 17:08:48 -0800825 lp.width = Math.min(availableWidthPx,
826 calculateOverviewModeWidth(getVisibleChildCount(overviewMode)));
Winson Chungb3800242013-10-24 11:01:54 -0700827 lp.height = r.height();
828 overviewMode.setLayoutParams(lp);
829 }
830 }
831}