blob: 2ebedf71de9c171ef092cd0316b38473a27a019d [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
Thales Lima1de4d552021-10-13 16:13:25 +01002 * Copyright (C) 2021 The Android Open Source Project
Adam Cohen2e6da152015-05-06 11:42:25 -07003 *
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
Sunny Goyal19ff7282021-04-22 10:12:54 -070019import static com.android.launcher3.Utilities.dpiFromPx;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010020import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Sunny Goyal35c7b192021-04-20 16:51:10 -070021import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Sunny Goyal19ff7282021-04-22 10:12:54 -070022import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080023import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070024
Sunny Goyalc6205602015-05-21 20:46:33 -070025import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070026import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080029import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080030import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070031import android.content.res.TypedArray;
32import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070033import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010034import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070035import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080036import android.text.TextUtils;
37import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070038import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010039import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080040import android.util.SparseArray;
41import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070042import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080043import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070044
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070045import androidx.annotation.Nullable;
46import androidx.annotation.VisibleForTesting;
47
Sunny Goyal68031ca2021-08-02 12:23:44 -070048import com.android.launcher3.model.DeviceGridState;
Alex Chau238aaee2021-10-06 16:15:24 +010049import com.android.launcher3.provider.RestoreDbTask;
Sunny Goyalfd58da62020-08-11 12:06:49 -070050import com.android.launcher3.util.DisplayController;
51import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080052import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070053import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080054import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070055import com.android.launcher3.util.WindowBounds;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070056
Sunny Goyal819e1932016-07-07 16:43:58 -070057import org.xmlpull.v1.XmlPullParser;
58import org.xmlpull.v1.XmlPullParserException;
59
60import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070061import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070062import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080063import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070064import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070065
66public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070067
Hyunyoung Songc55a3502018-12-04 15:43:16 -080068 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070069 // We do not need any synchronization for this variable as its only written on UI thread.
70 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070071 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070072
Hyunyoung Songc55a3502018-12-04 15:43:16 -080073 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080074
Sunny Goyal53d7ee42015-05-22 12:25:45 -070075 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
76
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070077 // Constants that affects the interpolation curve between statically defined device profile
78 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080079 private static final float KNEARESTNEIGHBOR = 3;
80 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070081
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070082 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080083 private static final float WEIGHT_EFFICIENT = 100000f;
84
Thales Lima83bedbf2021-10-05 17:47:39 +010085 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
86 // constraints
87 static final int COUNT_SIZES = 5;
88 static final int INDEX_DEFAULT = 0;
89 static final int INDEX_LANDSCAPE = 1;
90 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
91 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
92 static final int INDEX_ALL_APPS = 4;
93
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070094 /**
95 * Number of icons per row and column in the workspace.
96 */
Adam Cohen2e6da152015-05-06 11:42:25 -070097 public int numRows;
98 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099
100 /**
101 * Number of icons per row and column in the folder.
102 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700103 public int numFolderRows;
104 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100105 public float[] iconSize;
106 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700107 public int iconBitmapSize;
108 public int fillResIconDpi;
Thales Lima7ec83822021-08-05 13:32:10 +0100109 public boolean isSplitDisplay;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700110
Thales Lima83bedbf2021-10-05 17:47:39 +0100111 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100112
Thales Lima83bedbf2021-10-05 17:47:39 +0100113 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100114 public float folderBorderSpace;
115
Thales Lima83bedbf2021-10-05 17:47:39 +0100116 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500117
Sunny Goyal5bc18462019-01-07 15:13:39 -0800118 private SparseArray<TypedValue> mExtraAttrs;
119
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700120 /**
121 * Number of icons inside the hotseat area.
122 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700123 protected int numShownHotseatIcons;
124
125 /**
126 * Number of icons inside the hotseat area that is stored in the database. This is greater than
127 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
128 * sizes that share the same DB.
129 */
130 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700131
Jon Miranda6f7e9702019-09-16 14:44:14 -0700132 /**
133 * Number of columns in the all apps list.
134 */
135 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700136 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700137
Jon Mirandae126d722021-02-25 10:45:20 -0500138 /**
139 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
140 */
141 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400142 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500143
Tracy Zhou7df93d22020-01-27 13:44:06 -0800144 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800145 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700146 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700147
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000148 /**
149 * An immutable list of supported profiles.
150 */
151 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700152
sfufa@google.comde013292021-09-21 18:22:44 -0700153 @Nullable
154 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500155
Sunny Goyal6f866092016-03-17 17:04:15 -0700156 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700157 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700158
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700159 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700160
Sunny Goyalf633ef52018-03-13 09:57:05 -0700161 @VisibleForTesting
sfufa@google.comde013292021-09-21 18:22:44 -0700162 public InvariantDeviceProfile() {
163 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700164
Sunny Goyalbbf01842015-10-08 07:41:15 -0700165 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700166 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700167 String gridName = getCurrentGridName(context);
168 String newGridName = initGrid(context, gridName);
169 if (!newGridName.equals(gridName)) {
170 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
171 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700172 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700173
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700174 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100175 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700176 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100177 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700178 }
179 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700180 }
181
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700182 /**
183 * This constructor should NOT have any monitors by design.
184 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800185 public InvariantDeviceProfile(Context context, String gridName) {
186 String newName = initGrid(context, gridName);
187 if (newName == null || !newName.equals(gridName)) {
188 throw new IllegalArgumentException("Unknown grid name");
189 }
190 }
191
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700192 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800193 * This constructor should NOT have any monitors by design.
194 */
195 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700196 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100197 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700198 String gridName = getCurrentGridName(context);
199
200 // Get the display info based on default display and interpolate it to existing display
201 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700202 DisplayController.INSTANCE.get(context).getInfo(),
Alex Chau238aaee2021-10-06 16:15:24 +0100203 getPredefinedDeviceProfiles(context, gridName, false, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700204
Alex Chau417bd722021-01-26 15:22:18 +0000205 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700206 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau238aaee2021-10-06 16:15:24 +0100207 myInfo, getPredefinedDeviceProfiles(context, gridName, false, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700208
209 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
210 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100211 result.iconSizes[INDEX_DEFAULT] =
212 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
213 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700214 result.iconSizes[i] = Math.min(
215 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000216 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700217
Thales Lima83bedbf2021-10-05 17:47:39 +0100218 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
219 COUNT_SIZES);
220 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
221 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500222
Sunny Goyal19ff7282021-04-22 10:12:54 -0700223 initGrid(context, myInfo, result, false);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800224 }
225
Alex Chau238aaee2021-10-06 16:15:24 +0100226 /**
227 * Reinitialize the current grid after a restore, where some grids might now be disabled.
228 */
229 public void reinitializeAfterRestore(Context context) {
230 String currentDbFile = dbFile;
231 String gridName = getCurrentGridName(context);
232 String newGridName = initGrid(context, gridName);
233 if (!newGridName.equals(gridName)) {
234 Log.d(TAG, "Restored grid is disabled : " + gridName
235 + ", migrating to: " + newGridName
236 + ", removing all other grid db files");
237 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
238 if (gridDbFile.equals(currentDbFile)) {
239 continue;
240 }
241 if (context.getDatabasePath(gridDbFile).delete()) {
242 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
243 }
244 }
245 setCurrentGrid(context, gridName);
246 }
247 }
248
Tracy Zhou42255d22020-03-13 00:38:11 -0700249 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700250 return Utilities.isGridOptionsEnabled(context)
251 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700252 }
253
Sunny Goyaleff44f32019-01-09 17:29:49 -0800254 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700255 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Pat Manning67094bf2021-05-26 12:38:13 +0000256 // Each screen has two profiles (portrait/landscape), so devices with four or more
257 // supported profiles implies two or more internal displays.
258 boolean isSplitDisplay =
259 displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700260
261 ArrayList<DisplayOption> allOptions =
Alex Chau238aaee2021-10-06 16:15:24 +0100262 getPredefinedDeviceProfiles(context, gridName, isSplitDisplay,
263 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700264 DisplayOption displayOption =
265 invDistWeightedInterpolate(displayInfo, allOptions, isSplitDisplay);
266 initGrid(context, displayInfo, displayOption, isSplitDisplay);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700267 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800268 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700269
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700270 private void initGrid(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700271 Context context, Info displayInfo, DisplayOption displayOption,
272 boolean isSplitDisplay) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700273 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700274 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000275 numRows = closestProfile.numRows;
276 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000277 dbFile = closestProfile.dbFile;
278 defaultLayoutId = closestProfile.defaultLayoutId;
279 demoModeLayoutId = closestProfile.demoModeLayoutId;
280 numFolderRows = closestProfile.numFolderRows;
281 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500282 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400283 devicePaddingId = closestProfile.devicePaddingId;
Thales Lima7ec83822021-08-05 13:32:10 +0100284 this.isSplitDisplay = isSplitDisplay;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000285
286 mExtraAttrs = closestProfile.extraAttrs;
287
Thales Lima83bedbf2021-10-05 17:47:39 +0100288 iconSize = displayOption.iconSizes;
Thales Lima6e0005a2021-10-27 15:53:41 +0100289 float maxIconSize = iconSize[0];
290 for (int i = 1; i < iconSize.length; i++) {
291 maxIconSize = Math.max(maxIconSize, iconSize[i]);
292 }
293 iconBitmapSize = ResourceUtils.pxFromDp(maxIconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000294 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
295
Thales Lima83bedbf2021-10-05 17:47:39 +0100296 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700297
Thales Lima83bedbf2021-10-05 17:47:39 +0100298 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100299
Thales Lima83bedbf2021-10-05 17:47:39 +0100300 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100301 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700302
Thales Limad90faab2021-09-21 17:18:47 +0100303 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100304
Sunny Goyal19ff7282021-04-22 10:12:54 -0700305 numShownHotseatIcons = closestProfile.numHotseatIcons;
306 numDatabaseHotseatIcons = isSplitDisplay
307 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
308
309 numAllAppsColumns = closestProfile.numAllAppsColumns;
310 numDatabaseAllAppsColumns = isSplitDisplay
311 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500312
Thales Lima83bedbf2021-10-05 17:47:39 +0100313 if (!Utilities.isGridOptionsEnabled(context)) {
314 iconSize[INDEX_ALL_APPS] = iconSize[INDEX_DEFAULT];
315 iconTextSize[INDEX_ALL_APPS] = iconTextSize[INDEX_DEFAULT];
Sunny Goyalae190ff2020-04-14 00:19:01 +0000316 }
317
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400318 if (devicePaddingId != 0) {
319 devicePaddings = new DevicePaddings(context, devicePaddingId);
320 }
321
Sunny Goyalae190ff2020-04-14 00:19:01 +0000322 // If the partner customization apk contains any grid overrides, apply them
323 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700324 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700325
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000326 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700327 defaultWallpaperSize = new Point(displayInfo.currentSize);
328 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000329 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700330 .setUseTwoPanels(isSplitDisplay)
331 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700332
Sunny Goyal19ff7282021-04-22 10:12:54 -0700333 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
334 int displayWidth = bounds.bounds.width();
335 int displayHeight = bounds.bounds.height();
336 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700337
Sunny Goyal19ff7282021-04-22 10:12:54 -0700338 // We need to ensure that there is enough extra space in the wallpaper
339 // for the intended parallax effects
340 float parallaxFactor =
341 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
342 ? 2
343 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
344 defaultWallpaperSize.x =
345 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700346 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000347 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700348
349 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
350 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700351 }
352
Sunny Goyal5bc18462019-01-07 15:13:39 -0800353 @Nullable
354 public TypedValue getAttrValue(int attr) {
355 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
356 }
357
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700358 public void addOnChangeListener(OnIDPChangeListener listener) {
359 mChangeListeners.add(listener);
360 }
361
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800362 public void removeOnChangeListener(OnIDPChangeListener listener) {
363 mChangeListeners.remove(listener);
364 }
365
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800366
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800367 public void setCurrentGrid(Context context, String gridName) {
368 Context appContext = context.getApplicationContext();
369 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700370 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800371 }
372
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700373 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700374 return new Object[]{
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700375 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
376 numDatabaseAllAppsColumns, dbFile};
377 }
378
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700379 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700380 Object[] oldState = toModelState();
381
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700382 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700383 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700384 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700385
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700386 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700387 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700388 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700389 }
390 }
391
Sunny Goyal19ff7282021-04-22 10:12:54 -0700392 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
Alex Chau238aaee2021-10-06 16:15:24 +0100393 Context context, String gridName, boolean isSplitDisplay, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800394 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100395 int xmlResource = isSplitDisplay ? R.xml.device_profiles_split : R.xml.device_profiles;
396
397 try (XmlResourceParser parser = context.getResources().getXml(xmlResource)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700398 final int depth = parser.getDepth();
399 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700400 while (((type = parser.next()) != XmlPullParser.END_TAG ||
401 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800402 if ((type == XmlPullParser.START_TAG)
403 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800404
Thales Lima7ec83822021-08-05 13:32:10 +0100405 GridOption gridOption =
406 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
Alex Chau238aaee2021-10-06 16:15:24 +0100407 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100408 final int displayDepth = parser.getDepth();
409 while (((type = parser.next()) != XmlPullParser.END_TAG
410 || parser.getDepth() > displayDepth)
411 && type != XmlPullParser.END_DOCUMENT) {
412 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
413 parser.getName())) {
414 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100415 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100416 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800417 }
418 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700419 }
420 }
sfufa@google.comde013292021-09-21 18:22:44 -0700421 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700422 throw new RuntimeException(e);
423 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800424
Sunny Goyalae190ff2020-04-14 00:19:01 +0000425 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800426 if (!TextUtils.isEmpty(gridName)) {
427 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100428 if (gridName.equals(option.grid.name)
429 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000430 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800431 }
432 }
433 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000434 if (filteredProfiles.isEmpty()) {
435 // No grid found, use the default options
436 for (DisplayOption option : profiles) {
437 if (option.canBeDefault) {
438 filteredProfiles.add(option);
439 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800440 }
441 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000442 if (filteredProfiles.isEmpty()) {
443 throw new RuntimeException("No display option with canBeDefault=true");
444 }
445 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700446 }
447
Thales Lima7ec83822021-08-05 13:32:10 +0100448 /**
449 * @return all the grid options that can be shown on the device
450 */
451 public List<GridOption> parseAllGridOptions(Context context) {
452 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100453 int xmlResource = isSplitDisplay ? R.xml.device_profiles_split : R.xml.device_profiles;
454
455 try (XmlResourceParser parser = context.getResources().getXml(xmlResource)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100456 final int depth = parser.getDepth();
457 int type;
458 while (((type = parser.next()) != XmlPullParser.END_TAG
459 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
460 if ((type == XmlPullParser.START_TAG)
461 && GridOption.TAG_NAME.equals(parser.getName())) {
462 GridOption option =
463 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
464 if (option.isEnabled) {
465 result.add(option);
466 }
467 }
468 }
469 } catch (IOException | XmlPullParserException e) {
470 Log.e(TAG, "Error parsing device profile", e);
471 return Collections.emptyList();
472 }
473 return result;
474 }
475
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700476 private int getLauncherIconDensity(int requiredSize) {
477 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700478 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700479 DisplayMetrics.DENSITY_LOW,
480 DisplayMetrics.DENSITY_MEDIUM,
481 DisplayMetrics.DENSITY_TV,
482 DisplayMetrics.DENSITY_HIGH,
483 DisplayMetrics.DENSITY_XHIGH,
484 DisplayMetrics.DENSITY_XXHIGH,
485 DisplayMetrics.DENSITY_XXXHIGH
486 };
487
488 int density = DisplayMetrics.DENSITY_XXXHIGH;
489 for (int i = densityBuckets.length - 1; i >= 0; i--) {
490 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
491 / DisplayMetrics.DENSITY_DEFAULT;
492 if (expectedSize >= requiredSize) {
493 density = densityBuckets[i];
494 }
495 }
496
497 return density;
498 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700499
Adam Cohen2e6da152015-05-06 11:42:25 -0700500 /**
501 * Apply any Partner customization grid overrides.
502 *
503 * Currently we support: all apps row / column count.
504 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700505 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
506 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700507 if (p != null) {
508 p.applyInvariantDeviceProfileOverrides(this, dm);
509 }
510 }
511
Sunny Goyal415f1732018-11-29 10:33:47 -0800512 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700513 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700514 }
515
Sunny Goyal19ff7282021-04-22 10:12:54 -0700516 private static DisplayOption invDistWeightedInterpolate(
517 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
518 int minWidthPx = Integer.MAX_VALUE;
519 int minHeightPx = Integer.MAX_VALUE;
520 for (WindowBounds bounds : displayInfo.supportedBounds) {
521 boolean isTablet = displayInfo.isTablet(bounds);
522 if (isTablet && isSplitDisplay) {
523 // For split displays, take half width per page
524 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
525 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700526
Sunny Goyal19ff7282021-04-22 10:12:54 -0700527 } else if (!isTablet && bounds.isLandscape()) {
528 // We will use transposed layout in this case
529 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
530 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
531 } else {
532 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
533 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
534 }
535 }
536
537 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
538 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700539
540 // Sort the profiles based on the closeness to the device size
541 Collections.sort(points, (a, b) ->
542 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
543 dist(width, height, b.minWidthDps, b.minHeightDps)));
544
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700545 DisplayOption closestPoint = points.get(0);
546 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700547 float weights = 0;
548
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700549 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
550 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700551 }
552
553 DisplayOption out = new DisplayOption(closestOption);
554 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700555 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700556 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
557 weights += w;
558 out.add(new DisplayOption().add(p).multiply(w));
559 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700560 out.multiply(1.0f / weights);
561
Thales Lima6e0005a2021-10-27 15:53:41 +0100562 // Since the bitmaps are persisted, ensure that all bitmap sizes are not larger than
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700563 // predefined size to avoid cache invalidation
Thales Lima6e0005a2021-10-27 15:53:41 +0100564 for (int i = INDEX_DEFAULT; i < COUNT_SIZES; i++) {
565 out.iconSizes[i] = Math.min(out.iconSizes[i], closestPoint.iconSizes[i]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700566 }
567
568 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700569 }
570
Sunny Goyal27835952017-01-13 12:15:53 -0800571 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700572 Resources res = context.getResources();
573 Configuration config = context.getResources().getConfiguration();
574
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400575 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
576 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
577 return getBestMatch(screenWidth, screenHeight);
578 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700579
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400580 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700581 DeviceProfile bestMatch = supportedProfiles.get(0);
582 float minDiff = Float.MAX_VALUE;
583
584 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400585 float diff = Math.abs(profile.widthPx - screenWidth)
586 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700587 if (diff < minDiff) {
588 minDiff = diff;
589 bestMatch = profile;
590 }
591 }
592 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800593 }
594
Sunny Goyal415f1732018-11-29 10:33:47 -0800595 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700596 float d = dist(x0, y0, x1, y1);
597 if (Float.compare(d, 0f) == 0) {
598 return Float.POSITIVE_INFINITY;
599 }
600 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
601 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700602
603 /**
604 * As a ratio of screen height, the total distance we want the parallax effect to span
605 * horizontally
606 */
607 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
608 float aspectRatio = width / (float) height;
609
610 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
611 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
612 // We will use these two data points to extrapolate how much the wallpaper parallax effect
613 // to span (ie travel) at any aspect ratio:
614
sfufa@google.comde013292021-09-21 18:22:44 -0700615 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
616 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700617 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
618 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
619
620 // To find out the desired width at different aspect ratios, we use the following two
621 // formulas, where the coefficient on x is the aspect ratio (width/height):
622 // (16/10)x + y = 1.5
623 // (10/16)x + y = 1.2
624 // We solve for x and y and end up with a final formula:
625 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700626 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
627 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700628 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
629 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
630 return x * aspectRatio + y;
631 }
632
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700633 public interface OnIDPChangeListener {
634
Sunny Goyalb47172b2021-05-03 19:59:51 -0700635 /**
636 * Called when the device provide changes
637 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700638 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700639 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800640
641
Sunny Goyaleff44f32019-01-09 17:29:49 -0800642 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800643
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800644 public static final String TAG_NAME = "grid-option";
645
Sunny Goyaleff44f32019-01-09 17:29:49 -0800646 public final String name;
647 public final int numRows;
648 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100649 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800650
651 private final int numFolderRows;
652 private final int numFolderColumns;
653
Sunny Goyal19ff7282021-04-22 10:12:54 -0700654 private final int numAllAppsColumns;
655 private final int numDatabaseAllAppsColumns;
656 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700657 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800658
Tracy Zhou7df93d22020-01-27 13:44:06 -0800659 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000660
Sunny Goyal415f1732018-11-29 10:33:47 -0800661 private final int defaultLayoutId;
662 private final int demoModeLayoutId;
663
Jon Mirandae126d722021-02-25 10:45:20 -0500664 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400665 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500666
Sunny Goyal5bc18462019-01-07 15:13:39 -0800667 private final SparseArray<TypedValue> extraAttrs;
668
Thales Lima7ec83822021-08-05 13:32:10 +0100669 public GridOption(Context context, AttributeSet attrs, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800670 TypedArray a = context.obtainStyledAttributes(
671 attrs, R.styleable.GridDisplayOption);
672 name = a.getString(R.styleable.GridDisplayOption_name);
673 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
674 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
675
Tracy Zhou7df93d22020-01-27 13:44:06 -0800676 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1e448462021-08-13 21:48:35 +0100677 defaultLayoutId = a.getResourceId(isSplitDisplay && a.hasValue(
678 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
679 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
680 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800681 demoModeLayoutId = a.getResourceId(
682 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700683
684 numAllAppsColumns = a.getInt(
685 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
686 numDatabaseAllAppsColumns = a.getInt(
687 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
688
689 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800690 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700691 numDatabaseHotseatIcons = a.getInt(
692 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
693
Sunny Goyal415f1732018-11-29 10:33:47 -0800694 numFolderRows = a.getInt(
695 R.styleable.GridDisplayOption_numFolderRows, numRows);
696 numFolderColumns = a.getInt(
697 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700698
Jon Mirandae126d722021-02-25 10:45:20 -0500699 isScalable = a.getBoolean(
700 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400701 devicePaddingId = a.getResourceId(
702 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500703
Thales Lima1de4d552021-10-13 16:13:25 +0100704 isEnabled = a.getBoolean(R.styleable.GridDisplayOption_gridEnabled, true);
Thales Lima7ec83822021-08-05 13:32:10 +0100705
Sunny Goyal415f1732018-11-29 10:33:47 -0800706 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800707 extraAttrs = Themes.createValueMap(context, attrs,
708 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800709 }
710 }
711
Sunny Goyal19ff7282021-04-22 10:12:54 -0700712 @VisibleForTesting
713 static final class DisplayOption {
714
715 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800716
Sunny Goyal415f1732018-11-29 10:33:47 -0800717 private final float minWidthDps;
718 private final float minHeightDps;
719 private final boolean canBeDefault;
720
Thales Lima83bedbf2021-10-05 17:47:39 +0100721 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100722
723 private float folderBorderSpace;
724 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100725 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100726
Thales Lima78d00ad2021-09-30 11:29:06 +0100727 private final float[] iconSizes = new float[COUNT_SIZES];
728 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800729
Thales Lima1de4d552021-10-13 16:13:25 +0100730 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800731 this.grid = grid;
732
Thales Lima1de4d552021-10-13 16:13:25 +0100733 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800734
Sunny Goyal415f1732018-11-29 10:33:47 -0800735 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
736 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700737
Thales Lima1de4d552021-10-13 16:13:25 +0100738 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800739
Thales Lima83bedbf2021-10-05 17:47:39 +0100740 float x;
741 float y;
742
743 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
744 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
745 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
746 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
747 minCellSize[INDEX_ALL_APPS] = new PointF(x, y);
748
749 x = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellWidthDps,
750 minCellSize[INDEX_DEFAULT].x);
751 y = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellHeightDps,
752 minCellSize[INDEX_DEFAULT].y);
753 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
754
755 x = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellWidthDps,
756 minCellSize[INDEX_DEFAULT].x);
757 y = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellHeightDps,
758 minCellSize[INDEX_DEFAULT].y);
759 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100760
761 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceDps, 0);
762 float twoPanelPortraitBorderSpaceDps = a.getFloat(
763 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceDps, borderSpace);
764 float twoPanelLandscapeBorderSpaceDps = a.getFloat(
765 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceDps, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100766
767 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontalDps, borderSpace);
768 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVerticalDps, borderSpace);
769 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
770 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
771
772 x = a.getFloat(
773 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceHorizontalDps,
774 twoPanelPortraitBorderSpaceDps);
775 y = a.getFloat(
776 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceVerticalDps,
777 twoPanelPortraitBorderSpaceDps);
778 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
779
780 x = a.getFloat(
781 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceHorizontalDps,
782 twoPanelLandscapeBorderSpaceDps);
783 y = a.getFloat(
784 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceVerticalDps,
785 twoPanelLandscapeBorderSpaceDps);
786 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
787
788 x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellSpacingDps,
789 borderSpace);
790 borderSpaces[INDEX_ALL_APPS] = new PointF(x, y);
791 folderBorderSpace = borderSpace;
Jon Mirandae126d722021-02-25 10:45:20 -0500792
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700793 iconSizes[INDEX_DEFAULT] =
794 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
795 iconSizes[INDEX_LANDSCAPE] =
796 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
797 iconSizes[INDEX_DEFAULT]);
798 iconSizes[INDEX_ALL_APPS] =
799 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
800 iconSizes[INDEX_DEFAULT]);
801 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
802 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconSize,
803 iconSizes[INDEX_DEFAULT]);
804 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
805 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconSize,
Thales Lima83bedbf2021-10-05 17:47:39 +0100806 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700807
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700808 textSizes[INDEX_DEFAULT] =
809 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
810 textSizes[INDEX_LANDSCAPE] =
811 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconTextSize,
812 textSizes[INDEX_DEFAULT]);
813 textSizes[INDEX_ALL_APPS] =
814 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
815 textSizes[INDEX_DEFAULT]);
816 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
817 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconTextSize,
818 textSizes[INDEX_DEFAULT]);
819 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
820 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconTextSize,
Thales Lima83bedbf2021-10-05 17:47:39 +0100821 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700822
Thales Lima83bedbf2021-10-05 17:47:39 +0100823 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
824 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
825 horizontalMargin[INDEX_LANDSCAPE] = horizontalMargin[INDEX_DEFAULT];
826 horizontalMargin[INDEX_ALL_APPS] = horizontalMargin[INDEX_DEFAULT];
827 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Limad90faab2021-09-21 17:18:47 +0100828 R.styleable.ProfileDisplayOption_twoPanelLandscapeHorizontalMargin,
Thales Lima83bedbf2021-10-05 17:47:39 +0100829 horizontalMargin[INDEX_DEFAULT]);
830 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Limad90faab2021-09-21 17:18:47 +0100831 R.styleable.ProfileDisplayOption_twoPanelPortraitHorizontalMargin,
Thales Lima83bedbf2021-10-05 17:47:39 +0100832 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +0100833
Sunny Goyal415f1732018-11-29 10:33:47 -0800834 a.recycle();
835 }
836
837 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700838 this(null);
839 }
840
841 DisplayOption(GridOption grid) {
842 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800843 minWidthDps = 0;
844 minHeightDps = 0;
845 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +0100846 for (int i = 0; i < COUNT_SIZES; i++) {
847 iconSizes[i] = 0;
848 textSizes[i] = 0;
849 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +0100850 minCellSize[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +0100851 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800852 }
853
854 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100855 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700856 iconSizes[i] *= w;
857 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +0100858 borderSpaces[i].x *= w;
859 borderSpaces[i].y *= w;
860 minCellSize[i].x *= w;
861 minCellSize[i].y *= w;
862 horizontalMargin[i] *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700863 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100864
865 folderBorderSpace *= w;
866
Sunny Goyal415f1732018-11-29 10:33:47 -0800867 return this;
868 }
869
870 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100871 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700872 iconSizes[i] += p.iconSizes[i];
873 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +0100874 borderSpaces[i].x += p.borderSpaces[i].x;
875 borderSpaces[i].y += p.borderSpaces[i].y;
876 minCellSize[i].x += p.minCellSize[i].x;
877 minCellSize[i].y += p.minCellSize[i].y;
878 horizontalMargin[i] += p.horizontalMargin[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700879 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100880
881 folderBorderSpace += p.folderBorderSpace;
882
Sunny Goyal415f1732018-11-29 10:33:47 -0800883 return this;
884 }
885 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000886}