blob: d66ba731757b31a6f2c237056e2ab0a207f85507 [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;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070021import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Jon Miranda6f7e9702019-09-16 14:44:14 -070022import static com.android.launcher3.settings.SettingsActivity.GRID_OPTIONS_PREFERENCE_KEY;
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;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070044
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070045import androidx.annotation.Nullable;
46import androidx.annotation.VisibleForTesting;
47
Sunny Goyal905262c2019-05-03 16:50:43 -070048import com.android.launcher3.graphics.IconShape;
Sunny Goyald0e360a2018-06-29 14:40:18 -070049import com.android.launcher3.util.ConfigMonitor;
Winson Chung13c1c2c2019-09-06 11:46:19 -070050import com.android.launcher3.util.DefaultDisplay;
Sunny Goyal5bc18462019-01-07 15:13:39 -080051import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070052import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080053import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070054
Sunny Goyal819e1932016-07-07 16:43:58 -070055import org.xmlpull.v1.XmlPullParser;
56import org.xmlpull.v1.XmlPullParserException;
57
58import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070059import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080060import java.util.Collections;
Jon Miranda64d74812019-10-15 15:33:16 -070061import java.util.Comparator;
Adam Cohen2e6da152015-05-06 11:42:25 -070062
63public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070064
Hyunyoung Songc55a3502018-12-04 15:43:16 -080065 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070066 // We do not need any synchronization for this variable as its only written on UI thread.
67 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070068 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070069
Hyunyoung Songc55a3502018-12-04 15:43:16 -080070 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080071
Sunny Goyal53d7ee42015-05-22 12:25:45 -070072 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
73
Sunny Goyal87dc48b2018-10-12 11:42:33 -070074 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080075 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
76
77 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070078
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070079 // Constants that affects the interpolation curve between statically defined device profile
80 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080081 private static final float KNEARESTNEIGHBOR = 3;
82 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070083
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070084 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080085 private static final float WEIGHT_EFFICIENT = 100000f;
86
87 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
88 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070089
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070090 /**
91 * Number of icons per row and column in the workspace.
92 */
Adam Cohen2e6da152015-05-06 11:42:25 -070093 public int numRows;
94 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070095
96 /**
97 * Number of icons per row and column in the folder.
98 */
Adam Cohen2e6da152015-05-06 11:42:25 -070099 public int numFolderRows;
100 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700101 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800102 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700103 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700104 public int iconBitmapSize;
105 public int fillResIconDpi;
106 public float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700107 public float allAppsIconSize;
108 public float allAppsIconTextSize;
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
Sunny Goyal415f1732018-11-29 10:33:47 -0800122 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700123 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700124
cuijiaxingabda8d72017-03-20 09:51:36 -0700125 public DeviceProfile landscapeProfile;
126 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700127
Sunny Goyal6f866092016-03-17 17:04:15 -0700128 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700129 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700130
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700131 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
132 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700133 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700134
Sunny Goyalf633ef52018-03-13 09:57:05 -0700135 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700136 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700137
Sunny Goyalf633ef52018-03-13 09:57:05 -0700138 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800139 numRows = p.numRows;
140 numColumns = p.numColumns;
141 numFolderRows = p.numFolderRows;
142 numFolderColumns = p.numFolderColumns;
143 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800144 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800145 landscapeIconSize = p.landscapeIconSize;
146 iconTextSize = p.iconTextSize;
147 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700148 numAllAppsColumns = p.numAllAppsColumns;
149 allAppsIconSize = p.allAppsIconSize;
150 allAppsIconTextSize = p.allAppsIconTextSize;
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 /**
179 * Retrieve system defined or RRO overriden icon shape.
180 */
181 private static String getIconShapePath(Context context) {
182 if (CONFIG_ICON_MASK_RES_ID == 0) {
183 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
184 return "";
185 }
186 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
187 }
188
Sunny Goyaleff44f32019-01-09 17:29:49 -0800189 private String initGrid(Context context, String gridName) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700190 DefaultDisplay.Info displayInfo = DefaultDisplay.INSTANCE.get(context).getInfo();
Adam Cohen2e6da152015-05-06 11:42:25 -0700191
Winson Chung13c1c2c2019-09-06 11:46:19 -0700192 Point smallestSize = new Point(displayInfo.smallestSize);
193 Point largestSize = new Point(displayInfo.largestSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700194
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700195 // This guarantees that width < height
Winson Chung13c1c2c2019-09-06 11:46:19 -0700196 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
197 displayInfo.metrics);
198 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
199 displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700200
Winson Chung13c1c2c2019-09-06 11:46:19 -0700201 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700202 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700203 // same in any orientation.
204 int smallSide = Math.min(realSize.x, realSize.y);
205 int largeSide = Math.max(realSize.x, realSize.y);
206
Jon Miranda64d74812019-10-15 15:33:16 -0700207 // We want a list of all options as well as the list of filtered options. This allows us
208 // to have a consistent UI for areas that the grid size change should not affect
209 // ie. All Apps should be consistent between grid sizes.
210 ArrayList<DisplayOption> allOptions = new ArrayList<>();
211 ArrayList<DisplayOption> filteredOptions = new ArrayList<>();
212 getPredefinedDeviceProfiles(context, gridName, filteredOptions, allOptions);
213
214 if (allOptions.isEmpty() && filteredOptions.isEmpty()) {
215 throw new RuntimeException("No display option with canBeDefault=true");
216 }
217
218 // Sort the profiles based on the closeness to the device size
219 Comparator<DisplayOption> comparator = (a, b) -> Float.compare(dist(minWidthDps,
220 minHeightDps, a.minWidthDps, a.minHeightDps),
221 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps));
222
223 // Calculate the device profiles as if there is no grid override.
224 Collections.sort(allOptions, comparator);
225 DisplayOption interpolatedDisplayOption =
226 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
227 initGridOption(context, allOptions, interpolatedDisplayOption, displayInfo.metrics);
228
229 // Create IDP with no grid override values.
230 InvariantDeviceProfile originalIDP = new InvariantDeviceProfile(this);
231 originalIDP.landscapeProfile = new DeviceProfile(context, this, null, smallestSize,
232 largestSize, largeSide, smallSide, true /* isLandscape */,
233 false /* isMultiWindowMode */);
234 originalIDP.portraitProfile = new DeviceProfile(context, this, null, smallestSize,
235 largestSize, smallSide, largeSide, false /* isLandscape */,
236 false /* isMultiWindowMode */);
237
238 if (filteredOptions.isEmpty()) {
239 filteredOptions = allOptions;
240
241 landscapeProfile = originalIDP.landscapeProfile;
242 portraitProfile = originalIDP.portraitProfile;
243 } else {
244 Collections.sort(filteredOptions, comparator);
245 interpolatedDisplayOption =
246 invDistWeightedInterpolate(minWidthDps, minHeightDps, filteredOptions);
247
248 initGridOption(context, filteredOptions, interpolatedDisplayOption,
249 displayInfo.metrics);
250 numAllAppsColumns = originalIDP.numAllAppsColumns;
251
252 landscapeProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
253 largestSize, largeSide, smallSide, true /* isLandscape */,
254 false /* isMultiWindowMode */);
255 portraitProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
256 largestSize, smallSide, largeSide, false /* isLandscape */,
257 false /* isMultiWindowMode */);
258 }
259
260 GridOption closestProfile = filteredOptions.get(0).grid;
261 if (!closestProfile.name.equals(gridName)) {
262 Utilities.getPrefs(context).edit()
263 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
264 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700265
266 // We need to ensure that there is enough extra space in the wallpaper
267 // for the intended parallax effects
268 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
269 defaultWallpaperSize = new Point(
270 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
271 largeSide);
272 } else {
273 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
274 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700275
276 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
277 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
278
Sunny Goyaleff44f32019-01-09 17:29:49 -0800279 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700280 }
281
Jon Miranda64d74812019-10-15 15:33:16 -0700282 private void initGridOption(Context context, ArrayList<DisplayOption> options,
283 DisplayOption displayOption, DisplayMetrics metrics) {
284 GridOption closestProfile = options.get(0).grid;
285 numRows = closestProfile.numRows;
286 numColumns = closestProfile.numColumns;
287 numHotseatIcons = closestProfile.numHotseatIcons;
288 defaultLayoutId = closestProfile.defaultLayoutId;
289 demoModeLayoutId = closestProfile.demoModeLayoutId;
290 numFolderRows = closestProfile.numFolderRows;
291 numFolderColumns = closestProfile.numFolderColumns;
292 numAllAppsColumns = numColumns;
293
294 mExtraAttrs = closestProfile.extraAttrs;
295
296 iconSize = displayOption.iconSize;
297 iconShapePath = getIconShapePath(context);
298 landscapeIconSize = displayOption.landscapeIconSize;
299 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
300 iconTextSize = displayOption.iconTextSize;
301 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
302
303 // If the partner customization apk contains any grid overrides, apply them
304 // Supported overrides: numRows, numColumns, iconSize
305 applyPartnerDeviceProfileOverrides(context, metrics);
306 }
307
308
Sunny Goyal5bc18462019-01-07 15:13:39 -0800309 @Nullable
310 public TypedValue getAttrValue(int attr) {
311 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
312 }
313
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700314 public void addOnChangeListener(OnIDPChangeListener listener) {
315 mChangeListeners.add(listener);
316 }
317
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800318 public void removeOnChangeListener(OnIDPChangeListener listener) {
319 mChangeListeners.remove(listener);
320 }
321
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700322 private void killProcess(Context context) {
323 Log.e("ConfigMonitor", "restarting launcher");
324 android.os.Process.killProcess(android.os.Process.myPid());
325 }
326
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800327 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800328 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
329 // Good place to check if grid size changed in themepicker when launcher was dead.
330 if (savedIconMaskPath.isEmpty()) {
331 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
332 .apply();
333 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
334 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
335 .apply();
336 apply(context, CHANGE_FLAG_ICON_PARAMS);
337 }
338 }
339
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800340 public void setCurrentGrid(Context context, String gridName) {
341 Context appContext = context.getApplicationContext();
342 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700343 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800344 }
345
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700346 private void onConfigChanged(Context context) {
347 // Config changes, what shall we do?
348 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
349
350 // Re-init grid
Jon Miranda6f7e9702019-09-16 14:44:14 -0700351 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
352 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
353 : null;
354 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700355
356 int changeFlags = 0;
357 if (numRows != oldProfile.numRows ||
358 numColumns != oldProfile.numColumns ||
359 numFolderColumns != oldProfile.numFolderColumns ||
360 numFolderRows != oldProfile.numFolderRows ||
361 numHotseatIcons != oldProfile.numHotseatIcons) {
362 changeFlags |= CHANGE_FLAG_GRID;
363 }
364
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800365 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
366 !iconShapePath.equals(oldProfile.iconShapePath)) {
367 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700368 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800369 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700370 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800371 }
372
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800373 apply(context, changeFlags);
374 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700375
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800376 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700377 // Create a new config monitor
378 mConfigMonitor.unregister();
379 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
380
381 for (OnIDPChangeListener listener : mChangeListeners) {
382 listener.onIdpChanged(changeFlags, this);
383 }
384 }
385
Jon Miranda64d74812019-10-15 15:33:16 -0700386 /**
387 * @param gridName The current grid name.
388 * @param filteredOptionsOut List filled with all the filtered options based on gridName.
389 * @param allOptionsOut List filled with all the options that can be the default option.
390 */
391 static void getPredefinedDeviceProfiles(Context context, String gridName,
392 ArrayList<DisplayOption> filteredOptionsOut, ArrayList<DisplayOption> allOptionsOut) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800393 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700394 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
395 final int depth = parser.getDepth();
396 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700397 while (((type = parser.next()) != XmlPullParser.END_TAG ||
398 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800399 if ((type == XmlPullParser.START_TAG)
400 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800401
402 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
403 final int displayDepth = parser.getDepth();
404 while (((type = parser.next()) != XmlPullParser.END_TAG ||
405 parser.getDepth() > displayDepth)
406 && type != XmlPullParser.END_DOCUMENT) {
407 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
408 parser.getName())) {
409 profiles.add(new DisplayOption(
410 gridOption, context, Xml.asAttributeSet(parser)));
411 }
412 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700413 }
414 }
415 } catch (IOException|XmlPullParserException e) {
416 throw new RuntimeException(e);
417 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800418
Sunny Goyal415f1732018-11-29 10:33:47 -0800419 if (!TextUtils.isEmpty(gridName)) {
420 for (DisplayOption option : profiles) {
421 if (gridName.equals(option.grid.name)) {
Jon Miranda64d74812019-10-15 15:33:16 -0700422 filteredOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800423 }
424 }
425 }
Jon Miranda64d74812019-10-15 15:33:16 -0700426
427 for (DisplayOption option : profiles) {
428 if (option.canBeDefault) {
429 allOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800430 }
431 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700432 }
433
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700434 private int getLauncherIconDensity(int requiredSize) {
435 // Densities typically defined by an app.
436 int[] densityBuckets = new int[] {
437 DisplayMetrics.DENSITY_LOW,
438 DisplayMetrics.DENSITY_MEDIUM,
439 DisplayMetrics.DENSITY_TV,
440 DisplayMetrics.DENSITY_HIGH,
441 DisplayMetrics.DENSITY_XHIGH,
442 DisplayMetrics.DENSITY_XXHIGH,
443 DisplayMetrics.DENSITY_XXXHIGH
444 };
445
446 int density = DisplayMetrics.DENSITY_XXXHIGH;
447 for (int i = densityBuckets.length - 1; i >= 0; i--) {
448 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
449 / DisplayMetrics.DENSITY_DEFAULT;
450 if (expectedSize >= requiredSize) {
451 density = densityBuckets[i];
452 }
453 }
454
455 return density;
456 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700457
Adam Cohen2e6da152015-05-06 11:42:25 -0700458 /**
459 * Apply any Partner customization grid overrides.
460 *
461 * Currently we support: all apps row / column count.
462 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700463 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
464 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700465 if (p != null) {
466 p.applyInvariantDeviceProfileOverrides(this, dm);
467 }
468 }
469
Sunny Goyal415f1732018-11-29 10:33:47 -0800470 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700471 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700472 }
473
Sunny Goyal415f1732018-11-29 10:33:47 -0800474 @VisibleForTesting
475 static DisplayOption invDistWeightedInterpolate(float width, float height,
476 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700477 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700478
Sunny Goyal415f1732018-11-29 10:33:47 -0800479 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700480 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
481 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700482 }
483
Sunny Goyal415f1732018-11-29 10:33:47 -0800484 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700485 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800486 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700487 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
488 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800489 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700490 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800491 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700492 }
493
Sunny Goyal27835952017-01-13 12:15:53 -0800494 public DeviceProfile getDeviceProfile(Context context) {
495 return context.getResources().getConfiguration().orientation
496 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
497 }
498
Sunny Goyal415f1732018-11-29 10:33:47 -0800499 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700500 float d = dist(x0, y0, x1, y1);
501 if (Float.compare(d, 0f) == 0) {
502 return Float.POSITIVE_INFINITY;
503 }
504 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
505 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700506
507 /**
508 * As a ratio of screen height, the total distance we want the parallax effect to span
509 * horizontally
510 */
511 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
512 float aspectRatio = width / (float) height;
513
514 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
515 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
516 // We will use these two data points to extrapolate how much the wallpaper parallax effect
517 // to span (ie travel) at any aspect ratio:
518
519 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
520 final float ASPECT_RATIO_PORTRAIT = 10/16f;
521 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
522 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
523
524 // To find out the desired width at different aspect ratios, we use the following two
525 // formulas, where the coefficient on x is the aspect ratio (width/height):
526 // (16/10)x + y = 1.5
527 // (10/16)x + y = 1.2
528 // We solve for x and y and end up with a final formula:
529 final float x =
530 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
531 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
532 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
533 return x * aspectRatio + y;
534 }
535
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700536 public interface OnIDPChangeListener {
537
538 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
539 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800540
541
Sunny Goyaleff44f32019-01-09 17:29:49 -0800542 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800543
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800544 public static final String TAG_NAME = "grid-option";
545
Sunny Goyaleff44f32019-01-09 17:29:49 -0800546 public final String name;
547 public final int numRows;
548 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800549
550 private final int numFolderRows;
551 private final int numFolderColumns;
552
553 private final int numHotseatIcons;
554
555 private final int defaultLayoutId;
556 private final int demoModeLayoutId;
557
Sunny Goyal5bc18462019-01-07 15:13:39 -0800558 private final SparseArray<TypedValue> extraAttrs;
559
Sunny Goyaleff44f32019-01-09 17:29:49 -0800560 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800561 TypedArray a = context.obtainStyledAttributes(
562 attrs, R.styleable.GridDisplayOption);
563 name = a.getString(R.styleable.GridDisplayOption_name);
564 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
565 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
566
567 defaultLayoutId = a.getResourceId(
568 R.styleable.GridDisplayOption_defaultLayoutId, 0);
569 demoModeLayoutId = a.getResourceId(
570 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
571 numHotseatIcons = a.getInt(
572 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
573 numFolderRows = a.getInt(
574 R.styleable.GridDisplayOption_numFolderRows, numRows);
575 numFolderColumns = a.getInt(
576 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700577
Sunny Goyal415f1732018-11-29 10:33:47 -0800578 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800579
580 extraAttrs = Themes.createValueMap(context, attrs,
581 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800582 }
583 }
584
585 private static final class DisplayOption {
586 private final GridOption grid;
587
588 private final String name;
589 private final float minWidthDps;
590 private final float minHeightDps;
591 private final boolean canBeDefault;
592
593 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800594 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700595 private float landscapeIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800596
597 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
598 this.grid = grid;
599
600 TypedArray a = context.obtainStyledAttributes(
601 attrs, R.styleable.ProfileDisplayOption);
602
603 name = a.getString(R.styleable.ProfileDisplayOption_name);
604 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
605 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
606 canBeDefault = a.getBoolean(
607 R.styleable.ProfileDisplayOption_canBeDefault, false);
608
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700609 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800610 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
611 iconSize);
612 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700613
Sunny Goyal415f1732018-11-29 10:33:47 -0800614 a.recycle();
615 }
616
617 DisplayOption() {
618 grid = null;
619 name = null;
620 minWidthDps = 0;
621 minHeightDps = 0;
622 canBeDefault = false;
623 }
624
625 private DisplayOption multiply(float w) {
626 iconSize *= w;
627 landscapeIconSize *= w;
628 iconTextSize *= w;
629 return this;
630 }
631
632 private DisplayOption add(DisplayOption p) {
633 iconSize += p.iconSize;
634 landscapeIconSize += p.landscapeIconSize;
635 iconTextSize += p.iconTextSize;
636 return this;
637 }
638 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700639
640 private class OverlayMonitor extends BroadcastReceiver {
641
642 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
643
644 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700645 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700646 }
647
648 @Override
649 public void onReceive(Context context, Intent intent) {
650 onConfigChanged(context);
651 }
652 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700653}