blob: 22bc162b656c142f14e9f06f2deb733f20ff79a7 [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 Goyalbb011da2016-06-15 15:42:29 -070031import com.android.launcher3.config.FeatureFlags;
Sunny Goyald0e360a2018-06-29 14:40:18 -070032import com.android.launcher3.util.ConfigMonitor;
33import com.android.launcher3.util.MainThreadInitializedObject;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import com.android.launcher3.util.Thunk;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070035
Sunny Goyal819e1932016-07-07 16:43:58 -070036import org.xmlpull.v1.XmlPullParser;
37import org.xmlpull.v1.XmlPullParserException;
38
39import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070040import java.util.ArrayList;
41import java.util.Collections;
42import java.util.Comparator;
43
44public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070045
Sunny Goyald0e360a2018-06-29 14:40:18 -070046 // We do not need any synchronization for this variable as its only written on UI thread.
47 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
48 new MainThreadInitializedObject<>((c) -> {
49 new ConfigMonitor(c).register();
50 return new InvariantDeviceProfile(c);
51 });
Adam Cohen2e6da152015-05-06 11:42:25 -070052
Sunny Goyal53d7ee42015-05-22 12:25:45 -070053 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
54
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070055 // Constants that affects the interpolation curve between statically defined device profile
56 // buckets.
57 private static float KNEARESTNEIGHBOR = 3;
58 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070059
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070060 // used to offset float not being able to express extremely small weights in extreme cases.
61 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070062
63 // Profile-defining invariant properties
64 String name;
65 float minWidthDps;
66 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070067
68 /**
69 * Number of icons per row and column in the workspace.
70 */
Adam Cohen2e6da152015-05-06 11:42:25 -070071 public int numRows;
72 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070073
74 /**
75 * Number of icons per row and column in the folder.
76 */
Adam Cohen2e6da152015-05-06 11:42:25 -070077 public int numFolderRows;
78 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070079 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070080 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070081 public int iconBitmapSize;
82 public int fillResIconDpi;
83 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070084
85 /**
86 * Number of icons inside the hotseat area.
87 */
Sunny Goyalf862a262015-12-14 14:27:38 -080088 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -070089
Adam Cohen2e6da152015-05-06 11:42:25 -070090 int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -070091 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -070092
cuijiaxingabda8d72017-03-20 09:51:36 -070093 public DeviceProfile landscapeProfile;
94 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -070095
Sunny Goyal6f866092016-03-17 17:04:15 -070096 public Point defaultWallpaperSize;
97
Sunny Goyalf633ef52018-03-13 09:57:05 -070098 @VisibleForTesting
Sunny Goyalf076eae2016-01-11 12:25:10 -080099 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -0700100 }
101
Sunny Goyalf633ef52018-03-13 09:57:05 -0700102 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700103 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Sunny Goyalb1d222e2018-01-30 20:52:27 -0800104 p.numFolderRows, p.numFolderColumns,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700105 p.iconSize, p.landscapeIconSize, p.iconTextSize, p.numHotseatIcons,
Adam Cohen27824492017-09-22 17:10:55 -0700106 p.defaultLayoutId, p.demoModeLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700107 }
108
Sunny Goyalf633ef52018-03-13 09:57:05 -0700109 private InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc,
Adam Cohen27824492017-09-22 17:10:55 -0700110 float is, float lis, float its, int hs, int dlId, int dmlId) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700111 name = n;
112 minWidthDps = w;
113 minHeightDps = h;
114 numRows = r;
115 numColumns = c;
116 numFolderRows = fr;
117 numFolderColumns = fc;
118 iconSize = is;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700119 landscapeIconSize = lis;
Adam Cohen2e6da152015-05-06 11:42:25 -0700120 iconTextSize = its;
121 numHotseatIcons = hs;
Adam Cohen2e6da152015-05-06 11:42:25 -0700122 defaultLayoutId = dlId;
Adam Cohen27824492017-09-22 17:10:55 -0700123 demoModeLayoutId = dmlId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700124 }
125
Sunny Goyalbbf01842015-10-08 07:41:15 -0700126 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700127 private InvariantDeviceProfile(Context context) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700128 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
129 Display display = wm.getDefaultDisplay();
130 DisplayMetrics dm = new DisplayMetrics();
131 display.getMetrics(dm);
132
133 Point smallestSize = new Point();
134 Point largestSize = new Point();
135 display.getCurrentSizeRange(smallestSize, largestSize);
136
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700137 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700138 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
139 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
140
Sunny Goyal819e1932016-07-07 16:43:58 -0700141 ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
142 minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700143 InvariantDeviceProfile interpolatedDeviceProfileOut =
144 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700145
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700146 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700147 numRows = closestProfile.numRows;
148 numColumns = closestProfile.numColumns;
149 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700150 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700151 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700152 numFolderRows = closestProfile.numFolderRows;
153 numFolderColumns = closestProfile.numFolderColumns;
154
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700155 iconSize = interpolatedDeviceProfileOut.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700156 landscapeIconSize = interpolatedDeviceProfileOut.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700157 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700158 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700159 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700160
161 // If the partner customization apk contains any grid overrides, apply them
162 // Supported overrides: numRows, numColumns, iconSize
163 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700164
165 Point realSize = new Point();
166 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700167 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700168 // same in any orientation.
169 int smallSide = Math.min(realSize.x, realSize.y);
170 int largeSide = Math.max(realSize.x, realSize.y);
171
172 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800173 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700174 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800175 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700176
177 // We need to ensure that there is enough extra space in the wallpaper
178 // for the intended parallax effects
179 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
180 defaultWallpaperSize = new Point(
181 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
182 largeSide);
183 } else {
184 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
185 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700186 }
187
Sunny Goyal819e1932016-07-07 16:43:58 -0700188 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
189 ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
190 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
191 final int depth = parser.getDepth();
192 int type;
193
194 while (((type = parser.next()) != XmlPullParser.END_TAG ||
195 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
196 if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
197 TypedArray a = context.obtainStyledAttributes(
198 Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
199 int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
200 int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
201 float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
202 profiles.add(new InvariantDeviceProfile(
203 a.getString(R.styleable.InvariantDeviceProfile_name),
204 a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
205 a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
206 numRows,
207 numColumns,
208 a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
209 a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
Sunny Goyal819e1932016-07-07 16:43:58 -0700210 iconSize,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700211 a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize),
Sunny Goyal819e1932016-07-07 16:43:58 -0700212 a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
213 a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
Adam Cohen27824492017-09-22 17:10:55 -0700214 a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0),
215 a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0)));
Sunny Goyal819e1932016-07-07 16:43:58 -0700216 a.recycle();
217 }
218 }
219 } catch (IOException|XmlPullParserException e) {
220 throw new RuntimeException(e);
221 }
222 return profiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700223 }
224
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700225 private int getLauncherIconDensity(int requiredSize) {
226 // Densities typically defined by an app.
227 int[] densityBuckets = new int[] {
228 DisplayMetrics.DENSITY_LOW,
229 DisplayMetrics.DENSITY_MEDIUM,
230 DisplayMetrics.DENSITY_TV,
231 DisplayMetrics.DENSITY_HIGH,
232 DisplayMetrics.DENSITY_XHIGH,
233 DisplayMetrics.DENSITY_XXHIGH,
234 DisplayMetrics.DENSITY_XXXHIGH
235 };
236
237 int density = DisplayMetrics.DENSITY_XXXHIGH;
238 for (int i = densityBuckets.length - 1; i >= 0; i--) {
239 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
240 / DisplayMetrics.DENSITY_DEFAULT;
241 if (expectedSize >= requiredSize) {
242 density = densityBuckets[i];
243 }
244 }
245
246 return density;
247 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700248
Adam Cohen2e6da152015-05-06 11:42:25 -0700249 /**
250 * Apply any Partner customization grid overrides.
251 *
252 * Currently we support: all apps row / column count.
253 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700254 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
255 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700256 if (p != null) {
257 p.applyInvariantDeviceProfileOverrides(this, dm);
258 }
259 }
260
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700261 @Thunk float dist(float x0, float y0, float x1, float y1) {
262 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700263 }
264
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700265 /**
266 * Returns the closest device profiles ordered by closeness to the specified width and height
267 */
268 // Package private visibility for testing.
269 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
270 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700271
272 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700273 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
274 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
275 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700276 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
277 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700278 }
279 });
280
281 return pointsByNearness;
282 }
283
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700284 // Package private visibility for testing.
285 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
286 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700287 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700288
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700289 InvariantDeviceProfile p = points.get(0);
290 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
291 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700292 }
293
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700294 InvariantDeviceProfile out = new InvariantDeviceProfile();
295 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
296 p = new InvariantDeviceProfile(points.get(i));
297 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
298 weights += w;
299 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700300 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700301 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700302 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700303
304 private void add(InvariantDeviceProfile p) {
305 iconSize += p.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700306 landscapeIconSize += p.landscapeIconSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700307 iconTextSize += p.iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700308 }
309
310 private InvariantDeviceProfile multiply(float w) {
311 iconSize *= w;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700312 landscapeIconSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700313 iconTextSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700314 return this;
315 }
316
Sunny Goyalbb011da2016-06-15 15:42:29 -0700317 public int getAllAppsButtonRank() {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800318 if (FeatureFlags.IS_DOGFOOD_BUILD && FeatureFlags.NO_ALL_APPS_ICON) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700319 throw new IllegalAccessError("Accessing all apps rank when all-apps is disabled");
320 }
321 return numHotseatIcons / 2;
322 }
323
324 public boolean isAllAppsButtonRank(int rank) {
325 return rank == getAllAppsButtonRank();
326 }
327
Sunny Goyal27835952017-01-13 12:15:53 -0800328 public DeviceProfile getDeviceProfile(Context context) {
329 return context.getResources().getConfiguration().orientation
330 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
331 }
332
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700333 private float weight(float x0, float y0, float x1, float y1, float pow) {
334 float d = dist(x0, y0, x1, y1);
335 if (Float.compare(d, 0f) == 0) {
336 return Float.POSITIVE_INFINITY;
337 }
338 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
339 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700340
341 /**
342 * As a ratio of screen height, the total distance we want the parallax effect to span
343 * horizontally
344 */
345 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
346 float aspectRatio = width / (float) height;
347
348 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
349 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
350 // We will use these two data points to extrapolate how much the wallpaper parallax effect
351 // to span (ie travel) at any aspect ratio:
352
353 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
354 final float ASPECT_RATIO_PORTRAIT = 10/16f;
355 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
356 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
357
358 // To find out the desired width at different aspect ratios, we use the following two
359 // formulas, where the coefficient on x is the aspect ratio (width/height):
360 // (16/10)x + y = 1.5
361 // (10/16)x + y = 1.2
362 // We solve for x and y and end up with a final formula:
363 final float x =
364 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
365 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
366 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
367 return x * aspectRatio + y;
368 }
369
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700370}