blob: 0292e0402ddc60842931c0a88e0a210160aab30d [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;
Alex Chaue818bcb2022-09-02 17:27:11 +010055import java.util.Locale;
Jon Miranda58561d42021-03-17 10:51:54 -040056
Sunny Goyal35c7b192021-04-20 16:51:10 -070057@SuppressLint("NewApi")
Winson Chungb3800242013-10-24 11:01:54 -070058public class DeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070059
Sunny Goyal8b9919d2021-04-07 14:45:17 -070060 private static final int DEFAULT_DOT_SIZE = 100;
Alex Chau206ede92022-08-01 17:46:12 +010061 private static final float ALL_APPS_TABLET_MAX_ROWS = 5.5f;
62
Sihua Mae04aa202022-07-18 12:43:56 -070063 public static final PointF DEFAULT_SCALE = new PointF(1.0f, 1.0f);
64 public static final ViewScaleProvider DEFAULT_PROVIDER = itemInfo -> DEFAULT_SCALE;
65
Sunny Goyal57b22792021-05-25 14:35:01 -070066 // Ratio of empty space, qsb should take up to appear visually centered.
Pratyush9afe1ea2021-10-13 11:13:27 +000067 private final float mQsbCenterFactor;
Sunny Goyal1890f672020-04-30 12:28:00 -070068
Adam Cohen2e6da152015-05-06 11:42:25 -070069 public final InvariantDeviceProfile inv;
Sunny Goyalfd58da62020-08-11 12:06:49 -070070 private final Info mInfo;
Sunny Goyal35c7b192021-04-20 16:51:10 -070071 private final DisplayMetrics mMetrics;
Winson Chungbe876472014-05-14 14:15:20 -070072
Sunny Goyalc6205602015-05-21 20:46:33 -070073 // Device properties
74 public final boolean isTablet;
Sunny Goyalc6205602015-05-21 20:46:33 -070075 public final boolean isPhone;
76 public final boolean transposeLayoutWithOrientation;
Sunny Goyal19ff7282021-04-22 10:12:54 -070077 public final boolean isTwoPanels;
Thales Limaa1012902022-01-13 17:58:42 +000078 public final boolean isQsbInline;
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -070079
Sunny Goyalc6205602015-05-21 20:46:33 -070080 // Device properties in current orientation
Sunny Goyald792a772018-04-05 13:37:46 -070081 public final boolean isLandscape;
Sunny Goyald70e75a2018-02-22 10:07:32 -080082 public final boolean isMultiWindowMode;
Alex Chau6ed408f2022-03-04 12:58:05 +000083 public final boolean isGestureMode;
Sunny Goyald70e75a2018-02-22 10:07:32 -080084
Sunny Goyal0addbf02020-04-28 14:17:35 -070085 public final int windowX;
86 public final int windowY;
Sunny Goyalc6205602015-05-21 20:46:33 -070087 public final int widthPx;
88 public final int heightPx;
89 public final int availableWidthPx;
90 public final int availableHeightPx;
Sunny Goyal187b16c2022-03-01 16:53:23 -080091 public final int rotationHint;
Jon Mirandabba64512019-03-27 10:54:17 -070092
93 public final float aspectRatio;
94
Jon Mirandae126d722021-02-25 10:45:20 -050095 public final boolean isScalableGrid;
Thales Lima83bedbf2021-10-05 17:47:39 +010096 private final int mTypeIndex;
Jon Mirandae126d722021-02-25 10:45:20 -050097
Tony Wickhamd6b40372015-09-23 18:37:57 -070098 /**
99 * The maximum amount of left/right workspace padding as a percentage of the screen width.
100 * To be clear, this means that up to 7% of the screen width can be used as left padding, and
101 * 7% of the screen width can be used as right padding.
102 */
103 private static final float MAX_HORIZONTAL_PADDING_PERCENT = 0.14f;
Winson Chungb3800242013-10-24 11:01:54 -0700104
Jon Miranda3f9bab22017-07-28 14:50:17 -0700105 private static final float TALL_DEVICE_ASPECT_RATIO_THRESHOLD = 2.0f;
Jon Miranda80cddbc2021-07-22 13:51:16 -0700106 private static final float TALLER_DEVICE_ASPECT_RATIO_THRESHOLD = 2.15f;
Jon Miranda80dda302021-07-28 14:14:59 -0700107 private static final float TALL_DEVICE_EXTRA_SPACE_THRESHOLD_DP = 252;
108 private static final float TALL_DEVICE_MORE_EXTRA_SPACE_THRESHOLD_DP = 268;
Jon Miranda30d0aa22017-07-25 15:55:29 -0700109
Sunny Goyalc6205602015-05-21 20:46:33 -0700110 // Workspace
Thales Limad90faab2021-09-21 17:18:47 +0100111 public final int desiredWorkspaceHorizontalMarginOriginalPx;
112 public int desiredWorkspaceHorizontalMarginPx;
Alex Chau51da2192022-05-20 13:32:10 +0100113 public int gridVisualizationPaddingX;
114 public int gridVisualizationPaddingY;
Thales Lima78d00ad2021-09-30 11:29:06 +0100115 public Point cellLayoutBorderSpaceOriginalPx;
116 public Point cellLayoutBorderSpacePx;
Pat Manning08610ca2022-04-05 21:03:16 +0100117 public Rect cellLayoutPaddingPx = new Rect();
118
Sunny Goyalc6205602015-05-21 20:46:33 -0700119 public final int edgeMarginPx;
Alex Chau0c4e11b2022-07-08 18:41:36 +0100120 public final float workspaceContentScale;
Alex Chau906d8822022-05-23 10:42:25 +0100121 public final int workspaceSpringLoadedMinNextPageVisiblePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700122
Jon Miranda58561d42021-03-17 10:51:54 -0400123 private final int extraSpace;
Jon Miranda228877d2021-02-09 11:05:00 -0500124 public int workspaceTopPadding;
125 public int workspaceBottomPadding;
126
Tony Wickham5edf9e22020-03-27 20:06:52 -0700127 // Workspace page indicator
128 public final int workspacePageIndicatorHeight;
129 private final int mWorkspacePageIndicatorOverlapWorkspace;
Winson1f064272016-07-18 17:18:02 -0700130
Sunny Goyalc6205602015-05-21 20:46:33 -0700131 // Workspace icons
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400132 public float iconScale;
Sunny Goyalc6205602015-05-21 20:46:33 -0700133 public int iconSizePx;
134 public int iconTextSizePx;
135 public int iconDrawablePaddingPx;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700136 public int iconDrawablePaddingOriginalPx;
Winson Chungb3800242013-10-24 11:01:54 -0700137
Jon Mirandaab3c6812021-06-28 15:42:28 -0700138 public float cellScaleToFit;
Adam Cohen59400422014-03-05 18:07:04 -0800139 public int cellWidthPx;
140 public int cellHeightPx;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700141 public int workspaceCellPaddingXPx;
Adam Cohen59400422014-03-05 18:07:04 -0800142
Jon Mirandae126d722021-02-25 10:45:20 -0500143 public int cellYPaddingPx;
Jon Mirandae126d722021-02-25 10:45:20 -0500144
Sunny Goyalc6205602015-05-21 20:46:33 -0700145 // Folder
Jon Mirandae126d722021-02-25 10:45:20 -0500146 public float folderLabelTextScale;
147 public int folderLabelTextSizePx;
Sunny Goyalc6205602015-05-21 20:46:33 -0700148 public int folderIconSizePx;
Jon Miranda591e3602018-03-28 11:37:00 -0700149 public int folderIconOffsetYPx;
Jon Mirandabf7d8122016-11-03 15:29:29 -0700150
Jon Mirandae126d722021-02-25 10:45:20 -0500151 // Folder content
Thales Lima78d00ad2021-09-30 11:29:06 +0100152 public Point folderCellLayoutBorderSpacePx;
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 Limaa08a4432022-08-09 09:55:17 +0100330 folderCellLayoutBorderSpacePx = new Point(pxFromDp(inv.folderBorderSpaces.x, mMetrics),
331 pxFromDp(inv.folderBorderSpaces.y, mMetrics));
Jon Mirandae126d722021-02-25 10:45:20 -0500332
Tony Wickham5edf9e22020-03-27 20:06:52 -0700333 workspacePageIndicatorHeight = res.getDimensionPixelSize(
334 R.dimen.workspace_page_indicator_height);
335 mWorkspacePageIndicatorOverlapWorkspace =
336 res.getDimensionPixelSize(R.dimen.workspace_page_indicator_overlap_workspace);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700337
Winson Chungb3800242013-10-24 11:01:54 -0700338 iconDrawablePaddingOriginalPx =
339 res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
Alex Chaua02eddc2021-04-29 00:36:06 +0100340
Sunny Goyal47328fd2016-05-25 18:56:41 -0700341 dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
Pat Manningde25c0d2022-04-05 19:11:26 +0100342 dropTargetBarTopMarginPx = res.getDimensionPixelSize(R.dimen.drop_target_top_margin);
343 dropTargetBarBottomMarginPx = res.getDimensionPixelSize(R.dimen.drop_target_bottom_margin);
Alex Chaua02eddc2021-04-29 00:36:06 +0100344 dropTargetDragPaddingPx = res.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
345 dropTargetTextSizePx = res.getDimensionPixelSize(R.dimen.drop_target_text_size);
Pat Manningde25c0d2022-04-05 19:11:26 +0100346 dropTargetHorizontalPaddingPx = res.getDimensionPixelSize(
347 R.dimen.drop_target_button_drawable_horizontal_padding);
348 dropTargetVerticalPaddingPx = res.getDimensionPixelSize(
349 R.dimen.drop_target_button_drawable_vertical_padding);
350 dropTargetGapPx = res.getDimensionPixelSize(R.dimen.drop_target_button_gap);
Alex Chau5b019302022-05-23 10:42:25 +0100351 dropTargetButtonWorkspaceEdgeGapPx = res.getDimensionPixelSize(
352 R.dimen.drop_target_button_workspace_edge_gap);
Alex Chaua02eddc2021-04-29 00:36:06 +0100353
Alex Chau906d8822022-05-23 10:42:25 +0100354 workspaceSpringLoadedMinNextPageVisiblePx = res.getDimensionPixelSize(
355 R.dimen.dynamic_grid_spring_loaded_min_next_space_visible);
Sunny Goyald5190522017-04-24 03:06:54 -0700356
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700357 workspaceCellPaddingXPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_cell_padding_x);
Jon Miranda09660722017-06-14 14:20:14 -0700358
Thales Limaa1012902022-01-13 17:58:42 +0000359 hotseatQsbHeight = res.getDimensionPixelSize(R.dimen.qsb_widget_height);
Thales Limab8c05952022-05-23 16:58:38 +0100360 hotseatQsbShadowHeight = res.getDimensionPixelSize(R.dimen.qsb_shadow_height);
361 hotseatQsbVisualHeight = hotseatQsbHeight - 2 * hotseatQsbShadowHeight;
362
Thales Lima12d0eff2022-03-25 17:06:11 +0000363 // Whether QSB might be inline in appropriate orientation (e.g. landscape).
364 boolean canQsbInline = (isTwoPanels ? inv.inlineQsb[INDEX_TWO_PANEL_PORTRAIT]
365 || inv.inlineQsb[INDEX_TWO_PANEL_LANDSCAPE]
366 : inv.inlineQsb[INDEX_DEFAULT] || inv.inlineQsb[INDEX_LANDSCAPE])
367 && hotseatQsbHeight > 0;
368 isQsbInline = inv.inlineQsb[mTypeIndex] && canQsbInline;
Alex Chau6ed408f2022-03-04 12:58:05 +0000369
Thales Lima425f6822022-05-10 13:44:58 -0300370 areNavButtonsInline = isTaskbarPresent && !isGestureMode;
Thales Lima9938c2f2022-07-25 14:38:16 +0100371 numShownHotseatIcons =
372 isTwoPanels ? inv.numDatabaseHotseatIcons : inv.numShownHotseatIcons;
Alex Chau6ed408f2022-03-04 12:58:05 +0000373
Sunny Goyal19ff7282021-04-22 10:12:54 -0700374 numShownAllAppsColumns =
375 isTwoPanels ? inv.numDatabaseAllAppsColumns : inv.numAllAppsColumns;
Thales Limab8c05952022-05-23 16:58:38 +0100376
377 int hotseatBarBottomSpace = pxFromDp(inv.hotseatBarBottomSpace[mTypeIndex], mMetrics);
378 int minQsbMargin = res.getDimensionPixelSize(R.dimen.min_qsb_margin);
379 hotseatQsbSpace = pxFromDp(inv.hotseatQsbSpace[mTypeIndex], mMetrics);
380 // Have a little space between the inset and the QSB
381 if (mInsets.bottom + minQsbMargin > hotseatBarBottomSpace) {
382 int availableSpace = hotseatQsbSpace - (mInsets.bottom - hotseatBarBottomSpace);
383
384 // Only change the spaces if there is space
385 if (availableSpace > 0) {
386 // Make sure there is enough space between hotseat/QSB and QSB/navBar
387 if (availableSpace < minQsbMargin * 2) {
388 minQsbMargin = availableSpace / 2;
389 hotseatQsbSpace = minQsbMargin;
390 } else {
391 hotseatQsbSpace -= minQsbMargin;
392 }
393 }
394 hotseatBarBottomSpacePx = mInsets.bottom + minQsbMargin;
395
Thales Limaa1012902022-01-13 17:58:42 +0000396 } else {
Thales Limab8c05952022-05-23 16:58:38 +0100397 hotseatBarBottomSpacePx = hotseatBarBottomSpace;
Thales Limaa1012902022-01-13 17:58:42 +0000398 }
Thales Lima425f6822022-05-10 13:44:58 -0300399
Pat Manningde25c0d2022-04-05 19:11:26 +0100400 springLoadedHotseatBarTopMarginPx = res.getDimensionPixelSize(
401 R.dimen.spring_loaded_hotseat_top_margin);
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700402 hotseatBarSidePaddingEndPx =
Sunny Goyal228153d2018-01-04 15:35:22 -0800403 res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_side_padding);
Jon Miranda3f411e72019-05-16 17:15:16 -0700404 // Add a bit of space between nav bar and hotseat in vertical bar layout.
Tony Wickham5edf9e22020-03-27 20:06:52 -0700405 hotseatBarSidePaddingStartPx = isVerticalBarLayout() ? workspacePageIndicatorHeight : 0;
Thales Limab8c05952022-05-23 16:58:38 +0100406 updateHotseatSizes(pxFromDp(inv.iconSize[INDEX_DEFAULT], mMetrics));
Vinit Nayak8a3d0552022-08-05 10:43:29 -0700407 if (areNavButtonsInline && !isPhone) {
Pat Manning26f70f72022-07-07 13:50:39 +0100408 /*
409 * 3 nav buttons +
Vinit Nayakc7293172022-07-18 16:41:50 -0700410 * Spacing between nav buttons +
Pat Manning26f70f72022-07-07 13:50:39 +0100411 * Little space at the end for contextual buttons +
412 * Little space between icons and nav buttons
413 */
414 hotseatBarEndOffset = 3 * res.getDimensionPixelSize(R.dimen.taskbar_nav_buttons_size)
Vinit Nayakc7293172022-07-18 16:41:50 -0700415 + 2 * res.getDimensionPixelSize(R.dimen.taskbar_button_space_inbetween)
416 + res.getDimensionPixelSize(inv.inlineNavButtonsEndSpacing)
Pat Manning26f70f72022-07-07 13:50:39 +0100417 + res.getDimensionPixelSize(R.dimen.taskbar_hotseat_nav_spacing);
418 } else {
419 hotseatBarEndOffset = 0;
420 }
Jon Miranda8bdb2222021-06-23 18:10:10 -0700421
Alex Chau635b3ab2022-01-26 16:49:10 +0000422 overviewTaskMarginPx = res.getDimensionPixelSize(R.dimen.overview_task_margin);
Alex Chau5fd9d492021-07-22 17:27:11 +0100423 overviewTaskIconSizePx = res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_size);
Alex Chauac9df382021-08-02 19:08:41 +0100424 overviewTaskIconDrawableSizePx =
425 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size);
426 overviewTaskIconDrawableSizeGridPx =
427 res.getDimensionPixelSize(R.dimen.task_thumbnail_icon_drawable_size_grid);
Jeremy Sim1cfe6d42022-07-15 14:40:38 -0700428 overviewTaskThumbnailTopMarginPx = overviewTaskIconSizePx + overviewTaskMarginPx;
Jeremy Sim3c2c3f12022-05-16 20:37:43 -0700429 overviewActionsTopMarginPx = res.getDimensionPixelSize(R.dimen.overview_actions_top_margin);
Alex Chau635b3ab2022-01-26 16:49:10 +0000430 overviewPageSpacing = res.getDimensionPixelSize(R.dimen.overview_page_spacing);
431 overviewActionsButtonSpacing = res.getDimensionPixelSize(
432 R.dimen.overview_actions_button_spacing);
Alex Chau19c6eca2022-03-10 20:12:56 +0000433 overviewActionsHeight = res.getDimensionPixelSize(R.dimen.overview_actions_height);
Jeremy Sim1cfe6d42022-07-15 14:40:38 -0700434 overviewRowSpacing = res.getDimensionPixelSize(R.dimen.overview_grid_row_spacing);
Alex Chau635b3ab2022-01-26 16:49:10 +0000435 overviewGridSideMargin = res.getDimensionPixelSize(R.dimen.overview_grid_side_margin);
Zak Cohen334efeb2021-03-19 16:42:07 -0700436
Jon Miranda2ed276e2017-06-29 11:36:34 -0700437 // Calculate all of the remaining variables.
Jon Miranda58561d42021-03-17 10:51:54 -0400438 extraSpace = updateAvailableDimensions(res);
Jon Miranda80cddbc2021-07-22 13:51:16 -0700439
Jon Miranda2ed276e2017-06-29 11:36:34 -0700440 // Now that we have all of the variables calculated, we can tune certain sizes.
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400441 if (isScalableGrid && inv.devicePaddings != null) {
442 // Paddings were created assuming no scaling, so we first unscale the extra space.
Jon Mirandaab3c6812021-06-28 15:42:28 -0700443 int unscaledExtraSpace = (int) (extraSpace / cellScaleToFit);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400444 DevicePadding padding = inv.devicePaddings.getDevicePadding(unscaledExtraSpace);
Jon Miranda228877d2021-02-09 11:05:00 -0500445
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400446 int paddingWorkspaceTop = padding.getWorkspaceTopPadding(unscaledExtraSpace);
447 int paddingWorkspaceBottom = padding.getWorkspaceBottomPadding(unscaledExtraSpace);
448 int paddingHotseatBottom = padding.getHotseatBottomPadding(unscaledExtraSpace);
449
Jon Mirandaab3c6812021-06-28 15:42:28 -0700450 workspaceTopPadding = Math.round(paddingWorkspaceTop * cellScaleToFit);
451 workspaceBottomPadding = Math.round(paddingWorkspaceBottom * cellScaleToFit);
Jon Miranda2ed276e2017-06-29 11:36:34 -0700452 }
Pat Manning08610ca2022-04-05 21:03:16 +0100453
454 int cellLayoutPadding =
455 isTwoPanels ? cellLayoutBorderSpacePx.x / 2 : res.getDimensionPixelSize(
456 R.dimen.cell_layout_padding);
457 cellLayoutPaddingPx = new Rect(cellLayoutPadding, cellLayoutPadding, cellLayoutPadding,
458 cellLayoutPadding);
Sunny Goyal07b69292018-01-08 14:19:34 -0800459 updateWorkspacePadding();
Tony Wickham1237df02017-02-24 08:59:36 -0800460
Thales Lima425f6822022-05-10 13:44:58 -0300461 // Hotseat and QSB width depends on updated cellSize and workspace padding
Thales Lima9938c2f2022-07-25 14:38:16 +0100462 recalculateHotseatWidthAndBorderSpace(res);
Alex Chau206ede92022-08-01 17:46:12 +0100463
464 // AllApps height calculation depends on updated cellSize
465 if (isTablet) {
466 int collapseHandleHeight =
467 res.getDimensionPixelOffset(R.dimen.bottom_sheet_handle_area_height);
468 int contentHeight = heightPx - collapseHandleHeight - hotseatQsbHeight;
469 int targetContentHeight = (int) (allAppsCellHeightPx * ALL_APPS_TABLET_MAX_ROWS);
470 allAppsTopPadding = Math.max(mInsets.top, contentHeight - targetContentHeight);
471 allAppsShiftRange = heightPx - allAppsTopPadding;
472 } else {
473 allAppsTopPadding = 0;
474 allAppsShiftRange =
475 res.getDimensionPixelSize(R.dimen.all_apps_starting_vertical_translate);
476 }
Thales Lima425f6822022-05-10 13:44:58 -0300477
Alex Chaua02eddc2021-04-29 00:36:06 +0100478 flingToDeleteThresholdVelocity = res.getDimensionPixelSize(
479 R.dimen.drag_flingToDeleteMinVelocity);
480
Sihua Mae04aa202022-07-18 12:43:56 -0700481 mViewScaleProvider = viewScaleProvider;
482
Tony Wickham1237df02017-02-24 08:59:36 -0800483 // This is done last, after iconSizePx is calculated above.
Sunny Goyal65190ae2022-08-02 14:16:26 -0700484 mDotRendererWorkSpace = createDotRenderer(iconSizePx, dotRendererCache);
485 mDotRendererAllApps = createDotRenderer(allAppsIconSizePx, dotRendererCache);
486 }
487
488 private static DotRenderer createDotRenderer(
489 int size, @NonNull SparseArray<DotRenderer> cache) {
490 DotRenderer renderer = cache.get(size);
491 if (renderer == null) {
492 renderer = new DotRenderer(size, getShapePath(DEFAULT_DOT_SIZE), DEFAULT_DOT_SIZE);
493 cache.put(size, renderer);
494 }
495 return renderer;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700496 }
497
Thales Lima425f6822022-05-10 13:44:58 -0300498 /**
499 * QSB width is always calculated because when in 3 button nav the width doesn't follow the
500 * width of the hotseat.
501 */
Thales Lima9938c2f2022-07-25 14:38:16 +0100502 private int calculateQsbWidth(int hotseatBorderSpace) {
Thales Lima425f6822022-05-10 13:44:58 -0300503 if (isQsbInline) {
Alex Chau906d8822022-05-23 10:42:25 +0100504 int columns = getPanelCount() * inv.numColumns;
Thales Lima425f6822022-05-10 13:44:58 -0300505 return getIconToIconWidthForColumns(columns)
506 - iconSizePx * numShownHotseatIcons
507 - hotseatBorderSpace * numShownHotseatIcons;
508 } else {
509 int columns = inv.hotseatColumnSpan[mTypeIndex];
510 return getIconToIconWidthForColumns(columns);
511 }
512 }
Thales Lima2903a622022-03-17 13:52:19 +0000513
Thales Lima425f6822022-05-10 13:44:58 -0300514 private int getIconToIconWidthForColumns(int columns) {
515 return columns * getCellSize().x
516 + (columns - 1) * cellLayoutBorderSpacePx.x
Thales Lima1e8b45f2022-08-25 11:50:59 -0400517 - getCellHorizontalSpace();
Thales Limaa1012902022-01-13 17:58:42 +0000518 }
519
Thales Limad90faab2021-09-21 17:18:47 +0100520 private int getHorizontalMarginPx(InvariantDeviceProfile idp, Resources res) {
521 if (isVerticalBarLayout()) {
522 return 0;
523 }
524
Thales Lima83bedbf2021-10-05 17:47:39 +0100525 return isScalableGrid
526 ? pxFromDp(idp.horizontalMargin[mTypeIndex], mMetrics)
527 : res.getDimensionPixelSize(R.dimen.dynamic_grid_left_right_margin);
Thales Limad90faab2021-09-21 17:18:47 +0100528 }
529
Thales Limab8c05952022-05-23 16:58:38 +0100530 /** Updates hotseatCellHeightPx and hotseatBarSizePx */
531 private void updateHotseatSizes(int hotseatIconSizePx) {
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700532 // Ensure there is enough space for folder icons, which have a slightly larger radius.
533 hotseatCellHeightPx = (int) Math.ceil(hotseatIconSizePx * ICON_OVERLAP_FACTOR);
Thales Limab8c05952022-05-23 16:58:38 +0100534
Jon Miranda0d284852021-06-22 14:50:55 -0700535 if (isVerticalBarLayout()) {
536 hotseatBarSizePx = hotseatIconSizePx + hotseatBarSidePaddingStartPx
537 + hotseatBarSidePaddingEndPx;
Thales Limab8c05952022-05-23 16:58:38 +0100538 } else if (isQsbInline) {
539 hotseatBarSizePx = Math.max(hotseatIconSizePx, hotseatQsbVisualHeight)
540 + hotseatBarBottomSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700541 } else {
Thales Limab8c05952022-05-23 16:58:38 +0100542 hotseatBarSizePx = hotseatIconSizePx
543 + hotseatQsbSpace
544 + hotseatQsbVisualHeight
545 + hotseatBarBottomSpacePx;
Jon Miranda0d284852021-06-22 14:50:55 -0700546 }
547 }
548
Thales Lima9938c2f2022-07-25 14:38:16 +0100549 private void recalculateHotseatWidthAndBorderSpace(Resources res) {
550 hotseatBorderSpace = calculateHotseatBorderSpace();
551 hotseatQsbWidth = calculateQsbWidth(hotseatBorderSpace);
552 // Spaces should be correct when there nav buttons are not inline
553 if (!areNavButtonsInline) {
554 return;
555 }
556
557 // Get the maximum width that the hotseat can be
558 int columns = getPanelCount() * inv.numColumns;
559 int maxHotseatWidth = getIconToIconWidthForColumns(columns);
560 int sideSpace = (availableWidthPx - maxHotseatWidth) / 2;
561 int inlineButtonsOverlap = Math.max(0, hotseatBarEndOffset - sideSpace);
562 // decrease how much the nav buttons go "inside" the hotseat
563 maxHotseatWidth -= inlineButtonsOverlap;
564
565 // Get how much space is required to show the hotseat with QSB
566 int requiredWidth = getHotseatRequiredWidth();
567
568 // If spaces are fine, use them
569 if (requiredWidth <= maxHotseatWidth) {
570 return;
571 }
572
573 // Calculate the difference of widths and remove a little from each space between icons
574 // and QSB if it's inline
575 int spaceDiff = requiredWidth - maxHotseatWidth;
576 int numOfSpaces = numShownHotseatIcons - (isQsbInline ? 0 : 1);
577 hotseatBorderSpace -= (spaceDiff / numOfSpaces);
578
579 int minHotseatIconSpaceDp = res.getDimensionPixelSize(R.dimen.min_hotseat_icon_space);
580 int minHotseatQsbWidthDp = res.getDimensionPixelSize(R.dimen.min_hotseat_qsb_width);
581
582 if (hotseatBorderSpace >= minHotseatIconSpaceDp) {
583 return;
584 }
585
586 // Border space can't be less than the minimum
587 hotseatBorderSpace = minHotseatIconSpaceDp;
588 requiredWidth = getHotseatRequiredWidth();
589
590 // If there is an inline qsb, change its size
591 if (isQsbInline) {
592 hotseatQsbWidth -= requiredWidth - maxHotseatWidth;
593 if (hotseatQsbWidth >= minHotseatQsbWidthDp) {
594 return;
595 }
596
597 // QSB can't be less than the minimum
598 hotseatQsbWidth = minHotseatQsbWidthDp;
599 }
600
601 // If it still doesn't fit, start removing icons
602 do {
603 numShownHotseatIcons--;
604 requiredWidth = getHotseatRequiredWidth();
605 } while (requiredWidth > maxHotseatWidth && numShownHotseatIcons > 1);
606
607 // Add back some space between the icons
608 spaceDiff = maxHotseatWidth - requiredWidth;
609 numOfSpaces = numShownHotseatIcons - (isQsbInline ? 0 : 1);
610 hotseatBorderSpace += (spaceDiff / numOfSpaces);
611 }
612
Thales Lima78d00ad2021-09-30 11:29:06 +0100613 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp) {
Thales Lima080d8902022-03-28 15:30:29 +0100614 return getCellLayoutBorderSpace(idp, 1f);
Thales Lima080d8902022-03-28 15:30:29 +0100615 }
616
617 private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp, float scale) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100618 if (!isScalableGrid) {
619 return new Point(0, 0);
620 }
621
Thales Lima080d8902022-03-28 15:30:29 +0100622 int horizontalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].x, mMetrics, scale);
623 int verticalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].y, mMetrics, scale);
Thales Lima78d00ad2021-09-30 11:29:06 +0100624
625 return new Point(horizontalSpacePx, verticalSpacePx);
626 }
627
Sunny Goyal69a8eec2021-07-22 10:02:50 -0700628 public Info getDisplayInfo() {
629 return mInfo;
630 }
631
Jon Miranda611dba42021-03-02 14:12:13 -0500632 /**
633 * We inset the widget padding added by the system and instead rely on the border spacing
634 * between cells to create reliable consistency between widgets
635 */
636 public boolean shouldInsetWidgets() {
637 Rect widgetPadding = inv.defaultWidgetPadding;
638
Jon Miranda49811182021-06-03 09:52:40 -0700639 // Check all sides to ensure that the widget won't overlap into another cell, or into
640 // status bar.
641 return workspaceTopPadding > widgetPadding.top
Thales Lima78d00ad2021-09-30 11:29:06 +0100642 && cellLayoutBorderSpacePx.x > widgetPadding.left
643 && cellLayoutBorderSpacePx.y > widgetPadding.top
644 && cellLayoutBorderSpacePx.x > widgetPadding.right
645 && cellLayoutBorderSpacePx.y > widgetPadding.bottom;
Jon Miranda611dba42021-03-02 14:12:13 -0500646 }
Jon Mirandae126d722021-02-25 10:45:20 -0500647
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700648 public Builder toBuilder(Context context) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800649 WindowBounds bounds = new WindowBounds(
650 widthPx, heightPx, availableWidthPx, availableHeightPx, rotationHint);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700651 bounds.bounds.offsetTo(windowX, windowY);
Alex Chaue0227552022-04-06 19:44:43 +0100652 bounds.insets.set(mInsets);
Sunny Goyal65190ae2022-08-02 14:16:26 -0700653
654 SparseArray<DotRenderer> dotRendererCache = new SparseArray<>();
655 dotRendererCache.put(iconSizePx, mDotRendererWorkSpace);
656 dotRendererCache.put(allAppsIconSizePx, mDotRendererAllApps);
657
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700658 return new Builder(context, inv, mInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700659 .setWindowBounds(bounds)
660 .setUseTwoPanels(isTwoPanels)
Alex Chau6ed408f2022-03-04 12:58:05 +0000661 .setMultiWindowMode(isMultiWindowMode)
Sunny Goyal65190ae2022-08-02 14:16:26 -0700662 .setDotRendererCache(dotRendererCache)
Alex Chau6ed408f2022-03-04 12:58:05 +0000663 .setGestureMode(isGestureMode);
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800664 }
665
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700666 public DeviceProfile copy(Context context) {
667 return toBuilder(context).build();
668 }
669
670 /**
671 * TODO: Move this to the builder as part of setMultiWindowMode
672 */
Sunny Goyalb46703d2020-05-27 17:52:03 -0700673 public DeviceProfile getMultiWindowProfile(Context context, WindowBounds windowBounds) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700674 DeviceProfile profile = toBuilder(context)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700675 .setWindowBounds(windowBounds)
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700676 .setMultiWindowMode(true)
677 .build();
Jon Miranda93e1f042016-11-11 14:13:04 -0800678
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800679 // We use these scales to measure and layout the widgets using their full invariant profile
680 // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
681 float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
682 float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
Sihua Mae04aa202022-07-18 12:43:56 -0700683 if (appWidgetScaleX != 1 || appWidgetScaleY != 1) {
684 final PointF p = new PointF(appWidgetScaleX, appWidgetScaleY);
685 profile = profile.toBuilder(context)
686 .setViewScaleProvider(i -> p)
687 .build();
688 }
689
690 profile.hideWorkspaceLabelsIfNotEnoughSpace();
Jon Miranda7ae64ff2016-11-21 16:18:46 -0800691
Jon Miranda93e1f042016-11-11 14:13:04 -0800692 return profile;
Jon Mirandacc42c5b2016-10-27 17:15:27 -0700693 }
694
Adam Cohen63f1ec02014-08-12 09:23:13 -0700695 /**
Jon Miranda941375e2021-03-31 13:10:45 -0400696 * Checks if there is enough space for labels on the workspace.
697 * If there is not, labels on the Workspace are hidden.
Jon Miranda1091e532017-07-21 13:31:50 -0700698 * It is important to call this method after the All Apps variables have been set.
699 */
Jon Miranda941375e2021-03-31 13:10:45 -0400700 private void hideWorkspaceLabelsIfNotEnoughSpace() {
701 float iconTextHeight = Utilities.calculateTextHeight(iconTextSizePx);
702 float workspaceCellPaddingY = getCellSize().y - iconSizePx - iconDrawablePaddingPx
703 - iconTextHeight;
704
705 // We want enough space so that the text is closer to its corresponding icon.
706 if (workspaceCellPaddingY < iconTextHeight) {
707 iconTextSizePx = 0;
708 iconDrawablePaddingPx = 0;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700709 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR);
Jon Miranda941375e2021-03-31 13:10:45 -0400710 autoResizeAllAppsCells();
711 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700712 }
Jon Miranda1091e532017-07-21 13:31:50 -0700713
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700714 /**
715 * Re-computes the all-apps cell size to be independent of workspace
716 */
717 public void autoResizeAllAppsCells() {
Jon Miranda941375e2021-03-31 13:10:45 -0400718 int textHeight = Utilities.calculateTextHeight(allAppsIconTextSizePx);
719 int topBottomPadding = textHeight;
Jon Miranda1091e532017-07-21 13:31:50 -0700720 allAppsCellHeightPx = allAppsIconSizePx + allAppsIconDrawablePaddingPx
Jon Miranda941375e2021-03-31 13:10:45 -0400721 + textHeight + (topBottomPadding * 2);
Jon Miranda1091e532017-07-21 13:31:50 -0700722 }
723
Thales Limab7ef5692022-03-10 10:32:36 +0000724 private void updateAllAppsContainerWidth(Resources res) {
Pat Manning08610ca2022-04-05 21:03:16 +0100725 int cellLayoutHorizontalPadding =
726 (cellLayoutPaddingPx.left + cellLayoutPaddingPx.right) / 2;
Alex Chau27b39b92022-01-14 18:02:18 +0000727 if (isTablet) {
Thales Limab7ef5692022-03-10 10:32:36 +0000728 allAppsLeftRightPadding =
Pat Manning08610ca2022-04-05 21:03:16 +0100729 res.getDimensionPixelSize(R.dimen.all_apps_bottom_sheet_horizontal_padding);
730
sfufa@google.comde013292021-09-21 18:22:44 -0700731 int usedWidth = (allAppsCellWidthPx * numShownAllAppsColumns)
Thales Limab7ef5692022-03-10 10:32:36 +0000732 + (allAppsBorderSpacePx.x * (numShownAllAppsColumns - 1))
Alex Chau27b39b92022-01-14 18:02:18 +0000733 + allAppsLeftRightPadding * 2;
734 allAppsLeftRightMargin = Math.max(1, (availableWidthPx - usedWidth) / 2);
sfufa@google.comde013292021-09-21 18:22:44 -0700735 } else {
736 allAppsLeftRightPadding =
Pat Manning08610ca2022-04-05 21:03:16 +0100737 desiredWorkspaceHorizontalMarginPx + cellLayoutHorizontalPadding;
sfufa@google.comde013292021-09-21 18:22:44 -0700738 }
739 }
740
Jon Miranda228877d2021-02-09 11:05:00 -0500741 /**
742 * Returns the amount of extra (or unused) vertical space.
743 */
744 private int updateAvailableDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700745 updateIconSize(1f, res);
Winson Chung59a488a2013-12-10 12:32:14 -0800746
Pat Manning08610ca2022-04-05 21:03:16 +0100747 updateWorkspacePadding();
Jon Mirandae126d722021-02-25 10:45:20 -0500748
749 // Check to see if the icons fit within the available height.
Pat Manninga2e14992022-04-22 11:29:17 +0100750 float usedHeight = getCellLayoutHeightSpecification();
Pat Manning25d53342022-05-10 09:58:49 +0000751 final int maxHeight = getCellLayoutHeight();
Jon Miranda228877d2021-02-09 11:05:00 -0500752 float extraHeight = Math.max(0, maxHeight - usedHeight);
Jon Mirandae126d722021-02-25 10:45:20 -0500753 float scaleY = maxHeight / usedHeight;
754 boolean shouldScale = scaleY < 1f;
755
756 float scaleX = 1f;
757 if (isScalableGrid) {
758 // We scale to fit the cellWidth and cellHeight in the available space.
759 // The benefit of scalable grids is that we can get consistent aspect ratios between
760 // devices.
Pat Manninga2e14992022-04-22 11:29:17 +0100761 float usedWidth =
762 getCellLayoutWidthSpecification() + (desiredWorkspaceHorizontalMarginPx * 2);
Jon Mirandae126d722021-02-25 10:45:20 -0500763 // We do not subtract padding here, as we also scale the workspace padding if needed.
764 scaleX = availableWidthPx / usedWidth;
765 shouldScale = true;
Winson Chungb3800242013-10-24 11:01:54 -0700766 }
Jon Mirandae126d722021-02-25 10:45:20 -0500767
768 if (shouldScale) {
769 float scale = Math.min(scaleX, scaleY);
770 updateIconSize(scale, res);
Pat Manninga2e14992022-04-22 11:29:17 +0100771 extraHeight = Math.max(0, maxHeight - getCellLayoutHeightSpecification());
Jon Mirandae126d722021-02-25 10:45:20 -0500772 }
773
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700774 updateAvailableFolderCellDimensions(res);
Jon Miranda228877d2021-02-09 11:05:00 -0500775 return Math.round(extraHeight);
Winson Chungb3800242013-10-24 11:01:54 -0700776 }
777
Pat Manninga2e14992022-04-22 11:29:17 +0100778 private int getCellLayoutHeightSpecification() {
Pat Manning08610ca2022-04-05 21:03:16 +0100779 return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacePx.y * (inv.numRows - 1))
780 + cellLayoutPaddingPx.top + cellLayoutPaddingPx.bottom;
781 }
782
Pat Manninga2e14992022-04-22 11:29:17 +0100783 private int getCellLayoutWidthSpecification() {
Pat Manning25d53342022-05-10 09:58:49 +0000784 int numColumns = getPanelCount() * inv.numColumns;
Pat Manning08610ca2022-04-05 21:03:16 +0100785 return (cellWidthPx * numColumns) + (cellLayoutBorderSpacePx.x * (numColumns - 1))
786 + cellLayoutPaddingPx.left + cellLayoutPaddingPx.right;
Jon Mirandae126d722021-02-25 10:45:20 -0500787 }
788
Jon Miranda6f7e9702019-09-16 14:44:14 -0700789 /**
790 * Updating the iconSize affects many aspects of the launcher layout, such as: iconSizePx,
791 * iconTextSizePx, iconDrawablePaddingPx, cellWidth/Height, allApps* variants,
792 * hotseat sizes, workspaceSpringLoadedShrinkFactor, folderIconSizePx, and folderIconOffsetYPx.
793 */
Tony Wickham7ba547c2021-02-02 17:12:08 -0800794 public void updateIconSize(float scale, Resources res) {
Jon Mirandaab3c6812021-06-28 15:42:28 -0700795 // Icon scale should never exceed 1, otherwise pixellation may occur.
796 iconScale = Math.min(1f, scale);
797 cellScaleToFit = scale;
798
Jon Miranda18751b62017-07-31 17:25:27 -0700799 // Workspace
Sunny Goyal07b69292018-01-08 14:19:34 -0800800 final boolean isVerticalLayout = isVerticalBarLayout();
Thales Lima83bedbf2021-10-05 17:47:39 +0100801 float invIconSizeDp = inv.iconSize[mTypeIndex];
802 float invIconTextSizeSp = inv.iconTextSize[mTypeIndex];
Andras Kloczl30fe9152021-08-05 18:02:29 +0200803
Jon Mirandaab3c6812021-06-28 15:42:28 -0700804 iconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, iconScale));
Jon Mirandaab3c6812021-06-28 15:42:28 -0700805 iconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * iconScale);
806 iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * iconScale);
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700807
Thales Lima080d8902022-03-28 15:30:29 +0100808 cellLayoutBorderSpacePx = getCellLayoutBorderSpace(inv, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500809
810 if (isScalableGrid) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100811 cellWidthPx = pxFromDp(inv.minCellSize[mTypeIndex].x, mMetrics, scale);
812 cellHeightPx = pxFromDp(inv.minCellSize[mTypeIndex].y, mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500813 int cellContentHeight = iconSizePx + iconDrawablePaddingPx
814 + Utilities.calculateTextHeight(iconTextSizePx);
815 cellYPaddingPx = Math.max(0, cellHeightPx - cellContentHeight) / 2;
Thales Limad90faab2021-09-21 17:18:47 +0100816 desiredWorkspaceHorizontalMarginPx =
817 (int) (desiredWorkspaceHorizontalMarginOriginalPx * scale);
Jon Miranda228877d2021-02-09 11:05:00 -0500818 } else {
Jon Mirandae126d722021-02-25 10:45:20 -0500819 cellWidthPx = iconSizePx + iconDrawablePaddingPx;
Jon Miranda92c1b5d2021-07-20 13:57:16 -0700820 cellHeightPx = (int) Math.ceil(iconSizePx * ICON_OVERLAP_FACTOR)
821 + iconDrawablePaddingPx
Jon Miranda228877d2021-02-09 11:05:00 -0500822 + Utilities.calculateTextHeight(iconTextSizePx);
823 int cellPaddingY = (getCellSize().y - cellHeightPx) / 2;
824 if (iconDrawablePaddingPx > cellPaddingY && !isVerticalLayout
825 && !isMultiWindowMode) {
826 // Ensures that the label is closer to its corresponding icon. This is not an issue
827 // with vertical bar layout or multi-window mode since the issue is handled
828 // separately with their calls to {@link #adjustToHideWorkspaceLabels}.
829 cellHeightPx -= (iconDrawablePaddingPx - cellPaddingY);
830 iconDrawablePaddingPx = cellPaddingY;
831 }
Jon Miranda846455e2017-10-02 14:58:52 -0700832 }
Jon Miranda18751b62017-07-31 17:25:27 -0700833
Sunny Goyalae190ff2020-04-14 00:19:01 +0000834 // All apps
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500835 updateAllAppsIconSize(scale, res);
Winson Chungb3800242013-10-24 11:01:54 -0700836
Thales Limab8c05952022-05-23 16:58:38 +0100837 updateHotseatSizes(iconSizePx);
Winson Chungb3800242013-10-24 11:01:54 -0700838
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700839 // Folder icon
Jon Miranda591e3602018-03-28 11:37:00 -0700840 folderIconSizePx = IconNormalizer.getNormalizedCircleSize(iconSizePx);
841 folderIconOffsetYPx = (iconSizePx - folderIconSizePx) / 2;
Winson Chung0f785722015-04-08 10:27:49 -0700842 }
843
Thales Lima425f6822022-05-10 13:44:58 -0300844 /**
845 * Hotseat width spans a certain number of columns on scalable grids.
846 * This method calculates the space between the icons to achieve that width.
847 */
848 private int calculateHotseatBorderSpace() {
849 if (!isScalableGrid) return 0;
Thales Lima9938c2f2022-07-25 14:38:16 +0100850 int columns = inv.hotseatColumnSpan[mTypeIndex];
851 float hotseatWidthPx = getIconToIconWidthForColumns(columns);
852 float hotseatIconsTotalPx = iconSizePx * numShownHotseatIcons;
853 return (int) (hotseatWidthPx - hotseatIconsTotalPx) / (numShownHotseatIcons - 1);
Thales Lima425f6822022-05-10 13:44:58 -0300854 }
855
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500856
857 /**
858 * Updates the iconSize for allApps* variants.
859 */
Brian Isganitisa9a78112022-05-23 18:11:47 -0700860 private void updateAllAppsIconSize(float scale, Resources res) {
Thales Lima080d8902022-03-28 15:30:29 +0100861 allAppsBorderSpacePx = new Point(
862 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].x, mMetrics, scale),
863 pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].y, mMetrics, scale));
Brandon Dayauon07ca8842022-04-27 10:52:28 -0700864 // AllApps cells don't have real space between cells,
865 // so we add the border space to the cell height
866 allAppsCellHeightPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].y, mMetrics, scale)
867 + allAppsBorderSpacePx.y;
868 // but width is just the cell,
869 // the border is added in #updateAllAppsContainerWidth
Thales Lima080d8902022-03-28 15:30:29 +0100870 if (isScalableGrid) {
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500871 allAppsIconSizePx =
Brandon Dayauon969f6a82022-05-19 09:46:47 -0700872 pxFromDp(inv.allAppsIconSize[mTypeIndex], mMetrics, scale);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500873 allAppsIconTextSizePx =
Brandon Dayauon969f6a82022-05-19 09:46:47 -0700874 pxFromSp(inv.allAppsIconTextSize[mTypeIndex], mMetrics, scale);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500875 allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx;
Brandon Dayauon7a8a9ed2022-06-06 14:32:22 -0700876 allAppsCellWidthPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].x, mMetrics, scale);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500877 } else {
Brandon Dayauon07ca8842022-04-27 10:52:28 -0700878 float invIconSizeDp = inv.allAppsIconSize[mTypeIndex];
879 float invIconTextSizeSp = inv.allAppsIconTextSize[mTypeIndex];
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500880 allAppsIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
881 allAppsIconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * scale);
Brandon Dayauon07ca8842022-04-27 10:52:28 -0700882 allAppsIconDrawablePaddingPx =
883 res.getDimensionPixelSize(R.dimen.all_apps_icon_drawable_padding);
Brandon Dayauon7a8a9ed2022-06-06 14:32:22 -0700884 allAppsCellWidthPx = allAppsIconSizePx + (2 * allAppsIconDrawablePaddingPx);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500885 }
886
Thales Limab7ef5692022-03-10 10:32:36 +0000887 updateAllAppsContainerWidth(res);
Brian Isganitis25b2ac82022-01-28 16:25:13 -0500888 if (isVerticalBarLayout()) {
889 hideWorkspaceLabelsIfNotEnoughSpace();
890 }
891 }
892
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700893 private void updateAvailableFolderCellDimensions(Resources res) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700894 updateFolderCellSize(1f, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700895
Jon Mirandae126d722021-02-25 10:45:20 -0500896 final int folderBottomPanelSize = res.getDimensionPixelSize(R.dimen.folder_label_height);
897
Jon Mirandac1b23992016-12-13 08:57:36 -0800898 // Don't let the folder get too close to the edges of the screen.
Jon Miranda1786df32018-09-25 13:05:23 -0700899 int folderMargin = edgeMarginPx * 2;
Sunny Goyal07b69292018-01-08 14:19:34 -0800900 Point totalWorkspacePadding = getTotalWorkspacePadding();
Jon Mirandac1b23992016-12-13 08:57:36 -0800901
902 // Check if the icons fit within the available height.
Jon Miranda228877d2021-02-09 11:05:00 -0500903 float contentUsedHeight = folderCellHeightPx * inv.numFolderRows
Thales Lima78d00ad2021-09-30 11:29:06 +0100904 + ((inv.numFolderRows - 1) * folderCellLayoutBorderSpacePx.y);
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800905 int contentMaxHeight = availableHeightPx - totalWorkspacePadding.y - folderBottomPanelSize
Jon Mirandae126d722021-02-25 10:45:20 -0500906 - folderMargin - folderContentPaddingTop;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800907 float scaleY = contentMaxHeight / contentUsedHeight;
Jon Mirandac1b23992016-12-13 08:57:36 -0800908
909 // Check if the icons fit within the available width.
Jon Miranda228877d2021-02-09 11:05:00 -0500910 float contentUsedWidth = folderCellWidthPx * inv.numFolderColumns
Thales Lima78d00ad2021-09-30 11:29:06 +0100911 + ((inv.numFolderColumns - 1) * folderCellLayoutBorderSpacePx.x);
Jon Mirandae126d722021-02-25 10:45:20 -0500912 int contentMaxWidth = availableWidthPx - totalWorkspacePadding.x - folderMargin
913 - folderContentPaddingLeftRight * 2;
Samuel Fufa1c8d90a2019-11-12 17:54:58 -0800914 float scaleX = contentMaxWidth / contentUsedWidth;
Jon Mirandac1b23992016-12-13 08:57:36 -0800915
916 float scale = Math.min(scaleX, scaleY);
917 if (scale < 1f) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700918 updateFolderCellSize(scale, res);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700919 }
920 }
921
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700922 private void updateFolderCellSize(float scale, Resources res) {
Thales Lima83bedbf2021-10-05 17:47:39 +0100923 float invIconSizeDp = isVerticalBarLayout()
Thales Lima12d0eff2022-03-25 17:06:11 +0000924 ? inv.iconSize[INDEX_LANDSCAPE]
925 : inv.iconSize[INDEX_DEFAULT];
Sunny Goyal35c7b192021-04-20 16:51:10 -0700926 folderChildIconSizePx = Math.max(1, pxFromDp(invIconSizeDp, mMetrics, scale));
Thales Lima83bedbf2021-10-05 17:47:39 +0100927 folderChildTextSizePx =
Thales Lima12d0eff2022-03-25 17:06:11 +0000928 pxFromSp(inv.iconTextSize[INDEX_DEFAULT], mMetrics, scale);
Jon Mirandae126d722021-02-25 10:45:20 -0500929 folderLabelTextSizePx = (int) (folderChildTextSizePx * folderLabelTextScale);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700930
931 int textHeight = Utilities.calculateTextHeight(folderChildTextSizePx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700932
Jon Miranda823da222021-03-23 08:08:45 -0400933 if (isScalableGrid) {
Thales Limaa08a4432022-08-09 09:55:17 +0100934 folderCellWidthPx = pxFromDp(inv.folderCellSize.x, mMetrics, scale);
935 folderCellHeightPx = pxFromDp(inv.folderCellSize.y, mMetrics, scale);
Jon Mirandaf33f5b32021-07-22 17:15:31 -0700936
Thales Limaa08a4432022-08-09 09:55:17 +0100937 folderCellLayoutBorderSpacePx = new Point(
938 pxFromDp(inv.folderBorderSpaces.x, mMetrics, scale),
939 pxFromDp(inv.folderBorderSpaces.y, mMetrics, scale));
940 folderContentPaddingLeftRight = folderCellLayoutBorderSpacePx.x;
941 folderContentPaddingTop = pxFromDp(inv.folderTopPadding, mMetrics, scale);
Jon Miranda823da222021-03-23 08:08:45 -0400942 } else {
943 int cellPaddingX = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_x_padding)
944 * scale);
945 int cellPaddingY = (int) (res.getDimensionPixelSize(R.dimen.folder_cell_y_padding)
946 * scale);
947
948 folderCellWidthPx = folderChildIconSizePx + 2 * cellPaddingX;
949 folderCellHeightPx = folderChildIconSizePx + 2 * cellPaddingY + textHeight;
950 }
951
Jonathan Miranda9ad87462017-07-26 17:41:02 +0000952 folderChildDrawablePaddingPx = Math.max(0,
953 (folderCellHeightPx - folderChildIconSizePx - textHeight) / 3);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700954 }
955
Winson1f064272016-07-18 17:18:02 -0700956 public void updateInsets(Rect insets) {
957 mInsets.set(insets);
958 }
959
Sunny Goyalae6e3182019-04-30 12:04:37 -0700960 /**
961 * The current device insets. This is generally same as the insets being dispatched to
962 * {@link Insettable} elements, but can differ if the element is using a different profile.
963 */
Sunny Goyal1a52ef52018-01-11 10:15:03 -0800964 public Rect getInsets() {
965 return mInsets;
966 }
967
Sunny Goyal756cd262015-08-20 12:33:21 -0700968 public Point getCellSize() {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700969 return getCellSize(null);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700970 }
971
Sunny Goyal19ff7282021-04-22 10:12:54 -0700972 public Point getCellSize(Point result) {
973 if (result == null) {
974 result = new Point();
975 }
Thales Limad1df5fc2021-09-03 18:37:19 +0100976
Pat Manning25d53342022-05-10 09:58:49 +0000977 int shortcutAndWidgetContainerWidth =
978 getCellLayoutWidth() - (cellLayoutPaddingPx.left + cellLayoutPaddingPx.right);
979 result.x = calculateCellWidth(shortcutAndWidgetContainerWidth, cellLayoutBorderSpacePx.x,
980 inv.numColumns);
981 int shortcutAndWidgetContainerHeight =
982 getCellLayoutHeight() - (cellLayoutPaddingPx.top + cellLayoutPaddingPx.bottom);
983 result.y = calculateCellHeight(shortcutAndWidgetContainerHeight, cellLayoutBorderSpacePx.y,
984 inv.numRows);
Sunny Goyal3e8a04b2022-05-09 19:31:45 +0000985 return result;
Pat Manningb45d6c42022-05-03 14:32:06 +0100986 }
987
988 /**
Thales Lima1e8b45f2022-08-25 11:50:59 -0400989 * Returns the left and right space on the cell, which is the cell width - icon size
990 */
991 public int getCellHorizontalSpace() {
992 return getCellSize().x - iconSizePx;
993 }
994
995 /**
Pat Manning25d53342022-05-10 09:58:49 +0000996 * Gets the number of panels within the workspace.
997 */
998 public int getPanelCount() {
999 return isTwoPanels ? 2 : 1;
1000 }
1001
1002 /**
Pat Manningde25c0d2022-04-05 19:11:26 +01001003 * Gets the space in px from the bottom of last item in the vertical-bar hotseat to the
1004 * bottom of the screen.
1005 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001006 private int getVerticalHotseatLastItemBottomOffset(Context context) {
1007 Rect hotseatBarPadding = getHotseatLayoutPadding(context);
Pat Manningde25c0d2022-04-05 19:11:26 +01001008 int cellHeight = calculateCellHeight(
Pat Manning5f74bfd2022-07-20 12:08:54 +01001009 heightPx - hotseatBarPadding.top - hotseatBarPadding.bottom, hotseatBorderSpace,
Pat Manningde25c0d2022-04-05 19:11:26 +01001010 numShownHotseatIcons);
Pat Manningde25c0d2022-04-05 19:11:26 +01001011 int extraIconEndSpacing = (cellHeight - iconSizePx) / 2;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001012 return extraIconEndSpacing + hotseatBarPadding.bottom;
Pat Manningde25c0d2022-04-05 19:11:26 +01001013 }
1014
1015 /**
1016 * Gets the scaled top of the workspace in px for the spring-loaded edit state.
1017 */
Pat Manning25d53342022-05-10 09:58:49 +00001018 public float getCellLayoutSpringLoadShrunkTop() {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001019 return mInsets.top + dropTargetBarTopMarginPx + dropTargetBarSizePx
Pat Manningde25c0d2022-04-05 19:11:26 +01001020 + dropTargetBarBottomMarginPx;
Pat Manningde25c0d2022-04-05 19:11:26 +01001021 }
1022
1023 /**
1024 * Gets the scaled bottom of the workspace in px for the spring-loaded edit state.
1025 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001026 public float getCellLayoutSpringLoadShrunkBottom(Context context) {
Pat Manningde25c0d2022-04-05 19:11:26 +01001027 int topOfHotseat = hotseatBarSizePx + springLoadedHotseatBarTopMarginPx;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001028 return heightPx - (isVerticalBarLayout()
1029 ? getVerticalHotseatLastItemBottomOffset(context) : topOfHotseat);
Pat Manningde25c0d2022-04-05 19:11:26 +01001030 }
1031
Pat Manningea5c1d22022-04-14 10:58:16 +01001032 /**
Pat Manninga2e14992022-04-22 11:29:17 +01001033 * Gets the scale of the workspace for the spring-loaded edit state.
1034 */
Pat Manning5f74bfd2022-07-20 12:08:54 +01001035 public float getWorkspaceSpringLoadScale(Context context) {
1036 float scale =
1037 (getCellLayoutSpringLoadShrunkBottom(context) - getCellLayoutSpringLoadShrunkTop())
1038 / getCellLayoutHeight();
Pat Manninga2e14992022-04-22 11:29:17 +01001039 scale = Math.min(scale, 1f);
1040
Pat Manninge63dd252022-08-02 16:15:29 +01001041 // Reduce scale if next pages would not be visible after scaling the workspace.
Pat Manning25d53342022-05-10 09:58:49 +00001042 int workspaceWidth = availableWidthPx;
Pat Manninga2e14992022-04-22 11:29:17 +01001043 float scaledWorkspaceWidth = workspaceWidth * scale;
Alex Chau906d8822022-05-23 10:42:25 +01001044 float maxAvailableWidth = workspaceWidth - (2 * workspaceSpringLoadedMinNextPageVisiblePx);
Pat Manninga2e14992022-04-22 11:29:17 +01001045 if (scaledWorkspaceWidth > maxAvailableWidth) {
1046 scale *= maxAvailableWidth / scaledWorkspaceWidth;
1047 }
1048 return scale;
1049 }
1050
Pat Manning25d53342022-05-10 09:58:49 +00001051 /**
1052 * Gets the width of a single Cell Layout, aka a single panel within a Workspace.
1053 *
1054 * <p>This is the width of a Workspace, less its horizontal padding. Note that two-panel
1055 * layouts have two Cell Layouts per workspace.
1056 */
1057 public int getCellLayoutWidth() {
1058 return (availableWidthPx - getTotalWorkspacePadding().x) / getPanelCount();
Alex Chau60953332021-11-24 12:41:07 +00001059 }
1060
Pat Manning25d53342022-05-10 09:58:49 +00001061 /**
1062 * Gets the height of a single Cell Layout, aka a single panel within a Workspace.
1063 *
1064 * <p>This is the height of a Workspace, less its vertical padding.
1065 */
1066 public int getCellLayoutHeight() {
1067 return availableHeightPx - getTotalWorkspacePadding().y;
Pat Manning08610ca2022-04-05 21:03:16 +01001068 }
1069
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001070 public Point getTotalWorkspacePadding() {
Sunny Goyal07b69292018-01-08 14:19:34 -08001071 return new Point(workspacePadding.left + workspacePadding.right,
1072 workspacePadding.top + workspacePadding.bottom);
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001073 }
1074
1075 /**
Sunny Goyal07b69292018-01-08 14:19:34 -08001076 * Updates {@link #workspacePadding} as a result of any internal value change to reflect the
1077 * new workspace padding
Sunny Goyal6c2975e2016-07-06 09:47:56 -07001078 */
Sunny Goyal07b69292018-01-08 14:19:34 -08001079 private void updateWorkspacePadding() {
1080 Rect padding = workspacePadding;
Tony Wickham3a3517f2015-10-06 11:27:04 -07001081 if (isVerticalBarLayout()) {
Sunny Goyal228153d2018-01-04 15:35:22 -08001082 padding.top = 0;
1083 padding.bottom = edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001084 if (isSeascape()) {
Tony Wickham1eeffbe2018-06-12 15:01:15 -07001085 padding.left = hotseatBarSizePx;
Tony Wickham5edf9e22020-03-27 20:06:52 -07001086 padding.right = hotseatBarSidePaddingStartPx;
Winson1f064272016-07-18 17:18:02 -07001087 } else {
Tony Wickham5edf9e22020-03-27 20:06:52 -07001088 padding.left = hotseatBarSidePaddingStartPx;
Tony Wickham1eeffbe2018-06-12 15:01:15 -07001089 padding.right = hotseatBarSizePx;
Winson1f064272016-07-18 17:18:02 -07001090 }
Winson Chungb3800242013-10-24 11:01:54 -07001091 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001092 // Pad the bottom of the workspace with hotseat bar
1093 // and leave a bit of space in case a widget go all the way down
1094 int paddingBottom = hotseatBarSizePx + workspaceBottomPadding
1095 + workspacePageIndicatorHeight - mWorkspacePageIndicatorOverlapWorkspace
1096 - mInsets.bottom;
Pat Manning08610ca2022-04-05 21:03:16 +01001097 int paddingTop = workspaceTopPadding + (isScalableGrid ? 0 : edgeMarginPx);
1098 int paddingSide = desiredWorkspaceHorizontalMarginPx;
Andras Kloczl8e57cce2021-02-11 23:51:19 +01001099
Pat Manning08610ca2022-04-05 21:03:16 +01001100 padding.set(paddingSide, paddingTop, paddingSide, paddingBottom);
Winson Chungb3800242013-10-24 11:01:54 -07001101 }
Pat Manning08610ca2022-04-05 21:03:16 +01001102 insetPadding(workspacePadding, cellLayoutPaddingPx);
1103 }
1104
1105 private void insetPadding(Rect paddings, Rect insets) {
1106 insets.left = Math.min(insets.left, paddings.left);
1107 paddings.left -= insets.left;
1108
1109 insets.top = Math.min(insets.top, paddings.top);
1110 paddings.top -= insets.top;
1111
1112 insets.right = Math.min(insets.right, paddings.right);
1113 paddings.right -= insets.right;
1114
1115 insets.bottom = Math.min(insets.bottom, paddings.bottom);
1116 paddings.bottom -= insets.bottom;
Winson Chungb3800242013-10-24 11:01:54 -07001117 }
1118
Sunny Goyal57b22792021-05-25 14:35:01 -07001119 /**
1120 * Returns the padding for hotseat view
1121 */
1122 public Rect getHotseatLayoutPadding(Context context) {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001123 Rect hotseatBarPadding = new Rect();
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001124 if (isVerticalBarLayout()) {
Sihua Ma38bb3b02022-03-21 22:20:14 -07001125 // The hotseat icons will be placed in the middle of the hotseat cells.
1126 // Changing the hotseatCellHeightPx is not affecting hotseat icon positions
1127 // in vertical bar layout.
1128 // Workspace icons are moved up by a small factor. The variable diffOverlapFactor
1129 // is set to account for that difference.
1130 float diffOverlapFactor = iconSizePx * (ICON_OVERLAP_FACTOR - 1) / 2;
Pat Manning08610ca2022-04-05 21:03:16 +01001131 int paddingTop = Math.max((int) (mInsets.top + cellLayoutPaddingPx.top
1132 - diffOverlapFactor), 0);
1133 int paddingBottom = Math.max((int) (mInsets.bottom + cellLayoutPaddingPx.bottom
Sihua Ma38bb3b02022-03-21 22:20:14 -07001134 + diffOverlapFactor), 0);
1135
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001136 if (isSeascape()) {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001137 hotseatBarPadding.set(mInsets.left + hotseatBarSidePaddingStartPx, paddingTop,
Pat Manning08610ca2022-04-05 21:03:16 +01001138 hotseatBarSidePaddingEndPx, paddingBottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001139 } else {
Pat Manning5f74bfd2022-07-20 12:08:54 +01001140 hotseatBarPadding.set(hotseatBarSidePaddingEndPx, paddingTop,
Pat Manning08610ca2022-04-05 21:03:16 +01001141 mInsets.right + hotseatBarSidePaddingStartPx, paddingBottom);
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001142 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001143 } else if (isTaskbarPresent) {
Alex Chau51da2192022-05-20 13:32:10 +01001144 // Center the QSB vertically with hotseat
Thales Limab8c05952022-05-23 16:58:38 +01001145 int hotseatBarBottomPadding = getHotseatBarBottomPadding();
1146 int hotseatBarTopPadding =
1147 hotseatBarSizePx - hotseatBarBottomPadding - hotseatCellHeightPx;
Alex Chau51da2192022-05-20 13:32:10 +01001148
Thales Lima612230d2022-03-31 18:04:37 +01001149 // Push icons to the side
Thales Lima9938c2f2022-07-25 14:38:16 +01001150 int requiredWidth = getHotseatRequiredWidth();
Pat Manning26f70f72022-07-07 13:50:39 +01001151 int hotseatWidth = Math.min(requiredWidth, availableWidthPx - hotseatBarEndOffset);
Thales Lima612230d2022-03-31 18:04:37 +01001152 int sideSpacing = (availableWidthPx - hotseatWidth) / 2;
Thales Lima612230d2022-03-31 18:04:37 +01001153
Pat Manning5f74bfd2022-07-20 12:08:54 +01001154 hotseatBarPadding.set(sideSpacing, hotseatBarTopPadding, sideSpacing,
Thales Limab8c05952022-05-23 16:58:38 +01001155 hotseatBarBottomPadding);
Alex Chau51da2192022-05-20 13:32:10 +01001156
1157 boolean isRtl = Utilities.isRtl(context.getResources());
Thales Lima612230d2022-03-31 18:04:37 +01001158 if (isRtl) {
Thales Lima9938c2f2022-07-25 14:38:16 +01001159 hotseatBarPadding.right += getAdditionalQsbSpace();
Thales Lima612230d2022-03-31 18:04:37 +01001160 } else {
Thales Lima9938c2f2022-07-25 14:38:16 +01001161 hotseatBarPadding.left += getAdditionalQsbSpace();
Thales Lima612230d2022-03-31 18:04:37 +01001162 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001163
Pat Manning26f70f72022-07-07 13:50:39 +01001164 if (hotseatBarEndOffset > sideSpacing) {
Thales Lima612230d2022-03-31 18:04:37 +01001165 int diff = isRtl
Pat Manning26f70f72022-07-07 13:50:39 +01001166 ? sideSpacing - hotseatBarEndOffset
1167 : hotseatBarEndOffset - sideSpacing;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001168 hotseatBarPadding.left -= diff;
1169 hotseatBarPadding.right += diff;
Sunny Goyal57b22792021-05-25 14:35:01 -07001170 }
Thales Lima425f6822022-05-10 13:44:58 -03001171 } else if (isScalableGrid) {
Alex Chau206ede92022-08-01 17:46:12 +01001172 int sideSpacing = (availableWidthPx - hotseatQsbWidth) / 2;
Pat Manning5f74bfd2022-07-20 12:08:54 +01001173 hotseatBarPadding.set(sideSpacing,
Thales Limab8c05952022-05-23 16:58:38 +01001174 0,
Thales Lima425f6822022-05-10 13:44:58 -03001175 sideSpacing,
Thales Limab8c05952022-05-23 16:58:38 +01001176 getHotseatBarBottomPadding());
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001177 } else {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001178 // We want the edges of the hotseat to line up with the edges of the workspace, but the
1179 // icons in the hotseat are a different size, and so don't line up perfectly. To account
1180 // for this, we pad the left and right of the hotseat with half of the difference of a
1181 // workspace cell vs a hotseat cell.
1182 float workspaceCellWidth = (float) widthPx / inv.numColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001183 float hotseatCellWidth = (float) widthPx / numShownHotseatIcons;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001184 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
Pat Manning5f74bfd2022-07-20 12:08:54 +01001185 hotseatBarPadding.set(
Thales Limab8c05952022-05-23 16:58:38 +01001186 hotseatAdjustment + workspacePadding.left + cellLayoutPaddingPx.left
1187 + mInsets.left,
1188 0,
Pat Manning08610ca2022-04-05 21:03:16 +01001189 hotseatAdjustment + workspacePadding.right + cellLayoutPaddingPx.right
Sunny Goyal786940a2020-06-02 02:31:31 -07001190 + mInsets.right,
Thales Limab8c05952022-05-23 16:58:38 +01001191 getHotseatBarBottomPadding());
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001192 }
Pat Manning5f74bfd2022-07-20 12:08:54 +01001193 return hotseatBarPadding;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -07001194 }
1195
Thales Lima9938c2f2022-07-25 14:38:16 +01001196 private int getAdditionalQsbSpace() {
1197 return isQsbInline ? hotseatQsbWidth + hotseatBorderSpace : 0;
1198 }
1199
1200 /**
1201 * Calculate how much space the hotseat needs to be shown completely
1202 */
1203 private int getHotseatRequiredWidth() {
1204 int additionalQsbSpace = getAdditionalQsbSpace();
1205 return iconSizePx * numShownHotseatIcons
1206 + hotseatBorderSpace * (numShownHotseatIcons - 1)
1207 + additionalQsbSpace;
1208 }
1209
Winsonfadbe8f2016-07-22 16:35:37 -07001210 /**
Sunny Goyal57b22792021-05-25 14:35:01 -07001211 * Returns the number of pixels the QSB is translated from the bottom of the screen.
1212 */
1213 public int getQsbOffsetY() {
Thales Limaa1012902022-01-13 17:58:42 +00001214 if (isQsbInline) {
Thales Limab8c05952022-05-23 16:58:38 +01001215 return getHotseatBarBottomPadding() - ((hotseatQsbHeight - hotseatCellHeightPx) / 2);
1216 } else if (isTaskbarPresent) { // QSB on top
1217 return hotseatBarSizePx - hotseatQsbHeight + hotseatQsbShadowHeight;
Jonathan Miranda8f16a772021-07-23 23:10:37 +00001218 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001219 return hotseatBarBottomSpacePx - hotseatQsbShadowHeight;
Jonathan Miranda7a273e22021-06-24 21:29:10 +00001220 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001221 }
1222
Thales Limab8c05952022-05-23 16:58:38 +01001223 /**
1224 * Returns the number of pixels the hotseat is translated from the bottom of the screen.
1225 */
1226 private int getHotseatBarBottomPadding() {
1227 if (isTaskbarPresent) { // QSB on top or inline
1228 return hotseatBarBottomSpacePx - (Math.abs(hotseatCellHeightPx - iconSizePx) / 2);
Thales Limaa1012902022-01-13 17:58:42 +00001229 } else {
Thales Limab8c05952022-05-23 16:58:38 +01001230 return hotseatBarSizePx - hotseatCellHeightPx;
Thales Limaa1012902022-01-13 17:58:42 +00001231 }
Sunny Goyal57b22792021-05-25 14:35:01 -07001232 }
1233
1234 /**
Alex Chau51da2192022-05-20 13:32:10 +01001235 * Returns the number of pixels the taskbar is translated from the bottom of the screen.
1236 */
1237 public int getTaskbarOffsetY() {
1238 int taskbarIconBottomSpace = (taskbarSize - iconSizePx) / 2;
1239 int launcherIconBottomSpace =
1240 Math.min((hotseatCellHeightPx - iconSizePx) / 2, gridVisualizationPaddingY);
Thales Limab8c05952022-05-23 16:58:38 +01001241 return getHotseatBarBottomPadding() + launcherIconBottomSpace - taskbarIconBottomSpace;
Alex Chau51da2192022-05-20 13:32:10 +01001242 }
1243
1244 /**
Alex Chaua2fc7642022-04-21 14:20:23 +01001245 * Returns the number of pixels required below OverviewActions excluding insets.
1246 */
1247 public int getOverviewActionsClaimedSpaceBelow() {
1248 if (isTaskbarPresent && !isGestureMode) {
1249 // Align vertically to where nav buttons are.
Thales Limab8c05952022-05-23 16:58:38 +01001250 return ((taskbarSize - overviewActionsHeight) / 2) + getTaskbarOffsetY();
Alex Chaua2fc7642022-04-21 14:20:23 +01001251 }
1252
Alex Chau1fe83fe2022-05-09 19:21:13 +01001253 return isTaskbarPresent ? stashedTaskbarSize : mInsets.bottom;
Alex Chaua2fc7642022-04-21 14:20:23 +01001254 }
1255
1256 /** Gets the space that the overview actions will take, including bottom margin. */
1257 public int getOverviewActionsClaimedSpace() {
1258 return overviewActionsTopMarginPx + overviewActionsHeight
1259 + getOverviewActionsClaimedSpaceBelow();
1260 }
1261
1262 /**
Sihua Mae04aa202022-07-18 12:43:56 -07001263 * Takes the View and return the scales of width and height depending on the DeviceProfile
1264 * specifications
1265 *
1266 * @param itemInfo The tag of the widget view
1267 * @return A PointF instance with the x set to be the scale of width, and y being the scale of
1268 * height
1269 */
1270 @NonNull
1271 public PointF getAppWidgetScale(@Nullable final ItemInfo itemInfo) {
1272 return mViewScaleProvider.getScaleFromItemInfo(itemInfo);
1273 }
1274
1275 /**
Winsonfadbe8f2016-07-22 16:35:37 -07001276 * @return the bounds for which the open folders should be contained within
1277 */
1278 public Rect getAbsoluteOpenFolderBounds() {
1279 if (isVerticalBarLayout()) {
1280 // Folders should only appear right of the drop target bar and left of the hotseat
1281 return new Rect(mInsets.left + dropTargetBarSizePx + edgeMarginPx,
1282 mInsets.top,
Jon Miranda18751b62017-07-31 17:25:27 -07001283 mInsets.left + availableWidthPx - hotseatBarSizePx - edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -07001284 mInsets.top + availableHeightPx);
1285 } else {
1286 // Folders should only appear below the drop target bar and above the hotseat
Tony Wickhamae72b462021-03-10 16:18:40 -08001287 int hotseatTop = isTaskbarPresent ? taskbarSize : hotseatBarSizePx;
Tony Wickhamb4b7e202018-01-12 17:39:24 -08001288 return new Rect(mInsets.left + edgeMarginPx,
Winsonfadbe8f2016-07-22 16:35:37 -07001289 mInsets.top + dropTargetBarSizePx + edgeMarginPx,
Tony Wickhamb4b7e202018-01-12 17:39:24 -08001290 mInsets.left + availableWidthPx - edgeMarginPx,
Tony Wickhamae72b462021-03-10 16:18:40 -08001291 mInsets.top + availableHeightPx - hotseatTop
Tony Wickham5edf9e22020-03-27 20:06:52 -07001292 - workspacePageIndicatorHeight - edgeMarginPx);
Winsonfadbe8f2016-07-22 16:35:37 -07001293 }
1294 }
1295
Jon Miranda228877d2021-02-09 11:05:00 -05001296 public static int calculateCellWidth(int width, int borderSpacing, int countX) {
1297 return (width - ((countX - 1) * borderSpacing)) / countX;
Winson Chungb3800242013-10-24 11:01:54 -07001298 }
Pierre Barbier de Reuille578deba2021-09-24 13:47:44 +01001299
Jon Miranda228877d2021-02-09 11:05:00 -05001300 public static int calculateCellHeight(int height, int borderSpacing, int countY) {
1301 return (height - ((countY - 1) * borderSpacing)) / countY;
Winson Chungb3800242013-10-24 11:01:54 -07001302 }
1303
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -07001304 /**
Tony Wickham55616cd2015-09-23 14:55:17 -07001305 * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
1306 * When {@code false}, either device is in portrait mode or the device is in landscape mode and
1307 * the hotseat is on the bottom row.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -07001308 */
Tony Wickhamab946a12015-09-16 15:38:16 -07001309 public boolean isVerticalBarLayout() {
Winson Chungb3800242013-10-24 11:01:54 -07001310 return isLandscape && transposeLayoutWithOrientation;
1311 }
1312
Sunny Goyal59d086c2018-05-07 17:31:40 -07001313 /**
1314 * Updates orientation information and returns true if it has changed from the previous value.
1315 */
Winson Chung13c1c2c2019-09-06 11:46:19 -07001316 public boolean updateIsSeascape(Context context) {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001317 if (isVerticalBarLayout()) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001318 boolean isSeascape = DisplayController.INSTANCE.get(context)
1319 .getInfo().rotation == Surface.ROTATION_270;
Sunny Goyal59d086c2018-05-07 17:31:40 -07001320 if (mIsSeascape != isSeascape) {
1321 mIsSeascape = isSeascape;
Pat Manning08610ca2022-04-05 21:03:16 +01001322 // Hotseat changing sides requires updating workspace left/right paddings
1323 updateWorkspacePadding();
Sunny Goyal59d086c2018-05-07 17:31:40 -07001324 return true;
1325 }
1326 }
1327 return false;
1328 }
1329
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001330 public boolean isSeascape() {
Sunny Goyal59d086c2018-05-07 17:31:40 -07001331 return isVerticalBarLayout() && mIsSeascape;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -08001332 }
1333
Sunny Goyal07b69292018-01-08 14:19:34 -08001334 public boolean shouldFadeAdjacentWorkspaceScreens() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001335 return isVerticalBarLayout();
Winson Chungb3800242013-10-24 11:01:54 -07001336 }
1337
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001338 public int getCellContentHeight(@ContainerType int containerType) {
Sunny Goyalc13403c2016-11-18 23:44:48 -08001339 switch (containerType) {
1340 case CellLayout.WORKSPACE:
1341 return cellHeightPx;
1342 case CellLayout.FOLDER:
1343 return folderCellHeightPx;
1344 case CellLayout.HOTSEAT:
Jon Miranda92c1b5d2021-07-20 13:57:16 -07001345 // The hotseat is the only container where the cell height is going to be
1346 // different from the content within that cell.
1347 return iconSizePx;
Sunny Goyalc13403c2016-11-18 23:44:48 -08001348 default:
1349 // ??
1350 return 0;
1351 }
1352 }
Sunny Goyal13178ac2016-04-04 16:35:22 -07001353
Jon Miranda58561d42021-03-17 10:51:54 -04001354 private String pxToDpStr(String name, float value) {
Sunny Goyal35c7b192021-04-20 16:51:10 -07001355 return "\t" + name + ": " + value + "px (" + dpiFromPx(value, mMetrics.densityDpi) + "dp)";
Jon Miranda58561d42021-03-17 10:51:54 -04001356 }
1357
Alex Chaue818bcb2022-09-02 17:27:11 +01001358 private String dpPointFToString(String name, PointF value) {
1359 return String.format(Locale.ENGLISH, "\t%s: PointF(%.1f, %.1f)dp", name, value.x, value.y);
1360 }
1361
Pat Manning5f74bfd2022-07-20 12:08:54 +01001362 /** Dumps various DeviceProfile variables to the specified writer. */
1363 public void dump(Context context, String prefix, PrintWriter writer) {
Jon Miranda58561d42021-03-17 10:51:54 -04001364 writer.println(prefix + "DeviceProfile:");
Sunny Goyal35c7b192021-04-20 16:51:10 -07001365 writer.println(prefix + "\t1 dp = " + mMetrics.density + " px");
Jon Miranda58561d42021-03-17 10:51:54 -04001366
1367 writer.println(prefix + "\tisTablet:" + isTablet);
Jon Miranda58561d42021-03-17 10:51:54 -04001368 writer.println(prefix + "\tisPhone:" + isPhone);
1369 writer.println(prefix + "\ttransposeLayoutWithOrientation:"
1370 + transposeLayoutWithOrientation);
Alex Chau6ed408f2022-03-04 12:58:05 +00001371 writer.println(prefix + "\tisGestureMode:" + isGestureMode);
Jon Miranda58561d42021-03-17 10:51:54 -04001372
1373 writer.println(prefix + "\tisLandscape:" + isLandscape);
1374 writer.println(prefix + "\tisMultiWindowMode:" + isMultiWindowMode);
Sunny Goyal19ff7282021-04-22 10:12:54 -07001375 writer.println(prefix + "\tisTwoPanels:" + isTwoPanels);
Jon Miranda58561d42021-03-17 10:51:54 -04001376
1377 writer.println(prefix + pxToDpStr("windowX", windowX));
1378 writer.println(prefix + pxToDpStr("windowY", windowY));
1379 writer.println(prefix + pxToDpStr("widthPx", widthPx));
1380 writer.println(prefix + pxToDpStr("heightPx", heightPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001381 writer.println(prefix + pxToDpStr("availableWidthPx", availableWidthPx));
1382 writer.println(prefix + pxToDpStr("availableHeightPx", availableHeightPx));
Alex Chaue0227552022-04-06 19:44:43 +01001383 writer.println(prefix + pxToDpStr("mInsets.left", mInsets.left));
1384 writer.println(prefix + pxToDpStr("mInsets.top", mInsets.top));
1385 writer.println(prefix + pxToDpStr("mInsets.right", mInsets.right));
1386 writer.println(prefix + pxToDpStr("mInsets.bottom", mInsets.bottom));
Jon Miranda58561d42021-03-17 10:51:54 -04001387
1388 writer.println(prefix + "\taspectRatio:" + aspectRatio);
1389
1390 writer.println(prefix + "\tisScalableGrid:" + isScalableGrid);
1391
Thales Lima83bedbf2021-10-05 17:47:39 +01001392 writer.println(prefix + "\tinv.numRows: " + inv.numRows);
Alex Chau9fee3fd2021-12-01 18:43:10 +00001393 writer.println(prefix + "\tinv.numColumns: " + inv.numColumns);
1394 writer.println(prefix + "\tinv.numSearchContainerColumns: "
1395 + inv.numSearchContainerColumns);
Jon Miranda58561d42021-03-17 10:51:54 -04001396
Alex Chaue818bcb2022-09-02 17:27:11 +01001397 writer.println(prefix + dpPointFToString("minCellSize", inv.minCellSize[mTypeIndex]));
Jon Mirandaab3c6812021-06-28 15:42:28 -07001398
Jon Miranda58561d42021-03-17 10:51:54 -04001399 writer.println(prefix + pxToDpStr("cellWidthPx", cellWidthPx));
1400 writer.println(prefix + pxToDpStr("cellHeightPx", cellHeightPx));
1401
1402 writer.println(prefix + pxToDpStr("getCellSize().x", getCellSize().x));
1403 writer.println(prefix + pxToDpStr("getCellSize().y", getCellSize().y));
1404
Thales Lima83bedbf2021-10-05 17:47:39 +01001405 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Horizontal",
1406 cellLayoutBorderSpacePx.x));
1407 writer.println(prefix + pxToDpStr("cellLayoutBorderSpacePx Vertical",
1408 cellLayoutBorderSpacePx.y));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001409 writer.println(
1410 prefix + pxToDpStr("cellLayoutPaddingPx.left", cellLayoutPaddingPx.left));
1411 writer.println(
1412 prefix + pxToDpStr("cellLayoutPaddingPx.top", cellLayoutPaddingPx.top));
1413 writer.println(
1414 prefix + pxToDpStr("cellLayoutPaddingPx.right", cellLayoutPaddingPx.right));
Pat Manning08610ca2022-04-05 21:03:16 +01001415 writer.println(
1416 prefix + pxToDpStr("cellLayoutPaddingPx.bottom", cellLayoutPaddingPx.bottom));
Thales Lima83bedbf2021-10-05 17:47:39 +01001417
Jon Miranda58561d42021-03-17 10:51:54 -04001418 writer.println(prefix + pxToDpStr("iconSizePx", iconSizePx));
1419 writer.println(prefix + pxToDpStr("iconTextSizePx", iconTextSizePx));
1420 writer.println(prefix + pxToDpStr("iconDrawablePaddingPx", iconDrawablePaddingPx));
1421
1422 writer.println(prefix + pxToDpStr("folderCellWidthPx", folderCellWidthPx));
1423 writer.println(prefix + pxToDpStr("folderCellHeightPx", folderCellHeightPx));
1424 writer.println(prefix + pxToDpStr("folderChildIconSizePx", folderChildIconSizePx));
1425 writer.println(prefix + pxToDpStr("folderChildTextSizePx", folderChildTextSizePx));
1426 writer.println(prefix + pxToDpStr("folderChildDrawablePaddingPx",
1427 folderChildDrawablePaddingPx));
Thales Lima78d00ad2021-09-30 11:29:06 +01001428 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Horizontal",
1429 folderCellLayoutBorderSpacePx.x));
1430 writer.println(prefix + pxToDpStr("folderCellLayoutBorderSpacePx Vertical",
1431 folderCellLayoutBorderSpacePx.y));
Thales Limaa08a4432022-08-09 09:55:17 +01001432 writer.println(prefix + pxToDpStr("folderContentPaddingLeftRight",
1433 folderContentPaddingLeftRight));
1434 writer.println(prefix + pxToDpStr("folderTopPadding", folderContentPaddingTop));
Jon Miranda58561d42021-03-17 10:51:54 -04001435
Alex Chaue0227552022-04-06 19:44:43 +01001436 writer.println(prefix + pxToDpStr("bottomSheetTopPadding", bottomSheetTopPadding));
1437
1438 writer.println(prefix + pxToDpStr("allAppsShiftRange", allAppsShiftRange));
1439 writer.println(prefix + pxToDpStr("allAppsTopPadding", allAppsTopPadding));
Jon Miranda58561d42021-03-17 10:51:54 -04001440 writer.println(prefix + pxToDpStr("allAppsIconSizePx", allAppsIconSizePx));
1441 writer.println(prefix + pxToDpStr("allAppsIconTextSizePx", allAppsIconTextSizePx));
1442 writer.println(prefix + pxToDpStr("allAppsIconDrawablePaddingPx",
1443 allAppsIconDrawablePaddingPx));
1444 writer.println(prefix + pxToDpStr("allAppsCellHeightPx", allAppsCellHeightPx));
Alex Chau27b39b92022-01-14 18:02:18 +00001445 writer.println(prefix + pxToDpStr("allAppsCellWidthPx", allAppsCellWidthPx));
Pat Manning26f70f72022-07-07 13:50:39 +01001446 writer.println(prefix + pxToDpStr("allAppsBorderSpacePxX", allAppsBorderSpacePx.x));
1447 writer.println(prefix + pxToDpStr("allAppsBorderSpacePxY", allAppsBorderSpacePx.y));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001448 writer.println(prefix + "\tnumShownAllAppsColumns: " + numShownAllAppsColumns);
Alex Chau62572c02022-07-25 18:36:37 +00001449 writer.println(prefix + pxToDpStr("allAppsLeftRightPadding", allAppsLeftRightPadding));
Alex Chau27b39b92022-01-14 18:02:18 +00001450 writer.println(prefix + pxToDpStr("allAppsLeftRightMargin", allAppsLeftRightMargin));
Jon Miranda58561d42021-03-17 10:51:54 -04001451
1452 writer.println(prefix + pxToDpStr("hotseatBarSizePx", hotseatBarSizePx));
Thales Lima425f6822022-05-10 13:44:58 -03001453 writer.println(prefix + "\tinv.hotseatColumnSpan: " + inv.hotseatColumnSpan[mTypeIndex]);
Jon Miranda58561d42021-03-17 10:51:54 -04001454 writer.println(prefix + pxToDpStr("hotseatCellHeightPx", hotseatCellHeightPx));
Alex Chau62572c02022-07-25 18:36:37 +00001455 writer.println(prefix + pxToDpStr("hotseatBarBottomSpacePx", hotseatBarBottomSpacePx));
Jon Miranda58561d42021-03-17 10:51:54 -04001456 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingStartPx",
1457 hotseatBarSidePaddingStartPx));
1458 writer.println(prefix + pxToDpStr("hotseatBarSidePaddingEndPx",
1459 hotseatBarSidePaddingEndPx));
Pat Manning26f70f72022-07-07 13:50:39 +01001460 writer.println(prefix + pxToDpStr("hotseatBarEndOffset", hotseatBarEndOffset));
Thales Limab8c05952022-05-23 16:58:38 +01001461 writer.println(prefix + pxToDpStr("hotseatQsbSpace", hotseatQsbSpace));
1462 writer.println(prefix + pxToDpStr("hotseatQsbHeight", hotseatQsbHeight));
Pat Manningde25c0d2022-04-05 19:11:26 +01001463 writer.println(prefix + pxToDpStr("springLoadedHotseatBarTopMarginPx",
1464 springLoadedHotseatBarTopMarginPx));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001465 Rect hotseatLayoutPadding = getHotseatLayoutPadding(context);
1466 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).top",
1467 hotseatLayoutPadding.top));
1468 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).bottom",
1469 hotseatLayoutPadding.bottom));
1470 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).left",
1471 hotseatLayoutPadding.left));
1472 writer.println(prefix + pxToDpStr("getHotseatLayoutPadding(context).right",
1473 hotseatLayoutPadding.right));
Sunny Goyal19ff7282021-04-22 10:12:54 -07001474 writer.println(prefix + "\tnumShownHotseatIcons: " + numShownHotseatIcons);
Thales Lima116b51a2022-02-03 16:19:47 +00001475 writer.println(prefix + pxToDpStr("hotseatBorderSpace", hotseatBorderSpace));
Thales Limaa1012902022-01-13 17:58:42 +00001476 writer.println(prefix + "\tisQsbInline: " + isQsbInline);
Alex Chau206ede92022-08-01 17:46:12 +01001477 writer.println(prefix + pxToDpStr("hotseatQsbWidth", hotseatQsbWidth));
Jon Miranda58561d42021-03-17 10:51:54 -04001478
1479 writer.println(prefix + "\tisTaskbarPresent:" + isTaskbarPresent);
Tony Wickham635e1802021-07-22 14:28:04 -10001480 writer.println(prefix + "\tisTaskbarPresentInApps:" + isTaskbarPresentInApps);
Jon Miranda58561d42021-03-17 10:51:54 -04001481 writer.println(prefix + pxToDpStr("taskbarSize", taskbarSize));
Jon Miranda58561d42021-03-17 10:51:54 -04001482
Thales Lima83bedbf2021-10-05 17:47:39 +01001483 writer.println(prefix + pxToDpStr("desiredWorkspaceHorizontalMarginPx",
1484 desiredWorkspaceHorizontalMarginPx));
Jon Miranda58561d42021-03-17 10:51:54 -04001485 writer.println(prefix + pxToDpStr("workspacePadding.left", workspacePadding.left));
1486 writer.println(prefix + pxToDpStr("workspacePadding.top", workspacePadding.top));
1487 writer.println(prefix + pxToDpStr("workspacePadding.right", workspacePadding.right));
Alex Chau62572c02022-07-25 18:36:37 +00001488 writer.println(prefix + pxToDpStr("workspacePadding.bottom", workspacePadding.bottom));
Jon Miranda58561d42021-03-17 10:51:54 -04001489
Jon Mirandaab3c6812021-06-28 15:42:28 -07001490 writer.println(prefix + pxToDpStr("iconScale", iconScale));
1491 writer.println(prefix + pxToDpStr("cellScaleToFit ", cellScaleToFit));
Jon Miranda58561d42021-03-17 10:51:54 -04001492 writer.println(prefix + pxToDpStr("extraSpace", extraSpace));
Alex Chau62572c02022-07-25 18:36:37 +00001493 writer.println(prefix + pxToDpStr("unscaled extraSpace", extraSpace / iconScale));
Jon Mirandac9e69fa2021-03-22 17:13:34 -04001494
1495 if (inv.devicePaddings != null) {
1496 int unscaledExtraSpace = (int) (extraSpace / iconScale);
1497 writer.println(prefix + pxToDpStr("maxEmptySpace",
1498 inv.devicePaddings.getDevicePadding(unscaledExtraSpace).getMaxEmptySpacePx()));
1499 }
Jon Miranda58561d42021-03-17 10:51:54 -04001500 writer.println(prefix + pxToDpStr("workspaceTopPadding", workspaceTopPadding));
1501 writer.println(prefix + pxToDpStr("workspaceBottomPadding", workspaceBottomPadding));
Alex Chau635b3ab2022-01-26 16:49:10 +00001502
1503 writer.println(prefix + pxToDpStr("overviewTaskMarginPx", overviewTaskMarginPx));
Alex Chau635b3ab2022-01-26 16:49:10 +00001504 writer.println(prefix + pxToDpStr("overviewTaskIconSizePx", overviewTaskIconSizePx));
1505 writer.println(prefix + pxToDpStr("overviewTaskIconDrawableSizePx",
1506 overviewTaskIconDrawableSizePx));
1507 writer.println(prefix + pxToDpStr("overviewTaskIconDrawableSizeGridPx",
1508 overviewTaskIconDrawableSizeGridPx));
1509 writer.println(prefix + pxToDpStr("overviewTaskThumbnailTopMarginPx",
1510 overviewTaskThumbnailTopMarginPx));
Alex Chaua2fc7642022-04-21 14:20:23 +01001511 writer.println(prefix + pxToDpStr("overviewActionsTopMarginPx",
1512 overviewActionsTopMarginPx));
1513 writer.println(prefix + pxToDpStr("overviewActionsHeight",
1514 overviewActionsHeight));
Alex Chau635b3ab2022-01-26 16:49:10 +00001515 writer.println(prefix + pxToDpStr("overviewActionsButtonSpacing",
1516 overviewActionsButtonSpacing));
1517 writer.println(prefix + pxToDpStr("overviewPageSpacing", overviewPageSpacing));
1518 writer.println(prefix + pxToDpStr("overviewRowSpacing", overviewRowSpacing));
1519 writer.println(prefix + pxToDpStr("overviewGridSideMargin", overviewGridSideMargin));
Pat Manningde25c0d2022-04-05 19:11:26 +01001520
Alex Chau62572c02022-07-25 18:36:37 +00001521 writer.println(prefix + pxToDpStr("dropTargetBarTopMarginPx", dropTargetBarTopMarginPx));
Pat Manningde25c0d2022-04-05 19:11:26 +01001522 writer.println(prefix + pxToDpStr("dropTargetBarSizePx", dropTargetBarSizePx));
Alex Chau62572c02022-07-25 18:36:37 +00001523 writer.println(
1524 prefix + pxToDpStr("dropTargetBarBottomMarginPx", dropTargetBarBottomMarginPx));
Pat Manningde25c0d2022-04-05 19:11:26 +01001525
Pat Manning5f74bfd2022-07-20 12:08:54 +01001526 writer.println(prefix + pxToDpStr("getCellLayoutSpringLoadShrunkTop()",
1527 getCellLayoutSpringLoadShrunkTop()));
1528 writer.println(prefix + pxToDpStr("getCellLayoutSpringLoadShrunkBottom()",
1529 getCellLayoutSpringLoadShrunkBottom(context)));
Alex Chau906d8822022-05-23 10:42:25 +01001530 writer.println(prefix + pxToDpStr("workspaceSpringLoadedMinNextPageVisiblePx",
1531 workspaceSpringLoadedMinNextPageVisiblePx));
Pat Manning5f74bfd2022-07-20 12:08:54 +01001532 writer.println(prefix + pxToDpStr("getWorkspaceSpringLoadScale()",
1533 getWorkspaceSpringLoadScale(context)));
Thales Limab8c05952022-05-23 16:58:38 +01001534 writer.println(prefix + pxToDpStr("getCellLayoutHeight()", getCellLayoutHeight()));
1535 writer.println(prefix + pxToDpStr("getCellLayoutWidth()", getCellLayoutWidth()));
Jon Miranda58561d42021-03-17 10:51:54 -04001536 }
1537
Alex Chaua6907dc2022-03-18 15:24:07 +00001538 private static Context getContext(Context c, Info info, int orientation, WindowBounds bounds) {
Sunny Goyal1890f672020-04-30 12:28:00 -07001539 Configuration config = new Configuration(c.getResources().getConfiguration());
1540 config.orientation = orientation;
Thales Lima425f6822022-05-10 13:44:58 -03001541 config.densityDpi = info.getDensityDpi();
Alex Chaua6907dc2022-03-18 15:24:07 +00001542 config.smallestScreenWidthDp = (int) info.smallestSizeDp(bounds);
Sunny Goyal1890f672020-04-30 12:28:00 -07001543 return c.createConfigurationContext(config);
Jon Mirandab28c4fc2017-06-20 10:58:36 -07001544 }
Sunny Goyalfde55052018-02-01 14:46:13 -08001545
1546 /**
1547 * Callback when a component changes the DeviceProfile associated with it, as a result of
1548 * configuration change
1549 */
1550 public interface OnDeviceProfileChangeListener {
1551
1552 /**
1553 * Called when the device profile is reassigned. Note that for layout and measurements, it
1554 * is sufficient to listen for inset changes. Use this callback when you need to perform
1555 * a one time operation.
1556 */
1557 void onDeviceProfileChanged(DeviceProfile dp);
1558 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001559
Brian Isganitis099945b2022-01-31 18:15:00 -05001560 /** Allows registering listeners for {@link DeviceProfile} changes. */
1561 public interface DeviceProfileListenable {
1562
1563 /** The current device profile. */
1564 DeviceProfile getDeviceProfile();
1565
1566 /** Registered {@link OnDeviceProfileChangeListener} instances. */
1567 List<OnDeviceProfileChangeListener> getOnDeviceProfileChangeListeners();
1568
1569 /** Notifies listeners of a {@link DeviceProfile} change. */
1570 default void dispatchDeviceProfileChanged() {
1571 DeviceProfile deviceProfile = getDeviceProfile();
1572 List<OnDeviceProfileChangeListener> listeners = getOnDeviceProfileChangeListeners();
1573 for (int i = listeners.size() - 1; i >= 0; i--) {
1574 listeners.get(i).onDeviceProfileChanged(deviceProfile);
1575 }
1576 }
1577
1578 /** Register listener for {@link DeviceProfile} changes. */
1579 default void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
1580 getOnDeviceProfileChangeListeners().add(listener);
1581 }
1582
1583 /** Unregister listener for {@link DeviceProfile} changes. */
1584 default void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
1585 getOnDeviceProfileChangeListeners().remove(listener);
1586 }
1587 }
1588
Sihua Mae04aa202022-07-18 12:43:56 -07001589 /**
1590 * Handler that deals with ItemInfo of the views for the DeviceProfile
1591 */
1592 @FunctionalInterface
1593 public interface ViewScaleProvider {
1594 /**
1595 * Get the scales from the view
1596 *
1597 * @param itemInfo The tag of the widget view
1598 * @return PointF instance containing the scale information, or null if using the default
1599 * app widget scale of this device profile.
1600 */
1601 @NonNull
1602 PointF getScaleFromItemInfo(@Nullable ItemInfo itemInfo);
1603 }
1604
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001605 public static class Builder {
1606 private Context mContext;
1607 private InvariantDeviceProfile mInv;
Sunny Goyalfd58da62020-08-11 12:06:49 -07001608 private Info mInfo;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001609
Sunny Goyal19ff7282021-04-22 10:12:54 -07001610 private WindowBounds mWindowBounds;
1611 private boolean mUseTwoPanels;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001612
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001613 private boolean mIsMultiWindowMode = false;
Sunny Goyal19ff7282021-04-22 10:12:54 -07001614 private Boolean mTransposeLayoutWithOrientation;
Alex Chau6ed408f2022-03-04 12:58:05 +00001615 private Boolean mIsGestureMode;
Sihua Mae04aa202022-07-18 12:43:56 -07001616 private ViewScaleProvider mViewScaleProvider = null;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001617
Sunny Goyal65190ae2022-08-02 14:16:26 -07001618 private SparseArray<DotRenderer> mDotRendererCache;
1619
Sunny Goyalfd58da62020-08-11 12:06:49 -07001620 public Builder(Context context, InvariantDeviceProfile inv, Info info) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001621 mContext = context;
1622 mInv = inv;
1623 mInfo = info;
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001624 }
1625
1626 public Builder setMultiWindowMode(boolean isMultiWindowMode) {
1627 mIsMultiWindowMode = isMultiWindowMode;
1628 return this;
1629 }
1630
Sunny Goyal19ff7282021-04-22 10:12:54 -07001631 public Builder setUseTwoPanels(boolean useTwoPanels) {
1632 mUseTwoPanels = useTwoPanels;
1633 return this;
1634 }
1635
Sunny Goyal65190ae2022-08-02 14:16:26 -07001636 public Builder setDotRendererCache(SparseArray<DotRenderer> dotRendererCache) {
1637 mDotRendererCache = dotRendererCache;
1638 return this;
1639 }
Sunny Goyal19ff7282021-04-22 10:12:54 -07001640
1641 public Builder setWindowBounds(WindowBounds bounds) {
1642 mWindowBounds = bounds;
Sunny Goyal0addbf02020-04-28 14:17:35 -07001643 return this;
1644 }
1645
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001646 public Builder setTransposeLayoutWithOrientation(boolean transposeLayoutWithOrientation) {
1647 mTransposeLayoutWithOrientation = transposeLayoutWithOrientation;
1648 return this;
1649 }
1650
Alex Chau6ed408f2022-03-04 12:58:05 +00001651 public Builder setGestureMode(boolean isGestureMode) {
1652 mIsGestureMode = isGestureMode;
1653 return this;
1654 }
1655
Sihua Mae04aa202022-07-18 12:43:56 -07001656 /**
1657 * Set the viewScaleProvider for the builder
1658 *
1659 * @param viewScaleProvider The viewScaleProvider to be set for the
1660 * DeviceProfile
1661 * @return This builder
1662 */
1663 @NonNull
1664 public Builder setViewScaleProvider(@Nullable ViewScaleProvider viewScaleProvider) {
1665 mViewScaleProvider = viewScaleProvider;
1666 return this;
1667 }
1668
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001669 public DeviceProfile build() {
Sunny Goyal19ff7282021-04-22 10:12:54 -07001670 if (mWindowBounds == null) {
1671 throw new IllegalArgumentException("Window bounds not set");
1672 }
1673 if (mTransposeLayoutWithOrientation == null) {
1674 mTransposeLayoutWithOrientation = !mInfo.isTablet(mWindowBounds);
1675 }
Alex Chau6ed408f2022-03-04 12:58:05 +00001676 if (mIsGestureMode == null) {
Alex Chaue818bcb2022-09-02 17:27:11 +01001677 mIsGestureMode = mInfo.navigationMode.hasGestures;
Alex Chau6ed408f2022-03-04 12:58:05 +00001678 }
Sunny Goyal65190ae2022-08-02 14:16:26 -07001679 if (mDotRendererCache == null) {
1680 mDotRendererCache = new SparseArray<>();
1681 }
Sihua Mae04aa202022-07-18 12:43:56 -07001682 if (mViewScaleProvider == null) {
1683 mViewScaleProvider = DEFAULT_PROVIDER;
1684 }
Sunny Goyal65190ae2022-08-02 14:16:26 -07001685 return new DeviceProfile(mContext, mInv, mInfo, mWindowBounds, mDotRendererCache,
1686 mIsMultiWindowMode, mTransposeLayoutWithOrientation, mUseTwoPanels,
Sihua Mae04aa202022-07-18 12:43:56 -07001687 mIsGestureMode, mViewScaleProvider);
Sunny Goyal0e7a3382020-04-13 17:15:05 -07001688 }
1689 }
1690
Winson Chungb3800242013-10-24 11:01:54 -07001691}