blob: dafd5bb91b4aae2626a0aa17dea7b1cfeee3b306 [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
Sunny Goyaleff44f32019-01-09 17:29:49 -0800142 public InvariantDeviceProfile(Context context, String gridName) {
143 String newName = initGrid(context, gridName);
144 if (newName == null || !newName.equals(gridName)) {
145 throw new IllegalArgumentException("Unknown grid name");
146 }
147 }
148
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800149 /**
150 * Retrieve system defined or RRO overriden icon shape.
151 */
152 private static String getIconShapePath(Context context) {
153 if (CONFIG_ICON_MASK_RES_ID == 0) {
154 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
155 return "";
156 }
157 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
158 }
159
Sunny Goyaleff44f32019-01-09 17:29:49 -0800160 private String initGrid(Context context, String gridName) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700161 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
162 Display display = wm.getDefaultDisplay();
163 DisplayMetrics dm = new DisplayMetrics();
164 display.getMetrics(dm);
165
166 Point smallestSize = new Point();
167 Point largestSize = new Point();
168 display.getCurrentSizeRange(smallestSize, largestSize);
169
Sunny Goyal415f1732018-11-29 10:33:47 -0800170 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700171 // This guarantees that width < height
Sunny Goyal415f1732018-11-29 10:33:47 -0800172 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
173 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
174 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800175 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800176 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
177 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
178 DisplayOption interpolatedDisplayOption =
179 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700180
Sunny Goyal415f1732018-11-29 10:33:47 -0800181 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700182 numRows = closestProfile.numRows;
183 numColumns = closestProfile.numColumns;
184 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700185 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700186 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700187 numFolderRows = closestProfile.numFolderRows;
188 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800189 mExtraAttrs = closestProfile.extraAttrs;
190
Sunny Goyal415f1732018-11-29 10:33:47 -0800191 if (!closestProfile.name.equals(gridName)) {
192 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800193 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800194 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700195
Sunny Goyal415f1732018-11-29 10:33:47 -0800196 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800197 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800198 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700199 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800200 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700201 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700202
203 // If the partner customization apk contains any grid overrides, apply them
204 // Supported overrides: numRows, numColumns, iconSize
205 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700206
207 Point realSize = new Point();
208 display.getRealSize(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
214 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800215 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700216 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800217 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700218
219 // We need to ensure that there is enough extra space in the wallpaper
220 // for the intended parallax effects
221 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
222 defaultWallpaperSize = new Point(
223 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
224 largeSide);
225 } else {
226 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
227 }
Sunny Goyaleff44f32019-01-09 17:29:49 -0800228 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700229 }
230
Sunny Goyal5bc18462019-01-07 15:13:39 -0800231 @Nullable
232 public TypedValue getAttrValue(int attr) {
233 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
234 }
235
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700236 public void addOnChangeListener(OnIDPChangeListener listener) {
237 mChangeListeners.add(listener);
238 }
239
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800240 public void removeOnChangeListener(OnIDPChangeListener listener) {
241 mChangeListeners.remove(listener);
242 }
243
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700244 private void killProcess(Context context) {
245 Log.e("ConfigMonitor", "restarting launcher");
246 android.os.Process.killProcess(android.os.Process.myPid());
247 }
248
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800249 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800250 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
251 // Good place to check if grid size changed in themepicker when launcher was dead.
252 if (savedIconMaskPath.isEmpty()) {
253 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
254 .apply();
255 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
256 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
257 .apply();
258 apply(context, CHANGE_FLAG_ICON_PARAMS);
259 }
260 }
261
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800262 public void setCurrentGrid(Context context, String gridName) {
263 Context appContext = context.getApplicationContext();
264 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
265 new MainThreadExecutor().execute(() -> onConfigChanged(appContext));
266 }
267
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700268 private void onConfigChanged(Context context) {
269 // Config changes, what shall we do?
270 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
271
272 // Re-init grid
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800273 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700274
275 int changeFlags = 0;
276 if (numRows != oldProfile.numRows ||
277 numColumns != oldProfile.numColumns ||
278 numFolderColumns != oldProfile.numFolderColumns ||
279 numFolderRows != oldProfile.numFolderRows ||
280 numHotseatIcons != oldProfile.numHotseatIcons) {
281 changeFlags |= CHANGE_FLAG_GRID;
282 }
283
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800284 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
285 !iconShapePath.equals(oldProfile.iconShapePath)) {
286 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700287 }
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800288 apply(context, changeFlags);
289 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700290
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800291 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700292 // Create a new config monitor
293 mConfigMonitor.unregister();
294 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
295
296 for (OnIDPChangeListener listener : mChangeListeners) {
297 listener.onIdpChanged(changeFlags, this);
298 }
299 }
300
Sunny Goyal415f1732018-11-29 10:33:47 -0800301 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
302 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700303 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
304 final int depth = parser.getDepth();
305 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700306 while (((type = parser.next()) != XmlPullParser.END_TAG ||
307 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800308 if ((type == XmlPullParser.START_TAG)
309 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800310
311 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
312 final int displayDepth = parser.getDepth();
313 while (((type = parser.next()) != XmlPullParser.END_TAG ||
314 parser.getDepth() > displayDepth)
315 && type != XmlPullParser.END_DOCUMENT) {
316 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
317 parser.getName())) {
318 profiles.add(new DisplayOption(
319 gridOption, context, Xml.asAttributeSet(parser)));
320 }
321 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700322 }
323 }
324 } catch (IOException|XmlPullParserException e) {
325 throw new RuntimeException(e);
326 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800327
328 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
329 if (!TextUtils.isEmpty(gridName)) {
330 for (DisplayOption option : profiles) {
331 if (gridName.equals(option.grid.name)) {
332 filteredProfiles.add(option);
333 }
334 }
335 }
336 if (filteredProfiles.isEmpty()) {
337 // No grid found, use the default options
338 for (DisplayOption option : profiles) {
339 if (option.canBeDefault) {
340 filteredProfiles.add(option);
341 }
342 }
343 }
344 if (filteredProfiles.isEmpty()) {
345 throw new RuntimeException("No display option with canBeDefault=true");
346 }
347 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700348 }
349
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700350 private int getLauncherIconDensity(int requiredSize) {
351 // Densities typically defined by an app.
352 int[] densityBuckets = new int[] {
353 DisplayMetrics.DENSITY_LOW,
354 DisplayMetrics.DENSITY_MEDIUM,
355 DisplayMetrics.DENSITY_TV,
356 DisplayMetrics.DENSITY_HIGH,
357 DisplayMetrics.DENSITY_XHIGH,
358 DisplayMetrics.DENSITY_XXHIGH,
359 DisplayMetrics.DENSITY_XXXHIGH
360 };
361
362 int density = DisplayMetrics.DENSITY_XXXHIGH;
363 for (int i = densityBuckets.length - 1; i >= 0; i--) {
364 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
365 / DisplayMetrics.DENSITY_DEFAULT;
366 if (expectedSize >= requiredSize) {
367 density = densityBuckets[i];
368 }
369 }
370
371 return density;
372 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700373
Adam Cohen2e6da152015-05-06 11:42:25 -0700374 /**
375 * Apply any Partner customization grid overrides.
376 *
377 * Currently we support: all apps row / column count.
378 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700379 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
380 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700381 if (p != null) {
382 p.applyInvariantDeviceProfileOverrides(this, dm);
383 }
384 }
385
Sunny Goyal415f1732018-11-29 10:33:47 -0800386 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700387 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700388 }
389
Sunny Goyal415f1732018-11-29 10:33:47 -0800390 @VisibleForTesting
391 static DisplayOption invDistWeightedInterpolate(float width, float height,
392 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700393 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700394
Sunny Goyal415f1732018-11-29 10:33:47 -0800395 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700396 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
397 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700398 }
399
Sunny Goyal415f1732018-11-29 10:33:47 -0800400 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700401 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800402 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700403 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
404 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800405 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700406 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800407 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700408 }
409
Sunny Goyal27835952017-01-13 12:15:53 -0800410 public DeviceProfile getDeviceProfile(Context context) {
411 return context.getResources().getConfiguration().orientation
412 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
413 }
414
Sunny Goyal415f1732018-11-29 10:33:47 -0800415 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700416 float d = dist(x0, y0, x1, y1);
417 if (Float.compare(d, 0f) == 0) {
418 return Float.POSITIVE_INFINITY;
419 }
420 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
421 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700422
423 /**
424 * As a ratio of screen height, the total distance we want the parallax effect to span
425 * horizontally
426 */
427 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
428 float aspectRatio = width / (float) height;
429
430 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
431 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
432 // We will use these two data points to extrapolate how much the wallpaper parallax effect
433 // to span (ie travel) at any aspect ratio:
434
435 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
436 final float ASPECT_RATIO_PORTRAIT = 10/16f;
437 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
438 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
439
440 // To find out the desired width at different aspect ratios, we use the following two
441 // formulas, where the coefficient on x is the aspect ratio (width/height):
442 // (16/10)x + y = 1.5
443 // (10/16)x + y = 1.2
444 // We solve for x and y and end up with a final formula:
445 final float x =
446 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
447 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
448 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
449 return x * aspectRatio + y;
450 }
451
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700452 public interface OnIDPChangeListener {
453
454 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
455 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800456
457
Sunny Goyaleff44f32019-01-09 17:29:49 -0800458 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800459
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800460 public static final String TAG_NAME = "grid-option";
461
Sunny Goyaleff44f32019-01-09 17:29:49 -0800462 public final String name;
463 public final int numRows;
464 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800465
466 private final int numFolderRows;
467 private final int numFolderColumns;
468
469 private final int numHotseatIcons;
470
471 private final int defaultLayoutId;
472 private final int demoModeLayoutId;
473
Sunny Goyal5bc18462019-01-07 15:13:39 -0800474 private final SparseArray<TypedValue> extraAttrs;
475
Sunny Goyaleff44f32019-01-09 17:29:49 -0800476 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800477 TypedArray a = context.obtainStyledAttributes(
478 attrs, R.styleable.GridDisplayOption);
479 name = a.getString(R.styleable.GridDisplayOption_name);
480 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
481 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
482
483 defaultLayoutId = a.getResourceId(
484 R.styleable.GridDisplayOption_defaultLayoutId, 0);
485 demoModeLayoutId = a.getResourceId(
486 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
487 numHotseatIcons = a.getInt(
488 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
489 numFolderRows = a.getInt(
490 R.styleable.GridDisplayOption_numFolderRows, numRows);
491 numFolderColumns = a.getInt(
492 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
493 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800494
495 extraAttrs = Themes.createValueMap(context, attrs,
496 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800497 }
498 }
499
500 private static final class DisplayOption {
501 private final GridOption grid;
502
503 private final String name;
504 private final float minWidthDps;
505 private final float minHeightDps;
506 private final boolean canBeDefault;
507
508 private float iconSize;
509 private float landscapeIconSize;
510 private float iconTextSize;
511
512 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
513 this.grid = grid;
514
515 TypedArray a = context.obtainStyledAttributes(
516 attrs, R.styleable.ProfileDisplayOption);
517
518 name = a.getString(R.styleable.ProfileDisplayOption_name);
519 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
520 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
521 canBeDefault = a.getBoolean(
522 R.styleable.ProfileDisplayOption_canBeDefault, false);
523
524 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
525 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
526 iconSize);
527 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
528 a.recycle();
529 }
530
531 DisplayOption() {
532 grid = null;
533 name = null;
534 minWidthDps = 0;
535 minHeightDps = 0;
536 canBeDefault = false;
537 }
538
539 private DisplayOption multiply(float w) {
540 iconSize *= w;
541 landscapeIconSize *= w;
542 iconTextSize *= w;
543 return this;
544 }
545
546 private DisplayOption add(DisplayOption p) {
547 iconSize += p.iconSize;
548 landscapeIconSize += p.landscapeIconSize;
549 iconTextSize += p.iconTextSize;
550 return this;
551 }
552 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700553}