blob: 70845541b8ce12db3eb9aea1438bca967c6329c2 [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 Goyalc6205602015-05-21 20:46:33 -070019import android.annotation.TargetApi;
Adam Cohen2e6da152015-05-06 11:42:25 -070020import android.content.Context;
Sunny Goyal27835952017-01-13 12:15:53 -080021import android.content.res.Configuration;
Sunny Goyal819e1932016-07-07 16:43:58 -070022import android.content.res.TypedArray;
23import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070024import android.graphics.Point;
Sunny Goyalf633ef52018-03-13 09:57:05 -070025import android.support.annotation.VisibleForTesting;
Adam Cohen2e6da152015-05-06 11:42:25 -070026import android.util.DisplayMetrics;
Sunny Goyal819e1932016-07-07 16:43:58 -070027import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import android.view.Display;
29import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070030
Sunny Goyald0e360a2018-06-29 14:40:18 -070031import com.android.launcher3.util.ConfigMonitor;
32import com.android.launcher3.util.MainThreadInitializedObject;
Adam Cohen2e6da152015-05-06 11:42:25 -070033import com.android.launcher3.util.Thunk;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070034
Sunny Goyal819e1932016-07-07 16:43:58 -070035import org.xmlpull.v1.XmlPullParser;
36import org.xmlpull.v1.XmlPullParserException;
37
38import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070039import java.util.ArrayList;
40import java.util.Collections;
41import java.util.Comparator;
42
43public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070044
Sunny Goyald0e360a2018-06-29 14:40:18 -070045 // We do not need any synchronization for this variable as its only written on UI thread.
46 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
47 new MainThreadInitializedObject<>((c) -> {
48 new ConfigMonitor(c).register();
49 return new InvariantDeviceProfile(c);
50 });
Adam Cohen2e6da152015-05-06 11:42:25 -070051
Sunny Goyal53d7ee42015-05-22 12:25:45 -070052 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
53
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070054 // Constants that affects the interpolation curve between statically defined device profile
55 // buckets.
56 private static float KNEARESTNEIGHBOR = 3;
57 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070058
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070059 // used to offset float not being able to express extremely small weights in extreme cases.
60 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070061
62 // Profile-defining invariant properties
63 String name;
64 float minWidthDps;
65 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070066
67 /**
68 * Number of icons per row and column in the workspace.
69 */
Adam Cohen2e6da152015-05-06 11:42:25 -070070 public int numRows;
71 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070072
73 /**
74 * Number of icons per row and column in the folder.
75 */
Adam Cohen2e6da152015-05-06 11:42:25 -070076 public int numFolderRows;
77 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070078 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070079 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070080 public int iconBitmapSize;
81 public int fillResIconDpi;
82 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070083
84 /**
85 * Number of icons inside the hotseat area.
86 */
Sunny Goyalf862a262015-12-14 14:27:38 -080087 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -070088
Adam Cohen2e6da152015-05-06 11:42:25 -070089 int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -070090 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -070091
cuijiaxingabda8d72017-03-20 09:51:36 -070092 public DeviceProfile landscapeProfile;
93 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -070094
Sunny Goyal6f866092016-03-17 17:04:15 -070095 public Point defaultWallpaperSize;
96
Sunny Goyalf633ef52018-03-13 09:57:05 -070097 @VisibleForTesting
Sunny Goyalf076eae2016-01-11 12:25:10 -080098 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -070099 }
100
Sunny Goyalf633ef52018-03-13 09:57:05 -0700101 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700102 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Sunny Goyalb1d222e2018-01-30 20:52:27 -0800103 p.numFolderRows, p.numFolderColumns,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700104 p.iconSize, p.landscapeIconSize, p.iconTextSize, p.numHotseatIcons,
Adam Cohen27824492017-09-22 17:10:55 -0700105 p.defaultLayoutId, p.demoModeLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700106 }
107
Sunny Goyalf633ef52018-03-13 09:57:05 -0700108 private InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc,
Adam Cohen27824492017-09-22 17:10:55 -0700109 float is, float lis, float its, int hs, int dlId, int dmlId) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700110 name = n;
111 minWidthDps = w;
112 minHeightDps = h;
113 numRows = r;
114 numColumns = c;
115 numFolderRows = fr;
116 numFolderColumns = fc;
117 iconSize = is;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700118 landscapeIconSize = lis;
Adam Cohen2e6da152015-05-06 11:42:25 -0700119 iconTextSize = its;
120 numHotseatIcons = hs;
Adam Cohen2e6da152015-05-06 11:42:25 -0700121 defaultLayoutId = dlId;
Adam Cohen27824492017-09-22 17:10:55 -0700122 demoModeLayoutId = dmlId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700123 }
124
Sunny Goyalbbf01842015-10-08 07:41:15 -0700125 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700126 private InvariantDeviceProfile(Context context) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700127 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
128 Display display = wm.getDefaultDisplay();
129 DisplayMetrics dm = new DisplayMetrics();
130 display.getMetrics(dm);
131
132 Point smallestSize = new Point();
133 Point largestSize = new Point();
134 display.getCurrentSizeRange(smallestSize, largestSize);
135
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700136 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700137 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
138 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
139
Sunny Goyal819e1932016-07-07 16:43:58 -0700140 ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
141 minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700142 InvariantDeviceProfile interpolatedDeviceProfileOut =
143 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700144
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700145 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700146 numRows = closestProfile.numRows;
147 numColumns = closestProfile.numColumns;
148 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700149 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700150 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700151 numFolderRows = closestProfile.numFolderRows;
152 numFolderColumns = closestProfile.numFolderColumns;
153
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700154 iconSize = interpolatedDeviceProfileOut.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700155 landscapeIconSize = interpolatedDeviceProfileOut.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700156 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700157 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700158 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700159
160 // If the partner customization apk contains any grid overrides, apply them
161 // Supported overrides: numRows, numColumns, iconSize
162 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700163
164 Point realSize = new Point();
165 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700166 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700167 // same in any orientation.
168 int smallSide = Math.min(realSize.x, realSize.y);
169 int largeSide = Math.max(realSize.x, realSize.y);
170
171 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800172 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700173 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800174 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700175
176 // We need to ensure that there is enough extra space in the wallpaper
177 // for the intended parallax effects
178 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
179 defaultWallpaperSize = new Point(
180 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
181 largeSide);
182 } else {
183 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
184 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700185 }
186
Sunny Goyal819e1932016-07-07 16:43:58 -0700187 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
188 ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
189 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
190 final int depth = parser.getDepth();
191 int type;
192
193 while (((type = parser.next()) != XmlPullParser.END_TAG ||
194 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
195 if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
196 TypedArray a = context.obtainStyledAttributes(
197 Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
198 int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
199 int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
200 float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
201 profiles.add(new InvariantDeviceProfile(
202 a.getString(R.styleable.InvariantDeviceProfile_name),
203 a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
204 a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
205 numRows,
206 numColumns,
207 a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
208 a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
Sunny Goyal819e1932016-07-07 16:43:58 -0700209 iconSize,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700210 a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize),
Sunny Goyal819e1932016-07-07 16:43:58 -0700211 a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
212 a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
Adam Cohen27824492017-09-22 17:10:55 -0700213 a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0),
214 a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0)));
Sunny Goyal819e1932016-07-07 16:43:58 -0700215 a.recycle();
216 }
217 }
218 } catch (IOException|XmlPullParserException e) {
219 throw new RuntimeException(e);
220 }
221 return profiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700222 }
223
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700224 private int getLauncherIconDensity(int requiredSize) {
225 // Densities typically defined by an app.
226 int[] densityBuckets = new int[] {
227 DisplayMetrics.DENSITY_LOW,
228 DisplayMetrics.DENSITY_MEDIUM,
229 DisplayMetrics.DENSITY_TV,
230 DisplayMetrics.DENSITY_HIGH,
231 DisplayMetrics.DENSITY_XHIGH,
232 DisplayMetrics.DENSITY_XXHIGH,
233 DisplayMetrics.DENSITY_XXXHIGH
234 };
235
236 int density = DisplayMetrics.DENSITY_XXXHIGH;
237 for (int i = densityBuckets.length - 1; i >= 0; i--) {
238 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
239 / DisplayMetrics.DENSITY_DEFAULT;
240 if (expectedSize >= requiredSize) {
241 density = densityBuckets[i];
242 }
243 }
244
245 return density;
246 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700247
Adam Cohen2e6da152015-05-06 11:42:25 -0700248 /**
249 * Apply any Partner customization grid overrides.
250 *
251 * Currently we support: all apps row / column count.
252 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700253 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
254 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700255 if (p != null) {
256 p.applyInvariantDeviceProfileOverrides(this, dm);
257 }
258 }
259
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700260 @Thunk float dist(float x0, float y0, float x1, float y1) {
261 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700262 }
263
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700264 /**
265 * Returns the closest device profiles ordered by closeness to the specified width and height
266 */
267 // Package private visibility for testing.
268 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
269 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700270
271 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700272 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
273 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
274 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700275 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
276 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700277 }
278 });
279
280 return pointsByNearness;
281 }
282
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700283 // Package private visibility for testing.
284 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
285 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700286 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700287
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700288 InvariantDeviceProfile p = points.get(0);
289 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
290 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700291 }
292
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700293 InvariantDeviceProfile out = new InvariantDeviceProfile();
294 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
295 p = new InvariantDeviceProfile(points.get(i));
296 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
297 weights += w;
298 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700299 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700300 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700301 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700302
303 private void add(InvariantDeviceProfile p) {
304 iconSize += p.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700305 landscapeIconSize += p.landscapeIconSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700306 iconTextSize += p.iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700307 }
308
309 private InvariantDeviceProfile multiply(float w) {
310 iconSize *= w;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700311 landscapeIconSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700312 iconTextSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700313 return this;
314 }
315
Sunny Goyal27835952017-01-13 12:15:53 -0800316 public DeviceProfile getDeviceProfile(Context context) {
317 return context.getResources().getConfiguration().orientation
318 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
319 }
320
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700321 private float weight(float x0, float y0, float x1, float y1, float pow) {
322 float d = dist(x0, y0, x1, y1);
323 if (Float.compare(d, 0f) == 0) {
324 return Float.POSITIVE_INFINITY;
325 }
326 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
327 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700328
329 /**
330 * As a ratio of screen height, the total distance we want the parallax effect to span
331 * horizontally
332 */
333 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
334 float aspectRatio = width / (float) height;
335
336 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
337 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
338 // We will use these two data points to extrapolate how much the wallpaper parallax effect
339 // to span (ie travel) at any aspect ratio:
340
341 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
342 final float ASPECT_RATIO_PORTRAIT = 10/16f;
343 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
344 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
345
346 // To find out the desired width at different aspect ratios, we use the following two
347 // formulas, where the coefficient on x is the aspect ratio (width/height):
348 // (16/10)x + y = 1.5
349 // (10/16)x + y = 1.2
350 // We solve for x and y and end up with a final formula:
351 final float x =
352 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
353 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
354 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
355 return x * aspectRatio + y;
356 }
357
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700358}