blob: 244cb59d80de424e9d332966442979e13fbf2aab [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;
115 public float borderSpacing;
116
Sunny Goyal5bc18462019-01-07 15:13:39 -0800117 private SparseArray<TypedValue> mExtraAttrs;
118
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700119 /**
120 * Number of icons inside the hotseat area.
121 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700122 protected int numShownHotseatIcons;
123
124 /**
125 * Number of icons inside the hotseat area that is stored in the database. This is greater than
126 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
127 * sizes that share the same DB.
128 */
129 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700130
Jon Miranda6f7e9702019-09-16 14:44:14 -0700131 /**
132 * Number of columns in the all apps list.
133 */
134 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700135 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700136
Jon Mirandae126d722021-02-25 10:45:20 -0500137 /**
138 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
139 */
140 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400141 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500142
Tracy Zhou7df93d22020-01-27 13:44:06 -0800143 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800144 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700145 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700146
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000147 /**
148 * An immutable list of supported profiles.
149 */
150 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700151
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400152 @Nullable public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500153
Sunny Goyal6f866092016-03-17 17:04:15 -0700154 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700155 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700156
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700157 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700158
Sunny Goyalf633ef52018-03-13 09:57:05 -0700159 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700160 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700161
Sunny Goyalbbf01842015-10-08 07:41:15 -0700162 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700163 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700164 String gridName = getCurrentGridName(context);
165 String newGridName = initGrid(context, gridName);
166 if (!newGridName.equals(gridName)) {
167 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
168 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700169 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700170
Sunny Goyal35c7b192021-04-20 16:51:10 -0700171 DisplayController.INSTANCE.get(context).addChangeListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100172 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700173 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100174 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700175 }
176 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700177 }
178
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700179 /**
180 * This constructor should NOT have any monitors by design.
181 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800182 public InvariantDeviceProfile(Context context, String gridName) {
183 String newName = initGrid(context, gridName);
184 if (newName == null || !newName.equals(gridName)) {
185 throw new IllegalArgumentException("Unknown grid name");
186 }
187 }
188
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700189 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800190 * This constructor should NOT have any monitors by design.
191 */
192 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700193 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100194 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700195 String gridName = getCurrentGridName(context);
196
197 // Get the display info based on default display and interpolate it to existing display
198 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700199 DisplayController.INSTANCE.get(context).getInfo(),
Sunny Goyal19ff7282021-04-22 10:12:54 -0700200 getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700201
Alex Chau417bd722021-01-26 15:22:18 +0000202 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700203 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700204 myInfo, getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700205
206 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
207 .add(myDisplayOption);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700208 result.iconSizes[DisplayOption.INDEX_DEFAULT] =
209 defaultDisplayOption.iconSizes[DisplayOption.INDEX_DEFAULT];
210 for (int i = 1; i < DisplayOption.COUNT_TOTAL; i++) {
211 result.iconSizes[i] = Math.min(
212 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000213 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700214
Jon Mirandae126d722021-02-25 10:45:20 -0500215 result.minCellHeight = defaultDisplayOption.minCellHeight;
216 result.minCellWidth = defaultDisplayOption.minCellWidth;
217 result.borderSpacing = defaultDisplayOption.borderSpacing;
Jon Miranda228877d2021-02-09 11:05:00 -0500218
Sunny Goyal19ff7282021-04-22 10:12:54 -0700219 initGrid(context, myInfo, result, false);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800220 }
221
Tracy Zhou42255d22020-03-13 00:38:11 -0700222 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700223 return Utilities.isGridOptionsEnabled(context)
224 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700225 }
226
Sunny Goyaleff44f32019-01-09 17:29:49 -0800227 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700228 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Pat Manning67094bf2021-05-26 12:38:13 +0000229 // Each screen has two profiles (portrait/landscape), so devices with four or more
230 // supported profiles implies two or more internal displays.
231 boolean isSplitDisplay =
232 displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700233
234 ArrayList<DisplayOption> allOptions =
235 getPredefinedDeviceProfiles(context, gridName, isSplitDisplay);
236 DisplayOption displayOption =
237 invDistWeightedInterpolate(displayInfo, allOptions, isSplitDisplay);
238 initGrid(context, displayInfo, displayOption, isSplitDisplay);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700239 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800240 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700241
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700242 private void initGrid(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700243 Context context, Info displayInfo, DisplayOption displayOption,
244 boolean isSplitDisplay) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700245 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700246 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000247 numRows = closestProfile.numRows;
248 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000249 dbFile = closestProfile.dbFile;
250 defaultLayoutId = closestProfile.defaultLayoutId;
251 demoModeLayoutId = closestProfile.demoModeLayoutId;
252 numFolderRows = closestProfile.numFolderRows;
253 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500254 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400255 devicePaddingId = closestProfile.devicePaddingId;
Thales Lima7ec83822021-08-05 13:32:10 +0100256 this.isSplitDisplay = isSplitDisplay;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000257
258 mExtraAttrs = closestProfile.extraAttrs;
259
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700260 iconSize = displayOption.iconSizes[DisplayOption.INDEX_DEFAULT];
261 landscapeIconSize = displayOption.iconSizes[DisplayOption.INDEX_LANDSCAPE];
262 twoPanelPortraitIconSize = displayOption.iconSizes[DisplayOption.INDEX_TWO_PANEL_PORTRAIT];
263 twoPanelLandscapeIconSize =
264 displayOption.iconSizes[DisplayOption.INDEX_TWO_PANEL_LANDSCAPE];
Sunny Goyal35c7b192021-04-20 16:51:10 -0700265 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000266 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
267
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700268 iconTextSize = displayOption.textSizes[DisplayOption.INDEX_DEFAULT];
269 landscapeIconTextSize = displayOption.textSizes[DisplayOption.INDEX_LANDSCAPE];
270 twoPanelPortraitIconTextSize =
271 displayOption.textSizes[DisplayOption.INDEX_TWO_PANEL_PORTRAIT];
272 twoPanelLandscapeIconTextSize =
273 displayOption.textSizes[DisplayOption.INDEX_TWO_PANEL_LANDSCAPE];
274
Jon Mirandae126d722021-02-25 10:45:20 -0500275 minCellHeight = displayOption.minCellHeight;
276 minCellWidth = displayOption.minCellWidth;
277 borderSpacing = displayOption.borderSpacing;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700278
279 numShownHotseatIcons = closestProfile.numHotseatIcons;
280 numDatabaseHotseatIcons = isSplitDisplay
281 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
282
283 numAllAppsColumns = closestProfile.numAllAppsColumns;
284 numDatabaseAllAppsColumns = isSplitDisplay
285 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500286
Tracy Zhouc6060e62020-04-27 13:05:34 -0700287 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700288 allAppsIconSize = displayOption.iconSizes[DisplayOption.INDEX_ALL_APPS];
289 allAppsIconTextSize = displayOption.textSizes[DisplayOption.INDEX_ALL_APPS];
Sunny Goyalae190ff2020-04-14 00:19:01 +0000290 } else {
291 allAppsIconSize = iconSize;
292 allAppsIconTextSize = iconTextSize;
293 }
294
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400295 if (devicePaddingId != 0) {
296 devicePaddings = new DevicePaddings(context, devicePaddingId);
297 }
298
Sunny Goyalae190ff2020-04-14 00:19:01 +0000299 // If the partner customization apk contains any grid overrides, apply them
300 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700301 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700302
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000303 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700304 defaultWallpaperSize = new Point(displayInfo.currentSize);
305 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000306 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700307 .setUseTwoPanels(isSplitDisplay)
308 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700309
Sunny Goyal19ff7282021-04-22 10:12:54 -0700310 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
311 int displayWidth = bounds.bounds.width();
312 int displayHeight = bounds.bounds.height();
313 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700314
Sunny Goyal19ff7282021-04-22 10:12:54 -0700315 // We need to ensure that there is enough extra space in the wallpaper
316 // for the intended parallax effects
317 float parallaxFactor =
318 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
319 ? 2
320 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
321 defaultWallpaperSize.x =
322 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700323 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000324 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700325
326 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
327 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700328 }
329
Sunny Goyal5bc18462019-01-07 15:13:39 -0800330 @Nullable
331 public TypedValue getAttrValue(int attr) {
332 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
333 }
334
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700335 public void addOnChangeListener(OnIDPChangeListener listener) {
336 mChangeListeners.add(listener);
337 }
338
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800339 public void removeOnChangeListener(OnIDPChangeListener listener) {
340 mChangeListeners.remove(listener);
341 }
342
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800343
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800344 public void setCurrentGrid(Context context, String gridName) {
345 Context appContext = context.getApplicationContext();
346 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700347 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800348 }
349
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700350 private Object[] toModelState() {
351 return new Object[] {
352 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
353 numDatabaseAllAppsColumns, dbFile};
354 }
355
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700356 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700357 Object[] oldState = toModelState();
358
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700359 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700360 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700361 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700362
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700363 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700364 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700365 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700366 }
367 }
368
Sunny Goyal19ff7282021-04-22 10:12:54 -0700369 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
370 Context context, String gridName, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800371 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700372 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
373 final int depth = parser.getDepth();
374 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700375 while (((type = parser.next()) != XmlPullParser.END_TAG ||
376 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800377 if ((type == XmlPullParser.START_TAG)
378 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800379
Thales Lima7ec83822021-08-05 13:32:10 +0100380 GridOption gridOption =
381 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
382 if (gridOption.isEnabled) {
383 final int displayDepth = parser.getDepth();
384 while (((type = parser.next()) != XmlPullParser.END_TAG
385 || parser.getDepth() > displayDepth)
386 && type != XmlPullParser.END_DOCUMENT) {
387 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
388 parser.getName())) {
389 profiles.add(new DisplayOption(gridOption, context,
390 Xml.asAttributeSet(parser),
391 isSplitDisplay ? DEFAULT_SPLIT_DISPLAY : DEFAULT_TRUE));
392 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800393 }
394 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700395 }
396 }
397 } catch (IOException|XmlPullParserException e) {
398 throw new RuntimeException(e);
399 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800400
Sunny Goyalae190ff2020-04-14 00:19:01 +0000401 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800402 if (!TextUtils.isEmpty(gridName)) {
403 for (DisplayOption option : profiles) {
Thales Lima7ec83822021-08-05 13:32:10 +0100404 if (gridName.equals(option.grid.name) && option.grid.isEnabled) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000405 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800406 }
407 }
408 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000409 if (filteredProfiles.isEmpty()) {
410 // No grid found, use the default options
411 for (DisplayOption option : profiles) {
412 if (option.canBeDefault) {
413 filteredProfiles.add(option);
414 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800415 }
416 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000417 if (filteredProfiles.isEmpty()) {
418 throw new RuntimeException("No display option with canBeDefault=true");
419 }
420 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700421 }
422
Thales Lima7ec83822021-08-05 13:32:10 +0100423 /**
424 * @return all the grid options that can be shown on the device
425 */
426 public List<GridOption> parseAllGridOptions(Context context) {
427 List<GridOption> result = new ArrayList<>();
428 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
429 final int depth = parser.getDepth();
430 int type;
431 while (((type = parser.next()) != XmlPullParser.END_TAG
432 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
433 if ((type == XmlPullParser.START_TAG)
434 && GridOption.TAG_NAME.equals(parser.getName())) {
435 GridOption option =
436 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
437 if (option.isEnabled) {
438 result.add(option);
439 }
440 }
441 }
442 } catch (IOException | XmlPullParserException e) {
443 Log.e(TAG, "Error parsing device profile", e);
444 return Collections.emptyList();
445 }
446 return result;
447 }
448
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700449 private int getLauncherIconDensity(int requiredSize) {
450 // Densities typically defined by an app.
451 int[] densityBuckets = new int[] {
452 DisplayMetrics.DENSITY_LOW,
453 DisplayMetrics.DENSITY_MEDIUM,
454 DisplayMetrics.DENSITY_TV,
455 DisplayMetrics.DENSITY_HIGH,
456 DisplayMetrics.DENSITY_XHIGH,
457 DisplayMetrics.DENSITY_XXHIGH,
458 DisplayMetrics.DENSITY_XXXHIGH
459 };
460
461 int density = DisplayMetrics.DENSITY_XXXHIGH;
462 for (int i = densityBuckets.length - 1; i >= 0; i--) {
463 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
464 / DisplayMetrics.DENSITY_DEFAULT;
465 if (expectedSize >= requiredSize) {
466 density = densityBuckets[i];
467 }
468 }
469
470 return density;
471 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700472
Adam Cohen2e6da152015-05-06 11:42:25 -0700473 /**
474 * Apply any Partner customization grid overrides.
475 *
476 * Currently we support: all apps row / column count.
477 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700478 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
479 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700480 if (p != null) {
481 p.applyInvariantDeviceProfileOverrides(this, dm);
482 }
483 }
484
Sunny Goyal415f1732018-11-29 10:33:47 -0800485 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700486 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700487 }
488
Sunny Goyal19ff7282021-04-22 10:12:54 -0700489 private static DisplayOption invDistWeightedInterpolate(
490 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
491 int minWidthPx = Integer.MAX_VALUE;
492 int minHeightPx = Integer.MAX_VALUE;
493 for (WindowBounds bounds : displayInfo.supportedBounds) {
494 boolean isTablet = displayInfo.isTablet(bounds);
495 if (isTablet && isSplitDisplay) {
496 // For split displays, take half width per page
497 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
498 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700499
Sunny Goyal19ff7282021-04-22 10:12:54 -0700500 } else if (!isTablet && bounds.isLandscape()) {
501 // We will use transposed layout in this case
502 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
503 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
504 } else {
505 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
506 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
507 }
508 }
509
510 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
511 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700512
513 // Sort the profiles based on the closeness to the device size
514 Collections.sort(points, (a, b) ->
515 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
516 dist(width, height, b.minWidthDps, b.minHeightDps)));
517
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700518 DisplayOption closestPoint = points.get(0);
519 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700520 float weights = 0;
521
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700522 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
523 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700524 }
525
526 DisplayOption out = new DisplayOption(closestOption);
527 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700528 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700529 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
530 weights += w;
531 out.add(new DisplayOption().add(p).multiply(w));
532 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700533 out.multiply(1.0f / weights);
534
535 // Since the bitmaps are persisted, ensure that the default bitmap size is same as
536 // predefined size to avoid cache invalidation
537 out.iconSizes[DisplayOption.INDEX_DEFAULT] =
538 closestPoint.iconSizes[DisplayOption.INDEX_DEFAULT];
539 for (int i = DisplayOption.INDEX_DEFAULT + 1; i < DisplayOption.COUNT_TOTAL; i++) {
540 out.iconSizes[i] = Math.min(out.iconSizes[i],
541 out.iconSizes[DisplayOption.INDEX_DEFAULT]);
542 }
543
544 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700545 }
546
Sunny Goyal27835952017-01-13 12:15:53 -0800547 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700548 Resources res = context.getResources();
549 Configuration config = context.getResources().getConfiguration();
550
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400551 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
552 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
553 return getBestMatch(screenWidth, screenHeight);
554 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700555
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400556 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700557 DeviceProfile bestMatch = supportedProfiles.get(0);
558 float minDiff = Float.MAX_VALUE;
559
560 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400561 float diff = Math.abs(profile.widthPx - screenWidth)
562 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700563 if (diff < minDiff) {
564 minDiff = diff;
565 bestMatch = profile;
566 }
567 }
568 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800569 }
570
Sunny Goyal415f1732018-11-29 10:33:47 -0800571 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700572 float d = dist(x0, y0, x1, y1);
573 if (Float.compare(d, 0f) == 0) {
574 return Float.POSITIVE_INFINITY;
575 }
576 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
577 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700578
579 /**
580 * As a ratio of screen height, the total distance we want the parallax effect to span
581 * horizontally
582 */
583 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
584 float aspectRatio = width / (float) height;
585
586 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
587 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
588 // We will use these two data points to extrapolate how much the wallpaper parallax effect
589 // to span (ie travel) at any aspect ratio:
590
591 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
592 final float ASPECT_RATIO_PORTRAIT = 10/16f;
593 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
594 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
595
596 // To find out the desired width at different aspect ratios, we use the following two
597 // formulas, where the coefficient on x is the aspect ratio (width/height):
598 // (16/10)x + y = 1.5
599 // (10/16)x + y = 1.2
600 // We solve for x and y and end up with a final formula:
601 final float x =
602 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
603 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
604 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
605 return x * aspectRatio + y;
606 }
607
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700608 public interface OnIDPChangeListener {
609
Sunny Goyalb47172b2021-05-03 19:59:51 -0700610 /**
611 * Called when the device provide changes
612 */
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700613 void onIdpChanged(boolean modelPropertiesChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700614 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800615
616
Sunny Goyaleff44f32019-01-09 17:29:49 -0800617 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800618
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800619 public static final String TAG_NAME = "grid-option";
620
Sunny Goyaleff44f32019-01-09 17:29:49 -0800621 public final String name;
622 public final int numRows;
623 public final int numColumns;
Thales Lima7ec83822021-08-05 13:32:10 +0100624 public final boolean isEnabled;
Sunny Goyal415f1732018-11-29 10:33:47 -0800625
626 private final int numFolderRows;
627 private final int numFolderColumns;
628
Sunny Goyal19ff7282021-04-22 10:12:54 -0700629 private final int numAllAppsColumns;
630 private final int numDatabaseAllAppsColumns;
631 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700632 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800633
Tracy Zhou7df93d22020-01-27 13:44:06 -0800634 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000635
Sunny Goyal415f1732018-11-29 10:33:47 -0800636 private final int defaultLayoutId;
637 private final int demoModeLayoutId;
638
Jon Mirandae126d722021-02-25 10:45:20 -0500639 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400640 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500641
Sunny Goyal5bc18462019-01-07 15:13:39 -0800642 private final SparseArray<TypedValue> extraAttrs;
643
Thales Lima7ec83822021-08-05 13:32:10 +0100644 public GridOption(Context context, AttributeSet attrs, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800645 TypedArray a = context.obtainStyledAttributes(
646 attrs, R.styleable.GridDisplayOption);
647 name = a.getString(R.styleable.GridDisplayOption_name);
648 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
649 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
650
Tracy Zhou7df93d22020-01-27 13:44:06 -0800651 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Alex Chau1e448462021-08-13 21:48:35 +0100652 defaultLayoutId = a.getResourceId(isSplitDisplay && a.hasValue(
653 R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId)
654 ? R.styleable.GridDisplayOption_defaultSplitDisplayLayoutId
655 : R.styleable.GridDisplayOption_defaultLayoutId, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800656 demoModeLayoutId = a.getResourceId(
657 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700658
659 numAllAppsColumns = a.getInt(
660 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
661 numDatabaseAllAppsColumns = a.getInt(
662 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
663
664 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800665 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700666 numDatabaseHotseatIcons = a.getInt(
667 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
668
Sunny Goyal415f1732018-11-29 10:33:47 -0800669 numFolderRows = a.getInt(
670 R.styleable.GridDisplayOption_numFolderRows, numRows);
671 numFolderColumns = a.getInt(
672 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700673
Jon Mirandae126d722021-02-25 10:45:20 -0500674 isScalable = a.getBoolean(
675 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400676 devicePaddingId = a.getResourceId(
677 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500678
Thales Lima7ec83822021-08-05 13:32:10 +0100679 final int enabledInt =
680 a.getInteger(R.styleable.GridDisplayOption_gridEnabled,
681 GRID_ENABLED_ALL_DISPLAYS);
682 isEnabled = enabledInt == GRID_ENABLED_ALL_DISPLAYS
683 || enabledInt == GRID_ENABLED_SINGLE_DISPLAY && !isSplitDisplay;
684
Sunny Goyal415f1732018-11-29 10:33:47 -0800685 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800686 extraAttrs = Themes.createValueMap(context, attrs,
687 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800688 }
689 }
690
Sunny Goyal19ff7282021-04-22 10:12:54 -0700691 @VisibleForTesting
692 static final class DisplayOption {
693
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700694 static final int INDEX_DEFAULT = 0;
695 static final int INDEX_LANDSCAPE = 1;
696 static final int INDEX_ALL_APPS = 2;
697 static final int INDEX_TWO_PANEL_PORTRAIT = 3;
698 static final int INDEX_TWO_PANEL_LANDSCAPE = 4;
699
700 static final int COUNT_TOTAL = 5;
701
Sunny Goyal19ff7282021-04-22 10:12:54 -0700702 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800703
Sunny Goyal415f1732018-11-29 10:33:47 -0800704 private final float minWidthDps;
705 private final float minHeightDps;
706 private final boolean canBeDefault;
707
Jon Mirandae126d722021-02-25 10:45:20 -0500708 private float minCellHeight;
709 private float minCellWidth;
710 private float borderSpacing;
711
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700712 private final float[] iconSizes = new float[COUNT_TOTAL];
713 private final float[] textSizes = new float[COUNT_TOTAL];
Sunny Goyal415f1732018-11-29 10:33:47 -0800714
Sunny Goyal19ff7282021-04-22 10:12:54 -0700715 DisplayOption(GridOption grid, Context context, AttributeSet attrs, int defaultFlagValue) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800716 this.grid = grid;
717
718 TypedArray a = context.obtainStyledAttributes(
719 attrs, R.styleable.ProfileDisplayOption);
720
Sunny Goyal415f1732018-11-29 10:33:47 -0800721 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
722 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700723
724 canBeDefault = a.getInt(R.styleable.ProfileDisplayOption_canBeDefault, 0)
725 == defaultFlagValue;
Sunny Goyal415f1732018-11-29 10:33:47 -0800726
Jon Mirandae126d722021-02-25 10:45:20 -0500727 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
728 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
729 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
730
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700731 iconSizes[INDEX_DEFAULT] =
732 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
733 iconSizes[INDEX_LANDSCAPE] =
734 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
735 iconSizes[INDEX_DEFAULT]);
736 iconSizes[INDEX_ALL_APPS] =
737 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
738 iconSizes[INDEX_DEFAULT]);
739 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
740 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconSize,
741 iconSizes[INDEX_DEFAULT]);
742 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
743 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconSize,
744 iconSizes[INDEX_LANDSCAPE]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700745
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700746 textSizes[INDEX_DEFAULT] =
747 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
748 textSizes[INDEX_LANDSCAPE] =
749 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconTextSize,
750 textSizes[INDEX_DEFAULT]);
751 textSizes[INDEX_ALL_APPS] =
752 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
753 textSizes[INDEX_DEFAULT]);
754 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
755 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconTextSize,
756 textSizes[INDEX_DEFAULT]);
757 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
758 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconTextSize,
759 textSizes[INDEX_LANDSCAPE]);
760
Sunny Goyal415f1732018-11-29 10:33:47 -0800761 a.recycle();
762 }
763
764 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700765 this(null);
766 }
767
768 DisplayOption(GridOption grid) {
769 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800770 minWidthDps = 0;
771 minHeightDps = 0;
772 canBeDefault = false;
Jon Mirandae126d722021-02-25 10:45:20 -0500773 minCellHeight = 0;
774 minCellWidth = 0;
775 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800776 }
777
778 private DisplayOption multiply(float w) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700779 for (int i = 0; i < COUNT_TOTAL; i++) {
780 iconSizes[i] *= w;
781 textSizes[i] *= w;
782 }
Jon Mirandae126d722021-02-25 10:45:20 -0500783 minCellHeight *= w;
784 minCellWidth *= w;
785 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800786 return this;
787 }
788
789 private DisplayOption add(DisplayOption p) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700790 for (int i = 0; i < COUNT_TOTAL; i++) {
791 iconSizes[i] += p.iconSizes[i];
792 textSizes[i] += p.textSizes[i];
793 }
Jon Mirandae126d722021-02-25 10:45:20 -0500794 minCellHeight += p.minCellHeight;
795 minCellWidth += p.minCellWidth;
796 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800797 return this;
798 }
799 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000800}