blob: 6582df2b78b4ea02a549adec8306cee136e73fd4 [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 Goyal90e3fbc2019-01-23 16:42:43 -080039import com.android.launcher3.folder.FolderShape;
Sunny Goyald0e360a2018-06-29 14:40:18 -070040import com.android.launcher3.util.ConfigMonitor;
Sunny Goyal5bc18462019-01-07 15:13:39 -080041import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070042import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080043import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070044
Sunny Goyal819e1932016-07-07 16:43:58 -070045import org.xmlpull.v1.XmlPullParser;
46import org.xmlpull.v1.XmlPullParserException;
47
48import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070049import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080050import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070051
Sunny Goyal5bc18462019-01-07 15:13:39 -080052import androidx.annotation.Nullable;
Sunny Goyald2303072018-08-14 15:21:45 -070053import androidx.annotation.VisibleForTesting;
54
Adam Cohen2e6da152015-05-06 11:42:25 -070055public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070056
Hyunyoung Songc55a3502018-12-04 15:43:16 -080057 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070058 // We do not need any synchronization for this variable as its only written on UI thread.
59 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070060 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070061
Hyunyoung Songc55a3502018-12-04 15:43:16 -080062 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080063
Sunny Goyal53d7ee42015-05-22 12:25:45 -070064 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
65
Sunny Goyal87dc48b2018-10-12 11:42:33 -070066 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080067 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
68
69 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070070
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070071 // Constants that affects the interpolation curve between statically defined device profile
72 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080073 private static final float KNEARESTNEIGHBOR = 3;
74 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070075
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070076 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080077 private static final float WEIGHT_EFFICIENT = 100000f;
78
79 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
80 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070081
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070082 /**
83 * Number of icons per row and column in the workspace.
84 */
Adam Cohen2e6da152015-05-06 11:42:25 -070085 public int numRows;
86 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070087
88 /**
89 * Number of icons per row and column in the folder.
90 */
Adam Cohen2e6da152015-05-06 11:42:25 -070091 public int numFolderRows;
92 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070093 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080094 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070095 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070096 public int iconBitmapSize;
97 public int fillResIconDpi;
98 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099
Sunny Goyal5bc18462019-01-07 15:13:39 -0800100 private SparseArray<TypedValue> mExtraAttrs;
101
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700102 /**
103 * Number of icons inside the hotseat area.
104 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800105 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700106
Sunny Goyal415f1732018-11-29 10:33:47 -0800107 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700108 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700109
cuijiaxingabda8d72017-03-20 09:51:36 -0700110 public DeviceProfile landscapeProfile;
111 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700112
Sunny Goyal6f866092016-03-17 17:04:15 -0700113 public Point defaultWallpaperSize;
114
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700115 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
116 private ConfigMonitor mConfigMonitor;
117
Sunny Goyalf633ef52018-03-13 09:57:05 -0700118 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700119 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700120
Sunny Goyalf633ef52018-03-13 09:57:05 -0700121 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800122 numRows = p.numRows;
123 numColumns = p.numColumns;
124 numFolderRows = p.numFolderRows;
125 numFolderColumns = p.numFolderColumns;
126 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800127 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800128 landscapeIconSize = p.landscapeIconSize;
129 iconTextSize = p.iconTextSize;
130 numHotseatIcons = p.numHotseatIcons;
131 defaultLayoutId = p.defaultLayoutId;
132 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800133 mExtraAttrs = p.mExtraAttrs;
Adam Cohen2e6da152015-05-06 11:42:25 -0700134 }
135
Sunny Goyalbbf01842015-10-08 07:41:15 -0700136 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700137 private InvariantDeviceProfile(Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800138 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700139 mConfigMonitor = new ConfigMonitor(context,
140 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
141 }
142
Sunny Goyaleff44f32019-01-09 17:29:49 -0800143 public InvariantDeviceProfile(Context context, String gridName) {
144 String newName = initGrid(context, gridName);
145 if (newName == null || !newName.equals(gridName)) {
146 throw new IllegalArgumentException("Unknown grid name");
147 }
148 }
149
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800150 /**
151 * Retrieve system defined or RRO overriden icon shape.
152 */
153 private static String getIconShapePath(Context context) {
154 if (CONFIG_ICON_MASK_RES_ID == 0) {
155 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
156 return "";
157 }
158 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
159 }
160
Sunny Goyaleff44f32019-01-09 17:29:49 -0800161 private String initGrid(Context context, String gridName) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700162 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
163 Display display = wm.getDefaultDisplay();
164 DisplayMetrics dm = new DisplayMetrics();
165 display.getMetrics(dm);
166
167 Point smallestSize = new Point();
168 Point largestSize = new Point();
169 display.getCurrentSizeRange(smallestSize, largestSize);
170
Sunny Goyal415f1732018-11-29 10:33:47 -0800171 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700172 // This guarantees that width < height
Sunny Goyal415f1732018-11-29 10:33:47 -0800173 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
174 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
175 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800176 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800177 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
178 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
179 DisplayOption interpolatedDisplayOption =
180 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700181
Sunny Goyal415f1732018-11-29 10:33:47 -0800182 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700183 numRows = closestProfile.numRows;
184 numColumns = closestProfile.numColumns;
185 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700186 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700187 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700188 numFolderRows = closestProfile.numFolderRows;
189 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800190 mExtraAttrs = closestProfile.extraAttrs;
191
Sunny Goyal415f1732018-11-29 10:33:47 -0800192 if (!closestProfile.name.equals(gridName)) {
193 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800194 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800195 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700196
Sunny Goyal415f1732018-11-29 10:33:47 -0800197 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800198 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800199 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700200 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800201 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700202 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700203
204 // If the partner customization apk contains any grid overrides, apply them
205 // Supported overrides: numRows, numColumns, iconSize
206 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700207
208 Point realSize = new Point();
209 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700210 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700211 // same in any orientation.
212 int smallSide = Math.min(realSize.x, realSize.y);
213 int largeSide = Math.max(realSize.x, realSize.y);
214
215 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800216 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700217 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800218 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700219
220 // We need to ensure that there is enough extra space in the wallpaper
221 // for the intended parallax effects
222 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
223 defaultWallpaperSize = new Point(
224 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
225 largeSide);
226 } else {
227 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
228 }
Sunny Goyaleff44f32019-01-09 17:29:49 -0800229 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700230 }
231
Sunny Goyal5bc18462019-01-07 15:13:39 -0800232 @Nullable
233 public TypedValue getAttrValue(int attr) {
234 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
235 }
236
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700237 public void addOnChangeListener(OnIDPChangeListener listener) {
238 mChangeListeners.add(listener);
239 }
240
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800241 public void removeOnChangeListener(OnIDPChangeListener listener) {
242 mChangeListeners.remove(listener);
243 }
244
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700245 private void killProcess(Context context) {
246 Log.e("ConfigMonitor", "restarting launcher");
247 android.os.Process.killProcess(android.os.Process.myPid());
248 }
249
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800250 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800251 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
252 // Good place to check if grid size changed in themepicker when launcher was dead.
253 if (savedIconMaskPath.isEmpty()) {
254 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
255 .apply();
256 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
257 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
258 .apply();
259 apply(context, CHANGE_FLAG_ICON_PARAMS);
260 }
261 }
262
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800263 public void setCurrentGrid(Context context, String gridName) {
264 Context appContext = context.getApplicationContext();
265 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
266 new MainThreadExecutor().execute(() -> onConfigChanged(appContext));
267 }
268
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700269 private void onConfigChanged(Context context) {
270 // Config changes, what shall we do?
271 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
272
273 // Re-init grid
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800274 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700275
276 int changeFlags = 0;
277 if (numRows != oldProfile.numRows ||
278 numColumns != oldProfile.numColumns ||
279 numFolderColumns != oldProfile.numFolderColumns ||
280 numFolderRows != oldProfile.numFolderRows ||
281 numHotseatIcons != oldProfile.numHotseatIcons) {
282 changeFlags |= CHANGE_FLAG_GRID;
283 }
284
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800285 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
286 !iconShapePath.equals(oldProfile.iconShapePath)) {
287 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700288 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800289 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
290 FolderShape.init(context);
291 }
292
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800293 apply(context, changeFlags);
294 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700295
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800296 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700297 // Create a new config monitor
298 mConfigMonitor.unregister();
299 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
300
301 for (OnIDPChangeListener listener : mChangeListeners) {
302 listener.onIdpChanged(changeFlags, this);
303 }
304 }
305
Sunny Goyal415f1732018-11-29 10:33:47 -0800306 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
307 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700308 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
309 final int depth = parser.getDepth();
310 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700311 while (((type = parser.next()) != XmlPullParser.END_TAG ||
312 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800313 if ((type == XmlPullParser.START_TAG)
314 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800315
316 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
317 final int displayDepth = parser.getDepth();
318 while (((type = parser.next()) != XmlPullParser.END_TAG ||
319 parser.getDepth() > displayDepth)
320 && type != XmlPullParser.END_DOCUMENT) {
321 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
322 parser.getName())) {
323 profiles.add(new DisplayOption(
324 gridOption, context, Xml.asAttributeSet(parser)));
325 }
326 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700327 }
328 }
329 } catch (IOException|XmlPullParserException e) {
330 throw new RuntimeException(e);
331 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800332
333 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
334 if (!TextUtils.isEmpty(gridName)) {
335 for (DisplayOption option : profiles) {
336 if (gridName.equals(option.grid.name)) {
337 filteredProfiles.add(option);
338 }
339 }
340 }
341 if (filteredProfiles.isEmpty()) {
342 // No grid found, use the default options
343 for (DisplayOption option : profiles) {
344 if (option.canBeDefault) {
345 filteredProfiles.add(option);
346 }
347 }
348 }
349 if (filteredProfiles.isEmpty()) {
350 throw new RuntimeException("No display option with canBeDefault=true");
351 }
352 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700353 }
354
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700355 private int getLauncherIconDensity(int requiredSize) {
356 // Densities typically defined by an app.
357 int[] densityBuckets = new int[] {
358 DisplayMetrics.DENSITY_LOW,
359 DisplayMetrics.DENSITY_MEDIUM,
360 DisplayMetrics.DENSITY_TV,
361 DisplayMetrics.DENSITY_HIGH,
362 DisplayMetrics.DENSITY_XHIGH,
363 DisplayMetrics.DENSITY_XXHIGH,
364 DisplayMetrics.DENSITY_XXXHIGH
365 };
366
367 int density = DisplayMetrics.DENSITY_XXXHIGH;
368 for (int i = densityBuckets.length - 1; i >= 0; i--) {
369 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
370 / DisplayMetrics.DENSITY_DEFAULT;
371 if (expectedSize >= requiredSize) {
372 density = densityBuckets[i];
373 }
374 }
375
376 return density;
377 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700378
Adam Cohen2e6da152015-05-06 11:42:25 -0700379 /**
380 * Apply any Partner customization grid overrides.
381 *
382 * Currently we support: all apps row / column count.
383 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700384 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
385 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700386 if (p != null) {
387 p.applyInvariantDeviceProfileOverrides(this, dm);
388 }
389 }
390
Sunny Goyal415f1732018-11-29 10:33:47 -0800391 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700392 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700393 }
394
Sunny Goyal415f1732018-11-29 10:33:47 -0800395 @VisibleForTesting
396 static DisplayOption invDistWeightedInterpolate(float width, float height,
397 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700398 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700399
Sunny Goyal415f1732018-11-29 10:33:47 -0800400 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700401 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
402 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700403 }
404
Sunny Goyal415f1732018-11-29 10:33:47 -0800405 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700406 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800407 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700408 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
409 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800410 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700411 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800412 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700413 }
414
Sunny Goyal27835952017-01-13 12:15:53 -0800415 public DeviceProfile getDeviceProfile(Context context) {
416 return context.getResources().getConfiguration().orientation
417 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
418 }
419
Sunny Goyal415f1732018-11-29 10:33:47 -0800420 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700421 float d = dist(x0, y0, x1, y1);
422 if (Float.compare(d, 0f) == 0) {
423 return Float.POSITIVE_INFINITY;
424 }
425 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
426 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700427
428 /**
429 * As a ratio of screen height, the total distance we want the parallax effect to span
430 * horizontally
431 */
432 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
433 float aspectRatio = width / (float) height;
434
435 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
436 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
437 // We will use these two data points to extrapolate how much the wallpaper parallax effect
438 // to span (ie travel) at any aspect ratio:
439
440 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
441 final float ASPECT_RATIO_PORTRAIT = 10/16f;
442 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
443 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
444
445 // To find out the desired width at different aspect ratios, we use the following two
446 // formulas, where the coefficient on x is the aspect ratio (width/height):
447 // (16/10)x + y = 1.5
448 // (10/16)x + y = 1.2
449 // We solve for x and y and end up with a final formula:
450 final float x =
451 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
452 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
453 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
454 return x * aspectRatio + y;
455 }
456
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700457 public interface OnIDPChangeListener {
458
459 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
460 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800461
462
Sunny Goyaleff44f32019-01-09 17:29:49 -0800463 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800464
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800465 public static final String TAG_NAME = "grid-option";
466
Sunny Goyaleff44f32019-01-09 17:29:49 -0800467 public final String name;
468 public final int numRows;
469 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800470
471 private final int numFolderRows;
472 private final int numFolderColumns;
473
474 private final int numHotseatIcons;
475
476 private final int defaultLayoutId;
477 private final int demoModeLayoutId;
478
Sunny Goyal5bc18462019-01-07 15:13:39 -0800479 private final SparseArray<TypedValue> extraAttrs;
480
Sunny Goyaleff44f32019-01-09 17:29:49 -0800481 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800482 TypedArray a = context.obtainStyledAttributes(
483 attrs, R.styleable.GridDisplayOption);
484 name = a.getString(R.styleable.GridDisplayOption_name);
485 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
486 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
487
488 defaultLayoutId = a.getResourceId(
489 R.styleable.GridDisplayOption_defaultLayoutId, 0);
490 demoModeLayoutId = a.getResourceId(
491 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
492 numHotseatIcons = a.getInt(
493 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
494 numFolderRows = a.getInt(
495 R.styleable.GridDisplayOption_numFolderRows, numRows);
496 numFolderColumns = a.getInt(
497 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
498 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800499
500 extraAttrs = Themes.createValueMap(context, attrs,
501 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800502 }
503 }
504
505 private static final class DisplayOption {
506 private final GridOption grid;
507
508 private final String name;
509 private final float minWidthDps;
510 private final float minHeightDps;
511 private final boolean canBeDefault;
512
513 private float iconSize;
514 private float landscapeIconSize;
515 private float iconTextSize;
516
517 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
518 this.grid = grid;
519
520 TypedArray a = context.obtainStyledAttributes(
521 attrs, R.styleable.ProfileDisplayOption);
522
523 name = a.getString(R.styleable.ProfileDisplayOption_name);
524 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
525 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
526 canBeDefault = a.getBoolean(
527 R.styleable.ProfileDisplayOption_canBeDefault, false);
528
529 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
530 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
531 iconSize);
532 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
533 a.recycle();
534 }
535
536 DisplayOption() {
537 grid = null;
538 name = null;
539 minWidthDps = 0;
540 minHeightDps = 0;
541 canBeDefault = false;
542 }
543
544 private DisplayOption multiply(float w) {
545 iconSize *= w;
546 landscapeIconSize *= w;
547 iconTextSize *= w;
548 return this;
549 }
550
551 private DisplayOption add(DisplayOption p) {
552 iconSize += p.iconSize;
553 landscapeIconSize += p.landscapeIconSize;
554 iconTextSize += p.iconTextSize;
555 return this;
556 }
557 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700558}