blob: 45bdea88a0018747d0d1d73c7099cd72a14e76ed [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) {
250
251 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 Goyal87dc48b2018-10-12 11:42:33 -0700263 private void onConfigChanged(Context context) {
264 // Config changes, what shall we do?
265 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
266
267 // Re-init grid
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800268 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700269
270 int changeFlags = 0;
271 if (numRows != oldProfile.numRows ||
272 numColumns != oldProfile.numColumns ||
273 numFolderColumns != oldProfile.numFolderColumns ||
274 numFolderRows != oldProfile.numFolderRows ||
275 numHotseatIcons != oldProfile.numHotseatIcons) {
276 changeFlags |= CHANGE_FLAG_GRID;
277 }
278
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800279 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
280 !iconShapePath.equals(oldProfile.iconShapePath)) {
281 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700282 }
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800283 apply(context, changeFlags);
284 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700285
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800286 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700287 // Create a new config monitor
288 mConfigMonitor.unregister();
289 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
290
291 for (OnIDPChangeListener listener : mChangeListeners) {
292 listener.onIdpChanged(changeFlags, this);
293 }
294 }
295
Sunny Goyal415f1732018-11-29 10:33:47 -0800296 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
297 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700298 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
299 final int depth = parser.getDepth();
300 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700301 while (((type = parser.next()) != XmlPullParser.END_TAG ||
302 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800303 if ((type == XmlPullParser.START_TAG) && "grid-option".equals(parser.getName())) {
304
305 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
306 final int displayDepth = parser.getDepth();
307 while (((type = parser.next()) != XmlPullParser.END_TAG ||
308 parser.getDepth() > displayDepth)
309 && type != XmlPullParser.END_DOCUMENT) {
310 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
311 parser.getName())) {
312 profiles.add(new DisplayOption(
313 gridOption, context, Xml.asAttributeSet(parser)));
314 }
315 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700316 }
317 }
318 } catch (IOException|XmlPullParserException e) {
319 throw new RuntimeException(e);
320 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800321
322 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
323 if (!TextUtils.isEmpty(gridName)) {
324 for (DisplayOption option : profiles) {
325 if (gridName.equals(option.grid.name)) {
326 filteredProfiles.add(option);
327 }
328 }
329 }
330 if (filteredProfiles.isEmpty()) {
331 // No grid found, use the default options
332 for (DisplayOption option : profiles) {
333 if (option.canBeDefault) {
334 filteredProfiles.add(option);
335 }
336 }
337 }
338 if (filteredProfiles.isEmpty()) {
339 throw new RuntimeException("No display option with canBeDefault=true");
340 }
341 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700342 }
343
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700344 private int getLauncherIconDensity(int requiredSize) {
345 // Densities typically defined by an app.
346 int[] densityBuckets = new int[] {
347 DisplayMetrics.DENSITY_LOW,
348 DisplayMetrics.DENSITY_MEDIUM,
349 DisplayMetrics.DENSITY_TV,
350 DisplayMetrics.DENSITY_HIGH,
351 DisplayMetrics.DENSITY_XHIGH,
352 DisplayMetrics.DENSITY_XXHIGH,
353 DisplayMetrics.DENSITY_XXXHIGH
354 };
355
356 int density = DisplayMetrics.DENSITY_XXXHIGH;
357 for (int i = densityBuckets.length - 1; i >= 0; i--) {
358 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
359 / DisplayMetrics.DENSITY_DEFAULT;
360 if (expectedSize >= requiredSize) {
361 density = densityBuckets[i];
362 }
363 }
364
365 return density;
366 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700367
Adam Cohen2e6da152015-05-06 11:42:25 -0700368 /**
369 * Apply any Partner customization grid overrides.
370 *
371 * Currently we support: all apps row / column count.
372 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700373 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
374 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700375 if (p != null) {
376 p.applyInvariantDeviceProfileOverrides(this, dm);
377 }
378 }
379
Sunny Goyal415f1732018-11-29 10:33:47 -0800380 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700381 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700382 }
383
Sunny Goyal415f1732018-11-29 10:33:47 -0800384 @VisibleForTesting
385 static DisplayOption invDistWeightedInterpolate(float width, float height,
386 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700387 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700388
Sunny Goyal415f1732018-11-29 10:33:47 -0800389 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700390 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
391 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700392 }
393
Sunny Goyal415f1732018-11-29 10:33:47 -0800394 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700395 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800396 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700397 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
398 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800399 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700400 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800401 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700402 }
403
Sunny Goyal27835952017-01-13 12:15:53 -0800404 public DeviceProfile getDeviceProfile(Context context) {
405 return context.getResources().getConfiguration().orientation
406 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
407 }
408
Sunny Goyal415f1732018-11-29 10:33:47 -0800409 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700410 float d = dist(x0, y0, x1, y1);
411 if (Float.compare(d, 0f) == 0) {
412 return Float.POSITIVE_INFINITY;
413 }
414 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
415 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700416
417 /**
418 * As a ratio of screen height, the total distance we want the parallax effect to span
419 * horizontally
420 */
421 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
422 float aspectRatio = width / (float) height;
423
424 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
425 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
426 // We will use these two data points to extrapolate how much the wallpaper parallax effect
427 // to span (ie travel) at any aspect ratio:
428
429 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
430 final float ASPECT_RATIO_PORTRAIT = 10/16f;
431 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
432 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
433
434 // To find out the desired width at different aspect ratios, we use the following two
435 // formulas, where the coefficient on x is the aspect ratio (width/height):
436 // (16/10)x + y = 1.5
437 // (10/16)x + y = 1.2
438 // We solve for x and y and end up with a final formula:
439 final float x =
440 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
441 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
442 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
443 return x * aspectRatio + y;
444 }
445
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700446 public interface OnIDPChangeListener {
447
448 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
449 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800450
451
Sunny Goyaleff44f32019-01-09 17:29:49 -0800452 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800453
Sunny Goyaleff44f32019-01-09 17:29:49 -0800454 public final String name;
455 public final int numRows;
456 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800457
458 private final int numFolderRows;
459 private final int numFolderColumns;
460
461 private final int numHotseatIcons;
462
463 private final int defaultLayoutId;
464 private final int demoModeLayoutId;
465
Sunny Goyal5bc18462019-01-07 15:13:39 -0800466 private final SparseArray<TypedValue> extraAttrs;
467
Sunny Goyaleff44f32019-01-09 17:29:49 -0800468 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800469 TypedArray a = context.obtainStyledAttributes(
470 attrs, R.styleable.GridDisplayOption);
471 name = a.getString(R.styleable.GridDisplayOption_name);
472 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
473 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
474
475 defaultLayoutId = a.getResourceId(
476 R.styleable.GridDisplayOption_defaultLayoutId, 0);
477 demoModeLayoutId = a.getResourceId(
478 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
479 numHotseatIcons = a.getInt(
480 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
481 numFolderRows = a.getInt(
482 R.styleable.GridDisplayOption_numFolderRows, numRows);
483 numFolderColumns = a.getInt(
484 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
485 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800486
487 extraAttrs = Themes.createValueMap(context, attrs,
488 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800489 }
490 }
491
492 private static final class DisplayOption {
493 private final GridOption grid;
494
495 private final String name;
496 private final float minWidthDps;
497 private final float minHeightDps;
498 private final boolean canBeDefault;
499
500 private float iconSize;
501 private float landscapeIconSize;
502 private float iconTextSize;
503
504 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
505 this.grid = grid;
506
507 TypedArray a = context.obtainStyledAttributes(
508 attrs, R.styleable.ProfileDisplayOption);
509
510 name = a.getString(R.styleable.ProfileDisplayOption_name);
511 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
512 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
513 canBeDefault = a.getBoolean(
514 R.styleable.ProfileDisplayOption_canBeDefault, false);
515
516 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
517 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
518 iconSize);
519 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
520 a.recycle();
521 }
522
523 DisplayOption() {
524 grid = null;
525 name = null;
526 minWidthDps = 0;
527 minHeightDps = 0;
528 canBeDefault = false;
529 }
530
531 private DisplayOption multiply(float w) {
532 iconSize *= w;
533 landscapeIconSize *= w;
534 iconTextSize *= w;
535 return this;
536 }
537
538 private DisplayOption add(DisplayOption p) {
539 iconSize += p.iconSize;
540 landscapeIconSize += p.landscapeIconSize;
541 iconTextSize += p.iconTextSize;
542 return this;
543 }
544 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700545}