Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.appwidget.AppWidgetHostView; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.res.Configuration; |
| 23 | import android.content.res.Resources; |
| 24 | import android.graphics.Paint; |
| 25 | import android.graphics.Paint.FontMetrics; |
| 26 | import android.graphics.Point; |
| 27 | import android.graphics.PointF; |
| 28 | import android.graphics.Rect; |
| 29 | import android.util.DisplayMetrics; |
| 30 | import android.view.Display; |
| 31 | import android.view.Gravity; |
| 32 | import android.view.Surface; |
| 33 | import android.view.View; |
| 34 | import android.view.ViewGroup.LayoutParams; |
| 35 | import android.view.WindowManager; |
| 36 | import android.widget.FrameLayout; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.Collections; |
| 40 | import java.util.Comparator; |
| 41 | |
| 42 | |
| 43 | class DeviceProfileQuery { |
| 44 | float widthDps; |
| 45 | float heightDps; |
| 46 | float value; |
| 47 | PointF dimens; |
| 48 | |
| 49 | DeviceProfileQuery(float w, float h, float v) { |
| 50 | widthDps = w; |
| 51 | heightDps = h; |
| 52 | value = v; |
| 53 | dimens = new PointF(w, h); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public class DeviceProfile { |
| 58 | public static interface DeviceProfileCallbacks { |
| 59 | public void onAvailableSizeChanged(DeviceProfile grid); |
| 60 | } |
| 61 | |
| 62 | String name; |
| 63 | float minWidthDps; |
| 64 | float minHeightDps; |
| 65 | float numRows; |
| 66 | float numColumns; |
| 67 | float numHotseatIcons; |
| 68 | private float iconSize; |
| 69 | private float iconTextSize; |
| 70 | private int iconDrawablePaddingOriginalPx; |
| 71 | private float hotseatIconSize; |
| 72 | |
| 73 | boolean isLandscape; |
| 74 | boolean isTablet; |
| 75 | boolean isLargeTablet; |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 76 | boolean isLayoutRtl; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 77 | boolean transposeLayoutWithOrientation; |
| 78 | |
| 79 | int desiredWorkspaceLeftRightMarginPx; |
| 80 | int edgeMarginPx; |
| 81 | Rect defaultWidgetPadding; |
| 82 | |
| 83 | int widthPx; |
| 84 | int heightPx; |
| 85 | int availableWidthPx; |
| 86 | int availableHeightPx; |
| 87 | int defaultPageSpacingPx; |
| 88 | |
| 89 | int overviewModeMinIconZoneHeightPx; |
| 90 | int overviewModeMaxIconZoneHeightPx; |
| 91 | int overviewModeMaxBarWidthPx; |
| 92 | float overviewModeIconZoneRatio; |
| 93 | float overviewModeScaleFactor; |
| 94 | |
| 95 | int iconSizePx; |
| 96 | int iconTextSizePx; |
| 97 | int iconDrawablePaddingPx; |
| 98 | int cellWidthPx; |
| 99 | int cellHeightPx; |
| 100 | int allAppsIconSizePx; |
| 101 | int allAppsIconTextSizePx; |
| 102 | int allAppsCellWidthPx; |
| 103 | int allAppsCellHeightPx; |
| 104 | int allAppsCellPaddingPx; |
| 105 | int folderBackgroundOffset; |
| 106 | int folderIconSizePx; |
| 107 | int folderCellWidthPx; |
| 108 | int folderCellHeightPx; |
| 109 | int hotseatCellWidthPx; |
| 110 | int hotseatCellHeightPx; |
| 111 | int hotseatIconSizePx; |
| 112 | int hotseatBarHeightPx; |
| 113 | int hotseatAllAppsRank; |
| 114 | int allAppsNumRows; |
| 115 | int allAppsNumCols; |
| 116 | int searchBarSpaceWidthPx; |
| 117 | int searchBarSpaceMaxWidthPx; |
| 118 | int searchBarSpaceHeightPx; |
| 119 | int searchBarHeightPx; |
| 120 | int pageIndicatorHeightPx; |
| 121 | |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 122 | float dragViewScale; |
| 123 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 124 | private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>(); |
| 125 | |
| 126 | DeviceProfile(String n, float w, float h, float r, float c, |
| 127 | float is, float its, float hs, float his) { |
| 128 | // Ensure that we have an odd number of hotseat items (since we need to place all apps) |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame^] | 129 | if (!LauncherAppState.isDisableAllApps() && hs % 2 == 0) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 130 | throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces"); |
| 131 | } |
| 132 | |
| 133 | name = n; |
| 134 | minWidthDps = w; |
| 135 | minHeightDps = h; |
| 136 | numRows = r; |
| 137 | numColumns = c; |
| 138 | iconSize = is; |
| 139 | iconTextSize = its; |
| 140 | numHotseatIcons = hs; |
| 141 | hotseatIconSize = his; |
| 142 | } |
| 143 | |
| 144 | DeviceProfile(Context context, |
| 145 | ArrayList<DeviceProfile> profiles, |
| 146 | float minWidth, float minHeight, |
| 147 | int wPx, int hPx, |
| 148 | int awPx, int ahPx, |
| 149 | Resources res) { |
| 150 | DisplayMetrics dm = res.getDisplayMetrics(); |
| 151 | ArrayList<DeviceProfileQuery> points = |
| 152 | new ArrayList<DeviceProfileQuery>(); |
| 153 | transposeLayoutWithOrientation = |
| 154 | res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
| 155 | minWidthDps = minWidth; |
| 156 | minHeightDps = minHeight; |
| 157 | |
| 158 | ComponentName cn = new ComponentName(context.getPackageName(), |
| 159 | this.getClass().getName()); |
| 160 | defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null); |
| 161 | edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin); |
| 162 | desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx; |
| 163 | pageIndicatorHeightPx = |
| 164 | res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height); |
| 165 | defaultPageSpacingPx = |
| 166 | res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing); |
| 167 | allAppsCellPaddingPx = |
| 168 | res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_padding); |
| 169 | overviewModeMinIconZoneHeightPx = |
| 170 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height); |
| 171 | overviewModeMaxIconZoneHeightPx = |
| 172 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height); |
| 173 | overviewModeMaxBarWidthPx = |
| 174 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_max_width); |
| 175 | overviewModeIconZoneRatio = |
| 176 | res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f; |
| 177 | overviewModeScaleFactor = |
| 178 | res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f; |
| 179 | |
| 180 | // Interpolate the rows |
| 181 | for (DeviceProfile p : profiles) { |
| 182 | points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numRows)); |
| 183 | } |
| 184 | numRows = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points)); |
| 185 | // Interpolate the columns |
| 186 | points.clear(); |
| 187 | for (DeviceProfile p : profiles) { |
| 188 | points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numColumns)); |
| 189 | } |
| 190 | numColumns = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points)); |
| 191 | // Interpolate the hotseat length |
| 192 | points.clear(); |
| 193 | for (DeviceProfile p : profiles) { |
| 194 | points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numHotseatIcons)); |
| 195 | } |
| 196 | numHotseatIcons = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points)); |
| 197 | hotseatAllAppsRank = (int) (numHotseatIcons / 2); |
| 198 | |
| 199 | // Interpolate the icon size |
| 200 | points.clear(); |
| 201 | for (DeviceProfile p : profiles) { |
| 202 | points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconSize)); |
| 203 | } |
| 204 | iconSize = invDistWeightedInterpolate(minWidth, minHeight, points); |
| 205 | // AllApps uses the original non-scaled icon size |
| 206 | allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm); |
| 207 | |
| 208 | // Interpolate the icon text size |
| 209 | points.clear(); |
| 210 | for (DeviceProfile p : profiles) { |
| 211 | points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconTextSize)); |
| 212 | } |
| 213 | iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points); |
| 214 | iconDrawablePaddingOriginalPx = |
| 215 | res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding); |
| 216 | // AllApps uses the original non-scaled icon text size |
| 217 | allAppsIconTextSizePx = DynamicGrid.pxFromDp(iconTextSize, dm); |
| 218 | |
| 219 | // Interpolate the hotseat icon size |
| 220 | points.clear(); |
| 221 | for (DeviceProfile p : profiles) { |
| 222 | points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.hotseatIconSize)); |
| 223 | } |
| 224 | // Hotseat |
| 225 | hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points); |
| 226 | |
| 227 | // Calculate the remaining vars |
| 228 | updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx); |
| 229 | updateAvailableDimensions(context); |
| 230 | } |
| 231 | |
| 232 | void addCallback(DeviceProfileCallbacks cb) { |
| 233 | mCallbacks.add(cb); |
| 234 | cb.onAvailableSizeChanged(this); |
| 235 | } |
| 236 | void removeCallback(DeviceProfileCallbacks cb) { |
| 237 | mCallbacks.remove(cb); |
| 238 | } |
| 239 | |
| 240 | private int getDeviceOrientation(Context context) { |
| 241 | WindowManager windowManager = (WindowManager) |
| 242 | context.getSystemService(Context.WINDOW_SERVICE); |
| 243 | Resources resources = context.getResources(); |
| 244 | DisplayMetrics dm = resources.getDisplayMetrics(); |
| 245 | Configuration config = resources.getConfiguration(); |
| 246 | int rotation = windowManager.getDefaultDisplay().getRotation(); |
| 247 | |
| 248 | boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) && |
| 249 | (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180); |
| 250 | boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) && |
| 251 | (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270); |
| 252 | if (isLandscape || isRotatedPortrait) { |
| 253 | return CellLayout.LANDSCAPE; |
| 254 | } else { |
| 255 | return CellLayout.PORTRAIT; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private void updateAvailableDimensions(Context context) { |
| 260 | WindowManager windowManager = (WindowManager) |
| 261 | context.getSystemService(Context.WINDOW_SERVICE); |
| 262 | Display display = windowManager.getDefaultDisplay(); |
| 263 | Resources resources = context.getResources(); |
| 264 | DisplayMetrics dm = resources.getDisplayMetrics(); |
| 265 | Configuration config = resources.getConfiguration(); |
| 266 | |
| 267 | // There are three possible configurations that the dynamic grid accounts for, portrait, |
| 268 | // landscape with the nav bar at the bottom, and landscape with the nav bar at the side. |
| 269 | // To prevent waiting for fitSystemWindows(), we make the observation that in landscape, |
| 270 | // the height is the smallest height (either with the nav bar at the bottom or to the |
| 271 | // side) and otherwise, the height is simply the largest possible height for a portrait |
| 272 | // device. |
| 273 | Point size = new Point(); |
| 274 | Point smallestSize = new Point(); |
| 275 | Point largestSize = new Point(); |
| 276 | display.getSize(size); |
| 277 | display.getCurrentSizeRange(smallestSize, largestSize); |
| 278 | availableWidthPx = size.x; |
| 279 | if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 280 | availableHeightPx = smallestSize.y; |
| 281 | } else { |
| 282 | availableHeightPx = largestSize.y; |
| 283 | } |
| 284 | |
| 285 | // Check to see if the icons fit in the new available height. If not, then we need to |
| 286 | // shrink the icon size. |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 287 | float scale = 1f; |
| 288 | int drawablePadding = iconDrawablePaddingOriginalPx; |
| 289 | updateIconSize(1f, drawablePadding, resources, dm); |
| 290 | float usedHeight = (cellHeightPx * numRows); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 291 | |
| 292 | Rect workspacePadding = getWorkspacePadding(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 293 | int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom); |
| 294 | if (usedHeight > maxHeight) { |
| 295 | scale = maxHeight / usedHeight; |
| 296 | drawablePadding = 0; |
| 297 | } |
| 298 | updateIconSize(scale, drawablePadding, resources, dm); |
| 299 | |
| 300 | // Make the callbacks |
| 301 | for (DeviceProfileCallbacks cb : mCallbacks) { |
| 302 | cb.onAvailableSizeChanged(this); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | private void updateIconSize(float scale, int drawablePadding, Resources resources, |
| 307 | DisplayMetrics dm) { |
| 308 | iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale); |
| 309 | iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale); |
| 310 | iconDrawablePaddingPx = drawablePadding; |
| 311 | hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale); |
| 312 | |
| 313 | // Search Bar |
| 314 | searchBarSpaceMaxWidthPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width); |
| 315 | searchBarHeightPx = resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height); |
| 316 | searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx); |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 317 | searchBarSpaceHeightPx = searchBarHeightPx + getSearchBarTopOffset(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 318 | |
| 319 | // Calculate the actual text height |
| 320 | Paint textPaint = new Paint(); |
| 321 | textPaint.setTextSize(iconTextSizePx); |
| 322 | FontMetrics fm = textPaint.getFontMetrics(); |
| 323 | cellWidthPx = iconSizePx; |
| 324 | cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 325 | final float scaleDps = resources.getDimensionPixelSize(R.dimen.dragViewScale); |
| 326 | dragViewScale = (iconSizePx + scaleDps) / iconSizePx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 327 | |
| 328 | // Hotseat |
| 329 | hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx; |
| 330 | hotseatCellWidthPx = iconSizePx; |
| 331 | hotseatCellHeightPx = iconSizePx; |
| 332 | |
| 333 | // Folder |
| 334 | folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx; |
| 335 | folderCellHeightPx = cellHeightPx + edgeMarginPx; |
| 336 | folderBackgroundOffset = -edgeMarginPx; |
| 337 | folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset; |
| 338 | |
| 339 | // All Apps |
| 340 | Rect padding = getWorkspacePadding(isLandscape ? |
| 341 | CellLayout.LANDSCAPE : CellLayout.PORTRAIT); |
| 342 | int pageIndicatorOffset = |
| 343 | resources.getDimensionPixelSize(R.dimen.apps_customize_page_indicator_offset); |
| 344 | allAppsCellWidthPx = allAppsIconSizePx; |
| 345 | allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + iconTextSizePx; |
| 346 | int maxLongEdgeCellCount = |
| 347 | resources.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count); |
| 348 | int maxShortEdgeCellCount = |
| 349 | resources.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count); |
| 350 | int minEdgeCellCount = |
| 351 | resources.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count); |
| 352 | int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount); |
| 353 | int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount); |
| 354 | |
| 355 | allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx) / |
| 356 | (allAppsCellHeightPx + allAppsCellPaddingPx); |
| 357 | allAppsNumRows = Math.max(minEdgeCellCount, Math.min(maxRows, allAppsNumRows)); |
| 358 | allAppsNumCols = (availableWidthPx) / |
| 359 | (allAppsCellWidthPx + allAppsCellPaddingPx); |
| 360 | allAppsNumCols = Math.max(minEdgeCellCount, Math.min(maxCols, allAppsNumCols)); |
| 361 | } |
| 362 | |
| 363 | void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx, |
| 364 | int awPx, int ahPx) { |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 365 | Configuration configuration = resources.getConfiguration(); |
| 366 | isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 367 | isTablet = resources.getBoolean(R.bool.is_tablet); |
| 368 | isLargeTablet = resources.getBoolean(R.bool.is_large_tablet); |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 369 | isLayoutRtl = (configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 370 | widthPx = wPx; |
| 371 | heightPx = hPx; |
| 372 | availableWidthPx = awPx; |
| 373 | availableHeightPx = ahPx; |
| 374 | |
| 375 | updateAvailableDimensions(context); |
| 376 | } |
| 377 | |
| 378 | private float dist(PointF p0, PointF p1) { |
| 379 | return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) + |
| 380 | (p1.y-p0.y)*(p1.y-p0.y)); |
| 381 | } |
| 382 | |
| 383 | private float weight(PointF a, PointF b, |
| 384 | float pow) { |
| 385 | float d = dist(a, b); |
| 386 | if (d == 0f) { |
| 387 | return Float.POSITIVE_INFINITY; |
| 388 | } |
| 389 | return (float) (1f / Math.pow(d, pow)); |
| 390 | } |
| 391 | |
| 392 | private float invDistWeightedInterpolate(float width, float height, |
| 393 | ArrayList<DeviceProfileQuery> points) { |
| 394 | float sum = 0; |
| 395 | float weights = 0; |
| 396 | float pow = 5; |
| 397 | float kNearestNeighbors = 3; |
| 398 | final PointF xy = new PointF(width, height); |
| 399 | |
| 400 | ArrayList<DeviceProfileQuery> pointsByNearness = points; |
| 401 | Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() { |
| 402 | public int compare(DeviceProfileQuery a, DeviceProfileQuery b) { |
| 403 | return (int) (dist(xy, a.dimens) - dist(xy, b.dimens)); |
| 404 | } |
| 405 | }); |
| 406 | |
| 407 | for (int i = 0; i < pointsByNearness.size(); ++i) { |
| 408 | DeviceProfileQuery p = pointsByNearness.get(i); |
| 409 | if (i < kNearestNeighbors) { |
| 410 | float w = weight(xy, p.dimens, pow); |
| 411 | if (w == Float.POSITIVE_INFINITY) { |
| 412 | return p.value; |
| 413 | } |
| 414 | weights += w; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | for (int i = 0; i < pointsByNearness.size(); ++i) { |
| 419 | DeviceProfileQuery p = pointsByNearness.get(i); |
| 420 | if (i < kNearestNeighbors) { |
| 421 | float w = weight(xy, p.dimens, pow); |
| 422 | sum += w * p.value / weights; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return sum; |
| 427 | } |
| 428 | |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 429 | /** Returns the search bar top offset */ |
| 430 | int getSearchBarTopOffset() { |
| 431 | if (isTablet() && !isVerticalBarLayout()) { |
| 432 | return 4 * edgeMarginPx; |
| 433 | } else { |
| 434 | return 2 * edgeMarginPx; |
| 435 | } |
| 436 | } |
| 437 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 438 | /** Returns the search bar bounds in the current orientation */ |
| 439 | Rect getSearchBarBounds() { |
| 440 | return getSearchBarBounds(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT); |
| 441 | } |
| 442 | /** Returns the search bar bounds in the specified orientation */ |
| 443 | Rect getSearchBarBounds(int orientation) { |
| 444 | Rect bounds = new Rect(); |
| 445 | if (orientation == CellLayout.LANDSCAPE && |
| 446 | transposeLayoutWithOrientation) { |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 447 | if (isLayoutRtl) { |
| 448 | bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx, |
| 449 | availableWidthPx, availableHeightPx - edgeMarginPx); |
| 450 | } else { |
| 451 | bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx, |
| 452 | availableHeightPx - edgeMarginPx); |
| 453 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 454 | } else { |
| 455 | if (isTablet()) { |
| 456 | // Pad the left and right of the workspace to ensure consistent spacing |
| 457 | // between all icons |
| 458 | int width = (orientation == CellLayout.LANDSCAPE) |
| 459 | ? Math.max(widthPx, heightPx) |
| 460 | : Math.min(widthPx, heightPx); |
| 461 | // XXX: If the icon size changes across orientations, we will have to take |
| 462 | // that into account here too. |
| 463 | int gap = (int) ((width - 2 * edgeMarginPx - |
| 464 | (numColumns * cellWidthPx)) / (2 * (numColumns + 1))); |
Winson Chung | 2cb2471 | 2013-12-02 15:00:39 -0800 | [diff] [blame] | 465 | bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(), |
| 466 | availableWidthPx - (edgeMarginPx + gap), |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 467 | searchBarSpaceHeightPx); |
| 468 | } else { |
Winson Chung | 2cb2471 | 2013-12-02 15:00:39 -0800 | [diff] [blame] | 469 | bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left, |
| 470 | getSearchBarTopOffset(), |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 471 | availableWidthPx - (desiredWorkspaceLeftRightMarginPx - |
| 472 | defaultWidgetPadding.right), searchBarSpaceHeightPx); |
| 473 | } |
| 474 | } |
| 475 | return bounds; |
| 476 | } |
| 477 | |
| 478 | /** Returns the workspace padding in the specified orientation */ |
| 479 | Rect getWorkspacePadding() { |
| 480 | return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT); |
| 481 | } |
| 482 | Rect getWorkspacePadding(int orientation) { |
| 483 | Rect searchBarBounds = getSearchBarBounds(orientation); |
| 484 | Rect padding = new Rect(); |
| 485 | if (orientation == CellLayout.LANDSCAPE && |
| 486 | transposeLayoutWithOrientation) { |
| 487 | // Pad the left and right of the workspace with search/hotseat bar sizes |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 488 | if (isLayoutRtl) { |
| 489 | padding.set(hotseatBarHeightPx, edgeMarginPx, |
| 490 | searchBarBounds.width(), edgeMarginPx); |
| 491 | } else { |
| 492 | padding.set(searchBarBounds.width(), edgeMarginPx, |
| 493 | hotseatBarHeightPx, edgeMarginPx); |
| 494 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 495 | } else { |
| 496 | if (isTablet()) { |
| 497 | // Pad the left and right of the workspace to ensure consistent spacing |
| 498 | // between all icons |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 499 | float gapScale = 1f + (dragViewScale - 1f) / 2f; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 500 | int width = (orientation == CellLayout.LANDSCAPE) |
| 501 | ? Math.max(widthPx, heightPx) |
| 502 | : Math.min(widthPx, heightPx); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 503 | int height = (orientation != CellLayout.LANDSCAPE) |
| 504 | ? Math.max(widthPx, heightPx) |
| 505 | : Math.min(widthPx, heightPx); |
| 506 | int paddingTop = searchBarBounds.bottom; |
| 507 | int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx; |
| 508 | int availableWidth = Math.max(0, width - (int) ((numColumns * cellWidthPx) + |
| 509 | (numColumns * gapScale * cellWidthPx))); |
| 510 | int availableHeight = Math.max(0, height - paddingTop - paddingBottom |
| 511 | - (int) (2 * numRows * cellHeightPx)); |
| 512 | padding.set(availableWidth / 2, paddingTop + availableHeight / 2, |
| 513 | availableWidth / 2, paddingBottom + availableHeight / 2); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 514 | } else { |
| 515 | // Pad the top and bottom of the workspace with search/hotseat bar sizes |
| 516 | padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left, |
| 517 | searchBarBounds.bottom, |
| 518 | desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right, |
| 519 | hotseatBarHeightPx + pageIndicatorHeightPx); |
| 520 | } |
| 521 | } |
| 522 | return padding; |
| 523 | } |
| 524 | |
| 525 | int getWorkspacePageSpacing(int orientation) { |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 526 | if ((orientation == CellLayout.LANDSCAPE && |
| 527 | transposeLayoutWithOrientation) || isLargeTablet()) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 528 | // In landscape mode the page spacing is set to the default. |
| 529 | return defaultPageSpacingPx; |
| 530 | } else { |
| 531 | // In portrait, we want the pages spaced such that there is no |
| 532 | // overhang of the previous / next page into the current page viewport. |
| 533 | // We assume symmetrical padding in portrait mode. |
| 534 | return 2 * getWorkspacePadding().left; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | Rect getOverviewModeButtonBarRect() { |
| 539 | int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx); |
| 540 | zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx, |
| 541 | Math.max(overviewModeMinIconZoneHeightPx, zoneHeight)); |
| 542 | return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx); |
| 543 | } |
| 544 | |
| 545 | float getOverviewModeScale() { |
| 546 | Rect workspacePadding = getWorkspacePadding(); |
| 547 | Rect overviewBar = getOverviewModeButtonBarRect(); |
| 548 | int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom; |
| 549 | return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace; |
| 550 | } |
| 551 | |
| 552 | // The rect returned will be extended to below the system ui that covers the workspace |
| 553 | Rect getHotseatRect() { |
| 554 | if (isVerticalBarLayout()) { |
| 555 | return new Rect(availableWidthPx - hotseatBarHeightPx, 0, |
| 556 | Integer.MAX_VALUE, availableHeightPx); |
| 557 | } else { |
| 558 | return new Rect(0, availableHeightPx - hotseatBarHeightPx, |
| 559 | availableWidthPx, Integer.MAX_VALUE); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | int calculateCellWidth(int width, int countX) { |
| 564 | return width / countX; |
| 565 | } |
| 566 | int calculateCellHeight(int height, int countY) { |
| 567 | return height / countY; |
| 568 | } |
| 569 | |
| 570 | boolean isPhone() { |
| 571 | return !isTablet && !isLargeTablet; |
| 572 | } |
| 573 | boolean isTablet() { |
| 574 | return isTablet; |
| 575 | } |
| 576 | boolean isLargeTablet() { |
| 577 | return isLargeTablet; |
| 578 | } |
| 579 | |
| 580 | boolean isVerticalBarLayout() { |
| 581 | return isLandscape && transposeLayoutWithOrientation; |
| 582 | } |
| 583 | |
| 584 | boolean shouldFadeAdjacentWorkspaceScreens() { |
| 585 | return isVerticalBarLayout() || isLargeTablet(); |
| 586 | } |
| 587 | |
| 588 | public void layout(Launcher launcher) { |
| 589 | FrameLayout.LayoutParams lp; |
| 590 | Resources res = launcher.getResources(); |
| 591 | boolean hasVerticalBarLayout = isVerticalBarLayout(); |
| 592 | |
| 593 | // Layout the search bar space |
| 594 | View searchBar = launcher.getSearchBar(); |
| 595 | lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams(); |
| 596 | if (hasVerticalBarLayout) { |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 597 | // Vertical search bar space |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 598 | lp.gravity = Gravity.TOP | Gravity.LEFT; |
| 599 | lp.width = searchBarSpaceHeightPx; |
| 600 | lp.height = LayoutParams.MATCH_PARENT; |
| 601 | searchBar.setPadding( |
| 602 | 0, 2 * edgeMarginPx, 0, |
| 603 | 2 * edgeMarginPx); |
| 604 | } else { |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 605 | // Horizontal search bar space |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 606 | lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; |
| 607 | lp.width = searchBarSpaceWidthPx; |
| 608 | lp.height = searchBarSpaceHeightPx; |
| 609 | searchBar.setPadding( |
| 610 | 2 * edgeMarginPx, |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 611 | getSearchBarTopOffset(), |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 612 | 2 * edgeMarginPx, 0); |
| 613 | } |
| 614 | searchBar.setLayoutParams(lp); |
| 615 | |
| 616 | // Layout the search bar |
| 617 | View qsbBar = launcher.getQsbBar(); |
| 618 | LayoutParams vglp = qsbBar.getLayoutParams(); |
| 619 | vglp.width = LayoutParams.MATCH_PARENT; |
| 620 | vglp.height = LayoutParams.MATCH_PARENT; |
| 621 | qsbBar.setLayoutParams(vglp); |
| 622 | |
| 623 | // Layout the voice proxy |
| 624 | View voiceButtonProxy = launcher.findViewById(R.id.voice_button_proxy); |
| 625 | if (voiceButtonProxy != null) { |
| 626 | if (hasVerticalBarLayout) { |
| 627 | // TODO: MOVE THIS INTO SEARCH BAR MEASURE |
| 628 | } else { |
| 629 | lp = (FrameLayout.LayoutParams) voiceButtonProxy.getLayoutParams(); |
| 630 | lp.gravity = Gravity.TOP | Gravity.END; |
| 631 | lp.width = (widthPx - searchBarSpaceWidthPx) / 2 + |
| 632 | 2 * iconSizePx; |
| 633 | lp.height = searchBarSpaceHeightPx; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | // Layout the workspace |
| 638 | PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace); |
| 639 | lp = (FrameLayout.LayoutParams) workspace.getLayoutParams(); |
| 640 | lp.gravity = Gravity.CENTER; |
| 641 | int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT; |
| 642 | Rect padding = getWorkspacePadding(orientation); |
| 643 | workspace.setLayoutParams(lp); |
| 644 | workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom); |
| 645 | workspace.setPageSpacing(getWorkspacePageSpacing(orientation)); |
| 646 | |
| 647 | // Layout the hotseat |
| 648 | View hotseat = launcher.findViewById(R.id.hotseat); |
| 649 | lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams(); |
| 650 | if (hasVerticalBarLayout) { |
| 651 | // Vertical hotseat |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 652 | lp.gravity = Gravity.END; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 653 | lp.width = hotseatBarHeightPx; |
| 654 | lp.height = LayoutParams.MATCH_PARENT; |
| 655 | hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx); |
| 656 | } else if (isTablet()) { |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 657 | // Pad the hotseat with the workspace padding calculated above |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 658 | lp.gravity = Gravity.BOTTOM; |
| 659 | lp.width = LayoutParams.MATCH_PARENT; |
| 660 | lp.height = hotseatBarHeightPx; |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 661 | hotseat.setPadding(edgeMarginPx + padding.left, 0, |
| 662 | edgeMarginPx + padding.right, |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 663 | 2 * edgeMarginPx); |
| 664 | } else { |
| 665 | // For phones, layout the hotseat without any bottom margin |
| 666 | // to ensure that we have space for the folders |
| 667 | lp.gravity = Gravity.BOTTOM; |
| 668 | lp.width = LayoutParams.MATCH_PARENT; |
| 669 | lp.height = hotseatBarHeightPx; |
| 670 | hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0, |
| 671 | 2 * edgeMarginPx, 0); |
| 672 | } |
| 673 | hotseat.setLayoutParams(lp); |
| 674 | |
| 675 | // Layout the page indicators |
| 676 | View pageIndicator = launcher.findViewById(R.id.page_indicator); |
| 677 | if (pageIndicator != null) { |
| 678 | if (hasVerticalBarLayout) { |
| 679 | // Hide the page indicators when we have vertical search/hotseat |
| 680 | pageIndicator.setVisibility(View.GONE); |
| 681 | } else { |
| 682 | // Put the page indicators above the hotseat |
| 683 | lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams(); |
| 684 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
| 685 | lp.width = LayoutParams.WRAP_CONTENT; |
| 686 | lp.height = LayoutParams.WRAP_CONTENT; |
| 687 | lp.bottomMargin = hotseatBarHeightPx; |
| 688 | pageIndicator.setLayoutParams(lp); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | // Layout AllApps |
| 693 | AppsCustomizeTabHost host = (AppsCustomizeTabHost) |
| 694 | launcher.findViewById(R.id.apps_customize_pane); |
| 695 | if (host != null) { |
| 696 | // Center the all apps page indicator |
| 697 | int pageIndicatorHeight = (int) (pageIndicatorHeightPx * Math.min(1f, |
| 698 | (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX))); |
| 699 | pageIndicator = host.findViewById(R.id.apps_customize_page_indicator); |
| 700 | if (pageIndicator != null) { |
| 701 | lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams(); |
| 702 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
| 703 | lp.width = LayoutParams.WRAP_CONTENT; |
| 704 | lp.height = pageIndicatorHeight; |
| 705 | pageIndicator.setLayoutParams(lp); |
| 706 | } |
| 707 | |
| 708 | AppsCustomizePagedView pagedView = (AppsCustomizePagedView) |
| 709 | host.findViewById(R.id.apps_customize_pane_content); |
| 710 | padding = new Rect(); |
| 711 | if (pagedView != null) { |
| 712 | // Constrain the dimensions of all apps so that it does not span the full width |
| 713 | int paddingLR = (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) / |
| 714 | (2 * (allAppsNumCols + 1)); |
| 715 | int paddingTB = (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows)) / |
| 716 | (2 * (allAppsNumRows + 1)); |
| 717 | paddingLR = Math.min(paddingLR, (int)((paddingLR + paddingTB) * 0.75f)); |
| 718 | paddingTB = Math.min(paddingTB, (int)((paddingLR + paddingTB) * 0.75f)); |
| 719 | int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR)); |
| 720 | int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 2; |
Winson Chung | 495f44d | 2013-12-04 12:51:53 -0800 | [diff] [blame] | 721 | // Only adjust the side paddings on landscape phones, or tablets |
| 722 | if ((isTablet() || isLandscape) && gridPaddingLR > (allAppsCellWidthPx / 4)) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 723 | padding.left = padding.right = gridPaddingLR; |
| 724 | } |
| 725 | // The icons are centered, so we can't just offset by the page indicator height |
| 726 | // because the empty space will actually be pageIndicatorHeight + paddingTB |
| 727 | padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB); |
| 728 | pagedView.setAllAppsPadding(padding); |
| 729 | pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | // Layout the Overview Mode |
| 734 | View overviewMode = launcher.getOverviewPanel(); |
| 735 | if (overviewMode != null) { |
| 736 | Rect r = getOverviewModeButtonBarRect(); |
| 737 | lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams(); |
| 738 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
| 739 | lp.width = Math.min(availableWidthPx, overviewModeMaxBarWidthPx); |
| 740 | lp.height = r.height(); |
| 741 | overviewMode.setLayoutParams(lp); |
| 742 | } |
| 743 | } |
| 744 | } |