blob: 1fc895822f0492553535f4c301df49c017dcb914 [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 Goyal6d55f662019-01-02 12:13:43 -080060import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070061import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070062
63public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070064
Hyunyoung Songc55a3502018-12-04 15:43:16 -080065 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070066 // We do not need any synchronization for this variable as its only written on UI thread.
67 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070068 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070069
Sunny Goyal19ff7282021-04-22 10:12:54 -070070 private static final int DEFAULT_TRUE = -1;
71 private static final int DEFAULT_SPLIT_DISPLAY = 2;
Thales Lima7ec83822021-08-05 13:32:10 +010072 private static final int GRID_ENABLED_ALL_DISPLAYS = 0;
73 private static final int GRID_ENABLED_SINGLE_DISPLAY = 1;
Sunny Goyal19ff7282021-04-22 10:12:54 -070074
Hyunyoung Songc55a3502018-12-04 15:43:16 -080075 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080076
Sunny Goyal53d7ee42015-05-22 12:25:45 -070077 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
78
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070079 // Constants that affects the interpolation curve between statically defined device profile
80 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080081 private static final float KNEARESTNEIGHBOR = 3;
82 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070083
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070084 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080085 private static final float WEIGHT_EFFICIENT = 100000f;
86
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070087 /**
88 * Number of icons per row and column in the workspace.
89 */
Adam Cohen2e6da152015-05-06 11:42:25 -070090 public int numRows;
91 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070092
93 /**
94 * Number of icons per row and column in the folder.
95 */
Adam Cohen2e6da152015-05-06 11:42:25 -070096 public int numFolderRows;
97 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070098 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070099 public float landscapeIconSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200100 public float twoPanelPortraitIconSize;
101 public float twoPanelLandscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200102 public float landscapeIconTextSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200103 public float twoPanelPortraitIconTextSize;
104 public float twoPanelLandscapeIconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700105 public int iconBitmapSize;
106 public int fillResIconDpi;
107 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000108 public float allAppsIconSize;
109 public float allAppsIconTextSize;
Thales Lima7ec83822021-08-05 13:32:10 +0100110 public boolean isSplitDisplay;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700111
Jon Mirandae126d722021-02-25 10:45:20 -0500112 public float minCellHeight;
113 public float minCellWidth;
114 public float borderSpacing;
115
Sunny Goyal5bc18462019-01-07 15:13:39 -0800116 private SparseArray<TypedValue> mExtraAttrs;
117
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700118 /**
119 * Number of icons inside the hotseat area.
120 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700121 protected int numShownHotseatIcons;
122
123 /**
124 * Number of icons inside the hotseat area that is stored in the database. This is greater than
125 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
126 * sizes that share the same DB.
127 */
128 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700129
Jon Miranda6f7e9702019-09-16 14:44:14 -0700130 /**
131 * Number of columns in the all apps list.
132 */
133 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700134 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700135
Jon Mirandae126d722021-02-25 10:45:20 -0500136 /**
137 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
138 */
139 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400140 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500141
Tracy Zhou7df93d22020-01-27 13:44:06 -0800142 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800143 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700144 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700145
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000146 /**
147 * An immutable list of supported profiles.
148 */
149 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700150
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400151 @Nullable public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500152
Sunny Goyal6f866092016-03-17 17:04:15 -0700153 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700154 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700155
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700156 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700157
Sunny Goyalf633ef52018-03-13 09:57:05 -0700158 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700159 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700160
Sunny Goyalf633ef52018-03-13 09:57:05 -0700161 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800162 numRows = p.numRows;
163 numColumns = p.numColumns;
164 numFolderRows = p.numFolderRows;
165 numFolderColumns = p.numFolderColumns;
166 iconSize = p.iconSize;
167 landscapeIconSize = p.landscapeIconSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200168 twoPanelPortraitIconSize = p.twoPanelPortraitIconSize;
169 twoPanelLandscapeIconSize = p.twoPanelLandscapeIconSize;
Tony Wickham9459f312020-06-15 13:30:20 -0500170 iconBitmapSize = p.iconBitmapSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800171 iconTextSize = p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200172 landscapeIconTextSize = p.landscapeIconTextSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200173 twoPanelPortraitIconTextSize = p.twoPanelPortraitIconTextSize;
174 twoPanelLandscapeIconTextSize = p.twoPanelLandscapeIconTextSize;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700175 numShownHotseatIcons = p.numShownHotseatIcons;
176 numDatabaseHotseatIcons = p.numDatabaseHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700177 numAllAppsColumns = p.numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700178 numDatabaseAllAppsColumns = p.numDatabaseAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500179 isScalable = p.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400180 devicePaddingId = p.devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500181 minCellHeight = p.minCellHeight;
182 minCellWidth = p.minCellWidth;
183 borderSpacing = p.borderSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800184 dbFile = p.dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000185 allAppsIconSize = p.allAppsIconSize;
186 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800187 defaultLayoutId = p.defaultLayoutId;
188 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800189 mExtraAttrs = p.mExtraAttrs;
Jon Miranda228877d2021-02-09 11:05:00 -0500190 devicePaddings = p.devicePaddings;
Adam Cohen2e6da152015-05-06 11:42:25 -0700191 }
192
Sunny Goyalbbf01842015-10-08 07:41:15 -0700193 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700194 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700195 String gridName = getCurrentGridName(context);
196 String newGridName = initGrid(context, gridName);
197 if (!newGridName.equals(gridName)) {
198 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
199 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700200 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700201
Sunny Goyal35c7b192021-04-20 16:51:10 -0700202 DisplayController.INSTANCE.get(context).addChangeListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100203 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700204 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100205 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700206 }
207 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700208 }
209
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700210 /**
211 * This constructor should NOT have any monitors by design.
212 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800213 public InvariantDeviceProfile(Context context, String gridName) {
214 String newName = initGrid(context, gridName);
215 if (newName == null || !newName.equals(gridName)) {
216 throw new IllegalArgumentException("Unknown grid name");
217 }
218 }
219
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700220 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800221 * This constructor should NOT have any monitors by design.
222 */
223 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700224 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100225 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700226 String gridName = getCurrentGridName(context);
227
228 // Get the display info based on default display and interpolate it to existing display
229 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700230 DisplayController.INSTANCE.get(context).getInfo(),
Sunny Goyal19ff7282021-04-22 10:12:54 -0700231 getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700232
Alex Chau417bd722021-01-26 15:22:18 +0000233 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700234 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700235 myInfo, getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700236
237 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
238 .add(myDisplayOption);
239 result.iconSize = defaultDisplayOption.iconSize;
240 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
Alex Chau7c439722021-03-24 11:32:44 +0000241 if (defaultDisplayOption.allAppsIconSize < myDisplayOption.allAppsIconSize) {
242 result.allAppsIconSize = defaultDisplayOption.allAppsIconSize;
Alex Chau7c439722021-03-24 11:32:44 +0000243 } else {
244 result.allAppsIconSize = myDisplayOption.allAppsIconSize;
Alex Chau7c439722021-03-24 11:32:44 +0000245 }
Jon Mirandae126d722021-02-25 10:45:20 -0500246 result.minCellHeight = defaultDisplayOption.minCellHeight;
247 result.minCellWidth = defaultDisplayOption.minCellWidth;
248 result.borderSpacing = defaultDisplayOption.borderSpacing;
Jon Miranda228877d2021-02-09 11:05:00 -0500249
Sunny Goyal19ff7282021-04-22 10:12:54 -0700250 initGrid(context, myInfo, result, false);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800251 }
252
Tracy Zhou42255d22020-03-13 00:38:11 -0700253 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700254 return Utilities.isGridOptionsEnabled(context)
255 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700256 }
257
Sunny Goyaleff44f32019-01-09 17:29:49 -0800258 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700259 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Pat Manning67094bf2021-05-26 12:38:13 +0000260 // Each screen has two profiles (portrait/landscape), so devices with four or more
261 // supported profiles implies two or more internal displays.
262 boolean isSplitDisplay =
263 displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700264
265 ArrayList<DisplayOption> allOptions =
266 getPredefinedDeviceProfiles(context, gridName, isSplitDisplay);
267 DisplayOption displayOption =
268 invDistWeightedInterpolate(displayInfo, allOptions, isSplitDisplay);
269 initGrid(context, displayInfo, displayOption, isSplitDisplay);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700270 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800271 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700272
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700273 private void initGrid(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700274 Context context, Info displayInfo, DisplayOption displayOption,
275 boolean isSplitDisplay) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700276 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700277 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000278 numRows = closestProfile.numRows;
279 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000280 dbFile = closestProfile.dbFile;
281 defaultLayoutId = closestProfile.defaultLayoutId;
282 demoModeLayoutId = closestProfile.demoModeLayoutId;
283 numFolderRows = closestProfile.numFolderRows;
284 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500285 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400286 devicePaddingId = closestProfile.devicePaddingId;
Thales Lima7ec83822021-08-05 13:32:10 +0100287 this.isSplitDisplay = isSplitDisplay;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000288
289 mExtraAttrs = closestProfile.extraAttrs;
290
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700291 iconSize = displayOption.iconSize;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700292 landscapeIconSize = displayOption.landscapeIconSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200293 twoPanelPortraitIconSize = displayOption.twoPanelPortraitIconSize;
294 twoPanelLandscapeIconSize = displayOption.twoPanelLandscapeIconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700295 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700296 iconTextSize = displayOption.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200297 landscapeIconTextSize = displayOption.landscapeIconTextSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200298 twoPanelPortraitIconTextSize = displayOption.twoPanelPortraitIconTextSize;
299 twoPanelLandscapeIconTextSize = displayOption.twoPanelLandscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000300 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
301
Jon Mirandae126d722021-02-25 10:45:20 -0500302 minCellHeight = displayOption.minCellHeight;
303 minCellWidth = displayOption.minCellWidth;
304 borderSpacing = displayOption.borderSpacing;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700305
306 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
Tracy Zhouc6060e62020-04-27 13:05:34 -0700314 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700315 allAppsIconSize = displayOption.allAppsIconSize;
316 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000317 } else {
318 allAppsIconSize = iconSize;
319 allAppsIconTextSize = iconTextSize;
320 }
321
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400322 if (devicePaddingId != 0) {
323 devicePaddings = new DevicePaddings(context, devicePaddingId);
324 }
325
Sunny Goyalae190ff2020-04-14 00:19:01 +0000326 // If the partner customization apk contains any grid overrides, apply them
327 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700328 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700329
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000330 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700331 defaultWallpaperSize = new Point(displayInfo.currentSize);
332 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000333 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700334 .setUseTwoPanels(isSplitDisplay)
335 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700336
Sunny Goyal19ff7282021-04-22 10:12:54 -0700337 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
338 int displayWidth = bounds.bounds.width();
339 int displayHeight = bounds.bounds.height();
340 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700341
Sunny Goyal19ff7282021-04-22 10:12:54 -0700342 // We need to ensure that there is enough extra space in the wallpaper
343 // for the intended parallax effects
344 float parallaxFactor =
345 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
346 ? 2
347 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
348 defaultWallpaperSize.x =
349 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700350 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000351 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700352
353 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
354 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700355 }
356
Sunny Goyal5bc18462019-01-07 15:13:39 -0800357 @Nullable
358 public TypedValue getAttrValue(int attr) {
359 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
360 }
361
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700362 public void addOnChangeListener(OnIDPChangeListener listener) {
363 mChangeListeners.add(listener);
364 }
365
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800366 public void removeOnChangeListener(OnIDPChangeListener listener) {
367 mChangeListeners.remove(listener);
368 }
369
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800370
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800371 public void setCurrentGrid(Context context, String gridName) {
372 Context appContext = context.getApplicationContext();
373 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700374 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800375 }
376
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700377 private void onConfigChanged(Context context) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700378 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700379 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700380 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700381
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700382 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyalb47172b2021-05-03 19:59:51 -0700383 listener.onIdpChanged(this);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700384 }
385 }
386
Sunny Goyal19ff7282021-04-22 10:12:54 -0700387 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
388 Context context, String gridName, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800389 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700390 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
391 final int depth = parser.getDepth();
392 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700393 while (((type = parser.next()) != XmlPullParser.END_TAG ||
394 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800395 if ((type == XmlPullParser.START_TAG)
396 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800397
Thales Lima7ec83822021-08-05 13:32:10 +0100398 GridOption gridOption =
399 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
400 if (gridOption.isEnabled) {
401 final int displayDepth = parser.getDepth();
402 while (((type = parser.next()) != XmlPullParser.END_TAG
403 || parser.getDepth() > displayDepth)
404 && type != XmlPullParser.END_DOCUMENT) {
405 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
406 parser.getName())) {
407 profiles.add(new DisplayOption(gridOption, context,
408 Xml.asAttributeSet(parser),
409 isSplitDisplay ? DEFAULT_SPLIT_DISPLAY : DEFAULT_TRUE));
410 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800411 }
412 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700413 }
414 }
415 } catch (IOException|XmlPullParserException e) {
416 throw new RuntimeException(e);
417 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800418
Sunny Goyalae190ff2020-04-14 00:19:01 +0000419 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800420 if (!TextUtils.isEmpty(gridName)) {
421 for (DisplayOption option : profiles) {
Thales Lima7ec83822021-08-05 13:32:10 +0100422 if (gridName.equals(option.grid.name) && option.grid.isEnabled) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000423 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800424 }
425 }
426 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000427 if (filteredProfiles.isEmpty()) {
428 // No grid found, use the default options
429 for (DisplayOption option : profiles) {
430 if (option.canBeDefault) {
431 filteredProfiles.add(option);
432 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800433 }
434 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000435 if (filteredProfiles.isEmpty()) {
436 throw new RuntimeException("No display option with canBeDefault=true");
437 }
438 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700439 }
440
Thales Lima7ec83822021-08-05 13:32:10 +0100441 /**
442 * @return all the grid options that can be shown on the device
443 */
444 public List<GridOption> parseAllGridOptions(Context context) {
445 List<GridOption> result = new ArrayList<>();
446 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
447 final int depth = parser.getDepth();
448 int type;
449 while (((type = parser.next()) != XmlPullParser.END_TAG
450 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
451 if ((type == XmlPullParser.START_TAG)
452 && GridOption.TAG_NAME.equals(parser.getName())) {
453 GridOption option =
454 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
455 if (option.isEnabled) {
456 result.add(option);
457 }
458 }
459 }
460 } catch (IOException | XmlPullParserException e) {
461 Log.e(TAG, "Error parsing device profile", e);
462 return Collections.emptyList();
463 }
464 return result;
465 }
466
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700467 private int getLauncherIconDensity(int requiredSize) {
468 // Densities typically defined by an app.
469 int[] densityBuckets = new int[] {
470 DisplayMetrics.DENSITY_LOW,
471 DisplayMetrics.DENSITY_MEDIUM,
472 DisplayMetrics.DENSITY_TV,
473 DisplayMetrics.DENSITY_HIGH,
474 DisplayMetrics.DENSITY_XHIGH,
475 DisplayMetrics.DENSITY_XXHIGH,
476 DisplayMetrics.DENSITY_XXXHIGH
477 };
478
479 int density = DisplayMetrics.DENSITY_XXXHIGH;
480 for (int i = densityBuckets.length - 1; i >= 0; i--) {
481 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
482 / DisplayMetrics.DENSITY_DEFAULT;
483 if (expectedSize >= requiredSize) {
484 density = densityBuckets[i];
485 }
486 }
487
488 return density;
489 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700490
Adam Cohen2e6da152015-05-06 11:42:25 -0700491 /**
492 * Apply any Partner customization grid overrides.
493 *
494 * Currently we support: all apps row / column count.
495 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700496 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
497 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700498 if (p != null) {
499 p.applyInvariantDeviceProfileOverrides(this, dm);
500 }
501 }
502
Sunny Goyal415f1732018-11-29 10:33:47 -0800503 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700504 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700505 }
506
Sunny Goyal19ff7282021-04-22 10:12:54 -0700507 private static DisplayOption invDistWeightedInterpolate(
508 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
509 int minWidthPx = Integer.MAX_VALUE;
510 int minHeightPx = Integer.MAX_VALUE;
511 for (WindowBounds bounds : displayInfo.supportedBounds) {
512 boolean isTablet = displayInfo.isTablet(bounds);
513 if (isTablet && isSplitDisplay) {
514 // For split displays, take half width per page
515 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
516 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700517
Sunny Goyal19ff7282021-04-22 10:12:54 -0700518 } else if (!isTablet && bounds.isLandscape()) {
519 // We will use transposed layout in this case
520 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
521 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
522 } else {
523 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
524 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
525 }
526 }
527
528 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
529 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700530
531 // Sort the profiles based on the closeness to the device size
532 Collections.sort(points, (a, b) ->
533 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
534 dist(width, height, b.minWidthDps, b.minHeightDps)));
535
536 GridOption closestOption = points.get(0).grid;
537 float weights = 0;
538
539 DisplayOption p = points.get(0);
540 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
541 return p;
542 }
543
544 DisplayOption out = new DisplayOption(closestOption);
545 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
546 p = points.get(i);
547 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
548 weights += w;
549 out.add(new DisplayOption().add(p).multiply(w));
550 }
551 return out.multiply(1.0f / weights);
552 }
553
Sunny Goyal27835952017-01-13 12:15:53 -0800554 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700555 Resources res = context.getResources();
556 Configuration config = context.getResources().getConfiguration();
557
558 float availableWidth = config.screenWidthDp * res.getDisplayMetrics().density;
559 float availableHeight = config.screenHeightDp * res.getDisplayMetrics().density;
560
Sunny Goyal19ff7282021-04-22 10:12:54 -0700561 DeviceProfile bestMatch = supportedProfiles.get(0);
562 float minDiff = Float.MAX_VALUE;
563
564 for (DeviceProfile profile : supportedProfiles) {
565 float diff = Math.abs(profile.availableWidthPx - availableWidth)
566 + Math.abs(profile.availableHeightPx - availableHeight);
567 if (diff < minDiff) {
568 minDiff = diff;
569 bestMatch = profile;
570 }
571 }
572 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800573 }
574
Sunny Goyal415f1732018-11-29 10:33:47 -0800575 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700576 float d = dist(x0, y0, x1, y1);
577 if (Float.compare(d, 0f) == 0) {
578 return Float.POSITIVE_INFINITY;
579 }
580 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
581 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700582
583 /**
584 * As a ratio of screen height, the total distance we want the parallax effect to span
585 * horizontally
586 */
587 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
588 float aspectRatio = width / (float) height;
589
590 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
591 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
592 // We will use these two data points to extrapolate how much the wallpaper parallax effect
593 // to span (ie travel) at any aspect ratio:
594
595 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
596 final float ASPECT_RATIO_PORTRAIT = 10/16f;
597 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
598 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
599
600 // To find out the desired width at different aspect ratios, we use the following two
601 // formulas, where the coefficient on x is the aspect ratio (width/height):
602 // (16/10)x + y = 1.5
603 // (10/16)x + y = 1.2
604 // We solve for x and y and end up with a final formula:
605 final float x =
606 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
607 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
608 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
609 return x * aspectRatio + y;
610 }
611
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700612 public interface OnIDPChangeListener {
613
Sunny Goyalb47172b2021-05-03 19:59:51 -0700614 /**
615 * Called when the device provide changes
616 */
617 void onIdpChanged(InvariantDeviceProfile profile);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700618 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800619
620
Sunny Goyaleff44f32019-01-09 17:29:49 -0800621 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800622
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800623 public static final String TAG_NAME = "grid-option";
624
Sunny Goyaleff44f32019-01-09 17:29:49 -0800625 public final String name;
626 public final int numRows;
627 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100628 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800629
630 private final int numFolderRows;
631 private final int numFolderColumns;
632
Sunny Goyal19ff7282021-04-22 10:12:54 -0700633 private final int numAllAppsColumns;
634 private final int numDatabaseAllAppsColumns;
635 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700636 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800637
Tracy Zhou7df93d22020-01-27 13:44:06 -0800638 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000639
Sunny Goyal415f1732018-11-29 10:33:47 -0800640 private final int defaultLayoutId;
641 private final int demoModeLayoutId;
642
Jon Mirandae126d722021-02-25 10:45:20 -0500643 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400644 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500645
Sunny Goyal5bc18462019-01-07 15:13:39 -0800646 private final SparseArray<TypedValue> extraAttrs;
647
Thales Lima7ec83822021-08-05 13:32:10 +0100648 public GridOption(Context context, AttributeSet attrs, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800649 TypedArray a = context.obtainStyledAttributes(
650 attrs, R.styleable.GridDisplayOption);
651 name = a.getString(R.styleable.GridDisplayOption_name);
652 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
653 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
654
Tracy Zhou7df93d22020-01-27 13:44:06 -0800655 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800656 defaultLayoutId = a.getResourceId(
657 R.styleable.GridDisplayOption_defaultLayoutId, 0);
658 demoModeLayoutId = a.getResourceId(
659 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700660
661 numAllAppsColumns = a.getInt(
662 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
663 numDatabaseAllAppsColumns = a.getInt(
664 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
665
666 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800667 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700668 numDatabaseHotseatIcons = a.getInt(
669 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
670
Sunny Goyal415f1732018-11-29 10:33:47 -0800671 numFolderRows = a.getInt(
672 R.styleable.GridDisplayOption_numFolderRows, numRows);
673 numFolderColumns = a.getInt(
674 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700675
Jon Mirandae126d722021-02-25 10:45:20 -0500676 isScalable = a.getBoolean(
677 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400678 devicePaddingId = a.getResourceId(
679 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500680
Thales Lima7ec83822021-08-05 13:32:10 +0100681 final int enabledInt =
682 a.getInteger(R.styleable.GridDisplayOption_gridEnabled,
683 GRID_ENABLED_ALL_DISPLAYS);
684 isEnabled = enabledInt == GRID_ENABLED_ALL_DISPLAYS
685 || enabledInt == GRID_ENABLED_SINGLE_DISPLAY && !isSplitDisplay;
686
Sunny Goyal415f1732018-11-29 10:33:47 -0800687 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800688 extraAttrs = Themes.createValueMap(context, attrs,
689 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800690 }
691 }
692
Sunny Goyal19ff7282021-04-22 10:12:54 -0700693 @VisibleForTesting
694 static final class DisplayOption {
695
696 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800697
Sunny Goyal415f1732018-11-29 10:33:47 -0800698 private final float minWidthDps;
699 private final float minHeightDps;
700 private final boolean canBeDefault;
701
Jon Mirandae126d722021-02-25 10:45:20 -0500702 private float minCellHeight;
703 private float minCellWidth;
704 private float borderSpacing;
705
Sunny Goyal415f1732018-11-29 10:33:47 -0800706 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800707 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700708 private float landscapeIconSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200709 private float twoPanelPortraitIconSize;
710 private float twoPanelLandscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200711 private float landscapeIconTextSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200712 private float twoPanelPortraitIconTextSize;
713 private float twoPanelLandscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000714 private float allAppsIconSize;
715 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800716
Sunny Goyal19ff7282021-04-22 10:12:54 -0700717 DisplayOption(GridOption grid, Context context, AttributeSet attrs, int defaultFlagValue) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800718 this.grid = grid;
719
720 TypedArray a = context.obtainStyledAttributes(
721 attrs, R.styleable.ProfileDisplayOption);
722
Sunny Goyal415f1732018-11-29 10:33:47 -0800723 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
724 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700725
726 canBeDefault = a.getInt(R.styleable.ProfileDisplayOption_canBeDefault, 0)
727 == defaultFlagValue;
Sunny Goyal415f1732018-11-29 10:33:47 -0800728
Jon Mirandae126d722021-02-25 10:45:20 -0500729 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
730 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
731 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
732
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700733 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800734 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
735 iconSize);
Andras Kloczl30fe9152021-08-05 18:02:29 +0200736 twoPanelPortraitIconSize = a.getFloat(
737 R.styleable.ProfileDisplayOption_twoPanelPortraitIconSize, iconSize);
738 twoPanelLandscapeIconSize = a.getFloat(
739 R.styleable.ProfileDisplayOption_twoPanelLandscapeIconSize,
740 landscapeIconSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800741 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Andras Kloczl6057cb92021-04-07 13:45:17 +0200742 landscapeIconTextSize = a.getFloat(
743 R.styleable.ProfileDisplayOption_landscapeIconTextSize, iconTextSize);
Andras Kloczl30fe9152021-08-05 18:02:29 +0200744 twoPanelPortraitIconTextSize = a.getFloat(
745 R.styleable.ProfileDisplayOption_twoPanelPortraitIconTextSize, iconTextSize);
746 twoPanelLandscapeIconTextSize = a.getFloat(
747 R.styleable.ProfileDisplayOption_twoPanelLandscapeIconTextSize,
748 landscapeIconTextSize);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700749
Sunny Goyalae190ff2020-04-14 00:19:01 +0000750 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
751 iconSize);
752 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
753 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800754 a.recycle();
755 }
756
757 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700758 this(null);
759 }
760
761 DisplayOption(GridOption grid) {
762 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800763 minWidthDps = 0;
764 minHeightDps = 0;
765 canBeDefault = false;
Jon Mirandae126d722021-02-25 10:45:20 -0500766 minCellHeight = 0;
767 minCellWidth = 0;
768 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800769 }
770
771 private DisplayOption multiply(float w) {
772 iconSize *= w;
773 landscapeIconSize *= w;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200774 twoPanelPortraitIconSize *= w;
775 twoPanelLandscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000776 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800777 iconTextSize *= w;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200778 landscapeIconTextSize *= w;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200779 twoPanelPortraitIconTextSize *= w;
780 twoPanelLandscapeIconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000781 allAppsIconTextSize *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500782 minCellHeight *= w;
783 minCellWidth *= w;
784 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800785 return this;
786 }
787
788 private DisplayOption add(DisplayOption p) {
789 iconSize += p.iconSize;
790 landscapeIconSize += p.landscapeIconSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200791 twoPanelPortraitIconSize += p.twoPanelPortraitIconSize;
792 twoPanelLandscapeIconSize += p.twoPanelLandscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000793 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800794 iconTextSize += p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200795 landscapeIconTextSize += p.landscapeIconTextSize;
Andras Kloczl30fe9152021-08-05 18:02:29 +0200796 twoPanelPortraitIconTextSize += p.twoPanelPortraitIconTextSize;
797 twoPanelLandscapeIconTextSize += p.twoPanelLandscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000798 allAppsIconTextSize += p.allAppsIconTextSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500799 minCellHeight += p.minCellHeight;
800 minCellWidth += p.minCellWidth;
801 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800802 return this;
803 }
804 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000805}