blob: 5f441d183d092ddb9496041e24915f39a656907e [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;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070034import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080035import android.text.TextUtils;
36import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070037import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010038import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080039import android.util.SparseArray;
40import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070041import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080042import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070043
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070044import androidx.annotation.Nullable;
45import androidx.annotation.VisibleForTesting;
46
Sunny Goyal68031ca2021-08-02 12:23:44 -070047import com.android.launcher3.model.DeviceGridState;
Sunny Goyalfd58da62020-08-11 12:06:49 -070048import com.android.launcher3.util.DisplayController;
49import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080050import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070051import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080052import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070053import com.android.launcher3.util.WindowBounds;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070054
Sunny Goyal819e1932016-07-07 16:43:58 -070055import org.xmlpull.v1.XmlPullParser;
56import org.xmlpull.v1.XmlPullParserException;
57
58import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070059import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070060import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080061import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070062import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070063
64public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070065
Hyunyoung Songc55a3502018-12-04 15:43:16 -080066 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070067 // We do not need any synchronization for this variable as its only written on UI thread.
68 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070069 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070070
Sunny Goyal19ff7282021-04-22 10:12:54 -070071 private static final int DEFAULT_TRUE = -1;
72 private static final int DEFAULT_SPLIT_DISPLAY = 2;
Thales Lima7ec83822021-08-05 13:32:10 +010073 private static final int GRID_ENABLED_ALL_DISPLAYS = 0;
74 private static final int GRID_ENABLED_SINGLE_DISPLAY = 1;
Sunny Goyal19ff7282021-04-22 10:12:54 -070075
Hyunyoung Songc55a3502018-12-04 15:43:16 -080076 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080077
Sunny Goyal53d7ee42015-05-22 12:25:45 -070078 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
79
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070080 // Constants that affects the interpolation curve between statically defined device profile
81 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080082 private static final float KNEARESTNEIGHBOR = 3;
83 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070084
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070085 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080086 private static final float WEIGHT_EFFICIENT = 100000f;
87
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070088 /**
89 * Number of icons per row and column in the workspace.
90 */
Adam Cohen2e6da152015-05-06 11:42:25 -070091 public int numRows;
92 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070093
94 /**
95 * Number of icons per row and column in the folder.
96 */
Adam Cohen2e6da152015-05-06 11:42:25 -070097 public int numFolderRows;
98 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070099 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700100 public float landscapeIconSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200101 public float twoPanelPortraitIconSize;
102 public float twoPanelLandscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200103 public float landscapeIconTextSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200104 public float twoPanelPortraitIconTextSize;
105 public float twoPanelLandscapeIconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700106 public int iconBitmapSize;
107 public int fillResIconDpi;
108 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000109 public float allAppsIconSize;
110 public float allAppsIconTextSize;
Thales Lima7ec83822021-08-05 13:32:10 +0100111 public boolean isSplitDisplay;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700112
Jon Mirandae126d722021-02-25 10:45:20 -0500113 public float minCellHeight;
114 public float minCellWidth;
Thales Limad1df5fc2021-09-03 18:37:19 +0100115 public float twoPanelPortraitMinCellHeightDps;
116 public float twoPanelPortraitMinCellWidthDps;
117 public float twoPanelLandscapeMinCellHeightDps;
118 public float twoPanelLandscapeMinCellWidthDps;
Jon Mirandae126d722021-02-25 10:45:20 -0500119 public float borderSpacing;
120
Sunny Goyal5bc18462019-01-07 15:13:39 -0800121 private SparseArray<TypedValue> mExtraAttrs;
122
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700123 /**
124 * Number of icons inside the hotseat area.
125 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700126 protected int numShownHotseatIcons;
127
128 /**
129 * Number of icons inside the hotseat area that is stored in the database. This is greater than
130 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
131 * sizes that share the same DB.
132 */
133 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700134
Jon Miranda6f7e9702019-09-16 14:44:14 -0700135 /**
136 * Number of columns in the all apps list.
137 */
138 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700139 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700140
Jon Mirandae126d722021-02-25 10:45:20 -0500141 /**
142 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
143 */
144 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400145 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500146
Tracy Zhou7df93d22020-01-27 13:44:06 -0800147 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800148 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700149 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700150
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000151 /**
152 * An immutable list of supported profiles.
153 */
154 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700155
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400156 @Nullable public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500157
Sunny Goyal6f866092016-03-17 17:04:15 -0700158 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700159 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700160
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700161 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700162
Sunny Goyalf633ef52018-03-13 09:57:05 -0700163 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700164 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700165
Sunny Goyalbbf01842015-10-08 07:41:15 -0700166 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700167 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700168 String gridName = getCurrentGridName(context);
169 String newGridName = initGrid(context, gridName);
170 if (!newGridName.equals(gridName)) {
171 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
172 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700173 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700174
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700175 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100176 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700177 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100178 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700179 }
180 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700181 }
182
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700183 /**
184 * This constructor should NOT have any monitors by design.
185 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800186 public InvariantDeviceProfile(Context context, String gridName) {
187 String newName = initGrid(context, gridName);
188 if (newName == null || !newName.equals(gridName)) {
189 throw new IllegalArgumentException("Unknown grid name");
190 }
191 }
192
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700193 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800194 * This constructor should NOT have any monitors by design.
195 */
196 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700197 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100198 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700199 String gridName = getCurrentGridName(context);
200
201 // Get the display info based on default display and interpolate it to existing display
202 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700203 DisplayController.INSTANCE.get(context).getInfo(),
Sunny Goyal19ff7282021-04-22 10:12:54 -0700204 getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700205
Alex Chau417bd722021-01-26 15:22:18 +0000206 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700207 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700208 myInfo, getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700209
210 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
211 .add(myDisplayOption);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700212 result.iconSizes[DisplayOption.INDEX_DEFAULT] =
213 defaultDisplayOption.iconSizes[DisplayOption.INDEX_DEFAULT];
214 for (int i = 1; i < DisplayOption.COUNT_TOTAL; i++) {
215 result.iconSizes[i] = Math.min(
216 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000217 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700218
Jon Mirandae126d722021-02-25 10:45:20 -0500219 result.minCellHeight = defaultDisplayOption.minCellHeight;
220 result.minCellWidth = defaultDisplayOption.minCellWidth;
221 result.borderSpacing = defaultDisplayOption.borderSpacing;
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
Tracy Zhou42255d22020-03-13 00:38:11 -0700226 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700227 return Utilities.isGridOptionsEnabled(context)
228 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700229 }
230
Sunny Goyaleff44f32019-01-09 17:29:49 -0800231 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700232 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Pat Manning67094bf2021-05-26 12:38:13 +0000233 // Each screen has two profiles (portrait/landscape), so devices with four or more
234 // supported profiles implies two or more internal displays.
235 boolean isSplitDisplay =
236 displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700237
238 ArrayList<DisplayOption> allOptions =
239 getPredefinedDeviceProfiles(context, gridName, isSplitDisplay);
240 DisplayOption displayOption =
241 invDistWeightedInterpolate(displayInfo, allOptions, isSplitDisplay);
242 initGrid(context, displayInfo, displayOption, isSplitDisplay);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700243 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800244 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700245
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700246 private void initGrid(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700247 Context context, Info displayInfo, DisplayOption displayOption,
248 boolean isSplitDisplay) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700249 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700250 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000251 numRows = closestProfile.numRows;
252 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000253 dbFile = closestProfile.dbFile;
254 defaultLayoutId = closestProfile.defaultLayoutId;
255 demoModeLayoutId = closestProfile.demoModeLayoutId;
256 numFolderRows = closestProfile.numFolderRows;
257 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500258 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400259 devicePaddingId = closestProfile.devicePaddingId;
Thales Lima7ec83822021-08-05 13:32:10 +0100260 this.isSplitDisplay = isSplitDisplay;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000261
262 mExtraAttrs = closestProfile.extraAttrs;
263
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700264 iconSize = displayOption.iconSizes[DisplayOption.INDEX_DEFAULT];
265 landscapeIconSize = displayOption.iconSizes[DisplayOption.INDEX_LANDSCAPE];
266 twoPanelPortraitIconSize = displayOption.iconSizes[DisplayOption.INDEX_TWO_PANEL_PORTRAIT];
267 twoPanelLandscapeIconSize =
268 displayOption.iconSizes[DisplayOption.INDEX_TWO_PANEL_LANDSCAPE];
Sunny Goyal35c7b192021-04-20 16:51:10 -0700269 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000270 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
271
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700272 iconTextSize = displayOption.textSizes[DisplayOption.INDEX_DEFAULT];
273 landscapeIconTextSize = displayOption.textSizes[DisplayOption.INDEX_LANDSCAPE];
274 twoPanelPortraitIconTextSize =
275 displayOption.textSizes[DisplayOption.INDEX_TWO_PANEL_PORTRAIT];
276 twoPanelLandscapeIconTextSize =
277 displayOption.textSizes[DisplayOption.INDEX_TWO_PANEL_LANDSCAPE];
278
Jon Mirandae126d722021-02-25 10:45:20 -0500279 minCellHeight = displayOption.minCellHeight;
280 minCellWidth = displayOption.minCellWidth;
Thales Limad1df5fc2021-09-03 18:37:19 +0100281 twoPanelPortraitMinCellHeightDps = displayOption.twoPanelPortraitMinCellHeightDps;
282 twoPanelPortraitMinCellWidthDps = displayOption.twoPanelPortraitMinCellWidthDps;
283 twoPanelLandscapeMinCellHeightDps = displayOption.twoPanelLandscapeMinCellHeightDps;
284 twoPanelLandscapeMinCellWidthDps = displayOption.twoPanelLandscapeMinCellWidthDps;
Jon Mirandae126d722021-02-25 10:45:20 -0500285 borderSpacing = displayOption.borderSpacing;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700286
287 numShownHotseatIcons = closestProfile.numHotseatIcons;
288 numDatabaseHotseatIcons = isSplitDisplay
289 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
290
291 numAllAppsColumns = closestProfile.numAllAppsColumns;
292 numDatabaseAllAppsColumns = isSplitDisplay
293 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500294
Tracy Zhouc6060e62020-04-27 13:05:34 -0700295 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700296 allAppsIconSize = displayOption.iconSizes[DisplayOption.INDEX_ALL_APPS];
297 allAppsIconTextSize = displayOption.textSizes[DisplayOption.INDEX_ALL_APPS];
Sunny Goyalae190ff2020-04-14 00:19:01 +0000298 } else {
299 allAppsIconSize = iconSize;
300 allAppsIconTextSize = iconTextSize;
301 }
302
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400303 if (devicePaddingId != 0) {
304 devicePaddings = new DevicePaddings(context, devicePaddingId);
305 }
306
Sunny Goyalae190ff2020-04-14 00:19:01 +0000307 // If the partner customization apk contains any grid overrides, apply them
308 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700309 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700310
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000311 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700312 defaultWallpaperSize = new Point(displayInfo.currentSize);
313 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000314 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700315 .setUseTwoPanels(isSplitDisplay)
316 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700317
Sunny Goyal19ff7282021-04-22 10:12:54 -0700318 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
319 int displayWidth = bounds.bounds.width();
320 int displayHeight = bounds.bounds.height();
321 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700322
Sunny Goyal19ff7282021-04-22 10:12:54 -0700323 // We need to ensure that there is enough extra space in the wallpaper
324 // for the intended parallax effects
325 float parallaxFactor =
326 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
327 ? 2
328 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
329 defaultWallpaperSize.x =
330 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700331 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000332 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700333
334 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
335 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700336 }
337
Sunny Goyal5bc18462019-01-07 15:13:39 -0800338 @Nullable
339 public TypedValue getAttrValue(int attr) {
340 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
341 }
342
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700343 public void addOnChangeListener(OnIDPChangeListener listener) {
344 mChangeListeners.add(listener);
345 }
346
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800347 public void removeOnChangeListener(OnIDPChangeListener listener) {
348 mChangeListeners.remove(listener);
349 }
350
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800351
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800352 public void setCurrentGrid(Context context, String gridName) {
353 Context appContext = context.getApplicationContext();
354 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700355 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800356 }
357
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700358 private Object[] toModelState() {
359 return new Object[] {
360 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
361 numDatabaseAllAppsColumns, dbFile};
362 }
363
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700364 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700365 Object[] oldState = toModelState();
366
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700367 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700368 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700369 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700370
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700371 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700372 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700373 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700374 }
375 }
376
Sunny Goyal19ff7282021-04-22 10:12:54 -0700377 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
378 Context context, String gridName, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800379 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700380 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
381 final int depth = parser.getDepth();
382 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700383 while (((type = parser.next()) != XmlPullParser.END_TAG ||
384 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800385 if ((type == XmlPullParser.START_TAG)
386 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800387
Thales Lima7ec83822021-08-05 13:32:10 +0100388 GridOption gridOption =
389 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
390 if (gridOption.isEnabled) {
391 final int displayDepth = parser.getDepth();
392 while (((type = parser.next()) != XmlPullParser.END_TAG
393 || parser.getDepth() > displayDepth)
394 && type != XmlPullParser.END_DOCUMENT) {
395 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
396 parser.getName())) {
397 profiles.add(new DisplayOption(gridOption, context,
398 Xml.asAttributeSet(parser),
399 isSplitDisplay ? DEFAULT_SPLIT_DISPLAY : DEFAULT_TRUE));
400 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800401 }
402 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700403 }
404 }
405 } catch (IOException|XmlPullParserException e) {
406 throw new RuntimeException(e);
407 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800408
Sunny Goyalae190ff2020-04-14 00:19:01 +0000409 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800410 if (!TextUtils.isEmpty(gridName)) {
411 for (DisplayOption option : profiles) {
Thales Lima7ec83822021-08-05 13:32:10 +0100412 if (gridName.equals(option.grid.name) && option.grid.isEnabled) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000413 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800414 }
415 }
416 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000417 if (filteredProfiles.isEmpty()) {
418 // No grid found, use the default options
419 for (DisplayOption option : profiles) {
420 if (option.canBeDefault) {
421 filteredProfiles.add(option);
422 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800423 }
424 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000425 if (filteredProfiles.isEmpty()) {
426 throw new RuntimeException("No display option with canBeDefault=true");
427 }
428 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700429 }
430
Thales Lima7ec83822021-08-05 13:32:10 +0100431 /**
432 * @return all the grid options that can be shown on the device
433 */
434 public List<GridOption> parseAllGridOptions(Context context) {
435 List<GridOption> result = new ArrayList<>();
436 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
437 final int depth = parser.getDepth();
438 int type;
439 while (((type = parser.next()) != XmlPullParser.END_TAG
440 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
441 if ((type == XmlPullParser.START_TAG)
442 && GridOption.TAG_NAME.equals(parser.getName())) {
443 GridOption option =
444 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
445 if (option.isEnabled) {
446 result.add(option);
447 }
448 }
449 }
450 } catch (IOException | XmlPullParserException e) {
451 Log.e(TAG, "Error parsing device profile", e);
452 return Collections.emptyList();
453 }
454 return result;
455 }
456
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700457 private int getLauncherIconDensity(int requiredSize) {
458 // Densities typically defined by an app.
459 int[] densityBuckets = new int[] {
460 DisplayMetrics.DENSITY_LOW,
461 DisplayMetrics.DENSITY_MEDIUM,
462 DisplayMetrics.DENSITY_TV,
463 DisplayMetrics.DENSITY_HIGH,
464 DisplayMetrics.DENSITY_XHIGH,
465 DisplayMetrics.DENSITY_XXHIGH,
466 DisplayMetrics.DENSITY_XXXHIGH
467 };
468
469 int density = DisplayMetrics.DENSITY_XXXHIGH;
470 for (int i = densityBuckets.length - 1; i >= 0; i--) {
471 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
472 / DisplayMetrics.DENSITY_DEFAULT;
473 if (expectedSize >= requiredSize) {
474 density = densityBuckets[i];
475 }
476 }
477
478 return density;
479 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700480
Adam Cohen2e6da152015-05-06 11:42:25 -0700481 /**
482 * Apply any Partner customization grid overrides.
483 *
484 * Currently we support: all apps row / column count.
485 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700486 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
487 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700488 if (p != null) {
489 p.applyInvariantDeviceProfileOverrides(this, dm);
490 }
491 }
492
Sunny Goyal415f1732018-11-29 10:33:47 -0800493 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700494 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700495 }
496
Sunny Goyal19ff7282021-04-22 10:12:54 -0700497 private static DisplayOption invDistWeightedInterpolate(
498 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
499 int minWidthPx = Integer.MAX_VALUE;
500 int minHeightPx = Integer.MAX_VALUE;
501 for (WindowBounds bounds : displayInfo.supportedBounds) {
502 boolean isTablet = displayInfo.isTablet(bounds);
503 if (isTablet && isSplitDisplay) {
504 // For split displays, take half width per page
505 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
506 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700507
Sunny Goyal19ff7282021-04-22 10:12:54 -0700508 } else if (!isTablet && bounds.isLandscape()) {
509 // We will use transposed layout in this case
510 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
511 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
512 } else {
513 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
514 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
515 }
516 }
517
518 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
519 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700520
521 // Sort the profiles based on the closeness to the device size
522 Collections.sort(points, (a, b) ->
523 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
524 dist(width, height, b.minWidthDps, b.minHeightDps)));
525
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700526 DisplayOption closestPoint = points.get(0);
527 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700528 float weights = 0;
529
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700530 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
531 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700532 }
533
534 DisplayOption out = new DisplayOption(closestOption);
535 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700536 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700537 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
538 weights += w;
539 out.add(new DisplayOption().add(p).multiply(w));
540 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700541 out.multiply(1.0f / weights);
542
543 // Since the bitmaps are persisted, ensure that the default bitmap size is same as
544 // predefined size to avoid cache invalidation
545 out.iconSizes[DisplayOption.INDEX_DEFAULT] =
546 closestPoint.iconSizes[DisplayOption.INDEX_DEFAULT];
547 for (int i = DisplayOption.INDEX_DEFAULT + 1; i < DisplayOption.COUNT_TOTAL; i++) {
548 out.iconSizes[i] = Math.min(out.iconSizes[i],
549 out.iconSizes[DisplayOption.INDEX_DEFAULT]);
550 }
551
552 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700553 }
554
Sunny Goyal27835952017-01-13 12:15:53 -0800555 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700556 Resources res = context.getResources();
557 Configuration config = context.getResources().getConfiguration();
558
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400559 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
560 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
561 return getBestMatch(screenWidth, screenHeight);
562 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700563
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400564 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700565 DeviceProfile bestMatch = supportedProfiles.get(0);
566 float minDiff = Float.MAX_VALUE;
567
568 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400569 float diff = Math.abs(profile.widthPx - screenWidth)
570 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700571 if (diff < minDiff) {
572 minDiff = diff;
573 bestMatch = profile;
574 }
575 }
576 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800577 }
578
Sunny Goyal415f1732018-11-29 10:33:47 -0800579 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700580 float d = dist(x0, y0, x1, y1);
581 if (Float.compare(d, 0f) == 0) {
582 return Float.POSITIVE_INFINITY;
583 }
584 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
585 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700586
587 /**
588 * As a ratio of screen height, the total distance we want the parallax effect to span
589 * horizontally
590 */
591 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
592 float aspectRatio = width / (float) height;
593
594 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
595 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
596 // We will use these two data points to extrapolate how much the wallpaper parallax effect
597 // to span (ie travel) at any aspect ratio:
598
599 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
600 final float ASPECT_RATIO_PORTRAIT = 10/16f;
601 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
602 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
603
604 // To find out the desired width at different aspect ratios, we use the following two
605 // formulas, where the coefficient on x is the aspect ratio (width/height):
606 // (16/10)x + y = 1.5
607 // (10/16)x + y = 1.2
608 // We solve for x and y and end up with a final formula:
609 final float x =
610 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
611 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
612 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
613 return x * aspectRatio + y;
614 }
615
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700616 public interface OnIDPChangeListener {
617
Sunny Goyalb47172b2021-05-03 19:59:51 -0700618 /**
619 * Called when the device provide changes
620 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700621 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700622 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800623
624
Sunny Goyaleff44f32019-01-09 17:29:49 -0800625 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800626
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800627 public static final String TAG_NAME = "grid-option";
628
Sunny Goyaleff44f32019-01-09 17:29:49 -0800629 public final String name;
630 public final int numRows;
631 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100632 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800633
634 private final int numFolderRows;
635 private final int numFolderColumns;
636
Sunny Goyal19ff7282021-04-22 10:12:54 -0700637 private final int numAllAppsColumns;
638 private final int numDatabaseAllAppsColumns;
639 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700640 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800641
Tracy Zhou7df93d22020-01-27 13:44:06 -0800642 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000643
Sunny Goyal415f1732018-11-29 10:33:47 -0800644 private final int defaultLayoutId;
645 private final int demoModeLayoutId;
646
Jon Mirandae126d722021-02-25 10:45:20 -0500647 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400648 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500649
Sunny Goyal5bc18462019-01-07 15:13:39 -0800650 private final SparseArray<TypedValue> extraAttrs;
651
Thales Lima7ec83822021-08-05 13:32:10 +0100652 public GridOption(Context context, AttributeSet attrs, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800653 TypedArray a = context.obtainStyledAttributes(
654 attrs, R.styleable.GridDisplayOption);
655 name = a.getString(R.styleable.GridDisplayOption_name);
656 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
657 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
658
Tracy Zhou7df93d22020-01-27 13:44:06 -0800659 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1e448462021-08-13 21:48:35 +0100660 defaultLayoutId = a.getResourceId(isSplitDisplay && a.hasValue(
661 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
662 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
663 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800664 demoModeLayoutId = a.getResourceId(
665 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700666
667 numAllAppsColumns = a.getInt(
668 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
669 numDatabaseAllAppsColumns = a.getInt(
670 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
671
672 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800673 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700674 numDatabaseHotseatIcons = a.getInt(
675 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
676
Sunny Goyal415f1732018-11-29 10:33:47 -0800677 numFolderRows = a.getInt(
678 R.styleable.GridDisplayOption_numFolderRows, numRows);
679 numFolderColumns = a.getInt(
680 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700681
Jon Mirandae126d722021-02-25 10:45:20 -0500682 isScalable = a.getBoolean(
683 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400684 devicePaddingId = a.getResourceId(
685 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500686
Thales Lima7ec83822021-08-05 13:32:10 +0100687 final int enabledInt =
688 a.getInteger(R.styleable.GridDisplayOption_gridEnabled,
689 GRID_ENABLED_ALL_DISPLAYS);
690 isEnabled = enabledInt == GRID_ENABLED_ALL_DISPLAYS
691 || enabledInt == GRID_ENABLED_SINGLE_DISPLAY && !isSplitDisplay;
692
Sunny Goyal415f1732018-11-29 10:33:47 -0800693 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800694 extraAttrs = Themes.createValueMap(context, attrs,
695 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800696 }
697 }
698
Sunny Goyal19ff7282021-04-22 10:12:54 -0700699 @VisibleForTesting
700 static final class DisplayOption {
701
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700702 static final int INDEX_DEFAULT = 0;
703 static final int INDEX_LANDSCAPE = 1;
704 static final int INDEX_ALL_APPS = 2;
705 static final int INDEX_TWO_PANEL_PORTRAIT = 3;
706 static final int INDEX_TWO_PANEL_LANDSCAPE = 4;
707
708 static final int COUNT_TOTAL = 5;
709
Sunny Goyal19ff7282021-04-22 10:12:54 -0700710 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800711
Sunny Goyal415f1732018-11-29 10:33:47 -0800712 private final float minWidthDps;
713 private final float minHeightDps;
714 private final boolean canBeDefault;
715
Jon Mirandae126d722021-02-25 10:45:20 -0500716 private float minCellHeight;
717 private float minCellWidth;
Thales Limad1df5fc2021-09-03 18:37:19 +0100718 private float twoPanelPortraitMinCellHeightDps;
719 private float twoPanelPortraitMinCellWidthDps;
720 private float twoPanelLandscapeMinCellHeightDps;
721 private float twoPanelLandscapeMinCellWidthDps;
Jon Mirandae126d722021-02-25 10:45:20 -0500722 private float borderSpacing;
723
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700724 private final float[] iconSizes = new float[COUNT_TOTAL];
725 private final float[] textSizes = new float[COUNT_TOTAL];
Sunny Goyal415f1732018-11-29 10:33:47 -0800726
Sunny Goyal19ff7282021-04-22 10:12:54 -0700727 DisplayOption(GridOption grid, Context context, AttributeSet attrs, int defaultFlagValue) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800728 this.grid = grid;
729
730 TypedArray a = context.obtainStyledAttributes(
731 attrs, R.styleable.ProfileDisplayOption);
732
Sunny Goyal415f1732018-11-29 10:33:47 -0800733 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
734 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700735
736 canBeDefault = a.getInt(R.styleable.ProfileDisplayOption_canBeDefault, 0)
737 == defaultFlagValue;
Sunny Goyal415f1732018-11-29 10:33:47 -0800738
Jon Mirandae126d722021-02-25 10:45:20 -0500739 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
740 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
Thales Limad1df5fc2021-09-03 18:37:19 +0100741 twoPanelPortraitMinCellHeightDps = a.getFloat(
742 R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellHeightDps,
743 minCellHeight);
744 twoPanelPortraitMinCellWidthDps = a.getFloat(
745 R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellWidthDps, minCellWidth);
746 twoPanelLandscapeMinCellHeightDps = a.getFloat(
747 R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellHeightDps,
748 twoPanelPortraitMinCellHeightDps);
749 twoPanelLandscapeMinCellWidthDps = a.getFloat(
750 R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellWidthDps,
751 twoPanelPortraitMinCellWidthDps);
Jon Mirandae126d722021-02-25 10:45:20 -0500752 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
753
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700754 iconSizes[INDEX_DEFAULT] =
755 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
756 iconSizes[INDEX_LANDSCAPE] =
757 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
758 iconSizes[INDEX_DEFAULT]);
759 iconSizes[INDEX_ALL_APPS] =
760 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
761 iconSizes[INDEX_DEFAULT]);
762 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
763 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconSize,
764 iconSizes[INDEX_DEFAULT]);
765 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
766 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconSize,
767 iconSizes[INDEX_LANDSCAPE]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700768
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700769 textSizes[INDEX_DEFAULT] =
770 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
771 textSizes[INDEX_LANDSCAPE] =
772 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconTextSize,
773 textSizes[INDEX_DEFAULT]);
774 textSizes[INDEX_ALL_APPS] =
775 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
776 textSizes[INDEX_DEFAULT]);
777 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
778 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconTextSize,
779 textSizes[INDEX_DEFAULT]);
780 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
781 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconTextSize,
782 textSizes[INDEX_LANDSCAPE]);
783
Sunny Goyal415f1732018-11-29 10:33:47 -0800784 a.recycle();
785 }
786
787 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700788 this(null);
789 }
790
791 DisplayOption(GridOption grid) {
792 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800793 minWidthDps = 0;
794 minHeightDps = 0;
795 canBeDefault = false;
Jon Mirandae126d722021-02-25 10:45:20 -0500796 minCellHeight = 0;
797 minCellWidth = 0;
798 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800799 }
800
801 private DisplayOption multiply(float w) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700802 for (int i = 0; i < COUNT_TOTAL; i++) {
803 iconSizes[i] *= w;
804 textSizes[i] *= w;
805 }
Jon Mirandae126d722021-02-25 10:45:20 -0500806 minCellHeight *= w;
807 minCellWidth *= w;
Thales Limad1df5fc2021-09-03 18:37:19 +0100808 twoPanelPortraitMinCellHeightDps *= w;
809 twoPanelPortraitMinCellWidthDps *= w;
810 twoPanelLandscapeMinCellHeightDps *= w;
811 twoPanelLandscapeMinCellWidthDps *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500812 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800813 return this;
814 }
815
816 private DisplayOption add(DisplayOption p) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700817 for (int i = 0; i < COUNT_TOTAL; i++) {
818 iconSizes[i] += p.iconSizes[i];
819 textSizes[i] += p.textSizes[i];
820 }
Jon Mirandae126d722021-02-25 10:45:20 -0500821 minCellHeight += p.minCellHeight;
822 minCellWidth += p.minCellWidth;
Thales Limad1df5fc2021-09-03 18:37:19 +0100823 twoPanelPortraitMinCellHeightDps += p.twoPanelPortraitMinCellHeightDps;
824 twoPanelPortraitMinCellWidthDps += p.twoPanelPortraitMinCellWidthDps;
825 twoPanelLandscapeMinCellHeightDps += p.twoPanelLandscapeMinCellHeightDps;
826 twoPanelLandscapeMinCellWidthDps += p.twoPanelLandscapeMinCellWidthDps;
Jon Mirandae126d722021-02-25 10:45:20 -0500827 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800828 return this;
829 }
830 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000831}