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