blob: 5c842a57ddc897bf7eb684347768c38ac9dfda79 [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 Goyal819e1932016-07-07 16:43:58 -070033import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import android.view.Display;
35import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070036
Sunny Goyald0e360a2018-06-29 14:40:18 -070037import com.android.launcher3.util.ConfigMonitor;
38import com.android.launcher3.util.MainThreadInitializedObject;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070039
Sunny Goyal819e1932016-07-07 16:43:58 -070040import org.xmlpull.v1.XmlPullParser;
41import org.xmlpull.v1.XmlPullParserException;
42
43import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070044import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080045import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070046
Sunny Goyald2303072018-08-14 15:21:45 -070047import androidx.annotation.VisibleForTesting;
48
Adam Cohen2e6da152015-05-06 11:42:25 -070049public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070050
Hyunyoung Songc55a3502018-12-04 15:43:16 -080051 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070052 // We do not need any synchronization for this variable as its only written on UI thread.
53 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070054 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070055
Hyunyoung Songc55a3502018-12-04 15:43:16 -080056 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080057
Sunny Goyal53d7ee42015-05-22 12:25:45 -070058 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
59
Sunny Goyal87dc48b2018-10-12 11:42:33 -070060 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080061 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
62
63 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070064
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070065 // Constants that affects the interpolation curve between statically defined device profile
66 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080067 private static final float KNEARESTNEIGHBOR = 3;
68 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070069
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070070 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080071 private static final float WEIGHT_EFFICIENT = 100000f;
72
73 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
74 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070075
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070076 /**
77 * Number of icons per row and column in the workspace.
78 */
Adam Cohen2e6da152015-05-06 11:42:25 -070079 public int numRows;
80 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070081
82 /**
83 * Number of icons per row and column in the folder.
84 */
Adam Cohen2e6da152015-05-06 11:42:25 -070085 public int numFolderRows;
86 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070087 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080088 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070089 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070090 public int iconBitmapSize;
91 public int fillResIconDpi;
92 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070093
94 /**
95 * Number of icons inside the hotseat area.
96 */
Sunny Goyalf862a262015-12-14 14:27:38 -080097 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -070098
Sunny Goyal415f1732018-11-29 10:33:47 -080099 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700100 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700101
cuijiaxingabda8d72017-03-20 09:51:36 -0700102 public DeviceProfile landscapeProfile;
103 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700104
Sunny Goyal6f866092016-03-17 17:04:15 -0700105 public Point defaultWallpaperSize;
106
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700107 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
108 private ConfigMonitor mConfigMonitor;
109
Sunny Goyalf633ef52018-03-13 09:57:05 -0700110 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700111 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700112
Sunny Goyalf633ef52018-03-13 09:57:05 -0700113 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800114 numRows = p.numRows;
115 numColumns = p.numColumns;
116 numFolderRows = p.numFolderRows;
117 numFolderColumns = p.numFolderColumns;
118 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800119 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800120 landscapeIconSize = p.landscapeIconSize;
121 iconTextSize = p.iconTextSize;
122 numHotseatIcons = p.numHotseatIcons;
123 defaultLayoutId = p.defaultLayoutId;
124 demoModeLayoutId = p.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700125 }
126
Sunny Goyalbbf01842015-10-08 07:41:15 -0700127 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700128 private InvariantDeviceProfile(Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800129 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700130 mConfigMonitor = new ConfigMonitor(context,
131 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
132 }
133
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800134 /**
135 * Retrieve system defined or RRO overriden icon shape.
136 */
137 private static String getIconShapePath(Context context) {
138 if (CONFIG_ICON_MASK_RES_ID == 0) {
139 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
140 return "";
141 }
142 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
143 }
144
Sunny Goyal415f1732018-11-29 10:33:47 -0800145 private void initGrid(Context context, String gridName) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700146 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
147 Display display = wm.getDefaultDisplay();
148 DisplayMetrics dm = new DisplayMetrics();
149 display.getMetrics(dm);
150
151 Point smallestSize = new Point();
152 Point largestSize = new Point();
153 display.getCurrentSizeRange(smallestSize, largestSize);
154
Sunny Goyal415f1732018-11-29 10:33:47 -0800155 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700156 // This guarantees that width < height
Sunny Goyal415f1732018-11-29 10:33:47 -0800157 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
158 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
159 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800160 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800161 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
162 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
163 DisplayOption interpolatedDisplayOption =
164 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700165
Sunny Goyal415f1732018-11-29 10:33:47 -0800166 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700167 numRows = closestProfile.numRows;
168 numColumns = closestProfile.numColumns;
169 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700170 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700171 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700172 numFolderRows = closestProfile.numFolderRows;
173 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800174 if (!closestProfile.name.equals(gridName)) {
175 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800176 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800177 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700178
Sunny Goyal415f1732018-11-29 10:33:47 -0800179 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800180 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800181 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700182 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800183 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700184 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700185
186 // If the partner customization apk contains any grid overrides, apply them
187 // Supported overrides: numRows, numColumns, iconSize
188 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700189
190 Point realSize = new Point();
191 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700192 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700193 // same in any orientation.
194 int smallSide = Math.min(realSize.x, realSize.y);
195 int largeSide = Math.max(realSize.x, realSize.y);
196
197 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800198 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700199 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800200 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700201
202 // We need to ensure that there is enough extra space in the wallpaper
203 // for the intended parallax effects
204 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
205 defaultWallpaperSize = new Point(
206 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
207 largeSide);
208 } else {
209 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
210 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700211 }
212
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700213 public void addOnChangeListener(OnIDPChangeListener listener) {
214 mChangeListeners.add(listener);
215 }
216
217 private void killProcess(Context context) {
218 Log.e("ConfigMonitor", "restarting launcher");
219 android.os.Process.killProcess(android.os.Process.myPid());
220 }
221
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800222 public void verifyConfigChangedInBackground(final Context context) {
223
224 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
225 // Good place to check if grid size changed in themepicker when launcher was dead.
226 if (savedIconMaskPath.isEmpty()) {
227 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
228 .apply();
229 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
230 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
231 .apply();
232 apply(context, CHANGE_FLAG_ICON_PARAMS);
233 }
234 }
235
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700236 private void onConfigChanged(Context context) {
237 // Config changes, what shall we do?
238 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
239
240 // Re-init grid
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800241 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700242
243 int changeFlags = 0;
244 if (numRows != oldProfile.numRows ||
245 numColumns != oldProfile.numColumns ||
246 numFolderColumns != oldProfile.numFolderColumns ||
247 numFolderRows != oldProfile.numFolderRows ||
248 numHotseatIcons != oldProfile.numHotseatIcons) {
249 changeFlags |= CHANGE_FLAG_GRID;
250 }
251
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800252 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
253 !iconShapePath.equals(oldProfile.iconShapePath)) {
254 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700255 }
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800256 apply(context, changeFlags);
257 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700258
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800259 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700260 // Create a new config monitor
261 mConfigMonitor.unregister();
262 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
263
264 for (OnIDPChangeListener listener : mChangeListeners) {
265 listener.onIdpChanged(changeFlags, this);
266 }
267 }
268
Sunny Goyal415f1732018-11-29 10:33:47 -0800269 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
270 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700271 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
272 final int depth = parser.getDepth();
273 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700274 while (((type = parser.next()) != XmlPullParser.END_TAG ||
275 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800276 if ((type == XmlPullParser.START_TAG) && "grid-option".equals(parser.getName())) {
277
278 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
279 final int displayDepth = parser.getDepth();
280 while (((type = parser.next()) != XmlPullParser.END_TAG ||
281 parser.getDepth() > displayDepth)
282 && type != XmlPullParser.END_DOCUMENT) {
283 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
284 parser.getName())) {
285 profiles.add(new DisplayOption(
286 gridOption, context, Xml.asAttributeSet(parser)));
287 }
288 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700289 }
290 }
291 } catch (IOException|XmlPullParserException e) {
292 throw new RuntimeException(e);
293 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800294
295 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
296 if (!TextUtils.isEmpty(gridName)) {
297 for (DisplayOption option : profiles) {
298 if (gridName.equals(option.grid.name)) {
299 filteredProfiles.add(option);
300 }
301 }
302 }
303 if (filteredProfiles.isEmpty()) {
304 // No grid found, use the default options
305 for (DisplayOption option : profiles) {
306 if (option.canBeDefault) {
307 filteredProfiles.add(option);
308 }
309 }
310 }
311 if (filteredProfiles.isEmpty()) {
312 throw new RuntimeException("No display option with canBeDefault=true");
313 }
314 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700315 }
316
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700317 private int getLauncherIconDensity(int requiredSize) {
318 // Densities typically defined by an app.
319 int[] densityBuckets = new int[] {
320 DisplayMetrics.DENSITY_LOW,
321 DisplayMetrics.DENSITY_MEDIUM,
322 DisplayMetrics.DENSITY_TV,
323 DisplayMetrics.DENSITY_HIGH,
324 DisplayMetrics.DENSITY_XHIGH,
325 DisplayMetrics.DENSITY_XXHIGH,
326 DisplayMetrics.DENSITY_XXXHIGH
327 };
328
329 int density = DisplayMetrics.DENSITY_XXXHIGH;
330 for (int i = densityBuckets.length - 1; i >= 0; i--) {
331 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
332 / DisplayMetrics.DENSITY_DEFAULT;
333 if (expectedSize >= requiredSize) {
334 density = densityBuckets[i];
335 }
336 }
337
338 return density;
339 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700340
Adam Cohen2e6da152015-05-06 11:42:25 -0700341 /**
342 * Apply any Partner customization grid overrides.
343 *
344 * Currently we support: all apps row / column count.
345 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700346 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
347 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700348 if (p != null) {
349 p.applyInvariantDeviceProfileOverrides(this, dm);
350 }
351 }
352
Sunny Goyal415f1732018-11-29 10:33:47 -0800353 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700354 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700355 }
356
Sunny Goyal415f1732018-11-29 10:33:47 -0800357 @VisibleForTesting
358 static DisplayOption invDistWeightedInterpolate(float width, float height,
359 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700360 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700361
Sunny Goyal415f1732018-11-29 10:33:47 -0800362 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700363 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
364 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700365 }
366
Sunny Goyal415f1732018-11-29 10:33:47 -0800367 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700368 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800369 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700370 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
371 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800372 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700373 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800374 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700375 }
376
Sunny Goyal27835952017-01-13 12:15:53 -0800377 public DeviceProfile getDeviceProfile(Context context) {
378 return context.getResources().getConfiguration().orientation
379 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
380 }
381
Sunny Goyal415f1732018-11-29 10:33:47 -0800382 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700383 float d = dist(x0, y0, x1, y1);
384 if (Float.compare(d, 0f) == 0) {
385 return Float.POSITIVE_INFINITY;
386 }
387 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
388 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700389
390 /**
391 * As a ratio of screen height, the total distance we want the parallax effect to span
392 * horizontally
393 */
394 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
395 float aspectRatio = width / (float) height;
396
397 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
398 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
399 // We will use these two data points to extrapolate how much the wallpaper parallax effect
400 // to span (ie travel) at any aspect ratio:
401
402 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
403 final float ASPECT_RATIO_PORTRAIT = 10/16f;
404 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
405 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
406
407 // To find out the desired width at different aspect ratios, we use the following two
408 // formulas, where the coefficient on x is the aspect ratio (width/height):
409 // (16/10)x + y = 1.5
410 // (10/16)x + y = 1.2
411 // We solve for x and y and end up with a final formula:
412 final float x =
413 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
414 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
415 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
416 return x * aspectRatio + y;
417 }
418
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700419 public interface OnIDPChangeListener {
420
421 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
422 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800423
424
425 private static final class GridOption {
426
427 private final String name;
428 private final int numRows;
429 private final int numColumns;
430
431 private final int numFolderRows;
432 private final int numFolderColumns;
433
434 private final int numHotseatIcons;
435
436 private final int defaultLayoutId;
437 private final int demoModeLayoutId;
438
439 GridOption(Context context, AttributeSet attrs) {
440 TypedArray a = context.obtainStyledAttributes(
441 attrs, R.styleable.GridDisplayOption);
442 name = a.getString(R.styleable.GridDisplayOption_name);
443 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
444 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
445
446 defaultLayoutId = a.getResourceId(
447 R.styleable.GridDisplayOption_defaultLayoutId, 0);
448 demoModeLayoutId = a.getResourceId(
449 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
450 numHotseatIcons = a.getInt(
451 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
452 numFolderRows = a.getInt(
453 R.styleable.GridDisplayOption_numFolderRows, numRows);
454 numFolderColumns = a.getInt(
455 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
456 a.recycle();
457 }
458 }
459
460 private static final class DisplayOption {
461 private final GridOption grid;
462
463 private final String name;
464 private final float minWidthDps;
465 private final float minHeightDps;
466 private final boolean canBeDefault;
467
468 private float iconSize;
469 private float landscapeIconSize;
470 private float iconTextSize;
471
472 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
473 this.grid = grid;
474
475 TypedArray a = context.obtainStyledAttributes(
476 attrs, R.styleable.ProfileDisplayOption);
477
478 name = a.getString(R.styleable.ProfileDisplayOption_name);
479 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
480 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
481 canBeDefault = a.getBoolean(
482 R.styleable.ProfileDisplayOption_canBeDefault, false);
483
484 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
485 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
486 iconSize);
487 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
488 a.recycle();
489 }
490
491 DisplayOption() {
492 grid = null;
493 name = null;
494 minWidthDps = 0;
495 minHeightDps = 0;
496 canBeDefault = false;
497 }
498
499 private DisplayOption multiply(float w) {
500 iconSize *= w;
501 landscapeIconSize *= w;
502 iconTextSize *= w;
503 return this;
504 }
505
506 private DisplayOption add(DisplayOption p) {
507 iconSize += p.iconSize;
508 landscapeIconSize += p.landscapeIconSize;
509 iconTextSize += p.iconTextSize;
510 return this;
511 }
512 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700513}