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; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 24 | import android.graphics.Point; |
Jon Miranda | 7ae64ff | 2016-11-21 16:18:46 -0800 | [diff] [blame] | 25 | import android.graphics.PointF; |
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; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
| 33 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 34 | import com.android.launcher3.CellLayout.ContainerType; |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 35 | import com.android.launcher3.badge.BadgeRenderer; |
Tony Wickham | e0c3323 | 2016-02-08 11:37:04 -0800 | [diff] [blame] | 36 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 39 | public class DeviceProfile { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 40 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 41 | public interface LauncherLayoutChangeListener { |
| 42 | void onLauncherLayoutChanged(); |
| 43 | } |
| 44 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 45 | public final InvariantDeviceProfile inv; |
Winson Chung | be87647 | 2014-05-14 14:15:20 -0700 | [diff] [blame] | 46 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 47 | // Device properties |
| 48 | public final boolean isTablet; |
| 49 | public final boolean isLargeTablet; |
| 50 | public final boolean isPhone; |
| 51 | public final boolean transposeLayoutWithOrientation; |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 52 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 53 | // Device properties in current orientation |
| 54 | public final boolean isLandscape; |
| 55 | public final int widthPx; |
| 56 | public final int heightPx; |
| 57 | public final int availableWidthPx; |
| 58 | public final int availableHeightPx; |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 59 | /** |
| 60 | * The maximum amount of left/right workspace padding as a percentage of the screen width. |
| 61 | * To be clear, this means that up to 7% of the screen width can be used as left padding, and |
| 62 | * 7% of the screen width can be used as right padding. |
| 63 | */ |
| 64 | private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 65 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 66 | // Overview mode |
| 67 | private final int overviewModeMinIconZoneHeightPx; |
| 68 | private final int overviewModeMaxIconZoneHeightPx; |
| 69 | private final int overviewModeBarItemWidthPx; |
| 70 | private final int overviewModeBarSpacerWidthPx; |
| 71 | private final float overviewModeIconZoneRatio; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 72 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 73 | // Workspace |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 74 | private final int desiredWorkspaceLeftRightMarginPx; |
| 75 | public final int cellLayoutPaddingLeftRightPx; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 76 | public final int edgeMarginPx; |
| 77 | public final Rect defaultWidgetPadding; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 78 | private final int defaultPageSpacingPx; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 79 | private final int topWorkspacePadding; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 80 | public float workspaceSpringLoadShrinkFactor; |
| 81 | public final int workspaceSpringLoadedBottomSpace; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 82 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 83 | // Page indicator |
Jon Miranda | 2ed276e | 2017-06-29 11:36:34 -0700 | [diff] [blame] | 84 | private int pageIndicatorSizePx; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 85 | private final int pageIndicatorLandGutterPx; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 86 | private final int pageIndicatorLandWorkspaceOffsetPx; |
| 87 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 88 | // Workspace icons |
| 89 | public int iconSizePx; |
| 90 | public int iconTextSizePx; |
| 91 | public int iconDrawablePaddingPx; |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 92 | public int iconDrawablePaddingOriginalPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 93 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 94 | public int cellWidthPx; |
| 95 | public int cellHeightPx; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 96 | public int workspaceCellPaddingXPx; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 97 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 98 | // Folder |
| 99 | public int folderBackgroundOffset; |
| 100 | public int folderIconSizePx; |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 101 | public int folderIconPreviewPadding; |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 102 | |
| 103 | // Folder cell |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 104 | public int folderCellWidthPx; |
| 105 | public int folderCellHeightPx; |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 106 | |
| 107 | // Folder child |
| 108 | public int folderChildIconSizePx; |
| 109 | public int folderChildTextSizePx; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 110 | public int folderChildDrawablePaddingPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 111 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 112 | // Hotseat |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 113 | public int hotseatCellHeightPx; |
Jon Miranda | aca2145 | 2016-09-26 14:01:56 -0700 | [diff] [blame] | 114 | public int hotseatBarHeightPx; |
Sunny Goyal | 0dc1db4 | 2017-05-17 11:34:33 -0700 | [diff] [blame] | 115 | public int hotseatBarTopPaddingPx; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 116 | public int hotseatBarLeftNavBarRightPaddingPx; |
| 117 | public int hotseatBarRightNavBarRightPaddingPx; |
Sunny Goyal | 0dc1db4 | 2017-05-17 11:34:33 -0700 | [diff] [blame] | 118 | public int hotseatBarBottomPaddingPx; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 119 | public int hotseatLandLeftNavBarGutterPx; |
| 120 | public int hotseatLandRightNavBarGutterPx; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 121 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 122 | // All apps |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 123 | public int allAppsCellHeightPx; |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 124 | public int allAppsNumCols; |
| 125 | public int allAppsNumPredictiveCols; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 126 | public int allAppsButtonVisualSize; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 127 | public int allAppsIconSizePx; |
| 128 | public int allAppsIconDrawablePaddingPx; |
| 129 | public float allAppsIconTextSizePx; |
| 130 | |
Jon Miranda | 7ae64ff | 2016-11-21 16:18:46 -0800 | [diff] [blame] | 131 | // Widgets |
| 132 | public final PointF appWidgetScale = new PointF(1.0f, 1.0f); |
| 133 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 134 | // Drop Target |
| 135 | public int dropTargetBarSizePx; |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 136 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 137 | // Insets |
| 138 | private Rect mInsets = new Rect(); |
| 139 | |
| 140 | // Listeners |
| 141 | private ArrayList<LauncherLayoutChangeListener> mListeners = new ArrayList<>(); |
| 142 | |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 143 | // Icon badges |
| 144 | public BadgeRenderer mBadgeRenderer; |
| 145 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 146 | public DeviceProfile(Context context, InvariantDeviceProfile inv, |
| 147 | Point minSize, Point maxSize, |
| 148 | int width, int height, boolean isLandscape) { |
Sunny Goyal | fee35bb | 2015-05-11 11:38:19 -0700 | [diff] [blame] | 149 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 150 | this.inv = inv; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 151 | this.isLandscape = isLandscape; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 152 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 153 | Resources res = context.getResources(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 154 | DisplayMetrics dm = res.getDisplayMetrics(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 155 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 156 | // Constants from resources |
| 157 | isTablet = res.getBoolean(R.bool.is_tablet); |
| 158 | isLargeTablet = res.getBoolean(R.bool.is_large_tablet); |
| 159 | isPhone = !isTablet && !isLargeTablet; |
| 160 | |
| 161 | // Some more constants |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 162 | transposeLayoutWithOrientation = |
| 163 | res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 164 | |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 165 | context = getContext(context, isVerticalBarLayout() |
| 166 | ? Configuration.ORIENTATION_LANDSCAPE |
| 167 | : Configuration.ORIENTATION_PORTRAIT); |
| 168 | res = context.getResources(); |
| 169 | |
| 170 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 171 | ComponentName cn = new ComponentName(context.getPackageName(), |
| 172 | this.getClass().getName()); |
| 173 | defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null); |
| 174 | edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin); |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 175 | desiredWorkspaceLeftRightMarginPx = isVerticalBarLayout() ? 0 : edgeMarginPx; |
| 176 | cellLayoutPaddingLeftRightPx = isVerticalBarLayout() ? 0 : edgeMarginPx; |
Jon Miranda | 2ed276e | 2017-06-29 11:36:34 -0700 | [diff] [blame] | 177 | pageIndicatorSizePx = res.getDimensionPixelSize( |
| 178 | R.dimen.dynamic_grid_min_page_indicator_size); |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 179 | pageIndicatorLandGutterPx = res.getDimensionPixelSize( |
| 180 | R.dimen.dynamic_grid_page_indicator_gutter_width); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 181 | pageIndicatorLandWorkspaceOffsetPx = |
| 182 | res.getDimensionPixelSize(R.dimen.all_apps_caret_workspace_offset); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 183 | defaultPageSpacingPx = |
| 184 | res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 185 | topWorkspacePadding = |
| 186 | res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 187 | overviewModeMinIconZoneHeightPx = |
| 188 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height); |
| 189 | overviewModeMaxIconZoneHeightPx = |
| 190 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height); |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 191 | overviewModeBarItemWidthPx = |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 192 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width); |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 193 | overviewModeBarSpacerWidthPx = |
| 194 | res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 195 | overviewModeIconZoneRatio = |
| 196 | res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 197 | iconDrawablePaddingOriginalPx = |
| 198 | res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 199 | dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size); |
| 200 | workspaceSpringLoadedBottomSpace = |
| 201 | res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space); |
Sunny Goyal | d519052 | 2017-04-24 03:06:54 -0700 | [diff] [blame] | 202 | |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 203 | workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x); |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 204 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 205 | hotseatBarTopPaddingPx = |
| 206 | res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding); |
Sunny Goyal | d519052 | 2017-04-24 03:06:54 -0700 | [diff] [blame] | 207 | hotseatBarBottomPaddingPx = |
| 208 | res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_bottom_padding); |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 209 | hotseatBarLeftNavBarRightPaddingPx = res.getDimensionPixelSize( |
| 210 | R.dimen.dynamic_grid_hotseat_land_left_nav_bar_right_padding); |
| 211 | hotseatBarRightNavBarRightPaddingPx = res.getDimensionPixelSize( |
| 212 | R.dimen.dynamic_grid_hotseat_land_right_nav_bar_right_padding); |
| 213 | hotseatBarHeightPx = isVerticalBarLayout() |
| 214 | ? res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height) |
| 215 | : res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height) |
| 216 | + hotseatBarTopPaddingPx + hotseatBarBottomPaddingPx; |
| 217 | |
| 218 | hotseatLandLeftNavBarGutterPx = res.getDimensionPixelSize( |
| 219 | R.dimen.dynamic_grid_hotseat_land_left_nav_bar_gutter_width); |
| 220 | hotseatLandRightNavBarGutterPx = res.getDimensionPixelSize( |
| 221 | R.dimen.dynamic_grid_hotseat_land_right_nav_bar_gutter_width); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 222 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 223 | // Determine sizes. |
| 224 | widthPx = width; |
| 225 | heightPx = height; |
| 226 | if (isLandscape) { |
| 227 | availableWidthPx = maxSize.x; |
| 228 | availableHeightPx = minSize.y; |
| 229 | } else { |
| 230 | availableWidthPx = minSize.x; |
| 231 | availableHeightPx = maxSize.y; |
| 232 | } |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 233 | |
Jon Miranda | 2ed276e | 2017-06-29 11:36:34 -0700 | [diff] [blame] | 234 | // Calculate all of the remaining variables. |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 235 | updateAvailableDimensions(dm, res); |
Jon Miranda | 2ed276e | 2017-06-29 11:36:34 -0700 | [diff] [blame] | 236 | |
| 237 | // Now that we have all of the variables calculated, we can tune certain sizes. |
| 238 | if (!isVerticalBarLayout()) { |
| 239 | // We increase the page indicator size when there is extra space. |
| 240 | // ie. For a display with a large aspect ratio, we can keep the icons on the workspace |
| 241 | // in portrait mode closer together by increasing the page indicator size. |
| 242 | int newPageIndicatorSizePx = getCellSize().y - iconSizePx - iconTextSizePx |
| 243 | - iconDrawablePaddingOriginalPx; |
| 244 | if (newPageIndicatorSizePx > pageIndicatorSizePx) { |
| 245 | pageIndicatorSizePx = newPageIndicatorSizePx; |
| 246 | // Recalculate the available dimensions using the new page indicator size. |
| 247 | updateAvailableDimensions(dm, res); |
| 248 | } |
| 249 | } |
| 250 | |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 251 | computeAllAppsButtonSize(context); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 252 | |
| 253 | // This is done last, after iconSizePx is calculated above. |
| 254 | mBadgeRenderer = new BadgeRenderer(context, iconSizePx); |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Jon Miranda | cc42c5b | 2016-10-27 17:15:27 -0700 | [diff] [blame] | 257 | DeviceProfile getMultiWindowProfile(Context context, Point mwSize) { |
| 258 | // In multi-window mode, we can have widthPx = availableWidthPx |
| 259 | // and heightPx = availableHeightPx because Launcher uses the InvariantDeviceProfiles' |
| 260 | // widthPx and heightPx values where it's needed. |
Jon Miranda | 93e1f04 | 2016-11-11 14:13:04 -0800 | [diff] [blame] | 261 | DeviceProfile profile = new DeviceProfile(context, inv, mwSize, mwSize, mwSize.x, mwSize.y, |
| 262 | isLandscape); |
| 263 | |
| 264 | // Hide labels on the workspace. |
Jon Miranda | 1091e53 | 2017-07-21 13:31:50 -0700 | [diff] [blame] | 265 | profile.adjustToHideWorkspaceLabels(); |
Jon Miranda | 93e1f04 | 2016-11-11 14:13:04 -0800 | [diff] [blame] | 266 | |
Jon Miranda | 7ae64ff | 2016-11-21 16:18:46 -0800 | [diff] [blame] | 267 | // We use these scales to measure and layout the widgets using their full invariant profile |
| 268 | // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans. |
| 269 | float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x; |
| 270 | float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y; |
| 271 | profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY); |
| 272 | |
Jon Miranda | 93e1f04 | 2016-11-11 14:13:04 -0800 | [diff] [blame] | 273 | return profile; |
Jon Miranda | cc42c5b | 2016-10-27 17:15:27 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 276 | public void addLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) { |
| 277 | if (!mListeners.contains(listener)) { |
| 278 | mListeners.add(listener); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | public void removeLauncherLayoutChangedListener(LauncherLayoutChangeListener listener) { |
| 283 | if (mListeners.contains(listener)) { |
| 284 | mListeners.remove(listener); |
| 285 | } |
| 286 | } |
| 287 | |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 288 | /** |
Jon Miranda | 1091e53 | 2017-07-21 13:31:50 -0700 | [diff] [blame] | 289 | * Adjusts the profile so that the labels on the Workspace are hidden. |
| 290 | * It is important to call this method after the All Apps variables have been set. |
| 291 | */ |
| 292 | private void adjustToHideWorkspaceLabels() { |
| 293 | iconTextSizePx = 0; |
| 294 | iconDrawablePaddingPx = 0; |
| 295 | cellHeightPx = iconSizePx; |
| 296 | |
| 297 | // In normal cases, All Apps cell height should equal the Workspace cell height. |
| 298 | // Since we are removing labels from the Workspace, we need to manually compute the |
| 299 | // All Apps cell height. |
| 300 | allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx |
| 301 | + Utilities.calculateTextHeight(allAppsIconTextSizePx) |
| 302 | // Top and bottom padding is equal to the drawable padding |
| 303 | + allAppsIconDrawablePaddingPx * 2; |
| 304 | } |
| 305 | |
| 306 | /** |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 307 | * Determine the exact visual footprint of the all apps button, taking into account scaling |
| 308 | * and internal padding of the drawable. |
| 309 | */ |
| 310 | private void computeAllAppsButtonSize(Context context) { |
| 311 | Resources res = context.getResources(); |
| 312 | float padding = res.getInteger(R.integer.config_allAppsButtonPaddingPercent) / 100f; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 313 | allAppsButtonVisualSize = (int) (iconSizePx * (1 - padding)) - context.getResources() |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 314 | .getDimensionPixelSize(R.dimen.all_apps_button_scale_down); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 317 | private void updateAvailableDimensions(DisplayMetrics dm, Resources res) { |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 318 | updateIconSize(1f, res, dm); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 319 | |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 320 | // Check to see if the icons fit within the available height. If not, then scale down. |
| 321 | float usedHeight = (cellHeightPx * inv.numRows); |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 322 | int maxHeight = (availableHeightPx - getTotalWorkspacePadding().y); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 323 | if (usedHeight > maxHeight) { |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 324 | float scale = maxHeight / usedHeight; |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 325 | updateIconSize(scale, res, dm); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 326 | } |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 327 | updateAvailableFolderCellDimensions(dm, res); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 330 | private void updateIconSize(float scale, Resources res, DisplayMetrics dm) { |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 331 | float invIconSizePx = isVerticalBarLayout() ? inv.landscapeIconSize : inv.iconSize; |
| 332 | iconSizePx = (int) (Utilities.pxFromDp(invIconSizePx, dm) * scale); |
Sunny Goyal | a50a419 | 2015-12-11 09:50:49 -0800 | [diff] [blame] | 333 | iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale); |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 334 | iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * scale); |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 335 | |
| 336 | // All apps |
| 337 | allAppsIconTextSizePx = iconTextSizePx; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 338 | allAppsIconSizePx = iconSizePx; |
| 339 | allAppsIconDrawablePaddingPx = iconDrawablePaddingPx; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 340 | allAppsCellHeightPx = getCellSize().y; |
| 341 | |
| 342 | if (isVerticalBarLayout()) { |
| 343 | // Always hide the Workspace text with vertical bar layout. |
Jon Miranda | 1091e53 | 2017-07-21 13:31:50 -0700 | [diff] [blame] | 344 | adjustToHideWorkspaceLabels(); |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 345 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 346 | |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 347 | cellWidthPx = iconSizePx + iconDrawablePaddingPx; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 348 | cellHeightPx = iconSizePx + iconDrawablePaddingPx |
| 349 | + Utilities.calculateTextHeight(iconTextSizePx); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 350 | |
| 351 | // Hotseat |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 352 | hotseatCellHeightPx = iconSizePx + iconDrawablePaddingPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 353 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 354 | if (!isVerticalBarLayout()) { |
| 355 | int expectedWorkspaceHeight = availableHeightPx - hotseatBarHeightPx |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 356 | - pageIndicatorSizePx - topWorkspacePadding; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 357 | float minRequiredHeight = dropTargetBarSizePx + workspaceSpringLoadedBottomSpace; |
| 358 | workspaceSpringLoadShrinkFactor = Math.min( |
| 359 | res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f, |
| 360 | 1 - (minRequiredHeight / expectedWorkspaceHeight)); |
| 361 | } else { |
| 362 | workspaceSpringLoadShrinkFactor = |
| 363 | res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f; |
| 364 | } |
| 365 | |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 366 | // Folder icon |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 367 | folderBackgroundOffset = -iconDrawablePaddingPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 368 | folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset; |
Adam Cohen | efca027 | 2016-02-24 19:19:06 -0800 | [diff] [blame] | 369 | folderIconPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding); |
Winson Chung | 0f78572 | 2015-04-08 10:27:49 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 372 | private void updateAvailableFolderCellDimensions(DisplayMetrics dm, Resources res) { |
| 373 | int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_padding_top) |
| 374 | + res.getDimensionPixelSize(R.dimen.folder_label_padding_bottom) |
| 375 | + Utilities.calculateTextHeight(res.getDimension(R.dimen.folder_label_text_size)); |
| 376 | |
Jon Miranda | c1b2399 | 2016-12-13 08:57:36 -0800 | [diff] [blame] | 377 | updateFolderCellSize(1f, dm, res); |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 378 | |
Jon Miranda | c1b2399 | 2016-12-13 08:57:36 -0800 | [diff] [blame] | 379 | // Don't let the folder get too close to the edges of the screen. |
Jon Miranda | 72b5fd1 | 2017-06-25 18:06:23 -0700 | [diff] [blame] | 380 | int folderMargin = edgeMarginPx; |
Jon Miranda | c1b2399 | 2016-12-13 08:57:36 -0800 | [diff] [blame] | 381 | |
| 382 | // Check if the icons fit within the available height. |
| 383 | float usedHeight = folderCellHeightPx * inv.numFolderRows + folderBottomPanelSize; |
| 384 | int maxHeight = availableHeightPx - getTotalWorkspacePadding().y - folderMargin; |
| 385 | float scaleY = maxHeight / usedHeight; |
| 386 | |
| 387 | // Check if the icons fit within the available width. |
| 388 | float usedWidth = folderCellWidthPx * inv.numFolderColumns; |
| 389 | int maxWidth = availableWidthPx - getTotalWorkspacePadding().x - folderMargin; |
| 390 | float scaleX = maxWidth / usedWidth; |
| 391 | |
| 392 | float scale = Math.min(scaleX, scaleY); |
| 393 | if (scale < 1f) { |
| 394 | updateFolderCellSize(scale, dm, res); |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Jon Miranda | c1b2399 | 2016-12-13 08:57:36 -0800 | [diff] [blame] | 398 | private void updateFolderCellSize(float scale, DisplayMetrics dm, Resources res) { |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 399 | folderChildIconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale); |
| 400 | folderChildTextSizePx = |
| 401 | (int) (res.getDimensionPixelSize(R.dimen.folder_child_text_size) * scale); |
| 402 | |
| 403 | int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx); |
| 404 | int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding) * scale); |
| 405 | int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding) * scale); |
| 406 | |
Jon Miranda | c1b2399 | 2016-12-13 08:57:36 -0800 | [diff] [blame] | 407 | folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX; |
Jonathan Miranda | 9ad8746 | 2017-07-26 17:41:02 +0000 | [diff] [blame^] | 408 | folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight; |
| 409 | folderChildDrawablePaddingPx = Math.max(0, |
| 410 | (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3); |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 413 | public void updateInsets(Rect insets) { |
| 414 | mInsets.set(insets); |
| 415 | } |
| 416 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 417 | public void updateAppsViewNumCols() { |
| 418 | allAppsNumCols = allAppsNumPredictiveCols = inv.numColumns; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Tony Wickham | a3c74d1 | 2015-10-23 11:43:47 -0700 | [diff] [blame] | 421 | /** Returns the width and height of the search bar, ignoring any padding. */ |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 422 | public Point getSearchBarDimensForWidgetOpts() { |
Tony Wickham | a3c74d1 | 2015-10-23 11:43:47 -0700 | [diff] [blame] | 423 | if (isVerticalBarLayout()) { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 424 | return new Point(dropTargetBarSizePx, availableHeightPx - 2 * edgeMarginPx); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 425 | } else { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 426 | int gap; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 427 | if (isTablet) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 428 | // Pad the left and right of the workspace to ensure consistent spacing |
| 429 | // between all icons |
Winson Chung | 4f3bfad | 2015-07-13 11:14:30 -0700 | [diff] [blame] | 430 | int width = getCurrentWidth(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 431 | // XXX: If the icon size changes across orientations, we will have to take |
| 432 | // that into account here too. |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 433 | gap = ((width - 2 * edgeMarginPx |
| 434 | - (inv.numColumns * cellWidthPx)) / (2 * (inv.numColumns + 1))) |
| 435 | + edgeMarginPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 436 | } else { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 437 | gap = desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 438 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 439 | return new Point(availableWidthPx - 2 * gap, dropTargetBarSizePx); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 440 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Sunny Goyal | 756cd26 | 2015-08-20 12:33:21 -0700 | [diff] [blame] | 443 | public Point getCellSize() { |
| 444 | Point result = new Point(); |
| 445 | // Since we are only concerned with the overall padding, layout direction does |
| 446 | // not matter. |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 447 | Point padding = getTotalWorkspacePadding(); |
Jon Miranda | 8632d9d | 2017-07-18 12:42:15 -0700 | [diff] [blame] | 448 | int cellPadding = cellLayoutPaddingLeftRightPx * 2; |
| 449 | result.x = calculateCellWidth(availableWidthPx - padding.x - cellPadding, inv.numColumns); |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 450 | result.y = calculateCellHeight(availableHeightPx - padding.y, inv.numRows); |
Sunny Goyal | 756cd26 | 2015-08-20 12:33:21 -0700 | [diff] [blame] | 451 | return result; |
| 452 | } |
| 453 | |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 454 | public Point getTotalWorkspacePadding() { |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 455 | Rect padding = getWorkspacePadding(null); |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 456 | return new Point(padding.left + padding.right, padding.top + padding.bottom); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Returns the workspace padding in the specified orientation. |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 461 | */ |
Sunny Goyal | 6178f13 | 2016-07-11 17:30:03 -0700 | [diff] [blame] | 462 | public Rect getWorkspacePadding(Rect recycle) { |
| 463 | Rect padding = recycle == null ? new Rect() : recycle; |
Tony Wickham | 3a3517f | 2015-10-06 11:27:04 -0700 | [diff] [blame] | 464 | if (isVerticalBarLayout()) { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 465 | if (mInsets.left > 0) { |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 466 | padding.set(mInsets.left + pageIndicatorLandGutterPx, |
| 467 | 0, |
| 468 | hotseatBarHeightPx + hotseatLandLeftNavBarGutterPx |
| 469 | + hotseatBarLeftNavBarRightPaddingPx - mInsets.left, |
| 470 | edgeMarginPx); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 471 | } else { |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 472 | padding.set(pageIndicatorLandGutterPx, |
| 473 | 0, |
| 474 | hotseatBarHeightPx + hotseatLandRightNavBarGutterPx |
| 475 | + hotseatBarRightNavBarRightPaddingPx, |
| 476 | edgeMarginPx); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 477 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 478 | } else { |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 479 | int paddingBottom = hotseatBarHeightPx + pageIndicatorSizePx; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 480 | if (isTablet) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 481 | // Pad the left and right of the workspace to ensure consistent spacing |
| 482 | // between all icons |
Winson Chung | 4f3bfad | 2015-07-13 11:14:30 -0700 | [diff] [blame] | 483 | int width = getCurrentWidth(); |
| 484 | int height = getCurrentHeight(); |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 485 | // The amount of screen space available for left/right padding. |
Sunny Goyal | f5440cb | 2016-12-14 15:13:00 -0800 | [diff] [blame] | 486 | int availablePaddingX = Math.max(0, width - ((inv.numColumns * cellWidthPx) + |
| 487 | ((inv.numColumns - 1) * cellWidthPx))); |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 488 | availablePaddingX = (int) Math.min(availablePaddingX, |
| 489 | width * MAX_HORIZONTAL_PADDING_PERCENT); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 490 | int availablePaddingY = Math.max(0, height - topWorkspacePadding - paddingBottom |
Jon Miranda | c1b08c5 | 2016-11-15 14:37:56 -0800 | [diff] [blame] | 491 | - (2 * inv.numRows * cellHeightPx) - hotseatBarTopPaddingPx |
| 492 | - hotseatBarBottomPaddingPx); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 493 | padding.set(availablePaddingX / 2, topWorkspacePadding + availablePaddingY / 2, |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 494 | availablePaddingX / 2, paddingBottom + availablePaddingY / 2); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 495 | } else { |
| 496 | // Pad the top and bottom of the workspace with search/hotseat bar sizes |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 497 | padding.set(desiredWorkspaceLeftRightMarginPx, |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 498 | topWorkspacePadding, |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 499 | desiredWorkspaceLeftRightMarginPx, |
Winson | fadbe8f | 2016-07-22 16:35:37 -0700 | [diff] [blame] | 500 | paddingBottom); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | return padding; |
| 504 | } |
| 505 | |
Winson | fadbe8f | 2016-07-22 16:35:37 -0700 | [diff] [blame] | 506 | /** |
| 507 | * @return the bounds for which the open folders should be contained within |
| 508 | */ |
| 509 | public Rect getAbsoluteOpenFolderBounds() { |
| 510 | if (isVerticalBarLayout()) { |
| 511 | // Folders should only appear right of the drop target bar and left of the hotseat |
| 512 | return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx, |
| 513 | mInsets.top, |
| 514 | mInsets.left + availableWidthPx - hotseatBarHeightPx - edgeMarginPx, |
| 515 | mInsets.top + availableHeightPx); |
| 516 | } else { |
| 517 | // Folders should only appear below the drop target bar and above the hotseat |
| 518 | return new Rect(mInsets.left, |
| 519 | mInsets.top + dropTargetBarSizePx + edgeMarginPx, |
| 520 | mInsets.left + availableWidthPx, |
Jon Miranda | 2ed276e | 2017-06-29 11:36:34 -0700 | [diff] [blame] | 521 | mInsets.top + availableHeightPx - hotseatBarHeightPx |
| 522 | - pageIndicatorSizePx - edgeMarginPx); |
Winson | fadbe8f | 2016-07-22 16:35:37 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 526 | private int getWorkspacePageSpacing() { |
Tony Wickham | 3a3517f | 2015-10-06 11:27:04 -0700 | [diff] [blame] | 527 | if (isVerticalBarLayout() || isLargeTablet) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 528 | // In landscape mode the page spacing is set to the default. |
| 529 | return defaultPageSpacingPx; |
| 530 | } else { |
| 531 | // In portrait, we want the pages spaced such that there is no |
| 532 | // overhang of the previous / next page into the current page viewport. |
| 533 | // We assume symmetrical padding in portrait mode. |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 534 | return Math.max(defaultPageSpacingPx, getWorkspacePadding(null).left + 1); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Winson Chung | 4f7eb40 | 2015-07-10 18:00:48 -0700 | [diff] [blame] | 538 | int getOverviewModeButtonBarHeight() { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 539 | int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx); |
| 540 | zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx, |
| 541 | Math.max(overviewModeMinIconZoneHeightPx, zoneHeight)); |
Winson Chung | 4f7eb40 | 2015-07-10 18:00:48 -0700 | [diff] [blame] | 542 | return zoneHeight; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 545 | public static int calculateCellWidth(int width, int countX) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 546 | return width / countX; |
| 547 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 548 | public static int calculateCellHeight(int height, int countY) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 549 | return height / countY; |
| 550 | } |
| 551 | |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 552 | /** |
Tony Wickham | 55616cd | 2015-09-23 14:55:17 -0700 | [diff] [blame] | 553 | * When {@code true}, the device is in landscape mode and the hotseat is on the right column. |
| 554 | * When {@code false}, either device is in portrait mode or the device is in landscape mode and |
| 555 | * the hotseat is on the bottom row. |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 556 | */ |
Tony Wickham | ab946a1 | 2015-09-16 15:38:16 -0700 | [diff] [blame] | 557 | public boolean isVerticalBarLayout() { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 558 | return isLandscape && transposeLayoutWithOrientation; |
| 559 | } |
| 560 | |
| 561 | boolean shouldFadeAdjacentWorkspaceScreens() { |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 562 | return isVerticalBarLayout() || isLargeTablet; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 565 | private int getVisibleChildCount(ViewGroup parent) { |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 566 | int visibleChildren = 0; |
| 567 | for (int i = 0; i < parent.getChildCount(); i++) { |
| 568 | if (parent.getChildAt(i).getVisibility() != View.GONE) { |
| 569 | visibleChildren++; |
| 570 | } |
| 571 | } |
| 572 | return visibleChildren; |
| 573 | } |
| 574 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 575 | public void layout(Launcher launcher, boolean notifyListeners) { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 576 | FrameLayout.LayoutParams lp; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 577 | boolean hasVerticalBarLayout = isVerticalBarLayout(); |
| 578 | |
| 579 | // Layout the search bar space |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 580 | Point searchBarBounds = getSearchBarDimensForWidgetOpts(); |
| 581 | View searchBar = launcher.getDropTargetBar(); |
Sunny Goyal | f37a214 | 2016-03-24 17:28:25 -0700 | [diff] [blame] | 582 | lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams(); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 583 | lp.width = searchBarBounds.x; |
| 584 | lp.height = searchBarBounds.y; |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 585 | lp.topMargin = mInsets.top + edgeMarginPx; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 586 | searchBar.setLayoutParams(lp); |
| 587 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 588 | // Layout the workspace |
| 589 | PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 590 | Rect workspacePadding = getWorkspacePadding(null); |
| 591 | workspace.setPadding(workspacePadding.left, workspacePadding.top, workspacePadding.right, |
| 592 | workspacePadding.bottom); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 593 | workspace.setPageSpacing(getWorkspacePageSpacing()); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 594 | |
| 595 | // Layout the hotseat |
Winson | 6231ede | 2016-07-20 17:21:55 -0700 | [diff] [blame] | 596 | Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 597 | lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams(); |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 598 | // We want the edges of the hotseat to line up with the edges of the workspace, but the |
| 599 | // icons in the hotseat are a different size, and so don't line up perfectly. To account for |
| 600 | // this, we pad the left and right of the hotseat with half of the difference of a workspace |
| 601 | // cell vs a hotseat cell. |
| 602 | float workspaceCellWidth = (float) getCurrentWidth() / inv.numColumns; |
| 603 | float hotseatCellWidth = (float) getCurrentWidth() / inv.numHotseatIcons; |
| 604 | int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 605 | if (hasVerticalBarLayout) { |
Winson Chung | f8c6f88 | 2015-06-03 11:27:26 -0700 | [diff] [blame] | 606 | // Vertical hotseat -- The hotseat is fixed in the layout to be on the right of the |
| 607 | // screen regardless of RTL |
| 608 | lp.gravity = Gravity.RIGHT; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 609 | lp.width = hotseatBarHeightPx + mInsets.left + mInsets.right; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 610 | lp.height = LayoutParams.MATCH_PARENT; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 611 | |
| 612 | int paddingRight = mInsets.left > 0 |
| 613 | ? hotseatBarLeftNavBarRightPaddingPx |
| 614 | : hotseatBarRightNavBarRightPaddingPx; |
| 615 | |
Jon Miranda | 52ff325 | 2017-07-14 11:39:16 -0700 | [diff] [blame] | 616 | hotseat.getLayout().setPadding(mInsets.left + cellLayoutPaddingLeftRightPx, |
| 617 | mInsets.top, mInsets.right + paddingRight + cellLayoutPaddingLeftRightPx, |
Winson | 6231ede | 2016-07-20 17:21:55 -0700 | [diff] [blame] | 618 | workspacePadding.bottom); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 619 | } else if (isTablet) { |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 620 | // Pad the hotseat with the workspace padding calculated above |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 621 | lp.gravity = Gravity.BOTTOM; |
| 622 | lp.width = LayoutParams.MATCH_PARENT; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 623 | lp.height = hotseatBarHeightPx + mInsets.bottom; |
Jon Miranda | 52ff325 | 2017-07-14 11:39:16 -0700 | [diff] [blame] | 624 | hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left |
| 625 | + cellLayoutPaddingLeftRightPx, |
| 626 | hotseatBarTopPaddingPx, |
| 627 | hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx, |
Jon Miranda | c1b08c5 | 2016-11-15 14:37:56 -0800 | [diff] [blame] | 628 | hotseatBarBottomPaddingPx + mInsets.bottom); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 629 | } else { |
| 630 | // For phones, layout the hotseat without any bottom margin |
| 631 | // to ensure that we have space for the folders |
| 632 | lp.gravity = Gravity.BOTTOM; |
| 633 | lp.width = LayoutParams.MATCH_PARENT; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 634 | lp.height = hotseatBarHeightPx + mInsets.bottom; |
Jon Miranda | 52ff325 | 2017-07-14 11:39:16 -0700 | [diff] [blame] | 635 | hotseat.getLayout().setPadding(hotseatAdjustment + workspacePadding.left |
| 636 | + cellLayoutPaddingLeftRightPx, |
| 637 | hotseatBarTopPaddingPx, |
| 638 | hotseatAdjustment + workspacePadding.right + cellLayoutPaddingLeftRightPx, |
Jon Miranda | c1b08c5 | 2016-11-15 14:37:56 -0800 | [diff] [blame] | 639 | hotseatBarBottomPaddingPx + mInsets.bottom); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 640 | } |
| 641 | hotseat.setLayoutParams(lp); |
| 642 | |
| 643 | // Layout the page indicators |
| 644 | View pageIndicator = launcher.findViewById(R.id.page_indicator); |
| 645 | if (pageIndicator != null) { |
Hyunyoung Song | 7d2fc81 | 2016-06-15 12:51:30 -0700 | [diff] [blame] | 646 | lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams(); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 647 | if (isVerticalBarLayout()) { |
| 648 | if (mInsets.left > 0) { |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 649 | lp.leftMargin = mInsets.left; |
| 650 | } else { |
| 651 | lp.leftMargin = pageIndicatorLandWorkspaceOffsetPx; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 652 | } |
| 653 | lp.bottomMargin = workspacePadding.bottom; |
| 654 | } else { |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 655 | // Put the page indicators above the hotseat |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 656 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 657 | lp.height = pageIndicatorSizePx; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 658 | lp.bottomMargin = hotseatBarHeightPx + mInsets.bottom; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 659 | } |
Hyunyoung Song | 7d2fc81 | 2016-06-15 12:51:30 -0700 | [diff] [blame] | 660 | pageIndicator.setLayoutParams(lp); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 663 | // Layout the Overview Mode |
Jorim Jaggi | d017f88 | 2014-01-14 17:08:48 -0800 | [diff] [blame] | 664 | ViewGroup overviewMode = launcher.getOverviewPanel(); |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 665 | if (overviewMode != null) { |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 666 | int visibleChildCount = getVisibleChildCount(overviewMode); |
| 667 | int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx; |
Jon Miranda | df3bc52 | 2016-11-01 16:55:56 -0700 | [diff] [blame] | 668 | int maxWidth = totalItemWidth + (visibleChildCount - 1) * overviewModeBarSpacerWidthPx; |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 669 | |
Jon Miranda | df3bc52 | 2016-11-01 16:55:56 -0700 | [diff] [blame] | 670 | lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams(); |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 671 | lp.width = Math.min(availableWidthPx, maxWidth); |
Jon Miranda | df3bc52 | 2016-11-01 16:55:56 -0700 | [diff] [blame] | 672 | lp.height = getOverviewModeButtonBarHeight() + mInsets.bottom; |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 673 | overviewMode.setLayoutParams(lp); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 674 | } |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 675 | |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 676 | // Layout the AllAppsRecyclerView |
| 677 | View view = launcher.findViewById(R.id.apps_list_view); |
Jon Miranda | 2803200 | 2017-07-13 16:18:56 -0700 | [diff] [blame] | 678 | int paddingLeftRight = desiredWorkspaceLeftRightMarginPx + cellLayoutPaddingLeftRightPx; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 679 | view.setPadding(paddingLeftRight, view.getPaddingTop(), paddingLeftRight, |
| 680 | view.getPaddingBottom()); |
| 681 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 682 | if (notifyListeners) { |
| 683 | for (int i = mListeners.size() - 1; i >= 0; i--) { |
| 684 | mListeners.get(i).onLauncherLayoutChanged(); |
Sunny Goyal | 8dfe2da | 2014-10-24 16:26:52 -0700 | [diff] [blame] | 685 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
Winson Chung | 4f3bfad | 2015-07-13 11:14:30 -0700 | [diff] [blame] | 688 | |
| 689 | private int getCurrentWidth() { |
| 690 | return isLandscape |
| 691 | ? Math.max(widthPx, heightPx) |
| 692 | : Math.min(widthPx, heightPx); |
| 693 | } |
| 694 | |
| 695 | private int getCurrentHeight() { |
| 696 | return isLandscape |
| 697 | ? Math.min(widthPx, heightPx) |
| 698 | : Math.max(widthPx, heightPx); |
| 699 | } |
Sunny Goyal | 13178ac | 2016-04-04 16:35:22 -0700 | [diff] [blame] | 700 | |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 701 | public int getCellHeight(@ContainerType int containerType) { |
| 702 | switch (containerType) { |
| 703 | case CellLayout.WORKSPACE: |
| 704 | return cellHeightPx; |
| 705 | case CellLayout.FOLDER: |
| 706 | return folderCellHeightPx; |
| 707 | case CellLayout.HOTSEAT: |
| 708 | return hotseatCellHeightPx; |
| 709 | default: |
| 710 | // ?? |
| 711 | return 0; |
| 712 | } |
| 713 | } |
Sunny Goyal | 13178ac | 2016-04-04 16:35:22 -0700 | [diff] [blame] | 714 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 715 | /** |
| 716 | * @return the left/right paddings for all containers. |
| 717 | */ |
Jon Miranda | c3e4e22 | 2016-11-02 16:56:52 -0700 | [diff] [blame] | 718 | public final int[] getContainerPadding() { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 719 | // No paddings for portrait phone |
| 720 | if (isPhone && !isVerticalBarLayout()) { |
| 721 | return new int[] {0, 0}; |
Sunny Goyal | 13178ac | 2016-04-04 16:35:22 -0700 | [diff] [blame] | 722 | } |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 723 | |
Hyunyoung Song | 9d96fd5 | 2016-08-23 11:06:24 -0700 | [diff] [blame] | 724 | // In landscape, we match the width of the workspace |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 725 | Rect padding = getWorkspacePadding(null); |
| 726 | return new int[] { padding.left - mInsets.left, padding.right + mInsets.left}; |
Sunny Goyal | 13178ac | 2016-04-04 16:35:22 -0700 | [diff] [blame] | 727 | } |
Jon Miranda | cc42c5b | 2016-10-27 17:15:27 -0700 | [diff] [blame] | 728 | |
Jon Miranda | 51f037d | 2016-11-07 08:37:20 -0800 | [diff] [blame] | 729 | public boolean shouldIgnoreLongPressToOverview(float touchX) { |
Jon Miranda | cc42c5b | 2016-10-27 17:15:27 -0700 | [diff] [blame] | 730 | boolean inMultiWindowMode = this != inv.landscapeProfile && this != inv.portraitProfile; |
Jon Miranda | 51f037d | 2016-11-07 08:37:20 -0800 | [diff] [blame] | 731 | boolean touchedLhsEdge = mInsets.left == 0 && touchX < edgeMarginPx; |
| 732 | boolean touchedRhsEdge = mInsets.right == 0 && touchX > (widthPx - edgeMarginPx); |
Jon Miranda | cc42c5b | 2016-10-27 17:15:27 -0700 | [diff] [blame] | 733 | return !inMultiWindowMode && (touchedLhsEdge || touchedRhsEdge); |
| 734 | } |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 735 | |
| 736 | private static Context getContext(Context c, int orientation) { |
| 737 | Configuration context = new Configuration(c.getResources().getConfiguration()); |
| 738 | context.orientation = orientation; |
| 739 | return c.createConfigurationContext(context); |
| 740 | |
| 741 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 742 | } |