blob: a863e6b55cf19c1a29324abe7d3593bd2544a056 [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
Hyunyoung Songc55a3502018-12-04 15:43:16 -080019import static com.android.launcher3.Utilities.getDevicePrefs;
Tracy Zhoued5f3082020-04-20 01:13:26 -070020import static com.android.launcher3.Utilities.getPointString;
Jon Miranda73dcc982020-12-11 14:14:46 -080021import static com.android.launcher3.config.FeatureFlags.ENABLE_FOUR_COLUMNS;
Andras Kloczl8e57cce2021-02-11 23:51:19 +010022import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080023import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070024import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070025
Sunny Goyalc6205602015-05-21 20:46:33 -070026import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.appwidget.AppWidgetHostView;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070028import android.content.BroadcastReceiver;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070029import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070030import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070031import android.content.Intent;
Sunny Goyal27835952017-01-13 12:15:53 -080032import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080033import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070034import android.content.res.TypedArray;
35import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070036import android.graphics.Point;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070037import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080038import android.text.TextUtils;
39import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070040import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070041import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080042import android.util.SparseArray;
43import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070044import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080045import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070046
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070047import androidx.annotation.Nullable;
48import androidx.annotation.VisibleForTesting;
49
Sunny Goyal905262c2019-05-03 16:50:43 -070050import com.android.launcher3.graphics.IconShape;
Winson Chung8e64bba2021-04-20 13:26:26 -070051import com.android.launcher3.testing.TestProtocol;
Sunny Goyald0e360a2018-06-29 14:40:18 -070052import com.android.launcher3.util.ConfigMonitor;
Sunny Goyalfd58da62020-08-11 12:06:49 -070053import com.android.launcher3.util.DisplayController;
54import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080055import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070056import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080057import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070058
Sunny Goyal819e1932016-07-07 16:43:58 -070059import org.xmlpull.v1.XmlPullParser;
60import org.xmlpull.v1.XmlPullParserException;
61
62import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070063import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080064import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070065
66public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070067
Hyunyoung Songc55a3502018-12-04 15:43:16 -080068 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070069 // We do not need any synchronization for this variable as its only written on UI thread.
70 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070071 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070072
Tracy Zhoued5f3082020-04-20 01:13:26 -070073 public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size";
74 public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count";
75
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
Sunny Goyal87dc48b2018-10-12 11:42:33 -070080 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080081 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
82
83 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070084
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070085 // Constants that affects the interpolation curve between statically defined device profile
86 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080087 private static final float KNEARESTNEIGHBOR = 3;
88 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070089
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070090 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080091 private static final float WEIGHT_EFFICIENT = 100000f;
92
93 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
94 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070095
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070096 /**
97 * Number of icons per row and column in the workspace.
98 */
Adam Cohen2e6da152015-05-06 11:42:25 -070099 public int numRows;
100 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700101
102 /**
103 * Number of icons per row and column in the folder.
104 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700105 public int numFolderRows;
106 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700107 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800108 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700109 public float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200110 public float landscapeIconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700111 public int iconBitmapSize;
112 public int fillResIconDpi;
113 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000114 public float allAppsIconSize;
115 public float allAppsIconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700116
Jon Mirandae126d722021-02-25 10:45:20 -0500117 public float minCellHeight;
118 public float minCellWidth;
119 public float borderSpacing;
120
Sunny Goyal5bc18462019-01-07 15:13:39 -0800121 private SparseArray<TypedValue> mExtraAttrs;
122
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700123 /**
124 * Number of icons inside the hotseat area.
125 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700126 protected int numShownHotseatIcons;
127
128 /**
129 * Number of icons inside the hotseat area that is stored in the database. This is greater than
130 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
131 * sizes that share the same DB.
132 */
133 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700134
Jon Miranda6f7e9702019-09-16 14:44:14 -0700135 /**
136 * Number of columns in the all apps list.
137 */
138 public int numAllAppsColumns;
139
Jon Mirandae126d722021-02-25 10:45:20 -0500140 /**
141 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
142 */
143 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400144 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500145
Tracy Zhou7df93d22020-01-27 13:44:06 -0800146 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800147 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700148 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700149
cuijiaxingabda8d72017-03-20 09:51:36 -0700150 public DeviceProfile landscapeProfile;
151 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700152
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400153 @Nullable public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500154
Sunny Goyal6f866092016-03-17 17:04:15 -0700155 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700156 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700157
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700158 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
159 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700160 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700161
Sunny Goyalf633ef52018-03-13 09:57:05 -0700162 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700163 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700164
Sunny Goyalf633ef52018-03-13 09:57:05 -0700165 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800166 numRows = p.numRows;
167 numColumns = p.numColumns;
168 numFolderRows = p.numFolderRows;
169 numFolderColumns = p.numFolderColumns;
170 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800171 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800172 landscapeIconSize = p.landscapeIconSize;
Tony Wickham9459f312020-06-15 13:30:20 -0500173 iconBitmapSize = p.iconBitmapSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800174 iconTextSize = p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200175 landscapeIconTextSize = p.landscapeIconTextSize;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700176 numShownHotseatIcons = p.numShownHotseatIcons;
177 numDatabaseHotseatIcons = p.numDatabaseHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700178 numAllAppsColumns = p.numAllAppsColumns;
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;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700190 mOverlayMonitor = p.mOverlayMonitor;
Jon Miranda228877d2021-02-09 11:05:00 -0500191 devicePaddings = p.devicePaddings;
Adam Cohen2e6da152015-05-06 11:42:25 -0700192 }
193
Sunny Goyalbbf01842015-10-08 07:41:15 -0700194 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700195 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700196 String gridName = getCurrentGridName(context);
197 String newGridName = initGrid(context, gridName);
198 if (!newGridName.equals(gridName)) {
199 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
200 }
Tracy Zhoued5f3082020-04-20 01:13:26 -0700201 Utilities.getPrefs(context).edit()
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700202 .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numDatabaseHotseatIcons)
Tracy Zhoued5f3082020-04-20 01:13:26 -0700203 .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows))
204 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700205
Sunny Goyalead22a52021-02-02 23:21:55 -0800206 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700207 mOverlayMonitor = new OverlayMonitor(context);
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
225 InvariantDeviceProfile originalProfile = INSTANCE.get(context);
226 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 Goyalfd58da62020-08-11 12:06:49 -0700230 DisplayController.getDefaultDisplay(context).getInfo(),
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700231 getPredefinedDeviceProfiles(context, gridName));
232
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(
235 myInfo, getPredefinedDeviceProfiles(context, gridName));
236
237 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
238 .add(myDisplayOption);
239 result.iconSize = defaultDisplayOption.iconSize;
240 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700241 result.numShownHotseatIcons = myDisplayOption.numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000242 if (defaultDisplayOption.allAppsIconSize < myDisplayOption.allAppsIconSize) {
243 result.allAppsIconSize = defaultDisplayOption.allAppsIconSize;
244 result.numAllAppsColumns = defaultDisplayOption.numAllAppsColumns;
245 } else {
246 result.allAppsIconSize = myDisplayOption.allAppsIconSize;
247 result.numAllAppsColumns = myDisplayOption.numAllAppsColumns;
248 }
Jon Mirandae126d722021-02-25 10:45:20 -0500249 result.minCellHeight = defaultDisplayOption.minCellHeight;
250 result.minCellWidth = defaultDisplayOption.minCellWidth;
251 result.borderSpacing = defaultDisplayOption.borderSpacing;
Jon Miranda228877d2021-02-09 11:05:00 -0500252
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700253 initGrid(context, myInfo, result);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800254 }
255
Tracy Zhou42255d22020-03-13 00:38:11 -0700256 public static String getCurrentGridName(Context context) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100257 if (ENABLE_TWO_PANEL_HOME.get()) {
258 return ENABLE_TWO_PANEL_HOME.key;
259 }
Jon Miranda73dcc982020-12-11 14:14:46 -0800260 if (ENABLE_FOUR_COLUMNS.get()) {
261 return ENABLE_FOUR_COLUMNS.key;
262 }
Tracy Zhouc6060e62020-04-27 13:05:34 -0700263 return Utilities.isGridOptionsEnabled(context)
264 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700265 }
266
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800267 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800268 * Retrieve system defined or RRO overriden icon shape.
269 */
270 private static String getIconShapePath(Context context) {
271 if (CONFIG_ICON_MASK_RES_ID == 0) {
272 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
273 return "";
274 }
275 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
276 }
277
Sunny Goyaleff44f32019-01-09 17:29:49 -0800278 private String initGrid(Context context, String gridName) {
Sunny Goyalfd58da62020-08-11 12:06:49 -0700279 Info displayInfo = DisplayController.getDefaultDisplay(context).getInfo();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700280 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
281
282 DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions);
283 initGrid(context, displayInfo, displayOption);
284 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800285 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700286
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700287 private void initGrid(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700288 Context context, Info displayInfo, DisplayOption displayOption) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700289 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000290 numRows = closestProfile.numRows;
291 numColumns = closestProfile.numColumns;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700292 numDatabaseHotseatIcons = closestProfile.numDatabaseHotseatIcons;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000293 dbFile = closestProfile.dbFile;
294 defaultLayoutId = closestProfile.defaultLayoutId;
295 demoModeLayoutId = closestProfile.demoModeLayoutId;
296 numFolderRows = closestProfile.numFolderRows;
297 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500298 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400299 devicePaddingId = closestProfile.devicePaddingId;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000300
301 mExtraAttrs = closestProfile.extraAttrs;
302
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700303 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000304 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700305 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000306 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700307 iconTextSize = displayOption.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200308 landscapeIconTextSize = displayOption.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000309 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
310
Jon Mirandae126d722021-02-25 10:45:20 -0500311 minCellHeight = displayOption.minCellHeight;
312 minCellWidth = displayOption.minCellWidth;
313 borderSpacing = displayOption.borderSpacing;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700314 numShownHotseatIcons = Math.round(displayOption.numShownHotseatIcons);
Alex Chau7c439722021-03-24 11:32:44 +0000315 numAllAppsColumns = Math.round(displayOption.numAllAppsColumns);
Jon Mirandae126d722021-02-25 10:45:20 -0500316
Tracy Zhouc6060e62020-04-27 13:05:34 -0700317 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700318 allAppsIconSize = displayOption.allAppsIconSize;
319 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000320 } else {
321 allAppsIconSize = iconSize;
322 allAppsIconTextSize = iconTextSize;
323 }
324
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400325 if (devicePaddingId != 0) {
326 devicePaddings = new DevicePaddings(context, devicePaddingId);
327 }
328
Sunny Goyalae190ff2020-04-14 00:19:01 +0000329 // If the partner customization apk contains any grid overrides, apply them
330 // Supported overrides: numRows, numColumns, iconSize
331 applyPartnerDeviceProfileOverrides(context, displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700332
Winson Chung13c1c2c2019-09-06 11:46:19 -0700333 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700334 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700335 // same in any orientation.
336 int smallSide = Math.min(realSize.x, realSize.y);
337 int largeSide = Math.max(realSize.x, realSize.y);
338
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700339 DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo)
340 .setSizeRange(new Point(displayInfo.smallestSize),
341 new Point(displayInfo.largestSize));
Winson Chung8e64bba2021-04-20 13:26:26 -0700342 if (TestProtocol.sDebugTracing) {
343 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED,
344 "largeSide=" + largeSide + " smallSide=" + smallSide);
345 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700346
347 landscapeProfile = builder.setSize(largeSide, smallSide).build();
348 portraitProfile = builder.setSize(smallSide, largeSide).build();
Sunny Goyal6f866092016-03-17 17:04:15 -0700349
350 // We need to ensure that there is enough extra space in the wallpaper
351 // for the intended parallax effects
352 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
353 defaultWallpaperSize = new Point(
354 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
355 largeSide);
356 } else {
357 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
358 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700359
360 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
361 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700362 }
363
Sunny Goyal5bc18462019-01-07 15:13:39 -0800364 @Nullable
365 public TypedValue getAttrValue(int attr) {
366 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
367 }
368
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700369 public void addOnChangeListener(OnIDPChangeListener listener) {
370 mChangeListeners.add(listener);
371 }
372
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800373 public void removeOnChangeListener(OnIDPChangeListener listener) {
374 mChangeListeners.remove(listener);
375 }
376
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800377 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800378 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
379 // Good place to check if grid size changed in themepicker when launcher was dead.
380 if (savedIconMaskPath.isEmpty()) {
381 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
382 .apply();
383 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
384 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
385 .apply();
386 apply(context, CHANGE_FLAG_ICON_PARAMS);
387 }
388 }
389
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800390 public void setCurrentGrid(Context context, String gridName) {
391 Context appContext = context.getApplicationContext();
392 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700393 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800394 }
395
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700396 private void onConfigChanged(Context context) {
397 // Config changes, what shall we do?
398 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
399
400 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700401 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700402 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700403
404 int changeFlags = 0;
405 if (numRows != oldProfile.numRows ||
406 numColumns != oldProfile.numColumns ||
407 numFolderColumns != oldProfile.numFolderColumns ||
408 numFolderRows != oldProfile.numFolderRows ||
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700409 numDatabaseHotseatIcons != oldProfile.numDatabaseHotseatIcons) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700410 changeFlags |= CHANGE_FLAG_GRID;
411 }
412
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800413 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
414 !iconShapePath.equals(oldProfile.iconShapePath)) {
415 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700416 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800417 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700418 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800419 }
420
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800421 apply(context, changeFlags);
422 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700423
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800424 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700425 // Create a new config monitor
426 mConfigMonitor.unregister();
427 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
428
429 for (OnIDPChangeListener listener : mChangeListeners) {
430 listener.onIdpChanged(changeFlags, this);
431 }
432 }
433
Sunny Goyalae190ff2020-04-14 00:19:01 +0000434 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800435 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700436 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
437 final int depth = parser.getDepth();
438 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700439 while (((type = parser.next()) != XmlPullParser.END_TAG ||
440 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800441 if ((type == XmlPullParser.START_TAG)
442 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800443
444 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
445 final int displayDepth = parser.getDepth();
446 while (((type = parser.next()) != XmlPullParser.END_TAG ||
447 parser.getDepth() > displayDepth)
448 && type != XmlPullParser.END_DOCUMENT) {
449 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
450 parser.getName())) {
451 profiles.add(new DisplayOption(
452 gridOption, context, Xml.asAttributeSet(parser)));
453 }
454 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700455 }
456 }
457 } catch (IOException|XmlPullParserException e) {
458 throw new RuntimeException(e);
459 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800460
Sunny Goyalae190ff2020-04-14 00:19:01 +0000461 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800462 if (!TextUtils.isEmpty(gridName)) {
463 for (DisplayOption option : profiles) {
464 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000465 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800466 }
467 }
468 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000469 if (filteredProfiles.isEmpty()) {
470 // No grid found, use the default options
471 for (DisplayOption option : profiles) {
472 if (option.canBeDefault) {
473 filteredProfiles.add(option);
474 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800475 }
476 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000477 if (filteredProfiles.isEmpty()) {
478 throw new RuntimeException("No display option with canBeDefault=true");
479 }
480 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700481 }
482
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700483 private int getLauncherIconDensity(int requiredSize) {
484 // Densities typically defined by an app.
485 int[] densityBuckets = new int[] {
486 DisplayMetrics.DENSITY_LOW,
487 DisplayMetrics.DENSITY_MEDIUM,
488 DisplayMetrics.DENSITY_TV,
489 DisplayMetrics.DENSITY_HIGH,
490 DisplayMetrics.DENSITY_XHIGH,
491 DisplayMetrics.DENSITY_XXHIGH,
492 DisplayMetrics.DENSITY_XXXHIGH
493 };
494
495 int density = DisplayMetrics.DENSITY_XXXHIGH;
496 for (int i = densityBuckets.length - 1; i >= 0; i--) {
497 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
498 / DisplayMetrics.DENSITY_DEFAULT;
499 if (expectedSize >= requiredSize) {
500 density = densityBuckets[i];
501 }
502 }
503
504 return density;
505 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700506
Adam Cohen2e6da152015-05-06 11:42:25 -0700507 /**
508 * Apply any Partner customization grid overrides.
509 *
510 * Currently we support: all apps row / column count.
511 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700512 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
513 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700514 if (p != null) {
515 p.applyInvariantDeviceProfileOverrides(this, dm);
516 }
517 }
518
Sunny Goyal415f1732018-11-29 10:33:47 -0800519 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700520 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700521 }
522
Sunny Goyal415f1732018-11-29 10:33:47 -0800523 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700524 static DisplayOption invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700525 Info displayInfo, ArrayList<DisplayOption> points) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700526 Point smallestSize = new Point(displayInfo.smallestSize);
527 Point largestSize = new Point(displayInfo.largestSize);
528
529 // This guarantees that width < height
530 float width = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
531 displayInfo.metrics);
532 float height = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
533 displayInfo.metrics);
534
535 // Sort the profiles based on the closeness to the device size
536 Collections.sort(points, (a, b) ->
537 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
538 dist(width, height, b.minWidthDps, b.minHeightDps)));
539
540 GridOption closestOption = points.get(0).grid;
541 float weights = 0;
542
543 DisplayOption p = points.get(0);
544 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
545 return p;
546 }
547
548 DisplayOption out = new DisplayOption(closestOption);
549 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
550 p = points.get(i);
551 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
552 weights += w;
553 out.add(new DisplayOption().add(p).multiply(w));
554 }
555 return out.multiply(1.0f / weights);
556 }
557
558 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800559 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000560 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700561 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700562
Sunny Goyal415f1732018-11-29 10:33:47 -0800563 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700564 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
565 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700566 }
567
Sunny Goyal415f1732018-11-29 10:33:47 -0800568 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700569 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800570 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700571 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
572 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800573 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700574 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800575 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700576 }
577
Sunny Goyal27835952017-01-13 12:15:53 -0800578 public DeviceProfile getDeviceProfile(Context context) {
Winson Chung8e64bba2021-04-20 13:26:26 -0700579 if (TestProtocol.sDebugTracing) {
580 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED, "getDeviceProfile: orientation="
581 + context.getResources().getConfiguration().orientation, new Throwable());
582 }
Sunny Goyal27835952017-01-13 12:15:53 -0800583 return context.getResources().getConfiguration().orientation
584 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
585 }
586
Sunny Goyal415f1732018-11-29 10:33:47 -0800587 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700588 float d = dist(x0, y0, x1, y1);
589 if (Float.compare(d, 0f) == 0) {
590 return Float.POSITIVE_INFINITY;
591 }
592 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
593 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700594
595 /**
596 * As a ratio of screen height, the total distance we want the parallax effect to span
597 * horizontally
598 */
599 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
600 float aspectRatio = width / (float) height;
601
602 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
603 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
604 // We will use these two data points to extrapolate how much the wallpaper parallax effect
605 // to span (ie travel) at any aspect ratio:
606
607 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
608 final float ASPECT_RATIO_PORTRAIT = 10/16f;
609 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
610 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
611
612 // To find out the desired width at different aspect ratios, we use the following two
613 // formulas, where the coefficient on x is the aspect ratio (width/height):
614 // (16/10)x + y = 1.5
615 // (10/16)x + y = 1.2
616 // We solve for x and y and end up with a final formula:
617 final float x =
618 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
619 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
620 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
621 return x * aspectRatio + y;
622 }
623
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700624 public interface OnIDPChangeListener {
625
626 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
627 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800628
629
Sunny Goyaleff44f32019-01-09 17:29:49 -0800630 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800631
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800632 public static final String TAG_NAME = "grid-option";
633
Sunny Goyaleff44f32019-01-09 17:29:49 -0800634 public final String name;
635 public final int numRows;
636 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800637
638 private final int numFolderRows;
639 private final int numFolderColumns;
640
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700641 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800642
Tracy Zhou7df93d22020-01-27 13:44:06 -0800643 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000644
Sunny Goyal415f1732018-11-29 10:33:47 -0800645 private final int defaultLayoutId;
646 private final int demoModeLayoutId;
647
Jon Mirandae126d722021-02-25 10:45:20 -0500648 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400649 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500650
Jon Mirandaec1277e2021-03-25 10:41:54 -0400651 public final boolean visible;
652
Sunny Goyal5bc18462019-01-07 15:13:39 -0800653 private final SparseArray<TypedValue> extraAttrs;
654
Sunny Goyaleff44f32019-01-09 17:29:49 -0800655 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800656 TypedArray a = context.obtainStyledAttributes(
657 attrs, R.styleable.GridDisplayOption);
658 name = a.getString(R.styleable.GridDisplayOption_name);
659 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
660 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
661
Tracy Zhou7df93d22020-01-27 13:44:06 -0800662 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800663 defaultLayoutId = a.getResourceId(
664 R.styleable.GridDisplayOption_defaultLayoutId, 0);
665 demoModeLayoutId = a.getResourceId(
666 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700667 numDatabaseHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800668 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
669 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
Jon Mirandaec1277e2021-03-25 10:41:54 -0400679 visible = a.getBoolean(R.styleable.GridDisplayOption_visible, true);
680
Sunny Goyal415f1732018-11-29 10:33:47 -0800681 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800682
683 extraAttrs = Themes.createValueMap(context, attrs,
684 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800685 }
686 }
687
688 private static final class DisplayOption {
689 private final GridOption grid;
690
Sunny Goyal415f1732018-11-29 10:33:47 -0800691 private final float minWidthDps;
692 private final float minHeightDps;
693 private final boolean canBeDefault;
694
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700695 private float numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000696 private float numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500697 private float minCellHeight;
698 private float minCellWidth;
699 private float borderSpacing;
700
Sunny Goyal415f1732018-11-29 10:33:47 -0800701 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800702 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700703 private float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200704 private float landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000705 private float allAppsIconSize;
706 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800707
708 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
709 this.grid = grid;
710
711 TypedArray a = context.obtainStyledAttributes(
712 attrs, R.styleable.ProfileDisplayOption);
713
Sunny Goyal415f1732018-11-29 10:33:47 -0800714 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
715 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
716 canBeDefault = a.getBoolean(
717 R.styleable.ProfileDisplayOption_canBeDefault, false);
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700718 numShownHotseatIcons = a.getInt(R.styleable.ProfileDisplayOption_numShownHotseatIcons,
719 grid.numDatabaseHotseatIcons);
Alex Chau7c439722021-03-24 11:32:44 +0000720 numAllAppsColumns = a.getInt(R.styleable.ProfileDisplayOption_numAllAppsColumns,
721 grid.numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800722
Jon Mirandae126d722021-02-25 10:45:20 -0500723 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
724 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
725 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
726
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700727 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800728 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
729 iconSize);
730 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Andras Kloczl6057cb92021-04-07 13:45:17 +0200731 landscapeIconTextSize = a.getFloat(
732 R.styleable.ProfileDisplayOption_landscapeIconTextSize, iconTextSize);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700733
Sunny Goyalae190ff2020-04-14 00:19:01 +0000734 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
735 iconSize);
736 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
737 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800738 a.recycle();
739 }
740
741 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700742 this(null);
743 }
744
745 DisplayOption(GridOption grid) {
746 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800747 minWidthDps = 0;
748 minHeightDps = 0;
749 canBeDefault = false;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700750 numShownHotseatIcons = 0;
Alex Chau7c439722021-03-24 11:32:44 +0000751 numAllAppsColumns = 0;
Jon Mirandae126d722021-02-25 10:45:20 -0500752 minCellHeight = 0;
753 minCellWidth = 0;
754 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800755 }
756
757 private DisplayOption multiply(float w) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700758 numShownHotseatIcons *= w;
Alex Chau7c439722021-03-24 11:32:44 +0000759 numAllAppsColumns *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800760 iconSize *= w;
761 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000762 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800763 iconTextSize *= w;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200764 landscapeIconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000765 allAppsIconTextSize *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500766 minCellHeight *= w;
767 minCellWidth *= w;
768 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800769 return this;
770 }
771
772 private DisplayOption add(DisplayOption p) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700773 numShownHotseatIcons += p.numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000774 numAllAppsColumns += p.numAllAppsColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800775 iconSize += p.iconSize;
776 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000777 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800778 iconTextSize += p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200779 landscapeIconTextSize += p.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000780 allAppsIconTextSize += p.allAppsIconTextSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500781 minCellHeight += p.minCellHeight;
782 minCellWidth += p.minCellWidth;
783 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800784 return this;
785 }
786 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700787
788 private class OverlayMonitor extends BroadcastReceiver {
789
790 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
791
792 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700793 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700794 }
795
796 @Override
797 public void onReceive(Context context, Intent intent) {
798 onConfigChanged(context);
799 }
800 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000801}