blob: 3c8556412508a02b9bae27efcd44ade99670501e [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 Goyal35c7b192021-04-20 16:51:10 -070023import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
24import static com.android.launcher3.util.DisplayController.CHANGE_SIZE;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080025import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070026import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070027
Sunny Goyalc6205602015-05-21 20:46:33 -070028import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070029import android.appwidget.AppWidgetHostView;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070030import android.content.BroadcastReceiver;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070031import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070032import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070033import android.content.Intent;
Sunny Goyal27835952017-01-13 12:15:53 -080034import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080035import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070036import android.content.res.TypedArray;
37import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070038import android.graphics.Point;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070039import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080040import android.text.TextUtils;
41import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070042import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070043import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080044import android.util.SparseArray;
45import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070046import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080047import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070048
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070049import androidx.annotation.Nullable;
50import androidx.annotation.VisibleForTesting;
51
Sunny Goyal905262c2019-05-03 16:50:43 -070052import com.android.launcher3.graphics.IconShape;
Winson Chung8e64bba2021-04-20 13:26:26 -070053import com.android.launcher3.testing.TestProtocol;
Sunny Goyalfd58da62020-08-11 12:06:49 -070054import com.android.launcher3.util.DisplayController;
55import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080056import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070057import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080058import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070059
Sunny Goyal819e1932016-07-07 16:43:58 -070060import org.xmlpull.v1.XmlPullParser;
61import org.xmlpull.v1.XmlPullParserException;
62
63import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070064import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080065import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070066
67public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070068
Hyunyoung Songc55a3502018-12-04 15:43:16 -080069 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070070 // We do not need any synchronization for this variable as its only written on UI thread.
71 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070072 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070073
Tracy Zhoued5f3082020-04-20 01:13:26 -070074 public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size";
75 public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count";
76
Hyunyoung Songc55a3502018-12-04 15:43:16 -080077 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080078
Sunny Goyal53d7ee42015-05-22 12:25:45 -070079 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
80
Sunny Goyal87dc48b2018-10-12 11:42:33 -070081 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080082 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
83
84 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070085
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070086 // Constants that affects the interpolation curve between statically defined device profile
87 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080088 private static final float KNEARESTNEIGHBOR = 3;
89 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070090
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070091 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080092 private static final float WEIGHT_EFFICIENT = 100000f;
93
94 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
95 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070096
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070097 /**
98 * Number of icons per row and column in the workspace.
99 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700100 public int numRows;
101 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700102
103 /**
104 * Number of icons per row and column in the folder.
105 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700106 public int numFolderRows;
107 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700108 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800109 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700110 public float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200111 public float landscapeIconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700112 public int iconBitmapSize;
113 public int fillResIconDpi;
114 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000115 public float allAppsIconSize;
116 public float allAppsIconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700117
Jon Mirandae126d722021-02-25 10:45:20 -0500118 public float minCellHeight;
119 public float minCellWidth;
120 public float borderSpacing;
121
Sunny Goyal5bc18462019-01-07 15:13:39 -0800122 private SparseArray<TypedValue> mExtraAttrs;
123
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700124 /**
125 * Number of icons inside the hotseat area.
126 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700127 protected int numShownHotseatIcons;
128
129 /**
130 * Number of icons inside the hotseat area that is stored in the database. This is greater than
131 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
132 * sizes that share the same DB.
133 */
134 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700135
Jon Miranda6f7e9702019-09-16 14:44:14 -0700136 /**
137 * Number of columns in the all apps list.
138 */
139 public int numAllAppsColumns;
140
Jon Mirandae126d722021-02-25 10:45:20 -0500141 /**
142 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
143 */
144 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400145 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500146
Tracy Zhou7df93d22020-01-27 13:44:06 -0800147 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800148 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700149 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700150
cuijiaxingabda8d72017-03-20 09:51:36 -0700151 public DeviceProfile landscapeProfile;
152 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700153
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400154 @Nullable public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500155
Sunny Goyal6f866092016-03-17 17:04:15 -0700156 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700157 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700158
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700159 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
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 Goyal35c7b192021-04-20 16:51:10 -0700206 DisplayController.INSTANCE.get(context).addChangeListener(
207 (info, flags) -> {
208 if ((flags & (CHANGE_SIZE | CHANGE_DENSITY)) != 0) {
209 onConfigChanged(context);
210 }
211 });
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700212 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700213 }
214
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700215 /**
216 * This constructor should NOT have any monitors by design.
217 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800218 public InvariantDeviceProfile(Context context, String gridName) {
219 String newName = initGrid(context, gridName);
220 if (newName == null || !newName.equals(gridName)) {
221 throw new IllegalArgumentException("Unknown grid name");
222 }
223 }
224
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700225 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800226 * This constructor should NOT have any monitors by design.
227 */
228 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700229 // Ensure that the main device profile is initialized
230 InvariantDeviceProfile originalProfile = INSTANCE.get(context);
231 String gridName = getCurrentGridName(context);
232
233 // Get the display info based on default display and interpolate it to existing display
234 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700235 DisplayController.INSTANCE.get(context).getInfo(),
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700236 getPredefinedDeviceProfiles(context, gridName));
237
Alex Chau417bd722021-01-26 15:22:18 +0000238 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700239 DisplayOption myDisplayOption = invDistWeightedInterpolate(
240 myInfo, getPredefinedDeviceProfiles(context, gridName));
241
242 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
243 .add(myDisplayOption);
244 result.iconSize = defaultDisplayOption.iconSize;
245 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700246 result.numShownHotseatIcons = myDisplayOption.numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000247 if (defaultDisplayOption.allAppsIconSize < myDisplayOption.allAppsIconSize) {
248 result.allAppsIconSize = defaultDisplayOption.allAppsIconSize;
249 result.numAllAppsColumns = defaultDisplayOption.numAllAppsColumns;
250 } else {
251 result.allAppsIconSize = myDisplayOption.allAppsIconSize;
252 result.numAllAppsColumns = myDisplayOption.numAllAppsColumns;
253 }
Jon Mirandae126d722021-02-25 10:45:20 -0500254 result.minCellHeight = defaultDisplayOption.minCellHeight;
255 result.minCellWidth = defaultDisplayOption.minCellWidth;
256 result.borderSpacing = defaultDisplayOption.borderSpacing;
Jon Miranda228877d2021-02-09 11:05:00 -0500257
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700258 initGrid(context, myInfo, result);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800259 }
260
Tracy Zhou42255d22020-03-13 00:38:11 -0700261 public static String getCurrentGridName(Context context) {
Andras Kloczl8e57cce2021-02-11 23:51:19 +0100262 if (ENABLE_TWO_PANEL_HOME.get()) {
263 return ENABLE_TWO_PANEL_HOME.key;
264 }
Jon Miranda73dcc982020-12-11 14:14:46 -0800265 if (ENABLE_FOUR_COLUMNS.get()) {
266 return ENABLE_FOUR_COLUMNS.key;
267 }
Tracy Zhouc6060e62020-04-27 13:05:34 -0700268 return Utilities.isGridOptionsEnabled(context)
269 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700270 }
271
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800272 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800273 * Retrieve system defined or RRO overriden icon shape.
274 */
275 private static String getIconShapePath(Context context) {
276 if (CONFIG_ICON_MASK_RES_ID == 0) {
277 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
278 return "";
279 }
280 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
281 }
282
Sunny Goyaleff44f32019-01-09 17:29:49 -0800283 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700284 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700285 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
286
287 DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions);
288 initGrid(context, displayInfo, displayOption);
289 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800290 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700291
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700292 private void initGrid(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700293 Context context, Info displayInfo, DisplayOption displayOption) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700294 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700295 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000296 numRows = closestProfile.numRows;
297 numColumns = closestProfile.numColumns;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700298 numDatabaseHotseatIcons = closestProfile.numDatabaseHotseatIcons;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000299 dbFile = closestProfile.dbFile;
300 defaultLayoutId = closestProfile.defaultLayoutId;
301 demoModeLayoutId = closestProfile.demoModeLayoutId;
302 numFolderRows = closestProfile.numFolderRows;
303 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500304 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400305 devicePaddingId = closestProfile.devicePaddingId;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000306
307 mExtraAttrs = closestProfile.extraAttrs;
308
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700309 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000310 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700311 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700312 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700313 iconTextSize = displayOption.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200314 landscapeIconTextSize = displayOption.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000315 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
316
Jon Mirandae126d722021-02-25 10:45:20 -0500317 minCellHeight = displayOption.minCellHeight;
318 minCellWidth = displayOption.minCellWidth;
319 borderSpacing = displayOption.borderSpacing;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700320 numShownHotseatIcons = Math.round(displayOption.numShownHotseatIcons);
Alex Chau7c439722021-03-24 11:32:44 +0000321 numAllAppsColumns = Math.round(displayOption.numAllAppsColumns);
Jon Mirandae126d722021-02-25 10:45:20 -0500322
Tracy Zhouc6060e62020-04-27 13:05:34 -0700323 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700324 allAppsIconSize = displayOption.allAppsIconSize;
325 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000326 } else {
327 allAppsIconSize = iconSize;
328 allAppsIconTextSize = iconTextSize;
329 }
330
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400331 if (devicePaddingId != 0) {
332 devicePaddings = new DevicePaddings(context, devicePaddingId);
333 }
334
Sunny Goyalae190ff2020-04-14 00:19:01 +0000335 // If the partner customization apk contains any grid overrides, apply them
336 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700337 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700338
Winson Chung13c1c2c2019-09-06 11:46:19 -0700339 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700340 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700341 // same in any orientation.
342 int smallSide = Math.min(realSize.x, realSize.y);
343 int largeSide = Math.max(realSize.x, realSize.y);
344
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700345 DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo)
346 .setSizeRange(new Point(displayInfo.smallestSize),
347 new Point(displayInfo.largestSize));
Winson Chung8e64bba2021-04-20 13:26:26 -0700348 if (TestProtocol.sDebugTracing) {
349 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED,
350 "largeSide=" + largeSide + " smallSide=" + smallSide);
351 }
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700352
353 landscapeProfile = builder.setSize(largeSide, smallSide).build();
354 portraitProfile = builder.setSize(smallSide, largeSide).build();
Sunny Goyal6f866092016-03-17 17:04:15 -0700355
356 // We need to ensure that there is enough extra space in the wallpaper
357 // for the intended parallax effects
358 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
359 defaultWallpaperSize = new Point(
360 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
361 largeSide);
362 } else {
363 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
364 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700365
366 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
367 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700368 }
369
Sunny Goyal5bc18462019-01-07 15:13:39 -0800370 @Nullable
371 public TypedValue getAttrValue(int attr) {
372 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
373 }
374
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700375 public void addOnChangeListener(OnIDPChangeListener listener) {
376 mChangeListeners.add(listener);
377 }
378
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800379 public void removeOnChangeListener(OnIDPChangeListener listener) {
380 mChangeListeners.remove(listener);
381 }
382
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800383 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800384 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
385 // Good place to check if grid size changed in themepicker when launcher was dead.
386 if (savedIconMaskPath.isEmpty()) {
387 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
388 .apply();
389 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
390 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
391 .apply();
392 apply(context, CHANGE_FLAG_ICON_PARAMS);
393 }
394 }
395
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800396 public void setCurrentGrid(Context context, String gridName) {
397 Context appContext = context.getApplicationContext();
398 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700399 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800400 }
401
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700402 private void onConfigChanged(Context context) {
Winson Chungdc0f3972021-04-20 18:18:51 -0700403 if (TestProtocol.sDebugTracing) {
404 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED, "IDP.onConfigChanged");
405 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700406 // Config changes, what shall we do?
407 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
408
409 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700410 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700411 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700412
413 int changeFlags = 0;
414 if (numRows != oldProfile.numRows ||
415 numColumns != oldProfile.numColumns ||
416 numFolderColumns != oldProfile.numFolderColumns ||
417 numFolderRows != oldProfile.numFolderRows ||
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700418 numDatabaseHotseatIcons != oldProfile.numDatabaseHotseatIcons) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700419 changeFlags |= CHANGE_FLAG_GRID;
420 }
421
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800422 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
423 !iconShapePath.equals(oldProfile.iconShapePath)) {
424 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700425 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800426 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700427 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800428 }
429
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800430 apply(context, changeFlags);
431 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700432
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800433 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700434 for (OnIDPChangeListener listener : mChangeListeners) {
435 listener.onIdpChanged(changeFlags, this);
436 }
437 }
438
Sunny Goyalae190ff2020-04-14 00:19:01 +0000439 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800440 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700441 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
442 final int depth = parser.getDepth();
443 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700444 while (((type = parser.next()) != XmlPullParser.END_TAG ||
445 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800446 if ((type == XmlPullParser.START_TAG)
447 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800448
449 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
450 final int displayDepth = parser.getDepth();
451 while (((type = parser.next()) != XmlPullParser.END_TAG ||
452 parser.getDepth() > displayDepth)
453 && type != XmlPullParser.END_DOCUMENT) {
454 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
455 parser.getName())) {
456 profiles.add(new DisplayOption(
457 gridOption, context, Xml.asAttributeSet(parser)));
458 }
459 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700460 }
461 }
462 } catch (IOException|XmlPullParserException e) {
463 throw new RuntimeException(e);
464 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800465
Sunny Goyalae190ff2020-04-14 00:19:01 +0000466 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800467 if (!TextUtils.isEmpty(gridName)) {
468 for (DisplayOption option : profiles) {
469 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000470 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800471 }
472 }
473 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000474 if (filteredProfiles.isEmpty()) {
475 // No grid found, use the default options
476 for (DisplayOption option : profiles) {
477 if (option.canBeDefault) {
478 filteredProfiles.add(option);
479 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800480 }
481 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000482 if (filteredProfiles.isEmpty()) {
483 throw new RuntimeException("No display option with canBeDefault=true");
484 }
485 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700486 }
487
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700488 private int getLauncherIconDensity(int requiredSize) {
489 // Densities typically defined by an app.
490 int[] densityBuckets = new int[] {
491 DisplayMetrics.DENSITY_LOW,
492 DisplayMetrics.DENSITY_MEDIUM,
493 DisplayMetrics.DENSITY_TV,
494 DisplayMetrics.DENSITY_HIGH,
495 DisplayMetrics.DENSITY_XHIGH,
496 DisplayMetrics.DENSITY_XXHIGH,
497 DisplayMetrics.DENSITY_XXXHIGH
498 };
499
500 int density = DisplayMetrics.DENSITY_XXXHIGH;
501 for (int i = densityBuckets.length - 1; i >= 0; i--) {
502 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
503 / DisplayMetrics.DENSITY_DEFAULT;
504 if (expectedSize >= requiredSize) {
505 density = densityBuckets[i];
506 }
507 }
508
509 return density;
510 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700511
Adam Cohen2e6da152015-05-06 11:42:25 -0700512 /**
513 * Apply any Partner customization grid overrides.
514 *
515 * Currently we support: all apps row / column count.
516 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700517 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
518 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700519 if (p != null) {
520 p.applyInvariantDeviceProfileOverrides(this, dm);
521 }
522 }
523
Sunny Goyal415f1732018-11-29 10:33:47 -0800524 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700525 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700526 }
527
Sunny Goyal415f1732018-11-29 10:33:47 -0800528 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700529 static DisplayOption invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700530 Info displayInfo, ArrayList<DisplayOption> points) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700531 Point smallestSize = new Point(displayInfo.smallestSize);
532 Point largestSize = new Point(displayInfo.largestSize);
533
534 // This guarantees that width < height
Sunny Goyal35c7b192021-04-20 16:51:10 -0700535 float width = Utilities.dpiFromPx((float) Math.min(smallestSize.x, smallestSize.y),
536 displayInfo.densityDpi);
537 float height = Utilities.dpiFromPx((float) Math.min(largestSize.x, largestSize.y),
538 displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700539
540 // Sort the profiles based on the closeness to the device size
541 Collections.sort(points, (a, b) ->
542 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
543 dist(width, height, b.minWidthDps, b.minHeightDps)));
544
545 GridOption closestOption = points.get(0).grid;
546 float weights = 0;
547
548 DisplayOption p = points.get(0);
549 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
550 return p;
551 }
552
553 DisplayOption out = new DisplayOption(closestOption);
554 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
555 p = points.get(i);
556 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
557 weights += w;
558 out.add(new DisplayOption().add(p).multiply(w));
559 }
560 return out.multiply(1.0f / weights);
561 }
562
563 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800564 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000565 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700566 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700567
Sunny Goyal415f1732018-11-29 10:33:47 -0800568 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700569 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
570 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700571 }
572
Sunny Goyal415f1732018-11-29 10:33:47 -0800573 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700574 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800575 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700576 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
577 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800578 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700579 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800580 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700581 }
582
Sunny Goyal27835952017-01-13 12:15:53 -0800583 public DeviceProfile getDeviceProfile(Context context) {
Winson Chung8e64bba2021-04-20 13:26:26 -0700584 if (TestProtocol.sDebugTracing) {
585 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED, "getDeviceProfile: orientation="
Winson Chung0bba6a72021-04-20 15:41:51 -0700586 + context.getResources().getConfiguration().orientation);
Winson Chung8e64bba2021-04-20 13:26:26 -0700587 }
Sunny Goyal27835952017-01-13 12:15:53 -0800588 return context.getResources().getConfiguration().orientation
589 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
590 }
591
Sunny Goyal415f1732018-11-29 10:33:47 -0800592 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700593 float d = dist(x0, y0, x1, y1);
594 if (Float.compare(d, 0f) == 0) {
595 return Float.POSITIVE_INFINITY;
596 }
597 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
598 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700599
600 /**
601 * As a ratio of screen height, the total distance we want the parallax effect to span
602 * horizontally
603 */
604 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
605 float aspectRatio = width / (float) height;
606
607 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
608 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
609 // We will use these two data points to extrapolate how much the wallpaper parallax effect
610 // to span (ie travel) at any aspect ratio:
611
612 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
613 final float ASPECT_RATIO_PORTRAIT = 10/16f;
614 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
615 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
616
617 // To find out the desired width at different aspect ratios, we use the following two
618 // formulas, where the coefficient on x is the aspect ratio (width/height):
619 // (16/10)x + y = 1.5
620 // (10/16)x + y = 1.2
621 // We solve for x and y and end up with a final formula:
622 final float x =
623 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
624 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
625 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
626 return x * aspectRatio + y;
627 }
628
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700629 public interface OnIDPChangeListener {
630
631 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
632 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800633
634
Sunny Goyaleff44f32019-01-09 17:29:49 -0800635 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800636
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800637 public static final String TAG_NAME = "grid-option";
638
Sunny Goyaleff44f32019-01-09 17:29:49 -0800639 public final String name;
640 public final int numRows;
641 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800642
643 private final int numFolderRows;
644 private final int numFolderColumns;
645
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700646 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800647
Tracy Zhou7df93d22020-01-27 13:44:06 -0800648 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000649
Sunny Goyal415f1732018-11-29 10:33:47 -0800650 private final int defaultLayoutId;
651 private final int demoModeLayoutId;
652
Jon Mirandae126d722021-02-25 10:45:20 -0500653 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400654 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500655
Jon Mirandaec1277e2021-03-25 10:41:54 -0400656 public final boolean visible;
657
Sunny Goyal5bc18462019-01-07 15:13:39 -0800658 private final SparseArray<TypedValue> extraAttrs;
659
Sunny Goyaleff44f32019-01-09 17:29:49 -0800660 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800661 TypedArray a = context.obtainStyledAttributes(
662 attrs, R.styleable.GridDisplayOption);
663 name = a.getString(R.styleable.GridDisplayOption_name);
664 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
665 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
666
Tracy Zhou7df93d22020-01-27 13:44:06 -0800667 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800668 defaultLayoutId = a.getResourceId(
669 R.styleable.GridDisplayOption_defaultLayoutId, 0);
670 demoModeLayoutId = a.getResourceId(
671 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700672 numDatabaseHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800673 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
674 numFolderRows = a.getInt(
675 R.styleable.GridDisplayOption_numFolderRows, numRows);
676 numFolderColumns = a.getInt(
677 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700678
Jon Mirandae126d722021-02-25 10:45:20 -0500679 isScalable = a.getBoolean(
680 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400681 devicePaddingId = a.getResourceId(
682 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500683
Jon Mirandaec1277e2021-03-25 10:41:54 -0400684 visible = a.getBoolean(R.styleable.GridDisplayOption_visible, true);
685
Sunny Goyal415f1732018-11-29 10:33:47 -0800686 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800687
688 extraAttrs = Themes.createValueMap(context, attrs,
689 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800690 }
691 }
692
693 private static final class DisplayOption {
694 private final GridOption grid;
695
Sunny Goyal415f1732018-11-29 10:33:47 -0800696 private final float minWidthDps;
697 private final float minHeightDps;
698 private final boolean canBeDefault;
699
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700700 private float numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000701 private float numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500702 private float minCellHeight;
703 private float minCellWidth;
704 private float borderSpacing;
705
Sunny Goyal415f1732018-11-29 10:33:47 -0800706 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800707 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700708 private float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200709 private float landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000710 private float allAppsIconSize;
711 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800712
713 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
714 this.grid = grid;
715
716 TypedArray a = context.obtainStyledAttributes(
717 attrs, R.styleable.ProfileDisplayOption);
718
Sunny Goyal415f1732018-11-29 10:33:47 -0800719 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
720 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
721 canBeDefault = a.getBoolean(
722 R.styleable.ProfileDisplayOption_canBeDefault, false);
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700723 numShownHotseatIcons = a.getInt(R.styleable.ProfileDisplayOption_numShownHotseatIcons,
724 grid.numDatabaseHotseatIcons);
Alex Chau7c439722021-03-24 11:32:44 +0000725 numAllAppsColumns = a.getInt(R.styleable.ProfileDisplayOption_numAllAppsColumns,
726 grid.numColumns);
Sunny Goyal415f1732018-11-29 10:33:47 -0800727
Jon Mirandae126d722021-02-25 10:45:20 -0500728 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
729 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
730 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
731
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700732 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800733 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
734 iconSize);
735 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Andras Kloczl6057cb92021-04-07 13:45:17 +0200736 landscapeIconTextSize = a.getFloat(
737 R.styleable.ProfileDisplayOption_landscapeIconTextSize, iconTextSize);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700738
Sunny Goyalae190ff2020-04-14 00:19:01 +0000739 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
740 iconSize);
741 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
742 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800743 a.recycle();
744 }
745
746 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700747 this(null);
748 }
749
750 DisplayOption(GridOption grid) {
751 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800752 minWidthDps = 0;
753 minHeightDps = 0;
754 canBeDefault = false;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700755 numShownHotseatIcons = 0;
Alex Chau7c439722021-03-24 11:32:44 +0000756 numAllAppsColumns = 0;
Jon Mirandae126d722021-02-25 10:45:20 -0500757 minCellHeight = 0;
758 minCellWidth = 0;
759 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800760 }
761
762 private DisplayOption multiply(float w) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700763 numShownHotseatIcons *= w;
Alex Chau7c439722021-03-24 11:32:44 +0000764 numAllAppsColumns *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800765 iconSize *= w;
766 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000767 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800768 iconTextSize *= w;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200769 landscapeIconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000770 allAppsIconTextSize *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500771 minCellHeight *= w;
772 minCellWidth *= w;
773 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800774 return this;
775 }
776
777 private DisplayOption add(DisplayOption p) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700778 numShownHotseatIcons += p.numShownHotseatIcons;
Alex Chau7c439722021-03-24 11:32:44 +0000779 numAllAppsColumns += p.numAllAppsColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800780 iconSize += p.iconSize;
781 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000782 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800783 iconTextSize += p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200784 landscapeIconTextSize += p.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000785 allAppsIconTextSize += p.allAppsIconTextSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500786 minCellHeight += p.minCellHeight;
787 minCellWidth += p.minCellWidth;
788 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800789 return this;
790 }
791 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700792
793 private class OverlayMonitor extends BroadcastReceiver {
794
795 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
796
797 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700798 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700799 }
800
801 @Override
802 public void onReceive(Context context, Intent intent) {
803 onConfigChanged(context);
804 }
805 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000806}