blob: ea2852080f95d94c1c3abc5e7d6448d0bc63d2d3 [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;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -070036import android.view.ViewGroup.MarginLayoutParams;
Winson Chungb3800242013-10-24 11:01:54 -070037import android.view.WindowManager;
38import android.widget.FrameLayout;
Adam Cohen24ce0b32014-01-14 16:18:14 -080039import android.widget.LinearLayout;
Winson Chungb3800242013-10-24 11:01:54 -070040
Adam Cohen091440a2015-03-18 14:16:05 -070041import com.android.launcher3.util.Thunk;
42
Winson Chungb3800242013-10-24 11:01:54 -070043import java.util.ArrayList;
44import java.util.Collections;
45import java.util.Comparator;
46
47
48class DeviceProfileQuery {
Winson Chungbe876472014-05-14 14:15:20 -070049 DeviceProfile profile;
Winson Chungb3800242013-10-24 11:01:54 -070050 float widthDps;
51 float heightDps;
52 float value;
53 PointF dimens;
54
Winson Chungbe876472014-05-14 14:15:20 -070055 DeviceProfileQuery(DeviceProfile p, float v) {
56 widthDps = p.minWidthDps;
57 heightDps = p.minHeightDps;
Winson Chungb3800242013-10-24 11:01:54 -070058 value = v;
Winson Chungbe876472014-05-14 14:15:20 -070059 dimens = new PointF(widthDps, heightDps);
60 profile = p;
Winson Chungb3800242013-10-24 11:01:54 -070061 }
62}
63
64public class DeviceProfile {
65 public static interface DeviceProfileCallbacks {
66 public void onAvailableSizeChanged(DeviceProfile grid);
67 }
68
69 String name;
70 float minWidthDps;
71 float minHeightDps;
Adam Cohen59400422014-03-05 18:07:04 -080072 public float numRows;
73 public float numColumns;
Winson Chungb3800242013-10-24 11:01:54 -070074 float numHotseatIcons;
Adam Cohen4ae96ce2014-08-29 15:05:48 -070075 float iconSize;
Winson Chungb3800242013-10-24 11:01:54 -070076 private float iconTextSize;
77 private int iconDrawablePaddingOriginalPx;
78 private float hotseatIconSize;
79
Winson Chungbe876472014-05-14 14:15:20 -070080 int defaultLayoutId;
Winson Chungbe876472014-05-14 14:15:20 -070081
Winson Chungb3800242013-10-24 11:01:54 -070082 boolean isLandscape;
83 boolean isTablet;
84 boolean isLargeTablet;
Winson Chung42b3c062013-12-04 12:09:59 -080085 boolean isLayoutRtl;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070086
Winson Chungb3800242013-10-24 11:01:54 -070087 boolean transposeLayoutWithOrientation;
88
89 int desiredWorkspaceLeftRightMarginPx;
Adam Cohen59400422014-03-05 18:07:04 -080090 public int edgeMarginPx;
Winson Chungb3800242013-10-24 11:01:54 -070091 Rect defaultWidgetPadding;
92
93 int widthPx;
94 int heightPx;
Adam Cohen59400422014-03-05 18:07:04 -080095 public int availableWidthPx;
96 public int availableHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -070097 int defaultPageSpacingPx;
98
99 int overviewModeMinIconZoneHeightPx;
100 int overviewModeMaxIconZoneHeightPx;
Jorim Jaggid017f882014-01-14 17:08:48 -0800101 int overviewModeBarItemWidthPx;
102 int overviewModeBarSpacerWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700103 float overviewModeIconZoneRatio;
104 float overviewModeScaleFactor;
105
Adam Cohen59400422014-03-05 18:07:04 -0800106 public int cellWidthPx;
107 public int cellHeightPx;
108
Hyunyoung Song3f471442015-04-08 19:01:34 -0700109 public int iconSizePx;
110 public int iconTextSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700111 int iconDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700112 int allAppsIconSizePx;
113 int allAppsIconTextSizePx;
114 int allAppsCellWidthPx;
115 int allAppsCellHeightPx;
116 int allAppsCellPaddingPx;
117 int folderBackgroundOffset;
118 int folderIconSizePx;
119 int folderCellWidthPx;
120 int folderCellHeightPx;
121 int hotseatCellWidthPx;
122 int hotseatCellHeightPx;
123 int hotseatIconSizePx;
124 int hotseatBarHeightPx;
125 int hotseatAllAppsRank;
126 int allAppsNumRows;
127 int allAppsNumCols;
Winson Chung93f98ea2015-03-10 16:28:47 -0700128 int appsViewNumCols;
Winson Chungb3800242013-10-24 11:01:54 -0700129 int searchBarSpaceWidthPx;
Winson Chungb3800242013-10-24 11:01:54 -0700130 int searchBarSpaceHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700131 int pageIndicatorHeightPx;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700132 int allAppsButtonVisualSize;
Winson Chungb3800242013-10-24 11:01:54 -0700133
Winson Chung59a488a2013-12-10 12:32:14 -0800134 float dragViewScale;
135
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700136 int allAppsShortEdgeCount = -1;
137 int allAppsLongEdgeCount = -1;
138
Winson Chungb3800242013-10-24 11:01:54 -0700139 private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>();
140
141 DeviceProfile(String n, float w, float h, float r, float c,
Adam Cohencee8c662014-10-16 09:49:52 -0700142 float is, float its, float hs, float his, int dlId) {
Winson Chungb3800242013-10-24 11:01:54 -0700143 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800144 if (hs % 2 == 0) {
Winson Chungb3800242013-10-24 11:01:54 -0700145 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
146 }
147
148 name = n;
149 minWidthDps = w;
150 minHeightDps = h;
151 numRows = r;
152 numColumns = c;
153 iconSize = is;
154 iconTextSize = its;
155 numHotseatIcons = hs;
156 hotseatIconSize = his;
Winson Chungbe876472014-05-14 14:15:20 -0700157 defaultLayoutId = dlId;
Winson Chungb3800242013-10-24 11:01:54 -0700158 }
159
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700160 DeviceProfile() {
161 }
162
Winson Chungb3800242013-10-24 11:01:54 -0700163 DeviceProfile(Context context,
164 ArrayList<DeviceProfile> profiles,
165 float minWidth, float minHeight,
166 int wPx, int hPx,
167 int awPx, int ahPx,
168 Resources res) {
169 DisplayMetrics dm = res.getDisplayMetrics();
170 ArrayList<DeviceProfileQuery> points =
171 new ArrayList<DeviceProfileQuery>();
172 transposeLayoutWithOrientation =
173 res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
174 minWidthDps = minWidth;
175 minHeightDps = minHeight;
176
177 ComponentName cn = new ComponentName(context.getPackageName(),
178 this.getClass().getName());
179 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
180 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
181 desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
182 pageIndicatorHeightPx =
183 res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
184 defaultPageSpacingPx =
185 res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
186 allAppsCellPaddingPx =
187 res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_padding);
188 overviewModeMinIconZoneHeightPx =
189 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
190 overviewModeMaxIconZoneHeightPx =
191 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
Jorim Jaggid017f882014-01-14 17:08:48 -0800192 overviewModeBarItemWidthPx =
193 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
194 overviewModeBarSpacerWidthPx =
195 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
Winson Chungb3800242013-10-24 11:01:54 -0700196 overviewModeIconZoneRatio =
197 res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
198 overviewModeScaleFactor =
199 res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f;
200
Winson Chungbe876472014-05-14 14:15:20 -0700201 // Find the closes profile given the width/height
Winson Chungb3800242013-10-24 11:01:54 -0700202 for (DeviceProfile p : profiles) {
Winson Chungbe876472014-05-14 14:15:20 -0700203 points.add(new DeviceProfileQuery(p, 0f));
Winson Chungb3800242013-10-24 11:01:54 -0700204 }
Winson Chungbe876472014-05-14 14:15:20 -0700205 DeviceProfile closestProfile = findClosestDeviceProfile(minWidth, minHeight, points);
206
207 // Snap to the closest row count
208 numRows = closestProfile.numRows;
209
210 // Snap to the closest column count
211 numColumns = closestProfile.numColumns;
212
213 // Snap to the closest hotseat size
214 numHotseatIcons = closestProfile.numHotseatIcons;
Winson Chungb3800242013-10-24 11:01:54 -0700215 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
216
Winson Chungbe876472014-05-14 14:15:20 -0700217 // Snap to the closest default layout id
218 defaultLayoutId = closestProfile.defaultLayoutId;
219
Winson Chungb3800242013-10-24 11:01:54 -0700220 // Interpolate the icon size
221 points.clear();
222 for (DeviceProfile p : profiles) {
Winson Chungbe876472014-05-14 14:15:20 -0700223 points.add(new DeviceProfileQuery(p, p.iconSize));
Winson Chungb3800242013-10-24 11:01:54 -0700224 }
225 iconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700226
Winson Chungb3800242013-10-24 11:01:54 -0700227 // AllApps uses the original non-scaled icon size
228 allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm);
229
230 // Interpolate the icon text size
231 points.clear();
232 for (DeviceProfile p : profiles) {
Winson Chungbe876472014-05-14 14:15:20 -0700233 points.add(new DeviceProfileQuery(p, p.iconTextSize));
Winson Chungb3800242013-10-24 11:01:54 -0700234 }
235 iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points);
236 iconDrawablePaddingOriginalPx =
237 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
238 // AllApps uses the original non-scaled icon text size
239 allAppsIconTextSizePx = DynamicGrid.pxFromDp(iconTextSize, dm);
240
241 // Interpolate the hotseat icon size
242 points.clear();
243 for (DeviceProfile p : profiles) {
Winson Chungbe876472014-05-14 14:15:20 -0700244 points.add(new DeviceProfileQuery(p, p.hotseatIconSize));
Winson Chungb3800242013-10-24 11:01:54 -0700245 }
246 // Hotseat
247 hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
248
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700249 // If the partner customization apk contains any grid overrides, apply them
250 applyPartnerDeviceProfileOverrides(context, dm);
251
Winson Chungb3800242013-10-24 11:01:54 -0700252 // Calculate the remaining vars
253 updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx);
254 updateAvailableDimensions(context);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700255 computeAllAppsButtonSize(context);
256 }
257
258 /**
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700259 * Apply any Partner customization grid overrides.
260 *
261 * Currently we support: all apps row / column count.
262 */
263 private void applyPartnerDeviceProfileOverrides(Context ctx, DisplayMetrics dm) {
264 Partner p = Partner.get(ctx.getPackageManager());
265 if (p != null) {
266 DeviceProfile partnerDp = p.getDeviceProfileOverride(dm);
267 if (partnerDp != null) {
268 if (partnerDp.numRows > 0 && partnerDp.numColumns > 0) {
269 numRows = partnerDp.numRows;
270 numColumns = partnerDp.numColumns;
271 }
272 if (partnerDp.allAppsShortEdgeCount > 0 && partnerDp.allAppsLongEdgeCount > 0) {
273 allAppsShortEdgeCount = partnerDp.allAppsShortEdgeCount;
274 allAppsLongEdgeCount = partnerDp.allAppsLongEdgeCount;
275 }
276 if (partnerDp.iconSize > 0) {
277 iconSize = partnerDp.iconSize;
278 // AllApps uses the original non-scaled icon size
279 allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm);
280 }
281 }
282 }
283 }
284
285 /**
Adam Cohen63f1ec02014-08-12 09:23:13 -0700286 * Determine the exact visual footprint of the all apps button, taking into account scaling
287 * and internal padding of the drawable.
288 */
289 private void computeAllAppsButtonSize(Context context) {
290 Resources res = context.getResources();
291 float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f;
292 LauncherAppState app = LauncherAppState.getInstance();
293 allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding));
Winson Chungb3800242013-10-24 11:01:54 -0700294 }
295
296 void addCallback(DeviceProfileCallbacks cb) {
297 mCallbacks.add(cb);
298 cb.onAvailableSizeChanged(this);
299 }
300 void removeCallback(DeviceProfileCallbacks cb) {
301 mCallbacks.remove(cb);
302 }
303
304 private int getDeviceOrientation(Context context) {
305 WindowManager windowManager = (WindowManager)
306 context.getSystemService(Context.WINDOW_SERVICE);
307 Resources resources = context.getResources();
308 DisplayMetrics dm = resources.getDisplayMetrics();
309 Configuration config = resources.getConfiguration();
310 int rotation = windowManager.getDefaultDisplay().getRotation();
311
312 boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) &&
313 (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180);
314 boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) &&
315 (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
316 if (isLandscape || isRotatedPortrait) {
317 return CellLayout.LANDSCAPE;
318 } else {
319 return CellLayout.PORTRAIT;
320 }
321 }
322
323 private void updateAvailableDimensions(Context context) {
324 WindowManager windowManager = (WindowManager)
325 context.getSystemService(Context.WINDOW_SERVICE);
326 Display display = windowManager.getDefaultDisplay();
327 Resources resources = context.getResources();
328 DisplayMetrics dm = resources.getDisplayMetrics();
329 Configuration config = resources.getConfiguration();
330
331 // There are three possible configurations that the dynamic grid accounts for, portrait,
332 // landscape with the nav bar at the bottom, and landscape with the nav bar at the side.
333 // To prevent waiting for fitSystemWindows(), we make the observation that in landscape,
334 // the height is the smallest height (either with the nav bar at the bottom or to the
335 // side) and otherwise, the height is simply the largest possible height for a portrait
336 // device.
337 Point size = new Point();
338 Point smallestSize = new Point();
339 Point largestSize = new Point();
340 display.getSize(size);
341 display.getCurrentSizeRange(smallestSize, largestSize);
342 availableWidthPx = size.x;
343 if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
344 availableHeightPx = smallestSize.y;
345 } else {
346 availableHeightPx = largestSize.y;
347 }
348
349 // Check to see if the icons fit in the new available height. If not, then we need to
350 // shrink the icon size.
Winson Chungb3800242013-10-24 11:01:54 -0700351 float scale = 1f;
352 int drawablePadding = iconDrawablePaddingOriginalPx;
353 updateIconSize(1f, drawablePadding, resources, dm);
354 float usedHeight = (cellHeightPx * numRows);
Winson Chung59a488a2013-12-10 12:32:14 -0800355
356 Rect workspacePadding = getWorkspacePadding();
Winson Chungb3800242013-10-24 11:01:54 -0700357 int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
358 if (usedHeight > maxHeight) {
359 scale = maxHeight / usedHeight;
360 drawablePadding = 0;
361 }
362 updateIconSize(scale, drawablePadding, resources, dm);
363
364 // Make the callbacks
365 for (DeviceProfileCallbacks cb : mCallbacks) {
366 cb.onAvailableSizeChanged(this);
367 }
368 }
369
Winson Chung93f98ea2015-03-10 16:28:47 -0700370 private void updateIconSize(float scale, int drawablePadding, Resources res,
Winson Chungb3800242013-10-24 11:01:54 -0700371 DisplayMetrics dm) {
372 iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale);
373 iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale);
374 iconDrawablePaddingPx = drawablePadding;
375 hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale);
376
377 // Search Bar
Sunny Goyal594d76d2014-11-06 10:12:54 -0800378 searchBarSpaceWidthPx = Math.min(widthPx,
Winson Chung93f98ea2015-03-10 16:28:47 -0700379 res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
Sunny Goyal594d76d2014-11-06 10:12:54 -0800380 searchBarSpaceHeightPx = getSearchBarTopOffset()
Winson Chung93f98ea2015-03-10 16:28:47 -0700381 + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
Winson Chungb3800242013-10-24 11:01:54 -0700382
383 // Calculate the actual text height
384 Paint textPaint = new Paint();
385 textPaint.setTextSize(iconTextSizePx);
386 FontMetrics fm = textPaint.getFontMetrics();
387 cellWidthPx = iconSizePx;
388 cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
Winson Chung93f98ea2015-03-10 16:28:47 -0700389 final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
Winson Chung59a488a2013-12-10 12:32:14 -0800390 dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
Winson Chungb3800242013-10-24 11:01:54 -0700391
392 // Hotseat
393 hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
394 hotseatCellWidthPx = iconSizePx;
395 hotseatCellHeightPx = iconSizePx;
396
397 // Folder
398 folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
399 folderCellHeightPx = cellHeightPx + edgeMarginPx;
400 folderBackgroundOffset = -edgeMarginPx;
401 folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
402
403 // All Apps
Winson Chungb3800242013-10-24 11:01:54 -0700404 allAppsCellWidthPx = allAppsIconSizePx;
405 allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + iconTextSizePx;
406 int maxLongEdgeCellCount =
Winson Chung93f98ea2015-03-10 16:28:47 -0700407 res.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count);
Winson Chungb3800242013-10-24 11:01:54 -0700408 int maxShortEdgeCellCount =
Winson Chung93f98ea2015-03-10 16:28:47 -0700409 res.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count);
Winson Chungb3800242013-10-24 11:01:54 -0700410 int minEdgeCellCount =
Winson Chung93f98ea2015-03-10 16:28:47 -0700411 res.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count);
Winson Chungb3800242013-10-24 11:01:54 -0700412 int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount);
413 int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount);
414
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700415 if (allAppsShortEdgeCount > 0 && allAppsLongEdgeCount > 0) {
416 allAppsNumRows = isLandscape ? allAppsShortEdgeCount : allAppsLongEdgeCount;
417 allAppsNumCols = isLandscape ? allAppsLongEdgeCount : allAppsShortEdgeCount;
418 } else {
419 allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx) /
420 (allAppsCellHeightPx + allAppsCellPaddingPx);
421 allAppsNumRows = Math.max(minEdgeCellCount, Math.min(maxRows, allAppsNumRows));
Winson Chung93f98ea2015-03-10 16:28:47 -0700422 allAppsNumCols = (availableWidthPx) / (allAppsCellWidthPx + allAppsCellPaddingPx);
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700423 allAppsNumCols = Math.max(minEdgeCellCount, Math.min(maxCols, allAppsNumCols));
424 }
Winson Chung93f98ea2015-03-10 16:28:47 -0700425
426 int appsContainerViewPx = res.getDimensionPixelSize(R.dimen.apps_container_width);
427 int appsViewLeftMarginPx =
428 res.getDimensionPixelSize(R.dimen.apps_grid_view_start_margin);
429 int availableAppsWidthPx = (appsContainerViewPx > 0) ? appsContainerViewPx :
430 availableWidthPx;
431 appsViewNumCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chungbd808532015-04-08 11:07:53 -0700432 (allAppsCellWidthPx + 2 * allAppsCellPaddingPx);
Winson Chungb3800242013-10-24 11:01:54 -0700433 }
434
435 void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx,
436 int awPx, int ahPx) {
Winson Chung42b3c062013-12-04 12:09:59 -0800437 Configuration configuration = resources.getConfiguration();
438 isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
Winson Chungb3800242013-10-24 11:01:54 -0700439 isTablet = resources.getBoolean(R.bool.is_tablet);
440 isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
Winson Chung6033ceb2014-02-05 12:37:42 -0800441 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
442 isLayoutRtl = (configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
443 } else {
444 isLayoutRtl = false;
445 }
Winson Chungb3800242013-10-24 11:01:54 -0700446 widthPx = wPx;
447 heightPx = hPx;
448 availableWidthPx = awPx;
449 availableHeightPx = ahPx;
450
451 updateAvailableDimensions(context);
452 }
453
Adam Cohen091440a2015-03-18 14:16:05 -0700454 @Thunk float dist(PointF p0, PointF p1) {
Winson Chungb3800242013-10-24 11:01:54 -0700455 return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) +
456 (p1.y-p0.y)*(p1.y-p0.y));
457 }
458
459 private float weight(PointF a, PointF b,
460 float pow) {
461 float d = dist(a, b);
462 if (d == 0f) {
463 return Float.POSITIVE_INFINITY;
464 }
465 return (float) (1f / Math.pow(d, pow));
466 }
467
Winson Chungbe876472014-05-14 14:15:20 -0700468 /** Returns the closest device profile given the width and height and a list of profiles */
469 private DeviceProfile findClosestDeviceProfile(float width, float height,
470 ArrayList<DeviceProfileQuery> points) {
471 return findClosestDeviceProfiles(width, height, points).get(0).profile;
472 }
473
474 /** Returns the closest device profiles ordered by closeness to the specified width and height */
475 private ArrayList<DeviceProfileQuery> findClosestDeviceProfiles(float width, float height,
476 ArrayList<DeviceProfileQuery> points) {
477 final PointF xy = new PointF(width, height);
478
479 // Sort the profiles by their closeness to the dimensions
480 ArrayList<DeviceProfileQuery> pointsByNearness = points;
481 Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
482 public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
483 return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
484 }
485 });
486
487 return pointsByNearness;
488 }
489
Winson Chungb3800242013-10-24 11:01:54 -0700490 private float invDistWeightedInterpolate(float width, float height,
491 ArrayList<DeviceProfileQuery> points) {
492 float sum = 0;
493 float weights = 0;
494 float pow = 5;
495 float kNearestNeighbors = 3;
496 final PointF xy = new PointF(width, height);
497
Winson Chungbe876472014-05-14 14:15:20 -0700498 ArrayList<DeviceProfileQuery> pointsByNearness = findClosestDeviceProfiles(width, height,
499 points);
Winson Chungb3800242013-10-24 11:01:54 -0700500
501 for (int i = 0; i < pointsByNearness.size(); ++i) {
502 DeviceProfileQuery p = pointsByNearness.get(i);
503 if (i < kNearestNeighbors) {
504 float w = weight(xy, p.dimens, pow);
505 if (w == Float.POSITIVE_INFINITY) {
506 return p.value;
507 }
508 weights += w;
509 }
510 }
511
512 for (int i = 0; i < pointsByNearness.size(); ++i) {
513 DeviceProfileQuery p = pointsByNearness.get(i);
514 if (i < kNearestNeighbors) {
515 float w = weight(xy, p.dimens, pow);
516 sum += w * p.value / weights;
517 }
518 }
519
520 return sum;
521 }
522
Winson Chung69e04ea2013-12-02 14:43:44 -0800523 /** Returns the search bar top offset */
524 int getSearchBarTopOffset() {
525 if (isTablet() && !isVerticalBarLayout()) {
526 return 4 * edgeMarginPx;
527 } else {
528 return 2 * edgeMarginPx;
529 }
530 }
531
Winson Chungb3800242013-10-24 11:01:54 -0700532 /** Returns the search bar bounds in the current orientation */
533 Rect getSearchBarBounds() {
534 return getSearchBarBounds(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
535 }
536 /** Returns the search bar bounds in the specified orientation */
537 Rect getSearchBarBounds(int orientation) {
538 Rect bounds = new Rect();
539 if (orientation == CellLayout.LANDSCAPE &&
540 transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800541 if (isLayoutRtl) {
542 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
543 availableWidthPx, availableHeightPx - edgeMarginPx);
544 } else {
545 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
546 availableHeightPx - edgeMarginPx);
547 }
Winson Chungb3800242013-10-24 11:01:54 -0700548 } else {
549 if (isTablet()) {
550 // Pad the left and right of the workspace to ensure consistent spacing
551 // between all icons
552 int width = (orientation == CellLayout.LANDSCAPE)
553 ? Math.max(widthPx, heightPx)
554 : Math.min(widthPx, heightPx);
555 // XXX: If the icon size changes across orientations, we will have to take
556 // that into account here too.
557 int gap = (int) ((width - 2 * edgeMarginPx -
558 (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800559 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
560 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700561 searchBarSpaceHeightPx);
562 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800563 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
564 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700565 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
566 defaultWidgetPadding.right), searchBarSpaceHeightPx);
567 }
568 }
569 return bounds;
570 }
571
Winson Chunga6945242014-01-08 14:04:34 -0800572 /** Returns the bounds of the workspace page indicators. */
573 Rect getWorkspacePageIndicatorBounds(Rect insets) {
574 Rect workspacePadding = getWorkspacePadding();
Winson Chung205cd772014-01-15 14:31:59 -0800575 if (isLandscape && transposeLayoutWithOrientation) {
576 if (isLayoutRtl) {
577 return new Rect(workspacePadding.left, workspacePadding.top,
578 workspacePadding.left + pageIndicatorHeightPx,
579 heightPx - workspacePadding.bottom - insets.bottom);
580 } else {
581 int pageIndicatorLeft = widthPx - workspacePadding.right;
582 return new Rect(pageIndicatorLeft, workspacePadding.top,
583 pageIndicatorLeft + pageIndicatorHeightPx,
584 heightPx - workspacePadding.bottom - insets.bottom);
585 }
586 } else {
587 int pageIndicatorTop = heightPx - insets.bottom - workspacePadding.bottom;
588 return new Rect(workspacePadding.left, pageIndicatorTop,
589 widthPx - workspacePadding.right, pageIndicatorTop + pageIndicatorHeightPx);
590 }
Winson Chunga6945242014-01-08 14:04:34 -0800591 }
592
Adam Cohen59400422014-03-05 18:07:04 -0800593 public int getWorkspaceGridHeight() {
594 Rect p = getWorkspacePadding();
595 return availableHeightPx - p.top - p.bottom;
596 }
597
598 public int getWorkspaceGridWidth() {
599 Rect p = getWorkspacePadding();
600 return availableWidthPx - p.left - p.right;
601 }
602
Winson Chungb3800242013-10-24 11:01:54 -0700603 /** Returns the workspace padding in the specified orientation */
604 Rect getWorkspacePadding() {
605 return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
606 }
607 Rect getWorkspacePadding(int orientation) {
608 Rect searchBarBounds = getSearchBarBounds(orientation);
609 Rect padding = new Rect();
610 if (orientation == CellLayout.LANDSCAPE &&
611 transposeLayoutWithOrientation) {
612 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800613 if (isLayoutRtl) {
614 padding.set(hotseatBarHeightPx, edgeMarginPx,
615 searchBarBounds.width(), edgeMarginPx);
616 } else {
617 padding.set(searchBarBounds.width(), edgeMarginPx,
618 hotseatBarHeightPx, edgeMarginPx);
619 }
Winson Chungb3800242013-10-24 11:01:54 -0700620 } else {
621 if (isTablet()) {
622 // Pad the left and right of the workspace to ensure consistent spacing
623 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800624 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chungb3800242013-10-24 11:01:54 -0700625 int width = (orientation == CellLayout.LANDSCAPE)
626 ? Math.max(widthPx, heightPx)
627 : Math.min(widthPx, heightPx);
Winson Chung59a488a2013-12-10 12:32:14 -0800628 int height = (orientation != CellLayout.LANDSCAPE)
629 ? Math.max(widthPx, heightPx)
630 : Math.min(widthPx, heightPx);
631 int paddingTop = searchBarBounds.bottom;
632 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
633 int availableWidth = Math.max(0, width - (int) ((numColumns * cellWidthPx) +
634 (numColumns * gapScale * cellWidthPx)));
635 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
636 - (int) (2 * numRows * cellHeightPx));
637 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
638 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700639 } else {
640 // Pad the top and bottom of the workspace with search/hotseat bar sizes
641 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
642 searchBarBounds.bottom,
643 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
644 hotseatBarHeightPx + pageIndicatorHeightPx);
645 }
646 }
647 return padding;
648 }
649
650 int getWorkspacePageSpacing(int orientation) {
Winson Chung59a488a2013-12-10 12:32:14 -0800651 if ((orientation == CellLayout.LANDSCAPE &&
652 transposeLayoutWithOrientation) || isLargeTablet()) {
Winson Chungb3800242013-10-24 11:01:54 -0700653 // In landscape mode the page spacing is set to the default.
654 return defaultPageSpacingPx;
655 } else {
656 // In portrait, we want the pages spaced such that there is no
657 // overhang of the previous / next page into the current page viewport.
658 // We assume symmetrical padding in portrait mode.
Adam Cohenefb31e32014-01-16 16:07:50 -0800659 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding().left);
Winson Chungb3800242013-10-24 11:01:54 -0700660 }
661 }
662
663 Rect getOverviewModeButtonBarRect() {
664 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
665 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
666 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
667 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
668 }
669
670 float getOverviewModeScale() {
671 Rect workspacePadding = getWorkspacePadding();
672 Rect overviewBar = getOverviewModeButtonBarRect();
673 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
674 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
675 }
676
677 // The rect returned will be extended to below the system ui that covers the workspace
678 Rect getHotseatRect() {
679 if (isVerticalBarLayout()) {
680 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
681 Integer.MAX_VALUE, availableHeightPx);
682 } else {
683 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
684 availableWidthPx, Integer.MAX_VALUE);
685 }
686 }
687
688 int calculateCellWidth(int width, int countX) {
689 return width / countX;
690 }
691 int calculateCellHeight(int height, int countY) {
692 return height / countY;
693 }
694
695 boolean isPhone() {
696 return !isTablet && !isLargeTablet;
697 }
698 boolean isTablet() {
699 return isTablet;
700 }
701 boolean isLargeTablet() {
702 return isLargeTablet;
703 }
704
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700705 /**
706 * When {@code true}, hotseat is on the bottom row when in landscape mode.
707 * If {@code false}, hotseat is on the right column when in landscape mode.
708 */
Winson Chungb3800242013-10-24 11:01:54 -0700709 boolean isVerticalBarLayout() {
710 return isLandscape && transposeLayoutWithOrientation;
711 }
712
713 boolean shouldFadeAdjacentWorkspaceScreens() {
714 return isVerticalBarLayout() || isLargeTablet();
715 }
716
Jorim Jaggid017f882014-01-14 17:08:48 -0800717 int getVisibleChildCount(ViewGroup parent) {
718 int visibleChildren = 0;
719 for (int i = 0; i < parent.getChildCount(); i++) {
720 if (parent.getChildAt(i).getVisibility() != View.GONE) {
721 visibleChildren++;
722 }
723 }
724 return visibleChildren;
725 }
726
Winson Chungb3800242013-10-24 11:01:54 -0700727 public void layout(Launcher launcher) {
728 FrameLayout.LayoutParams lp;
729 Resources res = launcher.getResources();
730 boolean hasVerticalBarLayout = isVerticalBarLayout();
731
732 // Layout the search bar space
733 View searchBar = launcher.getSearchBar();
734 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
735 if (hasVerticalBarLayout) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800736 // Vertical search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700737 lp.gravity = Gravity.TOP | Gravity.LEFT;
738 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800739 lp.height = LayoutParams.WRAP_CONTENT;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800740
741 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
742 targets.setOrientation(LinearLayout.VERTICAL);
Winson Chungb3800242013-10-24 11:01:54 -0700743 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800744 // Horizontal search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700745 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
746 lp.width = searchBarSpaceWidthPx;
747 lp.height = searchBarSpaceHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700748 }
749 searchBar.setLayoutParams(lp);
750
Winson Chungb3800242013-10-24 11:01:54 -0700751 // Layout the workspace
752 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
753 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
754 lp.gravity = Gravity.CENTER;
755 int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
756 Rect padding = getWorkspacePadding(orientation);
757 workspace.setLayoutParams(lp);
758 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
759 workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
760
761 // Layout the hotseat
762 View hotseat = launcher.findViewById(R.id.hotseat);
763 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
764 if (hasVerticalBarLayout) {
765 // Vertical hotseat
Winson Chung42b3c062013-12-04 12:09:59 -0800766 lp.gravity = Gravity.END;
Winson Chungb3800242013-10-24 11:01:54 -0700767 lp.width = hotseatBarHeightPx;
768 lp.height = LayoutParams.MATCH_PARENT;
769 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
770 } else if (isTablet()) {
Winson Chung59a488a2013-12-10 12:32:14 -0800771 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700772 lp.gravity = Gravity.BOTTOM;
773 lp.width = LayoutParams.MATCH_PARENT;
774 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800775 hotseat.setPadding(edgeMarginPx + padding.left, 0,
776 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700777 2 * edgeMarginPx);
778 } else {
779 // For phones, layout the hotseat without any bottom margin
780 // to ensure that we have space for the folders
781 lp.gravity = Gravity.BOTTOM;
782 lp.width = LayoutParams.MATCH_PARENT;
783 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000784 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
785 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700786 }
787 hotseat.setLayoutParams(lp);
788
789 // Layout the page indicators
790 View pageIndicator = launcher.findViewById(R.id.page_indicator);
791 if (pageIndicator != null) {
792 if (hasVerticalBarLayout) {
793 // Hide the page indicators when we have vertical search/hotseat
794 pageIndicator.setVisibility(View.GONE);
795 } else {
796 // Put the page indicators above the hotseat
797 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
798 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
799 lp.width = LayoutParams.WRAP_CONTENT;
800 lp.height = LayoutParams.WRAP_CONTENT;
801 lp.bottomMargin = hotseatBarHeightPx;
802 pageIndicator.setLayoutParams(lp);
803 }
804 }
805
Winson Chungb3800242013-10-24 11:01:54 -0700806 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800807 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700808 if (overviewMode != null) {
809 Rect r = getOverviewModeButtonBarRect();
810 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
811 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700812
813 int visibleChildCount = getVisibleChildCount(overviewMode);
814 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
815 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
816
817 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chungb3800242013-10-24 11:01:54 -0700818 lp.height = r.height();
819 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700820
821 if (lp.width > totalItemWidth && visibleChildCount > 1) {
822 // We have enough space. Lets add some margin too.
823 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
824 View lastChild = null;
825
826 // Set margin of all visible children except the last visible child
827 for (int i = 0; i < visibleChildCount; i++) {
828 if (lastChild != null) {
829 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
830 if (isLayoutRtl) {
831 clp.leftMargin = margin;
832 } else {
833 clp.rightMargin = margin;
834 }
835 lastChild.setLayoutParams(clp);
836 lastChild = null;
837 }
838 View thisChild = overviewMode.getChildAt(i);
839 if (thisChild.getVisibility() != View.GONE) {
840 lastChild = thisChild;
841 }
842 }
843 }
Winson Chungb3800242013-10-24 11:01:54 -0700844 }
845 }
846}