blob: 5612dafe237a2f54bb0565ae1158c274400e0a8f [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;
Sunny Goyald0e360a2018-06-29 14:40:18 -070051import com.android.launcher3.util.ConfigMonitor;
Sunny Goyalfd58da62020-08-11 12:06:49 -070052import com.android.launcher3.util.DisplayController;
53import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080054import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070055import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080056import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070057
Sunny Goyal819e1932016-07-07 16:43:58 -070058import org.xmlpull.v1.XmlPullParser;
59import org.xmlpull.v1.XmlPullParserException;
60
61import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070062import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080063import java.util.Collections;
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
Tracy Zhoued5f3082020-04-20 01:13:26 -070072 public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size";
73 public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count";
74
Hyunyoung Songc55a3502018-12-04 15:43:16 -080075 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080076
Sunny Goyal53d7ee42015-05-22 12:25:45 -070077 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
78
Sunny Goyal87dc48b2018-10-12 11:42:33 -070079 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080080 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
81
82 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070083
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070084 // Constants that affects the interpolation curve between statically defined device profile
85 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080086 private static final float KNEARESTNEIGHBOR = 3;
87 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070088
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070089 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080090 private static final float WEIGHT_EFFICIENT = 100000f;
91
92 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
93 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070094
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070095 /**
96 * Number of icons per row and column in the workspace.
97 */
Adam Cohen2e6da152015-05-06 11:42:25 -070098 public int numRows;
99 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700100
101 /**
102 * Number of icons per row and column in the folder.
103 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700104 public int numFolderRows;
105 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700106 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800107 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700108 public float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200109 public float landscapeIconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700110 public int iconBitmapSize;
111 public int fillResIconDpi;
112 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000113 public float allAppsIconSize;
114 public float allAppsIconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700115
Jon Mirandae126d722021-02-25 10:45:20 -0500116 public float minCellHeight;
117 public float minCellWidth;
118 public float borderSpacing;
119
Sunny Goyal5bc18462019-01-07 15:13:39 -0800120 private SparseArray<TypedValue> mExtraAttrs;
121
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700122 /**
123 * Number of icons inside the hotseat area.
124 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800125 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700126
Jon Miranda6f7e9702019-09-16 14:44:14 -0700127 /**
128 * Number of columns in the all apps list.
129 */
130 public int numAllAppsColumns;
131
Jon Mirandae126d722021-02-25 10:45:20 -0500132 /**
133 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
134 */
135 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400136 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500137
Tracy Zhou7df93d22020-01-27 13:44:06 -0800138 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800139 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700140 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700141
cuijiaxingabda8d72017-03-20 09:51:36 -0700142 public DeviceProfile landscapeProfile;
143 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700144
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400145 @Nullable public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500146
Sunny Goyal6f866092016-03-17 17:04:15 -0700147 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700148 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700149
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700150 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
151 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700152 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700153
Sunny Goyalf633ef52018-03-13 09:57:05 -0700154 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700155 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700156
Sunny Goyalf633ef52018-03-13 09:57:05 -0700157 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800158 numRows = p.numRows;
159 numColumns = p.numColumns;
160 numFolderRows = p.numFolderRows;
161 numFolderColumns = p.numFolderColumns;
162 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800163 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800164 landscapeIconSize = p.landscapeIconSize;
Tony Wickham9459f312020-06-15 13:30:20 -0500165 iconBitmapSize = p.iconBitmapSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800166 iconTextSize = p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200167 landscapeIconTextSize = p.landscapeIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800168 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700169 numAllAppsColumns = p.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500170 isScalable = p.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400171 devicePaddingId = p.devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500172 minCellHeight = p.minCellHeight;
173 minCellWidth = p.minCellWidth;
174 borderSpacing = p.borderSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800175 dbFile = p.dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000176 allAppsIconSize = p.allAppsIconSize;
177 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800178 defaultLayoutId = p.defaultLayoutId;
179 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800180 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700181 mOverlayMonitor = p.mOverlayMonitor;
Jon Miranda228877d2021-02-09 11:05:00 -0500182 devicePaddings = p.devicePaddings;
Adam Cohen2e6da152015-05-06 11:42:25 -0700183 }
184
Sunny Goyalbbf01842015-10-08 07:41:15 -0700185 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700186 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700187 String gridName = getCurrentGridName(context);
188 String newGridName = initGrid(context, gridName);
189 if (!newGridName.equals(gridName)) {
190 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
191 }
Tracy Zhoued5f3082020-04-20 01:13:26 -0700192 Utilities.getPrefs(context).edit()
193 .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numHotseatIcons)
194 .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows))
195 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700196
Sunny Goyalead22a52021-02-02 23:21:55 -0800197 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700198 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700199 }
200
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700201 /**
202 * This constructor should NOT have any monitors by design.
203 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800204 public InvariantDeviceProfile(Context context, String gridName) {
205 String newName = initGrid(context, gridName);
206 if (newName == null || !newName.equals(gridName)) {
207 throw new IllegalArgumentException("Unknown grid name");
208 }
209 }
210
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700211 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800212 * This constructor should NOT have any monitors by design.
213 */
214 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700215 // Ensure that the main device profile is initialized
216 InvariantDeviceProfile originalProfile = INSTANCE.get(context);
217 String gridName = getCurrentGridName(context);
218
219 // Get the display info based on default display and interpolate it to existing display
220 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700221 DisplayController.getDefaultDisplay(context).getInfo(),
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700222 getPredefinedDeviceProfiles(context, gridName));
223
Alex Chau417bd722021-01-26 15:22:18 +0000224 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700225 DisplayOption myDisplayOption = invDistWeightedInterpolate(
226 myInfo, getPredefinedDeviceProfiles(context, gridName));
227
228 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
229 .add(myDisplayOption);
230 result.iconSize = defaultDisplayOption.iconSize;
231 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
Alex Chau7c439722021-03-24 11:32:44 +0000232 if (defaultDisplayOption.allAppsIconSize < myDisplayOption.allAppsIconSize) {
233 result.allAppsIconSize = defaultDisplayOption.allAppsIconSize;
234 result.numAllAppsColumns = defaultDisplayOption.numAllAppsColumns;
235 } else {
236 result.allAppsIconSize = myDisplayOption.allAppsIconSize;
237 result.numAllAppsColumns = myDisplayOption.numAllAppsColumns;
238 }
Jon Mirandae126d722021-02-25 10:45:20 -0500239 result.minCellHeight = defaultDisplayOption.minCellHeight;
240 result.minCellWidth = defaultDisplayOption.minCellWidth;
241 result.borderSpacing = defaultDisplayOption.borderSpacing;
Jon Miranda228877d2021-02-09 11:05:00 -0500242
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700243 initGrid(context, myInfo, result);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800244 }
245
Tracy Zhou42255d22020-03-13 00:38:11 -0700246 public static String getCurrentGridName(Context context) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100247 if (ENABLE_TWO_PANEL_HOME.get()) {
248 return ENABLE_TWO_PANEL_HOME.key;
249 }
Jon Miranda73dcc982020-12-11 14:14:46 -0800250 if (ENABLE_FOUR_COLUMNS.get()) {
251 return ENABLE_FOUR_COLUMNS.key;
252 }
Tracy Zhouc6060e62020-04-27 13:05:34 -0700253 return Utilities.isGridOptionsEnabled(context)
254 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700255 }
256
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800257 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800258 * Retrieve system defined or RRO overriden icon shape.
259 */
260 private static String getIconShapePath(Context context) {
261 if (CONFIG_ICON_MASK_RES_ID == 0) {
262 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
263 return "";
264 }
265 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
266 }
267
Sunny Goyaleff44f32019-01-09 17:29:49 -0800268 private String initGrid(Context context, String gridName) {
Sunny Goyalfd58da62020-08-11 12:06:49 -0700269 Info displayInfo = DisplayController.getDefaultDisplay(context).getInfo();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700270 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
271
272 DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions);
273 initGrid(context, displayInfo, displayOption);
274 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800275 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700276
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700277 private void initGrid(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700278 Context context, Info displayInfo, DisplayOption displayOption) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700279 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000280 numRows = closestProfile.numRows;
281 numColumns = closestProfile.numColumns;
282 numHotseatIcons = closestProfile.numHotseatIcons;
283 dbFile = closestProfile.dbFile;
284 defaultLayoutId = closestProfile.defaultLayoutId;
285 demoModeLayoutId = closestProfile.demoModeLayoutId;
286 numFolderRows = closestProfile.numFolderRows;
287 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500288 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400289 devicePaddingId = closestProfile.devicePaddingId;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000290
291 mExtraAttrs = closestProfile.extraAttrs;
292
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700293 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000294 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700295 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000296 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700297 iconTextSize = displayOption.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200298 landscapeIconTextSize = displayOption.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000299 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
300
Jon Mirandae126d722021-02-25 10:45:20 -0500301 minCellHeight = displayOption.minCellHeight;
302 minCellWidth = displayOption.minCellWidth;
303 borderSpacing = displayOption.borderSpacing;
Alex Chau7c439722021-03-24 11:32:44 +0000304 numAllAppsColumns = Math.round(displayOption.numAllAppsColumns);
Jon Mirandae126d722021-02-25 10:45:20 -0500305
Tracy Zhouc6060e62020-04-27 13:05:34 -0700306 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700307 allAppsIconSize = displayOption.allAppsIconSize;
308 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000309 } else {
310 allAppsIconSize = iconSize;
311 allAppsIconTextSize = iconTextSize;
312 }
313
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400314 if (devicePaddingId != 0) {
315 devicePaddings = new DevicePaddings(context, devicePaddingId);
316 }
317
Sunny Goyalae190ff2020-04-14 00:19:01 +0000318 // If the partner customization apk contains any grid overrides, apply them
319 // Supported overrides: numRows, numColumns, iconSize
320 applyPartnerDeviceProfileOverrides(context, displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700321
Winson Chung13c1c2c2019-09-06 11:46:19 -0700322 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700323 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700324 // same in any orientation.
325 int smallSide = Math.min(realSize.x, realSize.y);
326 int largeSide = Math.max(realSize.x, realSize.y);
327
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700328 DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo)
329 .setSizeRange(new Point(displayInfo.smallestSize),
330 new Point(displayInfo.largestSize));
331
332 landscapeProfile = builder.setSize(largeSide, smallSide).build();
333 portraitProfile = builder.setSize(smallSide, largeSide).build();
Sunny Goyal6f866092016-03-17 17:04:15 -0700334
335 // We need to ensure that there is enough extra space in the wallpaper
336 // for the intended parallax effects
337 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
338 defaultWallpaperSize = new Point(
339 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
340 largeSide);
341 } else {
342 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
343 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700344
345 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
346 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700347 }
348
Sunny Goyal5bc18462019-01-07 15:13:39 -0800349 @Nullable
350 public TypedValue getAttrValue(int attr) {
351 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
352 }
353
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700354 public void addOnChangeListener(OnIDPChangeListener listener) {
355 mChangeListeners.add(listener);
356 }
357
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800358 public void removeOnChangeListener(OnIDPChangeListener listener) {
359 mChangeListeners.remove(listener);
360 }
361
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800362 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800363 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
364 // Good place to check if grid size changed in themepicker when launcher was dead.
365 if (savedIconMaskPath.isEmpty()) {
366 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
367 .apply();
368 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
369 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
370 .apply();
371 apply(context, CHANGE_FLAG_ICON_PARAMS);
372 }
373 }
374
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800375 public void setCurrentGrid(Context context, String gridName) {
376 Context appContext = context.getApplicationContext();
377 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700378 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800379 }
380
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700381 private void onConfigChanged(Context context) {
382 // Config changes, what shall we do?
383 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
384
385 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700386 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700387 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700388
389 int changeFlags = 0;
390 if (numRows != oldProfile.numRows ||
391 numColumns != oldProfile.numColumns ||
392 numFolderColumns != oldProfile.numFolderColumns ||
393 numFolderRows != oldProfile.numFolderRows ||
394 numHotseatIcons != oldProfile.numHotseatIcons) {
395 changeFlags |= CHANGE_FLAG_GRID;
396 }
397
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800398 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
399 !iconShapePath.equals(oldProfile.iconShapePath)) {
400 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700401 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800402 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700403 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800404 }
405
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800406 apply(context, changeFlags);
407 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700408
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800409 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700410 // Create a new config monitor
411 mConfigMonitor.unregister();
412 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
413
414 for (OnIDPChangeListener listener : mChangeListeners) {
415 listener.onIdpChanged(changeFlags, this);
416 }
417 }
418
Sunny Goyalae190ff2020-04-14 00:19:01 +0000419 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800420 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700421 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
422 final int depth = parser.getDepth();
423 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700424 while (((type = parser.next()) != XmlPullParser.END_TAG ||
425 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800426 if ((type == XmlPullParser.START_TAG)
427 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800428
429 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
430 final int displayDepth = parser.getDepth();
431 while (((type = parser.next()) != XmlPullParser.END_TAG ||
432 parser.getDepth() > displayDepth)
433 && type != XmlPullParser.END_DOCUMENT) {
434 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
435 parser.getName())) {
436 profiles.add(new DisplayOption(
437 gridOption, context, Xml.asAttributeSet(parser)));
438 }
439 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700440 }
441 }
442 } catch (IOException|XmlPullParserException e) {
443 throw new RuntimeException(e);
444 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800445
Sunny Goyalae190ff2020-04-14 00:19:01 +0000446 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800447 if (!TextUtils.isEmpty(gridName)) {
448 for (DisplayOption option : profiles) {
449 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000450 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800451 }
452 }
453 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000454 if (filteredProfiles.isEmpty()) {
455 // No grid found, use the default options
456 for (DisplayOption option : profiles) {
457 if (option.canBeDefault) {
458 filteredProfiles.add(option);
459 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800460 }
461 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000462 if (filteredProfiles.isEmpty()) {
463 throw new RuntimeException("No display option with canBeDefault=true");
464 }
465 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700466 }
467
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700468 private int getLauncherIconDensity(int requiredSize) {
469 // Densities typically defined by an app.
470 int[] densityBuckets = new int[] {
471 DisplayMetrics.DENSITY_LOW,
472 DisplayMetrics.DENSITY_MEDIUM,
473 DisplayMetrics.DENSITY_TV,
474 DisplayMetrics.DENSITY_HIGH,
475 DisplayMetrics.DENSITY_XHIGH,
476 DisplayMetrics.DENSITY_XXHIGH,
477 DisplayMetrics.DENSITY_XXXHIGH
478 };
479
480 int density = DisplayMetrics.DENSITY_XXXHIGH;
481 for (int i = densityBuckets.length - 1; i >= 0; i--) {
482 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
483 / DisplayMetrics.DENSITY_DEFAULT;
484 if (expectedSize >= requiredSize) {
485 density = densityBuckets[i];
486 }
487 }
488
489 return density;
490 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700491
Adam Cohen2e6da152015-05-06 11:42:25 -0700492 /**
493 * Apply any Partner customization grid overrides.
494 *
495 * Currently we support: all apps row / column count.
496 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700497 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
498 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700499 if (p != null) {
500 p.applyInvariantDeviceProfileOverrides(this, dm);
501 }
502 }
503
Sunny Goyal415f1732018-11-29 10:33:47 -0800504 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700505 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700506 }
507
Sunny Goyal415f1732018-11-29 10:33:47 -0800508 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700509 static DisplayOption invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700510 Info displayInfo, ArrayList<DisplayOption> points) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700511 Point smallestSize = new Point(displayInfo.smallestSize);
512 Point largestSize = new Point(displayInfo.largestSize);
513
514 // This guarantees that width < height
515 float width = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
516 displayInfo.metrics);
517 float height = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
518 displayInfo.metrics);
519
520 // Sort the profiles based on the closeness to the device size
521 Collections.sort(points, (a, b) ->
522 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
523 dist(width, height, b.minWidthDps, b.minHeightDps)));
524
525 GridOption closestOption = points.get(0).grid;
526 float weights = 0;
527
528 DisplayOption p = points.get(0);
529 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
530 return p;
531 }
532
533 DisplayOption out = new DisplayOption(closestOption);
534 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
535 p = points.get(i);
536 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
537 weights += w;
538 out.add(new DisplayOption().add(p).multiply(w));
539 }
540 return out.multiply(1.0f / weights);
541 }
542
543 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800544 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000545 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700546 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700547
Sunny Goyal415f1732018-11-29 10:33:47 -0800548 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700549 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
550 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700551 }
552
Sunny Goyal415f1732018-11-29 10:33:47 -0800553 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700554 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800555 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700556 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
557 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800558 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700559 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800560 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700561 }
562
Sunny Goyal27835952017-01-13 12:15:53 -0800563 public DeviceProfile getDeviceProfile(Context context) {
564 return context.getResources().getConfiguration().orientation
565 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
566 }
567
Sunny Goyal415f1732018-11-29 10:33:47 -0800568 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700569 float d = dist(x0, y0, x1, y1);
570 if (Float.compare(d, 0f) == 0) {
571 return Float.POSITIVE_INFINITY;
572 }
573 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
574 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700575
576 /**
577 * As a ratio of screen height, the total distance we want the parallax effect to span
578 * horizontally
579 */
580 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
581 float aspectRatio = width / (float) height;
582
583 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
584 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
585 // We will use these two data points to extrapolate how much the wallpaper parallax effect
586 // to span (ie travel) at any aspect ratio:
587
588 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
589 final float ASPECT_RATIO_PORTRAIT = 10/16f;
590 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
591 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
592
593 // To find out the desired width at different aspect ratios, we use the following two
594 // formulas, where the coefficient on x is the aspect ratio (width/height):
595 // (16/10)x + y = 1.5
596 // (10/16)x + y = 1.2
597 // We solve for x and y and end up with a final formula:
598 final float x =
599 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
600 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
601 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
602 return x * aspectRatio + y;
603 }
604
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700605 public interface OnIDPChangeListener {
606
607 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
608 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800609
610
Sunny Goyaleff44f32019-01-09 17:29:49 -0800611 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800612
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800613 public static final String TAG_NAME = "grid-option";
614
Sunny Goyaleff44f32019-01-09 17:29:49 -0800615 public final String name;
616 public final int numRows;
617 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800618
619 private final int numFolderRows;
620 private final int numFolderColumns;
621
622 private final int numHotseatIcons;
623
Tracy Zhou7df93d22020-01-27 13:44:06 -0800624 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000625
Sunny Goyal415f1732018-11-29 10:33:47 -0800626 private final int defaultLayoutId;
627 private final int demoModeLayoutId;
628
Jon Mirandae126d722021-02-25 10:45:20 -0500629 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400630 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500631
Jon Mirandaec1277e2021-03-25 10:41:54 -0400632 public final boolean visible;
633
Sunny Goyal5bc18462019-01-07 15:13:39 -0800634 private final SparseArray<TypedValue> extraAttrs;
635
Sunny Goyaleff44f32019-01-09 17:29:49 -0800636 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800637 TypedArray a = context.obtainStyledAttributes(
638 attrs, R.styleable.GridDisplayOption);
639 name = a.getString(R.styleable.GridDisplayOption_name);
640 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
641 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
642
Tracy Zhou7df93d22020-01-27 13:44:06 -0800643 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800644 defaultLayoutId = a.getResourceId(
645 R.styleable.GridDisplayOption_defaultLayoutId, 0);
646 demoModeLayoutId = a.getResourceId(
647 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
648 numHotseatIcons = a.getInt(
649 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
650 numFolderRows = a.getInt(
651 R.styleable.GridDisplayOption_numFolderRows, numRows);
652 numFolderColumns = a.getInt(
653 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700654
Jon Mirandae126d722021-02-25 10:45:20 -0500655 isScalable = a.getBoolean(
656 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400657 devicePaddingId = a.getResourceId(
658 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500659
Jon Mirandaec1277e2021-03-25 10:41:54 -0400660 visible = a.getBoolean(R.styleable.GridDisplayOption_visible, true);
661
Sunny Goyal415f1732018-11-29 10:33:47 -0800662 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800663
664 extraAttrs = Themes.createValueMap(context, attrs,
665 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800666 }
667 }
668
669 private static final class DisplayOption {
670 private final GridOption grid;
671
Sunny Goyal415f1732018-11-29 10:33:47 -0800672 private final float minWidthDps;
673 private final float minHeightDps;
674 private final boolean canBeDefault;
675
Alex Chau7c439722021-03-24 11:32:44 +0000676 private float numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500677 private float minCellHeight;
678 private float minCellWidth;
679 private float borderSpacing;
680
Sunny Goyal415f1732018-11-29 10:33:47 -0800681 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800682 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700683 private float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200684 private float landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000685 private float allAppsIconSize;
686 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800687
688 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
689 this.grid = grid;
690
691 TypedArray a = context.obtainStyledAttributes(
692 attrs, R.styleable.ProfileDisplayOption);
693
Sunny Goyal415f1732018-11-29 10:33:47 -0800694 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
695 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
696 canBeDefault = a.getBoolean(
697 R.styleable.ProfileDisplayOption_canBeDefault, false);
Alex Chau7c439722021-03-24 11:32:44 +0000698 numAllAppsColumns = a.getInt(R.styleable.ProfileDisplayOption_numAllAppsColumns,
699 grid.numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800700
Jon Mirandae126d722021-02-25 10:45:20 -0500701 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
702 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
703 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
704
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700705 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800706 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
707 iconSize);
708 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Andras Kloczl6057cb92021-04-07 13:45:17 +0200709 landscapeIconTextSize = a.getFloat(
710 R.styleable.ProfileDisplayOption_landscapeIconTextSize, iconTextSize);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700711
Sunny Goyalae190ff2020-04-14 00:19:01 +0000712 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
713 iconSize);
714 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
715 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800716 a.recycle();
717 }
718
719 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700720 this(null);
721 }
722
723 DisplayOption(GridOption grid) {
724 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800725 minWidthDps = 0;
726 minHeightDps = 0;
727 canBeDefault = false;
Alex Chau7c439722021-03-24 11:32:44 +0000728 numAllAppsColumns = 0;
Jon Mirandae126d722021-02-25 10:45:20 -0500729 minCellHeight = 0;
730 minCellWidth = 0;
731 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800732 }
733
734 private DisplayOption multiply(float w) {
Alex Chau7c439722021-03-24 11:32:44 +0000735 numAllAppsColumns *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800736 iconSize *= w;
737 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000738 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800739 iconTextSize *= w;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200740 landscapeIconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000741 allAppsIconTextSize *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500742 minCellHeight *= w;
743 minCellWidth *= w;
744 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800745 return this;
746 }
747
748 private DisplayOption add(DisplayOption p) {
Alex Chau7c439722021-03-24 11:32:44 +0000749 numAllAppsColumns += p.numAllAppsColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800750 iconSize += p.iconSize;
751 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000752 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800753 iconTextSize += p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200754 landscapeIconTextSize += p.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000755 allAppsIconTextSize += p.allAppsIconTextSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500756 minCellHeight += p.minCellHeight;
757 minCellWidth += p.minCellWidth;
758 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800759 return this;
760 }
761 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700762
763 private class OverlayMonitor extends BroadcastReceiver {
764
765 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
766
767 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700768 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700769 }
770
771 @Override
772 public void onReceive(Context context, Intent intent) {
773 onConfigChanged(context);
774 }
775 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000776}