blob: 1b2f177ff2f26015633bee87ecd56f0da5edd6d1 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
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
Sunny Goyal19ff7282021-04-22 10:12:54 -070073 private static final int DEFAULT_TRUE = -1;
74 private static final int DEFAULT_SPLIT_DISPLAY = 2;
Thales Lima7ec83822021-08-05 13:32:10 +010075 private static final int GRID_ENABLED_ALL_DISPLAYS = 0;
76 private static final int GRID_ENABLED_SINGLE_DISPLAY = 1;
Sunny Goyal19ff7282021-04-22 10:12:54 -070077
Hyunyoung Songc55a3502018-12-04 15:43:16 -080078 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080079
Sunny Goyal53d7ee42015-05-22 12:25:45 -070080 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
81
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070082 // Constants that affects the interpolation curve between statically defined device profile
83 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080084 private static final float KNEARESTNEIGHBOR = 3;
85 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070086
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070087 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080088 private static final float WEIGHT_EFFICIENT = 100000f;
89
Thales Lima83bedbf2021-10-05 17:47:39 +010090 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
91 // constraints
92 static final int COUNT_SIZES = 5;
93 static final int INDEX_DEFAULT = 0;
94 static final int INDEX_LANDSCAPE = 1;
95 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
96 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
97 static final int INDEX_ALL_APPS = 4;
98
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099 /**
100 * Number of icons per row and column in the workspace.
101 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700102 public int numRows;
103 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700104
105 /**
106 * Number of icons per row and column in the folder.
107 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700108 public int numFolderRows;
109 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100110 public float[] iconSize;
111 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700112 public int iconBitmapSize;
113 public int fillResIconDpi;
Thales Lima7ec83822021-08-05 13:32:10 +0100114 public boolean isSplitDisplay;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700115
Thales Lima83bedbf2021-10-05 17:47:39 +0100116 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100117
Thales Lima83bedbf2021-10-05 17:47:39 +0100118 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100119 public float folderBorderSpace;
120
Thales Lima83bedbf2021-10-05 17:47:39 +0100121 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500122
Sunny Goyal5bc18462019-01-07 15:13:39 -0800123 private SparseArray<TypedValue> mExtraAttrs;
124
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700125 /**
126 * Number of icons inside the hotseat area.
127 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700128 protected int numShownHotseatIcons;
129
130 /**
131 * Number of icons inside the hotseat area that is stored in the database. This is greater than
132 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
133 * sizes that share the same DB.
134 */
135 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700136
Jon Miranda6f7e9702019-09-16 14:44:14 -0700137 /**
138 * Number of columns in the all apps list.
139 */
140 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700141 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700142
Jon Mirandae126d722021-02-25 10:45:20 -0500143 /**
144 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
145 */
146 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400147 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500148
Tracy Zhou7df93d22020-01-27 13:44:06 -0800149 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800150 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700151 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700152
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000153 /**
154 * An immutable list of supported profiles.
155 */
156 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700157
sfufa@google.comde013292021-09-21 18:22:44 -0700158 @Nullable
159 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500160
Sunny Goyal6f866092016-03-17 17:04:15 -0700161 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700162 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700163
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700164 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700165
Sunny Goyalf633ef52018-03-13 09:57:05 -0700166 @VisibleForTesting
sfufa@google.comde013292021-09-21 18:22:44 -0700167 public InvariantDeviceProfile() {
168 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700169
Sunny Goyalbbf01842015-10-08 07:41:15 -0700170 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700171 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700172 String gridName = getCurrentGridName(context);
173 String newGridName = initGrid(context, gridName);
174 if (!newGridName.equals(gridName)) {
175 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
176 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700177 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700178
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700179 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100180 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700181 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100182 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700183 }
184 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700185 }
186
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700187 /**
188 * This constructor should NOT have any monitors by design.
189 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800190 public InvariantDeviceProfile(Context context, String gridName) {
191 String newName = initGrid(context, gridName);
192 if (newName == null || !newName.equals(gridName)) {
193 throw new IllegalArgumentException("Unknown grid name");
194 }
195 }
196
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700197 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800198 * This constructor should NOT have any monitors by design.
199 */
200 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700201 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100202 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700203 String gridName = getCurrentGridName(context);
204
205 // Get the display info based on default display and interpolate it to existing display
206 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700207 DisplayController.INSTANCE.get(context).getInfo(),
Alex Chau238aaee2021-10-06 16:15:24 +0100208 getPredefinedDeviceProfiles(context, gridName, false, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700209
Alex Chau417bd722021-01-26 15:22:18 +0000210 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700211 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Alex Chau238aaee2021-10-06 16:15:24 +0100212 myInfo, getPredefinedDeviceProfiles(context, gridName, false, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700213
214 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
215 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100216 result.iconSizes[INDEX_DEFAULT] =
217 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
218 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700219 result.iconSizes[i] = Math.min(
220 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000221 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700222
Thales Lima83bedbf2021-10-05 17:47:39 +0100223 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
224 COUNT_SIZES);
225 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
226 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500227
Sunny Goyal19ff7282021-04-22 10:12:54 -0700228 initGrid(context, myInfo, result, false);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800229 }
230
Alex Chau238aaee2021-10-06 16:15:24 +0100231 /**
232 * Reinitialize the current grid after a restore, where some grids might now be disabled.
233 */
234 public void reinitializeAfterRestore(Context context) {
235 String currentDbFile = dbFile;
236 String gridName = getCurrentGridName(context);
237 String newGridName = initGrid(context, gridName);
238 if (!newGridName.equals(gridName)) {
239 Log.d(TAG, "Restored grid is disabled : " + gridName
240 + ", migrating to: " + newGridName
241 + ", removing all other grid db files");
242 for (String gridDbFile : LauncherFiles.GRID_DB_FILES) {
243 if (gridDbFile.equals(currentDbFile)) {
244 continue;
245 }
246 if (context.getDatabasePath(gridDbFile).delete()) {
247 Log.d(TAG, "Removed old grid db file: " + gridDbFile);
248 }
249 }
250 setCurrentGrid(context, gridName);
251 }
252 }
253
Tracy Zhou42255d22020-03-13 00:38:11 -0700254 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700255 return Utilities.isGridOptionsEnabled(context)
256 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700257 }
258
Sunny Goyaleff44f32019-01-09 17:29:49 -0800259 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700260 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Pat Manning67094bf2021-05-26 12:38:13 +0000261 // Each screen has two profiles (portrait/landscape), so devices with four or more
262 // supported profiles implies two or more internal displays.
263 boolean isSplitDisplay =
264 displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700265
266 ArrayList<DisplayOption> allOptions =
Alex Chau238aaee2021-10-06 16:15:24 +0100267 getPredefinedDeviceProfiles(context, gridName, isSplitDisplay,
268 RestoreDbTask.isPending(context));
Sunny Goyal19ff7282021-04-22 10:12:54 -0700269 DisplayOption displayOption =
270 invDistWeightedInterpolate(displayInfo, allOptions, isSplitDisplay);
271 initGrid(context, displayInfo, displayOption, isSplitDisplay);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700272 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800273 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700274
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700275 private void initGrid(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700276 Context context, Info displayInfo, DisplayOption displayOption,
277 boolean isSplitDisplay) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700278 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700279 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000280 numRows = closestProfile.numRows;
281 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000282 dbFile = closestProfile.dbFile;
283 defaultLayoutId = closestProfile.defaultLayoutId;
284 demoModeLayoutId = closestProfile.demoModeLayoutId;
285 numFolderRows = closestProfile.numFolderRows;
286 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500287 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400288 devicePaddingId = closestProfile.devicePaddingId;
Thales Lima7ec83822021-08-05 13:32:10 +0100289 this.isSplitDisplay = isSplitDisplay;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000290
291 mExtraAttrs = closestProfile.extraAttrs;
292
Thales Lima83bedbf2021-10-05 17:47:39 +0100293 iconSize = displayOption.iconSizes;
294 iconBitmapSize = ResourceUtils.pxFromDp(iconSize[INDEX_DEFAULT], metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000295 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
296
Thales Lima83bedbf2021-10-05 17:47:39 +0100297 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700298
Thales Lima83bedbf2021-10-05 17:47:39 +0100299 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100300
Thales Lima83bedbf2021-10-05 17:47:39 +0100301 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100302 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700303
Thales Limad90faab2021-09-21 17:18:47 +0100304 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100305
Sunny Goyal19ff7282021-04-22 10:12:54 -0700306 numShownHotseatIcons = closestProfile.numHotseatIcons;
307 numDatabaseHotseatIcons = isSplitDisplay
308 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
309
310 numAllAppsColumns = closestProfile.numAllAppsColumns;
311 numDatabaseAllAppsColumns = isSplitDisplay
312 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500313
Thales Lima83bedbf2021-10-05 17:47:39 +0100314 if (!Utilities.isGridOptionsEnabled(context)) {
315 iconSize[INDEX_ALL_APPS] = iconSize[INDEX_DEFAULT];
316 iconTextSize[INDEX_ALL_APPS] = iconTextSize[INDEX_DEFAULT];
Sunny Goyalae190ff2020-04-14 00:19:01 +0000317 }
318
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400319 if (devicePaddingId != 0) {
320 devicePaddings = new DevicePaddings(context, devicePaddingId);
321 }
322
Sunny Goyalae190ff2020-04-14 00:19:01 +0000323 // If the partner customization apk contains any grid overrides, apply them
324 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700325 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700326
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000327 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700328 defaultWallpaperSize = new Point(displayInfo.currentSize);
329 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000330 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700331 .setUseTwoPanels(isSplitDisplay)
332 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700333
Sunny Goyal19ff7282021-04-22 10:12:54 -0700334 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
335 int displayWidth = bounds.bounds.width();
336 int displayHeight = bounds.bounds.height();
337 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700338
Sunny Goyal19ff7282021-04-22 10:12:54 -0700339 // We need to ensure that there is enough extra space in the wallpaper
340 // for the intended parallax effects
341 float parallaxFactor =
342 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
343 ? 2
344 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
345 defaultWallpaperSize.x =
346 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700347 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000348 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700349
350 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
351 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700352 }
353
Sunny Goyal5bc18462019-01-07 15:13:39 -0800354 @Nullable
355 public TypedValue getAttrValue(int attr) {
356 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
357 }
358
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700359 public void addOnChangeListener(OnIDPChangeListener listener) {
360 mChangeListeners.add(listener);
361 }
362
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800363 public void removeOnChangeListener(OnIDPChangeListener listener) {
364 mChangeListeners.remove(listener);
365 }
366
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800367
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800368 public void setCurrentGrid(Context context, String gridName) {
369 Context appContext = context.getApplicationContext();
370 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700371 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800372 }
373
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700374 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700375 return new Object[]{
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700376 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
377 numDatabaseAllAppsColumns, dbFile};
378 }
379
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700380 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700381 Object[] oldState = toModelState();
382
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700383 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700384 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700385 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700386
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700387 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700388 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700389 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700390 }
391 }
392
Sunny Goyal19ff7282021-04-22 10:12:54 -0700393 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
Alex Chau238aaee2021-10-06 16:15:24 +0100394 Context context, String gridName, boolean isSplitDisplay, boolean allowDisabledGrid) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800395 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700396 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
397 final int depth = parser.getDepth();
398 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700399 while (((type = parser.next()) != XmlPullParser.END_TAG ||
400 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800401 if ((type == XmlPullParser.START_TAG)
402 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800403
Thales Lima7ec83822021-08-05 13:32:10 +0100404 GridOption gridOption =
405 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
Alex Chau238aaee2021-10-06 16:15:24 +0100406 if (gridOption.isEnabled || allowDisabledGrid) {
Thales Lima7ec83822021-08-05 13:32:10 +0100407 final int displayDepth = parser.getDepth();
408 while (((type = parser.next()) != XmlPullParser.END_TAG
409 || parser.getDepth() > displayDepth)
410 && type != XmlPullParser.END_DOCUMENT) {
411 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
412 parser.getName())) {
413 profiles.add(new DisplayOption(gridOption, context,
414 Xml.asAttributeSet(parser),
415 isSplitDisplay ? DEFAULT_SPLIT_DISPLAY : DEFAULT_TRUE));
416 }
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<>();
453 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
454 final int depth = parser.getDepth();
455 int type;
456 while (((type = parser.next()) != XmlPullParser.END_TAG
457 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
458 if ((type == XmlPullParser.START_TAG)
459 && GridOption.TAG_NAME.equals(parser.getName())) {
460 GridOption option =
461 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
462 if (option.isEnabled) {
463 result.add(option);
464 }
465 }
466 }
467 } catch (IOException | XmlPullParserException e) {
468 Log.e(TAG, "Error parsing device profile", e);
469 return Collections.emptyList();
470 }
471 return result;
472 }
473
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700474 private int getLauncherIconDensity(int requiredSize) {
475 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700476 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700477 DisplayMetrics.DENSITY_LOW,
478 DisplayMetrics.DENSITY_MEDIUM,
479 DisplayMetrics.DENSITY_TV,
480 DisplayMetrics.DENSITY_HIGH,
481 DisplayMetrics.DENSITY_XHIGH,
482 DisplayMetrics.DENSITY_XXHIGH,
483 DisplayMetrics.DENSITY_XXXHIGH
484 };
485
486 int density = DisplayMetrics.DENSITY_XXXHIGH;
487 for (int i = densityBuckets.length - 1; i >= 0; i--) {
488 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
489 / DisplayMetrics.DENSITY_DEFAULT;
490 if (expectedSize >= requiredSize) {
491 density = densityBuckets[i];
492 }
493 }
494
495 return density;
496 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700497
Adam Cohen2e6da152015-05-06 11:42:25 -0700498 /**
499 * Apply any Partner customization grid overrides.
500 *
501 * Currently we support: all apps row / column count.
502 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700503 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
504 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700505 if (p != null) {
506 p.applyInvariantDeviceProfileOverrides(this, dm);
507 }
508 }
509
Sunny Goyal415f1732018-11-29 10:33:47 -0800510 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700511 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700512 }
513
Sunny Goyal19ff7282021-04-22 10:12:54 -0700514 private static DisplayOption invDistWeightedInterpolate(
515 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
516 int minWidthPx = Integer.MAX_VALUE;
517 int minHeightPx = Integer.MAX_VALUE;
518 for (WindowBounds bounds : displayInfo.supportedBounds) {
519 boolean isTablet = displayInfo.isTablet(bounds);
520 if (isTablet && isSplitDisplay) {
521 // For split displays, take half width per page
522 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
523 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700524
Sunny Goyal19ff7282021-04-22 10:12:54 -0700525 } else if (!isTablet && bounds.isLandscape()) {
526 // We will use transposed layout in this case
527 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
528 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
529 } else {
530 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
531 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
532 }
533 }
534
535 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
536 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700537
538 // Sort the profiles based on the closeness to the device size
539 Collections.sort(points, (a, b) ->
540 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
541 dist(width, height, b.minWidthDps, b.minHeightDps)));
542
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700543 DisplayOption closestPoint = points.get(0);
544 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700545 float weights = 0;
546
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700547 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
548 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700549 }
550
551 DisplayOption out = new DisplayOption(closestOption);
552 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700553 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700554 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
555 weights += w;
556 out.add(new DisplayOption().add(p).multiply(w));
557 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700558 out.multiply(1.0f / weights);
559
560 // Since the bitmaps are persisted, ensure that the default bitmap size is same as
561 // predefined size to avoid cache invalidation
Thales Lima83bedbf2021-10-05 17:47:39 +0100562 out.iconSizes[INDEX_DEFAULT] =
563 closestPoint.iconSizes[INDEX_DEFAULT];
564 for (int i = INDEX_DEFAULT + 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700565 out.iconSizes[i] = Math.min(out.iconSizes[i],
Thales Lima83bedbf2021-10-05 17:47:39 +0100566 out.iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700567 }
568
569 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700570 }
571
Sunny Goyal27835952017-01-13 12:15:53 -0800572 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700573 Resources res = context.getResources();
574 Configuration config = context.getResources().getConfiguration();
575
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400576 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
577 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
578 return getBestMatch(screenWidth, screenHeight);
579 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700580
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400581 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700582 DeviceProfile bestMatch = supportedProfiles.get(0);
583 float minDiff = Float.MAX_VALUE;
584
585 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400586 float diff = Math.abs(profile.widthPx - screenWidth)
587 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700588 if (diff < minDiff) {
589 minDiff = diff;
590 bestMatch = profile;
591 }
592 }
593 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800594 }
595
Sunny Goyal415f1732018-11-29 10:33:47 -0800596 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700597 float d = dist(x0, y0, x1, y1);
598 if (Float.compare(d, 0f) == 0) {
599 return Float.POSITIVE_INFINITY;
600 }
601 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
602 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700603
604 /**
605 * As a ratio of screen height, the total distance we want the parallax effect to span
606 * horizontally
607 */
608 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
609 float aspectRatio = width / (float) height;
610
611 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
612 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
613 // We will use these two data points to extrapolate how much the wallpaper parallax effect
614 // to span (ie travel) at any aspect ratio:
615
sfufa@google.comde013292021-09-21 18:22:44 -0700616 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
617 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700618 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
619 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
620
621 // To find out the desired width at different aspect ratios, we use the following two
622 // formulas, where the coefficient on x is the aspect ratio (width/height):
623 // (16/10)x + y = 1.5
624 // (10/16)x + y = 1.2
625 // We solve for x and y and end up with a final formula:
626 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700627 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
628 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700629 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
630 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
631 return x * aspectRatio + y;
632 }
633
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700634 public interface OnIDPChangeListener {
635
Sunny Goyalb47172b2021-05-03 19:59:51 -0700636 /**
637 * Called when the device provide changes
638 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700639 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700640 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800641
642
Sunny Goyaleff44f32019-01-09 17:29:49 -0800643 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800644
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800645 public static final String TAG_NAME = "grid-option";
646
Sunny Goyaleff44f32019-01-09 17:29:49 -0800647 public final String name;
648 public final int numRows;
649 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100650 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800651
652 private final int numFolderRows;
653 private final int numFolderColumns;
654
Sunny Goyal19ff7282021-04-22 10:12:54 -0700655 private final int numAllAppsColumns;
656 private final int numDatabaseAllAppsColumns;
657 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700658 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800659
Tracy Zhou7df93d22020-01-27 13:44:06 -0800660 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000661
Sunny Goyal415f1732018-11-29 10:33:47 -0800662 private final int defaultLayoutId;
663 private final int demoModeLayoutId;
664
Jon Mirandae126d722021-02-25 10:45:20 -0500665 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400666 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500667
Sunny Goyal5bc18462019-01-07 15:13:39 -0800668 private final SparseArray<TypedValue> extraAttrs;
669
Thales Lima7ec83822021-08-05 13:32:10 +0100670 public GridOption(Context context, AttributeSet attrs, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800671 TypedArray a = context.obtainStyledAttributes(
672 attrs, R.styleable.GridDisplayOption);
673 name = a.getString(R.styleable.GridDisplayOption_name);
674 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
675 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
676
Tracy Zhou7df93d22020-01-27 13:44:06 -0800677 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1e448462021-08-13 21:48:35 +0100678 defaultLayoutId = a.getResourceId(isSplitDisplay && a.hasValue(
679 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
680 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
681 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800682 demoModeLayoutId = a.getResourceId(
683 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700684
685 numAllAppsColumns = a.getInt(
686 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
687 numDatabaseAllAppsColumns = a.getInt(
688 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
689
690 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800691 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700692 numDatabaseHotseatIcons = a.getInt(
693 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
694
Sunny Goyal415f1732018-11-29 10:33:47 -0800695 numFolderRows = a.getInt(
696 R.styleable.GridDisplayOption_numFolderRows, numRows);
697 numFolderColumns = a.getInt(
698 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700699
Jon Mirandae126d722021-02-25 10:45:20 -0500700 isScalable = a.getBoolean(
701 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400702 devicePaddingId = a.getResourceId(
703 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500704
Thales Lima7ec83822021-08-05 13:32:10 +0100705 final int enabledInt =
706 a.getInteger(R.styleable.GridDisplayOption_gridEnabled,
707 GRID_ENABLED_ALL_DISPLAYS);
708 isEnabled = enabledInt == GRID_ENABLED_ALL_DISPLAYS
709 || enabledInt == GRID_ENABLED_SINGLE_DISPLAY && !isSplitDisplay;
710
Sunny Goyal415f1732018-11-29 10:33:47 -0800711 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800712 extraAttrs = Themes.createValueMap(context, attrs,
713 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800714 }
715 }
716
Sunny Goyal19ff7282021-04-22 10:12:54 -0700717 @VisibleForTesting
718 static final class DisplayOption {
719
720 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800721
Sunny Goyal415f1732018-11-29 10:33:47 -0800722 private final float minWidthDps;
723 private final float minHeightDps;
724 private final boolean canBeDefault;
725
Thales Lima83bedbf2021-10-05 17:47:39 +0100726 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100727
728 private float folderBorderSpace;
729 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100730 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100731
Thales Lima78d00ad2021-09-30 11:29:06 +0100732 private final float[] iconSizes = new float[COUNT_SIZES];
733 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800734
Sunny Goyal19ff7282021-04-22 10:12:54 -0700735 DisplayOption(GridOption grid, Context context, AttributeSet attrs, int defaultFlagValue) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800736 this.grid = grid;
737
738 TypedArray a = context.obtainStyledAttributes(
739 attrs, R.styleable.ProfileDisplayOption);
740
Sunny Goyal415f1732018-11-29 10:33:47 -0800741 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
742 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700743
744 canBeDefault = a.getInt(R.styleable.ProfileDisplayOption_canBeDefault, 0)
745 == defaultFlagValue;
Sunny Goyal415f1732018-11-29 10:33:47 -0800746
Thales Lima83bedbf2021-10-05 17:47:39 +0100747 float x;
748 float y;
749
750 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
751 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
752 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
753 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
754 minCellSize[INDEX_ALL_APPS] = new PointF(x, y);
755
756 x = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellWidthDps,
757 minCellSize[INDEX_DEFAULT].x);
758 y = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellHeightDps,
759 minCellSize[INDEX_DEFAULT].y);
760 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
761
762 x = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellWidthDps,
763 minCellSize[INDEX_DEFAULT].x);
764 y = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellHeightDps,
765 minCellSize[INDEX_DEFAULT].y);
766 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100767
768 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceDps, 0);
769 float twoPanelPortraitBorderSpaceDps = a.getFloat(
770 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceDps, borderSpace);
771 float twoPanelLandscapeBorderSpaceDps = a.getFloat(
772 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceDps, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100773
774 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontalDps, borderSpace);
775 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVerticalDps, borderSpace);
776 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
777 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
778
779 x = a.getFloat(
780 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceHorizontalDps,
781 twoPanelPortraitBorderSpaceDps);
782 y = a.getFloat(
783 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceVerticalDps,
784 twoPanelPortraitBorderSpaceDps);
785 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
786
787 x = a.getFloat(
788 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceHorizontalDps,
789 twoPanelLandscapeBorderSpaceDps);
790 y = a.getFloat(
791 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceVerticalDps,
792 twoPanelLandscapeBorderSpaceDps);
793 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
794
795 x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellSpacingDps,
796 borderSpace);
797 borderSpaces[INDEX_ALL_APPS] = new PointF(x, y);
798 folderBorderSpace = borderSpace;
Jon Mirandae126d722021-02-25 10:45:20 -0500799
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700800 iconSizes[INDEX_DEFAULT] =
801 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
802 iconSizes[INDEX_LANDSCAPE] =
803 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
804 iconSizes[INDEX_DEFAULT]);
805 iconSizes[INDEX_ALL_APPS] =
806 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
807 iconSizes[INDEX_DEFAULT]);
808 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
809 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconSize,
810 iconSizes[INDEX_DEFAULT]);
811 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
812 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconSize,
Thales Lima83bedbf2021-10-05 17:47:39 +0100813 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700814
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700815 textSizes[INDEX_DEFAULT] =
816 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
817 textSizes[INDEX_LANDSCAPE] =
818 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconTextSize,
819 textSizes[INDEX_DEFAULT]);
820 textSizes[INDEX_ALL_APPS] =
821 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
822 textSizes[INDEX_DEFAULT]);
823 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
824 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconTextSize,
825 textSizes[INDEX_DEFAULT]);
826 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
827 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconTextSize,
Thales Lima83bedbf2021-10-05 17:47:39 +0100828 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700829
Thales Lima83bedbf2021-10-05 17:47:39 +0100830 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
831 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
832 horizontalMargin[INDEX_LANDSCAPE] = horizontalMargin[INDEX_DEFAULT];
833 horizontalMargin[INDEX_ALL_APPS] = horizontalMargin[INDEX_DEFAULT];
834 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Limad90faab2021-09-21 17:18:47 +0100835 R.styleable.ProfileDisplayOption_twoPanelLandscapeHorizontalMargin,
Thales Lima83bedbf2021-10-05 17:47:39 +0100836 horizontalMargin[INDEX_DEFAULT]);
837 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Limad90faab2021-09-21 17:18:47 +0100838 R.styleable.ProfileDisplayOption_twoPanelPortraitHorizontalMargin,
Thales Lima83bedbf2021-10-05 17:47:39 +0100839 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +0100840
Sunny Goyal415f1732018-11-29 10:33:47 -0800841 a.recycle();
842 }
843
844 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700845 this(null);
846 }
847
848 DisplayOption(GridOption grid) {
849 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800850 minWidthDps = 0;
851 minHeightDps = 0;
852 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +0100853 for (int i = 0; i < COUNT_SIZES; i++) {
854 iconSizes[i] = 0;
855 textSizes[i] = 0;
856 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +0100857 minCellSize[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +0100858 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800859 }
860
861 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100862 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700863 iconSizes[i] *= w;
864 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +0100865 borderSpaces[i].x *= w;
866 borderSpaces[i].y *= w;
867 minCellSize[i].x *= w;
868 minCellSize[i].y *= w;
869 horizontalMargin[i] *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700870 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100871
872 folderBorderSpace *= w;
873
Sunny Goyal415f1732018-11-29 10:33:47 -0800874 return this;
875 }
876
877 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100878 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700879 iconSizes[i] += p.iconSizes[i];
880 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +0100881 borderSpaces[i].x += p.borderSpaces[i].x;
882 borderSpaces[i].y += p.borderSpaces[i].y;
883 minCellSize[i].x += p.minCellSize[i].x;
884 minCellSize[i].y += p.minCellSize[i].y;
885 horizontalMargin[i] += p.horizontalMargin[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700886 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100887
888 folderBorderSpace += p.folderBorderSpace;
889
Sunny Goyal415f1732018-11-29 10:33:47 -0800890 return this;
891 }
892 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000893}