blob: 8ee530f0f938c4c3ce7468ed92faa1ed5f0cf8cb [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;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070022import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070023
Sunny Goyalc6205602015-05-21 20:46:33 -070024import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070025import android.appwidget.AppWidgetHostView;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070026import android.content.BroadcastReceiver;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070029import android.content.Intent;
Sunny Goyal27835952017-01-13 12:15:53 -080030import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080031import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070032import android.content.res.TypedArray;
33import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import android.graphics.Point;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070035import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080036import android.text.TextUtils;
37import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070038import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070039import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080040import android.util.SparseArray;
41import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070042import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070043import android.view.Display;
44import android.view.WindowManager;
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;
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;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700106
Sunny Goyal5bc18462019-01-07 15:13:39 -0800107 private SparseArray<TypedValue> mExtraAttrs;
108
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700109 /**
110 * Number of icons inside the hotseat area.
111 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800112 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700113
Sunny Goyal415f1732018-11-29 10:33:47 -0800114 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700115 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700116
cuijiaxingabda8d72017-03-20 09:51:36 -0700117 public DeviceProfile landscapeProfile;
118 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700119
Sunny Goyal6f866092016-03-17 17:04:15 -0700120 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700121 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700122
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700123 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
124 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700125 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700126
Sunny Goyalf633ef52018-03-13 09:57:05 -0700127 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700128 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700129
Sunny Goyalf633ef52018-03-13 09:57:05 -0700130 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800131 numRows = p.numRows;
132 numColumns = p.numColumns;
133 numFolderRows = p.numFolderRows;
134 numFolderColumns = p.numFolderColumns;
135 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800136 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800137 landscapeIconSize = p.landscapeIconSize;
138 iconTextSize = p.iconTextSize;
139 numHotseatIcons = p.numHotseatIcons;
140 defaultLayoutId = p.defaultLayoutId;
141 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800142 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700143 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700144 }
145
Sunny Goyalbbf01842015-10-08 07:41:15 -0700146 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700147 private InvariantDeviceProfile(Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800148 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700149 mConfigMonitor = new ConfigMonitor(context,
150 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700151 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700152 }
153
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700154 /**
155 * This constructor should NOT have any monitors by design.
156 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800157 public InvariantDeviceProfile(Context context, String gridName) {
158 String newName = initGrid(context, gridName);
159 if (newName == null || !newName.equals(gridName)) {
160 throw new IllegalArgumentException("Unknown grid name");
161 }
162 }
163
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800164 /**
165 * Retrieve system defined or RRO overriden icon shape.
166 */
167 private static String getIconShapePath(Context context) {
168 if (CONFIG_ICON_MASK_RES_ID == 0) {
169 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
170 return "";
171 }
172 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
173 }
174
Sunny Goyaleff44f32019-01-09 17:29:49 -0800175 private String initGrid(Context context, String gridName) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700176 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
177 Display display = wm.getDefaultDisplay();
178 DisplayMetrics dm = new DisplayMetrics();
179 display.getMetrics(dm);
180
181 Point smallestSize = new Point();
182 Point largestSize = new Point();
183 display.getCurrentSizeRange(smallestSize, largestSize);
184
Sunny Goyal415f1732018-11-29 10:33:47 -0800185 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700186 // This guarantees that width < height
Sunny Goyal415f1732018-11-29 10:33:47 -0800187 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
188 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
189 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800190 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800191 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
192 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
193 DisplayOption interpolatedDisplayOption =
194 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700195
Sunny Goyal415f1732018-11-29 10:33:47 -0800196 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700197 numRows = closestProfile.numRows;
198 numColumns = closestProfile.numColumns;
199 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700200 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700201 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700202 numFolderRows = closestProfile.numFolderRows;
203 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800204 mExtraAttrs = closestProfile.extraAttrs;
205
Sunny Goyal415f1732018-11-29 10:33:47 -0800206 if (!closestProfile.name.equals(gridName)) {
207 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800208 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800209 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700210
Sunny Goyal415f1732018-11-29 10:33:47 -0800211 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800212 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800213 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
vadimt1b383af2019-05-08 15:29:37 -0700214 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800215 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700216 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700217
218 // If the partner customization apk contains any grid overrides, apply them
219 // Supported overrides: numRows, numColumns, iconSize
220 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700221
222 Point realSize = new Point();
223 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700224 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700225 // same in any orientation.
226 int smallSide = Math.min(realSize.x, realSize.y);
227 int largeSide = Math.max(realSize.x, realSize.y);
228
229 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800230 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700231 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800232 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700233
234 // We need to ensure that there is enough extra space in the wallpaper
235 // for the intended parallax effects
236 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
237 defaultWallpaperSize = new Point(
238 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
239 largeSide);
240 } else {
241 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
242 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700243
244 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
245 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
246
Sunny Goyaleff44f32019-01-09 17:29:49 -0800247 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700248 }
249
Sunny Goyal5bc18462019-01-07 15:13:39 -0800250 @Nullable
251 public TypedValue getAttrValue(int attr) {
252 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
253 }
254
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700255 public void addOnChangeListener(OnIDPChangeListener listener) {
256 mChangeListeners.add(listener);
257 }
258
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800259 public void removeOnChangeListener(OnIDPChangeListener listener) {
260 mChangeListeners.remove(listener);
261 }
262
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700263 private void killProcess(Context context) {
264 Log.e("ConfigMonitor", "restarting launcher");
265 android.os.Process.killProcess(android.os.Process.myPid());
266 }
267
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800268 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800269 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
270 // Good place to check if grid size changed in themepicker when launcher was dead.
271 if (savedIconMaskPath.isEmpty()) {
272 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
273 .apply();
274 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
275 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
276 .apply();
277 apply(context, CHANGE_FLAG_ICON_PARAMS);
278 }
279 }
280
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800281 public void setCurrentGrid(Context context, String gridName) {
282 Context appContext = context.getApplicationContext();
283 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700284 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800285 }
286
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700287 private void onConfigChanged(Context context) {
288 // Config changes, what shall we do?
289 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
290
291 // Re-init grid
Jon Mirandac0f1f4f2019-05-28 15:49:09 -0700292 // TODO(b/131867841): We pass in null here so that we can calculate the closest profile
293 // without the bias of the grid name.
294 initGrid(context, null);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700295
296 int changeFlags = 0;
297 if (numRows != oldProfile.numRows ||
298 numColumns != oldProfile.numColumns ||
299 numFolderColumns != oldProfile.numFolderColumns ||
300 numFolderRows != oldProfile.numFolderRows ||
301 numHotseatIcons != oldProfile.numHotseatIcons) {
302 changeFlags |= CHANGE_FLAG_GRID;
303 }
304
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800305 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
306 !iconShapePath.equals(oldProfile.iconShapePath)) {
307 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700308 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800309 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700310 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800311 }
312
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800313 apply(context, changeFlags);
314 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700315
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800316 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700317 // Create a new config monitor
318 mConfigMonitor.unregister();
319 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
320
321 for (OnIDPChangeListener listener : mChangeListeners) {
322 listener.onIdpChanged(changeFlags, this);
323 }
324 }
325
Sunny Goyal415f1732018-11-29 10:33:47 -0800326 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
327 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700328 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
329 final int depth = parser.getDepth();
330 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700331 while (((type = parser.next()) != XmlPullParser.END_TAG ||
332 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800333 if ((type == XmlPullParser.START_TAG)
334 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800335
336 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
337 final int displayDepth = parser.getDepth();
338 while (((type = parser.next()) != XmlPullParser.END_TAG ||
339 parser.getDepth() > displayDepth)
340 && type != XmlPullParser.END_DOCUMENT) {
341 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
342 parser.getName())) {
343 profiles.add(new DisplayOption(
344 gridOption, context, Xml.asAttributeSet(parser)));
345 }
346 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700347 }
348 }
349 } catch (IOException|XmlPullParserException e) {
350 throw new RuntimeException(e);
351 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800352
353 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
354 if (!TextUtils.isEmpty(gridName)) {
355 for (DisplayOption option : profiles) {
356 if (gridName.equals(option.grid.name)) {
357 filteredProfiles.add(option);
358 }
359 }
360 }
361 if (filteredProfiles.isEmpty()) {
362 // No grid found, use the default options
363 for (DisplayOption option : profiles) {
364 if (option.canBeDefault) {
365 filteredProfiles.add(option);
366 }
367 }
368 }
369 if (filteredProfiles.isEmpty()) {
370 throw new RuntimeException("No display option with canBeDefault=true");
371 }
372 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700373 }
374
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700375 private int getLauncherIconDensity(int requiredSize) {
376 // Densities typically defined by an app.
377 int[] densityBuckets = new int[] {
378 DisplayMetrics.DENSITY_LOW,
379 DisplayMetrics.DENSITY_MEDIUM,
380 DisplayMetrics.DENSITY_TV,
381 DisplayMetrics.DENSITY_HIGH,
382 DisplayMetrics.DENSITY_XHIGH,
383 DisplayMetrics.DENSITY_XXHIGH,
384 DisplayMetrics.DENSITY_XXXHIGH
385 };
386
387 int density = DisplayMetrics.DENSITY_XXXHIGH;
388 for (int i = densityBuckets.length - 1; i >= 0; i--) {
389 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
390 / DisplayMetrics.DENSITY_DEFAULT;
391 if (expectedSize >= requiredSize) {
392 density = densityBuckets[i];
393 }
394 }
395
396 return density;
397 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700398
Adam Cohen2e6da152015-05-06 11:42:25 -0700399 /**
400 * Apply any Partner customization grid overrides.
401 *
402 * Currently we support: all apps row / column count.
403 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700404 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
405 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700406 if (p != null) {
407 p.applyInvariantDeviceProfileOverrides(this, dm);
408 }
409 }
410
Sunny Goyal415f1732018-11-29 10:33:47 -0800411 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700412 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700413 }
414
Sunny Goyal415f1732018-11-29 10:33:47 -0800415 @VisibleForTesting
416 static DisplayOption invDistWeightedInterpolate(float width, float height,
417 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700418 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700419
Sunny Goyal415f1732018-11-29 10:33:47 -0800420 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700421 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
422 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700423 }
424
Sunny Goyal415f1732018-11-29 10:33:47 -0800425 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700426 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800427 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700428 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
429 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800430 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700431 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800432 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700433 }
434
Sunny Goyal27835952017-01-13 12:15:53 -0800435 public DeviceProfile getDeviceProfile(Context context) {
436 return context.getResources().getConfiguration().orientation
437 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
438 }
439
Sunny Goyal415f1732018-11-29 10:33:47 -0800440 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700441 float d = dist(x0, y0, x1, y1);
442 if (Float.compare(d, 0f) == 0) {
443 return Float.POSITIVE_INFINITY;
444 }
445 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
446 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700447
448 /**
449 * As a ratio of screen height, the total distance we want the parallax effect to span
450 * horizontally
451 */
452 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
453 float aspectRatio = width / (float) height;
454
455 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
456 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
457 // We will use these two data points to extrapolate how much the wallpaper parallax effect
458 // to span (ie travel) at any aspect ratio:
459
460 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
461 final float ASPECT_RATIO_PORTRAIT = 10/16f;
462 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
463 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
464
465 // To find out the desired width at different aspect ratios, we use the following two
466 // formulas, where the coefficient on x is the aspect ratio (width/height):
467 // (16/10)x + y = 1.5
468 // (10/16)x + y = 1.2
469 // We solve for x and y and end up with a final formula:
470 final float x =
471 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
472 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
473 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
474 return x * aspectRatio + y;
475 }
476
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700477 public interface OnIDPChangeListener {
478
479 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
480 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800481
482
Sunny Goyaleff44f32019-01-09 17:29:49 -0800483 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800484
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800485 public static final String TAG_NAME = "grid-option";
486
Sunny Goyaleff44f32019-01-09 17:29:49 -0800487 public final String name;
488 public final int numRows;
489 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800490
491 private final int numFolderRows;
492 private final int numFolderColumns;
493
494 private final int numHotseatIcons;
495
496 private final int defaultLayoutId;
497 private final int demoModeLayoutId;
498
Sunny Goyal5bc18462019-01-07 15:13:39 -0800499 private final SparseArray<TypedValue> extraAttrs;
500
Sunny Goyaleff44f32019-01-09 17:29:49 -0800501 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800502 TypedArray a = context.obtainStyledAttributes(
503 attrs, R.styleable.GridDisplayOption);
504 name = a.getString(R.styleable.GridDisplayOption_name);
505 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
506 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
507
508 defaultLayoutId = a.getResourceId(
509 R.styleable.GridDisplayOption_defaultLayoutId, 0);
510 demoModeLayoutId = a.getResourceId(
511 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
512 numHotseatIcons = a.getInt(
513 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
514 numFolderRows = a.getInt(
515 R.styleable.GridDisplayOption_numFolderRows, numRows);
516 numFolderColumns = a.getInt(
517 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
518 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800519
520 extraAttrs = Themes.createValueMap(context, attrs,
521 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800522 }
523 }
524
525 private static final class DisplayOption {
526 private final GridOption grid;
527
528 private final String name;
529 private final float minWidthDps;
530 private final float minHeightDps;
531 private final boolean canBeDefault;
532
533 private float iconSize;
534 private float landscapeIconSize;
535 private float iconTextSize;
536
537 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
538 this.grid = grid;
539
540 TypedArray a = context.obtainStyledAttributes(
541 attrs, R.styleable.ProfileDisplayOption);
542
543 name = a.getString(R.styleable.ProfileDisplayOption_name);
544 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
545 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
546 canBeDefault = a.getBoolean(
547 R.styleable.ProfileDisplayOption_canBeDefault, false);
548
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700549 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800550 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
551 iconSize);
552 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
553 a.recycle();
554 }
555
556 DisplayOption() {
557 grid = null;
558 name = null;
559 minWidthDps = 0;
560 minHeightDps = 0;
561 canBeDefault = false;
562 }
563
564 private DisplayOption multiply(float w) {
565 iconSize *= w;
566 landscapeIconSize *= w;
567 iconTextSize *= w;
568 return this;
569 }
570
571 private DisplayOption add(DisplayOption p) {
572 iconSize += p.iconSize;
573 landscapeIconSize += p.landscapeIconSize;
574 iconTextSize += p.iconTextSize;
575 return this;
576 }
577 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700578
579 private class OverlayMonitor extends BroadcastReceiver {
580
581 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
582
583 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700584 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700585 }
586
587 @Override
588 public void onReceive(Context context, Intent intent) {
589 onConfigChanged(context);
590 }
591 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700592}