blob: 596b15ec57505934058d4106884cd0f7d56982b8 [file] [log] [blame]
Winson Chungb3800242013-10-24 11:01:54 -07001/*
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
17package com.android.launcher3;
18
Thales Lima12d0eff2022-03-25 17:06:11 +000019import static com.android.launcher3.InvariantDeviceProfile.INDEX_DEFAULT;
20import static com.android.launcher3.InvariantDeviceProfile.INDEX_LANDSCAPE;
21import static com.android.launcher3.InvariantDeviceProfile.INDEX_TWO_PANEL_LANDSCAPE;
22import static com.android.launcher3.InvariantDeviceProfile.INDEX_TWO_PANEL_PORTRAIT;
Jon Miranda58561d42021-03-17 10:51:54 -040023import static com.android.launcher3.Utilities.dpiFromPx;
Jon Mirandaaf3aed02021-05-06 18:28:45 -070024import static com.android.launcher3.Utilities.pxFromSp;
Jon Miranda92c1b5d2021-07-20 13:57:16 -070025import static com.android.launcher3.folder.ClippedFolderIconLayoutRule.ICON_OVERLAP_FACTOR;
Sunny Goyal65190ae2022-08-02 14:16:26 -070026import static com.android.launcher3.icons.GraphicsUtils.getShapePath;
Pat Manning5f74bfd2022-07-20 12:08:54 +010027import static com.android.launcher3.testing.shared.ResourceUtils.pxFromDp;
Jon Miranda228877d2021-02-09 11:05:00 -050028
Sunny Goyal35c7b192021-04-20 16:51:10 -070029import android.annotation.SuppressLint;
Winson Chungb3800242013-10-24 11:01:54 -070030import android.content.Context;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070031import android.content.res.Configuration;
Winson Chungb3800242013-10-24 11:01:54 -070032import android.content.res.Resources;
Winson Chungb3800242013-10-24 11:01:54 -070033import android.graphics.Point;
Jon Miranda7ae64ff2016-11-21 16:18:46 -080034import android.graphics.PointF;
Winson Chungb3800242013-10-24 11:01:54 -070035import android.graphics.Rect;
Sunny Goyal35c7b192021-04-20 16:51:10 -070036import android.util.DisplayMetrics;
Sunny Goyal65190ae2022-08-02 14:16:26 -070037import android.util.SparseArray;
Sunny Goyal59d086c2018-05-07 17:31:40 -070038import android.view.Surface;
Winson Chungb3800242013-10-24 11:01:54 -070039
Sunny Goyal65190ae2022-08-02 14:16:26 -070040import androidx.annotation.NonNull;
Sihua Mae04aa202022-07-18 12:43:56 -070041import androidx.annotation.Nullable;
Sunny Goyal65190ae2022-08-02 14:16:26 -070042
Sunny Goyalc13403c2016-11-18 23:44:48 -080043import com.android.launcher3.CellLayout.ContainerType;
Jon Miranda228877d2021-02-09 11:05:00 -050044import com.android.launcher3.DevicePaddings.DevicePadding;
Tony Wickham8912b042018-11-29 15:28:53 -080045import com.android.launcher3.icons.DotRenderer;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070046import com.android.launcher3.icons.IconNormalizer;
Sihua Mae04aa202022-07-18 12:43:56 -070047import com.android.launcher3.model.data.ItemInfo;
Sunny Goyal57b22792021-05-25 14:35:01 -070048import com.android.launcher3.uioverrides.ApiWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070049import com.android.launcher3.util.DisplayController;
50import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb46703d2020-05-27 17:52:03 -070051import com.android.launcher3.util.WindowBounds;
Winson1f064272016-07-18 17:18:02 -070052
Jon Miranda58561d42021-03-17 10:51:54 -040053import java.io.PrintWriter;
Brian Isganitis099945b2022-01-31 18:15:00 -050054import java.util.List;
Jon Miranda58561d42021-03-17 10:51:54 -040055
Sunny Goyal35c7b192021-04-20 16:51:10 -070056@SuppressLint("NewApi")
Winson Chungb3800242013-10-24 11:01:54 -070057public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070058
Sunny Goyal8b9919d2021-04-07 14:45:17 -070059 private static final int DEFAULT_DOT_SIZE = 100;
Alex Chau206ede92022-08-01 17:46:12 +010060 private static final float ALL_APPS_TABLET_MAX_ROWS = 5.5f;
61
Sihua Mae04aa202022-07-18 12:43:56 -070062 public static final PointF DEFAULT_SCALE = new PointF(1.0f, 1.0f);
63 public static final ViewScaleProvider DEFAULT_PROVIDER = itemInfo -> DEFAULT_SCALE;
64
Sunny Goyal57b22792021-05-25 14:35:01 -070065 // Ratio of empty space, qsb should take up to appear visually centered.
Pratyush9afe1ea2021-10-13 11:13:27 +000066 private final float mQsbCenterFactor;
Sunny Goyal1890f672020-04-30 12:28:00 -070067
Adam Cohen2e6da152015-05-06 11:42:25 -070068 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070069 private final Info mInfo;
Sunny Goyal35c7b192021-04-20 16:51:10 -070070 private final DisplayMetrics mMetrics;
Winson Chungbe876472014-05-14 14:15:20 -070071
Sunny Goyalc6205602015-05-21 20:46:33 -070072 // Device properties
73 public final boolean isTablet;
Sunny Goyalc6205602015-05-21 20:46:33 -070074 public final boolean isPhone;
75 public final boolean transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -070076 public final boolean isTwoPanels;
Thales Limaa1012902022-01-13 17:58:42 +000077 public final boolean isQsbInline;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070078
Sunny Goyalc6205602015-05-21 20:46:33 -070079 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070080 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080081 public final boolean isMultiWindowMode;
Alex Chau6ed408f2022-03-04 12:58:05 +000082 public final boolean isGestureMode;
Sunny Goyald70e75a2018-02-22 10:07:32 -080083
Sunny Goyal0addbf02020-04-28 14:17:35 -070084 public final int windowX;
85 public final int windowY;
Sunny Goyalc6205602015-05-21 20:46:33 -070086 public final int widthPx;
87 public final int heightPx;
88 public final int availableWidthPx;
89 public final int availableHeightPx;
Sunny Goyal187b16c2022-03-01 16:53:23 -080090 public final int rotationHint;
Jon Mirandabba64512019-03-27 10:54:17 -070091
92 public final float aspectRatio;
93
Jon Mirandae126d722021-02-25 10:45:20 -050094 public final boolean isScalableGrid;
Thales Lima83bedbf2021-10-05 17:47:39 +010095 private final int mTypeIndex;
Jon Mirandae126d722021-02-25 10:45:20 -050096
Tony Wickhamd6b40372015-09-23 18:37:57 -070097 /**
98 * The maximum amount of left/right workspace padding as a percentage of the screen width.
99 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
100 * 7% of the screen width can be used as right padding.
101 */
102 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -0700103
Jon Miranda3f9bab22017-07-28 14:50:17 -0700104 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700105 private static final float TALLER_DEVICE_ASPECT_RATIO_THRESHOLD = 2.15f;
Jon Miranda80dda302021-07-28 14:14:59 -0700106 private static final float TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP = 252;
107 private static final float TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP = 268;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700108
Sunny Goyalc6205602015-05-21 20:46:33 -0700109 // Workspace
Thales Limad90faab2021-09-21 17:18:47 +0100110 public final int desiredWorkspaceHorizontalMarginOriginalPx;
111 public int desiredWorkspaceHorizontalMarginPx;
Alex Chau51da2192022-05-20 13:32:10 +0100112 public int gridVisualizationPaddingX;
113 public int gridVisualizationPaddingY;
Thales Lima78d00ad2021-09-30 11:29:06 +0100114 public Point cellLayoutBorderSpaceOriginalPx;
115 public Point cellLayoutBorderSpacePx;
Pat Manning08610ca2022-04-05 21:03:16 +0100116 public Rect cellLayoutPaddingPx = new Rect();
117
Sunny Goyalc6205602015-05-21 20:46:33 -0700118 public final int edgeMarginPx;
Alex Chau0c4e11b2022-07-08 18:41:36 +0100119 public final float workspaceContentScale;
Alex Chau906d8822022-05-23 10:42:25 +0100120 public final int workspaceSpringLoadedMinNextPageVisiblePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700121
Jon Miranda58561d42021-03-17 10:51:54 -0400122 private final int extraSpace;
Jon Miranda228877d2021-02-09 11:05:00 -0500123 public int workspaceTopPadding;
124 public int workspaceBottomPadding;
125
Tony Wickham5edf9e22020-03-27 20:06:52 -0700126 // Workspace page indicator
127 public final int workspacePageIndicatorHeight;
128 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -0700129
Sunny Goyalc6205602015-05-21 20:46:33 -0700130 // Workspace icons
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400131 public float iconScale;
Sunny Goyalc6205602015-05-21 20:46:33 -0700132 public int iconSizePx;
133 public int iconTextSizePx;
134 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700135 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700136
Jon Mirandaab3c6812021-06-28 15:42:28 -0700137 public float cellScaleToFit;
Adam Cohen59400422014-03-05 18:07:04 -0800138 public int cellWidthPx;
139 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700140 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800141
Jon Mirandae126d722021-02-25 10:45:20 -0500142 public int cellYPaddingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500143
Sunny Goyalc6205602015-05-21 20:46:33 -0700144 // Folder
Jon Mirandae126d722021-02-25 10:45:20 -0500145 public float folderLabelTextScale;
146 public int folderLabelTextSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700147 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700148 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700149
Jon Mirandae126d722021-02-25 10:45:20 -0500150 // Folder content
Thales Lima78d00ad2021-09-30 11:29:06 +0100151 public Point folderCellLayoutBorderSpacePx;
152 public int folderCellLayoutBorderSpaceOriginalPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500153 public int folderContentPaddingLeftRight;
154 public int folderContentPaddingTop;
155
Jon Mirandabf7d8122016-11-03 15:29:29 -0700156 // Folder cell
Sunny Goyalc6205602015-05-21 20:46:33 -0700157 public int folderCellWidthPx;
158 public int folderCellHeightPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700159
160 // Folder child
161 public int folderChildIconSizePx;
162 public int folderChildTextSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700163 public int folderChildDrawablePaddingPx;
Winson Chungb3800242013-10-24 11:01:54 -0700164
Sunny Goyalc6205602015-05-21 20:46:33 -0700165 // Hotseat
Thales Lima9938c2f2022-07-25 14:38:16 +0100166 public int numShownHotseatIcons;
Sunny Goyalc6205602015-05-21 20:46:33 -0700167 public int hotseatCellHeightPx;
Pat Manning26f70f72022-07-07 13:50:39 +0100168 public final boolean areNavButtonsInline;
Jon Miranda18751b62017-07-31 17:25:27 -0700169 // In portrait: size = height, in landscape: size = width
170 public int hotseatBarSizePx;
Thales Limab8c05952022-05-23 16:58:38 +0100171 public int hotseatBarBottomSpacePx;
Pat Manning26f70f72022-07-07 13:50:39 +0100172 public int hotseatBarEndOffset;
Thales Limab8c05952022-05-23 16:58:38 +0100173 public int hotseatQsbSpace;
Pat Manningde25c0d2022-04-05 19:11:26 +0100174 public int springLoadedHotseatBarTopMarginPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700175 // Start is the side next to the nav bar, end is the side next to the workspace
176 public final int hotseatBarSidePaddingStartPx;
177 public final int hotseatBarSidePaddingEndPx;
Alex Chau206ede92022-08-01 17:46:12 +0100178 public int hotseatQsbWidth; // only used when isQsbInline
Sunny Goyal57b22792021-05-25 14:35:01 -0700179 public final int hotseatQsbHeight;
Thales Limab8c05952022-05-23 16:58:38 +0100180 public final int hotseatQsbVisualHeight;
181 private final int hotseatQsbShadowHeight;
Thales Limadd027342022-01-07 12:54:37 +0000182 public int hotseatBorderSpace;
Adam Cohen2e6da152015-05-06 11:42:25 -0700183
Sunny Goyalc6205602015-05-21 20:46:33 -0700184 // All apps
Thales Lima85c942f2021-12-31 13:04:20 +0000185 public Point allAppsBorderSpacePx;
Alex Chaub8c22e12022-02-14 18:35:58 +0000186 public int allAppsShiftRange;
187 public int allAppsTopPadding;
Andras Kloczl142b0542022-03-09 21:26:02 +0000188 public int bottomSheetTopPadding;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700189 public int allAppsCellHeightPx;
Samuel Fufaf1424a32019-10-04 15:15:19 -0700190 public int allAppsCellWidthPx;
Winson1f064272016-07-18 17:18:02 -0700191 public int allAppsIconSizePx;
192 public int allAppsIconDrawablePaddingPx;
sfufa@google.comde013292021-09-21 18:22:44 -0700193 public int allAppsLeftRightPadding;
Alex Chau27b39b92022-01-14 18:02:18 +0000194 public int allAppsLeftRightMargin;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700195 public final int numShownAllAppsColumns;
Winson1f064272016-07-18 17:18:02 -0700196 public float allAppsIconTextSizePx;
197
Zak Cohen334efeb2021-03-19 16:42:07 -0700198 // Overview
Zak Cohen334efeb2021-03-19 16:42:07 -0700199 public int overviewTaskMarginPx;
Alex Chaudedbc8a2021-03-17 16:53:26 +0000200 public int overviewTaskIconSizePx;
Alex Chauac9df382021-08-02 19:08:41 +0100201 public int overviewTaskIconDrawableSizePx;
202 public int overviewTaskIconDrawableSizeGridPx;
Alex Chaudedbc8a2021-03-17 16:53:26 +0000203 public int overviewTaskThumbnailTopMarginPx;
Alex Chau19c6eca2022-03-10 20:12:56 +0000204 public final int overviewActionsHeight;
Alex Chaua2fc7642022-04-21 14:20:23 +0100205 public final int overviewActionsTopMarginPx;
Alex Chau1bf0fe12021-11-30 15:32:45 +0000206 public final int overviewActionsButtonSpacing;
Alex Chau5fd9d492021-07-22 17:27:11 +0100207 public int overviewPageSpacing;
208 public int overviewRowSpacing;
Alex Chau56bd2572021-11-03 18:48:18 +0000209 public int overviewGridSideMargin;
Zak Cohen334efeb2021-03-19 16:42:07 -0700210
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800211 // Widgets
Sihua Mae04aa202022-07-18 12:43:56 -0700212 private final ViewScaleProvider mViewScaleProvider;
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800213
Sunny Goyal47328fd2016-05-25 18:56:41 -0700214 // Drop Target
215 public int dropTargetBarSizePx;
Pat Manningde25c0d2022-04-05 19:11:26 +0100216 public int dropTargetBarTopMarginPx;
217 public int dropTargetBarBottomMarginPx;
Alex Chaua02eddc2021-04-29 00:36:06 +0100218 public int dropTargetDragPaddingPx;
219 public int dropTargetTextSizePx;
Pat Manningde25c0d2022-04-05 19:11:26 +0100220 public int dropTargetHorizontalPaddingPx;
221 public int dropTargetVerticalPaddingPx;
222 public int dropTargetGapPx;
Alex Chau5b019302022-05-23 10:42:25 +0100223 public int dropTargetButtonWorkspaceEdgeGapPx;
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700224
Winson1f064272016-07-18 17:18:02 -0700225 // Insets
Sunny Goyal07b69292018-01-08 14:19:34 -0800226 private final Rect mInsets = new Rect();
227 public final Rect workspacePadding = new Rect();
Jon Miranda0bd63d12019-03-06 17:29:29 -0800228 // When true, nav bar is on the left side of the screen.
Sunny Goyal59d086c2018-05-07 17:31:40 -0700229 private boolean mIsSeascape;
Winson1f064272016-07-18 17:18:02 -0700230
Tony Wickhamf34bee82018-12-03 18:11:39 -0800231 // Notification dots
Sunny Goyal65190ae2022-08-02 14:16:26 -0700232 public final DotRenderer mDotRendererWorkSpace;
233 public final DotRenderer mDotRendererAllApps;
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800234
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +0100235 // Taskbar
Tony Wickham15883892021-02-12 11:24:40 -0800236 public boolean isTaskbarPresent;
Tony Wickham635e1802021-07-22 14:28:04 -1000237 // Whether Taskbar will inset the bottom of apps by taskbarSize.
238 public boolean isTaskbarPresentInApps;
Tony Wickham15883892021-02-12 11:24:40 -0800239 public int taskbarSize;
Alex Chau6a7d1e22022-01-20 19:54:17 +0000240 public int stashedTaskbarSize;
Tony Wickham15883892021-02-12 11:24:40 -0800241
Alex Chaua02eddc2021-04-29 00:36:06 +0100242 // DragController
243 public int flingToDeleteThresholdVelocity;
244
Vinit Nayak17c4b332021-08-05 12:54:58 -0700245 /** TODO: Once we fully migrate to staged split, remove "isMultiWindowMode" */
Sunny Goyal19ff7282021-04-22 10:12:54 -0700246 DeviceProfile(Context context, InvariantDeviceProfile inv, Info info, WindowBounds windowBounds,
Sunny Goyal65190ae2022-08-02 14:16:26 -0700247 SparseArray<DotRenderer> dotRendererCache, boolean isMultiWindowMode,
Sihua Mae04aa202022-07-18 12:43:56 -0700248 boolean transposeLayoutWithOrientation, boolean useTwoPanels, boolean isGestureMode,
249 @NonNull final ViewScaleProvider viewScaleProvider) {
Sunny Goyalfee35bb2015-05-11 11:38:19 -0700250
Adam Cohen2e6da152015-05-06 11:42:25 -0700251 this.inv = inv;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700252 this.isLandscape = windowBounds.isLandscape();
Sunny Goyald70e75a2018-02-22 10:07:32 -0800253 this.isMultiWindowMode = isMultiWindowMode;
Jon Mirandae126d722021-02-25 10:45:20 -0500254 this.transposeLayoutWithOrientation = transposeLayoutWithOrientation;
Alex Chau6ed408f2022-03-04 12:58:05 +0000255 this.isGestureMode = isGestureMode;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700256 windowX = windowBounds.bounds.left;
257 windowY = windowBounds.bounds.top;
Sunny Goyal187b16c2022-03-01 16:53:23 -0800258 this.rotationHint = windowBounds.rotationHint;
Alex Chaue0227552022-04-06 19:44:43 +0100259 mInsets.set(windowBounds.insets);
Winson Chungb3800242013-10-24 11:01:54 -0700260
Jon Mirandae126d722021-02-25 10:45:20 -0500261 isScalableGrid = inv.isScalable && !isVerticalBarLayout() && !isMultiWindowMode;
Alex Chaudfc8ddf2022-01-25 11:26:25 +0000262 // Determine device posture.
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700263 mInfo = info;
Alex Chau29983072021-11-19 15:14:20 +0000264 isTablet = info.isTablet(windowBounds);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700265 isPhone = !isTablet;
Alex Chau90125822021-11-17 14:16:58 +0000266 isTwoPanels = isTablet && useTwoPanels;
Vinit Nayak58c27cc2022-02-16 17:42:21 -0800267 isTaskbarPresent = isTablet && ApiWrapper.TASKBAR_DRAWN_IN_PROCESS;
Adam Cohen2e6da152015-05-06 11:42:25 -0700268
Alex Chaudfc8ddf2022-01-25 11:26:25 +0000269 // Some more constants.
Alex Chau635b3ab2022-01-26 16:49:10 +0000270 context = getContext(context, info, isVerticalBarLayout() || (isTablet && isLandscape)
Brandon Dayauon07ca8842022-04-27 10:52:28 -0700271 ? Configuration.ORIENTATION_LANDSCAPE
272 : Configuration.ORIENTATION_PORTRAIT,
Alex Chaua6907dc2022-03-18 15:24:07 +0000273 windowBounds);
Sunny Goyal1890f672020-04-30 12:28:00 -0700274 final Resources res = context.getResources();
Alex Chaudfc8ddf2022-01-25 11:26:25 +0000275 mMetrics = res.getDisplayMetrics();
276
277 // Determine sizes.
278 widthPx = windowBounds.bounds.width();
279 heightPx = windowBounds.bounds.height();
280 availableWidthPx = windowBounds.availableSize.x;
Thales Limab8c05952022-05-23 16:58:38 +0100281 availableHeightPx = windowBounds.availableSize.y;
Alex Chaudfc8ddf2022-01-25 11:26:25 +0000282
283 aspectRatio = ((float) Math.max(widthPx, heightPx)) / Math.min(widthPx, heightPx);
284 boolean isTallDevice = Float.compare(aspectRatio, TALL_DEVICE_ASPECT_RATIO_THRESHOLD) >= 0;
285 mQsbCenterFactor = res.getFloat(R.dimen.qsb_center_factor);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700286
Thales Lima83bedbf2021-10-05 17:47:39 +0100287 if (isTwoPanels) {
288 if (isLandscape) {
Thales Lima12d0eff2022-03-25 17:06:11 +0000289 mTypeIndex = INDEX_TWO_PANEL_LANDSCAPE;
Thales Lima83bedbf2021-10-05 17:47:39 +0100290 } else {
Thales Lima12d0eff2022-03-25 17:06:11 +0000291 mTypeIndex = INDEX_TWO_PANEL_PORTRAIT;
Thales Lima83bedbf2021-10-05 17:47:39 +0100292 }
293 } else {
294 if (isLandscape) {
Thales Lima12d0eff2022-03-25 17:06:11 +0000295 mTypeIndex = INDEX_LANDSCAPE;
Thales Lima83bedbf2021-10-05 17:47:39 +0100296 } else {
Thales Lima12d0eff2022-03-25 17:06:11 +0000297 mTypeIndex = INDEX_DEFAULT;
Thales Lima83bedbf2021-10-05 17:47:39 +0100298 }
299 }
300
Tony Wickham15883892021-02-12 11:24:40 -0800301 if (isTaskbarPresent) {
Tony Wickham15883892021-02-12 11:24:40 -0800302 taskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_size);
Alex Chau6a7d1e22022-01-20 19:54:17 +0000303 stashedTaskbarSize = res.getDimensionPixelSize(R.dimen.taskbar_stashed_size);
Tony Wickham15883892021-02-12 11:24:40 -0800304 }
Tony Wickham15883892021-02-12 11:24:40 -0800305
Winson Chungb3800242013-10-24 11:01:54 -0700306 edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
Alex Chau0c4e11b2022-07-08 18:41:36 +0100307 workspaceContentScale = res.getFloat(R.dimen.workspace_content_scale);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700308
Thales Limad90faab2021-09-21 17:18:47 +0100309 desiredWorkspaceHorizontalMarginPx = getHorizontalMarginPx(inv, res);
310 desiredWorkspaceHorizontalMarginOriginalPx = desiredWorkspaceHorizontalMarginPx;
Alex Chau51da2192022-05-20 13:32:10 +0100311 gridVisualizationPaddingX = res.getDimensionPixelSize(
312 R.dimen.grid_visualization_horizontal_cell_spacing);
313 gridVisualizationPaddingY = res.getDimensionPixelSize(
314 R.dimen.grid_visualization_vertical_cell_spacing);
Samuel Fufaee9aff92021-04-05 13:30:40 -0500315
Alex Chaue0227552022-04-06 19:44:43 +0100316 bottomSheetTopPadding = mInsets.top // statusbar height
Andras Kloczl142b0542022-03-09 21:26:02 +0000317 + res.getDimensionPixelSize(R.dimen.bottom_sheet_extra_top_padding)
318 + (isTablet ? 0 : edgeMarginPx); // phones need edgeMarginPx additional padding
319
Jon Mirandae126d722021-02-25 10:45:20 -0500320 folderLabelTextScale = res.getFloat(R.dimen.folder_label_text_scale);
321 folderContentPaddingLeftRight =
322 res.getDimensionPixelSize(R.dimen.folder_content_padding_left_right);
323 folderContentPaddingTop = res.getDimensionPixelSize(R.dimen.folder_content_padding_top);
324
Thales Lima78d00ad2021-09-30 11:29:06 +0100325 cellLayoutBorderSpacePx = getCellLayoutBorderSpace(inv);
Thales Lima85c942f2021-12-31 13:04:20 +0000326 allAppsBorderSpacePx = new Point(
Thales Limabb7d3882021-12-22 12:56:29 +0000327 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].x, mMetrics),
328 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].y, mMetrics));
Thales Lima78d00ad2021-09-30 11:29:06 +0100329 cellLayoutBorderSpaceOriginalPx = new Point(cellLayoutBorderSpacePx);
Thales Lima080d8902022-03-28 15:30:29 +0100330 folderCellLayoutBorderSpaceOriginalPx = pxFromDp(inv.folderBorderSpace, mMetrics);
Thales Lima78d00ad2021-09-30 11:29:06 +0100331 folderCellLayoutBorderSpacePx = new Point(folderCellLayoutBorderSpaceOriginalPx,
332 folderCellLayoutBorderSpaceOriginalPx);
Jon Mirandae126d722021-02-25 10:45:20 -0500333
Tony Wickham5edf9e22020-03-27 20:06:52 -0700334 workspacePageIndicatorHeight = res.getDimensionPixelSize(
335 R.dimen.workspace_page_indicator_height);
336 mWorkspacePageIndicatorOverlapWorkspace =
337 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700338
Winson Chungb3800242013-10-24 11:01:54 -0700339 iconDrawablePaddingOriginalPx =
340 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Alex Chaua02eddc2021-04-29 00:36:06 +0100341
Sunny Goyal47328fd2016-05-25 18:56:41 -0700342 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
Pat Manningde25c0d2022-04-05 19:11:26 +0100343 dropTargetBarTopMarginPx = res.getDimensionPixelSize(R.dimen.drop_target_top_margin);
344 dropTargetBarBottomMarginPx = res.getDimensionPixelSize(R.dimen.drop_target_bottom_margin);
Alex Chaua02eddc2021-04-29 00:36:06 +0100345 dropTargetDragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
346 dropTargetTextSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size);
Pat Manningde25c0d2022-04-05 19:11:26 +0100347 dropTargetHorizontalPaddingPx = res.getDimensionPixelSize(
348 R.dimen.drop_target_button_drawable_horizontal_padding);
349 dropTargetVerticalPaddingPx = res.getDimensionPixelSize(
350 R.dimen.drop_target_button_drawable_vertical_padding);
351 dropTargetGapPx = res.getDimensionPixelSize(R.dimen.drop_target_button_gap);
Alex Chau5b019302022-05-23 10:42:25 +0100352 dropTargetButtonWorkspaceEdgeGapPx = res.getDimensionPixelSize(
353 R.dimen.drop_target_button_workspace_edge_gap);
Alex Chaua02eddc2021-04-29 00:36:06 +0100354
Alex Chau906d8822022-05-23 10:42:25 +0100355 workspaceSpringLoadedMinNextPageVisiblePx = res.getDimensionPixelSize(
356 R.dimen.dynamic_grid_spring_loaded_min_next_space_visible);
Sunny Goyald5190522017-04-24 03:06:54 -0700357
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700358 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700359
Thales Limaa1012902022-01-13 17:58:42 +0000360 hotseatQsbHeight = res.getDimensionPixelSize(R.dimen.qsb_widget_height);
Thales Limab8c05952022-05-23 16:58:38 +0100361 hotseatQsbShadowHeight = res.getDimensionPixelSize(R.dimen.qsb_shadow_height);
362 hotseatQsbVisualHeight = hotseatQsbHeight - 2 * hotseatQsbShadowHeight;
363
Thales Lima12d0eff2022-03-25 17:06:11 +0000364 // Whether QSB might be inline in appropriate orientation (e.g. landscape).
365 boolean canQsbInline = (isTwoPanels ? inv.inlineQsb[INDEX_TWO_PANEL_PORTRAIT]
366 || inv.inlineQsb[INDEX_TWO_PANEL_LANDSCAPE]
367 : inv.inlineQsb[INDEX_DEFAULT] || inv.inlineQsb[INDEX_LANDSCAPE])
368 && hotseatQsbHeight > 0;
369 isQsbInline = inv.inlineQsb[mTypeIndex] && canQsbInline;
Alex Chau6ed408f2022-03-04 12:58:05 +0000370
Thales Lima425f6822022-05-10 13:44:58 -0300371 areNavButtonsInline = isTaskbarPresent && !isGestureMode;
Thales Lima9938c2f2022-07-25 14:38:16 +0100372 numShownHotseatIcons =
373 isTwoPanels ? inv.numDatabaseHotseatIcons : inv.numShownHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000374
Sunny Goyal19ff7282021-04-22 10:12:54 -0700375 numShownAllAppsColumns =
376 isTwoPanels ? inv.numDatabaseAllAppsColumns : inv.numAllAppsColumns;
Thales Limab8c05952022-05-23 16:58:38 +0100377
378 int hotseatBarBottomSpace = pxFromDp(inv.hotseatBarBottomSpace[mTypeIndex], mMetrics);
379 int minQsbMargin = res.getDimensionPixelSize(R.dimen.min_qsb_margin);
380 hotseatQsbSpace = pxFromDp(inv.hotseatQsbSpace[mTypeIndex], mMetrics);
381 // Have a little space between the inset and the QSB
382 if (mInsets.bottom + minQsbMargin > hotseatBarBottomSpace) {
383 int availableSpace = hotseatQsbSpace - (mInsets.bottom - hotseatBarBottomSpace);
384
385 // Only change the spaces if there is space
386 if (availableSpace > 0) {
387 // Make sure there is enough space between hotseat/QSB and QSB/navBar
388 if (availableSpace < minQsbMargin * 2) {
389 minQsbMargin = availableSpace / 2;
390 hotseatQsbSpace = minQsbMargin;
391 } else {
392 hotseatQsbSpace -= minQsbMargin;
393 }
394 }
395 hotseatBarBottomSpacePx = mInsets.bottom + minQsbMargin;
396
Thales Limaa1012902022-01-13 17:58:42 +0000397 } else {
Thales Limab8c05952022-05-23 16:58:38 +0100398 hotseatBarBottomSpacePx = hotseatBarBottomSpace;
Thales Limaa1012902022-01-13 17:58:42 +0000399 }
Thales Lima425f6822022-05-10 13:44:58 -0300400
Pat Manningde25c0d2022-04-05 19:11:26 +0100401 springLoadedHotseatBarTopMarginPx = res.getDimensionPixelSize(
402 R.dimen.spring_loaded_hotseat_top_margin);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700403 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800404 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700405 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700406 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Thales Limab8c05952022-05-23 16:58:38 +0100407 updateHotseatSizes(pxFromDp(inv.iconSize[INDEX_DEFAULT], mMetrics));
Pat Manning26f70f72022-07-07 13:50:39 +0100408 if (areNavButtonsInline) {
409 /*
410 * 3 nav buttons +
Vinit Nayakc7293172022-07-18 16:41:50 -0700411 * Spacing between nav buttons +
Pat Manning26f70f72022-07-07 13:50:39 +0100412 * Little space at the end for contextual buttons +
413 * Little space between icons and nav buttons
414 */
415 hotseatBarEndOffset = 3 * res.getDimensionPixelSize(R.dimen.taskbar_nav_buttons_size)
Vinit Nayakc7293172022-07-18 16:41:50 -0700416 + 2 * res.getDimensionPixelSize(R.dimen.taskbar_button_space_inbetween)
417 + res.getDimensionPixelSize(inv.inlineNavButtonsEndSpacing)
Pat Manning26f70f72022-07-07 13:50:39 +0100418 + res.getDimensionPixelSize(R.dimen.taskbar_hotseat_nav_spacing);
419 } else {
420 hotseatBarEndOffset = 0;
421 }
Jon Miranda8bdb2222021-06-23 18:10:10 -0700422
Alex Chau635b3ab2022-01-26 16:49:10 +0000423 overviewTaskMarginPx = res.getDimensionPixelSize(R.dimen.overview_task_margin);
Alex Chau5fd9d492021-07-22 17:27:11 +0100424 overviewTaskIconSizePx = res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_size);
Alex Chauac9df382021-08-02 19:08:41 +0100425 overviewTaskIconDrawableSizePx =
426 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size);
427 overviewTaskIconDrawableSizeGridPx =
428 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size_grid);
Jeremy Sim1cfe6d42022-07-15 14:40:38 -0700429 overviewTaskThumbnailTopMarginPx = overviewTaskIconSizePx + overviewTaskMarginPx;
Jeremy Sim3c2c3f12022-05-16 20:37:43 -0700430 overviewActionsTopMarginPx = res.getDimensionPixelSize(R.dimen.overview_actions_top_margin);
Alex Chau635b3ab2022-01-26 16:49:10 +0000431 overviewPageSpacing = res.getDimensionPixelSize(R.dimen.overview_page_spacing);
432 overviewActionsButtonSpacing = res.getDimensionPixelSize(
433 R.dimen.overview_actions_button_spacing);
Alex Chau19c6eca2022-03-10 20:12:56 +0000434 overviewActionsHeight = res.getDimensionPixelSize(R.dimen.overview_actions_height);
Jeremy Sim1cfe6d42022-07-15 14:40:38 -0700435 overviewRowSpacing = res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing);
Alex Chau635b3ab2022-01-26 16:49:10 +0000436 overviewGridSideMargin = res.getDimensionPixelSize(R.dimen.overview_grid_side_margin);
Zak Cohen334efeb2021-03-19 16:42:07 -0700437
Jon Miranda2ed276e2017-06-29 11:36:34 -0700438 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400439 extraSpace = updateAvailableDimensions(res);
Jon Miranda80cddbc2021-07-22 13:51:16 -0700440
Jon Miranda2ed276e2017-06-29 11:36:34 -0700441 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400442 if (isScalableGrid && inv.devicePaddings != null) {
443 // Paddings were created assuming no scaling, so we first unscale the extra space.
Jon Mirandaab3c6812021-06-28 15:42:28 -0700444 int unscaledExtraSpace = (int) (extraSpace / cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400445 DevicePadding padding = inv.devicePaddings.getDevicePadding(unscaledExtraSpace);
Jon Miranda228877d2021-02-09 11:05:00 -0500446
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400447 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
448 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
449 int paddingHotseatBottom = padding.getHotseatBottomPadding(unscaledExtraSpace);
450
Jon Mirandaab3c6812021-06-28 15:42:28 -0700451 workspaceTopPadding = Math.round(paddingWorkspaceTop * cellScaleToFit);
452 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * cellScaleToFit);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700453 }
Pat Manning08610ca2022-04-05 21:03:16 +0100454
455 int cellLayoutPadding =
456 isTwoPanels ? cellLayoutBorderSpacePx.x / 2 : res.getDimensionPixelSize(
457 R.dimen.cell_layout_padding);
458 cellLayoutPaddingPx = new Rect(cellLayoutPadding, cellLayoutPadding, cellLayoutPadding,
459 cellLayoutPadding);
Sunny Goyal07b69292018-01-08 14:19:34 -0800460 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800461
Thales Lima425f6822022-05-10 13:44:58 -0300462 // Hotseat and QSB width depends on updated cellSize and workspace padding
Thales Lima9938c2f2022-07-25 14:38:16 +0100463 recalculateHotseatWidthAndBorderSpace(res);
Alex Chau206ede92022-08-01 17:46:12 +0100464
465 // AllApps height calculation depends on updated cellSize
466 if (isTablet) {
467 int collapseHandleHeight =
468 res.getDimensionPixelOffset(R.dimen.bottom_sheet_handle_area_height);
469 int contentHeight = heightPx - collapseHandleHeight - hotseatQsbHeight;
470 int targetContentHeight = (int) (allAppsCellHeightPx * ALL_APPS_TABLET_MAX_ROWS);
471 allAppsTopPadding = Math.max(mInsets.top, contentHeight - targetContentHeight);
472 allAppsShiftRange = heightPx - allAppsTopPadding;
473 } else {
474 allAppsTopPadding = 0;
475 allAppsShiftRange =
476 res.getDimensionPixelSize(R.dimen.all_apps_starting_vertical_translate);
477 }
Thales Lima425f6822022-05-10 13:44:58 -0300478
Alex Chaua02eddc2021-04-29 00:36:06 +0100479 flingToDeleteThresholdVelocity = res.getDimensionPixelSize(
480 R.dimen.drag_flingToDeleteMinVelocity);
481
Sihua Mae04aa202022-07-18 12:43:56 -0700482 mViewScaleProvider = viewScaleProvider;
483
Tony Wickham1237df02017-02-24 08:59:36 -0800484 // This is done last, after iconSizePx is calculated above.
Sunny Goyal65190ae2022-08-02 14:16:26 -0700485 mDotRendererWorkSpace = createDotRenderer(iconSizePx, dotRendererCache);
486 mDotRendererAllApps = createDotRenderer(allAppsIconSizePx, dotRendererCache);
487 }
488
489 private static DotRenderer createDotRenderer(
490 int size, @NonNull SparseArray<DotRenderer> cache) {
491 DotRenderer renderer = cache.get(size);
492 if (renderer == null) {
493 renderer = new DotRenderer(size, getShapePath(DEFAULT_DOT_SIZE), DEFAULT_DOT_SIZE);
494 cache.put(size, renderer);
495 }
496 return renderer;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700497 }
498
Thales Lima425f6822022-05-10 13:44:58 -0300499 /**
500 * QSB width is always calculated because when in 3 button nav the width doesn't follow the
501 * width of the hotseat.
502 */
Thales Lima9938c2f2022-07-25 14:38:16 +0100503 private int calculateQsbWidth(int hotseatBorderSpace) {
Thales Lima425f6822022-05-10 13:44:58 -0300504 if (isQsbInline) {
Alex Chau906d8822022-05-23 10:42:25 +0100505 int columns = getPanelCount() * inv.numColumns;
Thales Lima425f6822022-05-10 13:44:58 -0300506 return getIconToIconWidthForColumns(columns)
507 - iconSizePx * numShownHotseatIcons
508 - hotseatBorderSpace * numShownHotseatIcons;
509 } else {
510 int columns = inv.hotseatColumnSpan[mTypeIndex];
511 return getIconToIconWidthForColumns(columns);
512 }
513 }
Thales Lima2903a622022-03-17 13:52:19 +0000514
Thales Lima425f6822022-05-10 13:44:58 -0300515 private int getIconToIconWidthForColumns(int columns) {
516 return columns * getCellSize().x
517 + (columns - 1) * cellLayoutBorderSpacePx.x
518 - (getCellSize().x - iconSizePx); // left and right cell space
Thales Limaa1012902022-01-13 17:58:42 +0000519 }
520
Thales Limad90faab2021-09-21 17:18:47 +0100521 private int getHorizontalMarginPx(InvariantDeviceProfile idp, Resources res) {
522 if (isVerticalBarLayout()) {
523 return 0;
524 }
525
Thales Lima83bedbf2021-10-05 17:47:39 +0100526 return isScalableGrid
527 ? pxFromDp(idp.horizontalMargin[mTypeIndex], mMetrics)
528 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
Thales Limad90faab2021-09-21 17:18:47 +0100529 }
530
Thales Limab8c05952022-05-23 16:58:38 +0100531 /** Updates hotseatCellHeightPx and hotseatBarSizePx */
532 private void updateHotseatSizes(int hotseatIconSizePx) {
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700533 // Ensure there is enough space for folder icons, which have a slightly larger radius.
534 hotseatCellHeightPx = (int) Math.ceil(hotseatIconSizePx * ICON_OVERLAP_FACTOR);
Thales Limab8c05952022-05-23 16:58:38 +0100535
Jon Miranda0d284852021-06-22 14:50:55 -0700536 if (isVerticalBarLayout()) {
537 hotseatBarSizePx = hotseatIconSizePx + hotseatBarSidePaddingStartPx
538 + hotseatBarSidePaddingEndPx;
Thales Limab8c05952022-05-23 16:58:38 +0100539 } else if (isQsbInline) {
540 hotseatBarSizePx = Math.max(hotseatIconSizePx, hotseatQsbVisualHeight)
541 + hotseatBarBottomSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700542 } else {
Thales Limab8c05952022-05-23 16:58:38 +0100543 hotseatBarSizePx = hotseatIconSizePx
544 + hotseatQsbSpace
545 + hotseatQsbVisualHeight
546 + hotseatBarBottomSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700547 }
548 }
549
Thales Lima9938c2f2022-07-25 14:38:16 +0100550 private void recalculateHotseatWidthAndBorderSpace(Resources res) {
551 hotseatBorderSpace = calculateHotseatBorderSpace();
552 hotseatQsbWidth = calculateQsbWidth(hotseatBorderSpace);
553 // Spaces should be correct when there nav buttons are not inline
554 if (!areNavButtonsInline) {
555 return;
556 }
557
558 // Get the maximum width that the hotseat can be
559 int columns = getPanelCount() * inv.numColumns;
560 int maxHotseatWidth = getIconToIconWidthForColumns(columns);
561 int sideSpace = (availableWidthPx - maxHotseatWidth) / 2;
562 int inlineButtonsOverlap = Math.max(0, hotseatBarEndOffset - sideSpace);
563 // decrease how much the nav buttons go "inside" the hotseat
564 maxHotseatWidth -= inlineButtonsOverlap;
565
566 // Get how much space is required to show the hotseat with QSB
567 int requiredWidth = getHotseatRequiredWidth();
568
569 // If spaces are fine, use them
570 if (requiredWidth <= maxHotseatWidth) {
571 return;
572 }
573
574 // Calculate the difference of widths and remove a little from each space between icons
575 // and QSB if it's inline
576 int spaceDiff = requiredWidth - maxHotseatWidth;
577 int numOfSpaces = numShownHotseatIcons - (isQsbInline ? 0 : 1);
578 hotseatBorderSpace -= (spaceDiff / numOfSpaces);
579
580 int minHotseatIconSpaceDp = res.getDimensionPixelSize(R.dimen.min_hotseat_icon_space);
581 int minHotseatQsbWidthDp = res.getDimensionPixelSize(R.dimen.min_hotseat_qsb_width);
582
583 if (hotseatBorderSpace >= minHotseatIconSpaceDp) {
584 return;
585 }
586
587 // Border space can't be less than the minimum
588 hotseatBorderSpace = minHotseatIconSpaceDp;
589 requiredWidth = getHotseatRequiredWidth();
590
591 // If there is an inline qsb, change its size
592 if (isQsbInline) {
593 hotseatQsbWidth -= requiredWidth - maxHotseatWidth;
594 if (hotseatQsbWidth >= minHotseatQsbWidthDp) {
595 return;
596 }
597
598 // QSB can't be less than the minimum
599 hotseatQsbWidth = minHotseatQsbWidthDp;
600 }
601
602 // If it still doesn't fit, start removing icons
603 do {
604 numShownHotseatIcons--;
605 requiredWidth = getHotseatRequiredWidth();
606 } while (requiredWidth > maxHotseatWidth && numShownHotseatIcons > 1);
607
608 // Add back some space between the icons
609 spaceDiff = maxHotseatWidth - requiredWidth;
610 numOfSpaces = numShownHotseatIcons - (isQsbInline ? 0 : 1);
611 hotseatBorderSpace += (spaceDiff / numOfSpaces);
612 }
613
Thales Lima78d00ad2021-09-30 11:29:06 +0100614 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp) {
Thales Lima080d8902022-03-28 15:30:29 +0100615 return getCellLayoutBorderSpace(idp, 1f);
Thales Lima080d8902022-03-28 15:30:29 +0100616 }
617
618 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp, float scale) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100619 if (!isScalableGrid) {
620 return new Point(0, 0);
621 }
622
Thales Lima080d8902022-03-28 15:30:29 +0100623 int horizontalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].x, mMetrics, scale);
624 int verticalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].y, mMetrics, scale);
Thales Lima78d00ad2021-09-30 11:29:06 +0100625
626 return new Point(horizontalSpacePx, verticalSpacePx);
627 }
628
Sunny Goyal69a8eec2021-07-22 10:02:50 -0700629 public Info getDisplayInfo() {
630 return mInfo;
631 }
632
Jon Miranda611dba42021-03-02 14:12:13 -0500633 /**
634 * We inset the widget padding added by the system and instead rely on the border spacing
635 * between cells to create reliable consistency between widgets
636 */
637 public boolean shouldInsetWidgets() {
638 Rect widgetPadding = inv.defaultWidgetPadding;
639
Jon Miranda49811182021-06-03 09:52:40 -0700640 // Check all sides to ensure that the widget won't overlap into another cell, or into
641 // status bar.
642 return workspaceTopPadding > widgetPadding.top
Thales Lima78d00ad2021-09-30 11:29:06 +0100643 && cellLayoutBorderSpacePx.x > widgetPadding.left
644 && cellLayoutBorderSpacePx.y > widgetPadding.top
645 && cellLayoutBorderSpacePx.x > widgetPadding.right
646 && cellLayoutBorderSpacePx.y > widgetPadding.bottom;
Jon Miranda611dba42021-03-02 14:12:13 -0500647 }
Jon Mirandae126d722021-02-25 10:45:20 -0500648
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700649 public Builder toBuilder(Context context) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800650 WindowBounds bounds = new WindowBounds(
651 widthPx, heightPx, availableWidthPx, availableHeightPx, rotationHint);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700652 bounds.bounds.offsetTo(windowX, windowY);
Alex Chaue0227552022-04-06 19:44:43 +0100653 bounds.insets.set(mInsets);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700654
655 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
656 dotRendererCache.put(iconSizePx, mDotRendererWorkSpace);
657 dotRendererCache.put(allAppsIconSizePx, mDotRendererAllApps);
658
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700659 return new Builder(context, inv, mInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700660 .setWindowBounds(bounds)
661 .setUseTwoPanels(isTwoPanels)
Alex Chau6ed408f2022-03-04 12:58:05 +0000662 .setMultiWindowMode(isMultiWindowMode)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700663 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000664 .setGestureMode(isGestureMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800665 }
666
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700667 public DeviceProfile copy(Context context) {
668 return toBuilder(context).build();
669 }
670
671 /**
672 * TODO: Move this to the builder as part of setMultiWindowMode
673 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700674 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700675 DeviceProfile profile = toBuilder(context)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700676 .setWindowBounds(windowBounds)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700677 .setMultiWindowMode(true)
678 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800679
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800680 // We use these scales to measure and layout the widgets using their full invariant profile
681 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
682 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
683 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
Sihua Mae04aa202022-07-18 12:43:56 -0700684 if (appWidgetScaleX != 1 || appWidgetScaleY != 1) {
685 final PointF p = new PointF(appWidgetScaleX, appWidgetScaleY);
686 profile = profile.toBuilder(context)
687 .setViewScaleProvider(i -> p)
688 .build();
689 }
690
691 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800692
Jon Miranda93e1f042016-11-11 14:13:04 -0800693 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700694 }
695
Adam Cohen63f1ec02014-08-12 09:23:13 -0700696 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400697 * Checks if there is enough space for labels on the workspace.
698 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700699 * It is important to call this method after the All Apps variables have been set.
700 */
Jon Miranda941375e2021-03-31 13:10:45 -0400701 private void hideWorkspaceLabelsIfNotEnoughSpace() {
702 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
703 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
704 - iconTextHeight;
705
706 // We want enough space so that the text is closer to its corresponding icon.
707 if (workspaceCellPaddingY < iconTextHeight) {
708 iconTextSizePx = 0;
709 iconDrawablePaddingPx = 0;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700710 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda941375e2021-03-31 13:10:45 -0400711 autoResizeAllAppsCells();
712 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700713 }
Jon Miranda1091e532017-07-21 13:31:50 -0700714
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700715 /**
716 * Re-computes the all-apps cell size to be independent of workspace
717 */
718 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400719 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
720 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700721 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400722 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700723 }
724
Thales Limab7ef5692022-03-10 10:32:36 +0000725 private void updateAllAppsContainerWidth(Resources res) {
Pat Manning08610ca2022-04-05 21:03:16 +0100726 int cellLayoutHorizontalPadding =
727 (cellLayoutPaddingPx.left + cellLayoutPaddingPx.right) / 2;
Alex Chau27b39b92022-01-14 18:02:18 +0000728 if (isTablet) {
Thales Limab7ef5692022-03-10 10:32:36 +0000729 allAppsLeftRightPadding =
Pat Manning08610ca2022-04-05 21:03:16 +0100730 res.getDimensionPixelSize(R.dimen.all_apps_bottom_sheet_horizontal_padding);
731
sfufa@google.comde013292021-09-21 18:22:44 -0700732 int usedWidth = (allAppsCellWidthPx * numShownAllAppsColumns)
Thales Limab7ef5692022-03-10 10:32:36 +0000733 + (allAppsBorderSpacePx.x * (numShownAllAppsColumns - 1))
Alex Chau27b39b92022-01-14 18:02:18 +0000734 + allAppsLeftRightPadding * 2;
735 allAppsLeftRightMargin = Math.max(1, (availableWidthPx - usedWidth) / 2);
sfufa@google.comde013292021-09-21 18:22:44 -0700736 } else {
737 allAppsLeftRightPadding =
Pat Manning08610ca2022-04-05 21:03:16 +0100738 desiredWorkspaceHorizontalMarginPx + cellLayoutHorizontalPadding;
sfufa@google.comde013292021-09-21 18:22:44 -0700739 }
740 }
741
Jon Miranda228877d2021-02-09 11:05:00 -0500742 /**
743 * Returns the amount of extra (or unused) vertical space.
744 */
745 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700746 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800747
Pat Manning08610ca2022-04-05 21:03:16 +0100748 updateWorkspacePadding();
Jon Mirandae126d722021-02-25 10:45:20 -0500749
750 // Check to see if the icons fit within the available height.
Pat Manninga2e14992022-04-22 11:29:17 +0100751 float usedHeight = getCellLayoutHeightSpecification();
Pat Manning25d53342022-05-10 09:58:49 +0000752 final int maxHeight = getCellLayoutHeight();
Jon Miranda228877d2021-02-09 11:05:00 -0500753 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500754 float scaleY = maxHeight / usedHeight;
755 boolean shouldScale = scaleY < 1f;
756
757 float scaleX = 1f;
758 if (isScalableGrid) {
759 // We scale to fit the cellWidth and cellHeight in the available space.
760 // The benefit of scalable grids is that we can get consistent aspect ratios between
761 // devices.
Pat Manninga2e14992022-04-22 11:29:17 +0100762 float usedWidth =
763 getCellLayoutWidthSpecification() + (desiredWorkspaceHorizontalMarginPx * 2);
Jon Mirandae126d722021-02-25 10:45:20 -0500764 // We do not subtract padding here, as we also scale the workspace padding if needed.
765 scaleX = availableWidthPx / usedWidth;
766 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700767 }
Jon Mirandae126d722021-02-25 10:45:20 -0500768
769 if (shouldScale) {
770 float scale = Math.min(scaleX, scaleY);
771 updateIconSize(scale, res);
Pat Manninga2e14992022-04-22 11:29:17 +0100772 extraHeight = Math.max(0, maxHeight - getCellLayoutHeightSpecification());
Jon Mirandae126d722021-02-25 10:45:20 -0500773 }
774
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700775 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500776 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700777 }
778
Pat Manninga2e14992022-04-22 11:29:17 +0100779 private int getCellLayoutHeightSpecification() {
Pat Manning08610ca2022-04-05 21:03:16 +0100780 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacePx.y * (inv.numRows - 1))
781 + cellLayoutPaddingPx.top + cellLayoutPaddingPx.bottom;
782 }
783
Pat Manninga2e14992022-04-22 11:29:17 +0100784 private int getCellLayoutWidthSpecification() {
Pat Manning25d53342022-05-10 09:58:49 +0000785 int numColumns = getPanelCount() * inv.numColumns;
Pat Manning08610ca2022-04-05 21:03:16 +0100786 return (cellWidthPx * numColumns) + (cellLayoutBorderSpacePx.x * (numColumns - 1))
787 + cellLayoutPaddingPx.left + cellLayoutPaddingPx.right;
Jon Mirandae126d722021-02-25 10:45:20 -0500788 }
789
Jon Miranda6f7e9702019-09-16 14:44:14 -0700790 /**
791 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
792 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
793 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
794 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800795 public void updateIconSize(float scale, Resources res) {
Jon Mirandaab3c6812021-06-28 15:42:28 -0700796 // Icon scale should never exceed 1, otherwise pixellation may occur.
797 iconScale = Math.min(1f, scale);
798 cellScaleToFit = scale;
799
Jon Miranda18751b62017-07-31 17:25:27 -0700800 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800801 final boolean isVerticalLayout = isVerticalBarLayout();
Thales Lima83bedbf2021-10-05 17:47:39 +0100802 float invIconSizeDp = inv.iconSize[mTypeIndex];
803 float invIconTextSizeSp = inv.iconTextSize[mTypeIndex];
Andras Kloczl30fe9152021-08-05 18:02:29 +0200804
Jon Mirandaab3c6812021-06-28 15:42:28 -0700805 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, iconScale));
Jon Mirandaab3c6812021-06-28 15:42:28 -0700806 iconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * iconScale);
807 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * iconScale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700808
Thales Lima080d8902022-03-28 15:30:29 +0100809 cellLayoutBorderSpacePx = getCellLayoutBorderSpace(inv, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500810
811 if (isScalableGrid) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100812 cellWidthPx = pxFromDp(inv.minCellSize[mTypeIndex].x, mMetrics, scale);
813 cellHeightPx = pxFromDp(inv.minCellSize[mTypeIndex].y, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500814 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
815 + Utilities.calculateTextHeight(iconTextSizePx);
816 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
Thales Limad90faab2021-09-21 17:18:47 +0100817 desiredWorkspaceHorizontalMarginPx =
818 (int) (desiredWorkspaceHorizontalMarginOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500819 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500820 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700821 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR)
822 + iconDrawablePaddingPx
Jon Miranda228877d2021-02-09 11:05:00 -0500823 + Utilities.calculateTextHeight(iconTextSizePx);
824 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
825 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
826 && !isMultiWindowMode) {
827 // Ensures that the label is closer to its corresponding icon. This is not an issue
828 // with vertical bar layout or multi-window mode since the issue is handled
829 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
830 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
831 iconDrawablePaddingPx = cellPaddingY;
832 }
Jon Miranda846455e2017-10-02 14:58:52 -0700833 }
Jon Miranda18751b62017-07-31 17:25:27 -0700834
Sunny Goyalae190ff2020-04-14 00:19:01 +0000835 // All apps
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500836 updateAllAppsIconSize(scale, res);
Winson Chungb3800242013-10-24 11:01:54 -0700837
Thales Limab8c05952022-05-23 16:58:38 +0100838 updateHotseatSizes(iconSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700839
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700840 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700841 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
842 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700843 }
844
Thales Lima425f6822022-05-10 13:44:58 -0300845 /**
846 * Hotseat width spans a certain number of columns on scalable grids.
847 * This method calculates the space between the icons to achieve that width.
848 */
849 private int calculateHotseatBorderSpace() {
850 if (!isScalableGrid) return 0;
Thales Lima9938c2f2022-07-25 14:38:16 +0100851 int columns = inv.hotseatColumnSpan[mTypeIndex];
852 float hotseatWidthPx = getIconToIconWidthForColumns(columns);
853 float hotseatIconsTotalPx = iconSizePx * numShownHotseatIcons;
854 return (int) (hotseatWidthPx - hotseatIconsTotalPx) / (numShownHotseatIcons - 1);
Thales Lima425f6822022-05-10 13:44:58 -0300855 }
856
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500857
858 /**
859 * Updates the iconSize for allApps* variants.
860 */
Brian Isganitisa9a78112022-05-23 18:11:47 -0700861 private void updateAllAppsIconSize(float scale, Resources res) {
Thales Lima080d8902022-03-28 15:30:29 +0100862 allAppsBorderSpacePx = new Point(
863 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].x, mMetrics, scale),
864 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].y, mMetrics, scale));
Brandon Dayauon07ca8842022-04-27 10:52:28 -0700865 // AllApps cells don't have real space between cells,
866 // so we add the border space to the cell height
867 allAppsCellHeightPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].y, mMetrics, scale)
868 + allAppsBorderSpacePx.y;
869 // but width is just the cell,
870 // the border is added in #updateAllAppsContainerWidth
Thales Lima080d8902022-03-28 15:30:29 +0100871 if (isScalableGrid) {
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500872 allAppsIconSizePx =
Brandon Dayauon969f6a82022-05-19 09:46:47 -0700873 pxFromDp(inv.allAppsIconSize[mTypeIndex], mMetrics, scale);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500874 allAppsIconTextSizePx =
Brandon Dayauon969f6a82022-05-19 09:46:47 -0700875 pxFromSp(inv.allAppsIconTextSize[mTypeIndex], mMetrics, scale);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500876 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Brandon Dayauon7a8a9ed2022-06-06 14:32:22 -0700877 allAppsCellWidthPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].x, mMetrics, scale);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500878 } else {
Brandon Dayauon07ca8842022-04-27 10:52:28 -0700879 float invIconSizeDp = inv.allAppsIconSize[mTypeIndex];
880 float invIconTextSizeSp = inv.allAppsIconTextSize[mTypeIndex];
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500881 allAppsIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
882 allAppsIconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * scale);
Brandon Dayauon07ca8842022-04-27 10:52:28 -0700883 allAppsIconDrawablePaddingPx =
884 res.getDimensionPixelSize(R.dimen.all_apps_icon_drawable_padding);
Brandon Dayauon7a8a9ed2022-06-06 14:32:22 -0700885 allAppsCellWidthPx = allAppsIconSizePx + (2 * allAppsIconDrawablePaddingPx);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500886 }
887
Thales Limab7ef5692022-03-10 10:32:36 +0000888 updateAllAppsContainerWidth(res);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500889 if (isVerticalBarLayout()) {
890 hideWorkspaceLabelsIfNotEnoughSpace();
891 }
892 }
893
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700894 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700895 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700896
Jon Mirandae126d722021-02-25 10:45:20 -0500897 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
898
Jon Mirandac1b23992016-12-13 08:57:36 -0800899 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700900 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800901 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800902
903 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500904 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Thales Lima78d00ad2021-09-30 11:29:06 +0100905 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacePx.y);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800906 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500907 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800908 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800909
910 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500911 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Thales Lima78d00ad2021-09-30 11:29:06 +0100912 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacePx.x);
Jon Mirandae126d722021-02-25 10:45:20 -0500913 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
914 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800915 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800916
917 float scale = Math.min(scaleX, scaleY);
918 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700919 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700920 }
921 }
922
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700923 private void updateFolderCellSize(float scale, Resources res) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100924 float invIconSizeDp = isVerticalBarLayout()
Thales Lima12d0eff2022-03-25 17:06:11 +0000925 ? inv.iconSize[INDEX_LANDSCAPE]
926 : inv.iconSize[INDEX_DEFAULT];
Sunny Goyal35c7b192021-04-20 16:51:10 -0700927 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Thales Lima83bedbf2021-10-05 17:47:39 +0100928 folderChildTextSizePx =
Thales Lima12d0eff2022-03-25 17:06:11 +0000929 pxFromSp(inv.iconTextSize[INDEX_DEFAULT], mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500930 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700931
932 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700933
Jon Miranda823da222021-03-23 08:08:45 -0400934 if (isScalableGrid) {
Jon Mirandaf33f5b32021-07-22 17:15:31 -0700935 int minWidth = folderChildIconSizePx + iconDrawablePaddingPx * 2;
936 int minHeight = folderChildIconSizePx + iconDrawablePaddingPx * 2 + textHeight;
937
938 folderCellWidthPx = (int) Math.max(minWidth, cellWidthPx * scale);
939 folderCellHeightPx = (int) Math.max(minHeight, cellHeightPx * scale);
Jon Miranda823da222021-03-23 08:08:45 -0400940
Thales Lima78d00ad2021-09-30 11:29:06 +0100941 int scaledSpace = (int) (folderCellLayoutBorderSpaceOriginalPx * scale);
942 folderCellLayoutBorderSpacePx = new Point(scaledSpace, scaledSpace);
943 folderContentPaddingLeftRight = scaledSpace;
944 folderContentPaddingTop = scaledSpace;
Jon Miranda823da222021-03-23 08:08:45 -0400945 } else {
946 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
947 * scale);
948 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
949 * scale);
950
951 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
952 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
953 }
954
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000955 folderChildDrawablePaddingPx = Math.max(0,
956 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700957 }
958
Winson1f064272016-07-18 17:18:02 -0700959 public void updateInsets(Rect insets) {
960 mInsets.set(insets);
961 }
962
Sunny Goyalae6e3182019-04-30 12:04:37 -0700963 /**
964 * The current device insets. This is generally same as the insets being dispatched to
965 * {@link Insettable} elements, but can differ if the element is using a different profile.
966 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800967 public Rect getInsets() {
968 return mInsets;
969 }
970
Sunny Goyal756cd262015-08-20 12:33:21 -0700971 public Point getCellSize() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700972 return getCellSize(null);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700973 }
974
Sunny Goyal19ff7282021-04-22 10:12:54 -0700975 public Point getCellSize(Point result) {
976 if (result == null) {
977 result = new Point();
978 }
Thales Limad1df5fc2021-09-03 18:37:19 +0100979
Pat Manning25d53342022-05-10 09:58:49 +0000980 int shortcutAndWidgetContainerWidth =
981 getCellLayoutWidth() - (cellLayoutPaddingPx.left + cellLayoutPaddingPx.right);
982 result.x = calculateCellWidth(shortcutAndWidgetContainerWidth, cellLayoutBorderSpacePx.x,
983 inv.numColumns);
984 int shortcutAndWidgetContainerHeight =
985 getCellLayoutHeight() - (cellLayoutPaddingPx.top + cellLayoutPaddingPx.bottom);
986 result.y = calculateCellHeight(shortcutAndWidgetContainerHeight, cellLayoutBorderSpacePx.y,
987 inv.numRows);
Sunny Goyal3e8a04b2022-05-09 19:31:45 +0000988 return result;
Pat Manningb45d6c42022-05-03 14:32:06 +0100989 }
990
991 /**
Pat Manning25d53342022-05-10 09:58:49 +0000992 * Gets the number of panels within the workspace.
993 */
994 public int getPanelCount() {
995 return isTwoPanels ? 2 : 1;
996 }
997
998 /**
Pat Manningde25c0d2022-04-05 19:11:26 +0100999 * Gets the space in px from the bottom of last item in the vertical-bar hotseat to the
1000 * bottom of the screen.
1001 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001002 private int getVerticalHotseatLastItemBottomOffset(Context context) {
1003 Rect hotseatBarPadding = getHotseatLayoutPadding(context);
Pat Manningde25c0d2022-04-05 19:11:26 +01001004 int cellHeight = calculateCellHeight(
Pat Manning5f74bfd2022-07-20 12:08:54 +01001005 heightPx - hotseatBarPadding.top - hotseatBarPadding.bottom, hotseatBorderSpace,
Pat Manningde25c0d2022-04-05 19:11:26 +01001006 numShownHotseatIcons);
Pat Manningde25c0d2022-04-05 19:11:26 +01001007 int extraIconEndSpacing = (cellHeight - iconSizePx) / 2;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001008 return extraIconEndSpacing + hotseatBarPadding.bottom;
Pat Manningde25c0d2022-04-05 19:11:26 +01001009 }
1010
1011 /**
1012 * Gets the scaled top of the workspace in px for the spring-loaded edit state.
1013 */
Pat Manning25d53342022-05-10 09:58:49 +00001014 public float getCellLayoutSpringLoadShrunkTop() {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001015 return mInsets.top + dropTargetBarTopMarginPx + dropTargetBarSizePx
Pat Manningde25c0d2022-04-05 19:11:26 +01001016 + dropTargetBarBottomMarginPx;
Pat Manningde25c0d2022-04-05 19:11:26 +01001017 }
1018
1019 /**
1020 * Gets the scaled bottom of the workspace in px for the spring-loaded edit state.
1021 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001022 public float getCellLayoutSpringLoadShrunkBottom(Context context) {
Pat Manningde25c0d2022-04-05 19:11:26 +01001023 int topOfHotseat = hotseatBarSizePx + springLoadedHotseatBarTopMarginPx;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001024 return heightPx - (isVerticalBarLayout()
1025 ? getVerticalHotseatLastItemBottomOffset(context) : topOfHotseat);
Pat Manningde25c0d2022-04-05 19:11:26 +01001026 }
1027
Pat Manningea5c1d22022-04-14 10:58:16 +01001028 /**
Pat Manninga2e14992022-04-22 11:29:17 +01001029 * Gets the scale of the workspace for the spring-loaded edit state.
1030 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001031 public float getWorkspaceSpringLoadScale(Context context) {
1032 float scale =
1033 (getCellLayoutSpringLoadShrunkBottom(context) - getCellLayoutSpringLoadShrunkTop())
1034 / getCellLayoutHeight();
Pat Manninga2e14992022-04-22 11:29:17 +01001035 scale = Math.min(scale, 1f);
1036
1037 // Reduce scale if next pages would not be visible after scaling the workspace
Pat Manning25d53342022-05-10 09:58:49 +00001038 int workspaceWidth = availableWidthPx;
Pat Manninga2e14992022-04-22 11:29:17 +01001039 float scaledWorkspaceWidth = workspaceWidth * scale;
Alex Chau906d8822022-05-23 10:42:25 +01001040 float maxAvailableWidth = workspaceWidth - (2 * workspaceSpringLoadedMinNextPageVisiblePx);
Pat Manninga2e14992022-04-22 11:29:17 +01001041 if (scaledWorkspaceWidth > maxAvailableWidth) {
1042 scale *= maxAvailableWidth / scaledWorkspaceWidth;
1043 }
1044 return scale;
1045 }
1046
Pat Manning25d53342022-05-10 09:58:49 +00001047 /**
1048 * Gets the width of a single Cell Layout, aka a single panel within a Workspace.
1049 *
1050 * <p>This is the width of a Workspace, less its horizontal padding. Note that two-panel
1051 * layouts have two Cell Layouts per workspace.
1052 */
1053 public int getCellLayoutWidth() {
1054 return (availableWidthPx - getTotalWorkspacePadding().x) / getPanelCount();
Alex Chau60953332021-11-24 12:41:07 +00001055 }
1056
Pat Manning25d53342022-05-10 09:58:49 +00001057 /**
1058 * Gets the height of a single Cell Layout, aka a single panel within a Workspace.
1059 *
1060 * <p>This is the height of a Workspace, less its vertical padding.
1061 */
1062 public int getCellLayoutHeight() {
1063 return availableHeightPx - getTotalWorkspacePadding().y;
Pat Manning08610ca2022-04-05 21:03:16 +01001064 }
1065
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001066 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -08001067 return new Point(workspacePadding.left + workspacePadding.right,
1068 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001069 }
1070
1071 /**
Sunny Goyal07b69292018-01-08 14:19:34 -08001072 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
1073 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001074 */
Sunny Goyal07b69292018-01-08 14:19:34 -08001075 private void updateWorkspacePadding() {
1076 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -07001077 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001078 padding.top = 0;
1079 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001080 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -07001081 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -07001082 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -07001083 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -07001084 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -07001085 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -07001086 }
Winson Chungb3800242013-10-24 11:01:54 -07001087 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001088 // Pad the bottom of the workspace with hotseat bar
1089 // and leave a bit of space in case a widget go all the way down
1090 int paddingBottom = hotseatBarSizePx + workspaceBottomPadding
1091 + workspacePageIndicatorHeight - mWorkspacePageIndicatorOverlapWorkspace
1092 - mInsets.bottom;
Pat Manning08610ca2022-04-05 21:03:16 +01001093 int paddingTop = workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx);
1094 int paddingSide = desiredWorkspaceHorizontalMarginPx;
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001095
Pat Manning08610ca2022-04-05 21:03:16 +01001096 padding.set(paddingSide, paddingTop, paddingSide, paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -07001097 }
Pat Manning08610ca2022-04-05 21:03:16 +01001098 insetPadding(workspacePadding, cellLayoutPaddingPx);
1099 }
1100
1101 private void insetPadding(Rect paddings, Rect insets) {
1102 insets.left = Math.min(insets.left, paddings.left);
1103 paddings.left -= insets.left;
1104
1105 insets.top = Math.min(insets.top, paddings.top);
1106 paddings.top -= insets.top;
1107
1108 insets.right = Math.min(insets.right, paddings.right);
1109 paddings.right -= insets.right;
1110
1111 insets.bottom = Math.min(insets.bottom, paddings.bottom);
1112 paddings.bottom -= insets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -07001113 }
1114
Sunny Goyal57b22792021-05-25 14:35:01 -07001115 /**
1116 * Returns the padding for hotseat view
1117 */
1118 public Rect getHotseatLayoutPadding(Context context) {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001119 Rect hotseatBarPadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001120 if (isVerticalBarLayout()) {
Sihua Ma38bb3b02022-03-21 22:20:14 -07001121 // The hotseat icons will be placed in the middle of the hotseat cells.
1122 // Changing the hotseatCellHeightPx is not affecting hotseat icon positions
1123 // in vertical bar layout.
1124 // Workspace icons are moved up by a small factor. The variable diffOverlapFactor
1125 // is set to account for that difference.
1126 float diffOverlapFactor = iconSizePx * (ICON_OVERLAP_FACTOR - 1) / 2;
Pat Manning08610ca2022-04-05 21:03:16 +01001127 int paddingTop = Math.max((int) (mInsets.top + cellLayoutPaddingPx.top
1128 - diffOverlapFactor), 0);
1129 int paddingBottom = Math.max((int) (mInsets.bottom + cellLayoutPaddingPx.bottom
Sihua Ma38bb3b02022-03-21 22:20:14 -07001130 + diffOverlapFactor), 0);
1131
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001132 if (isSeascape()) {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001133 hotseatBarPadding.set(mInsets.left + hotseatBarSidePaddingStartPx, paddingTop,
Pat Manning08610ca2022-04-05 21:03:16 +01001134 hotseatBarSidePaddingEndPx, paddingBottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001135 } else {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001136 hotseatBarPadding.set(hotseatBarSidePaddingEndPx, paddingTop,
Pat Manning08610ca2022-04-05 21:03:16 +01001137 mInsets.right + hotseatBarSidePaddingStartPx, paddingBottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001138 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001139 } else if (isTaskbarPresent) {
Alex Chau51da2192022-05-20 13:32:10 +01001140 // Center the QSB vertically with hotseat
Thales Limab8c05952022-05-23 16:58:38 +01001141 int hotseatBarBottomPadding = getHotseatBarBottomPadding();
1142 int hotseatBarTopPadding =
1143 hotseatBarSizePx - hotseatBarBottomPadding - hotseatCellHeightPx;
Alex Chau51da2192022-05-20 13:32:10 +01001144
Thales Lima612230d2022-03-31 18:04:37 +01001145 // Push icons to the side
Thales Lima9938c2f2022-07-25 14:38:16 +01001146 int requiredWidth = getHotseatRequiredWidth();
Pat Manning26f70f72022-07-07 13:50:39 +01001147 int hotseatWidth = Math.min(requiredWidth, availableWidthPx - hotseatBarEndOffset);
Thales Lima612230d2022-03-31 18:04:37 +01001148 int sideSpacing = (availableWidthPx - hotseatWidth) / 2;
Thales Lima612230d2022-03-31 18:04:37 +01001149
Pat Manning5f74bfd2022-07-20 12:08:54 +01001150 hotseatBarPadding.set(sideSpacing, hotseatBarTopPadding, sideSpacing,
Thales Limab8c05952022-05-23 16:58:38 +01001151 hotseatBarBottomPadding);
Alex Chau51da2192022-05-20 13:32:10 +01001152
1153 boolean isRtl = Utilities.isRtl(context.getResources());
Thales Lima612230d2022-03-31 18:04:37 +01001154 if (isRtl) {
Thales Lima9938c2f2022-07-25 14:38:16 +01001155 hotseatBarPadding.right += getAdditionalQsbSpace();
Thales Lima612230d2022-03-31 18:04:37 +01001156 } else {
Thales Lima9938c2f2022-07-25 14:38:16 +01001157 hotseatBarPadding.left += getAdditionalQsbSpace();
Thales Lima612230d2022-03-31 18:04:37 +01001158 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001159
Pat Manning26f70f72022-07-07 13:50:39 +01001160 if (hotseatBarEndOffset > sideSpacing) {
Thales Lima612230d2022-03-31 18:04:37 +01001161 int diff = isRtl
Pat Manning26f70f72022-07-07 13:50:39 +01001162 ? sideSpacing - hotseatBarEndOffset
1163 : hotseatBarEndOffset - sideSpacing;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001164 hotseatBarPadding.left -= diff;
1165 hotseatBarPadding.right += diff;
Sunny Goyal57b22792021-05-25 14:35:01 -07001166 }
Thales Lima425f6822022-05-10 13:44:58 -03001167 } else if (isScalableGrid) {
Alex Chau206ede92022-08-01 17:46:12 +01001168 int sideSpacing = (availableWidthPx - hotseatQsbWidth) / 2;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001169 hotseatBarPadding.set(sideSpacing,
Thales Limab8c05952022-05-23 16:58:38 +01001170 0,
Thales Lima425f6822022-05-10 13:44:58 -03001171 sideSpacing,
Thales Limab8c05952022-05-23 16:58:38 +01001172 getHotseatBarBottomPadding());
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001173 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001174 // We want the edges of the hotseat to line up with the edges of the workspace, but the
1175 // icons in the hotseat are a different size, and so don't line up perfectly. To account
1176 // for this, we pad the left and right of the hotseat with half of the difference of a
1177 // workspace cell vs a hotseat cell.
1178 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001179 float hotseatCellWidth = (float) widthPx / numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001180 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Pat Manning5f74bfd2022-07-20 12:08:54 +01001181 hotseatBarPadding.set(
Thales Limab8c05952022-05-23 16:58:38 +01001182 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingPx.left
1183 + mInsets.left,
1184 0,
Pat Manning08610ca2022-04-05 21:03:16 +01001185 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingPx.right
Sunny Goyal786940a2020-06-02 02:31:31 -07001186 + mInsets.right,
Thales Limab8c05952022-05-23 16:58:38 +01001187 getHotseatBarBottomPadding());
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001188 }
Pat Manning5f74bfd2022-07-20 12:08:54 +01001189 return hotseatBarPadding;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001190 }
1191
Thales Lima9938c2f2022-07-25 14:38:16 +01001192 private int getAdditionalQsbSpace() {
1193 return isQsbInline ? hotseatQsbWidth + hotseatBorderSpace : 0;
1194 }
1195
1196 /**
1197 * Calculate how much space the hotseat needs to be shown completely
1198 */
1199 private int getHotseatRequiredWidth() {
1200 int additionalQsbSpace = getAdditionalQsbSpace();
1201 return iconSizePx * numShownHotseatIcons
1202 + hotseatBorderSpace * (numShownHotseatIcons - 1)
1203 + additionalQsbSpace;
1204 }
1205
Winsonfadbe8f2016-07-22 16:35:37 -07001206 /**
Sunny Goyal57b22792021-05-25 14:35:01 -07001207 * Returns the number of pixels the QSB is translated from the bottom of the screen.
1208 */
1209 public int getQsbOffsetY() {
Thales Limaa1012902022-01-13 17:58:42 +00001210 if (isQsbInline) {
Thales Limab8c05952022-05-23 16:58:38 +01001211 return getHotseatBarBottomPadding() - ((hotseatQsbHeight - hotseatCellHeightPx) / 2);
1212 } else if (isTaskbarPresent) { // QSB on top
1213 return hotseatBarSizePx - hotseatQsbHeight + hotseatQsbShadowHeight;
Jonathan Miranda8f16a772021-07-23 23:10:37 +00001214 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001215 return hotseatBarBottomSpacePx - hotseatQsbShadowHeight;
Jonathan Miranda7a273e22021-06-24 21:29:10 +00001216 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001217 }
1218
Thales Limab8c05952022-05-23 16:58:38 +01001219 /**
1220 * Returns the number of pixels the hotseat is translated from the bottom of the screen.
1221 */
1222 private int getHotseatBarBottomPadding() {
1223 if (isTaskbarPresent) { // QSB on top or inline
1224 return hotseatBarBottomSpacePx - (Math.abs(hotseatCellHeightPx - iconSizePx) / 2);
Thales Limaa1012902022-01-13 17:58:42 +00001225 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001226 return hotseatBarSizePx - hotseatCellHeightPx;
Thales Limaa1012902022-01-13 17:58:42 +00001227 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001228 }
1229
1230 /**
Alex Chau51da2192022-05-20 13:32:10 +01001231 * Returns the number of pixels the taskbar is translated from the bottom of the screen.
1232 */
1233 public int getTaskbarOffsetY() {
1234 int taskbarIconBottomSpace = (taskbarSize - iconSizePx) / 2;
1235 int launcherIconBottomSpace =
1236 Math.min((hotseatCellHeightPx - iconSizePx) / 2, gridVisualizationPaddingY);
Thales Limab8c05952022-05-23 16:58:38 +01001237 return getHotseatBarBottomPadding() + launcherIconBottomSpace - taskbarIconBottomSpace;
Alex Chau51da2192022-05-20 13:32:10 +01001238 }
1239
1240 /**
Alex Chaua2fc7642022-04-21 14:20:23 +01001241 * Returns the number of pixels required below OverviewActions excluding insets.
1242 */
1243 public int getOverviewActionsClaimedSpaceBelow() {
1244 if (isTaskbarPresent && !isGestureMode) {
1245 // Align vertically to where nav buttons are.
Thales Limab8c05952022-05-23 16:58:38 +01001246 return ((taskbarSize - overviewActionsHeight) / 2) + getTaskbarOffsetY();
Alex Chaua2fc7642022-04-21 14:20:23 +01001247 }
1248
Alex Chau1fe83fe2022-05-09 19:21:13 +01001249 return isTaskbarPresent ? stashedTaskbarSize : mInsets.bottom;
Alex Chaua2fc7642022-04-21 14:20:23 +01001250 }
1251
1252 /** Gets the space that the overview actions will take, including bottom margin. */
1253 public int getOverviewActionsClaimedSpace() {
1254 return overviewActionsTopMarginPx + overviewActionsHeight
1255 + getOverviewActionsClaimedSpaceBelow();
1256 }
1257
1258 /**
Sihua Mae04aa202022-07-18 12:43:56 -07001259 * Takes the View and return the scales of width and height depending on the DeviceProfile
1260 * specifications
1261 *
1262 * @param itemInfo The tag of the widget view
1263 * @return A PointF instance with the x set to be the scale of width, and y being the scale of
1264 * height
1265 */
1266 @NonNull
1267 public PointF getAppWidgetScale(@Nullable final ItemInfo itemInfo) {
1268 return mViewScaleProvider.getScaleFromItemInfo(itemInfo);
1269 }
1270
1271 /**
Winsonfadbe8f2016-07-22 16:35:37 -07001272 * @return the bounds for which the open folders should be contained within
1273 */
1274 public Rect getAbsoluteOpenFolderBounds() {
1275 if (isVerticalBarLayout()) {
1276 // Folders should only appear right of the drop target bar and left of the hotseat
1277 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
1278 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -07001279 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -07001280 mInsets.top + availableHeightPx);
1281 } else {
1282 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -08001283 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -08001284 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -07001285 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -08001286 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -08001287 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -07001288 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -07001289 }
1290 }
1291
Jon Miranda228877d2021-02-09 11:05:00 -05001292 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
1293 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -07001294 }
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +01001295
Jon Miranda228877d2021-02-09 11:05:00 -05001296 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
1297 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -07001298 }
1299
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -07001300 /**
Tony Wickham55616cd2015-09-23 14:55:17 -07001301 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
1302 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
1303 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -07001304 */
Tony Wickhamab946a12015-09-16 15:38:16 -07001305 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -07001306 return isLandscape && transposeLayoutWithOrientation;
1307 }
1308
Sunny Goyal59d086c2018-05-07 17:31:40 -07001309 /**
1310 * Updates orientation information and returns true if it has changed from the previous value.
1311 */
Winson Chung13c1c2c2019-09-06 11:46:19 -07001312 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001313 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001314 boolean isSeascape = DisplayController.INSTANCE.get(context)
1315 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -07001316 if (mIsSeascape != isSeascape) {
1317 mIsSeascape = isSeascape;
Pat Manning08610ca2022-04-05 21:03:16 +01001318 // Hotseat changing sides requires updating workspace left/right paddings
1319 updateWorkspacePadding();
Sunny Goyal59d086c2018-05-07 17:31:40 -07001320 return true;
1321 }
1322 }
1323 return false;
1324 }
1325
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001326 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001327 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001328 }
1329
Sunny Goyal07b69292018-01-08 14:19:34 -08001330 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001331 return isVerticalBarLayout();
Winson Chungb3800242013-10-24 11:01:54 -07001332 }
1333
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001334 public int getCellContentHeight(@ContainerType int containerType) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001335 switch (containerType) {
1336 case CellLayout.WORKSPACE:
1337 return cellHeightPx;
1338 case CellLayout.FOLDER:
1339 return folderCellHeightPx;
1340 case CellLayout.HOTSEAT:
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001341 // The hotseat is the only container where the cell height is going to be
1342 // different from the content within that cell.
1343 return iconSizePx;
Sunny Goyalc13403c2016-11-18 23:44:48 -08001344 default:
1345 // ??
1346 return 0;
1347 }
1348 }
Sunny Goyal13178ac2016-04-04 16:35:22 -07001349
Jon Miranda58561d42021-03-17 10:51:54 -04001350 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001351 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -04001352 }
1353
Pat Manning5f74bfd2022-07-20 12:08:54 +01001354 /** Dumps various DeviceProfile variables to the specified writer. */
1355 public void dump(Context context, String prefix, PrintWriter writer) {
Jon Miranda58561d42021-03-17 10:51:54 -04001356 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -07001357 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -04001358
1359 writer.println(prefix + "\tisTablet:" + isTablet);
Jon Miranda58561d42021-03-17 10:51:54 -04001360 writer.println(prefix + "\tisPhone:" + isPhone);
1361 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
1362 + transposeLayoutWithOrientation);
Alex Chau6ed408f2022-03-04 12:58:05 +00001363 writer.println(prefix + "\tisGestureMode:" + isGestureMode);
Jon Miranda58561d42021-03-17 10:51:54 -04001364
1365 writer.println(prefix + "\tisLandscape:" + isLandscape);
1366 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
Sunny Goyal19ff7282021-04-22 10:12:54 -07001367 writer.println(prefix + "\tisTwoPanels:" + isTwoPanels);
Jon Miranda58561d42021-03-17 10:51:54 -04001368
1369 writer.println(prefix + pxToDpStr("windowX", windowX));
1370 writer.println(prefix + pxToDpStr("windowY", windowY));
1371 writer.println(prefix + pxToDpStr("widthPx", widthPx));
1372 writer.println(prefix + pxToDpStr("heightPx", heightPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001373 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
1374 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
Alex Chaue0227552022-04-06 19:44:43 +01001375 writer.println(prefix + pxToDpStr("mInsets.left", mInsets.left));
1376 writer.println(prefix + pxToDpStr("mInsets.top", mInsets.top));
1377 writer.println(prefix + pxToDpStr("mInsets.right", mInsets.right));
1378 writer.println(prefix + pxToDpStr("mInsets.bottom", mInsets.bottom));
Jon Miranda58561d42021-03-17 10:51:54 -04001379
1380 writer.println(prefix + "\taspectRatio:" + aspectRatio);
1381
1382 writer.println(prefix + "\tisScalableGrid:" + isScalableGrid);
1383
Thales Lima83bedbf2021-10-05 17:47:39 +01001384 writer.println(prefix + "\tinv.numRows: " + inv.numRows);
Alex Chau9fee3fd2021-12-01 18:43:10 +00001385 writer.println(prefix + "\tinv.numColumns: " + inv.numColumns);
1386 writer.println(prefix + "\tinv.numSearchContainerColumns: "
1387 + inv.numSearchContainerColumns);
Jon Miranda58561d42021-03-17 10:51:54 -04001388
Thales Lima83bedbf2021-10-05 17:47:39 +01001389 writer.println(prefix + "\tminCellSize: " + inv.minCellSize[mTypeIndex] + "dp");
Jon Mirandaab3c6812021-06-28 15:42:28 -07001390
Jon Miranda58561d42021-03-17 10:51:54 -04001391 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
1392 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
1393
1394 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
1395 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
1396
Thales Lima83bedbf2021-10-05 17:47:39 +01001397 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Horizontal",
1398 cellLayoutBorderSpacePx.x));
1399 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Vertical",
1400 cellLayoutBorderSpacePx.y));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001401 writer.println(
1402 prefix + pxToDpStr("cellLayoutPaddingPx.left", cellLayoutPaddingPx.left));
1403 writer.println(
1404 prefix + pxToDpStr("cellLayoutPaddingPx.top", cellLayoutPaddingPx.top));
1405 writer.println(
1406 prefix + pxToDpStr("cellLayoutPaddingPx.right", cellLayoutPaddingPx.right));
Pat Manning08610ca2022-04-05 21:03:16 +01001407 writer.println(
1408 prefix + pxToDpStr("cellLayoutPaddingPx.bottom", cellLayoutPaddingPx.bottom));
Thales Lima83bedbf2021-10-05 17:47:39 +01001409
Jon Miranda58561d42021-03-17 10:51:54 -04001410 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
1411 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
1412 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
1413
1414 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
1415 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
1416 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
1417 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
1418 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
1419 folderChildDrawablePaddingPx));
Thales Lima78d00ad2021-09-30 11:29:06 +01001420 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpaceOriginalPx",
1421 folderCellLayoutBorderSpaceOriginalPx));
1422 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Horizontal",
1423 folderCellLayoutBorderSpacePx.x));
1424 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Vertical",
1425 folderCellLayoutBorderSpacePx.y));
Jon Miranda58561d42021-03-17 10:51:54 -04001426
Alex Chaue0227552022-04-06 19:44:43 +01001427 writer.println(prefix + pxToDpStr("bottomSheetTopPadding", bottomSheetTopPadding));
1428
1429 writer.println(prefix + pxToDpStr("allAppsShiftRange", allAppsShiftRange));
1430 writer.println(prefix + pxToDpStr("allAppsTopPadding", allAppsTopPadding));
Jon Miranda58561d42021-03-17 10:51:54 -04001431 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
1432 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
1433 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
1434 allAppsIconDrawablePaddingPx));
1435 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
Alex Chau27b39b92022-01-14 18:02:18 +00001436 writer.println(prefix + pxToDpStr("allAppsCellWidthPx", allAppsCellWidthPx));
Pat Manning26f70f72022-07-07 13:50:39 +01001437 writer.println(prefix + pxToDpStr("allAppsBorderSpacePxX", allAppsBorderSpacePx.x));
1438 writer.println(prefix + pxToDpStr("allAppsBorderSpacePxY", allAppsBorderSpacePx.y));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001439 writer.println(prefix + "\tnumShownAllAppsColumns: " + numShownAllAppsColumns);
Alex Chau62572c02022-07-25 18:36:37 +00001440 writer.println(prefix + pxToDpStr("allAppsLeftRightPadding", allAppsLeftRightPadding));
Alex Chau27b39b92022-01-14 18:02:18 +00001441 writer.println(prefix + pxToDpStr("allAppsLeftRightMargin", allAppsLeftRightMargin));
Jon Miranda58561d42021-03-17 10:51:54 -04001442
1443 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
Thales Lima425f6822022-05-10 13:44:58 -03001444 writer.println(prefix + "\tinv.hotseatColumnSpan: " + inv.hotseatColumnSpan[mTypeIndex]);
Jon Miranda58561d42021-03-17 10:51:54 -04001445 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
Alex Chau62572c02022-07-25 18:36:37 +00001446 writer.println(prefix + pxToDpStr("hotseatBarBottomSpacePx", hotseatBarBottomSpacePx));
Jon Miranda58561d42021-03-17 10:51:54 -04001447 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
1448 hotseatBarSidePaddingStartPx));
1449 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
1450 hotseatBarSidePaddingEndPx));
Pat Manning26f70f72022-07-07 13:50:39 +01001451 writer.println(prefix + pxToDpStr("hotseatBarEndOffset", hotseatBarEndOffset));
Thales Limab8c05952022-05-23 16:58:38 +01001452 writer.println(prefix + pxToDpStr("hotseatQsbSpace", hotseatQsbSpace));
1453 writer.println(prefix + pxToDpStr("hotseatQsbHeight", hotseatQsbHeight));
Pat Manningde25c0d2022-04-05 19:11:26 +01001454 writer.println(prefix + pxToDpStr("springLoadedHotseatBarTopMarginPx",
1455 springLoadedHotseatBarTopMarginPx));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001456 Rect hotseatLayoutPadding = getHotseatLayoutPadding(context);
1457 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).top",
1458 hotseatLayoutPadding.top));
1459 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).bottom",
1460 hotseatLayoutPadding.bottom));
1461 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).left",
1462 hotseatLayoutPadding.left));
1463 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).right",
1464 hotseatLayoutPadding.right));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001465 writer.println(prefix + "\tnumShownHotseatIcons: " + numShownHotseatIcons);
Thales Lima116b51a2022-02-03 16:19:47 +00001466 writer.println(prefix + pxToDpStr("hotseatBorderSpace", hotseatBorderSpace));
Thales Limaa1012902022-01-13 17:58:42 +00001467 writer.println(prefix + "\tisQsbInline: " + isQsbInline);
Alex Chau206ede92022-08-01 17:46:12 +01001468 writer.println(prefix + pxToDpStr("hotseatQsbWidth", hotseatQsbWidth));
Jon Miranda58561d42021-03-17 10:51:54 -04001469
1470 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
Tony Wickham635e1802021-07-22 14:28:04 -10001471 writer.println(prefix + "\tisTaskbarPresentInApps:" + isTaskbarPresentInApps);
Jon Miranda58561d42021-03-17 10:51:54 -04001472 writer.println(prefix + pxToDpStr("taskbarSize", taskbarSize));
Jon Miranda58561d42021-03-17 10:51:54 -04001473
Thales Lima83bedbf2021-10-05 17:47:39 +01001474 writer.println(prefix + pxToDpStr("desiredWorkspaceHorizontalMarginPx",
1475 desiredWorkspaceHorizontalMarginPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001476 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
1477 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
1478 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
Alex Chau62572c02022-07-25 18:36:37 +00001479 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
Jon Miranda58561d42021-03-17 10:51:54 -04001480
Jon Mirandaab3c6812021-06-28 15:42:28 -07001481 writer.println(prefix + pxToDpStr("iconScale", iconScale));
1482 writer.println(prefix + pxToDpStr("cellScaleToFit ", cellScaleToFit));
Jon Miranda58561d42021-03-17 10:51:54 -04001483 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Alex Chau62572c02022-07-25 18:36:37 +00001484 writer.println(prefix + pxToDpStr("unscaled extraSpace", extraSpace / iconScale));
Jon Mirandac9e69fa2021-03-22 17:13:34 -04001485
1486 if (inv.devicePaddings != null) {
1487 int unscaledExtraSpace = (int) (extraSpace / iconScale);
1488 writer.println(prefix + pxToDpStr("maxEmptySpace",
1489 inv.devicePaddings.getDevicePadding(unscaledExtraSpace).getMaxEmptySpacePx()));
1490 }
Jon Miranda58561d42021-03-17 10:51:54 -04001491 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
1492 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
Alex Chau635b3ab2022-01-26 16:49:10 +00001493
1494 writer.println(prefix + pxToDpStr("overviewTaskMarginPx", overviewTaskMarginPx));
Alex Chau635b3ab2022-01-26 16:49:10 +00001495 writer.println(prefix + pxToDpStr("overviewTaskIconSizePx", overviewTaskIconSizePx));
1496 writer.println(prefix + pxToDpStr("overviewTaskIconDrawableSizePx",
1497 overviewTaskIconDrawableSizePx));
1498 writer.println(prefix + pxToDpStr("overviewTaskIconDrawableSizeGridPx",
1499 overviewTaskIconDrawableSizeGridPx));
1500 writer.println(prefix + pxToDpStr("overviewTaskThumbnailTopMarginPx",
1501 overviewTaskThumbnailTopMarginPx));
Alex Chaua2fc7642022-04-21 14:20:23 +01001502 writer.println(prefix + pxToDpStr("overviewActionsTopMarginPx",
1503 overviewActionsTopMarginPx));
1504 writer.println(prefix + pxToDpStr("overviewActionsHeight",
1505 overviewActionsHeight));
Alex Chau635b3ab2022-01-26 16:49:10 +00001506 writer.println(prefix + pxToDpStr("overviewActionsButtonSpacing",
1507 overviewActionsButtonSpacing));
1508 writer.println(prefix + pxToDpStr("overviewPageSpacing", overviewPageSpacing));
1509 writer.println(prefix + pxToDpStr("overviewRowSpacing", overviewRowSpacing));
1510 writer.println(prefix + pxToDpStr("overviewGridSideMargin", overviewGridSideMargin));
Pat Manningde25c0d2022-04-05 19:11:26 +01001511
Alex Chau62572c02022-07-25 18:36:37 +00001512 writer.println(prefix + pxToDpStr("dropTargetBarTopMarginPx", dropTargetBarTopMarginPx));
Pat Manningde25c0d2022-04-05 19:11:26 +01001513 writer.println(prefix + pxToDpStr("dropTargetBarSizePx", dropTargetBarSizePx));
Alex Chau62572c02022-07-25 18:36:37 +00001514 writer.println(
1515 prefix + pxToDpStr("dropTargetBarBottomMarginPx", dropTargetBarBottomMarginPx));
Pat Manningde25c0d2022-04-05 19:11:26 +01001516
Pat Manning5f74bfd2022-07-20 12:08:54 +01001517 writer.println(prefix + pxToDpStr("getCellLayoutSpringLoadShrunkTop()",
1518 getCellLayoutSpringLoadShrunkTop()));
1519 writer.println(prefix + pxToDpStr("getCellLayoutSpringLoadShrunkBottom()",
1520 getCellLayoutSpringLoadShrunkBottom(context)));
Alex Chau906d8822022-05-23 10:42:25 +01001521 writer.println(prefix + pxToDpStr("workspaceSpringLoadedMinNextPageVisiblePx",
1522 workspaceSpringLoadedMinNextPageVisiblePx));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001523 writer.println(prefix + pxToDpStr("getWorkspaceSpringLoadScale()",
1524 getWorkspaceSpringLoadScale(context)));
Thales Limab8c05952022-05-23 16:58:38 +01001525 writer.println(prefix + pxToDpStr("getCellLayoutHeight()", getCellLayoutHeight()));
1526 writer.println(prefix + pxToDpStr("getCellLayoutWidth()", getCellLayoutWidth()));
Jon Miranda58561d42021-03-17 10:51:54 -04001527 }
1528
Alex Chaua6907dc2022-03-18 15:24:07 +00001529 private static Context getContext(Context c, Info info, int orientation, WindowBounds bounds) {
Sunny Goyal1890f672020-04-30 12:28:00 -07001530 Configuration config = new Configuration(c.getResources().getConfiguration());
1531 config.orientation = orientation;
Thales Lima425f6822022-05-10 13:44:58 -03001532 config.densityDpi = info.getDensityDpi();
Alex Chaua6907dc2022-03-18 15:24:07 +00001533 config.smallestScreenWidthDp = (int) info.smallestSizeDp(bounds);
Sunny Goyal1890f672020-04-30 12:28:00 -07001534 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -07001535 }
Sunny Goyalfde55052018-02-01 14:46:13 -08001536
1537 /**
1538 * Callback when a component changes the DeviceProfile associated with it, as a result of
1539 * configuration change
1540 */
1541 public interface OnDeviceProfileChangeListener {
1542
1543 /**
1544 * Called when the device profile is reassigned. Note that for layout and measurements, it
1545 * is sufficient to listen for inset changes. Use this callback when you need to perform
1546 * a one time operation.
1547 */
1548 void onDeviceProfileChanged(DeviceProfile dp);
1549 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001550
Brian Isganitis099945b2022-01-31 18:15:00 -05001551 /** Allows registering listeners for {@link DeviceProfile} changes. */
1552 public interface DeviceProfileListenable {
1553
1554 /** The current device profile. */
1555 DeviceProfile getDeviceProfile();
1556
1557 /** Registered {@link OnDeviceProfileChangeListener} instances. */
1558 List<OnDeviceProfileChangeListener> getOnDeviceProfileChangeListeners();
1559
1560 /** Notifies listeners of a {@link DeviceProfile} change. */
1561 default void dispatchDeviceProfileChanged() {
1562 DeviceProfile deviceProfile = getDeviceProfile();
1563 List<OnDeviceProfileChangeListener> listeners = getOnDeviceProfileChangeListeners();
1564 for (int i = listeners.size() - 1; i >= 0; i--) {
1565 listeners.get(i).onDeviceProfileChanged(deviceProfile);
1566 }
1567 }
1568
1569 /** Register listener for {@link DeviceProfile} changes. */
1570 default void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
1571 getOnDeviceProfileChangeListeners().add(listener);
1572 }
1573
1574 /** Unregister listener for {@link DeviceProfile} changes. */
1575 default void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
1576 getOnDeviceProfileChangeListeners().remove(listener);
1577 }
1578 }
1579
Sihua Mae04aa202022-07-18 12:43:56 -07001580 /**
1581 * Handler that deals with ItemInfo of the views for the DeviceProfile
1582 */
1583 @FunctionalInterface
1584 public interface ViewScaleProvider {
1585 /**
1586 * Get the scales from the view
1587 *
1588 * @param itemInfo The tag of the widget view
1589 * @return PointF instance containing the scale information, or null if using the default
1590 * app widget scale of this device profile.
1591 */
1592 @NonNull
1593 PointF getScaleFromItemInfo(@Nullable ItemInfo itemInfo);
1594 }
1595
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001596 public static class Builder {
1597 private Context mContext;
1598 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -07001599 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001600
Sunny Goyal19ff7282021-04-22 10:12:54 -07001601 private WindowBounds mWindowBounds;
1602 private boolean mUseTwoPanels;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001603
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001604 private boolean mIsMultiWindowMode = false;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001605 private Boolean mTransposeLayoutWithOrientation;
Alex Chau6ed408f2022-03-04 12:58:05 +00001606 private Boolean mIsGestureMode;
Sihua Mae04aa202022-07-18 12:43:56 -07001607 private ViewScaleProvider mViewScaleProvider = null;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001608
Sunny Goyal65190ae2022-08-02 14:16:26 -07001609 private SparseArray<DotRenderer> mDotRendererCache;
1610
Sunny Goyalfd58da62020-08-11 12:06:49 -07001611 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001612 mContext = context;
1613 mInv = inv;
1614 mInfo = info;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001615 }
1616
1617 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
1618 mIsMultiWindowMode = isMultiWindowMode;
1619 return this;
1620 }
1621
Sunny Goyal19ff7282021-04-22 10:12:54 -07001622 public Builder setUseTwoPanels(boolean useTwoPanels) {
1623 mUseTwoPanels = useTwoPanels;
1624 return this;
1625 }
1626
Sunny Goyal65190ae2022-08-02 14:16:26 -07001627 public Builder setDotRendererCache(SparseArray<DotRenderer> dotRendererCache) {
1628 mDotRendererCache = dotRendererCache;
1629 return this;
1630 }
Sunny Goyal19ff7282021-04-22 10:12:54 -07001631
1632 public Builder setWindowBounds(WindowBounds bounds) {
1633 mWindowBounds = bounds;
Sunny Goyal0addbf02020-04-28 14:17:35 -07001634 return this;
1635 }
1636
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001637 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
1638 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
1639 return this;
1640 }
1641
Alex Chau6ed408f2022-03-04 12:58:05 +00001642 public Builder setGestureMode(boolean isGestureMode) {
1643 mIsGestureMode = isGestureMode;
1644 return this;
1645 }
1646
Sihua Mae04aa202022-07-18 12:43:56 -07001647 /**
1648 * Set the viewScaleProvider for the builder
1649 *
1650 * @param viewScaleProvider The viewScaleProvider to be set for the
1651 * DeviceProfile
1652 * @return This builder
1653 */
1654 @NonNull
1655 public Builder setViewScaleProvider(@Nullable ViewScaleProvider viewScaleProvider) {
1656 mViewScaleProvider = viewScaleProvider;
1657 return this;
1658 }
1659
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001660 public DeviceProfile build() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001661 if (mWindowBounds == null) {
1662 throw new IllegalArgumentException("Window bounds not set");
1663 }
1664 if (mTransposeLayoutWithOrientation == null) {
1665 mTransposeLayoutWithOrientation = !mInfo.isTablet(mWindowBounds);
1666 }
Alex Chau6ed408f2022-03-04 12:58:05 +00001667 if (mIsGestureMode == null) {
1668 mIsGestureMode = DisplayController.getNavigationMode(mContext).hasGestures;
1669 }
Sunny Goyal65190ae2022-08-02 14:16:26 -07001670 if (mDotRendererCache == null) {
1671 mDotRendererCache = new SparseArray<>();
1672 }
Sihua Mae04aa202022-07-18 12:43:56 -07001673 if (mViewScaleProvider == null) {
1674 mViewScaleProvider = DEFAULT_PROVIDER;
1675 }
Sunny Goyal65190ae2022-08-02 14:16:26 -07001676 return new DeviceProfile(mContext, mInv, mInfo, mWindowBounds, mDotRendererCache,
1677 mIsMultiWindowMode, mTransposeLayoutWithOrientation, mUseTwoPanels,
Sihua Mae04aa202022-07-18 12:43:56 -07001678 mIsGestureMode, mViewScaleProvider);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001679 }
1680 }
1681
Winson Chungb3800242013-10-24 11:01:54 -07001682}