blob: d7bebd1b191f5bd2852383badf7b56465153eb1a [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;
Adam Cohen2e6da152015-05-06 11:42:25 -070025import android.util.DisplayMetrics;
Sunny Goyal819e1932016-07-07 16:43:58 -070026import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070027import android.view.Display;
28import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070029
Sunny Goyalbb011da2016-06-15 15:42:29 -070030import com.android.launcher3.config.FeatureFlags;
Adam Cohen2e6da152015-05-06 11:42:25 -070031import com.android.launcher3.util.Thunk;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070032
Sunny Goyal819e1932016-07-07 16:43:58 -070033import org.xmlpull.v1.XmlPullParser;
34import org.xmlpull.v1.XmlPullParserException;
35
36import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070037import java.util.ArrayList;
38import java.util.Collections;
39import java.util.Comparator;
40
41public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070042
43 // 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 -070044 private static float DEFAULT_ICON_SIZE_DP = 60;
Adam Cohen2e6da152015-05-06 11:42:25 -070045
Sunny Goyal53d7ee42015-05-22 12:25:45 -070046 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
47
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070048 // Constants that affects the interpolation curve between statically defined device profile
49 // buckets.
50 private static float KNEARESTNEIGHBOR = 3;
51 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070052
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070053 // used to offset float not being able to express extremely small weights in extreme cases.
54 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070055
56 // Profile-defining invariant properties
57 String name;
58 float minWidthDps;
59 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070060
61 /**
62 * Number of icons per row and column in the workspace.
63 */
Adam Cohen2e6da152015-05-06 11:42:25 -070064 public int numRows;
65 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070066
67 /**
Winson Chung2c6e5cc2015-06-01 14:38:24 -070068 * The minimum number of predicted apps in all apps.
69 */
Winson1f064272016-07-18 17:18:02 -070070 @Deprecated
Winson Chung2c6e5cc2015-06-01 14:38:24 -070071 int minAllAppsPredictionColumns;
72
73 /**
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070074 * 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 Cohen2e6da152015-05-06 11:42:25 -070088 int defaultLayoutId;
89
cuijiaxingabda8d72017-03-20 09:51:36 -070090 public DeviceProfile landscapeProfile;
91 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -070092
Sunny Goyal6f866092016-03-17 17:04:15 -070093 public Point defaultWallpaperSize;
94
Sunny Goyalf076eae2016-01-11 12:25:10 -080095 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -070096 }
97
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098 public InvariantDeviceProfile(InvariantDeviceProfile p) {
99 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700100 p.numFolderRows, p.numFolderColumns, p.minAllAppsPredictionColumns,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700101 p.iconSize, p.landscapeIconSize, p.iconTextSize, p.numHotseatIcons,
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700102 p.defaultLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700103 }
104
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700105 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700106 float is, float lis, float its, int hs, int dlId) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700107 name = n;
108 minWidthDps = w;
109 minHeightDps = h;
110 numRows = r;
111 numColumns = c;
112 numFolderRows = fr;
113 numFolderColumns = fc;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700114 minAllAppsPredictionColumns = maapc;
Adam Cohen2e6da152015-05-06 11:42:25 -0700115 iconSize = is;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700116 landscapeIconSize = lis;
Adam Cohen2e6da152015-05-06 11:42:25 -0700117 iconTextSize = its;
118 numHotseatIcons = hs;
Adam Cohen2e6da152015-05-06 11:42:25 -0700119 defaultLayoutId = dlId;
120 }
121
Sunny Goyalbbf01842015-10-08 07:41:15 -0700122 @TargetApi(23)
Adam Cohen2e6da152015-05-06 11:42:25 -0700123 InvariantDeviceProfile(Context context) {
124 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
125 Display display = wm.getDefaultDisplay();
126 DisplayMetrics dm = new DisplayMetrics();
127 display.getMetrics(dm);
128
129 Point smallestSize = new Point();
130 Point largestSize = new Point();
131 display.getCurrentSizeRange(smallestSize, largestSize);
132
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700133 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700134 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
135 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
136
Sunny Goyal819e1932016-07-07 16:43:58 -0700137 ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
138 minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700139 InvariantDeviceProfile interpolatedDeviceProfileOut =
140 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700141
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700142 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700143 numRows = closestProfile.numRows;
144 numColumns = closestProfile.numColumns;
145 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700146 defaultLayoutId = closestProfile.defaultLayoutId;
147 numFolderRows = closestProfile.numFolderRows;
148 numFolderColumns = closestProfile.numFolderColumns;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700149 minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns;
Adam Cohen2e6da152015-05-06 11:42:25 -0700150
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700151 iconSize = interpolatedDeviceProfileOut.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700152 landscapeIconSize = interpolatedDeviceProfileOut.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700153 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700154 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700155 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700156
157 // If the partner customization apk contains any grid overrides, apply them
158 // Supported overrides: numRows, numColumns, iconSize
159 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700160
161 Point realSize = new Point();
162 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700163 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700164 // same in any orientation.
165 int smallSide = Math.min(realSize.x, realSize.y);
166 int largeSide = Math.max(realSize.x, realSize.y);
167
168 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
169 largeSide, smallSide, true /* isLandscape */);
170 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
171 smallSide, largeSide, false /* isLandscape */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700172
173 // We need to ensure that there is enough extra space in the wallpaper
174 // for the intended parallax effects
175 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
176 defaultWallpaperSize = new Point(
177 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
178 largeSide);
179 } else {
180 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
181 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700182 }
183
Sunny Goyal819e1932016-07-07 16:43:58 -0700184 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
185 ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
186 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
187 final int depth = parser.getDepth();
188 int type;
189
190 while (((type = parser.next()) != XmlPullParser.END_TAG ||
191 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
192 if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
193 TypedArray a = context.obtainStyledAttributes(
194 Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
195 int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
196 int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
197 float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
198 profiles.add(new InvariantDeviceProfile(
199 a.getString(R.styleable.InvariantDeviceProfile_name),
200 a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
201 a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
202 numRows,
203 numColumns,
204 a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
205 a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
206 a.getInt(R.styleable.InvariantDeviceProfile_minAllAppsPredictionColumns, numColumns),
207 iconSize,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700208 a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize),
Sunny Goyal819e1932016-07-07 16:43:58 -0700209 a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
210 a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
Sunny Goyal819e1932016-07-07 16:43:58 -0700211 a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0)));
212 a.recycle();
213 }
214 }
215 } catch (IOException|XmlPullParserException e) {
216 throw new RuntimeException(e);
217 }
218 return profiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700219 }
220
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700221 private int getLauncherIconDensity(int requiredSize) {
222 // Densities typically defined by an app.
223 int[] densityBuckets = new int[] {
224 DisplayMetrics.DENSITY_LOW,
225 DisplayMetrics.DENSITY_MEDIUM,
226 DisplayMetrics.DENSITY_TV,
227 DisplayMetrics.DENSITY_HIGH,
228 DisplayMetrics.DENSITY_XHIGH,
229 DisplayMetrics.DENSITY_XXHIGH,
230 DisplayMetrics.DENSITY_XXXHIGH
231 };
232
233 int density = DisplayMetrics.DENSITY_XXXHIGH;
234 for (int i = densityBuckets.length - 1; i >= 0; i--) {
235 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
236 / DisplayMetrics.DENSITY_DEFAULT;
237 if (expectedSize >= requiredSize) {
238 density = densityBuckets[i];
239 }
240 }
241
242 return density;
243 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700244
Adam Cohen2e6da152015-05-06 11:42:25 -0700245 /**
246 * Apply any Partner customization grid overrides.
247 *
248 * Currently we support: all apps row / column count.
249 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700250 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
251 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700252 if (p != null) {
253 p.applyInvariantDeviceProfileOverrides(this, dm);
254 }
255 }
256
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700257 @Thunk float dist(float x0, float y0, float x1, float y1) {
258 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700259 }
260
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700261 /**
262 * Returns the closest device profiles ordered by closeness to the specified width and height
263 */
264 // Package private visibility for testing.
265 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
266 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700267
268 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700269 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
270 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
271 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700272 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
273 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700274 }
275 });
276
277 return pointsByNearness;
278 }
279
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700280 // Package private visibility for testing.
281 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
282 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700283 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700284
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700285 InvariantDeviceProfile p = points.get(0);
286 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
287 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700288 }
289
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700290 InvariantDeviceProfile out = new InvariantDeviceProfile();
291 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
292 p = new InvariantDeviceProfile(points.get(i));
293 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
294 weights += w;
295 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700296 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700297 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700298 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700299
300 private void add(InvariantDeviceProfile p) {
301 iconSize += p.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700302 landscapeIconSize += p.landscapeIconSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700303 iconTextSize += p.iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700304 }
305
306 private InvariantDeviceProfile multiply(float w) {
307 iconSize *= w;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700308 landscapeIconSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700309 iconTextSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700310 return this;
311 }
312
Sunny Goyalbb011da2016-06-15 15:42:29 -0700313 public int getAllAppsButtonRank() {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800314 if (FeatureFlags.IS_DOGFOOD_BUILD && FeatureFlags.NO_ALL_APPS_ICON) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700315 throw new IllegalAccessError("Accessing all apps rank when all-apps is disabled");
316 }
317 return numHotseatIcons / 2;
318 }
319
320 public boolean isAllAppsButtonRank(int rank) {
321 return rank == getAllAppsButtonRank();
322 }
323
Sunny Goyal27835952017-01-13 12:15:53 -0800324 public DeviceProfile getDeviceProfile(Context context) {
325 return context.getResources().getConfiguration().orientation
326 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
327 }
328
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700329 private float weight(float x0, float y0, float x1, float y1, float pow) {
330 float d = dist(x0, y0, x1, y1);
331 if (Float.compare(d, 0f) == 0) {
332 return Float.POSITIVE_INFINITY;
333 }
334 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
335 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700336
337 /**
338 * As a ratio of screen height, the total distance we want the parallax effect to span
339 * horizontally
340 */
341 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
342 float aspectRatio = width / (float) height;
343
344 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
345 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
346 // We will use these two data points to extrapolate how much the wallpaper parallax effect
347 // to span (ie travel) at any aspect ratio:
348
349 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
350 final float ASPECT_RATIO_PORTRAIT = 10/16f;
351 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
352 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
353
354 // To find out the desired width at different aspect ratios, we use the following two
355 // formulas, where the coefficient on x is the aspect ratio (width/height):
356 // (16/10)x + y = 1.5
357 // (10/16)x + y = 1.2
358 // We solve for x and y and end up with a final formula:
359 final float x =
360 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
361 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
362 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
363 return x * aspectRatio + y;
364 }
365
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700366}