blob: 8951674879a096d8693d0287b47751aada48e373 [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;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070021import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME;
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;
Winson Chung13c1c2c2019-09-06 11:46:19 -070051import com.android.launcher3.util.DefaultDisplay;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080052import com.android.launcher3.util.DefaultDisplay.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;
151 iconTextSize = p.iconTextSize;
152 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700153 numAllAppsColumns = p.numAllAppsColumns;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800154 dbFile = p.dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000155 allAppsIconSize = p.allAppsIconSize;
156 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800157 defaultLayoutId = p.defaultLayoutId;
158 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800159 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700160 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700161 }
162
Sunny Goyalbbf01842015-10-08 07:41:15 -0700163 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700164 private InvariantDeviceProfile(Context context) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700165 String gridName = getCurrentGridName(context);
166 String newGridName = initGrid(context, gridName);
167 if (!newGridName.equals(gridName)) {
168 Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply();
169 }
Tracy Zhoued5f3082020-04-20 01:13:26 -0700170 Utilities.getPrefs(context).edit()
171 .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numHotseatIcons)
172 .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows))
173 .apply();
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700174
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700175 mConfigMonitor = new ConfigMonitor(context,
176 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
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(
200 DefaultDisplay.INSTANCE.get(context).getInfo(),
201 getPredefinedDeviceProfiles(context, gridName));
202
203 Info myInfo = new Info(display);
204 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) {
Tracy Zhouc6060e62020-04-27 13:05:34 -0700217 return Utilities.isGridOptionsEnabled(context)
218 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
Tracy Zhou42255d22020-03-13 00:38:11 -0700219 }
220
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800221 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800222 * Retrieve system defined or RRO overriden icon shape.
223 */
224 private static String getIconShapePath(Context context) {
225 if (CONFIG_ICON_MASK_RES_ID == 0) {
226 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
227 return "";
228 }
229 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
230 }
231
Sunny Goyaleff44f32019-01-09 17:29:49 -0800232 private String initGrid(Context context, String gridName) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700233 DefaultDisplay.Info displayInfo = DefaultDisplay.INSTANCE.get(context).getInfo();
234 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
235
236 DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions);
237 initGrid(context, displayInfo, displayOption);
238 return displayOption.grid.name;
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800239 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700240
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700241 private void initGrid(
242 Context context, DefaultDisplay.Info displayInfo, DisplayOption displayOption) {
243 GridOption closestProfile = displayOption.grid;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000244 numRows = closestProfile.numRows;
245 numColumns = closestProfile.numColumns;
246 numHotseatIcons = closestProfile.numHotseatIcons;
247 dbFile = closestProfile.dbFile;
248 defaultLayoutId = closestProfile.defaultLayoutId;
249 demoModeLayoutId = closestProfile.demoModeLayoutId;
250 numFolderRows = closestProfile.numFolderRows;
251 numFolderColumns = closestProfile.numFolderColumns;
252 numAllAppsColumns = closestProfile.numAllAppsColumns;
253
254 mExtraAttrs = closestProfile.extraAttrs;
255
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700256 iconSize = displayOption.iconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000257 iconShapePath = getIconShapePath(context);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700258 landscapeIconSize = displayOption.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000259 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics);
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700260 iconTextSize = displayOption.iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000261 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
262
Tracy Zhouc6060e62020-04-27 13:05:34 -0700263 if (Utilities.isGridOptionsEnabled(context)) {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700264 allAppsIconSize = displayOption.allAppsIconSize;
265 allAppsIconTextSize = displayOption.allAppsIconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000266 } else {
267 allAppsIconSize = iconSize;
268 allAppsIconTextSize = iconTextSize;
269 }
270
271 // If the partner customization apk contains any grid overrides, apply them
272 // Supported overrides: numRows, numColumns, iconSize
273 applyPartnerDeviceProfileOverrides(context, displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700274
Winson Chung13c1c2c2019-09-06 11:46:19 -0700275 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700276 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700277 // same in any orientation.
278 int smallSide = Math.min(realSize.x, realSize.y);
279 int largeSide = Math.max(realSize.x, realSize.y);
280
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700281 DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo)
282 .setSizeRange(new Point(displayInfo.smallestSize),
283 new Point(displayInfo.largestSize));
284
285 landscapeProfile = builder.setSize(largeSide, smallSide).build();
286 portraitProfile = builder.setSize(smallSide, largeSide).build();
Sunny Goyal6f866092016-03-17 17:04:15 -0700287
288 // We need to ensure that there is enough extra space in the wallpaper
289 // for the intended parallax effects
290 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
291 defaultWallpaperSize = new Point(
292 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
293 largeSide);
294 } else {
295 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
296 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700297
298 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
299 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
Adam Cohen2e6da152015-05-06 11:42:25 -0700300 }
301
Sunny Goyal5bc18462019-01-07 15:13:39 -0800302 @Nullable
303 public TypedValue getAttrValue(int attr) {
304 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
305 }
306
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700307 public void addOnChangeListener(OnIDPChangeListener listener) {
308 mChangeListeners.add(listener);
309 }
310
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800311 public void removeOnChangeListener(OnIDPChangeListener listener) {
312 mChangeListeners.remove(listener);
313 }
314
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700315 private void killProcess(Context context) {
316 Log.e("ConfigMonitor", "restarting launcher");
317 android.os.Process.killProcess(android.os.Process.myPid());
318 }
319
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800320 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800321 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
322 // Good place to check if grid size changed in themepicker when launcher was dead.
323 if (savedIconMaskPath.isEmpty()) {
324 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
325 .apply();
326 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
327 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
328 .apply();
329 apply(context, CHANGE_FLAG_ICON_PARAMS);
330 }
331 }
332
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800333 public void setCurrentGrid(Context context, String gridName) {
334 Context appContext = context.getApplicationContext();
335 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700336 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800337 }
338
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700339 private void onConfigChanged(Context context) {
340 // Config changes, what shall we do?
341 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
342
343 // Re-init grid
Tracy Zhouc6060e62020-04-27 13:05:34 -0700344 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700345 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700346
347 int changeFlags = 0;
348 if (numRows != oldProfile.numRows ||
349 numColumns != oldProfile.numColumns ||
350 numFolderColumns != oldProfile.numFolderColumns ||
351 numFolderRows != oldProfile.numFolderRows ||
352 numHotseatIcons != oldProfile.numHotseatIcons) {
353 changeFlags |= CHANGE_FLAG_GRID;
354 }
355
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800356 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
357 !iconShapePath.equals(oldProfile.iconShapePath)) {
358 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700359 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800360 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700361 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800362 }
363
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800364 apply(context, changeFlags);
365 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700366
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800367 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700368 // Create a new config monitor
369 mConfigMonitor.unregister();
370 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
371
372 for (OnIDPChangeListener listener : mChangeListeners) {
373 listener.onIdpChanged(changeFlags, this);
374 }
375 }
376
Sunny Goyalae190ff2020-04-14 00:19:01 +0000377 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800378 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700379 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
380 final int depth = parser.getDepth();
381 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700382 while (((type = parser.next()) != XmlPullParser.END_TAG ||
383 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800384 if ((type == XmlPullParser.START_TAG)
385 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800386
387 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
388 final int displayDepth = parser.getDepth();
389 while (((type = parser.next()) != XmlPullParser.END_TAG ||
390 parser.getDepth() > displayDepth)
391 && type != XmlPullParser.END_DOCUMENT) {
392 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
393 parser.getName())) {
394 profiles.add(new DisplayOption(
395 gridOption, context, Xml.asAttributeSet(parser)));
396 }
397 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700398 }
399 }
400 } catch (IOException|XmlPullParserException e) {
401 throw new RuntimeException(e);
402 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800403
Sunny Goyalae190ff2020-04-14 00:19:01 +0000404 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
Sunny Goyal415f1732018-11-29 10:33:47 -0800405 if (!TextUtils.isEmpty(gridName)) {
406 for (DisplayOption option : profiles) {
407 if (gridName.equals(option.grid.name)) {
Sunny Goyalae190ff2020-04-14 00:19:01 +0000408 filteredProfiles.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800409 }
410 }
411 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000412 if (filteredProfiles.isEmpty()) {
413 // No grid found, use the default options
414 for (DisplayOption option : profiles) {
415 if (option.canBeDefault) {
416 filteredProfiles.add(option);
417 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800418 }
419 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000420 if (filteredProfiles.isEmpty()) {
421 throw new RuntimeException("No display option with canBeDefault=true");
422 }
423 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700424 }
425
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700426 private int getLauncherIconDensity(int requiredSize) {
427 // Densities typically defined by an app.
428 int[] densityBuckets = new int[] {
429 DisplayMetrics.DENSITY_LOW,
430 DisplayMetrics.DENSITY_MEDIUM,
431 DisplayMetrics.DENSITY_TV,
432 DisplayMetrics.DENSITY_HIGH,
433 DisplayMetrics.DENSITY_XHIGH,
434 DisplayMetrics.DENSITY_XXHIGH,
435 DisplayMetrics.DENSITY_XXXHIGH
436 };
437
438 int density = DisplayMetrics.DENSITY_XXXHIGH;
439 for (int i = densityBuckets.length - 1; i >= 0; i--) {
440 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
441 / DisplayMetrics.DENSITY_DEFAULT;
442 if (expectedSize >= requiredSize) {
443 density = densityBuckets[i];
444 }
445 }
446
447 return density;
448 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700449
Adam Cohen2e6da152015-05-06 11:42:25 -0700450 /**
451 * Apply any Partner customization grid overrides.
452 *
453 * Currently we support: all apps row / column count.
454 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700455 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
456 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700457 if (p != null) {
458 p.applyInvariantDeviceProfileOverrides(this, dm);
459 }
460 }
461
Sunny Goyal415f1732018-11-29 10:33:47 -0800462 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700463 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700464 }
465
Sunny Goyal415f1732018-11-29 10:33:47 -0800466 @VisibleForTesting
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700467 static DisplayOption invDistWeightedInterpolate(
468 DefaultDisplay.Info displayInfo, ArrayList<DisplayOption> points) {
469 Point smallestSize = new Point(displayInfo.smallestSize);
470 Point largestSize = new Point(displayInfo.largestSize);
471
472 // This guarantees that width < height
473 float width = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
474 displayInfo.metrics);
475 float height = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
476 displayInfo.metrics);
477
478 // Sort the profiles based on the closeness to the device size
479 Collections.sort(points, (a, b) ->
480 Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
481 dist(width, height, b.minWidthDps, b.minHeightDps)));
482
483 GridOption closestOption = points.get(0).grid;
484 float weights = 0;
485
486 DisplayOption p = points.get(0);
487 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
488 return p;
489 }
490
491 DisplayOption out = new DisplayOption(closestOption);
492 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
493 p = points.get(i);
494 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
495 weights += w;
496 out.add(new DisplayOption().add(p).multiply(w));
497 }
498 return out.multiply(1.0f / weights);
499 }
500
501 @VisibleForTesting
Sunny Goyal415f1732018-11-29 10:33:47 -0800502 static DisplayOption invDistWeightedInterpolate(float width, float height,
Sunny Goyalae190ff2020-04-14 00:19:01 +0000503 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700504 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700505
Sunny Goyal415f1732018-11-29 10:33:47 -0800506 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700507 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
508 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700509 }
510
Sunny Goyal415f1732018-11-29 10:33:47 -0800511 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700512 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800513 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700514 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
515 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800516 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700517 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800518 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700519 }
520
Sunny Goyal27835952017-01-13 12:15:53 -0800521 public DeviceProfile getDeviceProfile(Context context) {
522 return context.getResources().getConfiguration().orientation
523 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
524 }
525
Sunny Goyal415f1732018-11-29 10:33:47 -0800526 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700527 float d = dist(x0, y0, x1, y1);
528 if (Float.compare(d, 0f) == 0) {
529 return Float.POSITIVE_INFINITY;
530 }
531 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
532 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700533
534 /**
535 * As a ratio of screen height, the total distance we want the parallax effect to span
536 * horizontally
537 */
538 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
539 float aspectRatio = width / (float) height;
540
541 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
542 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
543 // We will use these two data points to extrapolate how much the wallpaper parallax effect
544 // to span (ie travel) at any aspect ratio:
545
546 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
547 final float ASPECT_RATIO_PORTRAIT = 10/16f;
548 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
549 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
550
551 // To find out the desired width at different aspect ratios, we use the following two
552 // formulas, where the coefficient on x is the aspect ratio (width/height):
553 // (16/10)x + y = 1.5
554 // (10/16)x + y = 1.2
555 // We solve for x and y and end up with a final formula:
556 final float x =
557 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
558 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
559 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
560 return x * aspectRatio + y;
561 }
562
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700563 public interface OnIDPChangeListener {
564
565 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
566 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800567
568
Sunny Goyaleff44f32019-01-09 17:29:49 -0800569 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800570
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800571 public static final String TAG_NAME = "grid-option";
572
Sunny Goyaleff44f32019-01-09 17:29:49 -0800573 public final String name;
574 public final int numRows;
575 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800576
577 private final int numFolderRows;
578 private final int numFolderColumns;
579
580 private final int numHotseatIcons;
581
Tracy Zhou7df93d22020-01-27 13:44:06 -0800582 private final String dbFile;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000583 private final int numAllAppsColumns;
584
Sunny Goyal415f1732018-11-29 10:33:47 -0800585 private final int defaultLayoutId;
586 private final int demoModeLayoutId;
587
Sunny Goyal5bc18462019-01-07 15:13:39 -0800588 private final SparseArray<TypedValue> extraAttrs;
589
Sunny Goyaleff44f32019-01-09 17:29:49 -0800590 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800591 TypedArray a = context.obtainStyledAttributes(
592 attrs, R.styleable.GridDisplayOption);
593 name = a.getString(R.styleable.GridDisplayOption_name);
594 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
595 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
596
Tracy Zhou7df93d22020-01-27 13:44:06 -0800597 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800598 defaultLayoutId = a.getResourceId(
599 R.styleable.GridDisplayOption_defaultLayoutId, 0);
600 demoModeLayoutId = a.getResourceId(
601 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
602 numHotseatIcons = a.getInt(
603 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
604 numFolderRows = a.getInt(
605 R.styleable.GridDisplayOption_numFolderRows, numRows);
606 numFolderColumns = a.getInt(
607 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Sunny Goyalae190ff2020-04-14 00:19:01 +0000608 numAllAppsColumns = a.getInt(
609 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700610
Sunny Goyal415f1732018-11-29 10:33:47 -0800611 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800612
613 extraAttrs = Themes.createValueMap(context, attrs,
614 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800615 }
616 }
617
618 private static final class DisplayOption {
619 private final GridOption grid;
620
Sunny Goyal415f1732018-11-29 10:33:47 -0800621 private final float minWidthDps;
622 private final float minHeightDps;
623 private final boolean canBeDefault;
624
625 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800626 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700627 private float landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000628 private float allAppsIconSize;
629 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800630
631 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
632 this.grid = grid;
633
634 TypedArray a = context.obtainStyledAttributes(
635 attrs, R.styleable.ProfileDisplayOption);
636
Sunny Goyal415f1732018-11-29 10:33:47 -0800637 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
638 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
639 canBeDefault = a.getBoolean(
640 R.styleable.ProfileDisplayOption_canBeDefault, false);
641
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700642 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800643 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
644 iconSize);
645 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700646
Sunny Goyalae190ff2020-04-14 00:19:01 +0000647 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
648 iconSize);
649 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
650 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800651 a.recycle();
652 }
653
654 DisplayOption() {
Sunny Goyal0e7a3382020-04-13 17:15:05 -0700655 this(null);
656 }
657
658 DisplayOption(GridOption grid) {
659 this.grid = grid;
Sunny Goyal415f1732018-11-29 10:33:47 -0800660 minWidthDps = 0;
661 minHeightDps = 0;
662 canBeDefault = false;
663 }
664
665 private DisplayOption multiply(float w) {
666 iconSize *= w;
667 landscapeIconSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000668 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800669 iconTextSize *= w;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000670 allAppsIconTextSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800671 return this;
672 }
673
674 private DisplayOption add(DisplayOption p) {
675 iconSize += p.iconSize;
676 landscapeIconSize += p.landscapeIconSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000677 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800678 iconTextSize += p.iconTextSize;
Sunny Goyalae190ff2020-04-14 00:19:01 +0000679 allAppsIconTextSize += p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800680 return this;
681 }
682 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700683
684 private class OverlayMonitor extends BroadcastReceiver {
685
686 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
687
688 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700689 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700690 }
691
692 @Override
693 public void onReceive(Context context, Intent intent) {
694 onConfigChanged(context);
695 }
696 }
Sunny Goyalae190ff2020-04-14 00:19:01 +0000697}