blob: bb605571228ed120bdac0ed39bd308385dc1efd2 [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;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080022import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070023import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070024
Sunny Goyalc6205602015-05-21 20:46:33 -070025import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070026import android.appwidget.AppWidgetHostView;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070027import android.content.BroadcastReceiver;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070028import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070029import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070030import android.content.Intent;
Sunny Goyal27835952017-01-13 12:15:53 -080031import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080032import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070033import android.content.res.TypedArray;
34import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070035import android.graphics.Point;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070036import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080037import android.text.TextUtils;
38import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070039import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070040import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080041import android.util.SparseArray;
42import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070043import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080044import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070045
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070046import androidx.annotation.Nullable;
47import androidx.annotation.VisibleForTesting;
48
Sunny Goyal905262c2019-05-03 16:50:43 -070049import com.android.launcher3.graphics.IconShape;
Sunny Goyald0e360a2018-06-29 14:40:18 -070050import com.android.launcher3.util.ConfigMonitor;
Sunny Goyalfd58da62020-08-11 12:06:49 -070051import com.android.launcher3.util.DisplayController;
52import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080053import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070054import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080055import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070056
Sunny Goyal819e1932016-07-07 16:43:58 -070057import org.xmlpull.v1.XmlPullParser;
58import org.xmlpull.v1.XmlPullParserException;
59
60import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070061import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080062import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070063
64public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070065
Hyunyoung Songc55a3502018-12-04 15:43:16 -080066 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070067 // We do not need any synchronization for this variable as its only written on UI thread.
68 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070069 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070070
Tracy Zhoued5f3082020-04-20 01:13:26 -070071 public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size";
72 public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count";
73
Hyunyoung Songc55a3502018-12-04 15:43:16 -080074 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080075
Sunny Goyal53d7ee42015-05-22 12:25:45 -070076 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
77
Sunny Goyal87dc48b2018-10-12 11:42:33 -070078 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080079 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
80
81 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070082
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070083 // Constants that affects the interpolation curve between statically defined device profile
84 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080085 private static final float KNEARESTNEIGHBOR = 3;
86 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070087
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070088 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080089 private static final float WEIGHT_EFFICIENT = 100000f;
90
91 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
92 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070093
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070094 /**
95 * Number of icons per row and column in the workspace.
96 */
Adam Cohen2e6da152015-05-06 11:42:25 -070097 public int numRows;
98 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099
100 /**
101 * Number of icons per row and column in the folder.
102 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700103 public int numFolderRows;
104 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700105 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800106 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700107 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700108 public int iconBitmapSize;
109 public int fillResIconDpi;
110 public float iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000111 public float allAppsIconSize;
112 public float allAppsIconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700113
Jon Mirandae126d722021-02-25 10:45:20 -0500114 public float minCellHeight;
115 public float minCellWidth;
116 public float borderSpacing;
117
Sunny Goyal5bc18462019-01-07 15:13:39 -0800118 private SparseArray<TypedValue> mExtraAttrs;
119
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700120 /**
121 * Number of icons inside the hotseat area.
122 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800123 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700124
Jon Miranda6f7e9702019-09-16 14:44:14 -0700125 /**
126 * Number of columns in the all apps list.
127 */
128 public int numAllAppsColumns;
129
Jon Mirandae126d722021-02-25 10:45:20 -0500130 /**
131 * Do not query directly. see {@link DeviceProfile#isScalableGrid}.
132 */
133 protected boolean isScalable;
134
Tracy Zhou7df93d22020-01-27 13:44:06 -0800135 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800136 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700137 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700138
cuijiaxingabda8d72017-03-20 09:51:36 -0700139 public DeviceProfile landscapeProfile;
140 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700141
Jon Miranda228877d2021-02-09 11:05:00 -0500142 public DevicePaddings devicePaddings;
143
Sunny Goyal6f866092016-03-17 17:04:15 -0700144 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700145 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700146
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700147 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
148 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700149 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700150
Sunny Goyalf633ef52018-03-13 09:57:05 -0700151 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700152 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700153
Sunny Goyalf633ef52018-03-13 09:57:05 -0700154 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800155 numRows = p.numRows;
156 numColumns = p.numColumns;
157 numFolderRows = p.numFolderRows;
158 numFolderColumns = p.numFolderColumns;
159 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800160 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800161 landscapeIconSize = p.landscapeIconSize;
Tony Wickham9459f312020-06-15 13:30:20 -0500162 iconBitmapSize = p.iconBitmapSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800163 iconTextSize = p.iconTextSize;
164 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700165 numAllAppsColumns = p.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500166 isScalable = p.isScalable;
167 minCellHeight = p.minCellHeight;
168 minCellWidth = p.minCellWidth;
169 borderSpacing = p.borderSpacing;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800170 dbFile = p.dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000171 allAppsIconSize = p.allAppsIconSize;
172 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800173 defaultLayoutId = p.defaultLayoutId;
174 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800175 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700176 mOverlayMonitor = p.mOverlayMonitor;
Jon Miranda228877d2021-02-09 11:05:00 -0500177 devicePaddings = p.devicePaddings;
Adam Cohen2e6da152015-05-06 11:42:25 -0700178 }
179
Sunny Goyalbbf01842015-10-08 07:41:15 -0700180 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700181 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700182 String gridName = getCurrentGridName(context);
183 String newGridName = initGrid(context, gridName);
184 if (!newGridName.equals(gridName)) {
185 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
186 }
Tracy Zhoued5f3082020-04-20 01:13:26 -0700187 Utilities.getPrefs(context).edit()
188 .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numHotseatIcons)
189 .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows))
190 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700191
Sunny Goyalead22a52021-02-02 23:21:55 -0800192 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700193 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700194 }
195
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700196 /**
197 * This constructor should NOT have any monitors by design.
198 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800199 public InvariantDeviceProfile(Context context, String gridName) {
200 String newName = initGrid(context, gridName);
201 if (newName == null || !newName.equals(gridName)) {
202 throw new IllegalArgumentException("Unknown grid name");
203 }
204 }
205
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700206 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800207 * This constructor should NOT have any monitors by design.
208 */
209 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700210 // Ensure that the main device profile is initialized
211 InvariantDeviceProfile originalProfile = INSTANCE.get(context);
212 String gridName = getCurrentGridName(context);
213
214 // Get the display info based on default display and interpolate it to existing display
215 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700216 DisplayController.getDefaultDisplay(context).getInfo(),
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700217 getPredefinedDeviceProfiles(context, gridName));
218
Alex Chau417bd722021-01-26 15:22:18 +0000219 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700220 DisplayOption myDisplayOption = invDistWeightedInterpolate(
221 myInfo, getPredefinedDeviceProfiles(context, gridName));
222
223 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
224 .add(myDisplayOption);
225 result.iconSize = defaultDisplayOption.iconSize;
226 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
227 result.allAppsIconSize = Math.min(
228 defaultDisplayOption.allAppsIconSize, myDisplayOption.allAppsIconSize);
Jon Mirandae126d722021-02-25 10:45:20 -0500229 result.minCellHeight = defaultDisplayOption.minCellHeight;
230 result.minCellWidth = defaultDisplayOption.minCellWidth;
231 result.borderSpacing = defaultDisplayOption.borderSpacing;
Jon Miranda228877d2021-02-09 11:05:00 -0500232
233 devicePaddings = new DevicePaddings(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700234 initGrid(context, myInfo, result);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800235 }
236
Tracy Zhou42255d22020-03-13 00:38:11 -0700237 public static String getCurrentGridName(Context context) {
Jon Miranda73dcc982020-12-11 14:14:46 -0800238 if (ENABLE_FOUR_COLUMNS.get()) {
239 return ENABLE_FOUR_COLUMNS.key;
240 }
Tracy Zhouc6060e62020-04-27 13:05:34 -0700241 return Utilities.isGridOptionsEnabled(context)
242 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700243 }
244
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800245 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800246 * Retrieve system defined or RRO overriden icon shape.
247 */
248 private static String getIconShapePath(Context context) {
249 if (CONFIG_ICON_MASK_RES_ID == 0) {
250 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
251 return "";
252 }
253 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
254 }
255
Sunny Goyaleff44f32019-01-09 17:29:49 -0800256 private String initGrid(Context context, String gridName) {
Sunny Goyalfd58da62020-08-11 12:06:49 -0700257 Info displayInfo = DisplayController.getDefaultDisplay(context).getInfo();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700258 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
259
260 DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions);
Jon Miranda228877d2021-02-09 11:05:00 -0500261 devicePaddings = new DevicePaddings(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700262 initGrid(context, displayInfo, displayOption);
263 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800264 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700265
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700266 private void initGrid(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700267 Context context, Info displayInfo, DisplayOption displayOption) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700268 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000269 numRows = closestProfile.numRows;
270 numColumns = closestProfile.numColumns;
271 numHotseatIcons = closestProfile.numHotseatIcons;
272 dbFile = closestProfile.dbFile;
273 defaultLayoutId = closestProfile.defaultLayoutId;
274 demoModeLayoutId = closestProfile.demoModeLayoutId;
275 numFolderRows = closestProfile.numFolderRows;
276 numFolderColumns = closestProfile.numFolderColumns;
277 numAllAppsColumns = closestProfile.numAllAppsColumns;
Jon Mirandae126d722021-02-25 10:45:20 -0500278 isScalable = closestProfile.isScalable;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000279
280 mExtraAttrs = closestProfile.extraAttrs;
281
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700282 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000283 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700284 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000285 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700286 iconTextSize = displayOption.iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000287 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
288
Jon Mirandae126d722021-02-25 10:45:20 -0500289 minCellHeight = displayOption.minCellHeight;
290 minCellWidth = displayOption.minCellWidth;
291 borderSpacing = displayOption.borderSpacing;
292
Tracy Zhouc6060e62020-04-27 13:05:34 -0700293 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700294 allAppsIconSize = displayOption.allAppsIconSize;
295 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000296 } else {
297 allAppsIconSize = iconSize;
298 allAppsIconTextSize = iconTextSize;
299 }
300
301 // If the partner customization apk contains any grid overrides, apply them
302 // Supported overrides: numRows, numColumns, iconSize
303 applyPartnerDeviceProfileOverrides(context, displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700304
Winson Chung13c1c2c2019-09-06 11:46:19 -0700305 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700306 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700307 // same in any orientation.
308 int smallSide = Math.min(realSize.x, realSize.y);
309 int largeSide = Math.max(realSize.x, realSize.y);
310
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700311 DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo)
312 .setSizeRange(new Point(displayInfo.smallestSize),
313 new Point(displayInfo.largestSize));
314
315 landscapeProfile = builder.setSize(largeSide, smallSide).build();
316 portraitProfile = builder.setSize(smallSide, largeSide).build();
Sunny Goyal6f866092016-03-17 17:04:15 -0700317
318 // We need to ensure that there is enough extra space in the wallpaper
319 // for the intended parallax effects
320 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
321 defaultWallpaperSize = new Point(
322 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
323 largeSide);
324 } else {
325 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
326 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700327
328 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
329 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700330 }
331
Sunny Goyal5bc18462019-01-07 15:13:39 -0800332 @Nullable
333 public TypedValue getAttrValue(int attr) {
334 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
335 }
336
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700337 public void addOnChangeListener(OnIDPChangeListener listener) {
338 mChangeListeners.add(listener);
339 }
340
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800341 public void removeOnChangeListener(OnIDPChangeListener listener) {
342 mChangeListeners.remove(listener);
343 }
344
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800345 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800346 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
347 // Good place to check if grid size changed in themepicker when launcher was dead.
348 if (savedIconMaskPath.isEmpty()) {
349 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
350 .apply();
351 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
352 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
353 .apply();
354 apply(context, CHANGE_FLAG_ICON_PARAMS);
355 }
356 }
357
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800358 public void setCurrentGrid(Context context, String gridName) {
359 Context appContext = context.getApplicationContext();
360 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700361 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800362 }
363
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700364 private void onConfigChanged(Context context) {
365 // Config changes, what shall we do?
366 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
367
368 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700369 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700370 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700371
372 int changeFlags = 0;
373 if (numRows != oldProfile.numRows ||
374 numColumns != oldProfile.numColumns ||
375 numFolderColumns != oldProfile.numFolderColumns ||
376 numFolderRows != oldProfile.numFolderRows ||
377 numHotseatIcons != oldProfile.numHotseatIcons) {
378 changeFlags |= CHANGE_FLAG_GRID;
379 }
380
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800381 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
382 !iconShapePath.equals(oldProfile.iconShapePath)) {
383 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700384 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800385 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700386 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800387 }
388
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800389 apply(context, changeFlags);
390 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700391
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800392 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700393 // Create a new config monitor
394 mConfigMonitor.unregister();
395 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
396
397 for (OnIDPChangeListener listener : mChangeListeners) {
398 listener.onIdpChanged(changeFlags, this);
399 }
400 }
401
Sunny Goyalae190ff2020-04-14 00:19:01 +0000402 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800403 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700404 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
405 final int depth = parser.getDepth();
406 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700407 while (((type = parser.next()) != XmlPullParser.END_TAG ||
408 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800409 if ((type == XmlPullParser.START_TAG)
410 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800411
412 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
413 final int displayDepth = parser.getDepth();
414 while (((type = parser.next()) != XmlPullParser.END_TAG ||
415 parser.getDepth() > displayDepth)
416 && type != XmlPullParser.END_DOCUMENT) {
417 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
418 parser.getName())) {
419 profiles.add(new DisplayOption(
420 gridOption, context, Xml.asAttributeSet(parser)));
421 }
422 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700423 }
424 }
425 } catch (IOException|XmlPullParserException e) {
426 throw new RuntimeException(e);
427 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800428
Sunny Goyalae190ff2020-04-14 00:19:01 +0000429 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800430 if (!TextUtils.isEmpty(gridName)) {
431 for (DisplayOption option : profiles) {
432 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000433 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800434 }
435 }
436 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000437 if (filteredProfiles.isEmpty()) {
438 // No grid found, use the default options
439 for (DisplayOption option : profiles) {
440 if (option.canBeDefault) {
441 filteredProfiles.add(option);
442 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800443 }
444 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000445 if (filteredProfiles.isEmpty()) {
446 throw new RuntimeException("No display option with canBeDefault=true");
447 }
448 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700449 }
450
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700451 private int getLauncherIconDensity(int requiredSize) {
452 // Densities typically defined by an app.
453 int[] densityBuckets = new int[] {
454 DisplayMetrics.DENSITY_LOW,
455 DisplayMetrics.DENSITY_MEDIUM,
456 DisplayMetrics.DENSITY_TV,
457 DisplayMetrics.DENSITY_HIGH,
458 DisplayMetrics.DENSITY_XHIGH,
459 DisplayMetrics.DENSITY_XXHIGH,
460 DisplayMetrics.DENSITY_XXXHIGH
461 };
462
463 int density = DisplayMetrics.DENSITY_XXXHIGH;
464 for (int i = densityBuckets.length - 1; i >= 0; i--) {
465 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
466 / DisplayMetrics.DENSITY_DEFAULT;
467 if (expectedSize >= requiredSize) {
468 density = densityBuckets[i];
469 }
470 }
471
472 return density;
473 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700474
Adam Cohen2e6da152015-05-06 11:42:25 -0700475 /**
476 * Apply any Partner customization grid overrides.
477 *
478 * Currently we support: all apps row / column count.
479 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700480 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
481 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700482 if (p != null) {
483 p.applyInvariantDeviceProfileOverrides(this, dm);
484 }
485 }
486
Sunny Goyal415f1732018-11-29 10:33:47 -0800487 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700488 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700489 }
490
Sunny Goyal415f1732018-11-29 10:33:47 -0800491 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700492 static DisplayOption invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700493 Info displayInfo, ArrayList<DisplayOption> points) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700494 Point smallestSize = new Point(displayInfo.smallestSize);
495 Point largestSize = new Point(displayInfo.largestSize);
496
497 // This guarantees that width < height
498 float width = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
499 displayInfo.metrics);
500 float height = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
501 displayInfo.metrics);
502
503 // Sort the profiles based on the closeness to the device size
504 Collections.sort(points, (a, b) ->
505 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
506 dist(width, height, b.minWidthDps, b.minHeightDps)));
507
508 GridOption closestOption = points.get(0).grid;
509 float weights = 0;
510
511 DisplayOption p = points.get(0);
512 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
513 return p;
514 }
515
516 DisplayOption out = new DisplayOption(closestOption);
517 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
518 p = points.get(i);
519 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
520 weights += w;
521 out.add(new DisplayOption().add(p).multiply(w));
522 }
523 return out.multiply(1.0f / weights);
524 }
525
526 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800527 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000528 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700529 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700530
Sunny Goyal415f1732018-11-29 10:33:47 -0800531 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700532 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
533 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700534 }
535
Sunny Goyal415f1732018-11-29 10:33:47 -0800536 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700537 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800538 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700539 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
540 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800541 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700542 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800543 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700544 }
545
Sunny Goyal27835952017-01-13 12:15:53 -0800546 public DeviceProfile getDeviceProfile(Context context) {
547 return context.getResources().getConfiguration().orientation
548 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
549 }
550
Sunny Goyal415f1732018-11-29 10:33:47 -0800551 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700552 float d = dist(x0, y0, x1, y1);
553 if (Float.compare(d, 0f) == 0) {
554 return Float.POSITIVE_INFINITY;
555 }
556 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
557 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700558
559 /**
560 * As a ratio of screen height, the total distance we want the parallax effect to span
561 * horizontally
562 */
563 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
564 float aspectRatio = width / (float) height;
565
566 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
567 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
568 // We will use these two data points to extrapolate how much the wallpaper parallax effect
569 // to span (ie travel) at any aspect ratio:
570
571 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
572 final float ASPECT_RATIO_PORTRAIT = 10/16f;
573 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
574 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
575
576 // To find out the desired width at different aspect ratios, we use the following two
577 // formulas, where the coefficient on x is the aspect ratio (width/height):
578 // (16/10)x + y = 1.5
579 // (10/16)x + y = 1.2
580 // We solve for x and y and end up with a final formula:
581 final float x =
582 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
583 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
584 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
585 return x * aspectRatio + y;
586 }
587
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700588 public interface OnIDPChangeListener {
589
590 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
591 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800592
593
Sunny Goyaleff44f32019-01-09 17:29:49 -0800594 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800595
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800596 public static final String TAG_NAME = "grid-option";
597
Sunny Goyaleff44f32019-01-09 17:29:49 -0800598 public final String name;
599 public final int numRows;
600 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800601
602 private final int numFolderRows;
603 private final int numFolderColumns;
604
605 private final int numHotseatIcons;
606
Tracy Zhou7df93d22020-01-27 13:44:06 -0800607 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000608 private final int numAllAppsColumns;
609
Sunny Goyal415f1732018-11-29 10:33:47 -0800610 private final int defaultLayoutId;
611 private final int demoModeLayoutId;
612
Jon Mirandae126d722021-02-25 10:45:20 -0500613 private final boolean isScalable;
614
Sunny Goyal5bc18462019-01-07 15:13:39 -0800615 private final SparseArray<TypedValue> extraAttrs;
616
Sunny Goyaleff44f32019-01-09 17:29:49 -0800617 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800618 TypedArray a = context.obtainStyledAttributes(
619 attrs, R.styleable.GridDisplayOption);
620 name = a.getString(R.styleable.GridDisplayOption_name);
621 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
622 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
623
Tracy Zhou7df93d22020-01-27 13:44:06 -0800624 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800625 defaultLayoutId = a.getResourceId(
626 R.styleable.GridDisplayOption_defaultLayoutId, 0);
627 demoModeLayoutId = a.getResourceId(
628 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
629 numHotseatIcons = a.getInt(
630 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
631 numFolderRows = a.getInt(
632 R.styleable.GridDisplayOption_numFolderRows, numRows);
633 numFolderColumns = a.getInt(
634 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000635 numAllAppsColumns = a.getInt(
636 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700637
Jon Mirandae126d722021-02-25 10:45:20 -0500638 isScalable = a.getBoolean(
639 R.styleable.GridDisplayOption_isScalable, false);
640
Sunny Goyal415f1732018-11-29 10:33:47 -0800641 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800642
643 extraAttrs = Themes.createValueMap(context, attrs,
644 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800645 }
646 }
647
648 private static final class DisplayOption {
649 private final GridOption grid;
650
Sunny Goyal415f1732018-11-29 10:33:47 -0800651 private final float minWidthDps;
652 private final float minHeightDps;
653 private final boolean canBeDefault;
654
Jon Mirandae126d722021-02-25 10:45:20 -0500655 private float minCellHeight;
656 private float minCellWidth;
657 private float borderSpacing;
658
Sunny Goyal415f1732018-11-29 10:33:47 -0800659 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800660 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700661 private float landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000662 private float allAppsIconSize;
663 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800664
665 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
666 this.grid = grid;
667
668 TypedArray a = context.obtainStyledAttributes(
669 attrs, R.styleable.ProfileDisplayOption);
670
Sunny Goyal415f1732018-11-29 10:33:47 -0800671 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
672 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
673 canBeDefault = a.getBoolean(
674 R.styleable.ProfileDisplayOption_canBeDefault, false);
675
Jon Mirandae126d722021-02-25 10:45:20 -0500676 minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0);
677 minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0);
678 borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0);
679
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700680 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800681 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
682 iconSize);
683 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700684
Sunny Goyalae190ff2020-04-14 00:19:01 +0000685 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
686 iconSize);
687 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
688 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800689 a.recycle();
690 }
691
692 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700693 this(null);
694 }
695
696 DisplayOption(GridOption grid) {
697 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800698 minWidthDps = 0;
699 minHeightDps = 0;
700 canBeDefault = false;
Jon Mirandae126d722021-02-25 10:45:20 -0500701 minCellHeight = 0;
702 minCellWidth = 0;
703 borderSpacing = 0;
Sunny Goyal415f1732018-11-29 10:33:47 -0800704 }
705
706 private DisplayOption multiply(float w) {
707 iconSize *= w;
708 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000709 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800710 iconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000711 allAppsIconTextSize *= w;
Jon Mirandae126d722021-02-25 10:45:20 -0500712 minCellHeight *= w;
713 minCellWidth *= w;
714 borderSpacing *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800715 return this;
716 }
717
718 private DisplayOption add(DisplayOption p) {
719 iconSize += p.iconSize;
720 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000721 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800722 iconTextSize += p.iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000723 allAppsIconTextSize += p.allAppsIconTextSize;
Jon Mirandae126d722021-02-25 10:45:20 -0500724 minCellHeight += p.minCellHeight;
725 minCellWidth += p.minCellWidth;
726 borderSpacing += p.borderSpacing;
Sunny Goyal415f1732018-11-29 10:33:47 -0800727 return this;
728 }
729 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700730
731 private class OverlayMonitor extends BroadcastReceiver {
732
733 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
734
735 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700736 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700737 }
738
739 @Override
740 public void onReceive(Context context, Intent intent) {
741 onConfigChanged(context);
742 }
743 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000744}