blob: 857db8e715c214fec987e929fa0016bacd6d61fc [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;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070020import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME;
Jon Miranda6f7e9702019-09-16 14:44:14 -070021import static com.android.launcher3.settings.SettingsActivity.GRID_OPTIONS_PREFERENCE_KEY;
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;
Jon Miranda64d74812019-10-15 15:33:16 -070063import java.util.Comparator;
Adam Cohen2e6da152015-05-06 11:42:25 -070064
65public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070066
Hyunyoung Songc55a3502018-12-04 15:43:16 -080067 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070068 // We do not need any synchronization for this variable as its only written on UI thread.
69 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070070 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070071
Hyunyoung Songc55a3502018-12-04 15:43:16 -080072 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080073
Sunny Goyal53d7ee42015-05-22 12:25:45 -070074 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
75
Sunny Goyal87dc48b2018-10-12 11:42:33 -070076 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080077 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
78
79 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070080
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070081 // Constants that affects the interpolation curve between statically defined device profile
82 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080083 private static final float KNEARESTNEIGHBOR = 3;
84 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070085
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070086 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080087 private static final float WEIGHT_EFFICIENT = 100000f;
88
89 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
90 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070091
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070092 /**
93 * Number of icons per row and column in the workspace.
94 */
Adam Cohen2e6da152015-05-06 11:42:25 -070095 public int numRows;
96 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070097
98 /**
99 * Number of icons per row and column in the folder.
100 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700101 public int numFolderRows;
102 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700103 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800104 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700105 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700106 public int iconBitmapSize;
107 public int fillResIconDpi;
108 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700109
Sunny Goyal5bc18462019-01-07 15:13:39 -0800110 private SparseArray<TypedValue> mExtraAttrs;
111
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700112 /**
113 * Number of icons inside the hotseat area.
114 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800115 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700116
Jon Miranda6f7e9702019-09-16 14:44:14 -0700117 /**
118 * Number of columns in the all apps list.
119 */
120 public int numAllAppsColumns;
121
Tracy Zhou7df93d22020-01-27 13:44:06 -0800122 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800123 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700124 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700125
cuijiaxingabda8d72017-03-20 09:51:36 -0700126 public DeviceProfile landscapeProfile;
127 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700128
Sunny Goyal6f866092016-03-17 17:04:15 -0700129 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700130 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700131
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700132 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
133 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700134 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700135
Sunny Goyalf633ef52018-03-13 09:57:05 -0700136 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700137 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700138
Sunny Goyalf633ef52018-03-13 09:57:05 -0700139 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800140 numRows = p.numRows;
141 numColumns = p.numColumns;
142 numFolderRows = p.numFolderRows;
143 numFolderColumns = p.numFolderColumns;
144 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800145 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800146 landscapeIconSize = p.landscapeIconSize;
147 iconTextSize = p.iconTextSize;
148 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700149 numAllAppsColumns = p.numAllAppsColumns;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800150 dbFile = p.dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800151 defaultLayoutId = p.defaultLayoutId;
152 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800153 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700154 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700155 }
156
Sunny Goyalbbf01842015-10-08 07:41:15 -0700157 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700158 private InvariantDeviceProfile(Context context) {
Jon Miranda6f7e9702019-09-16 14:44:14 -0700159 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
160 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
161 : null;
162 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700163 mConfigMonitor = new ConfigMonitor(context,
164 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700165 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700166 }
167
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700168 /**
169 * This constructor should NOT have any monitors by design.
170 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800171 public InvariantDeviceProfile(Context context, String gridName) {
172 String newName = initGrid(context, gridName);
173 if (newName == null || !newName.equals(gridName)) {
174 throw new IllegalArgumentException("Unknown grid name");
175 }
176 }
177
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800178 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800179 * This constructor should NOT have any monitors by design.
180 */
181 public InvariantDeviceProfile(Context context, Display display) {
182 initGrid(context, null, new Info(display));
183 }
184
185 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800186 * Retrieve system defined or RRO overriden icon shape.
187 */
188 private static String getIconShapePath(Context context) {
189 if (CONFIG_ICON_MASK_RES_ID == 0) {
190 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
191 return "";
192 }
193 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
194 }
195
Sunny Goyaleff44f32019-01-09 17:29:49 -0800196 private String initGrid(Context context, String gridName) {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800197 return initGrid(context, gridName, DefaultDisplay.INSTANCE.get(context).getInfo());
198 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700199
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800200 private String initGrid(Context context, String gridName, DefaultDisplay.Info displayInfo) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700201 Point smallestSize = new Point(displayInfo.smallestSize);
202 Point largestSize = new Point(displayInfo.largestSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700203
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700204 // This guarantees that width < height
Winson Chung13c1c2c2019-09-06 11:46:19 -0700205 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
206 displayInfo.metrics);
207 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
208 displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700209
Winson Chung13c1c2c2019-09-06 11:46:19 -0700210 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700211 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700212 // same in any orientation.
213 int smallSide = Math.min(realSize.x, realSize.y);
214 int largeSide = Math.max(realSize.x, realSize.y);
215
Jon Miranda64d74812019-10-15 15:33:16 -0700216 // We want a list of all options as well as the list of filtered options. This allows us
217 // to have a consistent UI for areas that the grid size change should not affect
218 // ie. All Apps should be consistent between grid sizes.
219 ArrayList<DisplayOption> allOptions = new ArrayList<>();
220 ArrayList<DisplayOption> filteredOptions = new ArrayList<>();
221 getPredefinedDeviceProfiles(context, gridName, filteredOptions, allOptions);
222
223 if (allOptions.isEmpty() && filteredOptions.isEmpty()) {
224 throw new RuntimeException("No display option with canBeDefault=true");
225 }
226
227 // Sort the profiles based on the closeness to the device size
228 Comparator<DisplayOption> comparator = (a, b) -> Float.compare(dist(minWidthDps,
229 minHeightDps, a.minWidthDps, a.minHeightDps),
230 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps));
231
232 // Calculate the device profiles as if there is no grid override.
233 Collections.sort(allOptions, comparator);
234 DisplayOption interpolatedDisplayOption =
235 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
236 initGridOption(context, allOptions, interpolatedDisplayOption, displayInfo.metrics);
237
238 // Create IDP with no grid override values.
239 InvariantDeviceProfile originalIDP = new InvariantDeviceProfile(this);
240 originalIDP.landscapeProfile = new DeviceProfile(context, this, null, smallestSize,
241 largestSize, largeSide, smallSide, true /* isLandscape */,
242 false /* isMultiWindowMode */);
243 originalIDP.portraitProfile = new DeviceProfile(context, this, null, smallestSize,
244 largestSize, smallSide, largeSide, false /* isLandscape */,
245 false /* isMultiWindowMode */);
246
247 if (filteredOptions.isEmpty()) {
248 filteredOptions = allOptions;
249
250 landscapeProfile = originalIDP.landscapeProfile;
251 portraitProfile = originalIDP.portraitProfile;
252 } else {
253 Collections.sort(filteredOptions, comparator);
254 interpolatedDisplayOption =
255 invDistWeightedInterpolate(minWidthDps, minHeightDps, filteredOptions);
256
257 initGridOption(context, filteredOptions, interpolatedDisplayOption,
258 displayInfo.metrics);
259 numAllAppsColumns = originalIDP.numAllAppsColumns;
260
261 landscapeProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
262 largestSize, largeSide, smallSide, true /* isLandscape */,
263 false /* isMultiWindowMode */);
264 portraitProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
265 largestSize, smallSide, largeSide, false /* isLandscape */,
266 false /* isMultiWindowMode */);
267 }
268
269 GridOption closestProfile = filteredOptions.get(0).grid;
270 if (!closestProfile.name.equals(gridName)) {
271 Utilities.getPrefs(context).edit()
272 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
273 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700274
275 // We need to ensure that there is enough extra space in the wallpaper
276 // for the intended parallax effects
277 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
278 defaultWallpaperSize = new Point(
279 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
280 largeSide);
281 } else {
282 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
283 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700284
285 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
286 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
287
Sunny Goyaleff44f32019-01-09 17:29:49 -0800288 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700289 }
290
Jon Miranda64d74812019-10-15 15:33:16 -0700291 private void initGridOption(Context context, ArrayList<DisplayOption> options,
292 DisplayOption displayOption, DisplayMetrics metrics) {
293 GridOption closestProfile = options.get(0).grid;
294 numRows = closestProfile.numRows;
295 numColumns = closestProfile.numColumns;
296 numHotseatIcons = closestProfile.numHotseatIcons;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800297 dbFile = closestProfile.dbFile;
Jon Miranda64d74812019-10-15 15:33:16 -0700298 defaultLayoutId = closestProfile.defaultLayoutId;
299 demoModeLayoutId = closestProfile.demoModeLayoutId;
300 numFolderRows = closestProfile.numFolderRows;
301 numFolderColumns = closestProfile.numFolderColumns;
302 numAllAppsColumns = numColumns;
303
304 mExtraAttrs = closestProfile.extraAttrs;
305
306 iconSize = displayOption.iconSize;
307 iconShapePath = getIconShapePath(context);
308 landscapeIconSize = displayOption.landscapeIconSize;
309 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
310 iconTextSize = displayOption.iconTextSize;
311 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
312
313 // If the partner customization apk contains any grid overrides, apply them
314 // Supported overrides: numRows, numColumns, iconSize
315 applyPartnerDeviceProfileOverrides(context, metrics);
316 }
317
318
Sunny Goyal5bc18462019-01-07 15:13:39 -0800319 @Nullable
320 public TypedValue getAttrValue(int attr) {
321 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
322 }
323
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700324 public void addOnChangeListener(OnIDPChangeListener listener) {
325 mChangeListeners.add(listener);
326 }
327
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800328 public void removeOnChangeListener(OnIDPChangeListener listener) {
329 mChangeListeners.remove(listener);
330 }
331
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700332 private void killProcess(Context context) {
333 Log.e("ConfigMonitor", "restarting launcher");
334 android.os.Process.killProcess(android.os.Process.myPid());
335 }
336
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800337 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800338 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
339 // Good place to check if grid size changed in themepicker when launcher was dead.
340 if (savedIconMaskPath.isEmpty()) {
341 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
342 .apply();
343 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
344 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
345 .apply();
346 apply(context, CHANGE_FLAG_ICON_PARAMS);
347 }
348 }
349
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800350 public void setCurrentGrid(Context context, String gridName) {
351 Context appContext = context.getApplicationContext();
352 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700353 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800354 }
355
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700356 private void onConfigChanged(Context context) {
357 // Config changes, what shall we do?
358 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
359
360 // Re-init grid
Jon Miranda6f7e9702019-09-16 14:44:14 -0700361 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
362 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
363 : null;
364 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700365
366 int changeFlags = 0;
367 if (numRows != oldProfile.numRows ||
368 numColumns != oldProfile.numColumns ||
369 numFolderColumns != oldProfile.numFolderColumns ||
370 numFolderRows != oldProfile.numFolderRows ||
371 numHotseatIcons != oldProfile.numHotseatIcons) {
372 changeFlags |= CHANGE_FLAG_GRID;
373 }
374
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800375 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
376 !iconShapePath.equals(oldProfile.iconShapePath)) {
377 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700378 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800379 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700380 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800381 }
382
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800383 apply(context, changeFlags);
384 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700385
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800386 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700387 // Create a new config monitor
388 mConfigMonitor.unregister();
389 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
390
391 for (OnIDPChangeListener listener : mChangeListeners) {
392 listener.onIdpChanged(changeFlags, this);
393 }
394 }
395
Jon Miranda64d74812019-10-15 15:33:16 -0700396 /**
397 * @param gridName The current grid name.
398 * @param filteredOptionsOut List filled with all the filtered options based on gridName.
399 * @param allOptionsOut List filled with all the options that can be the default option.
400 */
401 static void getPredefinedDeviceProfiles(Context context, String gridName,
402 ArrayList<DisplayOption> filteredOptionsOut, ArrayList<DisplayOption> allOptionsOut) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800403 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700404 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
405 final int depth = parser.getDepth();
406 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700407 while (((type = parser.next()) != XmlPullParser.END_TAG ||
408 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800409 if ((type == XmlPullParser.START_TAG)
410 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800411
412 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
413 final int displayDepth = parser.getDepth();
414 while (((type = parser.next()) != XmlPullParser.END_TAG ||
415 parser.getDepth() > displayDepth)
416 && type != XmlPullParser.END_DOCUMENT) {
417 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
418 parser.getName())) {
419 profiles.add(new DisplayOption(
420 gridOption, context, Xml.asAttributeSet(parser)));
421 }
422 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700423 }
424 }
425 } catch (IOException|XmlPullParserException e) {
426 throw new RuntimeException(e);
427 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800428
Sunny Goyal415f1732018-11-29 10:33:47 -0800429 if (!TextUtils.isEmpty(gridName)) {
430 for (DisplayOption option : profiles) {
431 if (gridName.equals(option.grid.name)) {
Jon Miranda64d74812019-10-15 15:33:16 -0700432 filteredOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800433 }
434 }
435 }
Jon Miranda64d74812019-10-15 15:33:16 -0700436
437 for (DisplayOption option : profiles) {
438 if (option.canBeDefault) {
439 allOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800440 }
441 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700442 }
443
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700444 private int getLauncherIconDensity(int requiredSize) {
445 // Densities typically defined by an app.
446 int[] densityBuckets = new int[] {
447 DisplayMetrics.DENSITY_LOW,
448 DisplayMetrics.DENSITY_MEDIUM,
449 DisplayMetrics.DENSITY_TV,
450 DisplayMetrics.DENSITY_HIGH,
451 DisplayMetrics.DENSITY_XHIGH,
452 DisplayMetrics.DENSITY_XXHIGH,
453 DisplayMetrics.DENSITY_XXXHIGH
454 };
455
456 int density = DisplayMetrics.DENSITY_XXXHIGH;
457 for (int i = densityBuckets.length - 1; i >= 0; i--) {
458 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
459 / DisplayMetrics.DENSITY_DEFAULT;
460 if (expectedSize >= requiredSize) {
461 density = densityBuckets[i];
462 }
463 }
464
465 return density;
466 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700467
Adam Cohen2e6da152015-05-06 11:42:25 -0700468 /**
469 * Apply any Partner customization grid overrides.
470 *
471 * Currently we support: all apps row / column count.
472 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700473 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
474 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700475 if (p != null) {
476 p.applyInvariantDeviceProfileOverrides(this, dm);
477 }
478 }
479
Sunny Goyal415f1732018-11-29 10:33:47 -0800480 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700481 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700482 }
483
Sunny Goyal415f1732018-11-29 10:33:47 -0800484 @VisibleForTesting
485 static DisplayOption invDistWeightedInterpolate(float width, float height,
486 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700487 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700488
Sunny Goyal415f1732018-11-29 10:33:47 -0800489 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700490 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
491 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700492 }
493
Sunny Goyal415f1732018-11-29 10:33:47 -0800494 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700495 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800496 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700497 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
498 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800499 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700500 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800501 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700502 }
503
Sunny Goyal27835952017-01-13 12:15:53 -0800504 public DeviceProfile getDeviceProfile(Context context) {
505 return context.getResources().getConfiguration().orientation
506 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
507 }
508
Sunny Goyal415f1732018-11-29 10:33:47 -0800509 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700510 float d = dist(x0, y0, x1, y1);
511 if (Float.compare(d, 0f) == 0) {
512 return Float.POSITIVE_INFINITY;
513 }
514 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
515 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700516
517 /**
518 * As a ratio of screen height, the total distance we want the parallax effect to span
519 * horizontally
520 */
521 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
522 float aspectRatio = width / (float) height;
523
524 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
525 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
526 // We will use these two data points to extrapolate how much the wallpaper parallax effect
527 // to span (ie travel) at any aspect ratio:
528
529 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
530 final float ASPECT_RATIO_PORTRAIT = 10/16f;
531 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
532 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
533
534 // To find out the desired width at different aspect ratios, we use the following two
535 // formulas, where the coefficient on x is the aspect ratio (width/height):
536 // (16/10)x + y = 1.5
537 // (10/16)x + y = 1.2
538 // We solve for x and y and end up with a final formula:
539 final float x =
540 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
541 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
542 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
543 return x * aspectRatio + y;
544 }
545
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700546 public interface OnIDPChangeListener {
547
548 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
549 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800550
551
Sunny Goyaleff44f32019-01-09 17:29:49 -0800552 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800553
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800554 public static final String TAG_NAME = "grid-option";
555
Sunny Goyaleff44f32019-01-09 17:29:49 -0800556 public final String name;
557 public final int numRows;
558 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800559
560 private final int numFolderRows;
561 private final int numFolderColumns;
562
563 private final int numHotseatIcons;
564
Tracy Zhou7df93d22020-01-27 13:44:06 -0800565 private final String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800566 private final int defaultLayoutId;
567 private final int demoModeLayoutId;
568
Sunny Goyal5bc18462019-01-07 15:13:39 -0800569 private final SparseArray<TypedValue> extraAttrs;
570
Sunny Goyaleff44f32019-01-09 17:29:49 -0800571 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800572 TypedArray a = context.obtainStyledAttributes(
573 attrs, R.styleable.GridDisplayOption);
574 name = a.getString(R.styleable.GridDisplayOption_name);
575 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
576 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
577
Tracy Zhou7df93d22020-01-27 13:44:06 -0800578 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800579 defaultLayoutId = a.getResourceId(
580 R.styleable.GridDisplayOption_defaultLayoutId, 0);
581 demoModeLayoutId = a.getResourceId(
582 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
583 numHotseatIcons = a.getInt(
584 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
585 numFolderRows = a.getInt(
586 R.styleable.GridDisplayOption_numFolderRows, numRows);
587 numFolderColumns = a.getInt(
588 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700589
Sunny Goyal415f1732018-11-29 10:33:47 -0800590 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800591
592 extraAttrs = Themes.createValueMap(context, attrs,
593 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800594 }
595 }
596
597 private static final class DisplayOption {
598 private final GridOption grid;
599
600 private final String name;
601 private final float minWidthDps;
602 private final float minHeightDps;
603 private final boolean canBeDefault;
604
605 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800606 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700607 private float landscapeIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800608
609 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
610 this.grid = grid;
611
612 TypedArray a = context.obtainStyledAttributes(
613 attrs, R.styleable.ProfileDisplayOption);
614
615 name = a.getString(R.styleable.ProfileDisplayOption_name);
616 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
617 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
618 canBeDefault = a.getBoolean(
619 R.styleable.ProfileDisplayOption_canBeDefault, false);
620
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700621 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800622 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
623 iconSize);
624 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700625
Sunny Goyal415f1732018-11-29 10:33:47 -0800626 a.recycle();
627 }
628
629 DisplayOption() {
630 grid = null;
631 name = null;
632 minWidthDps = 0;
633 minHeightDps = 0;
634 canBeDefault = false;
635 }
636
637 private DisplayOption multiply(float w) {
638 iconSize *= w;
639 landscapeIconSize *= w;
640 iconTextSize *= w;
641 return this;
642 }
643
644 private DisplayOption add(DisplayOption p) {
645 iconSize += p.iconSize;
646 landscapeIconSize += p.landscapeIconSize;
647 iconTextSize += p.iconTextSize;
648 return this;
649 }
650 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700651
652 private class OverlayMonitor extends BroadcastReceiver {
653
654 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
655
656 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700657 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700658 }
659
660 @Override
661 public void onReceive(Context context, Intent intent) {
662 onConfigChanged(context);
663 }
664 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700665}