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