blob: ca07249b06fba66d2005312a62b1efb9dfca3198 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Sunny Goyal19ff7282021-04-22 10:12:54 -070019import static com.android.launcher3.Utilities.dpiFromPx;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010020import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Sunny Goyal35c7b192021-04-20 16:51:10 -070021import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
Sunny Goyal19ff7282021-04-22 10:12:54 -070022import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080023import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070024
Sunny Goyalc6205602015-05-21 20:46:33 -070025import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070026import android.appwidget.AppWidgetHostView;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080029import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080030import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070031import android.content.res.TypedArray;
32import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070033import android.graphics.Point;
Thales Lima78d00ad2021-09-30 11:29:06 +010034import android.graphics.PointF;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070035import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080036import android.text.TextUtils;
37import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070038import android.util.DisplayMetrics;
Thales Lima7ec83822021-08-05 13:32:10 +010039import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080040import android.util.SparseArray;
41import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070042import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080043import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070044
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070045import androidx.annotation.Nullable;
46import androidx.annotation.VisibleForTesting;
47
Sunny Goyal68031ca2021-08-02 12:23:44 -070048import com.android.launcher3.model.DeviceGridState;
Sunny Goyalfd58da62020-08-11 12:06:49 -070049import com.android.launcher3.util.DisplayController;
50import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080051import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070052import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080053import com.android.launcher3.util.Themes;
Sunny Goyal19ff7282021-04-22 10:12:54 -070054import com.android.launcher3.util.WindowBounds;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070055
Sunny Goyal819e1932016-07-07 16:43:58 -070056import org.xmlpull.v1.XmlPullParser;
57import org.xmlpull.v1.XmlPullParserException;
58
59import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070060import java.util.ArrayList;
Sunny Goyal6e6f7992021-08-24 16:23:29 -070061import java.util.Arrays;
Sunny Goyal6d55f662019-01-02 12:13:43 -080062import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070063import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070064
65public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070066
Hyunyoung Songc55a3502018-12-04 15:43:16 -080067 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070068 // We do not need any synchronization for this variable as its only written on UI thread.
69 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070070 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070071
Sunny Goyal19ff7282021-04-22 10:12:54 -070072 private static final int DEFAULT_TRUE = -1;
73 private static final int DEFAULT_SPLIT_DISPLAY = 2;
Thales Lima7ec83822021-08-05 13:32:10 +010074 private static final int GRID_ENABLED_ALL_DISPLAYS = 0;
75 private static final int GRID_ENABLED_SINGLE_DISPLAY = 1;
Sunny Goyal19ff7282021-04-22 10:12:54 -070076
Hyunyoung Songc55a3502018-12-04 15:43:16 -080077 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080078
Sunny Goyal53d7ee42015-05-22 12:25:45 -070079 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
80
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070081 // Constants that affects the interpolation curve between statically defined device profile
82 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080083 private static final float KNEARESTNEIGHBOR = 3;
84 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070085
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070086 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080087 private static final float WEIGHT_EFFICIENT = 100000f;
88
Thales Lima83bedbf2021-10-05 17:47:39 +010089 // Used for arrays to specify different sizes (e.g. border spaces, width/height) in different
90 // constraints
91 static final int COUNT_SIZES = 5;
92 static final int INDEX_DEFAULT = 0;
93 static final int INDEX_LANDSCAPE = 1;
94 static final int INDEX_TWO_PANEL_PORTRAIT = 2;
95 static final int INDEX_TWO_PANEL_LANDSCAPE = 3;
96 static final int INDEX_ALL_APPS = 4;
97
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098 /**
99 * Number of icons per row and column in the workspace.
100 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700101 public int numRows;
102 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700103
104 /**
105 * Number of icons per row and column in the folder.
106 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700107 public int numFolderRows;
108 public int numFolderColumns;
Thales Lima83bedbf2021-10-05 17:47:39 +0100109 public float[] iconSize;
110 public float[] iconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700111 public int iconBitmapSize;
112 public int fillResIconDpi;
Thales Lima7ec83822021-08-05 13:32:10 +0100113 public boolean isSplitDisplay;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700114
Thales Lima83bedbf2021-10-05 17:47:39 +0100115 public PointF[] minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100116
Thales Lima83bedbf2021-10-05 17:47:39 +0100117 public PointF[] borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100118 public float folderBorderSpace;
119
Thales Lima83bedbf2021-10-05 17:47:39 +0100120 public float[] horizontalMargin;
Jon Mirandae126d722021-02-25 10:45:20 -0500121
Sunny Goyal5bc18462019-01-07 15:13:39 -0800122 private SparseArray<TypedValue> mExtraAttrs;
123
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700124 /**
125 * Number of icons inside the hotseat area.
126 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700127 protected int numShownHotseatIcons;
128
129 /**
130 * Number of icons inside the hotseat area that is stored in the database. This is greater than
131 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
132 * sizes that share the same DB.
133 */
134 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700135
Jon Miranda6f7e9702019-09-16 14:44:14 -0700136 /**
137 * Number of columns in the all apps list.
138 */
139 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700140 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700141
Jon Mirandae126d722021-02-25 10:45:20 -0500142 /**
143 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
144 */
145 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400146 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500147
Tracy Zhou7df93d22020-01-27 13:44:06 -0800148 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800149 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700150 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700151
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000152 /**
153 * An immutable list of supported profiles.
154 */
155 public List<DeviceProfile> supportedProfiles = Collections.EMPTY_LIST;
Sunny Goyalc6205602015-05-21 20:46:33 -0700156
sfufa@google.comde013292021-09-21 18:22:44 -0700157 @Nullable
158 public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500159
Sunny Goyal6f866092016-03-17 17:04:15 -0700160 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700161 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700162
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700163 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700164
Sunny Goyalf633ef52018-03-13 09:57:05 -0700165 @VisibleForTesting
sfufa@google.comde013292021-09-21 18:22:44 -0700166 public InvariantDeviceProfile() {
167 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700168
Sunny Goyalbbf01842015-10-08 07:41:15 -0700169 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700170 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700171 String gridName = getCurrentGridName(context);
172 String newGridName = initGrid(context, gridName);
173 if (!newGridName.equals(gridName)) {
174 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
175 }
Sunny Goyal68031ca2021-08-02 12:23:44 -0700176 new DeviceGridState(this).writeToPrefs(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700177
Tracy Zhouc8beebf2021-09-23 10:18:11 -0700178 DisplayController.INSTANCE.get(context).setPriorityListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100179 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700180 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100181 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700182 }
183 });
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700184 }
185
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700186 /**
187 * This constructor should NOT have any monitors by design.
188 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800189 public InvariantDeviceProfile(Context context, String gridName) {
190 String newName = initGrid(context, gridName);
191 if (newName == null || !newName.equals(gridName)) {
192 throw new IllegalArgumentException("Unknown grid name");
193 }
194 }
195
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700196 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800197 * This constructor should NOT have any monitors by design.
198 */
199 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700200 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100201 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700202 String gridName = getCurrentGridName(context);
203
204 // Get the display info based on default display and interpolate it to existing display
205 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700206 DisplayController.INSTANCE.get(context).getInfo(),
Sunny Goyal19ff7282021-04-22 10:12:54 -0700207 getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700208
Alex Chau417bd722021-01-26 15:22:18 +0000209 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700210 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700211 myInfo, getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700212
213 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
214 .add(myDisplayOption);
Thales Lima83bedbf2021-10-05 17:47:39 +0100215 result.iconSizes[INDEX_DEFAULT] =
216 defaultDisplayOption.iconSizes[INDEX_DEFAULT];
217 for (int i = 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700218 result.iconSizes[i] = Math.min(
219 defaultDisplayOption.iconSizes[i], myDisplayOption.iconSizes[i]);
Alex Chau7c439722021-03-24 11:32:44 +0000220 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700221
Thales Lima83bedbf2021-10-05 17:47:39 +0100222 System.arraycopy(defaultDisplayOption.minCellSize, 0, result.minCellSize, 0,
223 COUNT_SIZES);
224 System.arraycopy(defaultDisplayOption.borderSpaces, 0, result.borderSpaces, 0,
225 COUNT_SIZES);
Jon Miranda228877d2021-02-09 11:05:00 -0500226
Sunny Goyal19ff7282021-04-22 10:12:54 -0700227 initGrid(context, myInfo, result, false);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800228 }
229
Tracy Zhou42255d22020-03-13 00:38:11 -0700230 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700231 return Utilities.isGridOptionsEnabled(context)
232 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700233 }
234
Sunny Goyaleff44f32019-01-09 17:29:49 -0800235 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700236 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Pat Manning67094bf2021-05-26 12:38:13 +0000237 // Each screen has two profiles (portrait/landscape), so devices with four or more
238 // supported profiles implies two or more internal displays.
239 boolean isSplitDisplay =
240 displayInfo.supportedBounds.size() >= 4 && ENABLE_TWO_PANEL_HOME.get();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700241
242 ArrayList<DisplayOption> allOptions =
243 getPredefinedDeviceProfiles(context, gridName, isSplitDisplay);
244 DisplayOption displayOption =
245 invDistWeightedInterpolate(displayInfo, allOptions, isSplitDisplay);
246 initGrid(context, displayInfo, displayOption, isSplitDisplay);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700247 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800248 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700249
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700250 private void initGrid(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700251 Context context, Info displayInfo, DisplayOption displayOption,
252 boolean isSplitDisplay) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700253 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700254 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000255 numRows = closestProfile.numRows;
256 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000257 dbFile = closestProfile.dbFile;
258 defaultLayoutId = closestProfile.defaultLayoutId;
259 demoModeLayoutId = closestProfile.demoModeLayoutId;
260 numFolderRows = closestProfile.numFolderRows;
261 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500262 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400263 devicePaddingId = closestProfile.devicePaddingId;
Thales Lima7ec83822021-08-05 13:32:10 +0100264 this.isSplitDisplay = isSplitDisplay;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000265
266 mExtraAttrs = closestProfile.extraAttrs;
267
Thales Lima83bedbf2021-10-05 17:47:39 +0100268 iconSize = displayOption.iconSizes;
269 iconBitmapSize = ResourceUtils.pxFromDp(iconSize[INDEX_DEFAULT], metrics);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000270 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
271
Thales Lima83bedbf2021-10-05 17:47:39 +0100272 iconTextSize = displayOption.textSizes;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700273
Thales Lima83bedbf2021-10-05 17:47:39 +0100274 minCellSize = displayOption.minCellSize;
Thales Lima78d00ad2021-09-30 11:29:06 +0100275
Thales Lima83bedbf2021-10-05 17:47:39 +0100276 borderSpaces = displayOption.borderSpaces;
Thales Lima78d00ad2021-09-30 11:29:06 +0100277 folderBorderSpace = displayOption.folderBorderSpace;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700278
Thales Limad90faab2021-09-21 17:18:47 +0100279 horizontalMargin = displayOption.horizontalMargin;
Thales Limad90faab2021-09-21 17:18:47 +0100280
Sunny Goyal19ff7282021-04-22 10:12:54 -0700281 numShownHotseatIcons = closestProfile.numHotseatIcons;
282 numDatabaseHotseatIcons = isSplitDisplay
283 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
284
285 numAllAppsColumns = closestProfile.numAllAppsColumns;
286 numDatabaseAllAppsColumns = isSplitDisplay
287 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500288
Thales Lima83bedbf2021-10-05 17:47:39 +0100289 if (!Utilities.isGridOptionsEnabled(context)) {
290 iconSize[INDEX_ALL_APPS] = iconSize[INDEX_DEFAULT];
291 iconTextSize[INDEX_ALL_APPS] = iconTextSize[INDEX_DEFAULT];
Sunny Goyalae190ff2020-04-14 00:19:01 +0000292 }
293
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400294 if (devicePaddingId != 0) {
295 devicePaddings = new DevicePaddings(context, devicePaddingId);
296 }
297
Sunny Goyalae190ff2020-04-14 00:19:01 +0000298 // If the partner customization apk contains any grid overrides, apply them
299 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700300 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700301
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000302 final List<DeviceProfile> localSupportedProfiles = new ArrayList<>();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700303 defaultWallpaperSize = new Point(displayInfo.currentSize);
304 for (WindowBounds bounds : displayInfo.supportedBounds) {
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000305 localSupportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
Sunny Goyal19ff7282021-04-22 10:12:54 -0700306 .setUseTwoPanels(isSplitDisplay)
307 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700308
Sunny Goyal19ff7282021-04-22 10:12:54 -0700309 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
310 int displayWidth = bounds.bounds.width();
311 int displayHeight = bounds.bounds.height();
312 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700313
Sunny Goyal19ff7282021-04-22 10:12:54 -0700314 // We need to ensure that there is enough extra space in the wallpaper
315 // for the intended parallax effects
316 float parallaxFactor =
317 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
318 ? 2
319 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
320 defaultWallpaperSize.x =
321 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700322 }
Pinyao Ting9cd63c92021-06-16 17:51:39 +0000323 supportedProfiles = Collections.unmodifiableList(localSupportedProfiles);
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700324
325 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
326 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700327 }
328
Sunny Goyal5bc18462019-01-07 15:13:39 -0800329 @Nullable
330 public TypedValue getAttrValue(int attr) {
331 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
332 }
333
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700334 public void addOnChangeListener(OnIDPChangeListener listener) {
335 mChangeListeners.add(listener);
336 }
337
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800338 public void removeOnChangeListener(OnIDPChangeListener listener) {
339 mChangeListeners.remove(listener);
340 }
341
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800342
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800343 public void setCurrentGrid(Context context, String gridName) {
344 Context appContext = context.getApplicationContext();
345 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700346 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800347 }
348
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700349 private Object[] toModelState() {
sfufa@google.comde013292021-09-21 18:22:44 -0700350 return new Object[]{
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700351 numColumns, numRows, numDatabaseHotseatIcons, iconBitmapSize, fillResIconDpi,
352 numDatabaseAllAppsColumns, dbFile};
353 }
354
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700355 private void onConfigChanged(Context context) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700356 Object[] oldState = toModelState();
357
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700358 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700359 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700360 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700361
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700362 boolean modelPropsChanged = !Arrays.equals(oldState, toModelState());
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700363 for (OnIDPChangeListener listener : mChangeListeners) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700364 listener.onIdpChanged(modelPropsChanged);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700365 }
366 }
367
Sunny Goyal19ff7282021-04-22 10:12:54 -0700368 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
369 Context context, String gridName, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800370 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700371 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
372 final int depth = parser.getDepth();
373 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700374 while (((type = parser.next()) != XmlPullParser.END_TAG ||
375 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800376 if ((type == XmlPullParser.START_TAG)
377 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800378
Thales Lima7ec83822021-08-05 13:32:10 +0100379 GridOption gridOption =
380 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
381 if (gridOption.isEnabled) {
382 final int displayDepth = parser.getDepth();
383 while (((type = parser.next()) != XmlPullParser.END_TAG
384 || parser.getDepth() > displayDepth)
385 && type != XmlPullParser.END_DOCUMENT) {
386 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
387 parser.getName())) {
388 profiles.add(new DisplayOption(gridOption, context,
389 Xml.asAttributeSet(parser),
390 isSplitDisplay ? DEFAULT_SPLIT_DISPLAY : DEFAULT_TRUE));
391 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800392 }
393 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700394 }
395 }
sfufa@google.comde013292021-09-21 18:22:44 -0700396 } catch (IOException | XmlPullParserException e) {
Sunny Goyal819e1932016-07-07 16:43:58 -0700397 throw new RuntimeException(e);
398 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800399
Sunny Goyalae190ff2020-04-14 00:19:01 +0000400 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800401 if (!TextUtils.isEmpty(gridName)) {
402 for (DisplayOption option : profiles) {
Thales Lima7ec83822021-08-05 13:32:10 +0100403 if (gridName.equals(option.grid.name) && option.grid.isEnabled) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000404 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800405 }
406 }
407 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000408 if (filteredProfiles.isEmpty()) {
409 // No grid found, use the default options
410 for (DisplayOption option : profiles) {
411 if (option.canBeDefault) {
412 filteredProfiles.add(option);
413 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800414 }
415 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000416 if (filteredProfiles.isEmpty()) {
417 throw new RuntimeException("No display option with canBeDefault=true");
418 }
419 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700420 }
421
Thales Lima7ec83822021-08-05 13:32:10 +0100422 /**
423 * @return all the grid options that can be shown on the device
424 */
425 public List<GridOption> parseAllGridOptions(Context context) {
426 List<GridOption> result = new ArrayList<>();
427 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
428 final int depth = parser.getDepth();
429 int type;
430 while (((type = parser.next()) != XmlPullParser.END_TAG
431 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
432 if ((type == XmlPullParser.START_TAG)
433 && GridOption.TAG_NAME.equals(parser.getName())) {
434 GridOption option =
435 new GridOption(context, Xml.asAttributeSet(parser), isSplitDisplay);
436 if (option.isEnabled) {
437 result.add(option);
438 }
439 }
440 }
441 } catch (IOException | XmlPullParserException e) {
442 Log.e(TAG, "Error parsing device profile", e);
443 return Collections.emptyList();
444 }
445 return result;
446 }
447
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700448 private int getLauncherIconDensity(int requiredSize) {
449 // Densities typically defined by an app.
sfufa@google.comde013292021-09-21 18:22:44 -0700450 int[] densityBuckets = new int[]{
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700451 DisplayMetrics.DENSITY_LOW,
452 DisplayMetrics.DENSITY_MEDIUM,
453 DisplayMetrics.DENSITY_TV,
454 DisplayMetrics.DENSITY_HIGH,
455 DisplayMetrics.DENSITY_XHIGH,
456 DisplayMetrics.DENSITY_XXHIGH,
457 DisplayMetrics.DENSITY_XXXHIGH
458 };
459
460 int density = DisplayMetrics.DENSITY_XXXHIGH;
461 for (int i = densityBuckets.length - 1; i >= 0; i--) {
462 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
463 / DisplayMetrics.DENSITY_DEFAULT;
464 if (expectedSize >= requiredSize) {
465 density = densityBuckets[i];
466 }
467 }
468
469 return density;
470 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700471
Adam Cohen2e6da152015-05-06 11:42:25 -0700472 /**
473 * Apply any Partner customization grid overrides.
474 *
475 * Currently we support: all apps row / column count.
476 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700477 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
478 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700479 if (p != null) {
480 p.applyInvariantDeviceProfileOverrides(this, dm);
481 }
482 }
483
Sunny Goyal415f1732018-11-29 10:33:47 -0800484 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700485 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700486 }
487
Sunny Goyal19ff7282021-04-22 10:12:54 -0700488 private static DisplayOption invDistWeightedInterpolate(
489 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
490 int minWidthPx = Integer.MAX_VALUE;
491 int minHeightPx = Integer.MAX_VALUE;
492 for (WindowBounds bounds : displayInfo.supportedBounds) {
493 boolean isTablet = displayInfo.isTablet(bounds);
494 if (isTablet && isSplitDisplay) {
495 // For split displays, take half width per page
496 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
497 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700498
Sunny Goyal19ff7282021-04-22 10:12:54 -0700499 } else if (!isTablet && bounds.isLandscape()) {
500 // We will use transposed layout in this case
501 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
502 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
503 } else {
504 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
505 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
506 }
507 }
508
509 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
510 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700511
512 // Sort the profiles based on the closeness to the device size
513 Collections.sort(points, (a, b) ->
514 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
515 dist(width, height, b.minWidthDps, b.minHeightDps)));
516
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700517 DisplayOption closestPoint = points.get(0);
518 GridOption closestOption = closestPoint.grid;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700519 float weights = 0;
520
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700521 if (dist(width, height, closestPoint.minWidthDps, closestPoint.minHeightDps) == 0) {
522 return closestPoint;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700523 }
524
525 DisplayOption out = new DisplayOption(closestOption);
526 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700527 DisplayOption p = points.get(i);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700528 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
529 weights += w;
530 out.add(new DisplayOption().add(p).multiply(w));
531 }
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700532 out.multiply(1.0f / weights);
533
534 // Since the bitmaps are persisted, ensure that the default bitmap size is same as
535 // predefined size to avoid cache invalidation
Thales Lima83bedbf2021-10-05 17:47:39 +0100536 out.iconSizes[INDEX_DEFAULT] =
537 closestPoint.iconSizes[INDEX_DEFAULT];
538 for (int i = INDEX_DEFAULT + 1; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700539 out.iconSizes[i] = Math.min(out.iconSizes[i],
Thales Lima83bedbf2021-10-05 17:47:39 +0100540 out.iconSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700541 }
542
543 return out;
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700544 }
545
Sunny Goyal27835952017-01-13 12:15:53 -0800546 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700547 Resources res = context.getResources();
548 Configuration config = context.getResources().getConfiguration();
549
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400550 float screenWidth = config.screenWidthDp * res.getDisplayMetrics().density;
551 float screenHeight = config.screenHeightDp * res.getDisplayMetrics().density;
552 return getBestMatch(screenWidth, screenHeight);
553 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700554
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400555 public DeviceProfile getBestMatch(float screenWidth, float screenHeight) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700556 DeviceProfile bestMatch = supportedProfiles.get(0);
557 float minDiff = Float.MAX_VALUE;
558
559 for (DeviceProfile profile : supportedProfiles) {
Sunny Goyal3e9a29c2021-09-07 15:53:09 -0400560 float diff = Math.abs(profile.widthPx - screenWidth)
561 + Math.abs(profile.heightPx - screenHeight);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700562 if (diff < minDiff) {
563 minDiff = diff;
564 bestMatch = profile;
565 }
566 }
567 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800568 }
569
Sunny Goyal415f1732018-11-29 10:33:47 -0800570 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700571 float d = dist(x0, y0, x1, y1);
572 if (Float.compare(d, 0f) == 0) {
573 return Float.POSITIVE_INFINITY;
574 }
575 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
576 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700577
578 /**
579 * As a ratio of screen height, the total distance we want the parallax effect to span
580 * horizontally
581 */
582 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
583 float aspectRatio = width / (float) height;
584
585 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
586 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
587 // We will use these two data points to extrapolate how much the wallpaper parallax effect
588 // to span (ie travel) at any aspect ratio:
589
sfufa@google.comde013292021-09-21 18:22:44 -0700590 final float ASPECT_RATIO_LANDSCAPE = 16 / 10f;
591 final float ASPECT_RATIO_PORTRAIT = 10 / 16f;
Sunny Goyal6f866092016-03-17 17:04:15 -0700592 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
593 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
594
595 // To find out the desired width at different aspect ratios, we use the following two
596 // formulas, where the coefficient on x is the aspect ratio (width/height):
597 // (16/10)x + y = 1.5
598 // (10/16)x + y = 1.2
599 // We solve for x and y and end up with a final formula:
600 final float x =
sfufa@google.comde013292021-09-21 18:22:44 -0700601 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE
602 - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
Sunny Goyal6f866092016-03-17 17:04:15 -0700603 (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
694 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800695
Sunny Goyal415f1732018-11-29 10:33:47 -0800696 private final float minWidthDps;
697 private final float minHeightDps;
698 private final boolean canBeDefault;
699
Thales Lima83bedbf2021-10-05 17:47:39 +0100700 private final PointF[] minCellSize = new PointF[COUNT_SIZES];
Thales Lima78d00ad2021-09-30 11:29:06 +0100701
702 private float folderBorderSpace;
703 private final PointF[] borderSpaces = new PointF[COUNT_SIZES];
Thales Lima83bedbf2021-10-05 17:47:39 +0100704 private final float[] horizontalMargin = new float[COUNT_SIZES];
Thales Limad90faab2021-09-21 17:18:47 +0100705
Thales Lima78d00ad2021-09-30 11:29:06 +0100706 private final float[] iconSizes = new float[COUNT_SIZES];
707 private final float[] textSizes = new float[COUNT_SIZES];
Sunny Goyal415f1732018-11-29 10:33:47 -0800708
Sunny Goyal19ff7282021-04-22 10:12:54 -0700709 DisplayOption(GridOption grid, Context context, AttributeSet attrs, int defaultFlagValue) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800710 this.grid = grid;
711
712 TypedArray a = context.obtainStyledAttributes(
713 attrs, R.styleable.ProfileDisplayOption);
714
Sunny Goyal415f1732018-11-29 10:33:47 -0800715 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
716 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700717
718 canBeDefault = a.getInt(R.styleable.ProfileDisplayOption_canBeDefault, 0)
719 == defaultFlagValue;
Sunny Goyal415f1732018-11-29 10:33:47 -0800720
Thales Lima83bedbf2021-10-05 17:47:39 +0100721 float x;
722 float y;
723
724 x = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
725 y = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
726 minCellSize[INDEX_DEFAULT] = new PointF(x, y);
727 minCellSize[INDEX_LANDSCAPE] = new PointF(x, y);
728 minCellSize[INDEX_ALL_APPS] = new PointF(x, y);
729
730 x = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellWidthDps,
731 minCellSize[INDEX_DEFAULT].x);
732 y = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitMinCellHeightDps,
733 minCellSize[INDEX_DEFAULT].y);
734 minCellSize[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
735
736 x = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellWidthDps,
737 minCellSize[INDEX_DEFAULT].x);
738 y = a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeMinCellHeightDps,
739 minCellSize[INDEX_DEFAULT].y);
740 minCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
Thales Lima78d00ad2021-09-30 11:29:06 +0100741
742 float borderSpace = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceDps, 0);
743 float twoPanelPortraitBorderSpaceDps = a.getFloat(
744 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceDps, borderSpace);
745 float twoPanelLandscapeBorderSpaceDps = a.getFloat(
746 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceDps, borderSpace);
Thales Lima78d00ad2021-09-30 11:29:06 +0100747
748 x = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceHorizontalDps, borderSpace);
749 y = a.getFloat(R.styleable.ProfileDisplayOption_borderSpaceVerticalDps, borderSpace);
750 borderSpaces[INDEX_DEFAULT] = new PointF(x, y);
751 borderSpaces[INDEX_LANDSCAPE] = new PointF(x, y);
752
753 x = a.getFloat(
754 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceHorizontalDps,
755 twoPanelPortraitBorderSpaceDps);
756 y = a.getFloat(
757 R.styleable.ProfileDisplayOption_twoPanelPortraitBorderSpaceVerticalDps,
758 twoPanelPortraitBorderSpaceDps);
759 borderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y);
760
761 x = a.getFloat(
762 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceHorizontalDps,
763 twoPanelLandscapeBorderSpaceDps);
764 y = a.getFloat(
765 R.styleable.ProfileDisplayOption_twoPanelLandscapeBorderSpaceVerticalDps,
766 twoPanelLandscapeBorderSpaceDps);
767 borderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y);
768
769 x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsCellSpacingDps,
770 borderSpace);
771 borderSpaces[INDEX_ALL_APPS] = new PointF(x, y);
772 folderBorderSpace = borderSpace;
Jon Mirandae126d722021-02-25 10:45:20 -0500773
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700774 iconSizes[INDEX_DEFAULT] =
775 a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
776 iconSizes[INDEX_LANDSCAPE] =
777 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
778 iconSizes[INDEX_DEFAULT]);
779 iconSizes[INDEX_ALL_APPS] =
780 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
781 iconSizes[INDEX_DEFAULT]);
782 iconSizes[INDEX_TWO_PANEL_PORTRAIT] =
783 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconSize,
784 iconSizes[INDEX_DEFAULT]);
785 iconSizes[INDEX_TWO_PANEL_LANDSCAPE] =
786 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconSize,
Thales Lima83bedbf2021-10-05 17:47:39 +0100787 iconSizes[INDEX_DEFAULT]);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700788
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700789 textSizes[INDEX_DEFAULT] =
790 a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
791 textSizes[INDEX_LANDSCAPE] =
792 a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconTextSize,
793 textSizes[INDEX_DEFAULT]);
794 textSizes[INDEX_ALL_APPS] =
795 a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
796 textSizes[INDEX_DEFAULT]);
797 textSizes[INDEX_TWO_PANEL_PORTRAIT] =
798 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelPortraitIconTextSize,
799 textSizes[INDEX_DEFAULT]);
800 textSizes[INDEX_TWO_PANEL_LANDSCAPE] =
801 a.getFloat(R.styleable.ProfileDisplayOption_twoPanelLandscapeIconTextSize,
Thales Lima83bedbf2021-10-05 17:47:39 +0100802 textSizes[INDEX_DEFAULT]);
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700803
Thales Lima83bedbf2021-10-05 17:47:39 +0100804 horizontalMargin[INDEX_DEFAULT] = a.getFloat(
805 R.styleable.ProfileDisplayOption_horizontalMargin, 0);
806 horizontalMargin[INDEX_LANDSCAPE] = horizontalMargin[INDEX_DEFAULT];
807 horizontalMargin[INDEX_ALL_APPS] = horizontalMargin[INDEX_DEFAULT];
808 horizontalMargin[INDEX_TWO_PANEL_LANDSCAPE] = a.getFloat(
Thales Limad90faab2021-09-21 17:18:47 +0100809 R.styleable.ProfileDisplayOption_twoPanelLandscapeHorizontalMargin,
Thales Lima83bedbf2021-10-05 17:47:39 +0100810 horizontalMargin[INDEX_DEFAULT]);
811 horizontalMargin[INDEX_TWO_PANEL_PORTRAIT] = a.getFloat(
Thales Limad90faab2021-09-21 17:18:47 +0100812 R.styleable.ProfileDisplayOption_twoPanelPortraitHorizontalMargin,
Thales Lima83bedbf2021-10-05 17:47:39 +0100813 horizontalMargin[INDEX_DEFAULT]);
Thales Limad90faab2021-09-21 17:18:47 +0100814
Sunny Goyal415f1732018-11-29 10:33:47 -0800815 a.recycle();
816 }
817
818 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700819 this(null);
820 }
821
822 DisplayOption(GridOption grid) {
823 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800824 minWidthDps = 0;
825 minHeightDps = 0;
826 canBeDefault = false;
Thales Lima78d00ad2021-09-30 11:29:06 +0100827 for (int i = 0; i < COUNT_SIZES; i++) {
828 iconSizes[i] = 0;
829 textSizes[i] = 0;
830 borderSpaces[i] = new PointF();
Thales Lima83bedbf2021-10-05 17:47:39 +0100831 minCellSize[i] = new PointF();
Thales Lima78d00ad2021-09-30 11:29:06 +0100832 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800833 }
834
835 private DisplayOption multiply(float w) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100836 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700837 iconSizes[i] *= w;
838 textSizes[i] *= w;
Thales Lima83bedbf2021-10-05 17:47:39 +0100839 borderSpaces[i].x *= w;
840 borderSpaces[i].y *= w;
841 minCellSize[i].x *= w;
842 minCellSize[i].y *= w;
843 horizontalMargin[i] *= w;
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700844 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100845
846 folderBorderSpace *= w;
847
Sunny Goyal415f1732018-11-29 10:33:47 -0800848 return this;
849 }
850
851 private DisplayOption add(DisplayOption p) {
Thales Lima78d00ad2021-09-30 11:29:06 +0100852 for (int i = 0; i < COUNT_SIZES; i++) {
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700853 iconSizes[i] += p.iconSizes[i];
854 textSizes[i] += p.textSizes[i];
Thales Lima83bedbf2021-10-05 17:47:39 +0100855 borderSpaces[i].x += p.borderSpaces[i].x;
856 borderSpaces[i].y += p.borderSpaces[i].y;
857 minCellSize[i].x += p.minCellSize[i].x;
858 minCellSize[i].y += p.minCellSize[i].y;
859 horizontalMargin[i] += p.horizontalMargin[i];
Sunny Goyal6e6f7992021-08-24 16:23:29 -0700860 }
Thales Lima78d00ad2021-09-30 11:29:06 +0100861
862 folderBorderSpace += p.folderBorderSpace;
863
Sunny Goyal415f1732018-11-29 10:33:47 -0800864 return this;
865 }
866 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000867}