blob: 935dda609fc4c301b5aa259db46d81c74f0bd898 [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
232 private void killProcess(Context context) {
233 Log.e("ConfigMonitor", "restarting launcher");
234 android.os.Process.killProcess(android.os.Process.myPid());
235 }
236
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800237 public void verifyConfigChangedInBackground(final Context context) {
238
239 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
240 // Good place to check if grid size changed in themepicker when launcher was dead.
241 if (savedIconMaskPath.isEmpty()) {
242 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
243 .apply();
244 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
245 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
246 .apply();
247 apply(context, CHANGE_FLAG_ICON_PARAMS);
248 }
249 }
250
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700251 private void onConfigChanged(Context context) {
252 // Config changes, what shall we do?
253 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
254
255 // Re-init grid
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800256 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700257
258 int changeFlags = 0;
259 if (numRows != oldProfile.numRows ||
260 numColumns != oldProfile.numColumns ||
261 numFolderColumns != oldProfile.numFolderColumns ||
262 numFolderRows != oldProfile.numFolderRows ||
263 numHotseatIcons != oldProfile.numHotseatIcons) {
264 changeFlags |= CHANGE_FLAG_GRID;
265 }
266
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800267 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
268 !iconShapePath.equals(oldProfile.iconShapePath)) {
269 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700270 }
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800271 apply(context, changeFlags);
272 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700273
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800274 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700275 // Create a new config monitor
276 mConfigMonitor.unregister();
277 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
278
279 for (OnIDPChangeListener listener : mChangeListeners) {
280 listener.onIdpChanged(changeFlags, this);
281 }
282 }
283
Sunny Goyal415f1732018-11-29 10:33:47 -0800284 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
285 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700286 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
287 final int depth = parser.getDepth();
288 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700289 while (((type = parser.next()) != XmlPullParser.END_TAG ||
290 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800291 if ((type == XmlPullParser.START_TAG) && "grid-option".equals(parser.getName())) {
292
293 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
294 final int displayDepth = parser.getDepth();
295 while (((type = parser.next()) != XmlPullParser.END_TAG ||
296 parser.getDepth() > displayDepth)
297 && type != XmlPullParser.END_DOCUMENT) {
298 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
299 parser.getName())) {
300 profiles.add(new DisplayOption(
301 gridOption, context, Xml.asAttributeSet(parser)));
302 }
303 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700304 }
305 }
306 } catch (IOException|XmlPullParserException e) {
307 throw new RuntimeException(e);
308 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800309
310 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
311 if (!TextUtils.isEmpty(gridName)) {
312 for (DisplayOption option : profiles) {
313 if (gridName.equals(option.grid.name)) {
314 filteredProfiles.add(option);
315 }
316 }
317 }
318 if (filteredProfiles.isEmpty()) {
319 // No grid found, use the default options
320 for (DisplayOption option : profiles) {
321 if (option.canBeDefault) {
322 filteredProfiles.add(option);
323 }
324 }
325 }
326 if (filteredProfiles.isEmpty()) {
327 throw new RuntimeException("No display option with canBeDefault=true");
328 }
329 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700330 }
331
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700332 private int getLauncherIconDensity(int requiredSize) {
333 // Densities typically defined by an app.
334 int[] densityBuckets = new int[] {
335 DisplayMetrics.DENSITY_LOW,
336 DisplayMetrics.DENSITY_MEDIUM,
337 DisplayMetrics.DENSITY_TV,
338 DisplayMetrics.DENSITY_HIGH,
339 DisplayMetrics.DENSITY_XHIGH,
340 DisplayMetrics.DENSITY_XXHIGH,
341 DisplayMetrics.DENSITY_XXXHIGH
342 };
343
344 int density = DisplayMetrics.DENSITY_XXXHIGH;
345 for (int i = densityBuckets.length - 1; i >= 0; i--) {
346 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
347 / DisplayMetrics.DENSITY_DEFAULT;
348 if (expectedSize >= requiredSize) {
349 density = densityBuckets[i];
350 }
351 }
352
353 return density;
354 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700355
Adam Cohen2e6da152015-05-06 11:42:25 -0700356 /**
357 * Apply any Partner customization grid overrides.
358 *
359 * Currently we support: all apps row / column count.
360 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700361 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
362 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700363 if (p != null) {
364 p.applyInvariantDeviceProfileOverrides(this, dm);
365 }
366 }
367
Sunny Goyal415f1732018-11-29 10:33:47 -0800368 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700369 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700370 }
371
Sunny Goyal415f1732018-11-29 10:33:47 -0800372 @VisibleForTesting
373 static DisplayOption invDistWeightedInterpolate(float width, float height,
374 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700375 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700376
Sunny Goyal415f1732018-11-29 10:33:47 -0800377 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700378 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
379 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700380 }
381
Sunny Goyal415f1732018-11-29 10:33:47 -0800382 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700383 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800384 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700385 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
386 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800387 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700388 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800389 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700390 }
391
Sunny Goyal27835952017-01-13 12:15:53 -0800392 public DeviceProfile getDeviceProfile(Context context) {
393 return context.getResources().getConfiguration().orientation
394 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
395 }
396
Sunny Goyal415f1732018-11-29 10:33:47 -0800397 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700398 float d = dist(x0, y0, x1, y1);
399 if (Float.compare(d, 0f) == 0) {
400 return Float.POSITIVE_INFINITY;
401 }
402 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
403 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700404
405 /**
406 * As a ratio of screen height, the total distance we want the parallax effect to span
407 * horizontally
408 */
409 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
410 float aspectRatio = width / (float) height;
411
412 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
413 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
414 // We will use these two data points to extrapolate how much the wallpaper parallax effect
415 // to span (ie travel) at any aspect ratio:
416
417 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
418 final float ASPECT_RATIO_PORTRAIT = 10/16f;
419 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
420 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
421
422 // To find out the desired width at different aspect ratios, we use the following two
423 // formulas, where the coefficient on x is the aspect ratio (width/height):
424 // (16/10)x + y = 1.5
425 // (10/16)x + y = 1.2
426 // We solve for x and y and end up with a final formula:
427 final float x =
428 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
429 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
430 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
431 return x * aspectRatio + y;
432 }
433
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700434 public interface OnIDPChangeListener {
435
436 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
437 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800438
439
440 private static final class GridOption {
441
442 private final String name;
443 private final int numRows;
444 private final int numColumns;
445
446 private final int numFolderRows;
447 private final int numFolderColumns;
448
449 private final int numHotseatIcons;
450
451 private final int defaultLayoutId;
452 private final int demoModeLayoutId;
453
Sunny Goyal5bc18462019-01-07 15:13:39 -0800454 private final SparseArray<TypedValue> extraAttrs;
455
Sunny Goyal415f1732018-11-29 10:33:47 -0800456 GridOption(Context context, AttributeSet attrs) {
457 TypedArray a = context.obtainStyledAttributes(
458 attrs, R.styleable.GridDisplayOption);
459 name = a.getString(R.styleable.GridDisplayOption_name);
460 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
461 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
462
463 defaultLayoutId = a.getResourceId(
464 R.styleable.GridDisplayOption_defaultLayoutId, 0);
465 demoModeLayoutId = a.getResourceId(
466 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
467 numHotseatIcons = a.getInt(
468 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
469 numFolderRows = a.getInt(
470 R.styleable.GridDisplayOption_numFolderRows, numRows);
471 numFolderColumns = a.getInt(
472 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
473 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800474
475 extraAttrs = Themes.createValueMap(context, attrs,
476 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800477 }
478 }
479
480 private static final class DisplayOption {
481 private final GridOption grid;
482
483 private final String name;
484 private final float minWidthDps;
485 private final float minHeightDps;
486 private final boolean canBeDefault;
487
488 private float iconSize;
489 private float landscapeIconSize;
490 private float iconTextSize;
491
492 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
493 this.grid = grid;
494
495 TypedArray a = context.obtainStyledAttributes(
496 attrs, R.styleable.ProfileDisplayOption);
497
498 name = a.getString(R.styleable.ProfileDisplayOption_name);
499 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
500 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
501 canBeDefault = a.getBoolean(
502 R.styleable.ProfileDisplayOption_canBeDefault, false);
503
504 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
505 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
506 iconSize);
507 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
508 a.recycle();
509 }
510
511 DisplayOption() {
512 grid = null;
513 name = null;
514 minWidthDps = 0;
515 minHeightDps = 0;
516 canBeDefault = false;
517 }
518
519 private DisplayOption multiply(float w) {
520 iconSize *= w;
521 landscapeIconSize *= w;
522 iconTextSize *= w;
523 return this;
524 }
525
526 private DisplayOption add(DisplayOption p) {
527 iconSize += p.iconSize;
528 landscapeIconSize += p.landscapeIconSize;
529 iconTextSize += p.iconTextSize;
530 return this;
531 }
532 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700533}