blob: a807e4f0fb1d75a4ade436a3d6d0d12976d9a239 [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Hyunyoung Songc55a3502018-12-04 15:43:16 -080019import static com.android.launcher3.Utilities.getDevicePrefs;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070020import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME;
Jon Miranda6f7e9702019-09-16 14:44:14 -070021import static com.android.launcher3.settings.SettingsActivity.GRID_OPTIONS_PREFERENCE_KEY;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080022import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070023import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070024
Sunny Goyalc6205602015-05-21 20:46:33 -070025import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070026import android.appwidget.AppWidgetHostView;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070027import android.content.BroadcastReceiver;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070028import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070029import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070030import android.content.Intent;
Sunny Goyal27835952017-01-13 12:15:53 -080031import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080032import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070033import android.content.res.TypedArray;
34import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070035import android.graphics.Point;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070036import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080037import android.text.TextUtils;
38import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070039import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070040import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080041import android.util.SparseArray;
42import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070043import android.util.Xml;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080044import android.view.Display;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070045
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070046import androidx.annotation.Nullable;
47import androidx.annotation.VisibleForTesting;
48
Sunny Goyal905262c2019-05-03 16:50:43 -070049import com.android.launcher3.graphics.IconShape;
Sunny Goyald0e360a2018-06-29 14:40:18 -070050import com.android.launcher3.util.ConfigMonitor;
Winson Chung13c1c2c2019-09-06 11:46:19 -070051import com.android.launcher3.util.DefaultDisplay;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080052import com.android.launcher3.util.DefaultDisplay.Info;
Sunny Goyal5bc18462019-01-07 15:13:39 -080053import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070054import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080055import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070056
Sunny Goyal819e1932016-07-07 16:43:58 -070057import org.xmlpull.v1.XmlPullParser;
58import org.xmlpull.v1.XmlPullParserException;
59
60import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070061import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080062import java.util.Collections;
Jon Miranda64d74812019-10-15 15:33:16 -070063import java.util.Comparator;
Adam Cohen2e6da152015-05-06 11:42:25 -070064
65public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070066
Hyunyoung Songc55a3502018-12-04 15:43:16 -080067 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070068 // We do not need any synchronization for this variable as its only written on UI thread.
69 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070070 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070071
Hyunyoung Songc55a3502018-12-04 15:43:16 -080072 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080073
Sunny Goyal53d7ee42015-05-22 12:25:45 -070074 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
75
Sunny Goyal87dc48b2018-10-12 11:42:33 -070076 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080077 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
78
79 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070080
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070081 // Constants that affects the interpolation curve between statically defined device profile
82 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080083 private static final float KNEARESTNEIGHBOR = 3;
84 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070085
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070086 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080087 private static final float WEIGHT_EFFICIENT = 100000f;
88
89 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
90 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070091
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070092 /**
93 * Number of icons per row and column in the workspace.
94 */
Adam Cohen2e6da152015-05-06 11:42:25 -070095 public int numRows;
96 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070097
98 /**
99 * Number of icons per row and column in the folder.
100 */
Adam Cohen2e6da152015-05-06 11:42:25 -0700101 public int numFolderRows;
102 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -0700103 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800104 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700105 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700106 public int iconBitmapSize;
107 public int fillResIconDpi;
108 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700109
Sunny Goyal5bc18462019-01-07 15:13:39 -0800110 private SparseArray<TypedValue> mExtraAttrs;
111
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700112 /**
113 * Number of icons inside the hotseat area.
114 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800115 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700116
Jon Miranda6f7e9702019-09-16 14:44:14 -0700117 /**
118 * Number of columns in the all apps list.
119 */
120 public int numAllAppsColumns;
121
Sunny Goyal415f1732018-11-29 10:33:47 -0800122 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700123 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700124
cuijiaxingabda8d72017-03-20 09:51:36 -0700125 public DeviceProfile landscapeProfile;
126 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700127
Sunny Goyal6f866092016-03-17 17:04:15 -0700128 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700129 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700130
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700131 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
132 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700133 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700134
Sunny Goyalf633ef52018-03-13 09:57:05 -0700135 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700136 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700137
Sunny Goyalf633ef52018-03-13 09:57:05 -0700138 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800139 numRows = p.numRows;
140 numColumns = p.numColumns;
141 numFolderRows = p.numFolderRows;
142 numFolderColumns = p.numFolderColumns;
143 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800144 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800145 landscapeIconSize = p.landscapeIconSize;
146 iconTextSize = p.iconTextSize;
147 numHotseatIcons = p.numHotseatIcons;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700148 numAllAppsColumns = p.numAllAppsColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800149 defaultLayoutId = p.defaultLayoutId;
150 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800151 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700152 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700153 }
154
Sunny Goyalbbf01842015-10-08 07:41:15 -0700155 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700156 private InvariantDeviceProfile(Context context) {
Jon Miranda6f7e9702019-09-16 14:44:14 -0700157 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
158 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
159 : null;
160 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700161 mConfigMonitor = new ConfigMonitor(context,
162 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700163 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700164 }
165
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700166 /**
167 * This constructor should NOT have any monitors by design.
168 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800169 public InvariantDeviceProfile(Context context, String gridName) {
170 String newName = initGrid(context, gridName);
171 if (newName == null || !newName.equals(gridName)) {
172 throw new IllegalArgumentException("Unknown grid name");
173 }
174 }
175
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800176 /**
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800177 * This constructor should NOT have any monitors by design.
178 */
179 public InvariantDeviceProfile(Context context, Display display) {
180 initGrid(context, null, new Info(display));
181 }
182
183 /**
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800184 * Retrieve system defined or RRO overriden icon shape.
185 */
186 private static String getIconShapePath(Context context) {
187 if (CONFIG_ICON_MASK_RES_ID == 0) {
188 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
189 return "";
190 }
191 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
192 }
193
Sunny Goyaleff44f32019-01-09 17:29:49 -0800194 private String initGrid(Context context, String gridName) {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800195 return initGrid(context, gridName, DefaultDisplay.INSTANCE.get(context).getInfo());
196 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700197
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800198 private String initGrid(Context context, String gridName, DefaultDisplay.Info displayInfo) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700199 Point smallestSize = new Point(displayInfo.smallestSize);
200 Point largestSize = new Point(displayInfo.largestSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700201
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700202 // This guarantees that width < height
Winson Chung13c1c2c2019-09-06 11:46:19 -0700203 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
204 displayInfo.metrics);
205 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
206 displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700207
Winson Chung13c1c2c2019-09-06 11:46:19 -0700208 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700209 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700210 // same in any orientation.
211 int smallSide = Math.min(realSize.x, realSize.y);
212 int largeSide = Math.max(realSize.x, realSize.y);
213
Jon Miranda64d74812019-10-15 15:33:16 -0700214 // We want a list of all options as well as the list of filtered options. This allows us
215 // to have a consistent UI for areas that the grid size change should not affect
216 // ie. All Apps should be consistent between grid sizes.
217 ArrayList<DisplayOption> allOptions = new ArrayList<>();
218 ArrayList<DisplayOption> filteredOptions = new ArrayList<>();
219 getPredefinedDeviceProfiles(context, gridName, filteredOptions, allOptions);
220
221 if (allOptions.isEmpty() && filteredOptions.isEmpty()) {
222 throw new RuntimeException("No display option with canBeDefault=true");
223 }
224
225 // Sort the profiles based on the closeness to the device size
226 Comparator<DisplayOption> comparator = (a, b) -> Float.compare(dist(minWidthDps,
227 minHeightDps, a.minWidthDps, a.minHeightDps),
228 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps));
229
230 // Calculate the device profiles as if there is no grid override.
231 Collections.sort(allOptions, comparator);
232 DisplayOption interpolatedDisplayOption =
233 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
234 initGridOption(context, allOptions, interpolatedDisplayOption, displayInfo.metrics);
235
236 // Create IDP with no grid override values.
237 InvariantDeviceProfile originalIDP = new InvariantDeviceProfile(this);
238 originalIDP.landscapeProfile = new DeviceProfile(context, this, null, smallestSize,
239 largestSize, largeSide, smallSide, true /* isLandscape */,
240 false /* isMultiWindowMode */);
241 originalIDP.portraitProfile = new DeviceProfile(context, this, null, smallestSize,
242 largestSize, smallSide, largeSide, false /* isLandscape */,
243 false /* isMultiWindowMode */);
244
245 if (filteredOptions.isEmpty()) {
246 filteredOptions = allOptions;
247
248 landscapeProfile = originalIDP.landscapeProfile;
249 portraitProfile = originalIDP.portraitProfile;
250 } else {
251 Collections.sort(filteredOptions, comparator);
252 interpolatedDisplayOption =
253 invDistWeightedInterpolate(minWidthDps, minHeightDps, filteredOptions);
254
255 initGridOption(context, filteredOptions, interpolatedDisplayOption,
256 displayInfo.metrics);
257 numAllAppsColumns = originalIDP.numAllAppsColumns;
258
259 landscapeProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
260 largestSize, largeSide, smallSide, true /* isLandscape */,
261 false /* isMultiWindowMode */);
262 portraitProfile = new DeviceProfile(context, this, originalIDP, smallestSize,
263 largestSize, smallSide, largeSide, false /* isLandscape */,
264 false /* isMultiWindowMode */);
265 }
266
267 GridOption closestProfile = filteredOptions.get(0).grid;
268 if (!closestProfile.name.equals(gridName)) {
269 Utilities.getPrefs(context).edit()
270 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
271 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700272
273 // We need to ensure that there is enough extra space in the wallpaper
274 // for the intended parallax effects
275 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
276 defaultWallpaperSize = new Point(
277 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
278 largeSide);
279 } else {
280 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
281 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700282
283 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
284 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
285
Sunny Goyaleff44f32019-01-09 17:29:49 -0800286 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700287 }
288
Jon Miranda64d74812019-10-15 15:33:16 -0700289 private void initGridOption(Context context, ArrayList<DisplayOption> options,
290 DisplayOption displayOption, DisplayMetrics metrics) {
291 GridOption closestProfile = options.get(0).grid;
292 numRows = closestProfile.numRows;
293 numColumns = closestProfile.numColumns;
294 numHotseatIcons = closestProfile.numHotseatIcons;
295 defaultLayoutId = closestProfile.defaultLayoutId;
296 demoModeLayoutId = closestProfile.demoModeLayoutId;
297 numFolderRows = closestProfile.numFolderRows;
298 numFolderColumns = closestProfile.numFolderColumns;
299 numAllAppsColumns = numColumns;
300
301 mExtraAttrs = closestProfile.extraAttrs;
302
303 iconSize = displayOption.iconSize;
304 iconShapePath = getIconShapePath(context);
305 landscapeIconSize = displayOption.landscapeIconSize;
306 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, metrics);
307 iconTextSize = displayOption.iconTextSize;
308 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
309
310 // If the partner customization apk contains any grid overrides, apply them
311 // Supported overrides: numRows, numColumns, iconSize
312 applyPartnerDeviceProfileOverrides(context, metrics);
313 }
314
315
Sunny Goyal5bc18462019-01-07 15:13:39 -0800316 @Nullable
317 public TypedValue getAttrValue(int attr) {
318 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
319 }
320
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700321 public void addOnChangeListener(OnIDPChangeListener listener) {
322 mChangeListeners.add(listener);
323 }
324
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800325 public void removeOnChangeListener(OnIDPChangeListener listener) {
326 mChangeListeners.remove(listener);
327 }
328
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700329 private void killProcess(Context context) {
330 Log.e("ConfigMonitor", "restarting launcher");
331 android.os.Process.killProcess(android.os.Process.myPid());
332 }
333
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800334 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800335 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
336 // Good place to check if grid size changed in themepicker when launcher was dead.
337 if (savedIconMaskPath.isEmpty()) {
338 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
339 .apply();
340 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
341 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
342 .apply();
343 apply(context, CHANGE_FLAG_ICON_PARAMS);
344 }
345 }
346
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800347 public void setCurrentGrid(Context context, String gridName) {
348 Context appContext = context.getApplicationContext();
349 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700350 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800351 }
352
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700353 private void onConfigChanged(Context context) {
354 // Config changes, what shall we do?
355 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
356
357 // Re-init grid
Jon Miranda6f7e9702019-09-16 14:44:14 -0700358 String gridName = Utilities.getPrefs(context).getBoolean(GRID_OPTIONS_PREFERENCE_KEY, false)
359 ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null)
360 : null;
361 initGrid(context, gridName);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700362
363 int changeFlags = 0;
364 if (numRows != oldProfile.numRows ||
365 numColumns != oldProfile.numColumns ||
366 numFolderColumns != oldProfile.numFolderColumns ||
367 numFolderRows != oldProfile.numFolderRows ||
368 numHotseatIcons != oldProfile.numHotseatIcons) {
369 changeFlags |= CHANGE_FLAG_GRID;
370 }
371
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800372 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
373 !iconShapePath.equals(oldProfile.iconShapePath)) {
374 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700375 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800376 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700377 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800378 }
379
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800380 apply(context, changeFlags);
381 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700382
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800383 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700384 // Create a new config monitor
385 mConfigMonitor.unregister();
386 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
387
388 for (OnIDPChangeListener listener : mChangeListeners) {
389 listener.onIdpChanged(changeFlags, this);
390 }
391 }
392
Jon Miranda64d74812019-10-15 15:33:16 -0700393 /**
394 * @param gridName The current grid name.
395 * @param filteredOptionsOut List filled with all the filtered options based on gridName.
396 * @param allOptionsOut List filled with all the options that can be the default option.
397 */
398 static void getPredefinedDeviceProfiles(Context context, String gridName,
399 ArrayList<DisplayOption> filteredOptionsOut, ArrayList<DisplayOption> allOptionsOut) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800400 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700401 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
402 final int depth = parser.getDepth();
403 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700404 while (((type = parser.next()) != XmlPullParser.END_TAG ||
405 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800406 if ((type == XmlPullParser.START_TAG)
407 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800408
409 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
410 final int displayDepth = parser.getDepth();
411 while (((type = parser.next()) != XmlPullParser.END_TAG ||
412 parser.getDepth() > displayDepth)
413 && type != XmlPullParser.END_DOCUMENT) {
414 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
415 parser.getName())) {
416 profiles.add(new DisplayOption(
417 gridOption, context, Xml.asAttributeSet(parser)));
418 }
419 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700420 }
421 }
422 } catch (IOException|XmlPullParserException e) {
423 throw new RuntimeException(e);
424 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800425
Sunny Goyal415f1732018-11-29 10:33:47 -0800426 if (!TextUtils.isEmpty(gridName)) {
427 for (DisplayOption option : profiles) {
428 if (gridName.equals(option.grid.name)) {
Jon Miranda64d74812019-10-15 15:33:16 -0700429 filteredOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800430 }
431 }
432 }
Jon Miranda64d74812019-10-15 15:33:16 -0700433
434 for (DisplayOption option : profiles) {
435 if (option.canBeDefault) {
436 allOptionsOut.add(option);
Sunny Goyal415f1732018-11-29 10:33:47 -0800437 }
438 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700439 }
440
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700441 private int getLauncherIconDensity(int requiredSize) {
442 // Densities typically defined by an app.
443 int[] densityBuckets = new int[] {
444 DisplayMetrics.DENSITY_LOW,
445 DisplayMetrics.DENSITY_MEDIUM,
446 DisplayMetrics.DENSITY_TV,
447 DisplayMetrics.DENSITY_HIGH,
448 DisplayMetrics.DENSITY_XHIGH,
449 DisplayMetrics.DENSITY_XXHIGH,
450 DisplayMetrics.DENSITY_XXXHIGH
451 };
452
453 int density = DisplayMetrics.DENSITY_XXXHIGH;
454 for (int i = densityBuckets.length - 1; i >= 0; i--) {
455 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
456 / DisplayMetrics.DENSITY_DEFAULT;
457 if (expectedSize >= requiredSize) {
458 density = densityBuckets[i];
459 }
460 }
461
462 return density;
463 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700464
Adam Cohen2e6da152015-05-06 11:42:25 -0700465 /**
466 * Apply any Partner customization grid overrides.
467 *
468 * Currently we support: all apps row / column count.
469 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700470 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
471 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700472 if (p != null) {
473 p.applyInvariantDeviceProfileOverrides(this, dm);
474 }
475 }
476
Sunny Goyal415f1732018-11-29 10:33:47 -0800477 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700478 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700479 }
480
Sunny Goyal415f1732018-11-29 10:33:47 -0800481 @VisibleForTesting
482 static DisplayOption invDistWeightedInterpolate(float width, float height,
483 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700484 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700485
Sunny Goyal415f1732018-11-29 10:33:47 -0800486 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700487 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
488 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700489 }
490
Sunny Goyal415f1732018-11-29 10:33:47 -0800491 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700492 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800493 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700494 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
495 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800496 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700497 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800498 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700499 }
500
Sunny Goyal27835952017-01-13 12:15:53 -0800501 public DeviceProfile getDeviceProfile(Context context) {
502 return context.getResources().getConfiguration().orientation
503 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
504 }
505
Sunny Goyal415f1732018-11-29 10:33:47 -0800506 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700507 float d = dist(x0, y0, x1, y1);
508 if (Float.compare(d, 0f) == 0) {
509 return Float.POSITIVE_INFINITY;
510 }
511 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
512 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700513
514 /**
515 * As a ratio of screen height, the total distance we want the parallax effect to span
516 * horizontally
517 */
518 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
519 float aspectRatio = width / (float) height;
520
521 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
522 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
523 // We will use these two data points to extrapolate how much the wallpaper parallax effect
524 // to span (ie travel) at any aspect ratio:
525
526 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
527 final float ASPECT_RATIO_PORTRAIT = 10/16f;
528 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
529 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
530
531 // To find out the desired width at different aspect ratios, we use the following two
532 // formulas, where the coefficient on x is the aspect ratio (width/height):
533 // (16/10)x + y = 1.5
534 // (10/16)x + y = 1.2
535 // We solve for x and y and end up with a final formula:
536 final float x =
537 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
538 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
539 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
540 return x * aspectRatio + y;
541 }
542
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700543 public interface OnIDPChangeListener {
544
545 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
546 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800547
548
Sunny Goyaleff44f32019-01-09 17:29:49 -0800549 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800550
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800551 public static final String TAG_NAME = "grid-option";
552
Sunny Goyaleff44f32019-01-09 17:29:49 -0800553 public final String name;
554 public final int numRows;
555 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800556
557 private final int numFolderRows;
558 private final int numFolderColumns;
559
560 private final int numHotseatIcons;
561
562 private final int defaultLayoutId;
563 private final int demoModeLayoutId;
564
Sunny Goyal5bc18462019-01-07 15:13:39 -0800565 private final SparseArray<TypedValue> extraAttrs;
566
Sunny Goyaleff44f32019-01-09 17:29:49 -0800567 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800568 TypedArray a = context.obtainStyledAttributes(
569 attrs, R.styleable.GridDisplayOption);
570 name = a.getString(R.styleable.GridDisplayOption_name);
571 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
572 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
573
574 defaultLayoutId = a.getResourceId(
575 R.styleable.GridDisplayOption_defaultLayoutId, 0);
576 demoModeLayoutId = a.getResourceId(
577 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
578 numHotseatIcons = a.getInt(
579 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
580 numFolderRows = a.getInt(
581 R.styleable.GridDisplayOption_numFolderRows, numRows);
582 numFolderColumns = a.getInt(
583 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700584
Sunny Goyal415f1732018-11-29 10:33:47 -0800585 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800586
587 extraAttrs = Themes.createValueMap(context, attrs,
588 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800589 }
590 }
591
592 private static final class DisplayOption {
593 private final GridOption grid;
594
595 private final String name;
596 private final float minWidthDps;
597 private final float minHeightDps;
598 private final boolean canBeDefault;
599
600 private float iconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800601 private float iconTextSize;
Jon Miranda6f7e9702019-09-16 14:44:14 -0700602 private float landscapeIconSize;
Sunny Goyal415f1732018-11-29 10:33:47 -0800603
604 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
605 this.grid = grid;
606
607 TypedArray a = context.obtainStyledAttributes(
608 attrs, R.styleable.ProfileDisplayOption);
609
610 name = a.getString(R.styleable.ProfileDisplayOption_name);
611 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
612 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
613 canBeDefault = a.getBoolean(
614 R.styleable.ProfileDisplayOption_canBeDefault, false);
615
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700616 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800617 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
618 iconSize);
619 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
Jon Miranda6f7e9702019-09-16 14:44:14 -0700620
Sunny Goyal415f1732018-11-29 10:33:47 -0800621 a.recycle();
622 }
623
624 DisplayOption() {
625 grid = null;
626 name = null;
627 minWidthDps = 0;
628 minHeightDps = 0;
629 canBeDefault = false;
630 }
631
632 private DisplayOption multiply(float w) {
633 iconSize *= w;
634 landscapeIconSize *= w;
635 iconTextSize *= w;
636 return this;
637 }
638
639 private DisplayOption add(DisplayOption p) {
640 iconSize += p.iconSize;
641 landscapeIconSize += p.landscapeIconSize;
642 iconTextSize += p.iconTextSize;
643 return this;
644 }
645 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700646
647 private class OverlayMonitor extends BroadcastReceiver {
648
649 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
650
651 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700652 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700653 }
654
655 @Override
656 public void onReceive(Context context, Intent intent) {
657 onConfigChanged(context);
658 }
659 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700660}