blob: aa3ef9b13956e98cb83a50d650935e55feb41f75 [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
Sunny Goyal6f866092016-03-17 17:04:15 -0700133 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700134 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700135
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700136 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
137 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700138 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700139
Sunny Goyalf633ef52018-03-13 09:57:05 -0700140 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700141 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700142
Sunny Goyalf633ef52018-03-13 09:57:05 -0700143 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800144 numRows = p.numRows;
145 numColumns = p.numColumns;
146 numFolderRows = p.numFolderRows;
147 numFolderColumns = p.numFolderColumns;
148 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800149 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800150 landscapeIconSize = p.landscapeIconSize;
Tony Wickham9459f312020-06-15 13:30:20 -0500151 iconBitmapSize = p.iconBitmapSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800152 iconTextSize = p.iconTextSize;
153 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700154 numAllAppsColumns = p.numAllAppsColumns;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800155 dbFile = p.dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000156 allAppsIconSize = p.allAppsIconSize;
157 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800158 defaultLayoutId = p.defaultLayoutId;
159 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800160 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700161 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700162 }
163
Sunny Goyalbbf01842015-10-08 07:41:15 -0700164 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700165 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700166 String gridName = getCurrentGridName(context);
167 String newGridName = initGrid(context, gridName);
168 if (!newGridName.equals(gridName)) {
169 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
170 }
Tracy Zhoued5f3082020-04-20 01:13:26 -0700171 Utilities.getPrefs(context).edit()
172 .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numHotseatIcons)
173 .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows))
174 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700175
Sunny Goyalead22a52021-02-02 23:21:55 -0800176 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700177 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700178 }
179
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700180 /**
181 * This constructor should NOT have any monitors by design.
182 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800183 public InvariantDeviceProfile(Context context, String gridName) {
184 String newName = initGrid(context, gridName);
185 if (newName == null || !newName.equals(gridName)) {
186 throw new IllegalArgumentException("Unknown grid name");
187 }
188 }
189
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700190 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800191 * This constructor should NOT have any monitors by design.
192 */
193 public InvariantDeviceProfile(Context context, Display display) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700194 // Ensure that the main device profile is initialized
195 InvariantDeviceProfile originalProfile = INSTANCE.get(context);
196 String gridName = getCurrentGridName(context);
197
198 // Get the display info based on default display and interpolate it to existing display
199 DisplayOption defaultDisplayOption = invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700200 DisplayController.getDefaultDisplay(context).getInfo(),
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700201 getPredefinedDeviceProfiles(context, gridName));
202
Alex Chau417bd722021-01-26 15:22:18 +0000203 Info myInfo = new Info(context, display);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700204 DisplayOption myDisplayOption = invDistWeightedInterpolate(
205 myInfo, getPredefinedDeviceProfiles(context, gridName));
206
207 DisplayOption result = new DisplayOption(defaultDisplayOption.grid)
208 .add(myDisplayOption);
209 result.iconSize = defaultDisplayOption.iconSize;
210 result.landscapeIconSize = defaultDisplayOption.landscapeIconSize;
211 result.allAppsIconSize = Math.min(
212 defaultDisplayOption.allAppsIconSize, myDisplayOption.allAppsIconSize);
213 initGrid(context, myInfo, result);
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800214 }
215
Tracy Zhou42255d22020-03-13 00:38:11 -0700216 public static String getCurrentGridName(Context context) {
Jon Miranda73dcc982020-12-11 14:14:46 -0800217 if (ENABLE_FOUR_COLUMNS.get()) {
218 return ENABLE_FOUR_COLUMNS.key;
219 }
Tracy Zhouc6060e62020-04-27 13:05:34 -0700220 return Utilities.isGridOptionsEnabled(context)
221 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700222 }
223
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800224 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800225 * Retrieve system defined or RRO overriden icon shape.
226 */
227 private static String getIconShapePath(Context context) {
228 if (CONFIG_ICON_MASK_RES_ID == 0) {
229 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
230 return "";
231 }
232 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
233 }
234
Sunny Goyaleff44f32019-01-09 17:29:49 -0800235 private String initGrid(Context context, String gridName) {
Sunny Goyalfd58da62020-08-11 12:06:49 -0700236 Info displayInfo = DisplayController.getDefaultDisplay(context).getInfo();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700237 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
238
239 DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions);
240 initGrid(context, displayInfo, displayOption);
241 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800242 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700243
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700244 private void initGrid(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700245 Context context, Info displayInfo, DisplayOption displayOption) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700246 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000247 numRows = closestProfile.numRows;
248 numColumns = closestProfile.numColumns;
249 numHotseatIcons = closestProfile.numHotseatIcons;
250 dbFile = closestProfile.dbFile;
251 defaultLayoutId = closestProfile.defaultLayoutId;
252 demoModeLayoutId = closestProfile.demoModeLayoutId;
253 numFolderRows = closestProfile.numFolderRows;
254 numFolderColumns = closestProfile.numFolderColumns;
255 numAllAppsColumns = closestProfile.numAllAppsColumns;
256
257 mExtraAttrs = closestProfile.extraAttrs;
258
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700259 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000260 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700261 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000262 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700263 iconTextSize = displayOption.iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000264 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
265
Tracy Zhouc6060e62020-04-27 13:05:34 -0700266 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700267 allAppsIconSize = displayOption.allAppsIconSize;
268 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000269 } else {
270 allAppsIconSize = iconSize;
271 allAppsIconTextSize = iconTextSize;
272 }
273
274 // If the partner customization apk contains any grid overrides, apply them
275 // Supported overrides: numRows, numColumns, iconSize
276 applyPartnerDeviceProfileOverrides(context, displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700277
Winson Chung13c1c2c2019-09-06 11:46:19 -0700278 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700279 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700280 // same in any orientation.
281 int smallSide = Math.min(realSize.x, realSize.y);
282 int largeSide = Math.max(realSize.x, realSize.y);
283
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700284 DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo)
285 .setSizeRange(new Point(displayInfo.smallestSize),
286 new Point(displayInfo.largestSize));
287
288 landscapeProfile = builder.setSize(largeSide, smallSide).build();
289 portraitProfile = builder.setSize(smallSide, largeSide).build();
Sunny Goyal6f866092016-03-17 17:04:15 -0700290
291 // We need to ensure that there is enough extra space in the wallpaper
292 // for the intended parallax effects
293 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
294 defaultWallpaperSize = new Point(
295 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
296 largeSide);
297 } else {
298 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
299 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700300
301 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
302 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700303 }
304
Sunny Goyal5bc18462019-01-07 15:13:39 -0800305 @Nullable
306 public TypedValue getAttrValue(int attr) {
307 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
308 }
309
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700310 public void addOnChangeListener(OnIDPChangeListener listener) {
311 mChangeListeners.add(listener);
312 }
313
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800314 public void removeOnChangeListener(OnIDPChangeListener listener) {
315 mChangeListeners.remove(listener);
316 }
317
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800318 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800319 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
320 // Good place to check if grid size changed in themepicker when launcher was dead.
321 if (savedIconMaskPath.isEmpty()) {
322 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
323 .apply();
324 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
325 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
326 .apply();
327 apply(context, CHANGE_FLAG_ICON_PARAMS);
328 }
329 }
330
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800331 public void setCurrentGrid(Context context, String gridName) {
332 Context appContext = context.getApplicationContext();
333 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700334 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800335 }
336
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700337 private void onConfigChanged(Context context) {
338 // Config changes, what shall we do?
339 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
340
341 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700342 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700343 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700344
345 int changeFlags = 0;
346 if (numRows != oldProfile.numRows ||
347 numColumns != oldProfile.numColumns ||
348 numFolderColumns != oldProfile.numFolderColumns ||
349 numFolderRows != oldProfile.numFolderRows ||
350 numHotseatIcons != oldProfile.numHotseatIcons) {
351 changeFlags |= CHANGE_FLAG_GRID;
352 }
353
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800354 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
355 !iconShapePath.equals(oldProfile.iconShapePath)) {
356 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700357 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800358 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700359 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800360 }
361
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800362 apply(context, changeFlags);
363 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700364
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800365 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700366 // Create a new config monitor
367 mConfigMonitor.unregister();
368 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
369
370 for (OnIDPChangeListener listener : mChangeListeners) {
371 listener.onIdpChanged(changeFlags, this);
372 }
373 }
374
Sunny Goyalae190ff2020-04-14 00:19:01 +0000375 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800376 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700377 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
378 final int depth = parser.getDepth();
379 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700380 while (((type = parser.next()) != XmlPullParser.END_TAG ||
381 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800382 if ((type == XmlPullParser.START_TAG)
383 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800384
385 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
386 final int displayDepth = parser.getDepth();
387 while (((type = parser.next()) != XmlPullParser.END_TAG ||
388 parser.getDepth() > displayDepth)
389 && type != XmlPullParser.END_DOCUMENT) {
390 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
391 parser.getName())) {
392 profiles.add(new DisplayOption(
393 gridOption, context, Xml.asAttributeSet(parser)));
394 }
395 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700396 }
397 }
398 } catch (IOException|XmlPullParserException e) {
399 throw new RuntimeException(e);
400 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800401
Sunny Goyalae190ff2020-04-14 00:19:01 +0000402 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800403 if (!TextUtils.isEmpty(gridName)) {
404 for (DisplayOption option : profiles) {
405 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000406 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800407 }
408 }
409 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000410 if (filteredProfiles.isEmpty()) {
411 // No grid found, use the default options
412 for (DisplayOption option : profiles) {
413 if (option.canBeDefault) {
414 filteredProfiles.add(option);
415 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800416 }
417 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000418 if (filteredProfiles.isEmpty()) {
419 throw new RuntimeException("No display option with canBeDefault=true");
420 }
421 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700422 }
423
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700424 private int getLauncherIconDensity(int requiredSize) {
425 // Densities typically defined by an app.
426 int[] densityBuckets = new int[] {
427 DisplayMetrics.DENSITY_LOW,
428 DisplayMetrics.DENSITY_MEDIUM,
429 DisplayMetrics.DENSITY_TV,
430 DisplayMetrics.DENSITY_HIGH,
431 DisplayMetrics.DENSITY_XHIGH,
432 DisplayMetrics.DENSITY_XXHIGH,
433 DisplayMetrics.DENSITY_XXXHIGH
434 };
435
436 int density = DisplayMetrics.DENSITY_XXXHIGH;
437 for (int i = densityBuckets.length - 1; i >= 0; i--) {
438 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
439 / DisplayMetrics.DENSITY_DEFAULT;
440 if (expectedSize >= requiredSize) {
441 density = densityBuckets[i];
442 }
443 }
444
445 return density;
446 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700447
Adam Cohen2e6da152015-05-06 11:42:25 -0700448 /**
449 * Apply any Partner customization grid overrides.
450 *
451 * Currently we support: all apps row / column count.
452 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700453 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
454 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700455 if (p != null) {
456 p.applyInvariantDeviceProfileOverrides(this, dm);
457 }
458 }
459
Sunny Goyal415f1732018-11-29 10:33:47 -0800460 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700461 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700462 }
463
Sunny Goyal415f1732018-11-29 10:33:47 -0800464 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700465 static DisplayOption invDistWeightedInterpolate(
Sunny Goyalfd58da62020-08-11 12:06:49 -0700466 Info displayInfo, ArrayList<DisplayOption> points) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700467 Point smallestSize = new Point(displayInfo.smallestSize);
468 Point largestSize = new Point(displayInfo.largestSize);
469
470 // This guarantees that width < height
471 float width = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
472 displayInfo.metrics);
473 float height = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
474 displayInfo.metrics);
475
476 // Sort the profiles based on the closeness to the device size
477 Collections.sort(points, (a, b) ->
478 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
479 dist(width, height, b.minWidthDps, b.minHeightDps)));
480
481 GridOption closestOption = points.get(0).grid;
482 float weights = 0;
483
484 DisplayOption p = points.get(0);
485 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
486 return p;
487 }
488
489 DisplayOption out = new DisplayOption(closestOption);
490 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
491 p = points.get(i);
492 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
493 weights += w;
494 out.add(new DisplayOption().add(p).multiply(w));
495 }
496 return out.multiply(1.0f / weights);
497 }
498
499 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800500 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000501 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700502 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700503
Sunny Goyal415f1732018-11-29 10:33:47 -0800504 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700505 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
506 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700507 }
508
Sunny Goyal415f1732018-11-29 10:33:47 -0800509 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700510 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800511 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700512 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
513 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800514 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700515 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800516 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700517 }
518
Sunny Goyal27835952017-01-13 12:15:53 -0800519 public DeviceProfile getDeviceProfile(Context context) {
520 return context.getResources().getConfiguration().orientation
521 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
522 }
523
Sunny Goyal415f1732018-11-29 10:33:47 -0800524 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700525 float d = dist(x0, y0, x1, y1);
526 if (Float.compare(d, 0f) == 0) {
527 return Float.POSITIVE_INFINITY;
528 }
529 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
530 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700531
532 /**
533 * As a ratio of screen height, the total distance we want the parallax effect to span
534 * horizontally
535 */
536 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
537 float aspectRatio = width / (float) height;
538
539 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
540 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
541 // We will use these two data points to extrapolate how much the wallpaper parallax effect
542 // to span (ie travel) at any aspect ratio:
543
544 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
545 final float ASPECT_RATIO_PORTRAIT = 10/16f;
546 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
547 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
548
549 // To find out the desired width at different aspect ratios, we use the following two
550 // formulas, where the coefficient on x is the aspect ratio (width/height):
551 // (16/10)x + y = 1.5
552 // (10/16)x + y = 1.2
553 // We solve for x and y and end up with a final formula:
554 final float x =
555 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
556 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
557 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
558 return x * aspectRatio + y;
559 }
560
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700561 public interface OnIDPChangeListener {
562
563 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
564 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800565
566
Sunny Goyaleff44f32019-01-09 17:29:49 -0800567 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800568
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800569 public static final String TAG_NAME = "grid-option";
570
Sunny Goyaleff44f32019-01-09 17:29:49 -0800571 public final String name;
572 public final int numRows;
573 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800574
575 private final int numFolderRows;
576 private final int numFolderColumns;
577
578 private final int numHotseatIcons;
579
Tracy Zhou7df93d22020-01-27 13:44:06 -0800580 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000581 private final int numAllAppsColumns;
582
Sunny Goyal415f1732018-11-29 10:33:47 -0800583 private final int defaultLayoutId;
584 private final int demoModeLayoutId;
585
Sunny Goyal5bc18462019-01-07 15:13:39 -0800586 private final SparseArray<TypedValue> extraAttrs;
587
Sunny Goyaleff44f32019-01-09 17:29:49 -0800588 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800589 TypedArray a = context.obtainStyledAttributes(
590 attrs, R.styleable.GridDisplayOption);
591 name = a.getString(R.styleable.GridDisplayOption_name);
592 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
593 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
594
Tracy Zhou7df93d22020-01-27 13:44:06 -0800595 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800596 defaultLayoutId = a.getResourceId(
597 R.styleable.GridDisplayOption_defaultLayoutId, 0);
598 demoModeLayoutId = a.getResourceId(
599 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
600 numHotseatIcons = a.getInt(
601 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
602 numFolderRows = a.getInt(
603 R.styleable.GridDisplayOption_numFolderRows, numRows);
604 numFolderColumns = a.getInt(
605 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000606 numAllAppsColumns = a.getInt(
607 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700608
Sunny Goyal415f1732018-11-29 10:33:47 -0800609 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800610
611 extraAttrs = Themes.createValueMap(context, attrs,
612 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800613 }
614 }
615
616 private static final class DisplayOption {
617 private final GridOption grid;
618
Sunny Goyal415f1732018-11-29 10:33:47 -0800619 private final float minWidthDps;
620 private final float minHeightDps;
621 private final boolean canBeDefault;
622
623 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800624 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700625 private float landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000626 private float allAppsIconSize;
627 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800628
629 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
630 this.grid = grid;
631
632 TypedArray a = context.obtainStyledAttributes(
633 attrs, R.styleable.ProfileDisplayOption);
634
Sunny Goyal415f1732018-11-29 10:33:47 -0800635 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
636 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
637 canBeDefault = a.getBoolean(
638 R.styleable.ProfileDisplayOption_canBeDefault, false);
639
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700640 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800641 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
642 iconSize);
643 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700644
Sunny Goyalae190ff2020-04-14 00:19:01 +0000645 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
646 iconSize);
647 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
648 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800649 a.recycle();
650 }
651
652 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700653 this(null);
654 }
655
656 DisplayOption(GridOption grid) {
657 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800658 minWidthDps = 0;
659 minHeightDps = 0;
660 canBeDefault = false;
661 }
662
663 private DisplayOption multiply(float w) {
664 iconSize *= w;
665 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000666 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800667 iconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000668 allAppsIconTextSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800669 return this;
670 }
671
672 private DisplayOption add(DisplayOption p) {
673 iconSize += p.iconSize;
674 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000675 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800676 iconTextSize += p.iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000677 allAppsIconTextSize += p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800678 return this;
679 }
680 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700681
682 private class OverlayMonitor extends BroadcastReceiver {
683
684 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
685
686 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700687 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700688 }
689
690 @Override
691 public void onReceive(Context context, Intent intent) {
692 onConfigChanged(context);
693 }
694 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000695}