blob: 61023be0197188e65e33eb64a85437a42e3cab27 [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) {
Winson Chungdc0f3972021-04-20 18:18:51 -0700397 if (TestProtocol.sDebugTracing) {
398 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED, "IDP.onConfigChanged");
399 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700400 // Config changes, what shall we do?
401 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
402
403 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700404 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700405 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700406
407 int changeFlags = 0;
408 if (numRows != oldProfile.numRows ||
409 numColumns != oldProfile.numColumns ||
410 numFolderColumns != oldProfile.numFolderColumns ||
411 numFolderRows != oldProfile.numFolderRows ||
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700412 numDatabaseHotseatIcons != oldProfile.numDatabaseHotseatIcons) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700413 changeFlags |= CHANGE_FLAG_GRID;
414 }
415
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800416 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
417 !iconShapePath.equals(oldProfile.iconShapePath)) {
418 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700419 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800420 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700421 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800422 }
423
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800424 apply(context, changeFlags);
425 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700426
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800427 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700428 // Create a new config monitor
429 mConfigMonitor.unregister();
430 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
431
432 for (OnIDPChangeListener listener : mChangeListeners) {
433 listener.onIdpChanged(changeFlags, this);
434 }
435 }
436
Sunny Goyalae190ff2020-04-14 00:19:01 +0000437 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800438 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700439 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
440 final int depth = parser.getDepth();
441 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700442 while (((type = parser.next()) != XmlPullParser.END_TAG ||
443 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800444 if ((type == XmlPullParser.START_TAG)
445 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800446
447 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
448 final int displayDepth = parser.getDepth();
449 while (((type = parser.next()) != XmlPullParser.END_TAG ||
450 parser.getDepth() > displayDepth)
451 && type != XmlPullParser.END_DOCUMENT) {
452 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
453 parser.getName())) {
454 profiles.add(new DisplayOption(
455 gridOption, context, Xml.asAttributeSet(parser)));
456 }
457 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700458 }
459 }
460 } catch (IOException|XmlPullParserException e) {
461 throw new RuntimeException(e);
462 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800463
Sunny Goyalae190ff2020-04-14 00:19:01 +0000464 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800465 if (!TextUtils.isEmpty(gridName)) {
466 for (DisplayOption option : profiles) {
467 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000468 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800469 }
470 }
471 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000472 if (filteredProfiles.isEmpty()) {
473 // No grid found, use the default options
474 for (DisplayOption option : profiles) {
475 if (option.canBeDefault) {
476 filteredProfiles.add(option);
477 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800478 }
479 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000480 if (filteredProfiles.isEmpty()) {
481 throw new RuntimeException("No display option with canBeDefault=true");
482 }
483 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700484 }
485
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700486 private int getLauncherIconDensity(int requiredSize) {
487 // Densities typically defined by an app.
488 int[] densityBuckets = new int[] {
489 DisplayMetrics.DENSITY_LOW,
490 DisplayMetrics.DENSITY_MEDIUM,
491 DisplayMetrics.DENSITY_TV,
492 DisplayMetrics.DENSITY_HIGH,
493 DisplayMetrics.DENSITY_XHIGH,
494 DisplayMetrics.DENSITY_XXHIGH,
495 DisplayMetrics.DENSITY_XXXHIGH
496 };
497
498 int density = DisplayMetrics.DENSITY_XXXHIGH;
499 for (int i = densityBuckets.length - 1; i >= 0; i--) {
500 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
501 / DisplayMetrics.DENSITY_DEFAULT;
502 if (expectedSize >= requiredSize) {
503 density = densityBuckets[i];
504 }
505 }
506
507 return density;
508 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700509
Adam Cohen2e6da152015-05-06 11:42:25 -0700510 /**
511 * Apply any Partner customization grid overrides.
512 *
513 * Currently we support: all apps row / column count.
514 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700515 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
516 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700517 if (p != null) {
518 p.applyInvariantDeviceProfileOverrides(this, dm);
519 }
520 }
521
Sunny Goyal415f1732018-11-29 10:33:47 -0800522 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700523 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700524 }
525
Sunny Goyal415f1732018-11-29 10:33:47 -0800526 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700527 static DisplayOption invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700528 Info displayInfo, ArrayList<DisplayOption> points) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700529 Point smallestSize = new Point(displayInfo.smallestSize);
530 Point largestSize = new Point(displayInfo.largestSize);
531
532 // This guarantees that width < height
533 float width = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
534 displayInfo.metrics);
535 float height = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
536 displayInfo.metrics);
537
538 // Sort the profiles based on the closeness to the device size
539 Collections.sort(points, (a, b) ->
540 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
541 dist(width, height, b.minWidthDps, b.minHeightDps)));
542
543 GridOption closestOption = points.get(0).grid;
544 float weights = 0;
545
546 DisplayOption p = points.get(0);
547 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
548 return p;
549 }
550
551 DisplayOption out = new DisplayOption(closestOption);
552 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
553 p = points.get(i);
554 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
555 weights += w;
556 out.add(new DisplayOption().add(p).multiply(w));
557 }
558 return out.multiply(1.0f / weights);
559 }
560
561 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800562 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000563 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700564 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700565
Sunny Goyal415f1732018-11-29 10:33:47 -0800566 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700567 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
568 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700569 }
570
Sunny Goyal415f1732018-11-29 10:33:47 -0800571 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700572 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800573 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700574 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
575 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800576 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700577 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800578 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700579 }
580
Sunny Goyal27835952017-01-13 12:15:53 -0800581 public DeviceProfile getDeviceProfile(Context context) {
Winson Chung8e64bba2021-04-20 13:26:26 -0700582 if (TestProtocol.sDebugTracing) {
583 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED, "getDeviceProfile: orientation="
Winson Chung0bba6a72021-04-20 15:41:51 -0700584 + context.getResources().getConfiguration().orientation);
Winson Chung8e64bba2021-04-20 13:26:26 -0700585 }
Sunny Goyal27835952017-01-13 12:15:53 -0800586 return context.getResources().getConfiguration().orientation
587 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
588 }
589
Sunny Goyal415f1732018-11-29 10:33:47 -0800590 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700591 float d = dist(x0, y0, x1, y1);
592 if (Float.compare(d, 0f) == 0) {
593 return Float.POSITIVE_INFINITY;
594 }
595 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
596 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700597
598 /**
599 * As a ratio of screen height, the total distance we want the parallax effect to span
600 * horizontally
601 */
602 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
603 float aspectRatio = width / (float) height;
604
605 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
606 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
607 // We will use these two data points to extrapolate how much the wallpaper parallax effect
608 // to span (ie travel) at any aspect ratio:
609
610 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
611 final float ASPECT_RATIO_PORTRAIT = 10/16f;
612 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
613 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
614
615 // To find out the desired width at different aspect ratios, we use the following two
616 // formulas, where the coefficient on x is the aspect ratio (width/height):
617 // (16/10)x + y = 1.5
618 // (10/16)x + y = 1.2
619 // We solve for x and y and end up with a final formula:
620 final float x =
621 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
622 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
623 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
624 return x * aspectRatio + y;
625 }
626
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700627 public interface OnIDPChangeListener {
628
629 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
630 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800631
632
Sunny Goyaleff44f32019-01-09 17:29:49 -0800633 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800634
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800635 public static final String TAG_NAME = "grid-option";
636
Sunny Goyaleff44f32019-01-09 17:29:49 -0800637 public final String name;
638 public final int numRows;
639 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800640
641 private final int numFolderRows;
642 private final int numFolderColumns;
643
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700644 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800645
Tracy Zhou7df93d22020-01-27 13:44:06 -0800646 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000647
Sunny Goyal415f1732018-11-29 10:33:47 -0800648 private final int defaultLayoutId;
649 private final int demoModeLayoutId;
650
Jon Mirandae126d722021-02-25 10:45:20 -0500651 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400652 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500653
Jon Mirandaec1277e2021-03-25 10:41:54 -0400654 public final boolean visible;
655
Sunny Goyal5bc18462019-01-07 15:13:39 -0800656 private final SparseArray<TypedValue> extraAttrs;
657
Sunny Goyaleff44f32019-01-09 17:29:49 -0800658 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800659 TypedArray a = context.obtainStyledAttributes(
660 attrs, R.styleable.GridDisplayOption);
661 name = a.getString(R.styleable.GridDisplayOption_name);
662 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
663 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
664
Tracy Zhou7df93d22020-01-27 13:44:06 -0800665 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800666 defaultLayoutId = a.getResourceId(
667 R.styleable.GridDisplayOption_defaultLayoutId, 0);
668 demoModeLayoutId = a.getResourceId(
669 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700670 numDatabaseHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800671 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
672 numFolderRows = a.getInt(
673 R.styleable.GridDisplayOption_numFolderRows, numRows);
674 numFolderColumns = a.getInt(
675 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700676
Jon Mirandae126d722021-02-25 10:45:20 -0500677 isScalable = a.getBoolean(
678 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400679 devicePaddingId = a.getResourceId(
680 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500681
Jon Mirandaec1277e2021-03-25 10:41:54 -0400682 visible = a.getBoolean(R.styleable.GridDisplayOption_visible, true);
683
Sunny Goyal415f1732018-11-29 10:33:47 -0800684 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800685
686 extraAttrs = Themes.createValueMap(context, attrs,
687 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800688 }
689 }
690
691 private static final class DisplayOption {
692 private final GridOption grid;
693
Sunny Goyal415f1732018-11-29 10:33:47 -0800694 private final float minWidthDps;
695 private final float minHeightDps;
696 private final boolean canBeDefault;
697
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700698 private float numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000699 private float numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500700 private float minCellHeight;
701 private float minCellWidth;
702 private float borderSpacing;
703
Sunny Goyal415f1732018-11-29 10:33:47 -0800704 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800705 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700706 private float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200707 private float landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000708 private float allAppsIconSize;
709 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800710
711 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
712 this.grid = grid;
713
714 TypedArray a = context.obtainStyledAttributes(
715 attrs, R.styleable.ProfileDisplayOption);
716
Sunny Goyal415f1732018-11-29 10:33:47 -0800717 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
718 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
719 canBeDefault = a.getBoolean(
720 R.styleable.ProfileDisplayOption_canBeDefault, false);
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700721 numShownHotseatIcons = a.getInt(R.styleable.ProfileDisplayOption_numShownHotseatIcons,
722 grid.numDatabaseHotseatIcons);
Alex Chau7c439722021-03-24 11:32:44 +0000723 numAllAppsColumns = a.getInt(R.styleable.ProfileDisplayOption_numAllAppsColumns,
724 grid.numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800725
Jon Mirandae126d722021-02-25 10:45:20 -0500726 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
727 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
728 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
729
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700730 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800731 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
732 iconSize);
733 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Andras Kloczl6057cb92021-04-07 13:45:17 +0200734 landscapeIconTextSize = a.getFloat(
735 R.styleable.ProfileDisplayOption_landscapeIconTextSize, iconTextSize);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700736
Sunny Goyalae190ff2020-04-14 00:19:01 +0000737 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
738 iconSize);
739 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
740 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800741 a.recycle();
742 }
743
744 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700745 this(null);
746 }
747
748 DisplayOption(GridOption grid) {
749 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800750 minWidthDps = 0;
751 minHeightDps = 0;
752 canBeDefault = false;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700753 numShownHotseatIcons = 0;
Alex Chau7c439722021-03-24 11:32:44 +0000754 numAllAppsColumns = 0;
Jon Mirandae126d722021-02-25 10:45:20 -0500755 minCellHeight = 0;
756 minCellWidth = 0;
757 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800758 }
759
760 private DisplayOption multiply(float w) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700761 numShownHotseatIcons *= w;
Alex Chau7c439722021-03-24 11:32:44 +0000762 numAllAppsColumns *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800763 iconSize *= w;
764 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000765 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800766 iconTextSize *= w;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200767 landscapeIconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000768 allAppsIconTextSize *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500769 minCellHeight *= w;
770 minCellWidth *= w;
771 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800772 return this;
773 }
774
775 private DisplayOption add(DisplayOption p) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700776 numShownHotseatIcons += p.numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000777 numAllAppsColumns += p.numAllAppsColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800778 iconSize += p.iconSize;
779 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000780 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800781 iconTextSize += p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200782 landscapeIconTextSize += p.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000783 allAppsIconTextSize += p.allAppsIconTextSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500784 minCellHeight += p.minCellHeight;
785 minCellWidth += p.minCellWidth;
786 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800787 return this;
788 }
789 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700790
791 private class OverlayMonitor extends BroadcastReceiver {
792
793 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
794
795 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700796 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700797 }
798
799 @Override
800 public void onReceive(Context context, Intent intent) {
801 onConfigChanged(context);
802 }
803 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000804}