blob: d844b877a72cb0129a65759fe6ff18a747cccc04 [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;
289 iconBitmapSize = ResourceUtils.pxFromDp(iconSize[INDEX_DEFAULT], metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000290 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
291
Thales Lima83bedbf2021-10-05 17:47:39 +0100292 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700293
Thales Lima83bedbf2021-10-05 17:47:39 +0100294 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100295
Thales Lima83bedbf2021-10-05 17:47:39 +0100296 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100297 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700298
Thales Limad90faab2021-09-21 17:18:47 +0100299 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100300
Sunny Goyal19ff7282021-04-22 10:12:54 -0700301 numShownHotseatIcons = closestProfile.numHotseatIcons;
302 numDatabaseHotseatIcons = isSplitDisplay
303 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
304
305 numAllAppsColumns = closestProfile.numAllAppsColumns;
306 numDatabaseAllAppsColumns = isSplitDisplay
307 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500308
Thales Lima83bedbf2021-10-05 17:47:39 +0100309 if (!Utilities.isGridOptionsEnabled(context)) {
310 iconSize[INDEX_ALL_APPS] = iconSize[INDEX_DEFAULT];
311 iconTextSize[INDEX_ALL_APPS] = iconTextSize[INDEX_DEFAULT];
Sunny Goyalae190ff2020-04-14 00:19:01 +0000312 }
313
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400314 if (devicePaddingId != 0) {
315 devicePaddings = new DevicePaddings(context, devicePaddingId);
316 }
317
Sunny Goyalae190ff2020-04-14 00:19:01 +0000318 // If the partner customization apk contains any grid overrides, apply them
319 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700320 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700321
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000322 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700323 defaultWallpaperSize = new Point(displayInfo.currentSize);
324 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000325 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700326 .setUseTwoPanels(isSplitDisplay)
327 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700328
Sunny Goyal19ff7282021-04-22 10:12:54 -0700329 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
330 int displayWidth = bounds.bounds.width();
331 int displayHeight = bounds.bounds.height();
332 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700333
Sunny Goyal19ff7282021-04-22 10:12:54 -0700334 // We need to ensure that there is enough extra space in the wallpaper
335 // for the intended parallax effects
336 float parallaxFactor =
337 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
338 ? 2
339 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
340 defaultWallpaperSize.x =
341 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700342 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000343 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700344
345 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
346 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700347 }
348
Sunny Goyal5bc18462019-01-07 15:13:39 -0800349 @Nullable
350 public TypedValue getAttrValue(int attr) {
351 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
352 }
353
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700354 public void addOnChangeListener(OnIDPChangeListener listener) {
355 mChangeListeners.add(listener);
356 }
357
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800358 public void removeOnChangeListener(OnIDPChangeListener listener) {
359 mChangeListeners.remove(listener);
360 }
361
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800362
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800363 public void setCurrentGrid(Context context, String gridName) {
364 Context appContext = context.getApplicationContext();
365 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700366 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800367 }
368
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700369 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700370 return new Object[]{
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700371 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
372 numDatabaseAllAppsColumns, dbFile};
373 }
374
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700375 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700376 Object[] oldState = toModelState();
377
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700378 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700379 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700380 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700381
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700382 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700383 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700384 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700385 }
386 }
387
Sunny Goyal19ff7282021-04-22 10:12:54 -0700388 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
Alex Chau238aaee2021-10-06 16:15:24 +0100389 Context context, String gridName, boolean isSplitDisplay, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800390 ArrayList<DisplayOption> profiles = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100391 int xmlResource = isSplitDisplay ? R.xml.device_profiles_split : R.xml.device_profiles;
392
393 try (XmlResourceParser parser = context.getResources().getXml(xmlResource)) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700394 final int depth = parser.getDepth();
395 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700396 while (((type = parser.next()) != XmlPullParser.END_TAG ||
397 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800398 if ((type == XmlPullParser.START_TAG)
399 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800400
Thales Lima7ec83822021-08-05 13:32:10 +0100401 GridOption gridOption =
402 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
Alex Chau238aaee2021-10-06 16:15:24 +0100403 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100404 final int displayDepth = parser.getDepth();
405 while (((type = parser.next()) != XmlPullParser.END_TAG
406 || parser.getDepth() > displayDepth)
407 && type != XmlPullParser.END_DOCUMENT) {
408 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
409 parser.getName())) {
410 profiles.add(new DisplayOption(gridOption, context,
Thales Lima1de4d552021-10-13 16:13:25 +0100411 Xml.asAttributeSet(parser)));
Thales Lima7ec83822021-08-05 13:32:10 +0100412 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800413 }
414 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700415 }
416 }
sfufa@google.comde013292021-09-21 18:22:44 -0700417 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700418 throw new RuntimeException(e);
419 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800420
Sunny Goyalae190ff2020-04-14 00:19:01 +0000421 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800422 if (!TextUtils.isEmpty(gridName)) {
423 for (DisplayOption option : profiles) {
Alex Chau238aaee2021-10-06 16:15:24 +0100424 if (gridName.equals(option.grid.name)
425 && (option.grid.isEnabled || allowDisabledGrid)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000426 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800427 }
428 }
429 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000430 if (filteredProfiles.isEmpty()) {
431 // No grid found, use the default options
432 for (DisplayOption option : profiles) {
433 if (option.canBeDefault) {
434 filteredProfiles.add(option);
435 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800436 }
437 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000438 if (filteredProfiles.isEmpty()) {
439 throw new RuntimeException("No display option with canBeDefault=true");
440 }
441 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700442 }
443
Thales Lima7ec83822021-08-05 13:32:10 +0100444 /**
445 * @return all the grid options that can be shown on the device
446 */
447 public List<GridOption> parseAllGridOptions(Context context) {
448 List<GridOption> result = new ArrayList<>();
Thales Lima1de4d552021-10-13 16:13:25 +0100449 int xmlResource = isSplitDisplay ? R.xml.device_profiles_split : R.xml.device_profiles;
450
451 try (XmlResourceParser parser = context.getResources().getXml(xmlResource)) {
Thales Lima7ec83822021-08-05 13:32:10 +0100452 final int depth = parser.getDepth();
453 int type;
454 while (((type = parser.next()) != XmlPullParser.END_TAG
455 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
456 if ((type == XmlPullParser.START_TAG)
457 && GridOption.TAG_NAME.equals(parser.getName())) {
458 GridOption option =
459 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
460 if (option.isEnabled) {
461 result.add(option);
462 }
463 }
464 }
465 } catch (IOException | XmlPullParserException e) {
466 Log.e(TAG, "Error parsing device profile", e);
467 return Collections.emptyList();
468 }
469 return result;
470 }
471
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700472 private int getLauncherIconDensity(int requiredSize) {
473 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700474 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700475 DisplayMetrics.DENSITY_LOW,
476 DisplayMetrics.DENSITY_MEDIUM,
477 DisplayMetrics.DENSITY_TV,
478 DisplayMetrics.DENSITY_HIGH,
479 DisplayMetrics.DENSITY_XHIGH,
480 DisplayMetrics.DENSITY_XXHIGH,
481 DisplayMetrics.DENSITY_XXXHIGH
482 };
483
484 int density = DisplayMetrics.DENSITY_XXXHIGH;
485 for (int i = densityBuckets.length - 1; i >= 0; i--) {
486 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
487 / DisplayMetrics.DENSITY_DEFAULT;
488 if (expectedSize >= requiredSize) {
489 density = densityBuckets[i];
490 }
491 }
492
493 return density;
494 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700495
Adam Cohen2e6da152015-05-06 11:42:25 -0700496 /**
497 * Apply any Partner customization grid overrides.
498 *
499 * Currently we support: all apps row / column count.
500 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700501 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
502 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700503 if (p != null) {
504 p.applyInvariantDeviceProfileOverrides(this, dm);
505 }
506 }
507
Sunny Goyal415f1732018-11-29 10:33:47 -0800508 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700509 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700510 }
511
Sunny Goyal19ff7282021-04-22 10:12:54 -0700512 private static DisplayOption invDistWeightedInterpolate(
513 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
514 int minWidthPx = Integer.MAX_VALUE;
515 int minHeightPx = Integer.MAX_VALUE;
516 for (WindowBounds bounds : displayInfo.supportedBounds) {
517 boolean isTablet = displayInfo.isTablet(bounds);
518 if (isTablet && isSplitDisplay) {
519 // For split displays, take half width per page
520 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
521 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700522
Sunny Goyal19ff7282021-04-22 10:12:54 -0700523 } else if (!isTablet && bounds.isLandscape()) {
524 // We will use transposed layout in this case
525 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
526 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
527 } else {
528 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
529 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
530 }
531 }
532
533 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
534 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700535
536 // Sort the profiles based on the closeness to the device size
537 Collections.sort(points, (a, b) ->
538 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
539 dist(width, height, b.minWidthDps, b.minHeightDps)));
540
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700541 DisplayOption closestPoint = points.get(0);
542 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700543 float weights = 0;
544
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700545 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
546 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700547 }
548
549 DisplayOption out = new DisplayOption(closestOption);
550 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700551 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700552 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
553 weights += w;
554 out.add(new DisplayOption().add(p).multiply(w));
555 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700556 out.multiply(1.0f / weights);
557
558 // Since the bitmaps are persisted, ensure that the default bitmap size is same as
559 // predefined size to avoid cache invalidation
Thales Lima83bedbf2021-10-05 17:47:39 +0100560 out.iconSizes[INDEX_DEFAULT] =
561 closestPoint.iconSizes[INDEX_DEFAULT];
562 for (int i = INDEX_DEFAULT + 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700563 out.iconSizes[i] = Math.min(out.iconSizes[i],
Thales Lima83bedbf2021-10-05 17:47:39 +0100564 out.iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700565 }
566
567 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700568 }
569
Sunny Goyal27835952017-01-13 12:15:53 -0800570 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700571 Resources res = context.getResources();
572 Configuration config = context.getResources().getConfiguration();
573
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400574 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
575 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
576 return getBestMatch(screenWidth, screenHeight);
577 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700578
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400579 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700580 DeviceProfile bestMatch = supportedProfiles.get(0);
581 float minDiff = Float.MAX_VALUE;
582
583 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400584 float diff = Math.abs(profile.widthPx - screenWidth)
585 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700586 if (diff < minDiff) {
587 minDiff = diff;
588 bestMatch = profile;
589 }
590 }
591 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800592 }
593
Sunny Goyal415f1732018-11-29 10:33:47 -0800594 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700595 float d = dist(x0, y0, x1, y1);
596 if (Float.compare(d, 0f) == 0) {
597 return Float.POSITIVE_INFINITY;
598 }
599 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
600 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700601
602 /**
603 * As a ratio of screen height, the total distance we want the parallax effect to span
604 * horizontally
605 */
606 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
607 float aspectRatio = width / (float) height;
608
609 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
610 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
611 // We will use these two data points to extrapolate how much the wallpaper parallax effect
612 // to span (ie travel) at any aspect ratio:
613
sfufa@google.comde013292021-09-21 18:22:44 -0700614 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
615 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700616 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
617 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
618
619 // To find out the desired width at different aspect ratios, we use the following two
620 // formulas, where the coefficient on x is the aspect ratio (width/height):
621 // (16/10)x + y = 1.5
622 // (10/16)x + y = 1.2
623 // We solve for x and y and end up with a final formula:
624 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700625 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
626 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700627 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
628 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
629 return x * aspectRatio + y;
630 }
631
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700632 public interface OnIDPChangeListener {
633
Sunny Goyalb47172b2021-05-03 19:59:51 -0700634 /**
635 * Called when the device provide changes
636 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700637 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700638 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800639
640
Sunny Goyaleff44f32019-01-09 17:29:49 -0800641 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800642
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800643 public static final String TAG_NAME = "grid-option";
644
Sunny Goyaleff44f32019-01-09 17:29:49 -0800645 public final String name;
646 public final int numRows;
647 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100648 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800649
650 private final int numFolderRows;
651 private final int numFolderColumns;
652
Sunny Goyal19ff7282021-04-22 10:12:54 -0700653 private final int numAllAppsColumns;
654 private final int numDatabaseAllAppsColumns;
655 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700656 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800657
Tracy Zhou7df93d22020-01-27 13:44:06 -0800658 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000659
Sunny Goyal415f1732018-11-29 10:33:47 -0800660 private final int defaultLayoutId;
661 private final int demoModeLayoutId;
662
Jon Mirandae126d722021-02-25 10:45:20 -0500663 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400664 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500665
Sunny Goyal5bc18462019-01-07 15:13:39 -0800666 private final SparseArray<TypedValue> extraAttrs;
667
Thales Lima7ec83822021-08-05 13:32:10 +0100668 public GridOption(Context context, AttributeSet attrs, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800669 TypedArray a = context.obtainStyledAttributes(
670 attrs, R.styleable.GridDisplayOption);
671 name = a.getString(R.styleable.GridDisplayOption_name);
672 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
673 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
674
Tracy Zhou7df93d22020-01-27 13:44:06 -0800675 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1e448462021-08-13 21:48:35 +0100676 defaultLayoutId = a.getResourceId(isSplitDisplay && a.hasValue(
677 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
678 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
679 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800680 demoModeLayoutId = a.getResourceId(
681 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700682
683 numAllAppsColumns = a.getInt(
684 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
685 numDatabaseAllAppsColumns = a.getInt(
686 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
687
688 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800689 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700690 numDatabaseHotseatIcons = a.getInt(
691 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
692
Sunny Goyal415f1732018-11-29 10:33:47 -0800693 numFolderRows = a.getInt(
694 R.styleable.GridDisplayOption_numFolderRows, numRows);
695 numFolderColumns = a.getInt(
696 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700697
Jon Mirandae126d722021-02-25 10:45:20 -0500698 isScalable = a.getBoolean(
699 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400700 devicePaddingId = a.getResourceId(
701 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500702
Thales Lima1de4d552021-10-13 16:13:25 +0100703 isEnabled = a.getBoolean(R.styleable.GridDisplayOption_gridEnabled, true);
Thales Lima7ec83822021-08-05 13:32:10 +0100704
Sunny Goyal415f1732018-11-29 10:33:47 -0800705 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800706 extraAttrs = Themes.createValueMap(context, attrs,
707 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800708 }
709 }
710
Sunny Goyal19ff7282021-04-22 10:12:54 -0700711 @VisibleForTesting
712 static final class DisplayOption {
713
714 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800715
Sunny Goyal415f1732018-11-29 10:33:47 -0800716 private final float minWidthDps;
717 private final float minHeightDps;
718 private final boolean canBeDefault;
719
Thales Lima83bedbf2021-10-05 17:47:39 +0100720 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100721
722 private float folderBorderSpace;
723 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100724 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100725
Thales Lima78d00ad2021-09-30 11:29:06 +0100726 private final float[] iconSizes = new float[COUNT_SIZES];
727 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800728
Thales Lima1de4d552021-10-13 16:13:25 +0100729 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800730 this.grid = grid;
731
Thales Lima1de4d552021-10-13 16:13:25 +0100732 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProfileDisplayOption);
Sunny Goyal415f1732018-11-29 10:33:47 -0800733
Sunny Goyal415f1732018-11-29 10:33:47 -0800734 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
735 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700736
Thales Lima1de4d552021-10-13 16:13:25 +0100737 canBeDefault = a.getBoolean(R.styleable.ProfileDisplayOption_canBeDefault, false);
Sunny Goyal415f1732018-11-29 10:33:47 -0800738
Thales Lima83bedbf2021-10-05 17:47:39 +0100739 float x;
740 float y;
741
742 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
743 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
744 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
745 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
746 minCellSize[INDEX_ALL_APPS] = new PointF(x, y);
747
748 x = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellWidthDps,
749 minCellSize[INDEX_DEFAULT].x);
750 y = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellHeightDps,
751 minCellSize[INDEX_DEFAULT].y);
752 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
753
754 x = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellWidthDps,
755 minCellSize[INDEX_DEFAULT].x);
756 y = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellHeightDps,
757 minCellSize[INDEX_DEFAULT].y);
758 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100759
760 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceDps, 0);
761 float twoPanelPortraitBorderSpaceDps = a.getFloat(
762 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceDps, borderSpace);
763 float twoPanelLandscapeBorderSpaceDps = a.getFloat(
764 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceDps, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100765
766 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontalDps, borderSpace);
767 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVerticalDps, borderSpace);
768 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
769 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
770
771 x = a.getFloat(
772 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceHorizontalDps,
773 twoPanelPortraitBorderSpaceDps);
774 y = a.getFloat(
775 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceVerticalDps,
776 twoPanelPortraitBorderSpaceDps);
777 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
778
779 x = a.getFloat(
780 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceHorizontalDps,
781 twoPanelLandscapeBorderSpaceDps);
782 y = a.getFloat(
783 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceVerticalDps,
784 twoPanelLandscapeBorderSpaceDps);
785 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
786
787 x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellSpacingDps,
788 borderSpace);
789 borderSpaces[INDEX_ALL_APPS] = new PointF(x, y);
790 folderBorderSpace = borderSpace;
Jon Mirandae126d722021-02-25 10:45:20 -0500791
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700792 iconSizes[INDEX_DEFAULT] =
793 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
794 iconSizes[INDEX_LANDSCAPE] =
795 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
796 iconSizes[INDEX_DEFAULT]);
797 iconSizes[INDEX_ALL_APPS] =
798 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
799 iconSizes[INDEX_DEFAULT]);
800 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
801 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconSize,
802 iconSizes[INDEX_DEFAULT]);
803 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
804 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconSize,
Thales Lima83bedbf2021-10-05 17:47:39 +0100805 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700806
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700807 textSizes[INDEX_DEFAULT] =
808 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
809 textSizes[INDEX_LANDSCAPE] =
810 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconTextSize,
811 textSizes[INDEX_DEFAULT]);
812 textSizes[INDEX_ALL_APPS] =
813 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
814 textSizes[INDEX_DEFAULT]);
815 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
816 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconTextSize,
817 textSizes[INDEX_DEFAULT]);
818 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
819 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconTextSize,
Thales Lima83bedbf2021-10-05 17:47:39 +0100820 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700821
Thales Lima83bedbf2021-10-05 17:47:39 +0100822 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
823 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
824 horizontalMargin[INDEX_LANDSCAPE] = horizontalMargin[INDEX_DEFAULT];
825 horizontalMargin[INDEX_ALL_APPS] = horizontalMargin[INDEX_DEFAULT];
826 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Limad90faab2021-09-21 17:18:47 +0100827 R.styleable.ProfileDisplayOption_twoPanelLandscapeHorizontalMargin,
Thales Lima83bedbf2021-10-05 17:47:39 +0100828 horizontalMargin[INDEX_DEFAULT]);
829 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Limad90faab2021-09-21 17:18:47 +0100830 R.styleable.ProfileDisplayOption_twoPanelPortraitHorizontalMargin,
Thales Lima83bedbf2021-10-05 17:47:39 +0100831 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +0100832
Sunny Goyal415f1732018-11-29 10:33:47 -0800833 a.recycle();
834 }
835
836 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700837 this(null);
838 }
839
840 DisplayOption(GridOption grid) {
841 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800842 minWidthDps = 0;
843 minHeightDps = 0;
844 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +0100845 for (int i = 0; i < COUNT_SIZES; i++) {
846 iconSizes[i] = 0;
847 textSizes[i] = 0;
848 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +0100849 minCellSize[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +0100850 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800851 }
852
853 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100854 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700855 iconSizes[i] *= w;
856 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +0100857 borderSpaces[i].x *= w;
858 borderSpaces[i].y *= w;
859 minCellSize[i].x *= w;
860 minCellSize[i].y *= w;
861 horizontalMargin[i] *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700862 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100863
864 folderBorderSpace *= w;
865
Sunny Goyal415f1732018-11-29 10:33:47 -0800866 return this;
867 }
868
869 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100870 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700871 iconSizes[i] += p.iconSizes[i];
872 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +0100873 borderSpaces[i].x += p.borderSpaces[i].x;
874 borderSpaces[i].y += p.borderSpaces[i].y;
875 minCellSize[i].x += p.minCellSize[i].x;
876 minCellSize[i].y += p.minCellSize[i].y;
877 horizontalMargin[i] += p.horizontalMargin[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700878 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100879
880 folderBorderSpace += p.folderBorderSpace;
881
Sunny Goyal415f1732018-11-29 10:33:47 -0800882 return this;
883 }
884 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000885}