blob: 16af648141aa012e5b2fd4cdbd808feaf2ba7724 [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
19import android.content.res.Configuration;
20import android.content.res.Resources;
21import android.graphics.Paint;
22import android.graphics.PointF;
23import android.graphics.Paint.FontMetrics;
24import android.graphics.Rect;
25import android.util.DisplayMetrics;
26import android.util.TypedValue;
27import android.view.Gravity;
28import android.view.View;
29import android.view.ViewGroup.LayoutParams;
30import android.widget.FrameLayout;
31
32import java.util.ArrayList;
33import java.util.Collections;
34import java.util.Comparator;
35
36
37class DeviceProfileQuery {
38 float widthDps;
39 float heightDps;
40 float value;
41 PointF dimens;
42
43 DeviceProfileQuery(float w, float h, float v) {
44 widthDps = w;
45 heightDps = h;
46 value = v;
47 dimens = new PointF(w, h);
48 }
49}
50
51class DeviceProfile {
52 String name;
53 float minWidthDps;
54 float minHeightDps;
55 float numRows;
56 float numColumns;
57 float iconSize;
58 float iconTextSize;
59 float numHotseatIcons;
60 float hotseatIconSize;
61
62 boolean isLandscape;
63 boolean isTablet;
64 boolean isLargeTablet;
65 boolean transposeLayoutWithOrientation;
66
67 int edgeMarginPx;
68
69 int widthPx;
70 int heightPx;
Winson Chung892c74d2013-08-22 16:15:50 -070071 int availableWidthPx;
72 int availableHeightPx;
Winson Chung5f8afe62013-08-12 16:19:28 -070073 int iconSizePx;
74 int iconTextSizePx;
75 int cellWidthPx;
76 int cellHeightPx;
77 int folderBackgroundOffset;
78 int folderIconSizePx;
79 int folderCellWidthPx;
80 int folderCellHeightPx;
81 int hotseatCellWidthPx;
82 int hotseatCellHeightPx;
83 int hotseatIconSizePx;
84 int hotseatBarHeightPx;
85 int searchBarSpaceWidthPx;
86 int searchBarSpaceMaxWidthPx;
87 int searchBarSpaceHeightPx;
88 int searchBarHeightPx;
89 int pageIndicatorHeightPx;
90
91 DeviceProfile(String n, float w, float h, float r, float c,
92 float is, float its, float hs, float his) {
93 name = n;
94 minWidthDps = w;
95 minHeightDps = h;
96 numRows = r;
97 numColumns = c;
98 iconSize = is;
99 iconTextSize = its;
100 numHotseatIcons = hs;
101 hotseatIconSize = his;
102 }
103
104 DeviceProfile(ArrayList<DeviceProfile> profiles,
Winson Chung892c74d2013-08-22 16:15:50 -0700105 float minWidth, float minHeight,
Winson Chung5f8afe62013-08-12 16:19:28 -0700106 int wPx, int hPx,
Winson Chung892c74d2013-08-22 16:15:50 -0700107 int awPx, int ahPx,
Winson Chung5f8afe62013-08-12 16:19:28 -0700108 Resources resources) {
109 DisplayMetrics dm = resources.getDisplayMetrics();
110 ArrayList<DeviceProfileQuery> points =
111 new ArrayList<DeviceProfileQuery>();
112 transposeLayoutWithOrientation =
113 resources.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung5f8afe62013-08-12 16:19:28 -0700114 minWidthDps = minWidth;
115 minHeightDps = minHeight;
116
117 edgeMarginPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
118 pageIndicatorHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
119
120 // Interpolate the rows
121 for (DeviceProfile p : profiles) {
122 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numRows));
123 }
124 numRows = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
125 // Interpolate the columns
126 points.clear();
127 for (DeviceProfile p : profiles) {
128 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numColumns));
129 }
130 numColumns = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
131 // Interpolate the icon size
132 points.clear();
133 for (DeviceProfile p : profiles) {
134 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconSize));
135 }
136 iconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
Winson Chung892c74d2013-08-22 16:15:50 -0700137 iconSizePx = DynamicGrid.pxFromDp(iconSize, dm);
138
Winson Chung5f8afe62013-08-12 16:19:28 -0700139 // Interpolate the icon text size
140 points.clear();
141 for (DeviceProfile p : profiles) {
142 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconTextSize));
143 }
144 iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points);
Winson Chung892c74d2013-08-22 16:15:50 -0700145 iconTextSizePx = DynamicGrid.pxFromSp(iconTextSize, dm);
146
Winson Chung5f8afe62013-08-12 16:19:28 -0700147 // Interpolate the hotseat size
148 points.clear();
149 for (DeviceProfile p : profiles) {
150 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numHotseatIcons));
151 }
152 numHotseatIcons = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
153 // Interpolate the hotseat icon size
154 points.clear();
155 for (DeviceProfile p : profiles) {
156 points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.hotseatIconSize));
157 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700158 // Hotseat
159 hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
Winson Chung892c74d2013-08-22 16:15:50 -0700160 hotseatIconSizePx = DynamicGrid.pxFromDp(hotseatIconSize, dm);
161
162 // Calculate other vars based on Configuration
163 updateFromConfiguration(resources, wPx, hPx, awPx, ahPx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700164
165 // Search Bar
166 searchBarSpaceMaxWidthPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width);
167 searchBarHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
168 searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
169 searchBarSpaceHeightPx = searchBarHeightPx + 2 * edgeMarginPx;
170
171 // Calculate the actual text height
172 Paint textPaint = new Paint();
173 textPaint.setTextSize(iconTextSizePx);
174 FontMetrics fm = textPaint.getFontMetrics();
175 cellWidthPx = iconSizePx;
176 cellHeightPx = iconSizePx + (int) Math.ceil(fm.bottom - fm.top);
177
Winson Chung892c74d2013-08-22 16:15:50 -0700178 // At this point, if the cells do not fit into the available height, then we need
179 // to shrink the icon size
180 /*
181 Rect padding = getWorkspacePadding(isLandscape ?
182 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
183 int h = (int) (numRows * cellHeightPx) + padding.top + padding.bottom;
184 if (h > availableHeightPx) {
185 float delta = h - availableHeightPx;
186 int deltaPx = (int) Math.ceil(delta / numRows);
187 iconSizePx -= deltaPx;
188 iconSize = DynamicGrid.dpiFromPx(iconSizePx, dm);
189 cellWidthPx = iconSizePx;
190 cellHeightPx = iconSizePx + (int) Math.ceil(fm.bottom - fm.top);
191 }
192 */
193
194 // Hotseat
195 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
196 hotseatCellWidthPx = iconSizePx;
197 hotseatCellHeightPx = iconSizePx;
198
Winson Chung5f8afe62013-08-12 16:19:28 -0700199 // Folder
200 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
201 folderCellHeightPx = cellHeightPx + edgeMarginPx;
202 folderBackgroundOffset = -edgeMarginPx;
203 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
204 }
205
Winson Chung892c74d2013-08-22 16:15:50 -0700206 void updateFromConfiguration(Resources resources, int wPx, int hPx,
207 int awPx, int ahPx) {
208 DisplayMetrics dm = resources.getDisplayMetrics();
Winson Chung5f8afe62013-08-12 16:19:28 -0700209 isLandscape = (resources.getConfiguration().orientation ==
210 Configuration.ORIENTATION_LANDSCAPE);
211 isTablet = resources.getBoolean(R.bool.is_tablet);
212 isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
213 widthPx = wPx;
214 heightPx = hPx;
Winson Chung892c74d2013-08-22 16:15:50 -0700215 availableWidthPx = awPx;
216 availableHeightPx = ahPx;
Winson Chung5f8afe62013-08-12 16:19:28 -0700217 }
218
219 private float dist(PointF p0, PointF p1) {
220 return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) +
221 (p1.y-p0.y)*(p1.y-p0.y));
222 }
223
224 private float weight(PointF a, PointF b,
225 float pow) {
226 float d = dist(a, b);
227 if (d == 0f) {
228 return Float.POSITIVE_INFINITY;
229 }
230 return (float) (1f / Math.pow(d, pow));
231 }
232
233 private float invDistWeightedInterpolate(float width, float height,
234 ArrayList<DeviceProfileQuery> points) {
235 float sum = 0;
236 float weights = 0;
237 float pow = 5;
238 float kNearestNeighbors = 3;
239 final PointF xy = new PointF(width, height);
240
241 ArrayList<DeviceProfileQuery> pointsByNearness = points;
242 Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
243 public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
244 return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
245 }
246 });
247
248 for (int i = 0; i < pointsByNearness.size(); ++i) {
249 DeviceProfileQuery p = pointsByNearness.get(i);
250 if (i < kNearestNeighbors) {
251 float w = weight(xy, p.dimens, pow);
252 if (w == Float.POSITIVE_INFINITY) {
253 return p.value;
254 }
255 weights += w;
256 }
257 }
258
259 for (int i = 0; i < pointsByNearness.size(); ++i) {
260 DeviceProfileQuery p = pointsByNearness.get(i);
261 if (i < kNearestNeighbors) {
262 float w = weight(xy, p.dimens, pow);
263 sum += w * p.value / weights;
264 }
265 }
266
267 return sum;
268 }
269
270 Rect getWorkspacePadding(int orientation) {
271 Rect padding = new Rect();
272 if (orientation == CellLayout.LANDSCAPE &&
273 transposeLayoutWithOrientation) {
274 // Pad the left and right of the workspace with search/hotseat bar sizes
275 padding.set(searchBarSpaceHeightPx, edgeMarginPx,
276 hotseatBarHeightPx, edgeMarginPx);
277 } else {
278 if (isTablet()) {
279 // Pad the left and right of the workspace to ensure consistent spacing
280 // between all icons
281 int width = (orientation == CellLayout.LANDSCAPE)
282 ? Math.max(widthPx, heightPx)
283 : Math.min(widthPx, heightPx);
284 // XXX: If the icon size changes across orientations, we will have to take
285 // that into account here too.
286 int gap = (int) ((width - 2 * edgeMarginPx -
287 (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
288 padding.set(edgeMarginPx + gap,
289 searchBarSpaceHeightPx,
290 edgeMarginPx + gap,
291 hotseatBarHeightPx + pageIndicatorHeightPx);
292 } else {
293 // Pad the top and bottom of the workspace with search/hotseat bar sizes
294 padding.set(edgeMarginPx,
295 searchBarSpaceHeightPx,
296 edgeMarginPx,
297 hotseatBarHeightPx + pageIndicatorHeightPx);
298 }
299 }
300 return padding;
301 }
302
303 int calculateCellWidth(int width, int countX) {
304 return width / countX;
305 }
306 int calculateCellHeight(int height, int countY) {
307 return height / countY;
308 }
309
310 boolean isTablet() {
311 return isTablet;
312 }
313
314 boolean isLargeTablet() {
315 return isLargeTablet;
316 }
317
318 public void layout(Launcher launcher) {
319 FrameLayout.LayoutParams lp;
320 Resources res = launcher.getResources();
321 boolean hasVerticalBarLayout = isLandscape &&
322 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
323
324 // Layout the search bar space
325 View searchBarSpace = launcher.findViewById(R.id.qsb_bar);
326 lp = (FrameLayout.LayoutParams) searchBarSpace.getLayoutParams();
327 if (hasVerticalBarLayout) {
328 // Vertical search bar
329 lp.gravity = Gravity.TOP | Gravity.LEFT;
330 lp.width = searchBarSpaceHeightPx;
331 lp.height = LayoutParams.MATCH_PARENT;
332 searchBarSpace.setPadding(
333 0, 2 * edgeMarginPx, 0,
334 2 * edgeMarginPx);
335 } else {
336 // Horizontal search bar
337 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
338 lp.width = searchBarSpaceWidthPx;
339 lp.height = searchBarSpaceHeightPx;
340 searchBarSpace.setPadding(
341 2 * edgeMarginPx,
342 2 * edgeMarginPx,
343 2 * edgeMarginPx, 0);
344 }
345 searchBarSpace.setLayoutParams(lp);
346
347 // Layout the search bar
348 View searchBar = searchBarSpace.findViewById(R.id.qsb_search_bar);
349 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
350 lp.width = LayoutParams.MATCH_PARENT;
351 lp.height = LayoutParams.MATCH_PARENT;
352 searchBar.setLayoutParams(lp);
353
354 // Layout the voice proxy
355 View voiceButtonProxy = launcher.findViewById(R.id.voice_button_proxy);
356 if (voiceButtonProxy != null) {
357 if (hasVerticalBarLayout) {
358 // TODO: MOVE THIS INTO SEARCH BAR MEASURE
359 } else {
360 lp = (FrameLayout.LayoutParams) voiceButtonProxy.getLayoutParams();
361 lp.gravity = Gravity.TOP | Gravity.END;
362 lp.width = (widthPx - searchBarSpaceWidthPx) / 2 +
363 2 * iconSizePx;
364 lp.height = searchBarSpaceHeightPx;
365 }
366 }
367
368 // Layout the workspace
369 View workspace = launcher.findViewById(R.id.workspace);
370 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
371 lp.gravity = Gravity.CENTER;
372 Rect padding = getWorkspacePadding(isLandscape
373 ? CellLayout.LANDSCAPE
374 : CellLayout.PORTRAIT);
375 workspace.setPadding(padding.left, padding.top,
376 padding.right, padding.bottom);
377 workspace.setLayoutParams(lp);
378
379 // Layout the hotseat
380 View hotseat = launcher.findViewById(R.id.hotseat);
381 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
382 if (hasVerticalBarLayout) {
383 // Vertical hotseat
384 lp.gravity = Gravity.RIGHT;
385 lp.width = hotseatBarHeightPx;
386 lp.height = LayoutParams.MATCH_PARENT;
387 hotseat.setPadding(0, 2 * edgeMarginPx,
388 2 * edgeMarginPx, 2 * edgeMarginPx);
389 } else if (isTablet()) {
390 // Pad the hotseat with the grid gap calculated above
391 int gridGap = (int) ((widthPx - 2 * edgeMarginPx -
392 (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
393 int gridWidth = (int) ((numColumns * cellWidthPx) +
394 ((numColumns - 1) * gridGap));
395 int hotseatGap = (int) Math.max(0,
396 (gridWidth - (numHotseatIcons * hotseatCellWidthPx))
397 / (numHotseatIcons - 1));
398 lp.gravity = Gravity.BOTTOM;
399 lp.width = LayoutParams.MATCH_PARENT;
400 lp.height = hotseatBarHeightPx;
401 hotseat.setPadding(2 * edgeMarginPx + gridGap + hotseatGap, 0,
402 2 * edgeMarginPx + gridGap + hotseatGap,
403 2 * edgeMarginPx);
404 } else {
405 // For phones, layout the hotseat without any bottom margin
406 // to ensure that we have space for the folders
407 lp.gravity = Gravity.BOTTOM;
408 lp.width = LayoutParams.MATCH_PARENT;
409 lp.height = hotseatBarHeightPx;
410 hotseat.setPadding(2 * edgeMarginPx, 0,
411 2 * edgeMarginPx, 0);
412 }
413 hotseat.setLayoutParams(lp);
414
415 // Layout the page indicators
416 View pageIndicator = launcher.findViewById(R.id.page_indicator);
417 if (pageIndicator != null) {
418 if (hasVerticalBarLayout) {
419 // Hide the page indicators when we have vertical search/hotseat
420 pageIndicator.setVisibility(View.GONE);
421 } else {
422 // Put the page indicators above the hotseat
423 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
424 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
425 lp.width = LayoutParams.WRAP_CONTENT;
426 lp.height = pageIndicatorHeightPx;
427 lp.bottomMargin = hotseatBarHeightPx;
428 pageIndicator.setLayoutParams(lp);
429 }
430 }
431 }
432}
433
434public class DynamicGrid {
435 @SuppressWarnings("unused")
436 private static final String TAG = "DynamicGrid";
437
438 private DeviceProfile mProfile;
439 private float mMinWidth;
440 private float mMinHeight;
441
Winson Chung892c74d2013-08-22 16:15:50 -0700442 public static float dpiFromPx(int size, DisplayMetrics metrics){
Winson Chung5f8afe62013-08-12 16:19:28 -0700443 float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT;
Winson Chung892c74d2013-08-22 16:15:50 -0700444 return (size / densityRatio);
445 }
446 public static int pxFromDp(float size, DisplayMetrics metrics) {
447 return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
448 size, metrics));
449 }
450 public static int pxFromSp(float size, DisplayMetrics metrics) {
451 return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
452 size, metrics));
Winson Chung5f8afe62013-08-12 16:19:28 -0700453 }
454
455 public DynamicGrid(Resources resources, int minWidthPx, int minHeightPx,
Winson Chung892c74d2013-08-22 16:15:50 -0700456 int widthPx, int heightPx,
457 int awPx, int ahPx) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700458 DisplayMetrics dm = resources.getDisplayMetrics();
459 ArrayList<DeviceProfile> deviceProfiles =
460 new ArrayList<DeviceProfile>();
461 // Our phone profiles include the bar sizes in each orientation
462 deviceProfiles.add(new DeviceProfile("Super Short Stubby",
463 255, 300, 2, 3, 48, 12, 4, 48));
464 deviceProfiles.add(new DeviceProfile("Shorter Stubby",
465 255, 400, 3, 3, 48, 12, 4, 48));
466 deviceProfiles.add(new DeviceProfile("Short Stubby",
467 275, 420, 3, 4, 48, 12, 4, 48));
468 deviceProfiles.add(new DeviceProfile("Stubby",
469 255, 450, 3, 4, 48, 12, 4, 48));
470 deviceProfiles.add(new DeviceProfile("Nexus S",
471 296, 491.33f, 4, 4, 48, 12, 4, 48));
472 deviceProfiles.add(new DeviceProfile("Nexus 4",
473 359, 518, 4, 4, 60, 12, 5, 56));
474 // The tablet profile is odd in that the landscape orientation
475 // also includes the nav bar on the side
476 deviceProfiles.add(new DeviceProfile("Nexus 7",
477 575, 904, 6, 6, 72, 14.4f, 7, 60));
478 // Larger tablet profiles always have system bars on the top & bottom
479 deviceProfiles.add(new DeviceProfile("Nexus 10",
480 727, 1207, 5, 8, 80, 14.4f, 9, 64));
481 /*
482 deviceProfiles.add(new DeviceProfile("Nexus 7",
483 600, 960, 5, 5, 72, 14.4f, 5, 60));
484 deviceProfiles.add(new DeviceProfile("Nexus 10",
485 800, 1280, 5, 5, 80, 14.4f, 6, 64));
486 */
487 deviceProfiles.add(new DeviceProfile("20-inch Tablet",
488 1527, 2527, 7, 7, 100, 20, 7, 72));
489 mMinWidth = dpiFromPx(minWidthPx, dm);
490 mMinHeight = dpiFromPx(minHeightPx, dm);
491 mProfile = new DeviceProfile(deviceProfiles,
Winson Chung892c74d2013-08-22 16:15:50 -0700492 mMinWidth, mMinHeight,
Winson Chung5f8afe62013-08-12 16:19:28 -0700493 widthPx, heightPx,
Winson Chung892c74d2013-08-22 16:15:50 -0700494 awPx, ahPx,
Winson Chung5f8afe62013-08-12 16:19:28 -0700495 resources);
496 }
497
498 DeviceProfile getDeviceProfile() {
499 return mProfile;
500 }
501
502 public String toString() {
503 return "-------- DYNAMIC GRID ------- \n" +
504 "Wd: " + mProfile.minWidthDps + ", Hd: " + mProfile.minHeightDps +
505 ", W: " + mProfile.widthPx + ", H: " + mProfile.heightPx +
506 " [r: " + mProfile.numRows + ", c: " + mProfile.numColumns +
507 ", is: " + mProfile.iconSizePx + ", its: " + mProfile.iconTextSize +
508 ", cw: " + mProfile.cellWidthPx + ", ch: " + mProfile.cellHeightPx +
509 ", hc: " + mProfile.numHotseatIcons + ", his: " + mProfile.hotseatIconSizePx + "]";
510 }
511}