blob: 2a08c500089701ee9c88a7146d42ad91edf16a41 [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
Sunny Goyal5bc18462019-01-07 15:13:39 -0800114 private SparseArray<TypedValue> mExtraAttrs;
115
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700116 /**
117 * Number of icons inside the hotseat area.
118 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800119 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700120
Jon Miranda6f7e9702019-09-16 14:44:14 -0700121 /**
122 * Number of columns in the all apps list.
123 */
124 public int numAllAppsColumns;
125
Tracy Zhou7df93d22020-01-27 13:44:06 -0800126 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800127 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700128 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700129
cuijiaxingabda8d72017-03-20 09:51:36 -0700130 public DeviceProfile landscapeProfile;
131 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700132
Jon Miranda228877d2021-02-09 11:05:00 -0500133 public DevicePaddings devicePaddings;
134
Sunny Goyal6f866092016-03-17 17:04:15 -0700135 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700136 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700137
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700138 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
139 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700140 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700141
Sunny Goyalf633ef52018-03-13 09:57:05 -0700142 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700143 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700144
Sunny Goyalf633ef52018-03-13 09:57:05 -0700145 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800146 numRows = p.numRows;
147 numColumns = p.numColumns;
148 numFolderRows = p.numFolderRows;
149 numFolderColumns = p.numFolderColumns;
150 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800151 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800152 landscapeIconSize = p.landscapeIconSize;
Tony Wickham9459f312020-06-15 13:30:20 -0500153 iconBitmapSize = p.iconBitmapSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800154 iconTextSize = p.iconTextSize;
155 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700156 numAllAppsColumns = p.numAllAppsColumns;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800157 dbFile = p.dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000158 allAppsIconSize = p.allAppsIconSize;
159 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800160 defaultLayoutId = p.defaultLayoutId;
161 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800162 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700163 mOverlayMonitor = p.mOverlayMonitor;
Jon Miranda228877d2021-02-09 11:05:00 -0500164 devicePaddings = p.devicePaddings;
Adam Cohen2e6da152015-05-06 11:42:25 -0700165 }
166
Sunny Goyalbbf01842015-10-08 07:41:15 -0700167 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700168 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700169 String gridName = getCurrentGridName(context);
170 String newGridName = initGrid(context, gridName);
171 if (!newGridName.equals(gridName)) {
172 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
173 }
Tracy Zhoued5f3082020-04-20 01:13:26 -0700174 Utilities.getPrefs(context).edit()
175 .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numHotseatIcons)
176 .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows))
177 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700178
Sunny Goyalead22a52021-02-02 23:21:55 -0800179 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700180 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700181 }
182
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700183 /**
184 * This constructor should NOT have any monitors by design.
185 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800186 public InvariantDeviceProfile(Context context, String gridName) {
187 String newName = initGrid(context, gridName);
188 if (newName == null || !newName.equals(gridName)) {
189 throw new IllegalArgumentException("Unknown grid name");
190 }
191 }
192
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700193 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800194 * This constructor should NOT have any monitors by design.
195 */
196 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700197 // Ensure that the main device profile is initialized
198 InvariantDeviceProfile originalProfile = INSTANCE.get(context);
199 String gridName = getCurrentGridName(context);
200
201 // Get the display info based on default display and interpolate it to existing display
202 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700203 DisplayController.getDefaultDisplay(context).getInfo(),
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700204 getPredefinedDeviceProfiles(context, gridName));
205
Alex Chau417bd722021-01-26 15:22:18 +0000206 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700207 DisplayOption myDisplayOption = invDistWeightedInterpolate(
208 myInfo, getPredefinedDeviceProfiles(context, gridName));
209
210 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
211 .add(myDisplayOption);
212 result.iconSize = defaultDisplayOption.iconSize;
213 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
214 result.allAppsIconSize = Math.min(
215 defaultDisplayOption.allAppsIconSize, myDisplayOption.allAppsIconSize);
Jon Miranda228877d2021-02-09 11:05:00 -0500216
217 devicePaddings = new DevicePaddings(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700218 initGrid(context, myInfo, result);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800219 }
220
Tracy Zhou42255d22020-03-13 00:38:11 -0700221 public static String getCurrentGridName(Context context) {
Jon Miranda73dcc982020-12-11 14:14:46 -0800222 if (ENABLE_FOUR_COLUMNS.get()) {
223 return ENABLE_FOUR_COLUMNS.key;
224 }
Tracy Zhouc6060e62020-04-27 13:05:34 -0700225 return Utilities.isGridOptionsEnabled(context)
226 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700227 }
228
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800229 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800230 * Retrieve system defined or RRO overriden icon shape.
231 */
232 private static String getIconShapePath(Context context) {
233 if (CONFIG_ICON_MASK_RES_ID == 0) {
234 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
235 return "";
236 }
237 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
238 }
239
Sunny Goyaleff44f32019-01-09 17:29:49 -0800240 private String initGrid(Context context, String gridName) {
Sunny Goyalfd58da62020-08-11 12:06:49 -0700241 Info displayInfo = DisplayController.getDefaultDisplay(context).getInfo();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700242 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
243
244 DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions);
Jon Miranda228877d2021-02-09 11:05:00 -0500245 devicePaddings = new DevicePaddings(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700246 initGrid(context, displayInfo, displayOption);
247 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800248 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700249
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700250 private void initGrid(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700251 Context context, Info displayInfo, DisplayOption displayOption) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700252 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000253 numRows = closestProfile.numRows;
254 numColumns = closestProfile.numColumns;
255 numHotseatIcons = closestProfile.numHotseatIcons;
256 dbFile = closestProfile.dbFile;
257 defaultLayoutId = closestProfile.defaultLayoutId;
258 demoModeLayoutId = closestProfile.demoModeLayoutId;
259 numFolderRows = closestProfile.numFolderRows;
260 numFolderColumns = closestProfile.numFolderColumns;
261 numAllAppsColumns = closestProfile.numAllAppsColumns;
262
263 mExtraAttrs = closestProfile.extraAttrs;
264
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700265 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000266 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700267 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000268 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700269 iconTextSize = displayOption.iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000270 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
271
Tracy Zhouc6060e62020-04-27 13:05:34 -0700272 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700273 allAppsIconSize = displayOption.allAppsIconSize;
274 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000275 } else {
276 allAppsIconSize = iconSize;
277 allAppsIconTextSize = iconTextSize;
278 }
279
280 // If the partner customization apk contains any grid overrides, apply them
281 // Supported overrides: numRows, numColumns, iconSize
282 applyPartnerDeviceProfileOverrides(context, displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700283
Winson Chung13c1c2c2019-09-06 11:46:19 -0700284 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700285 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700286 // same in any orientation.
287 int smallSide = Math.min(realSize.x, realSize.y);
288 int largeSide = Math.max(realSize.x, realSize.y);
289
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700290 DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo)
291 .setSizeRange(new Point(displayInfo.smallestSize),
292 new Point(displayInfo.largestSize));
293
294 landscapeProfile = builder.setSize(largeSide, smallSide).build();
295 portraitProfile = builder.setSize(smallSide, largeSide).build();
Sunny Goyal6f866092016-03-17 17:04:15 -0700296
297 // We need to ensure that there is enough extra space in the wallpaper
298 // for the intended parallax effects
299 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
300 defaultWallpaperSize = new Point(
301 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
302 largeSide);
303 } else {
304 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
305 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700306
307 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
308 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700309 }
310
Sunny Goyal5bc18462019-01-07 15:13:39 -0800311 @Nullable
312 public TypedValue getAttrValue(int attr) {
313 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
314 }
315
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700316 public void addOnChangeListener(OnIDPChangeListener listener) {
317 mChangeListeners.add(listener);
318 }
319
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800320 public void removeOnChangeListener(OnIDPChangeListener listener) {
321 mChangeListeners.remove(listener);
322 }
323
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800324 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800325 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
326 // Good place to check if grid size changed in themepicker when launcher was dead.
327 if (savedIconMaskPath.isEmpty()) {
328 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
329 .apply();
330 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
331 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
332 .apply();
333 apply(context, CHANGE_FLAG_ICON_PARAMS);
334 }
335 }
336
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800337 public void setCurrentGrid(Context context, String gridName) {
338 Context appContext = context.getApplicationContext();
339 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700340 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800341 }
342
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700343 private void onConfigChanged(Context context) {
344 // Config changes, what shall we do?
345 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
346
347 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700348 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700349 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700350
351 int changeFlags = 0;
352 if (numRows != oldProfile.numRows ||
353 numColumns != oldProfile.numColumns ||
354 numFolderColumns != oldProfile.numFolderColumns ||
355 numFolderRows != oldProfile.numFolderRows ||
356 numHotseatIcons != oldProfile.numHotseatIcons) {
357 changeFlags |= CHANGE_FLAG_GRID;
358 }
359
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800360 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
361 !iconShapePath.equals(oldProfile.iconShapePath)) {
362 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700363 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800364 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700365 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800366 }
367
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800368 apply(context, changeFlags);
369 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700370
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800371 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700372 // Create a new config monitor
373 mConfigMonitor.unregister();
374 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
375
376 for (OnIDPChangeListener listener : mChangeListeners) {
377 listener.onIdpChanged(changeFlags, this);
378 }
379 }
380
Sunny Goyalae190ff2020-04-14 00:19:01 +0000381 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800382 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700383 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
384 final int depth = parser.getDepth();
385 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700386 while (((type = parser.next()) != XmlPullParser.END_TAG ||
387 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800388 if ((type == XmlPullParser.START_TAG)
389 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800390
391 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
392 final int displayDepth = parser.getDepth();
393 while (((type = parser.next()) != XmlPullParser.END_TAG ||
394 parser.getDepth() > displayDepth)
395 && type != XmlPullParser.END_DOCUMENT) {
396 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
397 parser.getName())) {
398 profiles.add(new DisplayOption(
399 gridOption, context, Xml.asAttributeSet(parser)));
400 }
401 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700402 }
403 }
404 } catch (IOException|XmlPullParserException e) {
405 throw new RuntimeException(e);
406 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800407
Sunny Goyalae190ff2020-04-14 00:19:01 +0000408 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800409 if (!TextUtils.isEmpty(gridName)) {
410 for (DisplayOption option : profiles) {
411 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000412 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800413 }
414 }
415 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000416 if (filteredProfiles.isEmpty()) {
417 // No grid found, use the default options
418 for (DisplayOption option : profiles) {
419 if (option.canBeDefault) {
420 filteredProfiles.add(option);
421 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800422 }
423 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000424 if (filteredProfiles.isEmpty()) {
425 throw new RuntimeException("No display option with canBeDefault=true");
426 }
427 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700428 }
429
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700430 private int getLauncherIconDensity(int requiredSize) {
431 // Densities typically defined by an app.
432 int[] densityBuckets = new int[] {
433 DisplayMetrics.DENSITY_LOW,
434 DisplayMetrics.DENSITY_MEDIUM,
435 DisplayMetrics.DENSITY_TV,
436 DisplayMetrics.DENSITY_HIGH,
437 DisplayMetrics.DENSITY_XHIGH,
438 DisplayMetrics.DENSITY_XXHIGH,
439 DisplayMetrics.DENSITY_XXXHIGH
440 };
441
442 int density = DisplayMetrics.DENSITY_XXXHIGH;
443 for (int i = densityBuckets.length - 1; i >= 0; i--) {
444 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
445 / DisplayMetrics.DENSITY_DEFAULT;
446 if (expectedSize >= requiredSize) {
447 density = densityBuckets[i];
448 }
449 }
450
451 return density;
452 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700453
Adam Cohen2e6da152015-05-06 11:42:25 -0700454 /**
455 * Apply any Partner customization grid overrides.
456 *
457 * Currently we support: all apps row / column count.
458 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700459 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
460 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700461 if (p != null) {
462 p.applyInvariantDeviceProfileOverrides(this, dm);
463 }
464 }
465
Sunny Goyal415f1732018-11-29 10:33:47 -0800466 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700467 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700468 }
469
Sunny Goyal415f1732018-11-29 10:33:47 -0800470 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700471 static DisplayOption invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700472 Info displayInfo, ArrayList<DisplayOption> points) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700473 Point smallestSize = new Point(displayInfo.smallestSize);
474 Point largestSize = new Point(displayInfo.largestSize);
475
476 // This guarantees that width < height
477 float width = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
478 displayInfo.metrics);
479 float height = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
480 displayInfo.metrics);
481
482 // Sort the profiles based on the closeness to the device size
483 Collections.sort(points, (a, b) ->
484 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
485 dist(width, height, b.minWidthDps, b.minHeightDps)));
486
487 GridOption closestOption = points.get(0).grid;
488 float weights = 0;
489
490 DisplayOption p = points.get(0);
491 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
492 return p;
493 }
494
495 DisplayOption out = new DisplayOption(closestOption);
496 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
497 p = points.get(i);
498 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
499 weights += w;
500 out.add(new DisplayOption().add(p).multiply(w));
501 }
502 return out.multiply(1.0f / weights);
503 }
504
505 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800506 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000507 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700508 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700509
Sunny Goyal415f1732018-11-29 10:33:47 -0800510 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700511 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
512 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700513 }
514
Sunny Goyal415f1732018-11-29 10:33:47 -0800515 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700516 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800517 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700518 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
519 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800520 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700521 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800522 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700523 }
524
Sunny Goyal27835952017-01-13 12:15:53 -0800525 public DeviceProfile getDeviceProfile(Context context) {
526 return context.getResources().getConfiguration().orientation
527 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
528 }
529
Sunny Goyal415f1732018-11-29 10:33:47 -0800530 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700531 float d = dist(x0, y0, x1, y1);
532 if (Float.compare(d, 0f) == 0) {
533 return Float.POSITIVE_INFINITY;
534 }
535 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
536 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700537
538 /**
539 * As a ratio of screen height, the total distance we want the parallax effect to span
540 * horizontally
541 */
542 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
543 float aspectRatio = width / (float) height;
544
545 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
546 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
547 // We will use these two data points to extrapolate how much the wallpaper parallax effect
548 // to span (ie travel) at any aspect ratio:
549
550 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
551 final float ASPECT_RATIO_PORTRAIT = 10/16f;
552 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
553 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
554
555 // To find out the desired width at different aspect ratios, we use the following two
556 // formulas, where the coefficient on x is the aspect ratio (width/height):
557 // (16/10)x + y = 1.5
558 // (10/16)x + y = 1.2
559 // We solve for x and y and end up with a final formula:
560 final float x =
561 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
562 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
563 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
564 return x * aspectRatio + y;
565 }
566
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700567 public interface OnIDPChangeListener {
568
569 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
570 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800571
572
Sunny Goyaleff44f32019-01-09 17:29:49 -0800573 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800574
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800575 public static final String TAG_NAME = "grid-option";
576
Sunny Goyaleff44f32019-01-09 17:29:49 -0800577 public final String name;
578 public final int numRows;
579 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800580
581 private final int numFolderRows;
582 private final int numFolderColumns;
583
584 private final int numHotseatIcons;
585
Tracy Zhou7df93d22020-01-27 13:44:06 -0800586 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000587 private final int numAllAppsColumns;
588
Sunny Goyal415f1732018-11-29 10:33:47 -0800589 private final int defaultLayoutId;
590 private final int demoModeLayoutId;
591
Sunny Goyal5bc18462019-01-07 15:13:39 -0800592 private final SparseArray<TypedValue> extraAttrs;
593
Sunny Goyaleff44f32019-01-09 17:29:49 -0800594 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800595 TypedArray a = context.obtainStyledAttributes(
596 attrs, R.styleable.GridDisplayOption);
597 name = a.getString(R.styleable.GridDisplayOption_name);
598 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
599 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
600
Tracy Zhou7df93d22020-01-27 13:44:06 -0800601 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800602 defaultLayoutId = a.getResourceId(
603 R.styleable.GridDisplayOption_defaultLayoutId, 0);
604 demoModeLayoutId = a.getResourceId(
605 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
606 numHotseatIcons = a.getInt(
607 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
608 numFolderRows = a.getInt(
609 R.styleable.GridDisplayOption_numFolderRows, numRows);
610 numFolderColumns = a.getInt(
611 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000612 numAllAppsColumns = a.getInt(
613 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700614
Sunny Goyal415f1732018-11-29 10:33:47 -0800615 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800616
617 extraAttrs = Themes.createValueMap(context, attrs,
618 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800619 }
620 }
621
622 private static final class DisplayOption {
623 private final GridOption grid;
624
Sunny Goyal415f1732018-11-29 10:33:47 -0800625 private final float minWidthDps;
626 private final float minHeightDps;
627 private final boolean canBeDefault;
628
629 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800630 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700631 private float landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000632 private float allAppsIconSize;
633 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800634
635 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
636 this.grid = grid;
637
638 TypedArray a = context.obtainStyledAttributes(
639 attrs, R.styleable.ProfileDisplayOption);
640
Sunny Goyal415f1732018-11-29 10:33:47 -0800641 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
642 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
643 canBeDefault = a.getBoolean(
644 R.styleable.ProfileDisplayOption_canBeDefault, false);
645
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700646 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800647 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
648 iconSize);
649 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700650
Sunny Goyalae190ff2020-04-14 00:19:01 +0000651 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
652 iconSize);
653 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
654 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800655 a.recycle();
656 }
657
658 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700659 this(null);
660 }
661
662 DisplayOption(GridOption grid) {
663 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800664 minWidthDps = 0;
665 minHeightDps = 0;
666 canBeDefault = false;
667 }
668
669 private DisplayOption multiply(float w) {
670 iconSize *= w;
671 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000672 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800673 iconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000674 allAppsIconTextSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800675 return this;
676 }
677
678 private DisplayOption add(DisplayOption p) {
679 iconSize += p.iconSize;
680 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000681 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800682 iconTextSize += p.iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000683 allAppsIconTextSize += p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800684 return this;
685 }
686 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700687
688 private class OverlayMonitor extends BroadcastReceiver {
689
690 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
691
692 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700693 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700694 }
695
696 @Override
697 public void onReceive(Context context, Intent intent) {
698 onConfigChanged(context);
699 }
700 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000701}