blob: 3dadd1a70d5cacb37f86e4466c6159923fa1952c [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;
Hyunyoung Songada50982015-04-10 14:35:23 -070085 public 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
Winson Chung0f785722015-04-08 10:27:49 -0700426 int appsContainerViewWidthPx = res.getDimensionPixelSize(R.dimen.apps_container_width);
427 updateAppsViewNumCols(res, appsContainerViewWidthPx);
428 }
429
430 public boolean updateAppsViewNumCols(Resources res, int containerWidth) {
Winson Chung93f98ea2015-03-10 16:28:47 -0700431 int appsViewLeftMarginPx =
432 res.getDimensionPixelSize(R.dimen.apps_grid_view_start_margin);
Winson Chung0f785722015-04-08 10:27:49 -0700433 int availableAppsWidthPx = (containerWidth > 0) ? containerWidth : availableWidthPx;
434 int numCols = (availableAppsWidthPx - appsViewLeftMarginPx) /
Winson Chungbd808532015-04-08 11:07:53 -0700435 (allAppsCellWidthPx + 2 * allAppsCellPaddingPx);
Winson Chung0f785722015-04-08 10:27:49 -0700436 if (numCols != appsViewNumCols) {
437 appsViewNumCols = numCols;
438 return true;
439 }
440 return false;
Winson Chungb3800242013-10-24 11:01:54 -0700441 }
442
443 void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx,
444 int awPx, int ahPx) {
Winson Chung42b3c062013-12-04 12:09:59 -0800445 Configuration configuration = resources.getConfiguration();
446 isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
Winson Chungb3800242013-10-24 11:01:54 -0700447 isTablet = resources.getBoolean(R.bool.is_tablet);
448 isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
Winson Chung6033ceb2014-02-05 12:37:42 -0800449 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
450 isLayoutRtl = (configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
451 } else {
452 isLayoutRtl = false;
453 }
Winson Chungb3800242013-10-24 11:01:54 -0700454 widthPx = wPx;
455 heightPx = hPx;
456 availableWidthPx = awPx;
457 availableHeightPx = ahPx;
458
459 updateAvailableDimensions(context);
460 }
461
Adam Cohen091440a2015-03-18 14:16:05 -0700462 @Thunk float dist(PointF p0, PointF p1) {
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700463 return (float) Math.hypot(p1.x - p0.x, p1.y - p0.y);
Winson Chungb3800242013-10-24 11:01:54 -0700464 }
465
466 private float weight(PointF a, PointF b,
467 float pow) {
468 float d = dist(a, b);
469 if (d == 0f) {
470 return Float.POSITIVE_INFINITY;
471 }
472 return (float) (1f / Math.pow(d, pow));
473 }
474
Winson Chungbe876472014-05-14 14:15:20 -0700475 /** Returns the closest device profile given the width and height and a list of profiles */
476 private DeviceProfile findClosestDeviceProfile(float width, float height,
477 ArrayList<DeviceProfileQuery> points) {
478 return findClosestDeviceProfiles(width, height, points).get(0).profile;
479 }
480
481 /** Returns the closest device profiles ordered by closeness to the specified width and height */
482 private ArrayList<DeviceProfileQuery> findClosestDeviceProfiles(float width, float height,
483 ArrayList<DeviceProfileQuery> points) {
484 final PointF xy = new PointF(width, height);
485
486 // Sort the profiles by their closeness to the dimensions
487 ArrayList<DeviceProfileQuery> pointsByNearness = points;
488 Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
489 public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
490 return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
491 }
492 });
493
494 return pointsByNearness;
495 }
496
Winson Chungb3800242013-10-24 11:01:54 -0700497 private float invDistWeightedInterpolate(float width, float height,
498 ArrayList<DeviceProfileQuery> points) {
499 float sum = 0;
500 float weights = 0;
501 float pow = 5;
502 float kNearestNeighbors = 3;
503 final PointF xy = new PointF(width, height);
504
Winson Chungbe876472014-05-14 14:15:20 -0700505 ArrayList<DeviceProfileQuery> pointsByNearness = findClosestDeviceProfiles(width, height,
506 points);
Winson Chungb3800242013-10-24 11:01:54 -0700507
508 for (int i = 0; i < pointsByNearness.size(); ++i) {
509 DeviceProfileQuery p = pointsByNearness.get(i);
510 if (i < kNearestNeighbors) {
511 float w = weight(xy, p.dimens, pow);
512 if (w == Float.POSITIVE_INFINITY) {
513 return p.value;
514 }
515 weights += w;
516 }
517 }
518
519 for (int i = 0; i < pointsByNearness.size(); ++i) {
520 DeviceProfileQuery p = pointsByNearness.get(i);
521 if (i < kNearestNeighbors) {
522 float w = weight(xy, p.dimens, pow);
523 sum += w * p.value / weights;
524 }
525 }
526
527 return sum;
528 }
529
Winson Chung69e04ea2013-12-02 14:43:44 -0800530 /** Returns the search bar top offset */
531 int getSearchBarTopOffset() {
532 if (isTablet() && !isVerticalBarLayout()) {
533 return 4 * edgeMarginPx;
534 } else {
535 return 2 * edgeMarginPx;
536 }
537 }
538
Winson Chungb3800242013-10-24 11:01:54 -0700539 /** Returns the search bar bounds in the current orientation */
540 Rect getSearchBarBounds() {
541 return getSearchBarBounds(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
542 }
543 /** Returns the search bar bounds in the specified orientation */
544 Rect getSearchBarBounds(int orientation) {
545 Rect bounds = new Rect();
546 if (orientation == CellLayout.LANDSCAPE &&
547 transposeLayoutWithOrientation) {
Winson Chung42b3c062013-12-04 12:09:59 -0800548 if (isLayoutRtl) {
549 bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
550 availableWidthPx, availableHeightPx - edgeMarginPx);
551 } else {
552 bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx,
553 availableHeightPx - edgeMarginPx);
554 }
Winson Chungb3800242013-10-24 11:01:54 -0700555 } else {
556 if (isTablet()) {
557 // Pad the left and right of the workspace to ensure consistent spacing
558 // between all icons
559 int width = (orientation == CellLayout.LANDSCAPE)
560 ? Math.max(widthPx, heightPx)
561 : Math.min(widthPx, heightPx);
562 // XXX: If the icon size changes across orientations, we will have to take
563 // that into account here too.
564 int gap = (int) ((width - 2 * edgeMarginPx -
565 (numColumns * cellWidthPx)) / (2 * (numColumns + 1)));
Winson Chung2cb24712013-12-02 15:00:39 -0800566 bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
567 availableWidthPx - (edgeMarginPx + gap),
Winson Chungb3800242013-10-24 11:01:54 -0700568 searchBarSpaceHeightPx);
569 } else {
Winson Chung2cb24712013-12-02 15:00:39 -0800570 bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
571 getSearchBarTopOffset(),
Winson Chungb3800242013-10-24 11:01:54 -0700572 availableWidthPx - (desiredWorkspaceLeftRightMarginPx -
573 defaultWidgetPadding.right), searchBarSpaceHeightPx);
574 }
575 }
576 return bounds;
577 }
578
Winson Chunga6945242014-01-08 14:04:34 -0800579 /** Returns the bounds of the workspace page indicators. */
580 Rect getWorkspacePageIndicatorBounds(Rect insets) {
581 Rect workspacePadding = getWorkspacePadding();
Winson Chung205cd772014-01-15 14:31:59 -0800582 if (isLandscape && transposeLayoutWithOrientation) {
583 if (isLayoutRtl) {
584 return new Rect(workspacePadding.left, workspacePadding.top,
585 workspacePadding.left + pageIndicatorHeightPx,
586 heightPx - workspacePadding.bottom - insets.bottom);
587 } else {
588 int pageIndicatorLeft = widthPx - workspacePadding.right;
589 return new Rect(pageIndicatorLeft, workspacePadding.top,
590 pageIndicatorLeft + pageIndicatorHeightPx,
591 heightPx - workspacePadding.bottom - insets.bottom);
592 }
593 } else {
594 int pageIndicatorTop = heightPx - insets.bottom - workspacePadding.bottom;
595 return new Rect(workspacePadding.left, pageIndicatorTop,
596 widthPx - workspacePadding.right, pageIndicatorTop + pageIndicatorHeightPx);
597 }
Winson Chunga6945242014-01-08 14:04:34 -0800598 }
599
Adam Cohen59400422014-03-05 18:07:04 -0800600 public int getWorkspaceGridHeight() {
601 Rect p = getWorkspacePadding();
602 return availableHeightPx - p.top - p.bottom;
603 }
604
605 public int getWorkspaceGridWidth() {
606 Rect p = getWorkspacePadding();
607 return availableWidthPx - p.left - p.right;
608 }
609
Winson Chungb3800242013-10-24 11:01:54 -0700610 /** Returns the workspace padding in the specified orientation */
611 Rect getWorkspacePadding() {
612 return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
613 }
614 Rect getWorkspacePadding(int orientation) {
615 Rect searchBarBounds = getSearchBarBounds(orientation);
616 Rect padding = new Rect();
617 if (orientation == CellLayout.LANDSCAPE &&
618 transposeLayoutWithOrientation) {
619 // Pad the left and right of the workspace with search/hotseat bar sizes
Winson Chung42b3c062013-12-04 12:09:59 -0800620 if (isLayoutRtl) {
621 padding.set(hotseatBarHeightPx, edgeMarginPx,
622 searchBarBounds.width(), edgeMarginPx);
623 } else {
624 padding.set(searchBarBounds.width(), edgeMarginPx,
625 hotseatBarHeightPx, edgeMarginPx);
626 }
Winson Chungb3800242013-10-24 11:01:54 -0700627 } else {
628 if (isTablet()) {
629 // Pad the left and right of the workspace to ensure consistent spacing
630 // between all icons
Winson Chung59a488a2013-12-10 12:32:14 -0800631 float gapScale = 1f + (dragViewScale - 1f) / 2f;
Winson Chungb3800242013-10-24 11:01:54 -0700632 int width = (orientation == CellLayout.LANDSCAPE)
633 ? Math.max(widthPx, heightPx)
634 : Math.min(widthPx, heightPx);
Winson Chung59a488a2013-12-10 12:32:14 -0800635 int height = (orientation != CellLayout.LANDSCAPE)
636 ? Math.max(widthPx, heightPx)
637 : Math.min(widthPx, heightPx);
638 int paddingTop = searchBarBounds.bottom;
639 int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
640 int availableWidth = Math.max(0, width - (int) ((numColumns * cellWidthPx) +
641 (numColumns * gapScale * cellWidthPx)));
642 int availableHeight = Math.max(0, height - paddingTop - paddingBottom
643 - (int) (2 * numRows * cellHeightPx));
644 padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
645 availableWidth / 2, paddingBottom + availableHeight / 2);
Winson Chungb3800242013-10-24 11:01:54 -0700646 } else {
647 // Pad the top and bottom of the workspace with search/hotseat bar sizes
648 padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
649 searchBarBounds.bottom,
650 desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
651 hotseatBarHeightPx + pageIndicatorHeightPx);
652 }
653 }
654 return padding;
655 }
656
657 int getWorkspacePageSpacing(int orientation) {
Winson Chung59a488a2013-12-10 12:32:14 -0800658 if ((orientation == CellLayout.LANDSCAPE &&
659 transposeLayoutWithOrientation) || isLargeTablet()) {
Winson Chungb3800242013-10-24 11:01:54 -0700660 // In landscape mode the page spacing is set to the default.
661 return defaultPageSpacingPx;
662 } else {
663 // In portrait, we want the pages spaced such that there is no
664 // overhang of the previous / next page into the current page viewport.
665 // We assume symmetrical padding in portrait mode.
Adam Cohenefb31e32014-01-16 16:07:50 -0800666 return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding().left);
Winson Chungb3800242013-10-24 11:01:54 -0700667 }
668 }
669
670 Rect getOverviewModeButtonBarRect() {
671 int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
672 zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
673 Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
674 return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
675 }
676
677 float getOverviewModeScale() {
678 Rect workspacePadding = getWorkspacePadding();
679 Rect overviewBar = getOverviewModeButtonBarRect();
680 int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
681 return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
682 }
683
684 // The rect returned will be extended to below the system ui that covers the workspace
685 Rect getHotseatRect() {
686 if (isVerticalBarLayout()) {
687 return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
688 Integer.MAX_VALUE, availableHeightPx);
689 } else {
690 return new Rect(0, availableHeightPx - hotseatBarHeightPx,
691 availableWidthPx, Integer.MAX_VALUE);
692 }
693 }
694
695 int calculateCellWidth(int width, int countX) {
696 return width / countX;
697 }
698 int calculateCellHeight(int height, int countY) {
699 return height / countY;
700 }
701
702 boolean isPhone() {
703 return !isTablet && !isLargeTablet;
704 }
705 boolean isTablet() {
706 return isTablet;
707 }
708 boolean isLargeTablet() {
709 return isLargeTablet;
710 }
711
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700712 /**
713 * When {@code true}, hotseat is on the bottom row when in landscape mode.
714 * If {@code false}, hotseat is on the right column when in landscape mode.
715 */
Winson Chungb3800242013-10-24 11:01:54 -0700716 boolean isVerticalBarLayout() {
717 return isLandscape && transposeLayoutWithOrientation;
718 }
719
720 boolean shouldFadeAdjacentWorkspaceScreens() {
721 return isVerticalBarLayout() || isLargeTablet();
722 }
723
Jorim Jaggid017f882014-01-14 17:08:48 -0800724 int getVisibleChildCount(ViewGroup parent) {
725 int visibleChildren = 0;
726 for (int i = 0; i < parent.getChildCount(); i++) {
727 if (parent.getChildAt(i).getVisibility() != View.GONE) {
728 visibleChildren++;
729 }
730 }
731 return visibleChildren;
732 }
733
Winson Chungb3800242013-10-24 11:01:54 -0700734 public void layout(Launcher launcher) {
735 FrameLayout.LayoutParams lp;
736 Resources res = launcher.getResources();
737 boolean hasVerticalBarLayout = isVerticalBarLayout();
738
739 // Layout the search bar space
740 View searchBar = launcher.getSearchBar();
741 lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
742 if (hasVerticalBarLayout) {
Winson Chung69e04ea2013-12-02 14:43:44 -0800743 // Vertical search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700744 lp.gravity = Gravity.TOP | Gravity.LEFT;
745 lp.width = searchBarSpaceHeightPx;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800746 lp.height = LayoutParams.WRAP_CONTENT;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800747
748 LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
749 targets.setOrientation(LinearLayout.VERTICAL);
Winson Chungb3800242013-10-24 11:01:54 -0700750 } else {
Winson Chung69e04ea2013-12-02 14:43:44 -0800751 // Horizontal search bar space
Winson Chungb3800242013-10-24 11:01:54 -0700752 lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
753 lp.width = searchBarSpaceWidthPx;
754 lp.height = searchBarSpaceHeightPx;
Winson Chungb3800242013-10-24 11:01:54 -0700755 }
756 searchBar.setLayoutParams(lp);
757
Winson Chungb3800242013-10-24 11:01:54 -0700758 // Layout the workspace
759 PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
760 lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
761 lp.gravity = Gravity.CENTER;
762 int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
763 Rect padding = getWorkspacePadding(orientation);
764 workspace.setLayoutParams(lp);
765 workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
766 workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
767
768 // Layout the hotseat
769 View hotseat = launcher.findViewById(R.id.hotseat);
770 lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
771 if (hasVerticalBarLayout) {
772 // Vertical hotseat
Winson Chung42b3c062013-12-04 12:09:59 -0800773 lp.gravity = Gravity.END;
Winson Chungb3800242013-10-24 11:01:54 -0700774 lp.width = hotseatBarHeightPx;
775 lp.height = LayoutParams.MATCH_PARENT;
776 hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
777 } else if (isTablet()) {
Winson Chung59a488a2013-12-10 12:32:14 -0800778 // Pad the hotseat with the workspace padding calculated above
Winson Chungb3800242013-10-24 11:01:54 -0700779 lp.gravity = Gravity.BOTTOM;
780 lp.width = LayoutParams.MATCH_PARENT;
781 lp.height = hotseatBarHeightPx;
Winson Chung59a488a2013-12-10 12:32:14 -0800782 hotseat.setPadding(edgeMarginPx + padding.left, 0,
783 edgeMarginPx + padding.right,
Winson Chungb3800242013-10-24 11:01:54 -0700784 2 * edgeMarginPx);
785 } else {
786 // For phones, layout the hotseat without any bottom margin
787 // to ensure that we have space for the folders
788 lp.gravity = Gravity.BOTTOM;
789 lp.width = LayoutParams.MATCH_PARENT;
790 lp.height = hotseatBarHeightPx;
Sunny Goyale0bca382015-04-06 18:39:22 +0000791 hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0,
792 2 * edgeMarginPx, 0);
Winson Chungb3800242013-10-24 11:01:54 -0700793 }
794 hotseat.setLayoutParams(lp);
795
796 // Layout the page indicators
797 View pageIndicator = launcher.findViewById(R.id.page_indicator);
798 if (pageIndicator != null) {
799 if (hasVerticalBarLayout) {
800 // Hide the page indicators when we have vertical search/hotseat
801 pageIndicator.setVisibility(View.GONE);
802 } else {
803 // Put the page indicators above the hotseat
804 lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
805 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
806 lp.width = LayoutParams.WRAP_CONTENT;
807 lp.height = LayoutParams.WRAP_CONTENT;
808 lp.bottomMargin = hotseatBarHeightPx;
809 pageIndicator.setLayoutParams(lp);
810 }
811 }
812
Winson Chungb3800242013-10-24 11:01:54 -0700813 // Layout the Overview Mode
Jorim Jaggid017f882014-01-14 17:08:48 -0800814 ViewGroup overviewMode = launcher.getOverviewPanel();
Winson Chungb3800242013-10-24 11:01:54 -0700815 if (overviewMode != null) {
816 Rect r = getOverviewModeButtonBarRect();
817 lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
818 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700819
820 int visibleChildCount = getVisibleChildCount(overviewMode);
821 int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
822 int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx;
823
824 lp.width = Math.min(availableWidthPx, maxWidth);
Winson Chungb3800242013-10-24 11:01:54 -0700825 lp.height = r.height();
826 overviewMode.setLayoutParams(lp);
Sunny Goyal8dfe2da2014-10-24 16:26:52 -0700827
828 if (lp.width > totalItemWidth && visibleChildCount > 1) {
829 // We have enough space. Lets add some margin too.
830 int margin = (lp.width - totalItemWidth) / (visibleChildCount-1);
831 View lastChild = null;
832
833 // Set margin of all visible children except the last visible child
834 for (int i = 0; i < visibleChildCount; i++) {
835 if (lastChild != null) {
836 MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
837 if (isLayoutRtl) {
838 clp.leftMargin = margin;
839 } else {
840 clp.rightMargin = margin;
841 }
842 lastChild.setLayoutParams(clp);
843 lastChild = null;
844 }
845 View thisChild = overviewMode.getChildAt(i);
846 if (thisChild.getVisibility() != View.GONE) {
847 lastChild = thisChild;
848 }
849 }
850 }
Winson Chungb3800242013-10-24 11:01:54 -0700851 }
852 }
853}