blob: b263d3830e1f1d5fbc79e518c3f5da6d1657f22a [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Sunny Goyal19ff7282021-04-22 10:12:54 -070019import static com.android.launcher3.Utilities.dpiFromPx;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080020import static com.android.launcher3.Utilities.getDevicePrefs;
Tracy Zhoued5f3082020-04-20 01:13:26 -070021import static com.android.launcher3.Utilities.getPointString;
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;
Sunny Goyal19ff7282021-04-22 10:12:54 -070024import static com.android.launcher3.util.DisplayController.CHANGE_SUPPORTED_BOUNDS;
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;
Sunny Goyal19ff7282021-04-22 10:12:54 -070059import com.android.launcher3.util.WindowBounds;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070060
Sunny Goyal819e1932016-07-07 16:43:58 -070061import org.xmlpull.v1.XmlPullParser;
62import org.xmlpull.v1.XmlPullParserException;
63
64import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070065import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080066import java.util.Collections;
Sunny Goyal19ff7282021-04-22 10:12:54 -070067import java.util.List;
Adam Cohen2e6da152015-05-06 11:42:25 -070068
69public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070070
Hyunyoung Songc55a3502018-12-04 15:43:16 -080071 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070072 // We do not need any synchronization for this variable as its only written on UI thread.
73 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070074 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070075
Tracy Zhoued5f3082020-04-20 01:13:26 -070076 public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size";
77 public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count";
78
Sunny Goyal19ff7282021-04-22 10:12:54 -070079 private static final int DEFAULT_TRUE = -1;
80 private static final int DEFAULT_SPLIT_DISPLAY = 2;
81
Hyunyoung Songc55a3502018-12-04 15:43:16 -080082 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080083
Sunny Goyal53d7ee42015-05-22 12:25:45 -070084 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
85
Sunny Goyal87dc48b2018-10-12 11:42:33 -070086 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080087 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
88
89 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070090
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070091 // Constants that affects the interpolation curve between statically defined device profile
92 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080093 private static final float KNEARESTNEIGHBOR = 3;
94 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070095
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070096 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080097 private static final float WEIGHT_EFFICIENT = 100000f;
98
99 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
100 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -0700101
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700102 /**
103 * Number of icons per row and column in the workspace.
104 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700105 public int numRows;
106 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700107
108 /**
109 * Number of icons per row and column in the folder.
110 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700111 public int numFolderRows;
112 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700113 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800114 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700115 public float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200116 public float landscapeIconTextSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700117 public int iconBitmapSize;
118 public int fillResIconDpi;
119 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000120 public float allAppsIconSize;
121 public float allAppsIconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700122
Jon Mirandae126d722021-02-25 10:45:20 -0500123 public float minCellHeight;
124 public float minCellWidth;
125 public float borderSpacing;
126
Sunny Goyal5bc18462019-01-07 15:13:39 -0800127 private SparseArray<TypedValue> mExtraAttrs;
128
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700129 /**
130 * Number of icons inside the hotseat area.
131 */
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700132 protected int numShownHotseatIcons;
133
134 /**
135 * Number of icons inside the hotseat area that is stored in the database. This is greater than
136 * or equal to numnShownHotseatIcons, allowing for a seamless transition between two hotseat
137 * sizes that share the same DB.
138 */
139 public int numDatabaseHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700140
Jon Miranda6f7e9702019-09-16 14:44:14 -0700141 /**
142 * Number of columns in the all apps list.
143 */
144 public int numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700145 public int numDatabaseAllAppsColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700146
Jon Mirandae126d722021-02-25 10:45:20 -0500147 /**
148 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
149 */
150 protected boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400151 public int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500152
Tracy Zhou7df93d22020-01-27 13:44:06 -0800153 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800154 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700155 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700156
Sunny Goyal19ff7282021-04-22 10:12:54 -0700157 public final List<DeviceProfile> supportedProfiles = new ArrayList<>();
Sunny Goyalc6205602015-05-21 20:46:33 -0700158
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400159 @Nullable public DevicePaddings devicePaddings;
Jon Miranda228877d2021-02-09 11:05:00 -0500160
Sunny Goyal6f866092016-03-17 17:04:15 -0700161 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700162 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700163
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700164 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700165 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700166
Sunny Goyalf633ef52018-03-13 09:57:05 -0700167 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700168 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700169
Sunny Goyalf633ef52018-03-13 09:57:05 -0700170 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800171 numRows = p.numRows;
172 numColumns = p.numColumns;
173 numFolderRows = p.numFolderRows;
174 numFolderColumns = p.numFolderColumns;
175 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800176 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800177 landscapeIconSize = p.landscapeIconSize;
Tony Wickham9459f312020-06-15 13:30:20 -0500178 iconBitmapSize = p.iconBitmapSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800179 iconTextSize = p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200180 landscapeIconTextSize = p.landscapeIconTextSize;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700181 numShownHotseatIcons = p.numShownHotseatIcons;
182 numDatabaseHotseatIcons = p.numDatabaseHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700183 numAllAppsColumns = p.numAllAppsColumns;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700184 numDatabaseAllAppsColumns = p.numDatabaseAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500185 isScalable = p.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400186 devicePaddingId = p.devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500187 minCellHeight = p.minCellHeight;
188 minCellWidth = p.minCellWidth;
189 borderSpacing = p.borderSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800190 dbFile = p.dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000191 allAppsIconSize = p.allAppsIconSize;
192 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800193 defaultLayoutId = p.defaultLayoutId;
194 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800195 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700196 mOverlayMonitor = p.mOverlayMonitor;
Jon Miranda228877d2021-02-09 11:05:00 -0500197 devicePaddings = p.devicePaddings;
Adam Cohen2e6da152015-05-06 11:42:25 -0700198 }
199
Sunny Goyalbbf01842015-10-08 07:41:15 -0700200 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700201 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700202 String gridName = getCurrentGridName(context);
203 String newGridName = initGrid(context, gridName);
204 if (!newGridName.equals(gridName)) {
205 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
206 }
Tracy Zhoued5f3082020-04-20 01:13:26 -0700207 Utilities.getPrefs(context).edit()
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700208 .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numDatabaseHotseatIcons)
Tracy Zhoued5f3082020-04-20 01:13:26 -0700209 .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows))
210 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700211
Sunny Goyal35c7b192021-04-20 16:51:10 -0700212 DisplayController.INSTANCE.get(context).addChangeListener(
Alex Chaufd6d9422021-04-22 19:10:21 +0100213 (displayContext, info, flags) -> {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700214 if ((flags & (CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS)) != 0) {
Alex Chaufd6d9422021-04-22 19:10:21 +0100215 onConfigChanged(displayContext);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700216 }
217 });
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700218 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700219 }
220
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700221 /**
222 * This constructor should NOT have any monitors by design.
223 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800224 public InvariantDeviceProfile(Context context, String gridName) {
225 String newName = initGrid(context, gridName);
226 if (newName == null || !newName.equals(gridName)) {
227 throw new IllegalArgumentException("Unknown grid name");
228 }
229 }
230
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700231 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800232 * This constructor should NOT have any monitors by design.
233 */
234 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700235 // Ensure that the main device profile is initialized
Alex Chaufd6d9422021-04-22 19:10:21 +0100236 INSTANCE.get(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700237 String gridName = getCurrentGridName(context);
238
239 // Get the display info based on default display and interpolate it to existing display
240 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyal35c7b192021-04-20 16:51:10 -0700241 DisplayController.INSTANCE.get(context).getInfo(),
Sunny Goyal19ff7282021-04-22 10:12:54 -0700242 getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700243
Alex Chau417bd722021-01-26 15:22:18 +0000244 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700245 DisplayOption myDisplayOption = invDistWeightedInterpolate(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700246 myInfo, getPredefinedDeviceProfiles(context, gridName, false), false);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700247
248 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
249 .add(myDisplayOption);
250 result.iconSize = defaultDisplayOption.iconSize;
251 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
Alex Chau7c439722021-03-24 11:32:44 +0000252 if (defaultDisplayOption.allAppsIconSize < myDisplayOption.allAppsIconSize) {
253 result.allAppsIconSize = defaultDisplayOption.allAppsIconSize;
Alex Chau7c439722021-03-24 11:32:44 +0000254 } else {
255 result.allAppsIconSize = myDisplayOption.allAppsIconSize;
Alex Chau7c439722021-03-24 11:32:44 +0000256 }
Jon Mirandae126d722021-02-25 10:45:20 -0500257 result.minCellHeight = defaultDisplayOption.minCellHeight;
258 result.minCellWidth = defaultDisplayOption.minCellWidth;
259 result.borderSpacing = defaultDisplayOption.borderSpacing;
Jon Miranda228877d2021-02-09 11:05:00 -0500260
Sunny Goyal19ff7282021-04-22 10:12:54 -0700261 initGrid(context, myInfo, result, false);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800262 }
263
Tracy Zhou42255d22020-03-13 00:38:11 -0700264 public static String getCurrentGridName(Context context) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700265 return Utilities.isGridOptionsEnabled(context)
266 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700267 }
268
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800269 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800270 * Retrieve system defined or RRO overriden icon shape.
271 */
272 private static String getIconShapePath(Context context) {
273 if (CONFIG_ICON_MASK_RES_ID == 0) {
274 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
275 return "";
276 }
277 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
278 }
279
Sunny Goyaleff44f32019-01-09 17:29:49 -0800280 private String initGrid(Context context, String gridName) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700281 Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700282 // Determine if we have split display
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700283
Sunny Goyal19ff7282021-04-22 10:12:54 -0700284 boolean isTablet = false, isPhone = false;
285 for (WindowBounds bounds : displayInfo.supportedBounds) {
286 if (displayInfo.isTablet(bounds)) {
287 isTablet = true;
288 } else {
289 isPhone = true;
290 }
291 }
292 boolean isSplitDisplay = isPhone && isTablet && ENABLE_TWO_PANEL_HOME.get();
293
294 ArrayList<DisplayOption> allOptions =
295 getPredefinedDeviceProfiles(context, gridName, isSplitDisplay);
296 DisplayOption displayOption =
297 invDistWeightedInterpolate(displayInfo, allOptions, isSplitDisplay);
298 initGrid(context, displayInfo, displayOption, isSplitDisplay);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700299 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800300 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700301
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700302 private void initGrid(
Sunny Goyal19ff7282021-04-22 10:12:54 -0700303 Context context, Info displayInfo, DisplayOption displayOption,
304 boolean isSplitDisplay) {
Sunny Goyal35c7b192021-04-20 16:51:10 -0700305 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700306 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000307 numRows = closestProfile.numRows;
308 numColumns = closestProfile.numColumns;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000309 dbFile = closestProfile.dbFile;
310 defaultLayoutId = closestProfile.defaultLayoutId;
311 demoModeLayoutId = closestProfile.demoModeLayoutId;
312 numFolderRows = closestProfile.numFolderRows;
313 numFolderColumns = closestProfile.numFolderColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500314 isScalable = closestProfile.isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400315 devicePaddingId = closestProfile.devicePaddingId;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000316
317 mExtraAttrs = closestProfile.extraAttrs;
318
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700319 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000320 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700321 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyal35c7b192021-04-20 16:51:10 -0700322 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700323 iconTextSize = displayOption.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200324 landscapeIconTextSize = displayOption.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000325 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
326
Jon Mirandae126d722021-02-25 10:45:20 -0500327 minCellHeight = displayOption.minCellHeight;
328 minCellWidth = displayOption.minCellWidth;
329 borderSpacing = displayOption.borderSpacing;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700330
331 numShownHotseatIcons = closestProfile.numHotseatIcons;
332 numDatabaseHotseatIcons = isSplitDisplay
333 ? closestProfile.numDatabaseHotseatIcons : closestProfile.numHotseatIcons;
334
335 numAllAppsColumns = closestProfile.numAllAppsColumns;
336 numDatabaseAllAppsColumns = isSplitDisplay
337 ? closestProfile.numDatabaseAllAppsColumns : closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500338
Tracy Zhouc6060e62020-04-27 13:05:34 -0700339 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700340 allAppsIconSize = displayOption.allAppsIconSize;
341 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000342 } else {
343 allAppsIconSize = iconSize;
344 allAppsIconTextSize = iconTextSize;
345 }
346
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400347 if (devicePaddingId != 0) {
348 devicePaddings = new DevicePaddings(context, devicePaddingId);
349 }
350
Sunny Goyalae190ff2020-04-14 00:19:01 +0000351 // If the partner customization apk contains any grid overrides, apply them
352 // Supported overrides: numRows, numColumns, iconSize
Sunny Goyal35c7b192021-04-20 16:51:10 -0700353 applyPartnerDeviceProfileOverrides(context, metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700354
Sunny Goyal19ff7282021-04-22 10:12:54 -0700355 supportedProfiles.clear();
356 defaultWallpaperSize = new Point(displayInfo.currentSize);
357 for (WindowBounds bounds : displayInfo.supportedBounds) {
358 supportedProfiles.add(new DeviceProfile.Builder(context, this, displayInfo)
359 .setUseTwoPanels(isSplitDisplay)
360 .setWindowBounds(bounds).build());
Sunny Goyalc6205602015-05-21 20:46:33 -0700361
Sunny Goyal19ff7282021-04-22 10:12:54 -0700362 // Wallpaper size should be the maximum of the all possible sizes Launcher expects
363 int displayWidth = bounds.bounds.width();
364 int displayHeight = bounds.bounds.height();
365 defaultWallpaperSize.y = Math.max(defaultWallpaperSize.y, displayHeight);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700366
Sunny Goyal19ff7282021-04-22 10:12:54 -0700367 // We need to ensure that there is enough extra space in the wallpaper
368 // for the intended parallax effects
369 float parallaxFactor =
370 dpiFromPx(Math.min(displayWidth, displayHeight), displayInfo.densityDpi) < 720
371 ? 2
372 : wallpaperTravelToScreenWidthRatio(displayWidth, displayHeight);
373 defaultWallpaperSize.x =
374 Math.max(defaultWallpaperSize.x, Math.round(parallaxFactor * displayWidth));
Sunny Goyal6f866092016-03-17 17:04:15 -0700375 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700376
377 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
378 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700379 }
380
Sunny Goyal5bc18462019-01-07 15:13:39 -0800381 @Nullable
382 public TypedValue getAttrValue(int attr) {
383 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
384 }
385
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700386 public void addOnChangeListener(OnIDPChangeListener listener) {
387 mChangeListeners.add(listener);
388 }
389
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800390 public void removeOnChangeListener(OnIDPChangeListener listener) {
391 mChangeListeners.remove(listener);
392 }
393
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800394 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800395 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
396 // Good place to check if grid size changed in themepicker when launcher was dead.
397 if (savedIconMaskPath.isEmpty()) {
398 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
399 .apply();
400 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
401 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
402 .apply();
Sunny Goyal19ff7282021-04-22 10:12:54 -0700403 apply(CHANGE_FLAG_ICON_PARAMS);
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800404 }
405 }
406
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800407 public void setCurrentGrid(Context context, String gridName) {
408 Context appContext = context.getApplicationContext();
409 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700410 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800411 }
412
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700413 private void onConfigChanged(Context context) {
Winson Chungdc0f3972021-04-20 18:18:51 -0700414 if (TestProtocol.sDebugTracing) {
415 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED, "IDP.onConfigChanged");
416 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700417 // Config changes, what shall we do?
418 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
419
420 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700421 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700422 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700423
424 int changeFlags = 0;
425 if (numRows != oldProfile.numRows ||
426 numColumns != oldProfile.numColumns ||
427 numFolderColumns != oldProfile.numFolderColumns ||
428 numFolderRows != oldProfile.numFolderRows ||
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700429 numDatabaseHotseatIcons != oldProfile.numDatabaseHotseatIcons) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700430 changeFlags |= CHANGE_FLAG_GRID;
431 }
432
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800433 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
434 !iconShapePath.equals(oldProfile.iconShapePath)) {
435 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700436 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800437 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700438 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800439 }
440
Sunny Goyal19ff7282021-04-22 10:12:54 -0700441 apply(changeFlags);
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800442 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700443
Sunny Goyal19ff7282021-04-22 10:12:54 -0700444 private void apply(int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700445 for (OnIDPChangeListener listener : mChangeListeners) {
446 listener.onIdpChanged(changeFlags, this);
447 }
448 }
449
Sunny Goyal19ff7282021-04-22 10:12:54 -0700450 private static ArrayList<DisplayOption> getPredefinedDeviceProfiles(
451 Context context, String gridName, boolean isSplitDisplay) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800452 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700453 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
454 final int depth = parser.getDepth();
455 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700456 while (((type = parser.next()) != XmlPullParser.END_TAG ||
457 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800458 if ((type == XmlPullParser.START_TAG)
459 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800460
461 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
462 final int displayDepth = parser.getDepth();
463 while (((type = parser.next()) != XmlPullParser.END_TAG ||
464 parser.getDepth() > displayDepth)
465 && type != XmlPullParser.END_DOCUMENT) {
466 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
467 parser.getName())) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700468 profiles.add(new DisplayOption(gridOption, context,
469 Xml.asAttributeSet(parser),
470 isSplitDisplay ? DEFAULT_SPLIT_DISPLAY : DEFAULT_TRUE));
Sunny Goyal415f1732018-11-29 10:33:47 -0800471 }
472 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700473 }
474 }
475 } catch (IOException|XmlPullParserException e) {
476 throw new RuntimeException(e);
477 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800478
Sunny Goyalae190ff2020-04-14 00:19:01 +0000479 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800480 if (!TextUtils.isEmpty(gridName)) {
481 for (DisplayOption option : profiles) {
482 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000483 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800484 }
485 }
486 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000487 if (filteredProfiles.isEmpty()) {
488 // No grid found, use the default options
489 for (DisplayOption option : profiles) {
490 if (option.canBeDefault) {
491 filteredProfiles.add(option);
492 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800493 }
494 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000495 if (filteredProfiles.isEmpty()) {
496 throw new RuntimeException("No display option with canBeDefault=true");
497 }
498 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700499 }
500
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700501 private int getLauncherIconDensity(int requiredSize) {
502 // Densities typically defined by an app.
503 int[] densityBuckets = new int[] {
504 DisplayMetrics.DENSITY_LOW,
505 DisplayMetrics.DENSITY_MEDIUM,
506 DisplayMetrics.DENSITY_TV,
507 DisplayMetrics.DENSITY_HIGH,
508 DisplayMetrics.DENSITY_XHIGH,
509 DisplayMetrics.DENSITY_XXHIGH,
510 DisplayMetrics.DENSITY_XXXHIGH
511 };
512
513 int density = DisplayMetrics.DENSITY_XXXHIGH;
514 for (int i = densityBuckets.length - 1; i >= 0; i--) {
515 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
516 / DisplayMetrics.DENSITY_DEFAULT;
517 if (expectedSize >= requiredSize) {
518 density = densityBuckets[i];
519 }
520 }
521
522 return density;
523 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700524
Adam Cohen2e6da152015-05-06 11:42:25 -0700525 /**
526 * Apply any Partner customization grid overrides.
527 *
528 * Currently we support: all apps row / column count.
529 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700530 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
531 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700532 if (p != null) {
533 p.applyInvariantDeviceProfileOverrides(this, dm);
534 }
535 }
536
Sunny Goyal415f1732018-11-29 10:33:47 -0800537 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700538 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700539 }
540
Sunny Goyal19ff7282021-04-22 10:12:54 -0700541 private static DisplayOption invDistWeightedInterpolate(
542 Info displayInfo, ArrayList<DisplayOption> points, boolean isSplitDisplay) {
543 int minWidthPx = Integer.MAX_VALUE;
544 int minHeightPx = Integer.MAX_VALUE;
545 for (WindowBounds bounds : displayInfo.supportedBounds) {
546 boolean isTablet = displayInfo.isTablet(bounds);
547 if (isTablet && isSplitDisplay) {
548 // For split displays, take half width per page
549 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x / 2);
550 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700551
Sunny Goyal19ff7282021-04-22 10:12:54 -0700552 } else if (!isTablet && bounds.isLandscape()) {
553 // We will use transposed layout in this case
554 minWidthPx = Math.min(minWidthPx, bounds.availableSize.y);
555 minHeightPx = Math.min(minHeightPx, bounds.availableSize.x);
556 } else {
557 minWidthPx = Math.min(minWidthPx, bounds.availableSize.x);
558 minHeightPx = Math.min(minHeightPx, bounds.availableSize.y);
559 }
560 }
561
562 float width = dpiFromPx(minWidthPx, displayInfo.densityDpi);
563 float height = dpiFromPx(minHeightPx, displayInfo.densityDpi);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700564
565 // Sort the profiles based on the closeness to the device size
566 Collections.sort(points, (a, b) ->
567 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
568 dist(width, height, b.minWidthDps, b.minHeightDps)));
569
570 GridOption closestOption = points.get(0).grid;
571 float weights = 0;
572
573 DisplayOption p = points.get(0);
574 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
575 return p;
576 }
577
578 DisplayOption out = new DisplayOption(closestOption);
579 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
580 p = points.get(i);
581 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
582 weights += w;
583 out.add(new DisplayOption().add(p).multiply(w));
584 }
585 return out.multiply(1.0f / weights);
586 }
587
Sunny Goyal27835952017-01-13 12:15:53 -0800588 public DeviceProfile getDeviceProfile(Context context) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700589 Resources res = context.getResources();
590 Configuration config = context.getResources().getConfiguration();
591
592 float availableWidth = config.screenWidthDp * res.getDisplayMetrics().density;
593 float availableHeight = config.screenHeightDp * res.getDisplayMetrics().density;
594
Winson Chung8e64bba2021-04-20 13:26:26 -0700595 if (TestProtocol.sDebugTracing) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700596 Log.d(TestProtocol.LAUNCHER_NOT_TRANSPOSED,
597 "getDeviceProfile: orientation=" + config.orientation
598 + " size=" + availableWidth + "x" + availableHeight);
Winson Chung8e64bba2021-04-20 13:26:26 -0700599 }
Sunny Goyal19ff7282021-04-22 10:12:54 -0700600 DeviceProfile bestMatch = supportedProfiles.get(0);
601 float minDiff = Float.MAX_VALUE;
602
603 for (DeviceProfile profile : supportedProfiles) {
604 float diff = Math.abs(profile.availableWidthPx - availableWidth)
605 + Math.abs(profile.availableHeightPx - availableHeight);
606 if (diff < minDiff) {
607 minDiff = diff;
608 bestMatch = profile;
609 }
610 }
611 return bestMatch;
Sunny Goyal27835952017-01-13 12:15:53 -0800612 }
613
Sunny Goyal415f1732018-11-29 10:33:47 -0800614 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700615 float d = dist(x0, y0, x1, y1);
616 if (Float.compare(d, 0f) == 0) {
617 return Float.POSITIVE_INFINITY;
618 }
619 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
620 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700621
622 /**
623 * As a ratio of screen height, the total distance we want the parallax effect to span
624 * horizontally
625 */
626 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
627 float aspectRatio = width / (float) height;
628
629 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
630 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
631 // We will use these two data points to extrapolate how much the wallpaper parallax effect
632 // to span (ie travel) at any aspect ratio:
633
634 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
635 final float ASPECT_RATIO_PORTRAIT = 10/16f;
636 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
637 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
638
639 // To find out the desired width at different aspect ratios, we use the following two
640 // formulas, where the coefficient on x is the aspect ratio (width/height):
641 // (16/10)x + y = 1.5
642 // (10/16)x + y = 1.2
643 // We solve for x and y and end up with a final formula:
644 final float x =
645 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
646 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
647 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
648 return x * aspectRatio + y;
649 }
650
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700651 public interface OnIDPChangeListener {
652
653 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
654 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800655
656
Sunny Goyaleff44f32019-01-09 17:29:49 -0800657 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800658
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800659 public static final String TAG_NAME = "grid-option";
660
Sunny Goyaleff44f32019-01-09 17:29:49 -0800661 public final String name;
662 public final int numRows;
663 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800664
665 private final int numFolderRows;
666 private final int numFolderColumns;
667
Sunny Goyal19ff7282021-04-22 10:12:54 -0700668 private final int numAllAppsColumns;
669 private final int numDatabaseAllAppsColumns;
670 private final int numHotseatIcons;
Tony Wickhamb87f3cd2021-04-07 15:02:37 -0700671 private final int numDatabaseHotseatIcons;
Sunny Goyal415f1732018-11-29 10:33:47 -0800672
Tracy Zhou7df93d22020-01-27 13:44:06 -0800673 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000674
Sunny Goyal415f1732018-11-29 10:33:47 -0800675 private final int defaultLayoutId;
676 private final int demoModeLayoutId;
677
Jon Mirandae126d722021-02-25 10:45:20 -0500678 private final boolean isScalable;
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400679 private final int devicePaddingId;
Jon Mirandae126d722021-02-25 10:45:20 -0500680
Sunny Goyal5bc18462019-01-07 15:13:39 -0800681 private final SparseArray<TypedValue> extraAttrs;
682
Sunny Goyaleff44f32019-01-09 17:29:49 -0800683 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800684 TypedArray a = context.obtainStyledAttributes(
685 attrs, R.styleable.GridDisplayOption);
686 name = a.getString(R.styleable.GridDisplayOption_name);
687 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
688 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
689
Tracy Zhou7df93d22020-01-27 13:44:06 -0800690 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800691 defaultLayoutId = a.getResourceId(
692 R.styleable.GridDisplayOption_defaultLayoutId, 0);
693 demoModeLayoutId = a.getResourceId(
694 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700695
696 numAllAppsColumns = a.getInt(
697 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
698 numDatabaseAllAppsColumns = a.getInt(
699 R.styleable.GridDisplayOption_numExtendedAllAppsColumns, 2 * numAllAppsColumns);
700
701 numHotseatIcons = a.getInt(
Sunny Goyal415f1732018-11-29 10:33:47 -0800702 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700703 numDatabaseHotseatIcons = a.getInt(
704 R.styleable.GridDisplayOption_numExtendedHotseatIcons, 2 * numHotseatIcons);
705
Sunny Goyal415f1732018-11-29 10:33:47 -0800706 numFolderRows = a.getInt(
707 R.styleable.GridDisplayOption_numFolderRows, numRows);
708 numFolderColumns = a.getInt(
709 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700710
Jon Mirandae126d722021-02-25 10:45:20 -0500711 isScalable = a.getBoolean(
712 R.styleable.GridDisplayOption_isScalable, false);
Jon Mirandac9e69fa2021-03-22 17:13:34 -0400713 devicePaddingId = a.getResourceId(
714 R.styleable.GridDisplayOption_devicePaddingId, 0);
Jon Mirandae126d722021-02-25 10:45:20 -0500715
Sunny Goyal415f1732018-11-29 10:33:47 -0800716 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800717 extraAttrs = Themes.createValueMap(context, attrs,
718 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800719 }
720 }
721
Sunny Goyal19ff7282021-04-22 10:12:54 -0700722 @VisibleForTesting
723 static final class DisplayOption {
724
725 public final GridOption grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800726
Sunny Goyal415f1732018-11-29 10:33:47 -0800727 private final float minWidthDps;
728 private final float minHeightDps;
729 private final boolean canBeDefault;
730
Jon Mirandae126d722021-02-25 10:45:20 -0500731 private float minCellHeight;
732 private float minCellWidth;
733 private float borderSpacing;
734
Sunny Goyal415f1732018-11-29 10:33:47 -0800735 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800736 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700737 private float landscapeIconSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200738 private float landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000739 private float allAppsIconSize;
740 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800741
Sunny Goyal19ff7282021-04-22 10:12:54 -0700742 DisplayOption(GridOption grid, Context context, AttributeSet attrs, int defaultFlagValue) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800743 this.grid = grid;
744
745 TypedArray a = context.obtainStyledAttributes(
746 attrs, R.styleable.ProfileDisplayOption);
747
Sunny Goyal415f1732018-11-29 10:33:47 -0800748 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
749 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
Sunny Goyal19ff7282021-04-22 10:12:54 -0700750
751 canBeDefault = a.getInt(R.styleable.ProfileDisplayOption_canBeDefault, 0)
752 == defaultFlagValue;
Sunny Goyal415f1732018-11-29 10:33:47 -0800753
Jon Mirandae126d722021-02-25 10:45:20 -0500754 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
755 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
756 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
757
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700758 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800759 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
760 iconSize);
761 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Andras Kloczl6057cb92021-04-07 13:45:17 +0200762 landscapeIconTextSize = a.getFloat(
763 R.styleable.ProfileDisplayOption_landscapeIconTextSize, iconTextSize);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700764
Sunny Goyalae190ff2020-04-14 00:19:01 +0000765 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
766 iconSize);
767 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
768 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800769 a.recycle();
770 }
771
772 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700773 this(null);
774 }
775
776 DisplayOption(GridOption grid) {
777 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800778 minWidthDps = 0;
779 minHeightDps = 0;
780 canBeDefault = false;
Jon Mirandae126d722021-02-25 10:45:20 -0500781 minCellHeight = 0;
782 minCellWidth = 0;
783 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800784 }
785
786 private DisplayOption multiply(float w) {
787 iconSize *= w;
788 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000789 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800790 iconTextSize *= w;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200791 landscapeIconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000792 allAppsIconTextSize *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500793 minCellHeight *= w;
794 minCellWidth *= w;
795 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800796 return this;
797 }
798
799 private DisplayOption add(DisplayOption p) {
800 iconSize += p.iconSize;
801 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000802 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800803 iconTextSize += p.iconTextSize;
Andras Kloczl6057cb92021-04-07 13:45:17 +0200804 landscapeIconTextSize += p.landscapeIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000805 allAppsIconTextSize += p.allAppsIconTextSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500806 minCellHeight += p.minCellHeight;
807 minCellWidth += p.minCellWidth;
808 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800809 return this;
810 }
811 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700812
813 private class OverlayMonitor extends BroadcastReceiver {
814
815 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
816
817 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700818 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700819 }
820
821 @Override
822 public void onReceive(Context context, Intent intent) {
823 onConfigChanged(context);
824 }
825 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000826}