blob: 9d87152a87db46825bee1156470f5c6ae9dcd777 [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;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700107
Sunny Goyal5bc18462019-01-07 15:13:39 -0800108 private SparseArray<TypedValue> mExtraAttrs;
109
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700110 /**
111 * Number of icons inside the hotseat area.
112 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800113 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700114
Jon Miranda6f7e9702019-09-16 14:44:14 -0700115 /**
116 * Number of columns in the all apps list.
117 */
118 public int numAllAppsColumns;
119
Sunny Goyal415f1732018-11-29 10:33:47 -0800120 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700121 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700122
cuijiaxingabda8d72017-03-20 09:51:36 -0700123 public DeviceProfile landscapeProfile;
124 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700125
Sunny Goyal6f866092016-03-17 17:04:15 -0700126 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700127 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700128
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700129 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
130 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700131 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700132
Sunny Goyalf633ef52018-03-13 09:57:05 -0700133 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700134 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700135
Sunny Goyalf633ef52018-03-13 09:57:05 -0700136 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800137 numRows = p.numRows;
138 numColumns = p.numColumns;
139 numFolderRows = p.numFolderRows;
140 numFolderColumns = p.numFolderColumns;
141 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800142 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800143 landscapeIconSize = p.landscapeIconSize;
144 iconTextSize = p.iconTextSize;
145 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700146 numAllAppsColumns = p.numAllAppsColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800147 defaultLayoutId = p.defaultLayoutId;
148 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800149 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700150 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700151 }
152
Sunny Goyalbbf01842015-10-08 07:41:15 -0700153 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700154 private InvariantDeviceProfile(Context context) {
Jon Miranda6f7e9702019-09-16 14:44:14 -0700155 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
156 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
157 : null;
158 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700159 mConfigMonitor = new ConfigMonitor(context,
160 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700161 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700162 }
163
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700164 /**
165 * This constructor should NOT have any monitors by design.
166 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800167 public InvariantDeviceProfile(Context context, String gridName) {
168 String newName = initGrid(context, gridName);
169 if (newName == null || !newName.equals(gridName)) {
170 throw new IllegalArgumentException("Unknown grid name");
171 }
172 }
173
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800174 /**
175 * Retrieve system defined or RRO overriden icon shape.
176 */
177 private static String getIconShapePath(Context context) {
178 if (CONFIG_ICON_MASK_RES_ID == 0) {
179 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
180 return "";
181 }
182 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
183 }
184
Sunny Goyaleff44f32019-01-09 17:29:49 -0800185 private String initGrid(Context context, String gridName) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700186 DefaultDisplay.Info displayInfo = DefaultDisplay.INSTANCE.get(context).getInfo();
Adam Cohen2e6da152015-05-06 11:42:25 -0700187
Winson Chung13c1c2c2019-09-06 11:46:19 -0700188 Point smallestSize = new Point(displayInfo.smallestSize);
189 Point largestSize = new Point(displayInfo.largestSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700190
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700191 // This guarantees that width < height
Winson Chung13c1c2c2019-09-06 11:46:19 -0700192 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
193 displayInfo.metrics);
194 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
195 displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700196
Winson Chung13c1c2c2019-09-06 11:46:19 -0700197 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700198 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700199 // same in any orientation.
200 int smallSide = Math.min(realSize.x, realSize.y);
201 int largeSide = Math.max(realSize.x, realSize.y);
202
Jon Miranda64d74812019-10-15 15:33:16 -0700203 // We want a list of all options as well as the list of filtered options. This allows us
204 // to have a consistent UI for areas that the grid size change should not affect
205 // ie. All Apps should be consistent between grid sizes.
206 ArrayList<DisplayOption> allOptions = new ArrayList<>();
207 ArrayList<DisplayOption> filteredOptions = new ArrayList<>();
208 getPredefinedDeviceProfiles(context, gridName, filteredOptions, allOptions);
209
210 if (allOptions.isEmpty() && filteredOptions.isEmpty()) {
211 throw new RuntimeException("No display option with canBeDefault=true");
212 }
213
214 // Sort the profiles based on the closeness to the device size
215 Comparator<DisplayOption> comparator = (a, b) -> Float.compare(dist(minWidthDps,
216 minHeightDps, a.minWidthDps, a.minHeightDps),
217 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps));
218
219 // Calculate the device profiles as if there is no grid override.
220 Collections.sort(allOptions, comparator);
221 DisplayOption interpolatedDisplayOption =
222 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
223 initGridOption(context, allOptions, interpolatedDisplayOption, displayInfo.metrics);
224
225 // Create IDP with no grid override values.
226 InvariantDeviceProfile originalIDP = new InvariantDeviceProfile(this);
227 originalIDP.landscapeProfile = new DeviceProfile(context, this, null, smallestSize,
228 largestSize, largeSide, smallSide, true /* isLandscape */,
229 false /* isMultiWindowMode */);
230 originalIDP.portraitProfile = new DeviceProfile(context, this, null, smallestSize,
231 largestSize, smallSide, largeSide, false /* isLandscape */,
232 false /* isMultiWindowMode */);
233
234 if (filteredOptions.isEmpty()) {
235 filteredOptions = allOptions;
236
237 landscapeProfile = originalIDP.landscapeProfile;
238 portraitProfile = originalIDP.portraitProfile;
239 } else {
240 Collections.sort(filteredOptions, comparator);
241 interpolatedDisplayOption =
242 invDistWeightedInterpolate(minWidthDps, minHeightDps, filteredOptions);
243
244 initGridOption(context, filteredOptions, interpolatedDisplayOption,
245 displayInfo.metrics);
246 numAllAppsColumns = originalIDP.numAllAppsColumns;
247
248 landscapeProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
249 largestSize, largeSide, smallSide, true /* isLandscape */,
250 false /* isMultiWindowMode */);
251 portraitProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
252 largestSize, smallSide, largeSide, false /* isLandscape */,
253 false /* isMultiWindowMode */);
254 }
255
256 GridOption closestProfile = filteredOptions.get(0).grid;
257 if (!closestProfile.name.equals(gridName)) {
258 Utilities.getPrefs(context).edit()
259 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
260 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700261
262 // We need to ensure that there is enough extra space in the wallpaper
263 // for the intended parallax effects
264 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
265 defaultWallpaperSize = new Point(
266 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
267 largeSide);
268 } else {
269 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
270 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700271
272 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
273 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
274
Sunny Goyaleff44f32019-01-09 17:29:49 -0800275 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700276 }
277
Jon Miranda64d74812019-10-15 15:33:16 -0700278 private void initGridOption(Context context, ArrayList<DisplayOption> options,
279 DisplayOption displayOption, DisplayMetrics metrics) {
280 GridOption closestProfile = options.get(0).grid;
281 numRows = closestProfile.numRows;
282 numColumns = closestProfile.numColumns;
283 numHotseatIcons = closestProfile.numHotseatIcons;
284 defaultLayoutId = closestProfile.defaultLayoutId;
285 demoModeLayoutId = closestProfile.demoModeLayoutId;
286 numFolderRows = closestProfile.numFolderRows;
287 numFolderColumns = closestProfile.numFolderColumns;
288 numAllAppsColumns = numColumns;
289
290 mExtraAttrs = closestProfile.extraAttrs;
291
292 iconSize = displayOption.iconSize;
293 iconShapePath = getIconShapePath(context);
294 landscapeIconSize = displayOption.landscapeIconSize;
295 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
296 iconTextSize = displayOption.iconTextSize;
297 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
298
299 // If the partner customization apk contains any grid overrides, apply them
300 // Supported overrides: numRows, numColumns, iconSize
301 applyPartnerDeviceProfileOverrides(context, metrics);
302 }
303
304
Sunny Goyal5bc18462019-01-07 15:13:39 -0800305 @Nullable
306 public TypedValue getAttrValue(int attr) {
307 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
308 }
309
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700310 public void addOnChangeListener(OnIDPChangeListener listener) {
311 mChangeListeners.add(listener);
312 }
313
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800314 public void removeOnChangeListener(OnIDPChangeListener listener) {
315 mChangeListeners.remove(listener);
316 }
317
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700318 private void killProcess(Context context) {
319 Log.e("ConfigMonitor", "restarting launcher");
320 android.os.Process.killProcess(android.os.Process.myPid());
321 }
322
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800323 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800324 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
325 // Good place to check if grid size changed in themepicker when launcher was dead.
326 if (savedIconMaskPath.isEmpty()) {
327 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
328 .apply();
329 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
330 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
331 .apply();
332 apply(context, CHANGE_FLAG_ICON_PARAMS);
333 }
334 }
335
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800336 public void setCurrentGrid(Context context, String gridName) {
337 Context appContext = context.getApplicationContext();
338 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700339 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800340 }
341
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700342 private void onConfigChanged(Context context) {
343 // Config changes, what shall we do?
344 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
345
346 // Re-init grid
Jon Miranda6f7e9702019-09-16 14:44:14 -0700347 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
348 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
349 : null;
350 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700351
352 int changeFlags = 0;
353 if (numRows != oldProfile.numRows ||
354 numColumns != oldProfile.numColumns ||
355 numFolderColumns != oldProfile.numFolderColumns ||
356 numFolderRows != oldProfile.numFolderRows ||
357 numHotseatIcons != oldProfile.numHotseatIcons) {
358 changeFlags |= CHANGE_FLAG_GRID;
359 }
360
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800361 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
362 !iconShapePath.equals(oldProfile.iconShapePath)) {
363 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700364 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800365 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700366 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800367 }
368
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800369 apply(context, changeFlags);
370 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700371
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800372 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700373 // Create a new config monitor
374 mConfigMonitor.unregister();
375 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
376
377 for (OnIDPChangeListener listener : mChangeListeners) {
378 listener.onIdpChanged(changeFlags, this);
379 }
380 }
381
Jon Miranda64d74812019-10-15 15:33:16 -0700382 /**
383 * @param gridName The current grid name.
384 * @param filteredOptionsOut List filled with all the filtered options based on gridName.
385 * @param allOptionsOut List filled with all the options that can be the default option.
386 */
387 static void getPredefinedDeviceProfiles(Context context, String gridName,
388 ArrayList<DisplayOption> filteredOptionsOut, ArrayList<DisplayOption> allOptionsOut) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800389 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700390 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
391 final int depth = parser.getDepth();
392 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700393 while (((type = parser.next()) != XmlPullParser.END_TAG ||
394 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800395 if ((type == XmlPullParser.START_TAG)
396 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800397
398 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
399 final int displayDepth = parser.getDepth();
400 while (((type = parser.next()) != XmlPullParser.END_TAG ||
401 parser.getDepth() > displayDepth)
402 && type != XmlPullParser.END_DOCUMENT) {
403 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
404 parser.getName())) {
405 profiles.add(new DisplayOption(
406 gridOption, context, Xml.asAttributeSet(parser)));
407 }
408 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700409 }
410 }
411 } catch (IOException|XmlPullParserException e) {
412 throw new RuntimeException(e);
413 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800414
Sunny Goyal415f1732018-11-29 10:33:47 -0800415 if (!TextUtils.isEmpty(gridName)) {
416 for (DisplayOption option : profiles) {
417 if (gridName.equals(option.grid.name)) {
Jon Miranda64d74812019-10-15 15:33:16 -0700418 filteredOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800419 }
420 }
421 }
Jon Miranda64d74812019-10-15 15:33:16 -0700422
423 for (DisplayOption option : profiles) {
424 if (option.canBeDefault) {
425 allOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800426 }
427 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700428 }
429
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700430 private int getLauncherIconDensity(int requiredSize) {
431 // Densities typically defined by an app.
432 int[] densityBuckets = new int[] {
433 DisplayMetrics.DENSITY_LOW,
434 DisplayMetrics.DENSITY_MEDIUM,
435 DisplayMetrics.DENSITY_TV,
436 DisplayMetrics.DENSITY_HIGH,
437 DisplayMetrics.DENSITY_XHIGH,
438 DisplayMetrics.DENSITY_XXHIGH,
439 DisplayMetrics.DENSITY_XXXHIGH
440 };
441
442 int density = DisplayMetrics.DENSITY_XXXHIGH;
443 for (int i = densityBuckets.length - 1; i >= 0; i--) {
444 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
445 / DisplayMetrics.DENSITY_DEFAULT;
446 if (expectedSize >= requiredSize) {
447 density = densityBuckets[i];
448 }
449 }
450
451 return density;
452 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700453
Adam Cohen2e6da152015-05-06 11:42:25 -0700454 /**
455 * Apply any Partner customization grid overrides.
456 *
457 * Currently we support: all apps row / column count.
458 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700459 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
460 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700461 if (p != null) {
462 p.applyInvariantDeviceProfileOverrides(this, dm);
463 }
464 }
465
Sunny Goyal415f1732018-11-29 10:33:47 -0800466 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700467 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700468 }
469
Sunny Goyal415f1732018-11-29 10:33:47 -0800470 @VisibleForTesting
471 static DisplayOption invDistWeightedInterpolate(float width, float height,
472 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700473 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700474
Sunny Goyal415f1732018-11-29 10:33:47 -0800475 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700476 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
477 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700478 }
479
Sunny Goyal415f1732018-11-29 10:33:47 -0800480 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700481 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800482 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700483 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
484 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800485 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700486 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800487 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700488 }
489
Sunny Goyal27835952017-01-13 12:15:53 -0800490 public DeviceProfile getDeviceProfile(Context context) {
491 return context.getResources().getConfiguration().orientation
492 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
493 }
494
Sunny Goyal415f1732018-11-29 10:33:47 -0800495 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700496 float d = dist(x0, y0, x1, y1);
497 if (Float.compare(d, 0f) == 0) {
498 return Float.POSITIVE_INFINITY;
499 }
500 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
501 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700502
503 /**
504 * As a ratio of screen height, the total distance we want the parallax effect to span
505 * horizontally
506 */
507 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
508 float aspectRatio = width / (float) height;
509
510 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
511 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
512 // We will use these two data points to extrapolate how much the wallpaper parallax effect
513 // to span (ie travel) at any aspect ratio:
514
515 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
516 final float ASPECT_RATIO_PORTRAIT = 10/16f;
517 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
518 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
519
520 // To find out the desired width at different aspect ratios, we use the following two
521 // formulas, where the coefficient on x is the aspect ratio (width/height):
522 // (16/10)x + y = 1.5
523 // (10/16)x + y = 1.2
524 // We solve for x and y and end up with a final formula:
525 final float x =
526 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
527 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
528 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
529 return x * aspectRatio + y;
530 }
531
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700532 public interface OnIDPChangeListener {
533
534 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
535 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800536
537
Sunny Goyaleff44f32019-01-09 17:29:49 -0800538 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800539
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800540 public static final String TAG_NAME = "grid-option";
541
Sunny Goyaleff44f32019-01-09 17:29:49 -0800542 public final String name;
543 public final int numRows;
544 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800545
546 private final int numFolderRows;
547 private final int numFolderColumns;
548
549 private final int numHotseatIcons;
550
551 private final int defaultLayoutId;
552 private final int demoModeLayoutId;
553
Sunny Goyal5bc18462019-01-07 15:13:39 -0800554 private final SparseArray<TypedValue> extraAttrs;
555
Sunny Goyaleff44f32019-01-09 17:29:49 -0800556 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800557 TypedArray a = context.obtainStyledAttributes(
558 attrs, R.styleable.GridDisplayOption);
559 name = a.getString(R.styleable.GridDisplayOption_name);
560 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
561 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
562
563 defaultLayoutId = a.getResourceId(
564 R.styleable.GridDisplayOption_defaultLayoutId, 0);
565 demoModeLayoutId = a.getResourceId(
566 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
567 numHotseatIcons = a.getInt(
568 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
569 numFolderRows = a.getInt(
570 R.styleable.GridDisplayOption_numFolderRows, numRows);
571 numFolderColumns = a.getInt(
572 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700573
Sunny Goyal415f1732018-11-29 10:33:47 -0800574 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800575
576 extraAttrs = Themes.createValueMap(context, attrs,
577 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800578 }
579 }
580
581 private static final class DisplayOption {
582 private final GridOption grid;
583
584 private final String name;
585 private final float minWidthDps;
586 private final float minHeightDps;
587 private final boolean canBeDefault;
588
589 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800590 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700591 private float landscapeIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800592
593 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
594 this.grid = grid;
595
596 TypedArray a = context.obtainStyledAttributes(
597 attrs, R.styleable.ProfileDisplayOption);
598
599 name = a.getString(R.styleable.ProfileDisplayOption_name);
600 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
601 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
602 canBeDefault = a.getBoolean(
603 R.styleable.ProfileDisplayOption_canBeDefault, false);
604
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700605 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800606 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
607 iconSize);
608 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700609
Sunny Goyal415f1732018-11-29 10:33:47 -0800610 a.recycle();
611 }
612
613 DisplayOption() {
614 grid = null;
615 name = null;
616 minWidthDps = 0;
617 minHeightDps = 0;
618 canBeDefault = false;
619 }
620
621 private DisplayOption multiply(float w) {
622 iconSize *= w;
623 landscapeIconSize *= w;
624 iconTextSize *= w;
625 return this;
626 }
627
628 private DisplayOption add(DisplayOption p) {
629 iconSize += p.iconSize;
630 landscapeIconSize += p.landscapeIconSize;
631 iconTextSize += p.iconTextSize;
632 return this;
633 }
634 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700635
636 private class OverlayMonitor extends BroadcastReceiver {
637
638 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
639
640 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700641 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700642 }
643
644 @Override
645 public void onReceive(Context context, Intent intent) {
646 onConfigChanged(context);
647 }
648 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700649}