blob: 310a9e92ff485ed020c47558c36c536bc6cd81e5 [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;
Adam Cohen2e6da152015-05-06 11:42:25 -070061
62public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070063
Hyunyoung Songc55a3502018-12-04 15:43:16 -080064 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070065 // We do not need any synchronization for this variable as its only written on UI thread.
66 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070067 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070068
Hyunyoung Songc55a3502018-12-04 15:43:16 -080069 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080070
Sunny Goyal53d7ee42015-05-22 12:25:45 -070071 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
72
Sunny Goyal87dc48b2018-10-12 11:42:33 -070073 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080074 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
75
76 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070077
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070078 // Constants that affects the interpolation curve between statically defined device profile
79 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080080 private static final float KNEARESTNEIGHBOR = 3;
81 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070082
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070083 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080084 private static final float WEIGHT_EFFICIENT = 100000f;
85
86 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
87 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070088
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070089 /**
90 * Number of icons per row and column in the workspace.
91 */
Adam Cohen2e6da152015-05-06 11:42:25 -070092 public int numRows;
93 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070094
95 /**
96 * Number of icons per row and column in the folder.
97 */
Adam Cohen2e6da152015-05-06 11:42:25 -070098 public int numFolderRows;
99 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700100 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800101 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700102 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700103 public int iconBitmapSize;
104 public int fillResIconDpi;
105 public float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700106 public float allAppsIconSize;
107 public float allAppsIconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700108
Sunny Goyal5bc18462019-01-07 15:13:39 -0800109 private SparseArray<TypedValue> mExtraAttrs;
110
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700111 /**
112 * Number of icons inside the hotseat area.
113 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800114 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700115
Jon Miranda6f7e9702019-09-16 14:44:14 -0700116 /**
117 * Number of columns in the all apps list.
118 */
119 public int numAllAppsColumns;
120
Sunny Goyal415f1732018-11-29 10:33:47 -0800121 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700122 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700123
cuijiaxingabda8d72017-03-20 09:51:36 -0700124 public DeviceProfile landscapeProfile;
125 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700126
Sunny Goyal6f866092016-03-17 17:04:15 -0700127 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700128 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700129
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700130 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
131 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700132 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700133
Sunny Goyalf633ef52018-03-13 09:57:05 -0700134 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700135 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700136
Sunny Goyalf633ef52018-03-13 09:57:05 -0700137 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800138 numRows = p.numRows;
139 numColumns = p.numColumns;
140 numFolderRows = p.numFolderRows;
141 numFolderColumns = p.numFolderColumns;
142 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800143 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800144 landscapeIconSize = p.landscapeIconSize;
145 iconTextSize = p.iconTextSize;
146 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700147 numAllAppsColumns = p.numAllAppsColumns;
148 allAppsIconSize = p.allAppsIconSize;
149 allAppsIconTextSize = p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800150 defaultLayoutId = p.defaultLayoutId;
151 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800152 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700153 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700154 }
155
Sunny Goyalbbf01842015-10-08 07:41:15 -0700156 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700157 private InvariantDeviceProfile(Context context) {
Jon Miranda6f7e9702019-09-16 14:44:14 -0700158 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
159 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
160 : null;
161 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700162 mConfigMonitor = new ConfigMonitor(context,
163 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700164 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700165 }
166
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700167 /**
168 * This constructor should NOT have any monitors by design.
169 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800170 public InvariantDeviceProfile(Context context, String gridName) {
171 String newName = initGrid(context, gridName);
172 if (newName == null || !newName.equals(gridName)) {
173 throw new IllegalArgumentException("Unknown grid name");
174 }
175 }
176
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800177 /**
178 * Retrieve system defined or RRO overriden icon shape.
179 */
180 private static String getIconShapePath(Context context) {
181 if (CONFIG_ICON_MASK_RES_ID == 0) {
182 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
183 return "";
184 }
185 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
186 }
187
Sunny Goyaleff44f32019-01-09 17:29:49 -0800188 private String initGrid(Context context, String gridName) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700189 DefaultDisplay.Info displayInfo = DefaultDisplay.INSTANCE.get(context).getInfo();
Adam Cohen2e6da152015-05-06 11:42:25 -0700190
Winson Chung13c1c2c2019-09-06 11:46:19 -0700191 Point smallestSize = new Point(displayInfo.smallestSize);
192 Point largestSize = new Point(displayInfo.largestSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700193
Sunny Goyal415f1732018-11-29 10:33:47 -0800194 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
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 Goyal415f1732018-11-29 10:33:47 -0800200 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800201 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800202 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
203 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
204 DisplayOption interpolatedDisplayOption =
205 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700206
Sunny Goyal415f1732018-11-29 10:33:47 -0800207 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700208 numRows = closestProfile.numRows;
209 numColumns = closestProfile.numColumns;
210 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700211 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700212 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700213 numFolderRows = closestProfile.numFolderRows;
214 numFolderColumns = closestProfile.numFolderColumns;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700215 numAllAppsColumns = closestProfile.numAllAppsColumns;
216
Sunny Goyal5bc18462019-01-07 15:13:39 -0800217 mExtraAttrs = closestProfile.extraAttrs;
218
Sunny Goyal415f1732018-11-29 10:33:47 -0800219 if (!closestProfile.name.equals(gridName)) {
220 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800221 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800222 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700223
Sunny Goyal415f1732018-11-29 10:33:47 -0800224 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800225 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800226 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Winson Chung13c1c2c2019-09-06 11:46:19 -0700227 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics);
Sunny Goyal415f1732018-11-29 10:33:47 -0800228 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700229 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700230
Jon Miranda6f7e9702019-09-16 14:44:14 -0700231 if (Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)) {
232 allAppsIconSize = interpolatedDisplayOption.allAppsIconSize;
233 allAppsIconTextSize = interpolatedDisplayOption.allAppsIconTextSize;
234 } else {
235 allAppsIconSize = iconSize;
236 allAppsIconTextSize = iconTextSize;
237 }
238
Adam Cohen2e6da152015-05-06 11:42:25 -0700239 // If the partner customization apk contains any grid overrides, apply them
240 // Supported overrides: numRows, numColumns, iconSize
Winson Chung13c1c2c2019-09-06 11:46:19 -0700241 applyPartnerDeviceProfileOverrides(context, displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700242
Winson Chung13c1c2c2019-09-06 11:46:19 -0700243 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700244 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700245 // same in any orientation.
246 int smallSide = Math.min(realSize.x, realSize.y);
247 int largeSide = Math.max(realSize.x, realSize.y);
248
249 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800250 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700251 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800252 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700253
254 // We need to ensure that there is enough extra space in the wallpaper
255 // for the intended parallax effects
256 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
257 defaultWallpaperSize = new Point(
258 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
259 largeSide);
260 } else {
261 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
262 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700263
264 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
265 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
266
Sunny Goyaleff44f32019-01-09 17:29:49 -0800267 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700268 }
269
Sunny Goyal5bc18462019-01-07 15:13:39 -0800270 @Nullable
271 public TypedValue getAttrValue(int attr) {
272 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
273 }
274
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700275 public void addOnChangeListener(OnIDPChangeListener listener) {
276 mChangeListeners.add(listener);
277 }
278
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800279 public void removeOnChangeListener(OnIDPChangeListener listener) {
280 mChangeListeners.remove(listener);
281 }
282
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700283 private void killProcess(Context context) {
284 Log.e("ConfigMonitor", "restarting launcher");
285 android.os.Process.killProcess(android.os.Process.myPid());
286 }
287
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800288 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800289 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
290 // Good place to check if grid size changed in themepicker when launcher was dead.
291 if (savedIconMaskPath.isEmpty()) {
292 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
293 .apply();
294 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
295 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
296 .apply();
297 apply(context, CHANGE_FLAG_ICON_PARAMS);
298 }
299 }
300
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800301 public void setCurrentGrid(Context context, String gridName) {
302 Context appContext = context.getApplicationContext();
303 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700304 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800305 }
306
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700307 private void onConfigChanged(Context context) {
308 // Config changes, what shall we do?
309 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
310
311 // Re-init grid
Jon Miranda6f7e9702019-09-16 14:44:14 -0700312 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
313 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
314 : null;
315 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700316
317 int changeFlags = 0;
318 if (numRows != oldProfile.numRows ||
319 numColumns != oldProfile.numColumns ||
320 numFolderColumns != oldProfile.numFolderColumns ||
321 numFolderRows != oldProfile.numFolderRows ||
322 numHotseatIcons != oldProfile.numHotseatIcons) {
323 changeFlags |= CHANGE_FLAG_GRID;
324 }
325
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800326 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
327 !iconShapePath.equals(oldProfile.iconShapePath)) {
328 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700329 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800330 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700331 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800332 }
333
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800334 apply(context, changeFlags);
335 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700336
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800337 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700338 // Create a new config monitor
339 mConfigMonitor.unregister();
340 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
341
342 for (OnIDPChangeListener listener : mChangeListeners) {
343 listener.onIdpChanged(changeFlags, this);
344 }
345 }
346
Sunny Goyal415f1732018-11-29 10:33:47 -0800347 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
348 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700349 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
350 final int depth = parser.getDepth();
351 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700352 while (((type = parser.next()) != XmlPullParser.END_TAG ||
353 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800354 if ((type == XmlPullParser.START_TAG)
355 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800356
357 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
358 final int displayDepth = parser.getDepth();
359 while (((type = parser.next()) != XmlPullParser.END_TAG ||
360 parser.getDepth() > displayDepth)
361 && type != XmlPullParser.END_DOCUMENT) {
362 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
363 parser.getName())) {
364 profiles.add(new DisplayOption(
365 gridOption, context, Xml.asAttributeSet(parser)));
366 }
367 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700368 }
369 }
370 } catch (IOException|XmlPullParserException e) {
371 throw new RuntimeException(e);
372 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800373
374 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
375 if (!TextUtils.isEmpty(gridName)) {
376 for (DisplayOption option : profiles) {
377 if (gridName.equals(option.grid.name)) {
378 filteredProfiles.add(option);
379 }
380 }
381 }
382 if (filteredProfiles.isEmpty()) {
383 // No grid found, use the default options
384 for (DisplayOption option : profiles) {
385 if (option.canBeDefault) {
386 filteredProfiles.add(option);
387 }
388 }
389 }
390 if (filteredProfiles.isEmpty()) {
391 throw new RuntimeException("No display option with canBeDefault=true");
392 }
393 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700394 }
395
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700396 private int getLauncherIconDensity(int requiredSize) {
397 // Densities typically defined by an app.
398 int[] densityBuckets = new int[] {
399 DisplayMetrics.DENSITY_LOW,
400 DisplayMetrics.DENSITY_MEDIUM,
401 DisplayMetrics.DENSITY_TV,
402 DisplayMetrics.DENSITY_HIGH,
403 DisplayMetrics.DENSITY_XHIGH,
404 DisplayMetrics.DENSITY_XXHIGH,
405 DisplayMetrics.DENSITY_XXXHIGH
406 };
407
408 int density = DisplayMetrics.DENSITY_XXXHIGH;
409 for (int i = densityBuckets.length - 1; i >= 0; i--) {
410 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
411 / DisplayMetrics.DENSITY_DEFAULT;
412 if (expectedSize >= requiredSize) {
413 density = densityBuckets[i];
414 }
415 }
416
417 return density;
418 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700419
Adam Cohen2e6da152015-05-06 11:42:25 -0700420 /**
421 * Apply any Partner customization grid overrides.
422 *
423 * Currently we support: all apps row / column count.
424 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700425 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
426 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700427 if (p != null) {
428 p.applyInvariantDeviceProfileOverrides(this, dm);
429 }
430 }
431
Sunny Goyal415f1732018-11-29 10:33:47 -0800432 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700433 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700434 }
435
Sunny Goyal415f1732018-11-29 10:33:47 -0800436 @VisibleForTesting
437 static DisplayOption invDistWeightedInterpolate(float width, float height,
438 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700439 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700440
Sunny Goyal415f1732018-11-29 10:33:47 -0800441 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700442 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
443 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700444 }
445
Sunny Goyal415f1732018-11-29 10:33:47 -0800446 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700447 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800448 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700449 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
450 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800451 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700452 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800453 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700454 }
455
Sunny Goyal27835952017-01-13 12:15:53 -0800456 public DeviceProfile getDeviceProfile(Context context) {
457 return context.getResources().getConfiguration().orientation
458 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
459 }
460
Sunny Goyal415f1732018-11-29 10:33:47 -0800461 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700462 float d = dist(x0, y0, x1, y1);
463 if (Float.compare(d, 0f) == 0) {
464 return Float.POSITIVE_INFINITY;
465 }
466 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
467 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700468
469 /**
470 * As a ratio of screen height, the total distance we want the parallax effect to span
471 * horizontally
472 */
473 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
474 float aspectRatio = width / (float) height;
475
476 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
477 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
478 // We will use these two data points to extrapolate how much the wallpaper parallax effect
479 // to span (ie travel) at any aspect ratio:
480
481 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
482 final float ASPECT_RATIO_PORTRAIT = 10/16f;
483 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
484 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
485
486 // To find out the desired width at different aspect ratios, we use the following two
487 // formulas, where the coefficient on x is the aspect ratio (width/height):
488 // (16/10)x + y = 1.5
489 // (10/16)x + y = 1.2
490 // We solve for x and y and end up with a final formula:
491 final float x =
492 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
493 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
494 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
495 return x * aspectRatio + y;
496 }
497
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700498 public interface OnIDPChangeListener {
499
500 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
501 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800502
503
Sunny Goyaleff44f32019-01-09 17:29:49 -0800504 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800505
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800506 public static final String TAG_NAME = "grid-option";
507
Sunny Goyaleff44f32019-01-09 17:29:49 -0800508 public final String name;
509 public final int numRows;
510 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800511
512 private final int numFolderRows;
513 private final int numFolderColumns;
514
515 private final int numHotseatIcons;
516
Jon Miranda6f7e9702019-09-16 14:44:14 -0700517 private final int numAllAppsColumns;
518
Sunny Goyal415f1732018-11-29 10:33:47 -0800519 private final int defaultLayoutId;
520 private final int demoModeLayoutId;
521
Sunny Goyal5bc18462019-01-07 15:13:39 -0800522 private final SparseArray<TypedValue> extraAttrs;
523
Sunny Goyaleff44f32019-01-09 17:29:49 -0800524 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800525 TypedArray a = context.obtainStyledAttributes(
526 attrs, R.styleable.GridDisplayOption);
527 name = a.getString(R.styleable.GridDisplayOption_name);
528 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
529 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
530
531 defaultLayoutId = a.getResourceId(
532 R.styleable.GridDisplayOption_defaultLayoutId, 0);
533 demoModeLayoutId = a.getResourceId(
534 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
535 numHotseatIcons = a.getInt(
536 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
537 numFolderRows = a.getInt(
538 R.styleable.GridDisplayOption_numFolderRows, numRows);
539 numFolderColumns = a.getInt(
540 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700541 numAllAppsColumns = a.getInt(
542 R.styleable.GridDisplayOption_numAllAppsColumns, numColumns);
543
Sunny Goyal415f1732018-11-29 10:33:47 -0800544 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800545
546 extraAttrs = Themes.createValueMap(context, attrs,
547 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800548 }
549 }
550
551 private static final class DisplayOption {
552 private final GridOption grid;
553
554 private final String name;
555 private final float minWidthDps;
556 private final float minHeightDps;
557 private final boolean canBeDefault;
558
559 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800560 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700561 private float landscapeIconSize;
562 private float allAppsIconSize;
563 private float allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800564
565 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
566 this.grid = grid;
567
568 TypedArray a = context.obtainStyledAttributes(
569 attrs, R.styleable.ProfileDisplayOption);
570
571 name = a.getString(R.styleable.ProfileDisplayOption_name);
572 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
573 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
574 canBeDefault = a.getBoolean(
575 R.styleable.ProfileDisplayOption_canBeDefault, false);
576
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700577 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800578 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
579 iconSize);
580 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700581
582 allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize,
583 iconSize);
584 allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize,
585 iconTextSize);
Sunny Goyal415f1732018-11-29 10:33:47 -0800586 a.recycle();
587 }
588
589 DisplayOption() {
590 grid = null;
591 name = null;
592 minWidthDps = 0;
593 minHeightDps = 0;
594 canBeDefault = false;
595 }
596
597 private DisplayOption multiply(float w) {
598 iconSize *= w;
599 landscapeIconSize *= w;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700600 allAppsIconSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800601 iconTextSize *= w;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700602 allAppsIconTextSize *= w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800603 return this;
604 }
605
606 private DisplayOption add(DisplayOption p) {
607 iconSize += p.iconSize;
608 landscapeIconSize += p.landscapeIconSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700609 allAppsIconSize += p.allAppsIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800610 iconTextSize += p.iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700611 allAppsIconTextSize += p.allAppsIconTextSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800612 return this;
613 }
614 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700615
616 private class OverlayMonitor extends BroadcastReceiver {
617
618 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
619
620 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700621 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700622 }
623
624 @Override
625 public void onReceive(Context context, Intent intent) {
626 onConfigChanged(context);
627 }
628 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700629}