blob: 2dd3198f99ae3b1a191d6bc89b8bb3cf6960aa6b [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;
31import com.android.launcher3.config.ProviderConfig;
Sunny Goyalc29de852017-02-15 14:39:54 -080032import com.android.launcher3.logging.FileLog;
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
45 // 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 -070046 private static float DEFAULT_ICON_SIZE_DP = 60;
Adam Cohen2e6da152015-05-06 11:42:25 -070047
Sunny Goyal53d7ee42015-05-22 12:25:45 -070048 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
49
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070050 // Constants that affects the interpolation curve between statically defined device profile
51 // buckets.
52 private static float KNEARESTNEIGHBOR = 3;
53 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070054
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070055 // used to offset float not being able to express extremely small weights in extreme cases.
56 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070057
58 // Profile-defining invariant properties
59 String name;
60 float minWidthDps;
61 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070062
63 /**
64 * Number of icons per row and column in the workspace.
65 */
Adam Cohen2e6da152015-05-06 11:42:25 -070066 public int numRows;
67 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070068
69 /**
Winson Chung2c6e5cc2015-06-01 14:38:24 -070070 * The minimum number of predicted apps in all apps.
71 */
Winson1f064272016-07-18 17:18:02 -070072 @Deprecated
Winson Chung2c6e5cc2015-06-01 14:38:24 -070073 int minAllAppsPredictionColumns;
74
75 /**
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070076 * Number of icons per row and column in the folder.
77 */
Adam Cohen2e6da152015-05-06 11:42:25 -070078 public int numFolderRows;
79 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070080 public float iconSize;
81 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 Cohen2e6da152015-05-06 11:42:25 -070089 float hotseatIconSize;
Sunny Goyalc13403c2016-11-18 23:44:48 -080090 public float hotseatScale;
Adam Cohen2e6da152015-05-06 11:42:25 -070091 int defaultLayoutId;
92
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 Goyalf076eae2016-01-11 12:25:10 -080098 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -070099 }
100
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700101 public InvariantDeviceProfile(InvariantDeviceProfile p) {
102 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700103 p.numFolderRows, p.numFolderColumns, p.minAllAppsPredictionColumns,
104 p.iconSize, p.iconTextSize, p.numHotseatIcons, p.hotseatIconSize,
105 p.defaultLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700106 }
107
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700108 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700109 float is, float its, int hs, float his, int dlId) {
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;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700117 minAllAppsPredictionColumns = maapc;
Adam Cohen2e6da152015-05-06 11:42:25 -0700118 iconSize = is;
119 iconTextSize = its;
120 numHotseatIcons = hs;
121 hotseatIconSize = his;
122 defaultLayoutId = dlId;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800123
124 hotseatScale = hotseatIconSize / iconSize;
Adam Cohen2e6da152015-05-06 11:42:25 -0700125 }
126
Sunny Goyalbbf01842015-10-08 07:41:15 -0700127 @TargetApi(23)
Adam Cohen2e6da152015-05-06 11:42:25 -0700128 InvariantDeviceProfile(Context context) {
129 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
130 Display display = wm.getDefaultDisplay();
131 DisplayMetrics dm = new DisplayMetrics();
132 display.getMetrics(dm);
133
134 Point smallestSize = new Point();
135 Point largestSize = new Point();
136 display.getCurrentSizeRange(smallestSize, largestSize);
137
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700138 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700139 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
140 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
141
Sunny Goyal819e1932016-07-07 16:43:58 -0700142 ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
143 minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700144 InvariantDeviceProfile interpolatedDeviceProfileOut =
145 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700146
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700147 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700148 numRows = closestProfile.numRows;
149 numColumns = closestProfile.numColumns;
150 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700151 defaultLayoutId = closestProfile.defaultLayoutId;
152 numFolderRows = closestProfile.numFolderRows;
153 numFolderColumns = closestProfile.numFolderColumns;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700154 minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns;
Adam Cohen2e6da152015-05-06 11:42:25 -0700155
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700156 iconSize = interpolatedDeviceProfileOut.iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700157 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700158 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
159 hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700160 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700161
162 // If the partner customization apk contains any grid overrides, apply them
163 // Supported overrides: numRows, numColumns, iconSize
164 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700165
Sunny Goyalc13403c2016-11-18 23:44:48 -0800166 hotseatScale = hotseatIconSize / iconSize;
167
Sunny Goyalc6205602015-05-21 20:46:33 -0700168 Point realSize = new Point();
169 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700170 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700171 // same in any orientation.
172 int smallSide = Math.min(realSize.x, realSize.y);
173 int largeSide = Math.max(realSize.x, realSize.y);
174
175 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
176 largeSide, smallSide, true /* isLandscape */);
177 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
178 smallSide, largeSide, false /* isLandscape */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700179
180 // We need to ensure that there is enough extra space in the wallpaper
181 // for the intended parallax effects
182 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
183 defaultWallpaperSize = new Point(
184 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
185 largeSide);
186 } else {
187 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
188 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700189 }
190
Sunny Goyalc29de852017-02-15 14:39:54 -0800191 public void dumpDisplayInfo(Context context) {
192 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
193 Display display = wm.getDefaultDisplay();
194 DisplayMetrics dm = new DisplayMetrics();
195 display.getMetrics(dm);
196
197 Point smallestSize = new Point();
198 Point largestSize = new Point();
199 display.getCurrentSizeRange(smallestSize, largestSize);
200
201 FileLog.e("DisplayInfo", "Default Density: " + DisplayMetrics.DENSITY_DEFAULT);
202 FileLog.e("DisplayInfo", "Density: " + dm.densityDpi);
203 FileLog.e("DisplayInfo", "Smallest size: " + smallestSize);
204 FileLog.e("DisplayInfo", "Largest size: " + largestSize);
205 FileLog.e("DisplayInfo", "minWidth/Height DPS: " + minWidthDps + ", " + minHeightDps);
206 }
207
Sunny Goyal819e1932016-07-07 16:43:58 -0700208 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
209 ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
210 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
211 final int depth = parser.getDepth();
212 int type;
213
214 while (((type = parser.next()) != XmlPullParser.END_TAG ||
215 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
216 if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
217 TypedArray a = context.obtainStyledAttributes(
218 Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
219 int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
220 int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
221 float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
222 profiles.add(new InvariantDeviceProfile(
223 a.getString(R.styleable.InvariantDeviceProfile_name),
224 a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
225 a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
226 numRows,
227 numColumns,
228 a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
229 a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
230 a.getInt(R.styleable.InvariantDeviceProfile_minAllAppsPredictionColumns, numColumns),
231 iconSize,
232 a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
233 a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
234 a.getFloat(R.styleable.InvariantDeviceProfile_hotseatIconSize, iconSize),
235 a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0)));
236 a.recycle();
237 }
238 }
239 } catch (IOException|XmlPullParserException e) {
240 throw new RuntimeException(e);
241 }
242 return profiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700243 }
244
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700245 private int getLauncherIconDensity(int requiredSize) {
246 // Densities typically defined by an app.
247 int[] densityBuckets = new int[] {
248 DisplayMetrics.DENSITY_LOW,
249 DisplayMetrics.DENSITY_MEDIUM,
250 DisplayMetrics.DENSITY_TV,
251 DisplayMetrics.DENSITY_HIGH,
252 DisplayMetrics.DENSITY_XHIGH,
253 DisplayMetrics.DENSITY_XXHIGH,
254 DisplayMetrics.DENSITY_XXXHIGH
255 };
256
257 int density = DisplayMetrics.DENSITY_XXXHIGH;
258 for (int i = densityBuckets.length - 1; i >= 0; i--) {
259 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
260 / DisplayMetrics.DENSITY_DEFAULT;
261 if (expectedSize >= requiredSize) {
262 density = densityBuckets[i];
263 }
264 }
265
266 return density;
267 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700268
Adam Cohen2e6da152015-05-06 11:42:25 -0700269 /**
270 * Apply any Partner customization grid overrides.
271 *
272 * Currently we support: all apps row / column count.
273 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700274 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
275 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700276 if (p != null) {
277 p.applyInvariantDeviceProfileOverrides(this, dm);
278 }
279 }
280
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700281 @Thunk float dist(float x0, float y0, float x1, float y1) {
282 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700283 }
284
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700285 /**
286 * Returns the closest device profiles ordered by closeness to the specified width and height
287 */
288 // Package private visibility for testing.
289 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
290 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700291
292 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700293 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
294 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
295 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700296 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
297 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700298 }
299 });
300
301 return pointsByNearness;
302 }
303
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700304 // Package private visibility for testing.
305 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
306 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700307 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700308
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700309 InvariantDeviceProfile p = points.get(0);
310 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
311 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700312 }
313
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700314 InvariantDeviceProfile out = new InvariantDeviceProfile();
315 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
316 p = new InvariantDeviceProfile(points.get(i));
317 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
318 weights += w;
319 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700320 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700321 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700322 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700323
324 private void add(InvariantDeviceProfile p) {
325 iconSize += p.iconSize;
326 iconTextSize += p.iconTextSize;
327 hotseatIconSize += p.hotseatIconSize;
328 }
329
330 private InvariantDeviceProfile multiply(float w) {
331 iconSize *= w;
332 iconTextSize *= w;
333 hotseatIconSize *= w;
334 return this;
335 }
336
Sunny Goyalbb011da2016-06-15 15:42:29 -0700337 public int getAllAppsButtonRank() {
338 if (ProviderConfig.IS_DOGFOOD_BUILD && FeatureFlags.NO_ALL_APPS_ICON) {
339 throw new IllegalAccessError("Accessing all apps rank when all-apps is disabled");
340 }
341 return numHotseatIcons / 2;
342 }
343
344 public boolean isAllAppsButtonRank(int rank) {
345 return rank == getAllAppsButtonRank();
346 }
347
Sunny Goyal27835952017-01-13 12:15:53 -0800348 public DeviceProfile getDeviceProfile(Context context) {
349 return context.getResources().getConfiguration().orientation
350 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
351 }
352
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700353 private float weight(float x0, float y0, float x1, float y1, float pow) {
354 float d = dist(x0, y0, x1, y1);
355 if (Float.compare(d, 0f) == 0) {
356 return Float.POSITIVE_INFINITY;
357 }
358 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
359 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700360
361 /**
362 * As a ratio of screen height, the total distance we want the parallax effect to span
363 * horizontally
364 */
365 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
366 float aspectRatio = width / (float) height;
367
368 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
369 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
370 // We will use these two data points to extrapolate how much the wallpaper parallax effect
371 // to span (ie travel) at any aspect ratio:
372
373 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
374 final float ASPECT_RATIO_PORTRAIT = 10/16f;
375 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
376 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
377
378 // To find out the desired width at different aspect ratios, we use the following two
379 // formulas, where the coefficient on x is the aspect ratio (width/height):
380 // (16/10)x + y = 1.5
381 // (10/16)x + y = 1.2
382 // We solve for x and y and end up with a final formula:
383 final float x =
384 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
385 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
386 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
387 return x * aspectRatio + y;
388 }
389
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700390}