blob: 7414a885868af83a336528f69f14b0c5d85bb537 [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;
Tracy Zhoube13d102020-01-12 01:07:59 -080050import com.android.launcher3.graphics.LauncherPreviewRenderer;
Sunny Goyald0e360a2018-06-29 14:40:18 -070051import com.android.launcher3.util.ConfigMonitor;
Winson Chung13c1c2c2019-09-06 11:46:19 -070052import com.android.launcher3.util.DefaultDisplay;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080053import com.android.launcher3.util.DefaultDisplay.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080054import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070055import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080056import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070057
Sunny Goyal819e1932016-07-07 16:43:58 -070058import org.xmlpull.v1.XmlPullParser;
59import org.xmlpull.v1.XmlPullParserException;
60
61import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070062import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080063import java.util.Collections;
Jon Miranda64d74812019-10-15 15:33:16 -070064import java.util.Comparator;
Adam Cohen2e6da152015-05-06 11:42:25 -070065
66public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070067
Hyunyoung Songc55a3502018-12-04 15:43:16 -080068 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070069 // We do not need any synchronization for this variable as its only written on UI thread.
70 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070071 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070072
Hyunyoung Songc55a3502018-12-04 15:43:16 -080073 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080074
Sunny Goyal53d7ee42015-05-22 12:25:45 -070075 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
76
Sunny Goyal87dc48b2018-10-12 11:42:33 -070077 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080078 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
79
80 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070081
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070082 // Constants that affects the interpolation curve between statically defined device profile
83 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080084 private static final float KNEARESTNEIGHBOR = 3;
85 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070086
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070087 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080088 private static final float WEIGHT_EFFICIENT = 100000f;
89
90 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
91 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070092
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070093 /**
94 * Number of icons per row and column in the workspace.
95 */
Adam Cohen2e6da152015-05-06 11:42:25 -070096 public int numRows;
97 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098
99 /**
100 * Number of icons per row and column in the folder.
101 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700102 public int numFolderRows;
103 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700104 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800105 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700106 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700107 public int iconBitmapSize;
108 public int fillResIconDpi;
109 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700110
Sunny Goyal5bc18462019-01-07 15:13:39 -0800111 private SparseArray<TypedValue> mExtraAttrs;
112
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700113 /**
114 * Number of icons inside the hotseat area.
115 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800116 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700117
Jon Miranda6f7e9702019-09-16 14:44:14 -0700118 /**
119 * Number of columns in the all apps list.
120 */
121 public int numAllAppsColumns;
122
Tracy Zhou7df93d22020-01-27 13:44:06 -0800123 public String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800124 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700125 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700126
cuijiaxingabda8d72017-03-20 09:51:36 -0700127 public DeviceProfile landscapeProfile;
128 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700129
Sunny Goyal6f866092016-03-17 17:04:15 -0700130 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700131 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700132
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700133 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
134 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700135 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700136
Sunny Goyalf633ef52018-03-13 09:57:05 -0700137 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700138 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700139
Sunny Goyalf633ef52018-03-13 09:57:05 -0700140 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800141 numRows = p.numRows;
142 numColumns = p.numColumns;
143 numFolderRows = p.numFolderRows;
144 numFolderColumns = p.numFolderColumns;
145 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800146 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800147 landscapeIconSize = p.landscapeIconSize;
148 iconTextSize = p.iconTextSize;
149 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700150 numAllAppsColumns = p.numAllAppsColumns;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800151 dbFile = p.dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800152 defaultLayoutId = p.defaultLayoutId;
153 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800154 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700155 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700156 }
157
Sunny Goyalbbf01842015-10-08 07:41:15 -0700158 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700159 private InvariantDeviceProfile(Context context) {
Tracy Zhoube13d102020-01-12 01:07:59 -0800160 if (context instanceof LauncherPreviewRenderer.PreviewContext) {
161 throw new IllegalArgumentException(
162 "PreviewContext is passed into this IDP constructor");
163 }
164
Tracy Zhou42255d22020-03-13 00:38:11 -0700165 String gridName = getCurrentGridName(context);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700166 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700167 mConfigMonitor = new ConfigMonitor(context,
168 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700169 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700170 }
171
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700172 /**
173 * This constructor should NOT have any monitors by design.
174 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800175 public InvariantDeviceProfile(Context context, String gridName) {
176 String newName = initGrid(context, gridName);
177 if (newName == null || !newName.equals(gridName)) {
178 throw new IllegalArgumentException("Unknown grid name");
179 }
180 }
181
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800182 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800183 * This constructor should NOT have any monitors by design.
184 */
185 public InvariantDeviceProfile(Context context, Display display) {
186 initGrid(context, null, new Info(display));
187 }
188
Tracy Zhou42255d22020-03-13 00:38:11 -0700189 public static String getCurrentGridName(Context context) {
190 return Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
191 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
192 : null;
193 }
194
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800195 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800196 * Retrieve system defined or RRO overriden icon shape.
197 */
198 private static String getIconShapePath(Context context) {
199 if (CONFIG_ICON_MASK_RES_ID == 0) {
200 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
201 return "";
202 }
203 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
204 }
205
Sunny Goyaleff44f32019-01-09 17:29:49 -0800206 private String initGrid(Context context, String gridName) {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800207 return initGrid(context, gridName, DefaultDisplay.INSTANCE.get(context).getInfo());
208 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700209
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800210 private String initGrid(Context context, String gridName, DefaultDisplay.Info displayInfo) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700211 Point smallestSize = new Point(displayInfo.smallestSize);
212 Point largestSize = new Point(displayInfo.largestSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700213
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700214 // This guarantees that width < height
Winson Chung13c1c2c2019-09-06 11:46:19 -0700215 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
216 displayInfo.metrics);
217 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
218 displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700219
Winson Chung13c1c2c2019-09-06 11:46:19 -0700220 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700221 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700222 // same in any orientation.
223 int smallSide = Math.min(realSize.x, realSize.y);
224 int largeSide = Math.max(realSize.x, realSize.y);
225
Jon Miranda64d74812019-10-15 15:33:16 -0700226 // We want a list of all options as well as the list of filtered options. This allows us
227 // to have a consistent UI for areas that the grid size change should not affect
228 // ie. All Apps should be consistent between grid sizes.
229 ArrayList<DisplayOption> allOptions = new ArrayList<>();
230 ArrayList<DisplayOption> filteredOptions = new ArrayList<>();
231 getPredefinedDeviceProfiles(context, gridName, filteredOptions, allOptions);
232
233 if (allOptions.isEmpty() && filteredOptions.isEmpty()) {
234 throw new RuntimeException("No display option with canBeDefault=true");
235 }
236
237 // Sort the profiles based on the closeness to the device size
238 Comparator<DisplayOption> comparator = (a, b) -> Float.compare(dist(minWidthDps,
239 minHeightDps, a.minWidthDps, a.minHeightDps),
240 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps));
241
242 // Calculate the device profiles as if there is no grid override.
243 Collections.sort(allOptions, comparator);
244 DisplayOption interpolatedDisplayOption =
245 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
246 initGridOption(context, allOptions, interpolatedDisplayOption, displayInfo.metrics);
247
248 // Create IDP with no grid override values.
249 InvariantDeviceProfile originalIDP = new InvariantDeviceProfile(this);
250 originalIDP.landscapeProfile = new DeviceProfile(context, this, null, smallestSize,
251 largestSize, largeSide, smallSide, true /* isLandscape */,
252 false /* isMultiWindowMode */);
253 originalIDP.portraitProfile = new DeviceProfile(context, this, null, smallestSize,
254 largestSize, smallSide, largeSide, false /* isLandscape */,
255 false /* isMultiWindowMode */);
256
257 if (filteredOptions.isEmpty()) {
258 filteredOptions = allOptions;
259
260 landscapeProfile = originalIDP.landscapeProfile;
261 portraitProfile = originalIDP.portraitProfile;
262 } else {
263 Collections.sort(filteredOptions, comparator);
264 interpolatedDisplayOption =
265 invDistWeightedInterpolate(minWidthDps, minHeightDps, filteredOptions);
266
267 initGridOption(context, filteredOptions, interpolatedDisplayOption,
268 displayInfo.metrics);
269 numAllAppsColumns = originalIDP.numAllAppsColumns;
270
271 landscapeProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
272 largestSize, largeSide, smallSide, true /* isLandscape */,
273 false /* isMultiWindowMode */);
274 portraitProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
275 largestSize, smallSide, largeSide, false /* isLandscape */,
276 false /* isMultiWindowMode */);
277 }
278
279 GridOption closestProfile = filteredOptions.get(0).grid;
280 if (!closestProfile.name.equals(gridName)) {
281 Utilities.getPrefs(context).edit()
282 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
283 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700284
285 // We need to ensure that there is enough extra space in the wallpaper
286 // for the intended parallax effects
287 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
288 defaultWallpaperSize = new Point(
289 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
290 largeSide);
291 } else {
292 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
293 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700294
295 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
296 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
297
Sunny Goyaleff44f32019-01-09 17:29:49 -0800298 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700299 }
300
Jon Miranda64d74812019-10-15 15:33:16 -0700301 private void initGridOption(Context context, ArrayList<DisplayOption> options,
302 DisplayOption displayOption, DisplayMetrics metrics) {
303 GridOption closestProfile = options.get(0).grid;
304 numRows = closestProfile.numRows;
305 numColumns = closestProfile.numColumns;
306 numHotseatIcons = closestProfile.numHotseatIcons;
Tracy Zhou7df93d22020-01-27 13:44:06 -0800307 dbFile = closestProfile.dbFile;
Jon Miranda64d74812019-10-15 15:33:16 -0700308 defaultLayoutId = closestProfile.defaultLayoutId;
309 demoModeLayoutId = closestProfile.demoModeLayoutId;
310 numFolderRows = closestProfile.numFolderRows;
311 numFolderColumns = closestProfile.numFolderColumns;
312 numAllAppsColumns = numColumns;
313
314 mExtraAttrs = closestProfile.extraAttrs;
315
316 iconSize = displayOption.iconSize;
317 iconShapePath = getIconShapePath(context);
318 landscapeIconSize = displayOption.landscapeIconSize;
319 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
320 iconTextSize = displayOption.iconTextSize;
321 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
322
323 // If the partner customization apk contains any grid overrides, apply them
324 // Supported overrides: numRows, numColumns, iconSize
325 applyPartnerDeviceProfileOverrides(context, metrics);
326 }
327
328
Sunny Goyal5bc18462019-01-07 15:13:39 -0800329 @Nullable
330 public TypedValue getAttrValue(int attr) {
331 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
332 }
333
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700334 public void addOnChangeListener(OnIDPChangeListener listener) {
335 mChangeListeners.add(listener);
336 }
337
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800338 public void removeOnChangeListener(OnIDPChangeListener listener) {
339 mChangeListeners.remove(listener);
340 }
341
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700342 private void killProcess(Context context) {
343 Log.e("ConfigMonitor", "restarting launcher");
344 android.os.Process.killProcess(android.os.Process.myPid());
345 }
346
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800347 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800348 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
349 // Good place to check if grid size changed in themepicker when launcher was dead.
350 if (savedIconMaskPath.isEmpty()) {
351 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
352 .apply();
353 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
354 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
355 .apply();
356 apply(context, CHANGE_FLAG_ICON_PARAMS);
357 }
358 }
359
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800360 public void setCurrentGrid(Context context, String gridName) {
361 Context appContext = context.getApplicationContext();
362 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700363 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800364 }
365
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700366 private void onConfigChanged(Context context) {
367 // Config changes, what shall we do?
368 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
369
370 // Re-init grid
Jon Miranda6f7e9702019-09-16 14:44:14 -0700371 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
372 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
373 : null;
374 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700375
376 int changeFlags = 0;
377 if (numRows != oldProfile.numRows ||
378 numColumns != oldProfile.numColumns ||
379 numFolderColumns != oldProfile.numFolderColumns ||
380 numFolderRows != oldProfile.numFolderRows ||
381 numHotseatIcons != oldProfile.numHotseatIcons) {
382 changeFlags |= CHANGE_FLAG_GRID;
383 }
384
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800385 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
386 !iconShapePath.equals(oldProfile.iconShapePath)) {
387 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700388 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800389 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700390 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800391 }
392
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800393 apply(context, changeFlags);
394 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700395
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800396 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700397 // Create a new config monitor
398 mConfigMonitor.unregister();
399 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
400
401 for (OnIDPChangeListener listener : mChangeListeners) {
402 listener.onIdpChanged(changeFlags, this);
403 }
404 }
405
Jon Miranda64d74812019-10-15 15:33:16 -0700406 /**
407 * @param gridName The current grid name.
408 * @param filteredOptionsOut List filled with all the filtered options based on gridName.
409 * @param allOptionsOut List filled with all the options that can be the default option.
410 */
411 static void getPredefinedDeviceProfiles(Context context, String gridName,
412 ArrayList<DisplayOption> filteredOptionsOut, ArrayList<DisplayOption> allOptionsOut) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800413 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700414 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
415 final int depth = parser.getDepth();
416 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700417 while (((type = parser.next()) != XmlPullParser.END_TAG ||
418 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800419 if ((type == XmlPullParser.START_TAG)
420 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800421
422 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
423 final int displayDepth = parser.getDepth();
424 while (((type = parser.next()) != XmlPullParser.END_TAG ||
425 parser.getDepth() > displayDepth)
426 && type != XmlPullParser.END_DOCUMENT) {
427 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
428 parser.getName())) {
429 profiles.add(new DisplayOption(
430 gridOption, context, Xml.asAttributeSet(parser)));
431 }
432 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700433 }
434 }
435 } catch (IOException|XmlPullParserException e) {
436 throw new RuntimeException(e);
437 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800438
Sunny Goyal415f1732018-11-29 10:33:47 -0800439 if (!TextUtils.isEmpty(gridName)) {
440 for (DisplayOption option : profiles) {
441 if (gridName.equals(option.grid.name)) {
Jon Miranda64d74812019-10-15 15:33:16 -0700442 filteredOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800443 }
444 }
445 }
Jon Miranda64d74812019-10-15 15:33:16 -0700446
447 for (DisplayOption option : profiles) {
448 if (option.canBeDefault) {
449 allOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800450 }
451 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700452 }
453
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700454 private int getLauncherIconDensity(int requiredSize) {
455 // Densities typically defined by an app.
456 int[] densityBuckets = new int[] {
457 DisplayMetrics.DENSITY_LOW,
458 DisplayMetrics.DENSITY_MEDIUM,
459 DisplayMetrics.DENSITY_TV,
460 DisplayMetrics.DENSITY_HIGH,
461 DisplayMetrics.DENSITY_XHIGH,
462 DisplayMetrics.DENSITY_XXHIGH,
463 DisplayMetrics.DENSITY_XXXHIGH
464 };
465
466 int density = DisplayMetrics.DENSITY_XXXHIGH;
467 for (int i = densityBuckets.length - 1; i >= 0; i--) {
468 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
469 / DisplayMetrics.DENSITY_DEFAULT;
470 if (expectedSize >= requiredSize) {
471 density = densityBuckets[i];
472 }
473 }
474
475 return density;
476 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700477
Adam Cohen2e6da152015-05-06 11:42:25 -0700478 /**
479 * Apply any Partner customization grid overrides.
480 *
481 * Currently we support: all apps row / column count.
482 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700483 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
484 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700485 if (p != null) {
486 p.applyInvariantDeviceProfileOverrides(this, dm);
487 }
488 }
489
Sunny Goyal415f1732018-11-29 10:33:47 -0800490 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700491 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700492 }
493
Sunny Goyal415f1732018-11-29 10:33:47 -0800494 @VisibleForTesting
495 static DisplayOption invDistWeightedInterpolate(float width, float height,
496 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700497 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700498
Sunny Goyal415f1732018-11-29 10:33:47 -0800499 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700500 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
501 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700502 }
503
Sunny Goyal415f1732018-11-29 10:33:47 -0800504 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700505 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800506 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700507 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
508 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800509 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700510 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800511 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700512 }
513
Sunny Goyal27835952017-01-13 12:15:53 -0800514 public DeviceProfile getDeviceProfile(Context context) {
515 return context.getResources().getConfiguration().orientation
516 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
517 }
518
Sunny Goyal415f1732018-11-29 10:33:47 -0800519 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700520 float d = dist(x0, y0, x1, y1);
521 if (Float.compare(d, 0f) == 0) {
522 return Float.POSITIVE_INFINITY;
523 }
524 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
525 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700526
527 /**
528 * As a ratio of screen height, the total distance we want the parallax effect to span
529 * horizontally
530 */
531 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
532 float aspectRatio = width / (float) height;
533
534 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
535 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
536 // We will use these two data points to extrapolate how much the wallpaper parallax effect
537 // to span (ie travel) at any aspect ratio:
538
539 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
540 final float ASPECT_RATIO_PORTRAIT = 10/16f;
541 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
542 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
543
544 // To find out the desired width at different aspect ratios, we use the following two
545 // formulas, where the coefficient on x is the aspect ratio (width/height):
546 // (16/10)x + y = 1.5
547 // (10/16)x + y = 1.2
548 // We solve for x and y and end up with a final formula:
549 final float x =
550 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
551 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
552 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
553 return x * aspectRatio + y;
554 }
555
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700556 public interface OnIDPChangeListener {
557
558 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
559 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800560
561
Sunny Goyaleff44f32019-01-09 17:29:49 -0800562 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800563
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800564 public static final String TAG_NAME = "grid-option";
565
Sunny Goyaleff44f32019-01-09 17:29:49 -0800566 public final String name;
567 public final int numRows;
568 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800569
570 private final int numFolderRows;
571 private final int numFolderColumns;
572
573 private final int numHotseatIcons;
574
Tracy Zhou7df93d22020-01-27 13:44:06 -0800575 private final String dbFile;
Sunny Goyal415f1732018-11-29 10:33:47 -0800576 private final int defaultLayoutId;
577 private final int demoModeLayoutId;
578
Sunny Goyal5bc18462019-01-07 15:13:39 -0800579 private final SparseArray<TypedValue> extraAttrs;
580
Sunny Goyaleff44f32019-01-09 17:29:49 -0800581 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800582 TypedArray a = context.obtainStyledAttributes(
583 attrs, R.styleable.GridDisplayOption);
584 name = a.getString(R.styleable.GridDisplayOption_name);
585 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
586 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
587
Tracy Zhou7df93d22020-01-27 13:44:06 -0800588 dbFile = a.getString(R.styleable.GridDisplayOption_dbFile);
Sunny Goyal415f1732018-11-29 10:33:47 -0800589 defaultLayoutId = a.getResourceId(
590 R.styleable.GridDisplayOption_defaultLayoutId, 0);
591 demoModeLayoutId = a.getResourceId(
592 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
593 numHotseatIcons = a.getInt(
594 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
595 numFolderRows = a.getInt(
596 R.styleable.GridDisplayOption_numFolderRows, numRows);
597 numFolderColumns = a.getInt(
598 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700599
Sunny Goyal415f1732018-11-29 10:33:47 -0800600 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800601
602 extraAttrs = Themes.createValueMap(context, attrs,
603 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800604 }
605 }
606
607 private static final class DisplayOption {
608 private final GridOption grid;
609
610 private final String name;
611 private final float minWidthDps;
612 private final float minHeightDps;
613 private final boolean canBeDefault;
614
615 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800616 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700617 private float landscapeIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800618
619 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
620 this.grid = grid;
621
622 TypedArray a = context.obtainStyledAttributes(
623 attrs, R.styleable.ProfileDisplayOption);
624
625 name = a.getString(R.styleable.ProfileDisplayOption_name);
626 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
627 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
628 canBeDefault = a.getBoolean(
629 R.styleable.ProfileDisplayOption_canBeDefault, false);
630
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700631 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800632 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
633 iconSize);
634 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700635
Sunny Goyal415f1732018-11-29 10:33:47 -0800636 a.recycle();
637 }
638
639 DisplayOption() {
640 grid = null;
641 name = null;
642 minWidthDps = 0;
643 minHeightDps = 0;
644 canBeDefault = false;
645 }
646
647 private DisplayOption multiply(float w) {
648 iconSize *= w;
649 landscapeIconSize *= w;
650 iconTextSize *= w;
651 return this;
652 }
653
654 private DisplayOption add(DisplayOption p) {
655 iconSize += p.iconSize;
656 landscapeIconSize += p.landscapeIconSize;
657 iconTextSize += p.iconTextSize;
658 return this;
659 }
660 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700661
662 private class OverlayMonitor extends BroadcastReceiver {
663
664 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
665
666 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700667 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700668 }
669
670 @Override
671 public void onReceive(Context context, Intent intent) {
672 onConfigChanged(context);
673 }
674 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700675}