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; |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 34 | import android.view.ViewGroup; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 35 | import android.view.ViewGroup.LayoutParams; |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 36 | import android.view.ViewGroup.MarginLayoutParams; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 37 | import android.view.WindowManager; |
| 38 | import android.widget.FrameLayout; |
Adam Cohen | 24ce0b3 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 39 | import android.widget.LinearLayout; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 40 | |
| 41 | import java.util.ArrayList; |
| 42 | import java.util.Collections; |
| 43 | import java.util.Comparator; |
| 44 | |
| 45 | |
| 46 | class DeviceProfileQuery { |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 47 | DeviceProfile profile; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 48 | float widthDps; |
| 49 | float heightDps; |
| 50 | float value; |
| 51 | PointF dimens; |
| 52 | |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 53 | DeviceProfileQuery(DeviceProfile p, float v) { |
| 54 | widthDps = p.minWidthDps; |
| 55 | heightDps = p.minHeightDps; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 56 | value = v; |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 57 | dimens = new PointF(widthDps, heightDps); |
| 58 | profile = p; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | public class DeviceProfile { |
| 63 | public static interface DeviceProfileCallbacks { |
| 64 | public void onAvailableSizeChanged(DeviceProfile grid); |
| 65 | } |
| 66 | |
| 67 | String name; |
| 68 | float minWidthDps; |
| 69 | float minHeightDps; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 70 | public float numRows; |
| 71 | public float numColumns; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 72 | float numHotseatIcons; |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 73 | float iconSize; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 74 | private float iconTextSize; |
| 75 | private int iconDrawablePaddingOriginalPx; |
| 76 | private float hotseatIconSize; |
| 77 | |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 78 | int defaultLayoutId; |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 79 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 80 | boolean isLandscape; |
| 81 | boolean isTablet; |
| 82 | boolean isLargeTablet; |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 83 | boolean isLayoutRtl; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 84 | boolean transposeLayoutWithOrientation; |
| 85 | |
| 86 | int desiredWorkspaceLeftRightMarginPx; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 87 | public int edgeMarginPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 88 | Rect defaultWidgetPadding; |
| 89 | |
| 90 | int widthPx; |
| 91 | int heightPx; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 92 | public int availableWidthPx; |
| 93 | public int availableHeightPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 94 | int defaultPageSpacingPx; |
| 95 | |
| 96 | int overviewModeMinIconZoneHeightPx; |
| 97 | int overviewModeMaxIconZoneHeightPx; |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 98 | int overviewModeBarItemWidthPx; |
| 99 | int overviewModeBarSpacerWidthPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 100 | float overviewModeIconZoneRatio; |
| 101 | float overviewModeScaleFactor; |
| 102 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 103 | public int cellWidthPx; |
| 104 | public int cellHeightPx; |
| 105 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 106 | int iconSizePx; |
| 107 | int iconTextSizePx; |
| 108 | int iconDrawablePaddingPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 109 | int allAppsIconSizePx; |
| 110 | int allAppsIconTextSizePx; |
| 111 | int allAppsCellWidthPx; |
| 112 | int allAppsCellHeightPx; |
| 113 | int allAppsCellPaddingPx; |
| 114 | int folderBackgroundOffset; |
| 115 | int folderIconSizePx; |
| 116 | int folderCellWidthPx; |
| 117 | int folderCellHeightPx; |
| 118 | int hotseatCellWidthPx; |
| 119 | int hotseatCellHeightPx; |
| 120 | int hotseatIconSizePx; |
| 121 | int hotseatBarHeightPx; |
| 122 | int hotseatAllAppsRank; |
| 123 | int allAppsNumRows; |
| 124 | int allAppsNumCols; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 125 | int appsViewNumCols; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 126 | int searchBarSpaceWidthPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 127 | int searchBarSpaceHeightPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 128 | int pageIndicatorHeightPx; |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 129 | int allAppsButtonVisualSize; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 130 | |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 131 | float dragViewScale; |
| 132 | |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 133 | int allAppsShortEdgeCount = -1; |
| 134 | int allAppsLongEdgeCount = -1; |
| 135 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 136 | private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>(); |
| 137 | |
| 138 | DeviceProfile(String n, float w, float h, float r, float c, |
Adam Cohen | cee8c66 | 2014-10-16 09:49:52 -0700 | [diff] [blame] | 139 | float is, float its, float hs, float his, int dlId) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 140 | // Ensure that we have an odd number of hotseat items (since we need to place all apps) |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 141 | if (hs % 2 == 0) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 142 | throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces"); |
| 143 | } |
| 144 | |
| 145 | name = n; |
| 146 | minWidthDps = w; |
| 147 | minHeightDps = h; |
| 148 | numRows = r; |
| 149 | numColumns = c; |
| 150 | iconSize = is; |
| 151 | iconTextSize = its; |
| 152 | numHotseatIcons = hs; |
| 153 | hotseatIconSize = his; |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 154 | defaultLayoutId = dlId; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 157 | DeviceProfile() { |
| 158 | } |
| 159 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 160 | DeviceProfile(Context context, |
| 161 | ArrayList<DeviceProfile> profiles, |
| 162 | float minWidth, float minHeight, |
| 163 | int wPx, int hPx, |
| 164 | int awPx, int ahPx, |
| 165 | Resources res) { |
| 166 | DisplayMetrics dm = res.getDisplayMetrics(); |
| 167 | ArrayList<DeviceProfileQuery> points = |
| 168 | new ArrayList<DeviceProfileQuery>(); |
| 169 | transposeLayoutWithOrientation = |
| 170 | res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
| 171 | minWidthDps = minWidth; |
| 172 | minHeightDps = minHeight; |
| 173 | |
| 174 | ComponentName cn = new ComponentName(context.getPackageName(), |
| 175 | this.getClass().getName()); |
| 176 | defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null); |
| 177 | edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin); |
| 178 | desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx; |
| 179 | pageIndicatorHeightPx = |
| 180 | res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height); |
| 181 | defaultPageSpacingPx = |
| 182 | res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing); |
| 183 | allAppsCellPaddingPx = |
| 184 | res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_padding); |
| 185 | overviewModeMinIconZoneHeightPx = |
| 186 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height); |
| 187 | overviewModeMaxIconZoneHeightPx = |
| 188 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height); |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 189 | overviewModeBarItemWidthPx = |
| 190 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width); |
| 191 | overviewModeBarSpacerWidthPx = |
| 192 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 193 | overviewModeIconZoneRatio = |
| 194 | res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f; |
| 195 | overviewModeScaleFactor = |
| 196 | res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f; |
| 197 | |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 198 | // Find the closes profile given the width/height |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 199 | for (DeviceProfile p : profiles) { |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 200 | points.add(new DeviceProfileQuery(p, 0f)); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 201 | } |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 202 | DeviceProfile closestProfile = findClosestDeviceProfile(minWidth, minHeight, points); |
| 203 | |
| 204 | // Snap to the closest row count |
| 205 | numRows = closestProfile.numRows; |
| 206 | |
| 207 | // Snap to the closest column count |
| 208 | numColumns = closestProfile.numColumns; |
| 209 | |
| 210 | // Snap to the closest hotseat size |
| 211 | numHotseatIcons = closestProfile.numHotseatIcons; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 212 | hotseatAllAppsRank = (int) (numHotseatIcons / 2); |
| 213 | |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 214 | // Snap to the closest default layout id |
| 215 | defaultLayoutId = closestProfile.defaultLayoutId; |
| 216 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 217 | // Interpolate the icon size |
| 218 | points.clear(); |
| 219 | for (DeviceProfile p : profiles) { |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 220 | points.add(new DeviceProfileQuery(p, p.iconSize)); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 221 | } |
| 222 | iconSize = invDistWeightedInterpolate(minWidth, minHeight, points); |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 223 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 224 | // AllApps uses the original non-scaled icon size |
| 225 | allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm); |
| 226 | |
| 227 | // Interpolate the icon text size |
| 228 | points.clear(); |
| 229 | for (DeviceProfile p : profiles) { |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 230 | points.add(new DeviceProfileQuery(p, p.iconTextSize)); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 231 | } |
| 232 | iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points); |
| 233 | iconDrawablePaddingOriginalPx = |
| 234 | res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding); |
| 235 | // AllApps uses the original non-scaled icon text size |
| 236 | allAppsIconTextSizePx = DynamicGrid.pxFromDp(iconTextSize, dm); |
| 237 | |
| 238 | // Interpolate the hotseat icon size |
| 239 | points.clear(); |
| 240 | for (DeviceProfile p : profiles) { |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 241 | points.add(new DeviceProfileQuery(p, p.hotseatIconSize)); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 242 | } |
| 243 | // Hotseat |
| 244 | hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points); |
| 245 | |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 246 | // If the partner customization apk contains any grid overrides, apply them |
| 247 | applyPartnerDeviceProfileOverrides(context, dm); |
| 248 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 249 | // Calculate the remaining vars |
| 250 | updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx); |
| 251 | updateAvailableDimensions(context); |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 252 | computeAllAppsButtonSize(context); |
| 253 | } |
| 254 | |
| 255 | /** |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 256 | * Apply any Partner customization grid overrides. |
| 257 | * |
| 258 | * Currently we support: all apps row / column count. |
| 259 | */ |
| 260 | private void applyPartnerDeviceProfileOverrides(Context ctx, DisplayMetrics dm) { |
| 261 | Partner p = Partner.get(ctx.getPackageManager()); |
| 262 | if (p != null) { |
| 263 | DeviceProfile partnerDp = p.getDeviceProfileOverride(dm); |
| 264 | if (partnerDp != null) { |
| 265 | if (partnerDp.numRows > 0 && partnerDp.numColumns > 0) { |
| 266 | numRows = partnerDp.numRows; |
| 267 | numColumns = partnerDp.numColumns; |
| 268 | } |
| 269 | if (partnerDp.allAppsShortEdgeCount > 0 && partnerDp.allAppsLongEdgeCount > 0) { |
| 270 | allAppsShortEdgeCount = partnerDp.allAppsShortEdgeCount; |
| 271 | allAppsLongEdgeCount = partnerDp.allAppsLongEdgeCount; |
| 272 | } |
| 273 | if (partnerDp.iconSize > 0) { |
| 274 | iconSize = partnerDp.iconSize; |
| 275 | // AllApps uses the original non-scaled icon size |
| 276 | allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 283 | * Determine the exact visual footprint of the all apps button, taking into account scaling |
| 284 | * and internal padding of the drawable. |
| 285 | */ |
| 286 | private void computeAllAppsButtonSize(Context context) { |
| 287 | Resources res = context.getResources(); |
| 288 | float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f; |
| 289 | LauncherAppState app = LauncherAppState.getInstance(); |
| 290 | allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void addCallback(DeviceProfileCallbacks cb) { |
| 294 | mCallbacks.add(cb); |
| 295 | cb.onAvailableSizeChanged(this); |
| 296 | } |
| 297 | void removeCallback(DeviceProfileCallbacks cb) { |
| 298 | mCallbacks.remove(cb); |
| 299 | } |
| 300 | |
| 301 | private int getDeviceOrientation(Context context) { |
| 302 | WindowManager windowManager = (WindowManager) |
| 303 | context.getSystemService(Context.WINDOW_SERVICE); |
| 304 | Resources resources = context.getResources(); |
| 305 | DisplayMetrics dm = resources.getDisplayMetrics(); |
| 306 | Configuration config = resources.getConfiguration(); |
| 307 | int rotation = windowManager.getDefaultDisplay().getRotation(); |
| 308 | |
| 309 | boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) && |
| 310 | (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180); |
| 311 | boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) && |
| 312 | (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270); |
| 313 | if (isLandscape || isRotatedPortrait) { |
| 314 | return CellLayout.LANDSCAPE; |
| 315 | } else { |
| 316 | return CellLayout.PORTRAIT; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private void updateAvailableDimensions(Context context) { |
| 321 | WindowManager windowManager = (WindowManager) |
| 322 | context.getSystemService(Context.WINDOW_SERVICE); |
| 323 | Display display = windowManager.getDefaultDisplay(); |
| 324 | Resources resources = context.getResources(); |
| 325 | DisplayMetrics dm = resources.getDisplayMetrics(); |
| 326 | Configuration config = resources.getConfiguration(); |
| 327 | |
| 328 | // There are three possible configurations that the dynamic grid accounts for, portrait, |
| 329 | // landscape with the nav bar at the bottom, and landscape with the nav bar at the side. |
| 330 | // To prevent waiting for fitSystemWindows(), we make the observation that in landscape, |
| 331 | // the height is the smallest height (either with the nav bar at the bottom or to the |
| 332 | // side) and otherwise, the height is simply the largest possible height for a portrait |
| 333 | // device. |
| 334 | Point size = new Point(); |
| 335 | Point smallestSize = new Point(); |
| 336 | Point largestSize = new Point(); |
| 337 | display.getSize(size); |
| 338 | display.getCurrentSizeRange(smallestSize, largestSize); |
| 339 | availableWidthPx = size.x; |
| 340 | if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 341 | availableHeightPx = smallestSize.y; |
| 342 | } else { |
| 343 | availableHeightPx = largestSize.y; |
| 344 | } |
| 345 | |
| 346 | // Check to see if the icons fit in the new available height. If not, then we need to |
| 347 | // shrink the icon size. |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 348 | float scale = 1f; |
| 349 | int drawablePadding = iconDrawablePaddingOriginalPx; |
| 350 | updateIconSize(1f, drawablePadding, resources, dm); |
| 351 | float usedHeight = (cellHeightPx * numRows); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 352 | |
| 353 | Rect workspacePadding = getWorkspacePadding(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 354 | int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom); |
| 355 | if (usedHeight > maxHeight) { |
| 356 | scale = maxHeight / usedHeight; |
| 357 | drawablePadding = 0; |
| 358 | } |
| 359 | updateIconSize(scale, drawablePadding, resources, dm); |
| 360 | |
| 361 | // Make the callbacks |
| 362 | for (DeviceProfileCallbacks cb : mCallbacks) { |
| 363 | cb.onAvailableSizeChanged(this); |
| 364 | } |
| 365 | } |
| 366 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 367 | private void updateIconSize(float scale, int drawablePadding, Resources res, |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 368 | DisplayMetrics dm) { |
| 369 | iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale); |
| 370 | iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale); |
| 371 | iconDrawablePaddingPx = drawablePadding; |
| 372 | hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale); |
| 373 | |
| 374 | // Search Bar |
Sunny Goyal | 594d76d | 2014-11-06 10:12:54 -0800 | [diff] [blame] | 375 | searchBarSpaceWidthPx = Math.min(widthPx, |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 376 | res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width)); |
Sunny Goyal | 594d76d | 2014-11-06 10:12:54 -0800 | [diff] [blame] | 377 | searchBarSpaceHeightPx = getSearchBarTopOffset() |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 378 | + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 379 | |
| 380 | // Calculate the actual text height |
| 381 | Paint textPaint = new Paint(); |
| 382 | textPaint.setTextSize(iconTextSizePx); |
| 383 | FontMetrics fm = textPaint.getFontMetrics(); |
| 384 | cellWidthPx = iconSizePx; |
| 385 | cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 386 | final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 387 | dragViewScale = (iconSizePx + scaleDps) / iconSizePx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 388 | |
| 389 | // Hotseat |
| 390 | hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx; |
| 391 | hotseatCellWidthPx = iconSizePx; |
| 392 | hotseatCellHeightPx = iconSizePx; |
| 393 | |
| 394 | // Folder |
| 395 | folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx; |
| 396 | folderCellHeightPx = cellHeightPx + edgeMarginPx; |
| 397 | folderBackgroundOffset = -edgeMarginPx; |
| 398 | folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset; |
| 399 | |
| 400 | // All Apps |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 401 | allAppsCellWidthPx = allAppsIconSizePx; |
| 402 | allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + iconTextSizePx; |
| 403 | int maxLongEdgeCellCount = |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 404 | res.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 405 | int maxShortEdgeCellCount = |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 406 | res.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 407 | int minEdgeCellCount = |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 408 | res.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 409 | int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount); |
| 410 | int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount); |
| 411 | |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 412 | if (allAppsShortEdgeCount > 0 && allAppsLongEdgeCount > 0) { |
| 413 | allAppsNumRows = isLandscape ? allAppsShortEdgeCount : allAppsLongEdgeCount; |
| 414 | allAppsNumCols = isLandscape ? allAppsLongEdgeCount : allAppsShortEdgeCount; |
| 415 | } else { |
| 416 | allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx) / |
| 417 | (allAppsCellHeightPx + allAppsCellPaddingPx); |
| 418 | allAppsNumRows = Math.max(minEdgeCellCount, Math.min(maxRows, allAppsNumRows)); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 419 | allAppsNumCols = (availableWidthPx) / (allAppsCellWidthPx + allAppsCellPaddingPx); |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 420 | allAppsNumCols = Math.max(minEdgeCellCount, Math.min(maxCols, allAppsNumCols)); |
| 421 | } |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame^] | 422 | |
| 423 | int appsContainerViewPx = res.getDimensionPixelSize(R.dimen.apps_container_width); |
| 424 | int appsViewLeftMarginPx = |
| 425 | res.getDimensionPixelSize(R.dimen.apps_grid_view_start_margin); |
| 426 | int availableAppsWidthPx = (appsContainerViewPx > 0) ? appsContainerViewPx : |
| 427 | availableWidthPx; |
| 428 | appsViewNumCols = (availableAppsWidthPx - appsViewLeftMarginPx) / |
| 429 | (allAppsCellWidthPx + allAppsCellPaddingPx); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx, |
| 433 | int awPx, int ahPx) { |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 434 | Configuration configuration = resources.getConfiguration(); |
| 435 | isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 436 | isTablet = resources.getBoolean(R.bool.is_tablet); |
| 437 | isLargeTablet = resources.getBoolean(R.bool.is_large_tablet); |
Winson Chung | 6033ceb | 2014-02-05 12:37:42 -0800 | [diff] [blame] | 438 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 439 | isLayoutRtl = (configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL); |
| 440 | } else { |
| 441 | isLayoutRtl = false; |
| 442 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 443 | widthPx = wPx; |
| 444 | heightPx = hPx; |
| 445 | availableWidthPx = awPx; |
| 446 | availableHeightPx = ahPx; |
| 447 | |
| 448 | updateAvailableDimensions(context); |
| 449 | } |
| 450 | |
| 451 | private float dist(PointF p0, PointF p1) { |
| 452 | return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) + |
| 453 | (p1.y-p0.y)*(p1.y-p0.y)); |
| 454 | } |
| 455 | |
| 456 | private float weight(PointF a, PointF b, |
| 457 | float pow) { |
| 458 | float d = dist(a, b); |
| 459 | if (d == 0f) { |
| 460 | return Float.POSITIVE_INFINITY; |
| 461 | } |
| 462 | return (float) (1f / Math.pow(d, pow)); |
| 463 | } |
| 464 | |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 465 | /** Returns the closest device profile given the width and height and a list of profiles */ |
| 466 | private DeviceProfile findClosestDeviceProfile(float width, float height, |
| 467 | ArrayList<DeviceProfileQuery> points) { |
| 468 | return findClosestDeviceProfiles(width, height, points).get(0).profile; |
| 469 | } |
| 470 | |
| 471 | /** Returns the closest device profiles ordered by closeness to the specified width and height */ |
| 472 | private ArrayList<DeviceProfileQuery> findClosestDeviceProfiles(float width, float height, |
| 473 | ArrayList<DeviceProfileQuery> points) { |
| 474 | final PointF xy = new PointF(width, height); |
| 475 | |
| 476 | // Sort the profiles by their closeness to the dimensions |
| 477 | ArrayList<DeviceProfileQuery> pointsByNearness = points; |
| 478 | Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() { |
| 479 | public int compare(DeviceProfileQuery a, DeviceProfileQuery b) { |
| 480 | return (int) (dist(xy, a.dimens) - dist(xy, b.dimens)); |
| 481 | } |
| 482 | }); |
| 483 | |
| 484 | return pointsByNearness; |
| 485 | } |
| 486 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 487 | private float invDistWeightedInterpolate(float width, float height, |
| 488 | ArrayList<DeviceProfileQuery> points) { |
| 489 | float sum = 0; |
| 490 | float weights = 0; |
| 491 | float pow = 5; |
| 492 | float kNearestNeighbors = 3; |
| 493 | final PointF xy = new PointF(width, height); |
| 494 | |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 495 | ArrayList<DeviceProfileQuery> pointsByNearness = findClosestDeviceProfiles(width, height, |
| 496 | points); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 497 | |
| 498 | for (int i = 0; i < pointsByNearness.size(); ++i) { |
| 499 | DeviceProfileQuery p = pointsByNearness.get(i); |
| 500 | if (i < kNearestNeighbors) { |
| 501 | float w = weight(xy, p.dimens, pow); |
| 502 | if (w == Float.POSITIVE_INFINITY) { |
| 503 | return p.value; |
| 504 | } |
| 505 | weights += w; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | for (int i = 0; i < pointsByNearness.size(); ++i) { |
| 510 | DeviceProfileQuery p = pointsByNearness.get(i); |
| 511 | if (i < kNearestNeighbors) { |
| 512 | float w = weight(xy, p.dimens, pow); |
| 513 | sum += w * p.value / weights; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return sum; |
| 518 | } |
| 519 | |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 520 | /** Returns the search bar top offset */ |
| 521 | int getSearchBarTopOffset() { |
| 522 | if (isTablet() && !isVerticalBarLayout()) { |
| 523 | return 4 * edgeMarginPx; |
| 524 | } else { |
| 525 | return 2 * edgeMarginPx; |
| 526 | } |
| 527 | } |
| 528 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 529 | /** Returns the search bar bounds in the current orientation */ |
| 530 | Rect getSearchBarBounds() { |
| 531 | return getSearchBarBounds(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT); |
| 532 | } |
| 533 | /** Returns the search bar bounds in the specified orientation */ |
| 534 | Rect getSearchBarBounds(int orientation) { |
| 535 | Rect bounds = new Rect(); |
| 536 | if (orientation == CellLayout.LANDSCAPE && |
| 537 | transposeLayoutWithOrientation) { |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 538 | if (isLayoutRtl) { |
| 539 | bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx, |
| 540 | availableWidthPx, availableHeightPx - edgeMarginPx); |
| 541 | } else { |
| 542 | bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx, |
| 543 | availableHeightPx - edgeMarginPx); |
| 544 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 545 | } else { |
| 546 | if (isTablet()) { |
| 547 | // Pad the left and right of the workspace to ensure consistent spacing |
| 548 | // between all icons |
| 549 | int width = (orientation == CellLayout.LANDSCAPE) |
| 550 | ? Math.max(widthPx, heightPx) |
| 551 | : Math.min(widthPx, heightPx); |
| 552 | // XXX: If the icon size changes across orientations, we will have to take |
| 553 | // that into account here too. |
| 554 | int gap = (int) ((width - 2 * edgeMarginPx - |
| 555 | (numColumns * cellWidthPx)) / (2 * (numColumns + 1))); |
Winson Chung | 2cb2471 | 2013-12-02 15:00:39 -0800 | [diff] [blame] | 556 | bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(), |
| 557 | availableWidthPx - (edgeMarginPx + gap), |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 558 | searchBarSpaceHeightPx); |
| 559 | } else { |
Winson Chung | 2cb2471 | 2013-12-02 15:00:39 -0800 | [diff] [blame] | 560 | bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left, |
| 561 | getSearchBarTopOffset(), |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 562 | availableWidthPx - (desiredWorkspaceLeftRightMarginPx - |
| 563 | defaultWidgetPadding.right), searchBarSpaceHeightPx); |
| 564 | } |
| 565 | } |
| 566 | return bounds; |
| 567 | } |
| 568 | |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 569 | /** Returns the bounds of the workspace page indicators. */ |
| 570 | Rect getWorkspacePageIndicatorBounds(Rect insets) { |
| 571 | Rect workspacePadding = getWorkspacePadding(); |
Winson Chung | 205cd77 | 2014-01-15 14:31:59 -0800 | [diff] [blame] | 572 | if (isLandscape && transposeLayoutWithOrientation) { |
| 573 | if (isLayoutRtl) { |
| 574 | return new Rect(workspacePadding.left, workspacePadding.top, |
| 575 | workspacePadding.left + pageIndicatorHeightPx, |
| 576 | heightPx - workspacePadding.bottom - insets.bottom); |
| 577 | } else { |
| 578 | int pageIndicatorLeft = widthPx - workspacePadding.right; |
| 579 | return new Rect(pageIndicatorLeft, workspacePadding.top, |
| 580 | pageIndicatorLeft + pageIndicatorHeightPx, |
| 581 | heightPx - workspacePadding.bottom - insets.bottom); |
| 582 | } |
| 583 | } else { |
| 584 | int pageIndicatorTop = heightPx - insets.bottom - workspacePadding.bottom; |
| 585 | return new Rect(workspacePadding.left, pageIndicatorTop, |
| 586 | widthPx - workspacePadding.right, pageIndicatorTop + pageIndicatorHeightPx); |
| 587 | } |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 590 | public int getWorkspaceGridHeight() { |
| 591 | Rect p = getWorkspacePadding(); |
| 592 | return availableHeightPx - p.top - p.bottom; |
| 593 | } |
| 594 | |
| 595 | public int getWorkspaceGridWidth() { |
| 596 | Rect p = getWorkspacePadding(); |
| 597 | return availableWidthPx - p.left - p.right; |
| 598 | } |
| 599 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 600 | /** Returns the workspace padding in the specified orientation */ |
| 601 | Rect getWorkspacePadding() { |
| 602 | return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT); |
| 603 | } |
| 604 | Rect getWorkspacePadding(int orientation) { |
| 605 | Rect searchBarBounds = getSearchBarBounds(orientation); |
| 606 | Rect padding = new Rect(); |
| 607 | if (orientation == CellLayout.LANDSCAPE && |
| 608 | transposeLayoutWithOrientation) { |
| 609 | // 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] | 610 | if (isLayoutRtl) { |
| 611 | padding.set(hotseatBarHeightPx, edgeMarginPx, |
| 612 | searchBarBounds.width(), edgeMarginPx); |
| 613 | } else { |
| 614 | padding.set(searchBarBounds.width(), edgeMarginPx, |
| 615 | hotseatBarHeightPx, edgeMarginPx); |
| 616 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 617 | } else { |
| 618 | if (isTablet()) { |
| 619 | // Pad the left and right of the workspace to ensure consistent spacing |
| 620 | // between all icons |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 621 | float gapScale = 1f + (dragViewScale - 1f) / 2f; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 622 | int width = (orientation == CellLayout.LANDSCAPE) |
| 623 | ? Math.max(widthPx, heightPx) |
| 624 | : Math.min(widthPx, heightPx); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 625 | int height = (orientation != CellLayout.LANDSCAPE) |
| 626 | ? Math.max(widthPx, heightPx) |
| 627 | : Math.min(widthPx, heightPx); |
| 628 | int paddingTop = searchBarBounds.bottom; |
| 629 | int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx; |
| 630 | int availableWidth = Math.max(0, width - (int) ((numColumns * cellWidthPx) + |
| 631 | (numColumns * gapScale * cellWidthPx))); |
| 632 | int availableHeight = Math.max(0, height - paddingTop - paddingBottom |
| 633 | - (int) (2 * numRows * cellHeightPx)); |
| 634 | padding.set(availableWidth / 2, paddingTop + availableHeight / 2, |
| 635 | availableWidth / 2, paddingBottom + availableHeight / 2); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 636 | } else { |
| 637 | // Pad the top and bottom of the workspace with search/hotseat bar sizes |
| 638 | padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left, |
| 639 | searchBarBounds.bottom, |
| 640 | desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right, |
| 641 | hotseatBarHeightPx + pageIndicatorHeightPx); |
| 642 | } |
| 643 | } |
| 644 | return padding; |
| 645 | } |
| 646 | |
| 647 | int getWorkspacePageSpacing(int orientation) { |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 648 | if ((orientation == CellLayout.LANDSCAPE && |
| 649 | transposeLayoutWithOrientation) || isLargeTablet()) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 650 | // In landscape mode the page spacing is set to the default. |
| 651 | return defaultPageSpacingPx; |
| 652 | } else { |
| 653 | // In portrait, we want the pages spaced such that there is no |
| 654 | // overhang of the previous / next page into the current page viewport. |
| 655 | // We assume symmetrical padding in portrait mode. |
Adam Cohen | efb31e3 | 2014-01-16 16:07:50 -0800 | [diff] [blame] | 656 | return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding().left); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | Rect getOverviewModeButtonBarRect() { |
| 661 | int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx); |
| 662 | zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx, |
| 663 | Math.max(overviewModeMinIconZoneHeightPx, zoneHeight)); |
| 664 | return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx); |
| 665 | } |
| 666 | |
| 667 | float getOverviewModeScale() { |
| 668 | Rect workspacePadding = getWorkspacePadding(); |
| 669 | Rect overviewBar = getOverviewModeButtonBarRect(); |
| 670 | int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom; |
| 671 | return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace; |
| 672 | } |
| 673 | |
| 674 | // The rect returned will be extended to below the system ui that covers the workspace |
| 675 | Rect getHotseatRect() { |
| 676 | if (isVerticalBarLayout()) { |
| 677 | return new Rect(availableWidthPx - hotseatBarHeightPx, 0, |
| 678 | Integer.MAX_VALUE, availableHeightPx); |
| 679 | } else { |
| 680 | return new Rect(0, availableHeightPx - hotseatBarHeightPx, |
| 681 | availableWidthPx, Integer.MAX_VALUE); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | int calculateCellWidth(int width, int countX) { |
| 686 | return width / countX; |
| 687 | } |
| 688 | int calculateCellHeight(int height, int countY) { |
| 689 | return height / countY; |
| 690 | } |
| 691 | |
| 692 | boolean isPhone() { |
| 693 | return !isTablet && !isLargeTablet; |
| 694 | } |
| 695 | boolean isTablet() { |
| 696 | return isTablet; |
| 697 | } |
| 698 | boolean isLargeTablet() { |
| 699 | return isLargeTablet; |
| 700 | } |
| 701 | |
| 702 | boolean isVerticalBarLayout() { |
| 703 | return isLandscape && transposeLayoutWithOrientation; |
| 704 | } |
| 705 | |
| 706 | boolean shouldFadeAdjacentWorkspaceScreens() { |
| 707 | return isVerticalBarLayout() || isLargeTablet(); |
| 708 | } |
| 709 | |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 710 | int getVisibleChildCount(ViewGroup parent) { |
| 711 | int visibleChildren = 0; |
| 712 | for (int i = 0; i < parent.getChildCount(); i++) { |
| 713 | if (parent.getChildAt(i).getVisibility() != View.GONE) { |
| 714 | visibleChildren++; |
| 715 | } |
| 716 | } |
| 717 | return visibleChildren; |
| 718 | } |
| 719 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 720 | public void layout(Launcher launcher) { |
| 721 | FrameLayout.LayoutParams lp; |
| 722 | Resources res = launcher.getResources(); |
| 723 | boolean hasVerticalBarLayout = isVerticalBarLayout(); |
| 724 | |
| 725 | // Layout the search bar space |
| 726 | View searchBar = launcher.getSearchBar(); |
| 727 | lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams(); |
| 728 | if (hasVerticalBarLayout) { |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 729 | // Vertical search bar space |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 730 | lp.gravity = Gravity.TOP | Gravity.LEFT; |
| 731 | lp.width = searchBarSpaceHeightPx; |
Adam Cohen | 24ce0b3 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 732 | lp.height = LayoutParams.WRAP_CONTENT; |
Adam Cohen | 24ce0b3 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 733 | |
| 734 | LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar); |
| 735 | targets.setOrientation(LinearLayout.VERTICAL); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 736 | } else { |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 737 | // Horizontal search bar space |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 738 | lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; |
| 739 | lp.width = searchBarSpaceWidthPx; |
| 740 | lp.height = searchBarSpaceHeightPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 741 | } |
| 742 | searchBar.setLayoutParams(lp); |
| 743 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 744 | // Layout the workspace |
| 745 | PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace); |
| 746 | lp = (FrameLayout.LayoutParams) workspace.getLayoutParams(); |
| 747 | lp.gravity = Gravity.CENTER; |
| 748 | int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT; |
| 749 | Rect padding = getWorkspacePadding(orientation); |
| 750 | workspace.setLayoutParams(lp); |
| 751 | workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom); |
| 752 | workspace.setPageSpacing(getWorkspacePageSpacing(orientation)); |
| 753 | |
| 754 | // Layout the hotseat |
| 755 | View hotseat = launcher.findViewById(R.id.hotseat); |
| 756 | lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams(); |
| 757 | if (hasVerticalBarLayout) { |
| 758 | // Vertical hotseat |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 759 | lp.gravity = Gravity.END; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 760 | lp.width = hotseatBarHeightPx; |
| 761 | lp.height = LayoutParams.MATCH_PARENT; |
| 762 | hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx); |
| 763 | } else if (isTablet()) { |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 764 | // Pad the hotseat with the workspace padding calculated above |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 765 | lp.gravity = Gravity.BOTTOM; |
| 766 | lp.width = LayoutParams.MATCH_PARENT; |
| 767 | lp.height = hotseatBarHeightPx; |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 768 | hotseat.setPadding(edgeMarginPx + padding.left, 0, |
| 769 | edgeMarginPx + padding.right, |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 770 | 2 * edgeMarginPx); |
| 771 | } else { |
| 772 | // For phones, layout the hotseat without any bottom margin |
| 773 | // to ensure that we have space for the folders |
| 774 | lp.gravity = Gravity.BOTTOM; |
| 775 | lp.width = LayoutParams.MATCH_PARENT; |
| 776 | lp.height = hotseatBarHeightPx; |
Sunny Goyal | d81992b | 2015-03-05 14:00:18 -0800 | [diff] [blame] | 777 | hotseat.setPadding(2 * edgeMarginPx, 0, 2 * edgeMarginPx, 0); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 778 | } |
| 779 | hotseat.setLayoutParams(lp); |
| 780 | |
| 781 | // Layout the page indicators |
| 782 | View pageIndicator = launcher.findViewById(R.id.page_indicator); |
| 783 | if (pageIndicator != null) { |
| 784 | if (hasVerticalBarLayout) { |
| 785 | // Hide the page indicators when we have vertical search/hotseat |
| 786 | pageIndicator.setVisibility(View.GONE); |
| 787 | } else { |
| 788 | // Put the page indicators above the hotseat |
| 789 | lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams(); |
| 790 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
| 791 | lp.width = LayoutParams.WRAP_CONTENT; |
| 792 | lp.height = LayoutParams.WRAP_CONTENT; |
| 793 | lp.bottomMargin = hotseatBarHeightPx; |
| 794 | pageIndicator.setLayoutParams(lp); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // Layout AllApps |
| 799 | AppsCustomizeTabHost host = (AppsCustomizeTabHost) |
| 800 | launcher.findViewById(R.id.apps_customize_pane); |
| 801 | if (host != null) { |
| 802 | // Center the all apps page indicator |
| 803 | int pageIndicatorHeight = (int) (pageIndicatorHeightPx * Math.min(1f, |
| 804 | (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX))); |
| 805 | pageIndicator = host.findViewById(R.id.apps_customize_page_indicator); |
| 806 | if (pageIndicator != null) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 807 | LinearLayout.LayoutParams lllp = (LinearLayout.LayoutParams) pageIndicator.getLayoutParams(); |
| 808 | lllp.width = LayoutParams.WRAP_CONTENT; |
| 809 | lllp.height = pageIndicatorHeight; |
| 810 | pageIndicator.setLayoutParams(lllp); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | AppsCustomizePagedView pagedView = (AppsCustomizePagedView) |
| 814 | host.findViewById(R.id.apps_customize_pane_content); |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 815 | |
| 816 | FrameLayout fakePageContainer = (FrameLayout) |
| 817 | host.findViewById(R.id.fake_page_container); |
| 818 | FrameLayout fakePage = (FrameLayout) host.findViewById(R.id.fake_page); |
| 819 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 820 | padding = new Rect(); |
| 821 | if (pagedView != null) { |
| 822 | // Constrain the dimensions of all apps so that it does not span the full width |
| 823 | int paddingLR = (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) / |
| 824 | (2 * (allAppsNumCols + 1)); |
| 825 | int paddingTB = (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows)) / |
| 826 | (2 * (allAppsNumRows + 1)); |
| 827 | paddingLR = Math.min(paddingLR, (int)((paddingLR + paddingTB) * 0.75f)); |
| 828 | paddingTB = Math.min(paddingTB, (int)((paddingLR + paddingTB) * 0.75f)); |
| 829 | int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR)); |
| 830 | int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 2; |
Winson Chung | 495f44d | 2013-12-04 12:51:53 -0800 | [diff] [blame] | 831 | // Only adjust the side paddings on landscape phones, or tablets |
| 832 | if ((isTablet() || isLandscape) && gridPaddingLR > (allAppsCellWidthPx / 4)) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 833 | padding.left = padding.right = gridPaddingLR; |
| 834 | } |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 835 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 836 | // The icons are centered, so we can't just offset by the page indicator height |
| 837 | // because the empty space will actually be pageIndicatorHeight + paddingTB |
| 838 | padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB); |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 839 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 840 | pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight); |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 841 | fakePage.setBackground(res.getDrawable(R.drawable.quantum_panel)); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 842 | |
| 843 | // Horizontal padding for the whole paged view |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 844 | int pagedFixedViewPadding = |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 845 | res.getDimensionPixelSize(R.dimen.apps_customize_horizontal_padding); |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 846 | |
| 847 | padding.left += pagedFixedViewPadding; |
| 848 | padding.right += pagedFixedViewPadding; |
| 849 | |
| 850 | pagedView.setPadding(padding.left, padding.top, padding.right, padding.bottom); |
| 851 | fakePageContainer.setPadding(padding.left, padding.top, padding.right, padding.bottom); |
| 852 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
| 856 | // Layout the Overview Mode |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 857 | ViewGroup overviewMode = launcher.getOverviewPanel(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 858 | if (overviewMode != null) { |
| 859 | Rect r = getOverviewModeButtonBarRect(); |
| 860 | lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams(); |
| 861 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 862 | |
| 863 | int visibleChildCount = getVisibleChildCount(overviewMode); |
| 864 | int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx; |
| 865 | int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx; |
| 866 | |
| 867 | lp.width = Math.min(availableWidthPx, maxWidth); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 868 | lp.height = r.height(); |
| 869 | overviewMode.setLayoutParams(lp); |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 870 | |
| 871 | if (lp.width > totalItemWidth && visibleChildCount > 1) { |
| 872 | // We have enough space. Lets add some margin too. |
| 873 | int margin = (lp.width - totalItemWidth) / (visibleChildCount-1); |
| 874 | View lastChild = null; |
| 875 | |
| 876 | // Set margin of all visible children except the last visible child |
| 877 | for (int i = 0; i < visibleChildCount; i++) { |
| 878 | if (lastChild != null) { |
| 879 | MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams(); |
| 880 | if (isLayoutRtl) { |
| 881 | clp.leftMargin = margin; |
| 882 | } else { |
| 883 | clp.rightMargin = margin; |
| 884 | } |
| 885 | lastChild.setLayoutParams(clp); |
| 886 | lastChild = null; |
| 887 | } |
| 888 | View thisChild = overviewMode.getChildAt(i); |
| 889 | if (thisChild.getVisibility() != View.GONE) { |
| 890 | lastChild = thisChild; |
| 891 | } |
| 892 | } |
| 893 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | } |