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