blob: 5f8c011f93aa34739ba8f51a893fcd3f6cb300ff [file] [log] [blame]
Winson Chung5f8afe62013-08-12 16:19:28 -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
Winson Chungf7d45852013-10-10 18:57:15 -070019import android.appwidget.AppWidgetHostView;
20import android.content.ComponentName;
21import android.content.Context;
Winson Chung5f8afe62013-08-12 16:19:28 -070022import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.graphics.Paint;
Winson Chung5f8afe62013-08-12 16:19:28 -070025import android.graphics.Paint.FontMetrics;
Winson Chung6e1c0d32013-10-25 15:24:24 -070026import android.graphics.Point;
Winson Chungf7d45852013-10-10 18:57:15 -070027import android.graphics.PointF;
Winson Chung5f8afe62013-08-12 16:19:28 -070028import android.graphics.Rect;
29import android.util.DisplayMetrics;
30import android.util.TypedValue;
Winson Chung6e1c0d32013-10-25 15:24:24 -070031import android.view.Display;
Winson Chung5f8afe62013-08-12 16:19:28 -070032import android.view.Gravity;
Winson Chung6e1c0d32013-10-25 15:24:24 -070033import android.view.Surface;
Winson Chung5f8afe62013-08-12 16:19:28 -070034import android.view.View;
35import android.view.ViewGroup.LayoutParams;
Winson Chung6e1c0d32013-10-25 15:24:24 -070036import android.view.WindowManager;
Winson Chung5f8afe62013-08-12 16:19:28 -070037import android.widget.FrameLayout;
38
39import java.util.ArrayList;
40import java.util.Collections;
41import java.util.Comparator;
42
43
44class DeviceProfileQuery {
45 float widthDps;
46 float heightDps;
47 float value;
48 PointF dimens;
49
50 DeviceProfileQuery(float w, float h, float v) {
51 widthDps = w;
52 heightDps = h;
53 value = v;
54 dimens = new PointF(w, h);
55 }
56}
57
58class DeviceProfile {
Winson Chung6e1c0d32013-10-25 15:24:24 -070059 public static interface DeviceProfileCallbacks {
60 public void onAvailableSizeChanged(DeviceProfile grid);
61 }
62
Winson Chung5f8afe62013-08-12 16:19:28 -070063 String name;
64 float minWidthDps;
65 float minHeightDps;
66 float numRows;
67 float numColumns;
Winson Chung5f8afe62013-08-12 16:19:28 -070068 float numHotseatIcons;
Winson Chung6e1c0d32013-10-25 15:24:24 -070069 private float iconSize;
70 private float iconTextSize;
71 private int iconDrawablePaddingOriginalPx;
72 private float hotseatIconSize;
Winson Chung5f8afe62013-08-12 16:19:28 -070073
74 boolean isLandscape;
75 boolean isTablet;
76 boolean isLargeTablet;
77 boolean transposeLayoutWithOrientation;
78
Winson Chungf7d45852013-10-10 18:57:15 -070079 int desiredWorkspaceLeftRightMarginPx;
Winson Chung5f8afe62013-08-12 16:19:28 -070080 int edgeMarginPx;
Winson Chungf7d45852013-10-10 18:57:15 -070081 Rect defaultWidgetPadding;
Winson Chung5f8afe62013-08-12 16:19:28 -070082
83 int widthPx;
84 int heightPx;
Winson Chung892c74d2013-08-22 16:15:50 -070085 int availableWidthPx;
86 int availableHeightPx;
Adam Cohen3b185e22013-10-29 14:45:58 -070087 int defaultPageSpacingPx;
Winson Chung6e1c0d32013-10-25 15:24:24 -070088
Winson Chungc82d2622013-11-06 13:23:29 -080089 int overviewModeMinIconZoneHeightPx;
90 int overviewModeMaxIconZoneHeightPx;
91 int overviewModeMaxBarWidthPx;
92 float overviewModeIconZoneRatio;
93 float overviewModeScaleFactor;
94
Winson Chung5f8afe62013-08-12 16:19:28 -070095 int iconSizePx;
96 int iconTextSizePx;
Winson Chung6e1c0d32013-10-25 15:24:24 -070097 int iconDrawablePaddingPx;
Winson Chung5f8afe62013-08-12 16:19:28 -070098 int cellWidthPx;
99 int cellHeightPx;
Winson Chung67ca7e42013-10-31 16:53:19 -0700100 int allAppsIconSizePx;
101 int allAppsIconTextSizePx;
102 int allAppsCellWidthPx;
103 int allAppsCellHeightPx;
104 int allAppsCellPaddingPx;
Winson Chung5f8afe62013-08-12 16:19:28 -0700105 int folderBackgroundOffset;
106 int folderIconSizePx;
107 int folderCellWidthPx;
108 int folderCellHeightPx;
109 int hotseatCellWidthPx;
110 int hotseatCellHeightPx;
111 int hotseatIconSizePx;
112 int hotseatBarHeightPx;
Winson Chungc58497e2013-09-03 17:48:37 -0700113 int hotseatAllAppsRank;
114 int allAppsNumRows;
115 int allAppsNumCols;
Winson Chung5f8afe62013-08-12 16:19:28 -0700116 int searchBarSpaceWidthPx;
117 int searchBarSpaceMaxWidthPx;
118 int searchBarSpaceHeightPx;
119 int searchBarHeightPx;
120 int pageIndicatorHeightPx;
121
Winson Chung6e1c0d32013-10-25 15:24:24 -0700122 private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>();
123
Winson Chung5f8afe62013-08-12 16:19:28 -0700124 DeviceProfile(String n, float w, float h, float r, float c,
125 float is, float its, float hs, float his) {
Winson Chungc58497e2013-09-03 17:48:37 -0700126 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
127 if (!AppsCustomizePagedView.DISABLE_ALL_APPS && hs % 2 == 0) {
128 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
129 }
130
Winson Chung5f8afe62013-08-12 16:19:28 -0700131 name = n;
132 minWidthDps = w;
133 minHeightDps = h;
134 numRows = r;
135 numColumns = c;
136 iconSize = is;
137 iconTextSize = its;
138 numHotseatIcons = hs;
139 hotseatIconSize = his;
140 }
141
Winson Chungf7d45852013-10-10 18:57:15 -0700142 DeviceProfile(Context context,
143 ArrayList<DeviceProfile> profiles,
Winson Chung892c74d2013-08-22 16:15:50 -0700144 float minWidth, float minHeight,
Winson Chung5f8afe62013-08-12 16:19:28 -0700145 int wPx, int hPx,
Winson Chung892c74d2013-08-22 16:15:50 -0700146 int awPx, int ahPx,
Adam Cohen3b185e22013-10-29 14:45:58 -0700147 Resources res) {
148 DisplayMetrics dm = res.getDisplayMetrics();
Winson Chung5f8afe62013-08-12 16:19:28 -0700149 ArrayList<DeviceProfileQuery> points =
150 new ArrayList<DeviceProfileQuery>();
151 transposeLayoutWithOrientation =
Adam Cohen3b185e22013-10-29 14:45:58 -0700152 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700153 minWidthDps = minWidth;
154 minHeightDps = minHeight;
155
Winson Chungf7d45852013-10-10 18:57:15 -0700156 ComponentName cn = new ComponentName(context.getPackageName(),
157 this.getClass().getName());
158 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen3b185e22013-10-29 14:45:58 -0700159 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Winson Chungf7d45852013-10-10 18:57:15 -0700160 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
Adam Cohen3b185e22013-10-29 14:45:58 -0700161 pageIndicatorHeightPx =
162 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
163 defaultPageSpacingPx =
164 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
Winson Chung67ca7e42013-10-31 16:53:19 -0700165 allAppsCellPaddingPx =
166 res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_padding);
Winson Chungc82d2622013-11-06 13:23:29 -0800167 overviewModeMinIconZoneHeightPx =
168 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
169 overviewModeMaxIconZoneHeightPx =
170 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
171 overviewModeMaxBarWidthPx =
172 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_max_width);
173 overviewModeIconZoneRatio =
174 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
175 overviewModeScaleFactor =
176 res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f;
Winson Chung5f8afe62013-08-12 16:19:28 -0700177
178 // Interpolate the rows
179 for (DeviceProfile p : profiles) {
180 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numRows));
181 }
182 numRows = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
183 // Interpolate the columns
184 points.clear();
185 for (DeviceProfile p : profiles) {
186 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numColumns));
187 }
188 numColumns = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
Winson Chung6e1c0d32013-10-25 15:24:24 -0700189 // Interpolate the hotseat length
190 points.clear();
191 for (DeviceProfile p : profiles) {
192 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numHotseatIcons));
193 }
194 numHotseatIcons = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
195 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
196
Winson Chung5f8afe62013-08-12 16:19:28 -0700197 // Interpolate the icon size
198 points.clear();
199 for (DeviceProfile p : profiles) {
200 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconSize));
201 }
202 iconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
Winson Chung67ca7e42013-10-31 16:53:19 -0700203 // AllApps uses the original non-scaled icon size
204 allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm);
Winson Chung892c74d2013-08-22 16:15:50 -0700205
Winson Chung5f8afe62013-08-12 16:19:28 -0700206 // Interpolate the icon text size
207 points.clear();
208 for (DeviceProfile p : profiles) {
209 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconTextSize));
210 }
211 iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points);
Adam Cohen3b185e22013-10-29 14:45:58 -0700212 iconDrawablePaddingOriginalPx =
213 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Winson Chung67ca7e42013-10-31 16:53:19 -0700214 // AllApps uses the original non-scaled icon text size
215 allAppsIconTextSizePx = DynamicGrid.pxFromDp(iconTextSize, dm);
Winson Chung892c74d2013-08-22 16:15:50 -0700216
Winson Chung5f8afe62013-08-12 16:19:28 -0700217 // Interpolate the hotseat icon size
218 points.clear();
219 for (DeviceProfile p : profiles) {
220 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.hotseatIconSize));
221 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700222 // Hotseat
223 hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
Winson Chung892c74d2013-08-22 16:15:50 -0700224
Winson Chung6e1c0d32013-10-25 15:24:24 -0700225 // Calculate the remaining vars
Adam Cohen3b185e22013-10-29 14:45:58 -0700226 updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700227 updateAvailableDimensions(context);
228 }
229
230 void addCallback(DeviceProfileCallbacks cb) {
231 mCallbacks.add(cb);
232 cb.onAvailableSizeChanged(this);
233 }
234 void removeCallback(DeviceProfileCallbacks cb) {
235 mCallbacks.remove(cb);
236 }
237
238 private int getDeviceOrientation(Context context) {
239 WindowManager windowManager = (WindowManager)
240 context.getSystemService(Context.WINDOW_SERVICE);
241 Resources resources = context.getResources();
242 DisplayMetrics dm = resources.getDisplayMetrics();
243 Configuration config = resources.getConfiguration();
244 int rotation = windowManager.getDefaultDisplay().getRotation();
245
246 boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) &&
247 (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180);
248 boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) &&
249 (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
250 if (isLandscape || isRotatedPortrait) {
251 return CellLayout.LANDSCAPE;
252 } else {
253 return CellLayout.PORTRAIT;
254 }
255 }
256
257 private void updateAvailableDimensions(Context context) {
258 WindowManager windowManager = (WindowManager)
259 context.getSystemService(Context.WINDOW_SERVICE);
260 Display display = windowManager.getDefaultDisplay();
261 Resources resources = context.getResources();
262 DisplayMetrics dm = resources.getDisplayMetrics();
263 Configuration config = resources.getConfiguration();
264
265 // There are three possible configurations that the dynamic grid accounts for, portrait,
266 // landscape with the nav bar at the bottom, and landscape with the nav bar at the side.
267 // To prevent waiting for fitSystemWindows(), we make the observation that in landscape,
268 // the height is the smallest height (either with the nav bar at the bottom or to the
269 // side) and otherwise, the height is simply the largest possible height for a portrait
270 // device.
271 Point size = new Point();
272 Point smallestSize = new Point();
273 Point largestSize = new Point();
274 display.getSize(size);
275 display.getCurrentSizeRange(smallestSize, largestSize);
276 availableWidthPx = size.x;
277 if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
278 availableHeightPx = smallestSize.y;
279 } else {
280 availableHeightPx = largestSize.y;
281 }
282
283 // Check to see if the icons fit in the new available height. If not, then we need to
284 // shrink the icon size.
285 Rect workspacePadding = getWorkspacePadding();
286 float scale = 1f;
287 int drawablePadding = iconDrawablePaddingOriginalPx;
288 updateIconSize(1f, drawablePadding, resources, dm);
289 float usedHeight = (cellHeightPx * numRows);
290 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
291 if (usedHeight > maxHeight) {
292 scale = maxHeight / usedHeight;
293 drawablePadding = 0;
294 }
295 updateIconSize(scale, drawablePadding, resources, dm);
296
297 // Make the callbacks
298 for (DeviceProfileCallbacks cb : mCallbacks) {
299 cb.onAvailableSizeChanged(this);
300 }
301 }
302
303 private void updateIconSize(float scale, int drawablePadding, Resources resources,
304 DisplayMetrics dm) {
305 iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale);
306 iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale);
307 iconDrawablePaddingPx = drawablePadding;
308 hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale);
Winson Chung5f8afe62013-08-12 16:19:28 -0700309
310 // Search Bar
311 searchBarSpaceMaxWidthPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width);
312 searchBarHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
313 searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
314 searchBarSpaceHeightPx = searchBarHeightPx + 2 * edgeMarginPx;
315
316 // Calculate the actual text height
317 Paint textPaint = new Paint();
318 textPaint.setTextSize(iconTextSizePx);
319 FontMetrics fm = textPaint.getFontMetrics();
320 cellWidthPx = iconSizePx;
Winson Chung6e1c0d32013-10-25 15:24:24 -0700321 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung892c74d2013-08-22 16:15:50 -0700322
323 // Hotseat
324 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
325 hotseatCellWidthPx = iconSizePx;
326 hotseatCellHeightPx = iconSizePx;
327
Winson Chung5f8afe62013-08-12 16:19:28 -0700328 // Folder
329 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
Winson Chung6e1c0d32013-10-25 15:24:24 -0700330 folderCellHeightPx = cellHeightPx + edgeMarginPx;
Winson Chung5f8afe62013-08-12 16:19:28 -0700331 folderBackgroundOffset = -edgeMarginPx;
332 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Winson Chung6e1c0d32013-10-25 15:24:24 -0700333
334 // All Apps
335 Rect padding = getWorkspacePadding(isLandscape ?
336 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
337 int pageIndicatorOffset =
338 resources.getDimensionPixelSize(R.dimen.apps_customize_page_indicator_offset);
Winson Chung67ca7e42013-10-31 16:53:19 -0700339 allAppsCellWidthPx = allAppsIconSizePx;
340 allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + iconTextSizePx;
341 int maxLongEdgeCellCount =
342 resources.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count);
343 int maxShortEdgeCellCount =
344 resources.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count);
345 int minEdgeCellCount =
346 resources.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count);
347 int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount);
348 int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount);
349
350 allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx) /
351 (allAppsCellHeightPx + allAppsCellPaddingPx);
352 allAppsNumRows = Math.max(minEdgeCellCount, Math.min(maxRows, allAppsNumRows));
353 allAppsNumCols = (availableWidthPx) /
354 (allAppsCellWidthPx + allAppsCellPaddingPx);
355 allAppsNumCols = Math.max(minEdgeCellCount, Math.min(maxCols, allAppsNumCols));
Winson Chung5f8afe62013-08-12 16:19:28 -0700356 }
357
Winson Chung6e1c0d32013-10-25 15:24:24 -0700358 void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx,
Winson Chung892c74d2013-08-22 16:15:50 -0700359 int awPx, int ahPx) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700360 isLandscape = (resources.getConfiguration().orientation ==
361 Configuration.ORIENTATION_LANDSCAPE);
362 isTablet = resources.getBoolean(R.bool.is_tablet);
363 isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
364 widthPx = wPx;
365 heightPx = hPx;
Winson Chung892c74d2013-08-22 16:15:50 -0700366 availableWidthPx = awPx;
367 availableHeightPx = ahPx;
Winson Chungc58497e2013-09-03 17:48:37 -0700368
Winson Chung6e1c0d32013-10-25 15:24:24 -0700369 updateAvailableDimensions(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700370 }
371
372 private float dist(PointF p0, PointF p1) {
373 return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) +
374 (p1.y-p0.y)*(p1.y-p0.y));
375 }
376
377 private float weight(PointF a, PointF b,
378 float pow) {
379 float d = dist(a, b);
380 if (d == 0f) {
381 return Float.POSITIVE_INFINITY;
382 }
383 return (float) (1f / Math.pow(d, pow));
384 }
385
386 private float invDistWeightedInterpolate(float width, float height,
387 ArrayList<DeviceProfileQuery> points) {
388 float sum = 0;
389 float weights = 0;
390 float pow = 5;
391 float kNearestNeighbors = 3;
392 final PointF xy = new PointF(width, height);
393
394 ArrayList<DeviceProfileQuery> pointsByNearness = points;
395 Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
396 public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
397 return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
398 }
399 });
400
401 for (int i = 0; i < pointsByNearness.size(); ++i) {
402 DeviceProfileQuery p = pointsByNearness.get(i);
403 if (i < kNearestNeighbors) {
404 float w = weight(xy, p.dimens, pow);
405 if (w == Float.POSITIVE_INFINITY) {
406 return p.value;
407 }
408 weights += w;
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 sum += w * p.value / weights;
417 }
418 }
419
420 return sum;
421 }
422
Winson Chung6e1c0d32013-10-25 15:24:24 -0700423 Rect getWorkspacePadding() {
424 return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
425 }
Adam Cohen3b185e22013-10-29 14:45:58 -0700426
Winson Chung5f8afe62013-08-12 16:19:28 -0700427 Rect getWorkspacePadding(int orientation) {
428 Rect padding = new Rect();
Winson Chungfe411c82013-09-26 16:07:17 -0700429 if (orientation == CellLayout.LANDSCAPE &&
430 transposeLayoutWithOrientation) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700431 // Pad the left and right of the workspace with search/hotseat bar sizes
432 padding.set(searchBarSpaceHeightPx, edgeMarginPx,
433 hotseatBarHeightPx, edgeMarginPx);
434 } else {
435 if (isTablet()) {
436 // Pad the left and right of the workspace to ensure consistent spacing
437 // between all icons
438 int width = (orientation == CellLayout.LANDSCAPE)
439 ? Math.max(widthPx, heightPx)
440 : Math.min(widthPx, heightPx);
441 // XXX: If the icon size changes across orientations, we will have to take
442 // that into account here too.
443 int gap = (int) ((width - 2 * edgeMarginPx -
444 (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
445 padding.set(edgeMarginPx + gap,
446 searchBarSpaceHeightPx,
447 edgeMarginPx + gap,
448 hotseatBarHeightPx + pageIndicatorHeightPx);
449 } else {
450 // Pad the top and bottom of the workspace with search/hotseat bar sizes
Winson Chungf7d45852013-10-10 18:57:15 -0700451 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
Winson Chung5f8afe62013-08-12 16:19:28 -0700452 searchBarSpaceHeightPx,
Winson Chungf7d45852013-10-10 18:57:15 -0700453 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
Winson Chung5f8afe62013-08-12 16:19:28 -0700454 hotseatBarHeightPx + pageIndicatorHeightPx);
455 }
456 }
457 return padding;
458 }
459
Adam Cohen3b185e22013-10-29 14:45:58 -0700460 int getWorkspacePageSpacing(int orientation) {
461 if (orientation == CellLayout.LANDSCAPE &&
462 transposeLayoutWithOrientation) {
463 // In landscape mode the page spacing is set to the default.
464 return defaultPageSpacingPx;
465 } else {
466 // In portrait, we want the pages spaced such that there is no
467 // overhang of the previous / next page into the current page viewport.
468 // We assume symmetrical padding in portrait mode.
Adam Cohena1648442013-11-07 04:11:15 -0800469 return 2 * getWorkspacePadding().left;
Adam Cohen3b185e22013-10-29 14:45:58 -0700470 }
471 }
472
Winson Chungc82d2622013-11-06 13:23:29 -0800473 Rect getOverviewModeButtonBarRect() {
474 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
475 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
476 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
477 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
478 }
479
480 float getOverviewModeScale() {
481 Rect workspacePadding = getWorkspacePadding();
482 Rect overviewBar = getOverviewModeButtonBarRect();
483 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
484 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
485 }
486
Winson Chungabedd9f2013-09-24 15:41:09 -0700487 // The rect returned will be extended to below the system ui that covers the workspace
488 Rect getHotseatRect() {
489 if (isVerticalBarLayout()) {
490 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
491 Integer.MAX_VALUE, availableHeightPx);
492 } else {
493 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
494 availableWidthPx, Integer.MAX_VALUE);
495 }
496 }
497
Winson Chung5f8afe62013-08-12 16:19:28 -0700498 int calculateCellWidth(int width, int countX) {
499 return width / countX;
500 }
501 int calculateCellHeight(int height, int countY) {
502 return height / countY;
503 }
504
Winson Chungaf40f202013-09-18 18:26:31 -0700505 boolean isPhone() {
506 return !isTablet && !isLargeTablet;
507 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700508 boolean isTablet() {
509 return isTablet;
510 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700511 boolean isLargeTablet() {
512 return isLargeTablet;
513 }
514
Winson Chungabedd9f2013-09-24 15:41:09 -0700515 boolean isVerticalBarLayout() {
516 return isLandscape && transposeLayoutWithOrientation;
517 }
518
Adam Cohen3b185e22013-10-29 14:45:58 -0700519 boolean shouldFadeAdjacentWorkspaceScreens() {
520 return isVerticalBarLayout() || isLargeTablet();
521 }
522
Winson Chung5f8afe62013-08-12 16:19:28 -0700523 public void layout(Launcher launcher) {
524 FrameLayout.LayoutParams lp;
525 Resources res = launcher.getResources();
Winson Chungabedd9f2013-09-24 15:41:09 -0700526 boolean hasVerticalBarLayout = isVerticalBarLayout();
Winson Chung5f8afe62013-08-12 16:19:28 -0700527
528 // Layout the search bar space
Winson Chung54e65132013-09-27 11:44:58 -0700529 View searchBar = launcher.getSearchBar();
530 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
Winson Chung5f8afe62013-08-12 16:19:28 -0700531 if (hasVerticalBarLayout) {
532 // Vertical search bar
533 lp.gravity = Gravity.TOP | Gravity.LEFT;
534 lp.width = searchBarSpaceHeightPx;
535 lp.height = LayoutParams.MATCH_PARENT;
Winson Chung54e65132013-09-27 11:44:58 -0700536 searchBar.setPadding(
Winson Chung5f8afe62013-08-12 16:19:28 -0700537 0, 2 * edgeMarginPx, 0,
538 2 * edgeMarginPx);
539 } else {
540 // Horizontal search bar
541 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
542 lp.width = searchBarSpaceWidthPx;
543 lp.height = searchBarSpaceHeightPx;
Winson Chung54e65132013-09-27 11:44:58 -0700544 searchBar.setPadding(
Winson Chung5f8afe62013-08-12 16:19:28 -0700545 2 * edgeMarginPx,
546 2 * edgeMarginPx,
547 2 * edgeMarginPx, 0);
548 }
Winson Chung54e65132013-09-27 11:44:58 -0700549 searchBar.setLayoutParams(lp);
Winson Chung5f8afe62013-08-12 16:19:28 -0700550
551 // Layout the search bar
Winson Chung54e65132013-09-27 11:44:58 -0700552 View qsbBar = launcher.getQsbBar();
553 LayoutParams vglp = qsbBar.getLayoutParams();
554 vglp.width = LayoutParams.MATCH_PARENT;
555 vglp.height = LayoutParams.MATCH_PARENT;
556 qsbBar.setLayoutParams(vglp);
Winson Chung5f8afe62013-08-12 16:19:28 -0700557
558 // Layout the voice proxy
559 View voiceButtonProxy = launcher.findViewById(R.id.voice_button_proxy);
560 if (voiceButtonProxy != null) {
561 if (hasVerticalBarLayout) {
562 // TODO: MOVE THIS INTO SEARCH BAR MEASURE
563 } else {
564 lp = (FrameLayout.LayoutParams) voiceButtonProxy.getLayoutParams();
565 lp.gravity = Gravity.TOP | Gravity.END;
566 lp.width = (widthPx - searchBarSpaceWidthPx) / 2 +
567 2 * iconSizePx;
568 lp.height = searchBarSpaceHeightPx;
569 }
570 }
571
572 // Layout the workspace
Adam Cohen3b185e22013-10-29 14:45:58 -0700573 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
Winson Chung5f8afe62013-08-12 16:19:28 -0700574 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
575 lp.gravity = Gravity.CENTER;
Adam Cohen3b185e22013-10-29 14:45:58 -0700576 int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
577 Rect padding = getWorkspacePadding(orientation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700578 workspace.setLayoutParams(lp);
Adam Cohen3b185e22013-10-29 14:45:58 -0700579 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
580 workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
Winson Chung5f8afe62013-08-12 16:19:28 -0700581
582 // Layout the hotseat
583 View hotseat = launcher.findViewById(R.id.hotseat);
584 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
585 if (hasVerticalBarLayout) {
586 // Vertical hotseat
587 lp.gravity = Gravity.RIGHT;
588 lp.width = hotseatBarHeightPx;
589 lp.height = LayoutParams.MATCH_PARENT;
Winson Chung6e1c0d32013-10-25 15:24:24 -0700590 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700591 } else if (isTablet()) {
592 // Pad the hotseat with the grid gap calculated above
593 int gridGap = (int) ((widthPx - 2 * edgeMarginPx -
594 (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
595 int gridWidth = (int) ((numColumns * cellWidthPx) +
596 ((numColumns - 1) * gridGap));
597 int hotseatGap = (int) Math.max(0,
598 (gridWidth - (numHotseatIcons * hotseatCellWidthPx))
599 / (numHotseatIcons - 1));
600 lp.gravity = Gravity.BOTTOM;
601 lp.width = LayoutParams.MATCH_PARENT;
602 lp.height = hotseatBarHeightPx;
603 hotseat.setPadding(2 * edgeMarginPx + gridGap + hotseatGap, 0,
604 2 * edgeMarginPx + gridGap + hotseatGap,
605 2 * edgeMarginPx);
606 } else {
607 // For phones, layout the hotseat without any bottom margin
608 // to ensure that we have space for the folders
609 lp.gravity = Gravity.BOTTOM;
610 lp.width = LayoutParams.MATCH_PARENT;
611 lp.height = hotseatBarHeightPx;
Winson Chung2d75f122013-09-23 16:53:31 -0700612 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
Winson Chung5f8afe62013-08-12 16:19:28 -0700613 2 * edgeMarginPx, 0);
614 }
615 hotseat.setLayoutParams(lp);
616
617 // Layout the page indicators
618 View pageIndicator = launcher.findViewById(R.id.page_indicator);
619 if (pageIndicator != null) {
620 if (hasVerticalBarLayout) {
621 // Hide the page indicators when we have vertical search/hotseat
622 pageIndicator.setVisibility(View.GONE);
623 } else {
624 // Put the page indicators above the hotseat
625 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
626 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
627 lp.width = LayoutParams.WRAP_CONTENT;
Winson Chungf4bd2362013-10-07 17:11:27 -0700628 lp.height = LayoutParams.WRAP_CONTENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700629 lp.bottomMargin = hotseatBarHeightPx;
630 pageIndicator.setLayoutParams(lp);
631 }
632 }
Winson Chung67ca7e42013-10-31 16:53:19 -0700633
Winson Chungc82d2622013-11-06 13:23:29 -0800634 // Layout AllApps
Winson Chung67ca7e42013-10-31 16:53:19 -0700635 AppsCustomizeTabHost host = (AppsCustomizeTabHost)
636 launcher.findViewById(R.id.apps_customize_pane);
637 if (host != null) {
638 // Center the all apps page indicator
639 int pageIndicatorHeight = (int) (pageIndicatorHeightPx * Math.min(1f,
640 (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX)));
641 pageIndicator = host.findViewById(R.id.apps_customize_page_indicator);
642 if (pageIndicator != null) {
643 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
644 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
645 lp.width = LayoutParams.WRAP_CONTENT;
646 lp.height = pageIndicatorHeight;
647 pageIndicator.setLayoutParams(lp);
648 }
649
650 AppsCustomizePagedView pagedView = (AppsCustomizePagedView)
651 host.findViewById(R.id.apps_customize_pane_content);
652 padding = new Rect();
653 if (pagedView != null) {
654 // Constrain the dimensions of all apps so that it does not span the full width
655 int paddingLR = (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) /
656 (2 * (allAppsNumCols + 1));
657 int paddingTB = (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows)) /
658 (2 * (allAppsNumRows + 1));
659 paddingLR = Math.min(paddingLR, (int)((paddingLR + paddingTB) * 0.75f));
660 paddingTB = Math.min(paddingTB, (int)((paddingLR + paddingTB) * 0.75f));
661 int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR));
662 int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 2;
663 if (gridPaddingLR > (allAppsCellWidthPx / 4)) {
664 padding.left = padding.right = gridPaddingLR;
665 }
666 // The icons are centered, so we can't just offset by the page indicator height
667 // because the empty space will actually be pageIndicatorHeight + paddingTB
668 padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB);
669 pagedView.setAllAppsPadding(padding);
670 pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight);
671 }
672 }
Winson Chungc82d2622013-11-06 13:23:29 -0800673
674 // Layout the Overview Mode
675 View overviewMode = launcher.getOverviewPanel();
676 if (overviewMode != null) {
677 Rect r = getOverviewModeButtonBarRect();
678 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
679 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
680 lp.width = Math.min(availableWidthPx, overviewModeMaxBarWidthPx);
681 lp.height = r.height();
682 overviewMode.setLayoutParams(lp);
683 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700684 }
685}
686
687public class DynamicGrid {
688 @SuppressWarnings("unused")
689 private static final String TAG = "DynamicGrid";
690
691 private DeviceProfile mProfile;
692 private float mMinWidth;
693 private float mMinHeight;
694
Winson Chung67ca7e42013-10-31 16:53:19 -0700695 // This is a static that we use for the default icon size on a 4/5-inch phone
696 static float DEFAULT_ICON_SIZE_DP = 60;
697 static float DEFAULT_ICON_SIZE_PX = 0;
698
Winson Chung892c74d2013-08-22 16:15:50 -0700699 public static float dpiFromPx(int size, DisplayMetrics metrics){
Winson Chung5f8afe62013-08-12 16:19:28 -0700700 float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT;
Winson Chung892c74d2013-08-22 16:15:50 -0700701 return (size / densityRatio);
702 }
703 public static int pxFromDp(float size, DisplayMetrics metrics) {
704 return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
705 size, metrics));
706 }
707 public static int pxFromSp(float size, DisplayMetrics metrics) {
708 return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
709 size, metrics));
Winson Chung5f8afe62013-08-12 16:19:28 -0700710 }
711
Winson Chungf7d45852013-10-10 18:57:15 -0700712 public DynamicGrid(Context context, Resources resources,
713 int minWidthPx, int minHeightPx,
Winson Chung892c74d2013-08-22 16:15:50 -0700714 int widthPx, int heightPx,
715 int awPx, int ahPx) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700716 DisplayMetrics dm = resources.getDisplayMetrics();
717 ArrayList<DeviceProfile> deviceProfiles =
718 new ArrayList<DeviceProfile>();
Winson Chungc58497e2013-09-03 17:48:37 -0700719 boolean hasAA = !AppsCustomizePagedView.DISABLE_ALL_APPS;
Winson Chung67ca7e42013-10-31 16:53:19 -0700720 DEFAULT_ICON_SIZE_PX = pxFromDp(DEFAULT_ICON_SIZE_DP, dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700721 // Our phone profiles include the bar sizes in each orientation
722 deviceProfiles.add(new DeviceProfile("Super Short Stubby",
Winson Chung2651d132013-10-01 16:28:15 -0700723 255, 300, 2, 3, 48, 13, (hasAA ? 5 : 4), 48));
Winson Chung5f8afe62013-08-12 16:19:28 -0700724 deviceProfiles.add(new DeviceProfile("Shorter Stubby",
Winson Chung2651d132013-10-01 16:28:15 -0700725 255, 400, 3, 3, 48, 13, (hasAA ? 5 : 4), 48));
Winson Chung5f8afe62013-08-12 16:19:28 -0700726 deviceProfiles.add(new DeviceProfile("Short Stubby",
Winson Chung2651d132013-10-01 16:28:15 -0700727 275, 420, 3, 4, 48, 13, (hasAA ? 5 : 4), 48));
Winson Chung5f8afe62013-08-12 16:19:28 -0700728 deviceProfiles.add(new DeviceProfile("Stubby",
Winson Chung2651d132013-10-01 16:28:15 -0700729 255, 450, 3, 4, 48, 13, (hasAA ? 5 : 4), 48));
Winson Chung5f8afe62013-08-12 16:19:28 -0700730 deviceProfiles.add(new DeviceProfile("Nexus S",
Winson Chung2651d132013-10-01 16:28:15 -0700731 296, 491.33f, 4, 4, 48, 13, (hasAA ? 5 : 4), 48));
Winson Chung5f8afe62013-08-12 16:19:28 -0700732 deviceProfiles.add(new DeviceProfile("Nexus 4",
Winson Chung67ca7e42013-10-31 16:53:19 -0700733 359, 518, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasAA ? 5 : 4), 56));
Winson Chung5f8afe62013-08-12 16:19:28 -0700734 // The tablet profile is odd in that the landscape orientation
735 // also includes the nav bar on the side
736 deviceProfiles.add(new DeviceProfile("Nexus 7",
737 575, 904, 6, 6, 72, 14.4f, 7, 60));
738 // Larger tablet profiles always have system bars on the top & bottom
739 deviceProfiles.add(new DeviceProfile("Nexus 10",
740 727, 1207, 5, 8, 80, 14.4f, 9, 64));
741 /*
742 deviceProfiles.add(new DeviceProfile("Nexus 7",
743 600, 960, 5, 5, 72, 14.4f, 5, 60));
744 deviceProfiles.add(new DeviceProfile("Nexus 10",
Winson Chungc58497e2013-09-03 17:48:37 -0700745 800, 1280, 5, 5, 80, 14.4f, (hasAA ? 7 : 6), 64));
Winson Chung5f8afe62013-08-12 16:19:28 -0700746 */
747 deviceProfiles.add(new DeviceProfile("20-inch Tablet",
748 1527, 2527, 7, 7, 100, 20, 7, 72));
749 mMinWidth = dpiFromPx(minWidthPx, dm);
750 mMinHeight = dpiFromPx(minHeightPx, dm);
Winson Chungf7d45852013-10-10 18:57:15 -0700751 mProfile = new DeviceProfile(context, deviceProfiles,
Winson Chung892c74d2013-08-22 16:15:50 -0700752 mMinWidth, mMinHeight,
Winson Chung5f8afe62013-08-12 16:19:28 -0700753 widthPx, heightPx,
Winson Chung892c74d2013-08-22 16:15:50 -0700754 awPx, ahPx,
Winson Chung5f8afe62013-08-12 16:19:28 -0700755 resources);
756 }
757
758 DeviceProfile getDeviceProfile() {
759 return mProfile;
760 }
761
762 public String toString() {
763 return "-------- DYNAMIC GRID ------- \n" +
764 "Wd: " + mProfile.minWidthDps + ", Hd: " + mProfile.minHeightDps +
765 ", W: " + mProfile.widthPx + ", H: " + mProfile.heightPx +
766 " [r: " + mProfile.numRows + ", c: " + mProfile.numColumns +
Winson Chung6e1c0d32013-10-25 15:24:24 -0700767 ", is: " + mProfile.iconSizePx + ", its: " + mProfile.iconTextSizePx +
Winson Chung5f8afe62013-08-12 16:19:28 -0700768 ", cw: " + mProfile.cellWidthPx + ", ch: " + mProfile.cellHeightPx +
769 ", hc: " + mProfile.numHotseatIcons + ", his: " + mProfile.hotseatIconSizePx + "]";
770 }
771}