blob: e3c6965f7f45e9a719dc823dcbaeae99b141a479 [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;
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 /**
Winson Chung2c6e5cc2015-06-01 14:38:24 -070069 * The minimum number of predicted apps in all apps.
70 */
Winson1f064272016-07-18 17:18:02 -070071 @Deprecated
Winson Chung2c6e5cc2015-06-01 14:38:24 -070072 int minAllAppsPredictionColumns;
73
74 /**
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070075 * 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;
80 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 float hotseatIconSize;
Sunny Goyalc13403c2016-11-18 23:44:48 -080089 public float hotseatScale;
Adam Cohen2e6da152015-05-06 11:42:25 -070090 int defaultLayoutId;
91
Sunny Goyalc6205602015-05-21 20:46:33 -070092 DeviceProfile landscapeProfile;
93 DeviceProfile portraitProfile;
94
Sunny Goyal6f866092016-03-17 17:04:15 -070095 public Point defaultWallpaperSize;
96
Sunny Goyalf076eae2016-01-11 12:25:10 -080097 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -070098 }
99
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700100 public InvariantDeviceProfile(InvariantDeviceProfile p) {
101 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700102 p.numFolderRows, p.numFolderColumns, p.minAllAppsPredictionColumns,
103 p.iconSize, p.iconTextSize, p.numHotseatIcons, p.hotseatIconSize,
104 p.defaultLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700105 }
106
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700107 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700108 float is, float its, int hs, float his, int dlId) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700109 name = n;
110 minWidthDps = w;
111 minHeightDps = h;
112 numRows = r;
113 numColumns = c;
114 numFolderRows = fr;
115 numFolderColumns = fc;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700116 minAllAppsPredictionColumns = maapc;
Adam Cohen2e6da152015-05-06 11:42:25 -0700117 iconSize = is;
118 iconTextSize = its;
119 numHotseatIcons = hs;
120 hotseatIconSize = his;
121 defaultLayoutId = dlId;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800122
123 hotseatScale = hotseatIconSize / iconSize;
Adam Cohen2e6da152015-05-06 11:42:25 -0700124 }
125
Sunny Goyalbbf01842015-10-08 07:41:15 -0700126 @TargetApi(23)
Adam Cohen2e6da152015-05-06 11:42:25 -0700127 InvariantDeviceProfile(Context context) {
128 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;
151 numFolderRows = closestProfile.numFolderRows;
152 numFolderColumns = closestProfile.numFolderColumns;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700153 minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns;
Adam Cohen2e6da152015-05-06 11:42:25 -0700154
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700155 iconSize = interpolatedDeviceProfileOut.iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700156 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700157 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
158 hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
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
Sunny Goyalc13403c2016-11-18 23:44:48 -0800165 hotseatScale = hotseatIconSize / iconSize;
166
Sunny Goyalc6205602015-05-21 20:46:33 -0700167 Point realSize = new Point();
168 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700169 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700170 // same in any orientation.
171 int smallSide = Math.min(realSize.x, realSize.y);
172 int largeSide = Math.max(realSize.x, realSize.y);
173
174 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
175 largeSide, smallSide, true /* isLandscape */);
176 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
177 smallSide, largeSide, false /* isLandscape */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700178
179 // We need to ensure that there is enough extra space in the wallpaper
180 // for the intended parallax effects
181 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
182 defaultWallpaperSize = new Point(
183 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
184 largeSide);
185 } else {
186 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
187 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700188 }
189
Sunny Goyal819e1932016-07-07 16:43:58 -0700190 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
191 ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
192 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
193 final int depth = parser.getDepth();
194 int type;
195
196 while (((type = parser.next()) != XmlPullParser.END_TAG ||
197 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
198 if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
199 TypedArray a = context.obtainStyledAttributes(
200 Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
201 int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
202 int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
203 float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
204 profiles.add(new InvariantDeviceProfile(
205 a.getString(R.styleable.InvariantDeviceProfile_name),
206 a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
207 a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
208 numRows,
209 numColumns,
210 a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
211 a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
212 a.getInt(R.styleable.InvariantDeviceProfile_minAllAppsPredictionColumns, numColumns),
213 iconSize,
214 a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
215 a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
216 a.getFloat(R.styleable.InvariantDeviceProfile_hotseatIconSize, iconSize),
217 a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0)));
218 a.recycle();
219 }
220 }
221 } catch (IOException|XmlPullParserException e) {
222 throw new RuntimeException(e);
223 }
224 return profiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700225 }
226
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700227 private int getLauncherIconDensity(int requiredSize) {
228 // Densities typically defined by an app.
229 int[] densityBuckets = new int[] {
230 DisplayMetrics.DENSITY_LOW,
231 DisplayMetrics.DENSITY_MEDIUM,
232 DisplayMetrics.DENSITY_TV,
233 DisplayMetrics.DENSITY_HIGH,
234 DisplayMetrics.DENSITY_XHIGH,
235 DisplayMetrics.DENSITY_XXHIGH,
236 DisplayMetrics.DENSITY_XXXHIGH
237 };
238
239 int density = DisplayMetrics.DENSITY_XXXHIGH;
240 for (int i = densityBuckets.length - 1; i >= 0; i--) {
241 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
242 / DisplayMetrics.DENSITY_DEFAULT;
243 if (expectedSize >= requiredSize) {
244 density = densityBuckets[i];
245 }
246 }
247
248 return density;
249 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700250
Adam Cohen2e6da152015-05-06 11:42:25 -0700251 /**
252 * Apply any Partner customization grid overrides.
253 *
254 * Currently we support: all apps row / column count.
255 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700256 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
257 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700258 if (p != null) {
259 p.applyInvariantDeviceProfileOverrides(this, dm);
260 }
261 }
262
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700263 @Thunk float dist(float x0, float y0, float x1, float y1) {
264 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700265 }
266
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700267 /**
268 * Returns the closest device profiles ordered by closeness to the specified width and height
269 */
270 // Package private visibility for testing.
271 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
272 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700273
274 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700275 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
276 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
277 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700278 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
279 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700280 }
281 });
282
283 return pointsByNearness;
284 }
285
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700286 // Package private visibility for testing.
287 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
288 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700289 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700290
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700291 InvariantDeviceProfile p = points.get(0);
292 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
293 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700294 }
295
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700296 InvariantDeviceProfile out = new InvariantDeviceProfile();
297 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
298 p = new InvariantDeviceProfile(points.get(i));
299 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
300 weights += w;
301 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700302 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700303 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700304 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700305
306 private void add(InvariantDeviceProfile p) {
307 iconSize += p.iconSize;
308 iconTextSize += p.iconTextSize;
309 hotseatIconSize += p.hotseatIconSize;
310 }
311
312 private InvariantDeviceProfile multiply(float w) {
313 iconSize *= w;
314 iconTextSize *= w;
315 hotseatIconSize *= w;
316 return this;
317 }
318
Sunny Goyalbb011da2016-06-15 15:42:29 -0700319 public int getAllAppsButtonRank() {
320 if (ProviderConfig.IS_DOGFOOD_BUILD && FeatureFlags.NO_ALL_APPS_ICON) {
321 throw new IllegalAccessError("Accessing all apps rank when all-apps is disabled");
322 }
323 return numHotseatIcons / 2;
324 }
325
326 public boolean isAllAppsButtonRank(int rank) {
327 return rank == getAllAppsButtonRank();
328 }
329
Sunny Goyal27835952017-01-13 12:15:53 -0800330 public DeviceProfile getDeviceProfile(Context context) {
331 return context.getResources().getConfiguration().orientation
332 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
333 }
334
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700335 private float weight(float x0, float y0, float x1, float y1, float pow) {
336 float d = dist(x0, y0, x1, y1);
337 if (Float.compare(d, 0f) == 0) {
338 return Float.POSITIVE_INFINITY;
339 }
340 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
341 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700342
343 /**
344 * As a ratio of screen height, the total distance we want the parallax effect to span
345 * horizontally
346 */
347 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
348 float aspectRatio = width / (float) height;
349
350 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
351 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
352 // We will use these two data points to extrapolate how much the wallpaper parallax effect
353 // to span (ie travel) at any aspect ratio:
354
355 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
356 final float ASPECT_RATIO_PORTRAIT = 10/16f;
357 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
358 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
359
360 // To find out the desired width at different aspect ratios, we use the following two
361 // formulas, where the coefficient on x is the aspect ratio (width/height):
362 // (16/10)x + y = 1.5
363 // (10/16)x + y = 1.2
364 // We solve for x and y and end up with a final formula:
365 final float x =
366 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
367 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
368 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
369 return x * aspectRatio + y;
370 }
371
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700372}