blob: 3c0ef79f1efa08216fca56e568ebda6f1a11c141 [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
Sunny Goyal87dc48b2018-10-12 11:42:33 -070019import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080020import static com.android.launcher3.Utilities.getDevicePrefs;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070021
Sunny Goyalc6205602015-05-21 20:46:33 -070022import android.annotation.TargetApi;
Adam Cohen2e6da152015-05-06 11:42:25 -070023import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080024import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080025import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070026import android.content.res.TypedArray;
27import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import android.graphics.Point;
Sunny Goyal415f1732018-11-29 10:33:47 -080029import android.text.TextUtils;
30import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070031import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070032import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080033import android.util.SparseArray;
34import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070035import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070036import android.view.Display;
37import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070038
Sunny Goyald0e360a2018-06-29 14:40:18 -070039import com.android.launcher3.util.ConfigMonitor;
Sunny Goyal5bc18462019-01-07 15:13:39 -080040import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070041import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080042import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070043
Sunny Goyal819e1932016-07-07 16:43:58 -070044import org.xmlpull.v1.XmlPullParser;
45import org.xmlpull.v1.XmlPullParserException;
46
47import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070048import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080049import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070050
Sunny Goyal5bc18462019-01-07 15:13:39 -080051import androidx.annotation.Nullable;
Sunny Goyald2303072018-08-14 15:21:45 -070052import androidx.annotation.VisibleForTesting;
53
Adam Cohen2e6da152015-05-06 11:42:25 -070054public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070055
Hyunyoung Songc55a3502018-12-04 15:43:16 -080056 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070057 // We do not need any synchronization for this variable as its only written on UI thread.
58 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070059 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070060
Hyunyoung Songc55a3502018-12-04 15:43:16 -080061 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080062
Sunny Goyal53d7ee42015-05-22 12:25:45 -070063 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
64
Sunny Goyal87dc48b2018-10-12 11:42:33 -070065 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080066 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
67
68 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070069
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070070 // Constants that affects the interpolation curve between statically defined device profile
71 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080072 private static final float KNEARESTNEIGHBOR = 3;
73 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070074
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070075 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080076 private static final float WEIGHT_EFFICIENT = 100000f;
77
78 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
79 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070080
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070081 /**
82 * Number of icons per row and column in the workspace.
83 */
Adam Cohen2e6da152015-05-06 11:42:25 -070084 public int numRows;
85 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070086
87 /**
88 * Number of icons per row and column in the folder.
89 */
Adam Cohen2e6da152015-05-06 11:42:25 -070090 public int numFolderRows;
91 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070092 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080093 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070094 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070095 public int iconBitmapSize;
96 public int fillResIconDpi;
97 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098
Sunny Goyal5bc18462019-01-07 15:13:39 -080099 private SparseArray<TypedValue> mExtraAttrs;
100
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700101 /**
102 * Number of icons inside the hotseat area.
103 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800104 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700105
Sunny Goyal415f1732018-11-29 10:33:47 -0800106 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700107 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700108
cuijiaxingabda8d72017-03-20 09:51:36 -0700109 public DeviceProfile landscapeProfile;
110 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700111
Sunny Goyal6f866092016-03-17 17:04:15 -0700112 public Point defaultWallpaperSize;
113
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700114 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
115 private ConfigMonitor mConfigMonitor;
116
Sunny Goyalf633ef52018-03-13 09:57:05 -0700117 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700118 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700119
Sunny Goyalf633ef52018-03-13 09:57:05 -0700120 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800121 numRows = p.numRows;
122 numColumns = p.numColumns;
123 numFolderRows = p.numFolderRows;
124 numFolderColumns = p.numFolderColumns;
125 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800126 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800127 landscapeIconSize = p.landscapeIconSize;
128 iconTextSize = p.iconTextSize;
129 numHotseatIcons = p.numHotseatIcons;
130 defaultLayoutId = p.defaultLayoutId;
131 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800132 mExtraAttrs = p.mExtraAttrs;
Adam Cohen2e6da152015-05-06 11:42:25 -0700133 }
134
Sunny Goyalbbf01842015-10-08 07:41:15 -0700135 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700136 private InvariantDeviceProfile(Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800137 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700138 mConfigMonitor = new ConfigMonitor(context,
139 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
140 }
141
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800142 /**
143 * Retrieve system defined or RRO overriden icon shape.
144 */
145 private static String getIconShapePath(Context context) {
146 if (CONFIG_ICON_MASK_RES_ID == 0) {
147 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
148 return "";
149 }
150 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
151 }
152
Sunny Goyal415f1732018-11-29 10:33:47 -0800153 private void initGrid(Context context, String gridName) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700154 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
155 Display display = wm.getDefaultDisplay();
156 DisplayMetrics dm = new DisplayMetrics();
157 display.getMetrics(dm);
158
159 Point smallestSize = new Point();
160 Point largestSize = new Point();
161 display.getCurrentSizeRange(smallestSize, largestSize);
162
Sunny Goyal415f1732018-11-29 10:33:47 -0800163 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700164 // This guarantees that width < height
Sunny Goyal415f1732018-11-29 10:33:47 -0800165 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
166 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
167 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800168 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800169 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
170 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
171 DisplayOption interpolatedDisplayOption =
172 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700173
Sunny Goyal415f1732018-11-29 10:33:47 -0800174 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700175 numRows = closestProfile.numRows;
176 numColumns = closestProfile.numColumns;
177 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700178 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700179 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700180 numFolderRows = closestProfile.numFolderRows;
181 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800182 mExtraAttrs = closestProfile.extraAttrs;
183
Sunny Goyal415f1732018-11-29 10:33:47 -0800184 if (!closestProfile.name.equals(gridName)) {
185 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800186 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800187 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700188
Sunny Goyal415f1732018-11-29 10:33:47 -0800189 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800190 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800191 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700192 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800193 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700194 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700195
196 // If the partner customization apk contains any grid overrides, apply them
197 // Supported overrides: numRows, numColumns, iconSize
198 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700199
200 Point realSize = new Point();
201 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700202 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700203 // same in any orientation.
204 int smallSide = Math.min(realSize.x, realSize.y);
205 int largeSide = Math.max(realSize.x, realSize.y);
206
207 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800208 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700209 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800210 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700211
212 // We need to ensure that there is enough extra space in the wallpaper
213 // for the intended parallax effects
214 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
215 defaultWallpaperSize = new Point(
216 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
217 largeSide);
218 } else {
219 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
220 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700221 }
222
Sunny Goyal5bc18462019-01-07 15:13:39 -0800223 @Nullable
224 public TypedValue getAttrValue(int attr) {
225 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
226 }
227
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700228 public void addOnChangeListener(OnIDPChangeListener listener) {
229 mChangeListeners.add(listener);
230 }
231
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800232 public void removeOnChangeListener(OnIDPChangeListener listener) {
233 mChangeListeners.remove(listener);
234 }
235
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700236 private void killProcess(Context context) {
237 Log.e("ConfigMonitor", "restarting launcher");
238 android.os.Process.killProcess(android.os.Process.myPid());
239 }
240
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800241 public void verifyConfigChangedInBackground(final Context context) {
242
243 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
244 // Good place to check if grid size changed in themepicker when launcher was dead.
245 if (savedIconMaskPath.isEmpty()) {
246 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
247 .apply();
248 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
249 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
250 .apply();
251 apply(context, CHANGE_FLAG_ICON_PARAMS);
252 }
253 }
254
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700255 private void onConfigChanged(Context context) {
256 // Config changes, what shall we do?
257 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
258
259 // Re-init grid
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800260 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700261
262 int changeFlags = 0;
263 if (numRows != oldProfile.numRows ||
264 numColumns != oldProfile.numColumns ||
265 numFolderColumns != oldProfile.numFolderColumns ||
266 numFolderRows != oldProfile.numFolderRows ||
267 numHotseatIcons != oldProfile.numHotseatIcons) {
268 changeFlags |= CHANGE_FLAG_GRID;
269 }
270
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800271 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
272 !iconShapePath.equals(oldProfile.iconShapePath)) {
273 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700274 }
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800275 apply(context, changeFlags);
276 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700277
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800278 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700279 // Create a new config monitor
280 mConfigMonitor.unregister();
281 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
282
283 for (OnIDPChangeListener listener : mChangeListeners) {
284 listener.onIdpChanged(changeFlags, this);
285 }
286 }
287
Sunny Goyal415f1732018-11-29 10:33:47 -0800288 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
289 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700290 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
291 final int depth = parser.getDepth();
292 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700293 while (((type = parser.next()) != XmlPullParser.END_TAG ||
294 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800295 if ((type == XmlPullParser.START_TAG) && "grid-option".equals(parser.getName())) {
296
297 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
298 final int displayDepth = parser.getDepth();
299 while (((type = parser.next()) != XmlPullParser.END_TAG ||
300 parser.getDepth() > displayDepth)
301 && type != XmlPullParser.END_DOCUMENT) {
302 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
303 parser.getName())) {
304 profiles.add(new DisplayOption(
305 gridOption, context, Xml.asAttributeSet(parser)));
306 }
307 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700308 }
309 }
310 } catch (IOException|XmlPullParserException e) {
311 throw new RuntimeException(e);
312 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800313
314 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
315 if (!TextUtils.isEmpty(gridName)) {
316 for (DisplayOption option : profiles) {
317 if (gridName.equals(option.grid.name)) {
318 filteredProfiles.add(option);
319 }
320 }
321 }
322 if (filteredProfiles.isEmpty()) {
323 // No grid found, use the default options
324 for (DisplayOption option : profiles) {
325 if (option.canBeDefault) {
326 filteredProfiles.add(option);
327 }
328 }
329 }
330 if (filteredProfiles.isEmpty()) {
331 throw new RuntimeException("No display option with canBeDefault=true");
332 }
333 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700334 }
335
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700336 private int getLauncherIconDensity(int requiredSize) {
337 // Densities typically defined by an app.
338 int[] densityBuckets = new int[] {
339 DisplayMetrics.DENSITY_LOW,
340 DisplayMetrics.DENSITY_MEDIUM,
341 DisplayMetrics.DENSITY_TV,
342 DisplayMetrics.DENSITY_HIGH,
343 DisplayMetrics.DENSITY_XHIGH,
344 DisplayMetrics.DENSITY_XXHIGH,
345 DisplayMetrics.DENSITY_XXXHIGH
346 };
347
348 int density = DisplayMetrics.DENSITY_XXXHIGH;
349 for (int i = densityBuckets.length - 1; i >= 0; i--) {
350 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
351 / DisplayMetrics.DENSITY_DEFAULT;
352 if (expectedSize >= requiredSize) {
353 density = densityBuckets[i];
354 }
355 }
356
357 return density;
358 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700359
Adam Cohen2e6da152015-05-06 11:42:25 -0700360 /**
361 * Apply any Partner customization grid overrides.
362 *
363 * Currently we support: all apps row / column count.
364 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700365 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
366 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700367 if (p != null) {
368 p.applyInvariantDeviceProfileOverrides(this, dm);
369 }
370 }
371
Sunny Goyal415f1732018-11-29 10:33:47 -0800372 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700373 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700374 }
375
Sunny Goyal415f1732018-11-29 10:33:47 -0800376 @VisibleForTesting
377 static DisplayOption invDistWeightedInterpolate(float width, float height,
378 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700379 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700380
Sunny Goyal415f1732018-11-29 10:33:47 -0800381 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700382 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
383 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700384 }
385
Sunny Goyal415f1732018-11-29 10:33:47 -0800386 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700387 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800388 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700389 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
390 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800391 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700392 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800393 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700394 }
395
Sunny Goyal27835952017-01-13 12:15:53 -0800396 public DeviceProfile getDeviceProfile(Context context) {
397 return context.getResources().getConfiguration().orientation
398 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
399 }
400
Sunny Goyal415f1732018-11-29 10:33:47 -0800401 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700402 float d = dist(x0, y0, x1, y1);
403 if (Float.compare(d, 0f) == 0) {
404 return Float.POSITIVE_INFINITY;
405 }
406 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
407 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700408
409 /**
410 * As a ratio of screen height, the total distance we want the parallax effect to span
411 * horizontally
412 */
413 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
414 float aspectRatio = width / (float) height;
415
416 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
417 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
418 // We will use these two data points to extrapolate how much the wallpaper parallax effect
419 // to span (ie travel) at any aspect ratio:
420
421 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
422 final float ASPECT_RATIO_PORTRAIT = 10/16f;
423 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
424 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
425
426 // To find out the desired width at different aspect ratios, we use the following two
427 // formulas, where the coefficient on x is the aspect ratio (width/height):
428 // (16/10)x + y = 1.5
429 // (10/16)x + y = 1.2
430 // We solve for x and y and end up with a final formula:
431 final float x =
432 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
433 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
434 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
435 return x * aspectRatio + y;
436 }
437
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700438 public interface OnIDPChangeListener {
439
440 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
441 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800442
443
444 private static final class GridOption {
445
446 private final String name;
447 private final int numRows;
448 private final int numColumns;
449
450 private final int numFolderRows;
451 private final int numFolderColumns;
452
453 private final int numHotseatIcons;
454
455 private final int defaultLayoutId;
456 private final int demoModeLayoutId;
457
Sunny Goyal5bc18462019-01-07 15:13:39 -0800458 private final SparseArray<TypedValue> extraAttrs;
459
Sunny Goyal415f1732018-11-29 10:33:47 -0800460 GridOption(Context context, AttributeSet attrs) {
461 TypedArray a = context.obtainStyledAttributes(
462 attrs, R.styleable.GridDisplayOption);
463 name = a.getString(R.styleable.GridDisplayOption_name);
464 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
465 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
466
467 defaultLayoutId = a.getResourceId(
468 R.styleable.GridDisplayOption_defaultLayoutId, 0);
469 demoModeLayoutId = a.getResourceId(
470 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
471 numHotseatIcons = a.getInt(
472 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
473 numFolderRows = a.getInt(
474 R.styleable.GridDisplayOption_numFolderRows, numRows);
475 numFolderColumns = a.getInt(
476 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
477 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800478
479 extraAttrs = Themes.createValueMap(context, attrs,
480 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800481 }
482 }
483
484 private static final class DisplayOption {
485 private final GridOption grid;
486
487 private final String name;
488 private final float minWidthDps;
489 private final float minHeightDps;
490 private final boolean canBeDefault;
491
492 private float iconSize;
493 private float landscapeIconSize;
494 private float iconTextSize;
495
496 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
497 this.grid = grid;
498
499 TypedArray a = context.obtainStyledAttributes(
500 attrs, R.styleable.ProfileDisplayOption);
501
502 name = a.getString(R.styleable.ProfileDisplayOption_name);
503 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
504 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
505 canBeDefault = a.getBoolean(
506 R.styleable.ProfileDisplayOption_canBeDefault, false);
507
508 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
509 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
510 iconSize);
511 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
512 a.recycle();
513 }
514
515 DisplayOption() {
516 grid = null;
517 name = null;
518 minWidthDps = 0;
519 minHeightDps = 0;
520 canBeDefault = false;
521 }
522
523 private DisplayOption multiply(float w) {
524 iconSize *= w;
525 landscapeIconSize *= w;
526 iconTextSize *= w;
527 return this;
528 }
529
530 private DisplayOption add(DisplayOption p) {
531 iconSize += p.iconSize;
532 landscapeIconSize += p.landscapeIconSize;
533 iconTextSize += p.iconTextSize;
534 return this;
535 }
536 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700537}