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; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
| 23 | import android.graphics.Paint; |
| 24 | import android.graphics.Paint.FontMetrics; |
| 25 | import android.graphics.Point; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 26 | import android.graphics.Rect; |
| 27 | import android.util.DisplayMetrics; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 28 | import android.view.Gravity; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 29 | import android.view.View; |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 30 | import android.view.ViewGroup; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 31 | import android.view.ViewGroup.LayoutParams; |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 32 | import android.view.ViewGroup.MarginLayoutParams; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 33 | import android.widget.FrameLayout; |
Adam Cohen | 24ce0b3 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 34 | import android.widget.LinearLayout; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 35 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 36 | public class DeviceProfile { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 37 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 38 | public final InvariantDeviceProfile inv; |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 39 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 40 | // Device properties |
| 41 | public final boolean isTablet; |
| 42 | public final boolean isLargeTablet; |
| 43 | public final boolean isPhone; |
| 44 | public final boolean transposeLayoutWithOrientation; |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 45 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 46 | // Device properties in current orientation |
| 47 | public final boolean isLandscape; |
| 48 | public final int widthPx; |
| 49 | public final int heightPx; |
| 50 | public final int availableWidthPx; |
| 51 | public final int availableHeightPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 52 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 53 | // Overview mode |
| 54 | private final int overviewModeMinIconZoneHeightPx; |
| 55 | private final int overviewModeMaxIconZoneHeightPx; |
| 56 | private final int overviewModeBarItemWidthPx; |
| 57 | private final int overviewModeBarSpacerWidthPx; |
| 58 | private final float overviewModeIconZoneRatio; |
| 59 | private final float overviewModeScaleFactor; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 60 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 61 | // Workspace |
| 62 | private int desiredWorkspaceLeftRightMarginPx; |
| 63 | public final int edgeMarginPx; |
| 64 | public final Rect defaultWidgetPadding; |
| 65 | private final int pageIndicatorHeightPx; |
| 66 | private final int defaultPageSpacingPx; |
| 67 | private float dragViewScale; |
| 68 | |
| 69 | // Workspace icons |
| 70 | public int iconSizePx; |
| 71 | public int iconTextSizePx; |
| 72 | public int iconDrawablePaddingPx; |
| 73 | private final int iconDrawablePaddingOriginalPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 74 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 75 | public int cellWidthPx; |
| 76 | public int cellHeightPx; |
| 77 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 78 | // Folder |
| 79 | public int folderBackgroundOffset; |
| 80 | public int folderIconSizePx; |
| 81 | public int folderCellWidthPx; |
| 82 | public int folderCellHeightPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 83 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 84 | // Hotseat |
| 85 | public int hotseatCellWidthPx; |
| 86 | public int hotseatCellHeightPx; |
| 87 | public int hotseatIconSizePx; |
| 88 | private int hotseatBarHeightPx; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 89 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 90 | // All apps |
| 91 | private int allAppsCellWidthPx; |
| 92 | public int allAppsCellHeightPx; |
| 93 | private final int allAppsCellPaddingPx; |
| 94 | public int appsViewNumCols; |
| 95 | public int appsViewNumPredictiveCols; |
| 96 | public int allAppsButtonVisualSize; |
| 97 | public final int allAppsIconSizePx; |
| 98 | public final int allAppsIconTextSizePx; |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 99 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 100 | // QSB |
| 101 | private int searchBarSpaceWidthPx; |
| 102 | private int searchBarSpaceHeightPx; |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 103 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 104 | public DeviceProfile(Context context, InvariantDeviceProfile inv, |
| 105 | Point minSize, Point maxSize, |
| 106 | int width, int height, boolean isLandscape) { |
Sunny Goyal | fee35bb | 2015-05-11 11:38:19 -0700 | [diff] [blame] | 107 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 108 | this.inv = inv; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 109 | this.isLandscape = isLandscape; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 110 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 111 | Resources res = context.getResources(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 112 | DisplayMetrics dm = res.getDisplayMetrics(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 113 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 114 | // Constants from resources |
| 115 | isTablet = res.getBoolean(R.bool.is_tablet); |
| 116 | isLargeTablet = res.getBoolean(R.bool.is_large_tablet); |
| 117 | isPhone = !isTablet && !isLargeTablet; |
| 118 | |
| 119 | // Some more constants |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 120 | transposeLayoutWithOrientation = |
| 121 | res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 122 | |
| 123 | ComponentName cn = new ComponentName(context.getPackageName(), |
| 124 | this.getClass().getName()); |
| 125 | defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null); |
| 126 | edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin); |
| 127 | desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx; |
| 128 | pageIndicatorHeightPx = |
| 129 | res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height); |
| 130 | defaultPageSpacingPx = |
| 131 | res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing); |
| 132 | allAppsCellPaddingPx = |
| 133 | res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_padding); |
| 134 | overviewModeMinIconZoneHeightPx = |
| 135 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height); |
| 136 | overviewModeMaxIconZoneHeightPx = |
| 137 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height); |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 138 | overviewModeBarItemWidthPx = |
| 139 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width); |
| 140 | overviewModeBarSpacerWidthPx = |
| 141 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 142 | overviewModeIconZoneRatio = |
| 143 | res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f; |
| 144 | overviewModeScaleFactor = |
| 145 | res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f; |
| 146 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 147 | iconDrawablePaddingOriginalPx = |
| 148 | res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 149 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 150 | // AllApps uses the original non-scaled icon text size |
| 151 | allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm); |
| 152 | |
| 153 | // AllApps uses the original non-scaled icon size |
| 154 | allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 155 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 156 | // Determine sizes. |
| 157 | widthPx = width; |
| 158 | heightPx = height; |
| 159 | if (isLandscape) { |
| 160 | availableWidthPx = maxSize.x; |
| 161 | availableHeightPx = minSize.y; |
| 162 | } else { |
| 163 | availableWidthPx = minSize.x; |
| 164 | availableHeightPx = maxSize.y; |
| 165 | } |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 166 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 167 | // Calculate the remaining vars |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 168 | updateAvailableDimensions(dm, res); |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 169 | computeAllAppsButtonSize(context); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Determine the exact visual footprint of the all apps button, taking into account scaling |
| 174 | * and internal padding of the drawable. |
| 175 | */ |
| 176 | private void computeAllAppsButtonSize(Context context) { |
| 177 | Resources res = context.getResources(); |
| 178 | float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f; |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 179 | allAppsButtonVisualSize = (int) (hotseatIconSizePx * (1 - padding)); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 182 | private void updateAvailableDimensions(DisplayMetrics dm, Resources res) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 183 | // Check to see if the icons fit in the new available height. If not, then we need to |
| 184 | // shrink the icon size. |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 185 | float scale = 1f; |
| 186 | int drawablePadding = iconDrawablePaddingOriginalPx; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 187 | updateIconSize(1f, drawablePadding, res, dm); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 188 | float usedHeight = (cellHeightPx * inv.numRows); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 189 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 190 | // We only care about the top and bottom workspace padding, which is not affected by RTL. |
| 191 | Rect workspacePadding = getWorkspacePadding(false /* isLayoutRtl */); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 192 | int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom); |
| 193 | if (usedHeight > maxHeight) { |
| 194 | scale = maxHeight / usedHeight; |
| 195 | drawablePadding = 0; |
| 196 | } |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 197 | updateIconSize(scale, drawablePadding, res, dm); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 200 | private void updateIconSize(float scale, int drawablePadding, Resources res, |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 201 | DisplayMetrics dm) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 202 | iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale); |
| 203 | iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 204 | iconDrawablePaddingPx = drawablePadding; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 205 | hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 206 | |
| 207 | // Search Bar |
Sunny Goyal | 594d76d | 2014-11-06 10:12:54 -0800 | [diff] [blame] | 208 | searchBarSpaceWidthPx = Math.min(widthPx, |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 209 | res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width)); |
Sunny Goyal | 594d76d | 2014-11-06 10:12:54 -0800 | [diff] [blame] | 210 | searchBarSpaceHeightPx = getSearchBarTopOffset() |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 211 | + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 212 | |
| 213 | // Calculate the actual text height |
| 214 | Paint textPaint = new Paint(); |
| 215 | textPaint.setTextSize(iconTextSizePx); |
| 216 | FontMetrics fm = textPaint.getFontMetrics(); |
| 217 | cellWidthPx = iconSizePx; |
| 218 | cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 219 | final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 220 | dragViewScale = (iconSizePx + scaleDps) / iconSizePx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 221 | |
| 222 | // Hotseat |
| 223 | hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx; |
| 224 | hotseatCellWidthPx = iconSizePx; |
| 225 | hotseatCellHeightPx = iconSizePx; |
| 226 | |
| 227 | // Folder |
| 228 | folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx; |
| 229 | folderCellHeightPx = cellHeightPx + edgeMarginPx; |
| 230 | folderBackgroundOffset = -edgeMarginPx; |
| 231 | folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset; |
| 232 | |
| 233 | // All Apps |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 234 | allAppsCellWidthPx = allAppsIconSizePx; |
Winson Chung | 208ed75 | 2015-05-12 19:05:30 -0700 | [diff] [blame] | 235 | allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + allAppsIconTextSizePx; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 236 | |
Winson Chung | 0f78572 | 2015-04-08 10:27:49 -0700 | [diff] [blame] | 237 | int appsContainerViewWidthPx = res.getDimensionPixelSize(R.dimen.apps_container_width); |
| 238 | updateAppsViewNumCols(res, appsContainerViewWidthPx); |
| 239 | } |
| 240 | |
| 241 | public boolean updateAppsViewNumCols(Resources res, int containerWidth) { |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 242 | int appsViewLeftMarginPx = |
| 243 | res.getDimensionPixelSize(R.dimen.apps_grid_view_start_margin); |
Winson Chung | 0f78572 | 2015-04-08 10:27:49 -0700 | [diff] [blame] | 244 | int availableAppsWidthPx = (containerWidth > 0) ? containerWidth : availableWidthPx; |
Winson Chung | 208ed75 | 2015-05-12 19:05:30 -0700 | [diff] [blame] | 245 | int numAppsCols = (availableAppsWidthPx - appsViewLeftMarginPx) / |
Winson Chung | bd80853 | 2015-04-08 11:07:53 -0700 | [diff] [blame] | 246 | (allAppsCellWidthPx + 2 * allAppsCellPaddingPx); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 247 | int numPredictiveAppCols = isPhone ? 4 : numAppsCols; |
Winson Chung | 208ed75 | 2015-05-12 19:05:30 -0700 | [diff] [blame] | 248 | if ((numAppsCols != appsViewNumCols) || |
| 249 | (numPredictiveAppCols != appsViewNumPredictiveCols)) { |
| 250 | appsViewNumCols = numAppsCols; |
| 251 | appsViewNumPredictiveCols = numPredictiveAppCols; |
Winson Chung | 0f78572 | 2015-04-08 10:27:49 -0700 | [diff] [blame] | 252 | return true; |
| 253 | } |
| 254 | return false; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 257 | /** Returns the search bar top offset */ |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 258 | private int getSearchBarTopOffset() { |
| 259 | if (isTablet && !isVerticalBarLayout()) { |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 260 | return 4 * edgeMarginPx; |
| 261 | } else { |
| 262 | return 2 * edgeMarginPx; |
| 263 | } |
| 264 | } |
| 265 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 266 | /** Returns the search bar bounds in the current orientation */ |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 267 | public Rect getSearchBarBounds(boolean isLayoutRtl) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 268 | Rect bounds = new Rect(); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 269 | if (isLandscape && transposeLayoutWithOrientation) { |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 270 | if (isLayoutRtl) { |
| 271 | bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx, |
| 272 | availableWidthPx, availableHeightPx - edgeMarginPx); |
| 273 | } else { |
| 274 | bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx, |
| 275 | availableHeightPx - edgeMarginPx); |
| 276 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 277 | } else { |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 278 | if (isTablet) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 279 | // Pad the left and right of the workspace to ensure consistent spacing |
| 280 | // between all icons |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 281 | int width = isLandscape ? Math.max(widthPx, heightPx) : Math.min(widthPx, heightPx); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 282 | // XXX: If the icon size changes across orientations, we will have to take |
| 283 | // that into account here too. |
| 284 | int gap = (int) ((width - 2 * edgeMarginPx - |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 285 | (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1))); |
Winson Chung | 2cb2471 | 2013-12-02 15:00:39 -0800 | [diff] [blame] | 286 | bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(), |
| 287 | availableWidthPx - (edgeMarginPx + gap), |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 288 | searchBarSpaceHeightPx); |
| 289 | } else { |
Winson Chung | 2cb2471 | 2013-12-02 15:00:39 -0800 | [diff] [blame] | 290 | bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left, |
| 291 | getSearchBarTopOffset(), |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 292 | availableWidthPx - (desiredWorkspaceLeftRightMarginPx - |
| 293 | defaultWidgetPadding.right), searchBarSpaceHeightPx); |
| 294 | } |
| 295 | } |
| 296 | return bounds; |
| 297 | } |
| 298 | |
| 299 | /** Returns the workspace padding in the specified orientation */ |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 300 | Rect getWorkspacePadding(boolean isLayoutRtl) { |
| 301 | Rect searchBarBounds = getSearchBarBounds(isLayoutRtl); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 302 | Rect padding = new Rect(); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 303 | if (isLandscape && transposeLayoutWithOrientation) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 304 | // 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] | 305 | if (isLayoutRtl) { |
| 306 | padding.set(hotseatBarHeightPx, edgeMarginPx, |
| 307 | searchBarBounds.width(), edgeMarginPx); |
| 308 | } else { |
| 309 | padding.set(searchBarBounds.width(), edgeMarginPx, |
| 310 | hotseatBarHeightPx, edgeMarginPx); |
| 311 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 312 | } else { |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 313 | if (isTablet) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 314 | // Pad the left and right of the workspace to ensure consistent spacing |
| 315 | // between all icons |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 316 | float gapScale = 1f + (dragViewScale - 1f) / 2f; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 317 | int width = isLandscape |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 318 | ? Math.max(widthPx, heightPx) |
| 319 | : Math.min(widthPx, heightPx); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 320 | int height = isLandscape |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 321 | ? Math.max(widthPx, heightPx) |
| 322 | : Math.min(widthPx, heightPx); |
| 323 | int paddingTop = searchBarBounds.bottom; |
| 324 | int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 325 | int availableWidth = Math.max(0, width - (int) ((inv.numColumns * cellWidthPx) + |
| 326 | (inv.numColumns * gapScale * cellWidthPx))); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 327 | int availableHeight = Math.max(0, height - paddingTop - paddingBottom |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 328 | - (int) (2 * inv.numRows * cellHeightPx)); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 329 | padding.set(availableWidth / 2, paddingTop + availableHeight / 2, |
| 330 | availableWidth / 2, paddingBottom + availableHeight / 2); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 331 | } else { |
| 332 | // Pad the top and bottom of the workspace with search/hotseat bar sizes |
| 333 | padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left, |
| 334 | searchBarBounds.bottom, |
| 335 | desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right, |
| 336 | hotseatBarHeightPx + pageIndicatorHeightPx); |
| 337 | } |
| 338 | } |
| 339 | return padding; |
| 340 | } |
| 341 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 342 | private int getWorkspacePageSpacing(boolean isLayoutRtl) { |
| 343 | if ((isLandscape && transposeLayoutWithOrientation) || isLargeTablet) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 344 | // In landscape mode the page spacing is set to the default. |
| 345 | return defaultPageSpacingPx; |
| 346 | } else { |
| 347 | // In portrait, we want the pages spaced such that there is no |
| 348 | // overhang of the previous / next page into the current page viewport. |
| 349 | // We assume symmetrical padding in portrait mode. |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 350 | return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding(isLayoutRtl).left); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | Rect getOverviewModeButtonBarRect() { |
| 355 | int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx); |
| 356 | zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx, |
| 357 | Math.max(overviewModeMinIconZoneHeightPx, zoneHeight)); |
| 358 | return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx); |
| 359 | } |
| 360 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 361 | public float getOverviewModeScale(boolean isLayoutRtl) { |
| 362 | Rect workspacePadding = getWorkspacePadding(isLayoutRtl); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 363 | Rect overviewBar = getOverviewModeButtonBarRect(); |
| 364 | int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom; |
| 365 | return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace; |
| 366 | } |
| 367 | |
| 368 | // The rect returned will be extended to below the system ui that covers the workspace |
| 369 | Rect getHotseatRect() { |
| 370 | if (isVerticalBarLayout()) { |
| 371 | return new Rect(availableWidthPx - hotseatBarHeightPx, 0, |
| 372 | Integer.MAX_VALUE, availableHeightPx); |
| 373 | } else { |
| 374 | return new Rect(0, availableHeightPx - hotseatBarHeightPx, |
| 375 | availableWidthPx, Integer.MAX_VALUE); |
| 376 | } |
| 377 | } |
| 378 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 379 | public static int calculateCellWidth(int width, int countX) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 380 | return width / countX; |
| 381 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 382 | public static int calculateCellHeight(int height, int countY) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 383 | return height / countY; |
| 384 | } |
| 385 | |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 386 | /** |
| 387 | * When {@code true}, hotseat is on the bottom row when in landscape mode. |
| 388 | * If {@code false}, hotseat is on the right column when in landscape mode. |
| 389 | */ |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 390 | boolean isVerticalBarLayout() { |
| 391 | return isLandscape && transposeLayoutWithOrientation; |
| 392 | } |
| 393 | |
| 394 | boolean shouldFadeAdjacentWorkspaceScreens() { |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 395 | return isVerticalBarLayout() || isLargeTablet; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 398 | private int getVisibleChildCount(ViewGroup parent) { |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 399 | int visibleChildren = 0; |
| 400 | for (int i = 0; i < parent.getChildCount(); i++) { |
| 401 | if (parent.getChildAt(i).getVisibility() != View.GONE) { |
| 402 | visibleChildren++; |
| 403 | } |
| 404 | } |
| 405 | return visibleChildren; |
| 406 | } |
| 407 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 408 | public void layout(Launcher launcher) { |
| 409 | FrameLayout.LayoutParams lp; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 410 | boolean hasVerticalBarLayout = isVerticalBarLayout(); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 411 | final boolean isLayoutRtl = Utilities.isRtl(launcher.getResources()); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 412 | |
| 413 | // Layout the search bar space |
| 414 | View searchBar = launcher.getSearchBar(); |
| 415 | lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams(); |
| 416 | if (hasVerticalBarLayout) { |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 417 | // Vertical search bar space |
Sunny Goyal | d9cc780 | 2015-04-23 14:47:19 -0700 | [diff] [blame] | 418 | lp.gravity = Gravity.LEFT; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 419 | lp.width = searchBarSpaceHeightPx; |
Adam Cohen | 24ce0b3 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 420 | |
| 421 | LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar); |
| 422 | targets.setOrientation(LinearLayout.VERTICAL); |
Sunny Goyal | d9cc780 | 2015-04-23 14:47:19 -0700 | [diff] [blame] | 423 | FrameLayout.LayoutParams targetsLp = (FrameLayout.LayoutParams) targets.getLayoutParams(); |
| 424 | targetsLp.gravity = Gravity.TOP; |
| 425 | targetsLp.height = LayoutParams.WRAP_CONTENT; |
| 426 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 427 | } else { |
Winson Chung | 69e04ea | 2013-12-02 14:43:44 -0800 | [diff] [blame] | 428 | // Horizontal search bar space |
Sunny Goyal | d9cc780 | 2015-04-23 14:47:19 -0700 | [diff] [blame] | 429 | lp.gravity = Gravity.TOP; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 430 | lp.height = searchBarSpaceHeightPx; |
Sunny Goyal | d9cc780 | 2015-04-23 14:47:19 -0700 | [diff] [blame] | 431 | |
| 432 | LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar); |
| 433 | targets.getLayoutParams().width = searchBarSpaceWidthPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 434 | } |
| 435 | searchBar.setLayoutParams(lp); |
| 436 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 437 | // Layout the workspace |
| 438 | PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace); |
| 439 | lp = (FrameLayout.LayoutParams) workspace.getLayoutParams(); |
| 440 | lp.gravity = Gravity.CENTER; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 441 | Rect padding = getWorkspacePadding(isLayoutRtl); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 442 | workspace.setLayoutParams(lp); |
| 443 | workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 444 | workspace.setPageSpacing(getWorkspacePageSpacing(isLayoutRtl)); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 445 | |
| 446 | // Layout the hotseat |
| 447 | View hotseat = launcher.findViewById(R.id.hotseat); |
| 448 | lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams(); |
| 449 | if (hasVerticalBarLayout) { |
| 450 | // Vertical hotseat |
Winson Chung | 42b3c06 | 2013-12-04 12:09:59 -0800 | [diff] [blame] | 451 | lp.gravity = Gravity.END; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 452 | lp.width = hotseatBarHeightPx; |
| 453 | lp.height = LayoutParams.MATCH_PARENT; |
| 454 | hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 455 | } else if (isTablet) { |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 456 | // Pad the hotseat with the workspace padding calculated above |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 457 | lp.gravity = Gravity.BOTTOM; |
| 458 | lp.width = LayoutParams.MATCH_PARENT; |
| 459 | lp.height = hotseatBarHeightPx; |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 460 | hotseat.setPadding(edgeMarginPx + padding.left, 0, |
| 461 | edgeMarginPx + padding.right, |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 462 | 2 * edgeMarginPx); |
| 463 | } else { |
| 464 | // For phones, layout the hotseat without any bottom margin |
| 465 | // to ensure that we have space for the folders |
| 466 | lp.gravity = Gravity.BOTTOM; |
| 467 | lp.width = LayoutParams.MATCH_PARENT; |
| 468 | lp.height = hotseatBarHeightPx; |
Sunny Goyal | e0bca38 | 2015-04-06 18:39:22 +0000 | [diff] [blame] | 469 | hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0, |
| 470 | 2 * edgeMarginPx, 0); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 471 | } |
| 472 | hotseat.setLayoutParams(lp); |
| 473 | |
| 474 | // Layout the page indicators |
| 475 | View pageIndicator = launcher.findViewById(R.id.page_indicator); |
| 476 | if (pageIndicator != null) { |
| 477 | if (hasVerticalBarLayout) { |
| 478 | // Hide the page indicators when we have vertical search/hotseat |
| 479 | pageIndicator.setVisibility(View.GONE); |
| 480 | } else { |
| 481 | // Put the page indicators above the hotseat |
| 482 | lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams(); |
| 483 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
| 484 | lp.width = LayoutParams.WRAP_CONTENT; |
| 485 | lp.height = LayoutParams.WRAP_CONTENT; |
| 486 | lp.bottomMargin = hotseatBarHeightPx; |
| 487 | pageIndicator.setLayoutParams(lp); |
| 488 | } |
| 489 | } |
| 490 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 491 | // Layout the Overview Mode |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 492 | ViewGroup overviewMode = launcher.getOverviewPanel(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 493 | if (overviewMode != null) { |
| 494 | Rect r = getOverviewModeButtonBarRect(); |
| 495 | lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams(); |
| 496 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 497 | |
| 498 | int visibleChildCount = getVisibleChildCount(overviewMode); |
| 499 | int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx; |
| 500 | int maxWidth = totalItemWidth + (visibleChildCount-1) * overviewModeBarSpacerWidthPx; |
| 501 | |
| 502 | lp.width = Math.min(availableWidthPx, maxWidth); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 503 | lp.height = r.height(); |
| 504 | overviewMode.setLayoutParams(lp); |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 505 | |
| 506 | if (lp.width > totalItemWidth && visibleChildCount > 1) { |
| 507 | // We have enough space. Lets add some margin too. |
| 508 | int margin = (lp.width - totalItemWidth) / (visibleChildCount-1); |
| 509 | View lastChild = null; |
| 510 | |
| 511 | // Set margin of all visible children except the last visible child |
| 512 | for (int i = 0; i < visibleChildCount; i++) { |
| 513 | if (lastChild != null) { |
| 514 | MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams(); |
| 515 | if (isLayoutRtl) { |
| 516 | clp.leftMargin = margin; |
| 517 | } else { |
| 518 | clp.rightMargin = margin; |
| 519 | } |
| 520 | lastChild.setLayoutParams(clp); |
| 521 | lastChild = null; |
| 522 | } |
| 523 | View thisChild = overviewMode.getChildAt(i); |
| 524 | if (thisChild.getVisibility() != View.GONE) { |
| 525 | lastChild = thisChild; |
| 526 | } |
| 527 | } |
| 528 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | } |