blob: f63cce58d2f8d18aaa12a9183cae959324dd9207 [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;
Adam Cohen2e6da152015-05-06 11:42:25 -070032import com.android.launcher3.util.Thunk;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070033
Sunny Goyal819e1932016-07-07 16:43:58 -070034import org.xmlpull.v1.XmlPullParser;
35import org.xmlpull.v1.XmlPullParserException;
36
37import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070038import java.util.ArrayList;
39import java.util.Collections;
40import java.util.Comparator;
41
42public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070043
44 // This is a static that we use for the default icon size on a 4/5-inch phone
Sunny Goyalc6205602015-05-21 20:46:33 -070045 private static float DEFAULT_ICON_SIZE_DP = 60;
Adam Cohen2e6da152015-05-06 11:42:25 -070046
Sunny Goyal53d7ee42015-05-22 12:25:45 -070047 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
48
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070049 // Constants that affects the interpolation curve between statically defined device profile
50 // buckets.
51 private static float KNEARESTNEIGHBOR = 3;
52 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070053
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070054 // used to offset float not being able to express extremely small weights in extreme cases.
55 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070056
57 // Profile-defining invariant properties
58 String name;
59 float minWidthDps;
60 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070061
62 /**
63 * Number of icons per row and column in the workspace.
64 */
Adam Cohen2e6da152015-05-06 11:42:25 -070065 public int numRows;
66 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070067
68 /**
69 * Number of icons per row and column in the folder.
70 */
Adam Cohen2e6da152015-05-06 11:42:25 -070071 public int numFolderRows;
72 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070073 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070074 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070075 public int iconBitmapSize;
76 public int fillResIconDpi;
77 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070078
79 /**
80 * Number of icons inside the hotseat area.
81 */
Sunny Goyalf862a262015-12-14 14:27:38 -080082 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -070083
Adam Cohen2e6da152015-05-06 11:42:25 -070084 int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -070085 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -070086
cuijiaxingabda8d72017-03-20 09:51:36 -070087 public DeviceProfile landscapeProfile;
88 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -070089
Sunny Goyal6f866092016-03-17 17:04:15 -070090 public Point defaultWallpaperSize;
91
Sunny Goyalf633ef52018-03-13 09:57:05 -070092 @VisibleForTesting
Sunny Goyalf076eae2016-01-11 12:25:10 -080093 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -070094 }
95
Sunny Goyalf633ef52018-03-13 09:57:05 -070096 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070097 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Sunny Goyalb1d222e2018-01-30 20:52:27 -080098 p.numFolderRows, p.numFolderColumns,
Jon Mirandab28c4fc2017-06-20 10:58:36 -070099 p.iconSize, p.landscapeIconSize, p.iconTextSize, p.numHotseatIcons,
Adam Cohen27824492017-09-22 17:10:55 -0700100 p.defaultLayoutId, p.demoModeLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700101 }
102
Sunny Goyalf633ef52018-03-13 09:57:05 -0700103 private InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc,
Adam Cohen27824492017-09-22 17:10:55 -0700104 float is, float lis, float its, int hs, int dlId, int dmlId) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700105 name = n;
106 minWidthDps = w;
107 minHeightDps = h;
108 numRows = r;
109 numColumns = c;
110 numFolderRows = fr;
111 numFolderColumns = fc;
112 iconSize = is;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700113 landscapeIconSize = lis;
Adam Cohen2e6da152015-05-06 11:42:25 -0700114 iconTextSize = its;
115 numHotseatIcons = hs;
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 defaultLayoutId = dlId;
Adam Cohen27824492017-09-22 17:10:55 -0700117 demoModeLayoutId = dmlId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700118 }
119
Sunny Goyalbbf01842015-10-08 07:41:15 -0700120 @TargetApi(23)
Sunny Goyalf633ef52018-03-13 09:57:05 -0700121 public InvariantDeviceProfile(Context context) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700122 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
123 Display display = wm.getDefaultDisplay();
124 DisplayMetrics dm = new DisplayMetrics();
125 display.getMetrics(dm);
126
127 Point smallestSize = new Point();
128 Point largestSize = new Point();
129 display.getCurrentSizeRange(smallestSize, largestSize);
130
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700131 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700132 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
133 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
134
Sunny Goyal819e1932016-07-07 16:43:58 -0700135 ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
136 minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700137 InvariantDeviceProfile interpolatedDeviceProfileOut =
138 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700139
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700140 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700141 numRows = closestProfile.numRows;
142 numColumns = closestProfile.numColumns;
143 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700144 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700145 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700146 numFolderRows = closestProfile.numFolderRows;
147 numFolderColumns = closestProfile.numFolderColumns;
148
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700149 iconSize = interpolatedDeviceProfileOut.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700150 landscapeIconSize = interpolatedDeviceProfileOut.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700151 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700152 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700153 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700154
155 // If the partner customization apk contains any grid overrides, apply them
156 // Supported overrides: numRows, numColumns, iconSize
157 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700158
159 Point realSize = new Point();
160 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700161 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700162 // same in any orientation.
163 int smallSide = Math.min(realSize.x, realSize.y);
164 int largeSide = Math.max(realSize.x, realSize.y);
165
166 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800167 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700168 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800169 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700170
171 // We need to ensure that there is enough extra space in the wallpaper
172 // for the intended parallax effects
173 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
174 defaultWallpaperSize = new Point(
175 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
176 largeSide);
177 } else {
178 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
179 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700180 }
181
Sunny Goyal819e1932016-07-07 16:43:58 -0700182 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
183 ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
184 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
185 final int depth = parser.getDepth();
186 int type;
187
188 while (((type = parser.next()) != XmlPullParser.END_TAG ||
189 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
190 if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
191 TypedArray a = context.obtainStyledAttributes(
192 Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
193 int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
194 int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
195 float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
196 profiles.add(new InvariantDeviceProfile(
197 a.getString(R.styleable.InvariantDeviceProfile_name),
198 a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
199 a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
200 numRows,
201 numColumns,
202 a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
203 a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
Sunny Goyal819e1932016-07-07 16:43:58 -0700204 iconSize,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700205 a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize),
Sunny Goyal819e1932016-07-07 16:43:58 -0700206 a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
207 a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
Adam Cohen27824492017-09-22 17:10:55 -0700208 a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0),
209 a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0)));
Sunny Goyal819e1932016-07-07 16:43:58 -0700210 a.recycle();
211 }
212 }
213 } catch (IOException|XmlPullParserException e) {
214 throw new RuntimeException(e);
215 }
216 return profiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700217 }
218
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700219 private int getLauncherIconDensity(int requiredSize) {
220 // Densities typically defined by an app.
221 int[] densityBuckets = new int[] {
222 DisplayMetrics.DENSITY_LOW,
223 DisplayMetrics.DENSITY_MEDIUM,
224 DisplayMetrics.DENSITY_TV,
225 DisplayMetrics.DENSITY_HIGH,
226 DisplayMetrics.DENSITY_XHIGH,
227 DisplayMetrics.DENSITY_XXHIGH,
228 DisplayMetrics.DENSITY_XXXHIGH
229 };
230
231 int density = DisplayMetrics.DENSITY_XXXHIGH;
232 for (int i = densityBuckets.length - 1; i >= 0; i--) {
233 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
234 / DisplayMetrics.DENSITY_DEFAULT;
235 if (expectedSize >= requiredSize) {
236 density = densityBuckets[i];
237 }
238 }
239
240 return density;
241 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700242
Adam Cohen2e6da152015-05-06 11:42:25 -0700243 /**
244 * Apply any Partner customization grid overrides.
245 *
246 * Currently we support: all apps row / column count.
247 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700248 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
249 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700250 if (p != null) {
251 p.applyInvariantDeviceProfileOverrides(this, dm);
252 }
253 }
254
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700255 @Thunk float dist(float x0, float y0, float x1, float y1) {
256 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700257 }
258
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700259 /**
260 * Returns the closest device profiles ordered by closeness to the specified width and height
261 */
262 // Package private visibility for testing.
263 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
264 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700265
266 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700267 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
268 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
269 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700270 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
271 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700272 }
273 });
274
275 return pointsByNearness;
276 }
277
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700278 // Package private visibility for testing.
279 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
280 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700281 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700282
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700283 InvariantDeviceProfile p = points.get(0);
284 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
285 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700286 }
287
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700288 InvariantDeviceProfile out = new InvariantDeviceProfile();
289 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
290 p = new InvariantDeviceProfile(points.get(i));
291 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
292 weights += w;
293 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700294 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700295 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700296 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700297
298 private void add(InvariantDeviceProfile p) {
299 iconSize += p.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700300 landscapeIconSize += p.landscapeIconSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700301 iconTextSize += p.iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700302 }
303
304 private InvariantDeviceProfile multiply(float w) {
305 iconSize *= w;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700306 landscapeIconSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700307 iconTextSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700308 return this;
309 }
310
Sunny Goyalbb011da2016-06-15 15:42:29 -0700311 public int getAllAppsButtonRank() {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800312 if (FeatureFlags.IS_DOGFOOD_BUILD && FeatureFlags.NO_ALL_APPS_ICON) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700313 throw new IllegalAccessError("Accessing all apps rank when all-apps is disabled");
314 }
315 return numHotseatIcons / 2;
316 }
317
318 public boolean isAllAppsButtonRank(int rank) {
319 return rank == getAllAppsButtonRank();
320 }
321
Sunny Goyal27835952017-01-13 12:15:53 -0800322 public DeviceProfile getDeviceProfile(Context context) {
323 return context.getResources().getConfiguration().orientation
324 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
325 }
326
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700327 private float weight(float x0, float y0, float x1, float y1, float pow) {
328 float d = dist(x0, y0, x1, y1);
329 if (Float.compare(d, 0f) == 0) {
330 return Float.POSITIVE_INFINITY;
331 }
332 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
333 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700334
335 /**
336 * As a ratio of screen height, the total distance we want the parallax effect to span
337 * horizontally
338 */
339 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
340 float aspectRatio = width / (float) height;
341
342 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
343 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
344 // We will use these two data points to extrapolate how much the wallpaper parallax effect
345 // to span (ie travel) at any aspect ratio:
346
347 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
348 final float ASPECT_RATIO_PORTRAIT = 10/16f;
349 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
350 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
351
352 // To find out the desired width at different aspect ratios, we use the following two
353 // formulas, where the coefficient on x is the aspect ratio (width/height):
354 // (16/10)x + y = 1.5
355 // (10/16)x + y = 1.2
356 // We solve for x and y and end up with a final formula:
357 final float x =
358 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
359 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
360 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
361 return x * aspectRatio + y;
362 }
363
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700364}