blob: 1b0e5b371b312efafaef19782ca9cc927f603ff0 [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 Goyal819e1932016-07-07 16:43:58 -070021import android.content.res.TypedArray;
22import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070023import android.graphics.Point;
Adam Cohen2e6da152015-05-06 11:42:25 -070024import android.util.DisplayMetrics;
Sunny Goyal819e1932016-07-07 16:43:58 -070025import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070026import android.view.Display;
27import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070028
Sunny Goyalbb011da2016-06-15 15:42:29 -070029import com.android.launcher3.config.FeatureFlags;
30import com.android.launcher3.config.ProviderConfig;
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;
79 public int iconBitmapSize;
80 public int fillResIconDpi;
81 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070082
83 /**
84 * Number of icons inside the hotseat area.
85 */
Sunny Goyalf862a262015-12-14 14:27:38 -080086 public int numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -070087 float hotseatIconSize;
Sunny Goyalc13403c2016-11-18 23:44:48 -080088 public float hotseatScale;
Adam Cohen2e6da152015-05-06 11:42:25 -070089 int defaultLayoutId;
90
Sunny Goyalc6205602015-05-21 20:46:33 -070091 DeviceProfile landscapeProfile;
92 DeviceProfile portraitProfile;
93
Sunny Goyal6f866092016-03-17 17:04:15 -070094 public Point defaultWallpaperSize;
95
Sunny Goyalf076eae2016-01-11 12:25:10 -080096 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -070097 }
98
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099 public InvariantDeviceProfile(InvariantDeviceProfile p) {
100 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700101 p.numFolderRows, p.numFolderColumns, p.minAllAppsPredictionColumns,
102 p.iconSize, p.iconTextSize, p.numHotseatIcons, p.hotseatIconSize,
103 p.defaultLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700104 }
105
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700106 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc,
Tony Wickhamd6b40372015-09-23 18:37:57 -0700107 float is, float its, int hs, float his, int dlId) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700108 name = n;
109 minWidthDps = w;
110 minHeightDps = h;
111 numRows = r;
112 numColumns = c;
113 numFolderRows = fr;
114 numFolderColumns = fc;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700115 minAllAppsPredictionColumns = maapc;
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 iconSize = is;
117 iconTextSize = its;
118 numHotseatIcons = hs;
119 hotseatIconSize = his;
120 defaultLayoutId = dlId;
Sunny Goyalc13403c2016-11-18 23:44:48 -0800121
122 hotseatScale = hotseatIconSize / iconSize;
Adam Cohen2e6da152015-05-06 11:42:25 -0700123 }
124
Sunny Goyalbbf01842015-10-08 07:41:15 -0700125 @TargetApi(23)
Adam Cohen2e6da152015-05-06 11:42:25 -0700126 InvariantDeviceProfile(Context context) {
127 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
128 Display display = wm.getDefaultDisplay();
129 DisplayMetrics dm = new DisplayMetrics();
130 display.getMetrics(dm);
131
132 Point smallestSize = new Point();
133 Point largestSize = new Point();
134 display.getCurrentSizeRange(smallestSize, largestSize);
135
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700136 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700137 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
138 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
139
Sunny Goyal819e1932016-07-07 16:43:58 -0700140 ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
141 minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700142 InvariantDeviceProfile interpolatedDeviceProfileOut =
143 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700144
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700145 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700146 numRows = closestProfile.numRows;
147 numColumns = closestProfile.numColumns;
148 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700149 defaultLayoutId = closestProfile.defaultLayoutId;
150 numFolderRows = closestProfile.numFolderRows;
151 numFolderColumns = closestProfile.numFolderColumns;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700152 minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns;
Adam Cohen2e6da152015-05-06 11:42:25 -0700153
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700154 iconSize = interpolatedDeviceProfileOut.iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700155 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700156 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
157 hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700158 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700159
160 // If the partner customization apk contains any grid overrides, apply them
161 // Supported overrides: numRows, numColumns, iconSize
162 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700163
Sunny Goyalc13403c2016-11-18 23:44:48 -0800164 hotseatScale = hotseatIconSize / iconSize;
165
Sunny Goyalc6205602015-05-21 20:46:33 -0700166 Point realSize = new Point();
167 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700168 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700169 // same in any orientation.
170 int smallSide = Math.min(realSize.x, realSize.y);
171 int largeSide = Math.max(realSize.x, realSize.y);
172
173 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
174 largeSide, smallSide, true /* isLandscape */);
175 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
176 smallSide, largeSide, false /* isLandscape */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700177
178 // We need to ensure that there is enough extra space in the wallpaper
179 // for the intended parallax effects
180 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
181 defaultWallpaperSize = new Point(
182 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
183 largeSide);
184 } else {
185 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
186 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700187 }
188
Sunny Goyal819e1932016-07-07 16:43:58 -0700189 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
190 ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
191 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
192 final int depth = parser.getDepth();
193 int type;
194
195 while (((type = parser.next()) != XmlPullParser.END_TAG ||
196 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
197 if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
198 TypedArray a = context.obtainStyledAttributes(
199 Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
200 int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
201 int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
202 float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
203 profiles.add(new InvariantDeviceProfile(
204 a.getString(R.styleable.InvariantDeviceProfile_name),
205 a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
206 a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
207 numRows,
208 numColumns,
209 a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
210 a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
211 a.getInt(R.styleable.InvariantDeviceProfile_minAllAppsPredictionColumns, numColumns),
212 iconSize,
213 a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
214 a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
215 a.getFloat(R.styleable.InvariantDeviceProfile_hotseatIconSize, iconSize),
216 a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0)));
217 a.recycle();
218 }
219 }
220 } catch (IOException|XmlPullParserException e) {
221 throw new RuntimeException(e);
222 }
223 return profiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700224 }
225
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700226 private int getLauncherIconDensity(int requiredSize) {
227 // Densities typically defined by an app.
228 int[] densityBuckets = new int[] {
229 DisplayMetrics.DENSITY_LOW,
230 DisplayMetrics.DENSITY_MEDIUM,
231 DisplayMetrics.DENSITY_TV,
232 DisplayMetrics.DENSITY_HIGH,
233 DisplayMetrics.DENSITY_XHIGH,
234 DisplayMetrics.DENSITY_XXHIGH,
235 DisplayMetrics.DENSITY_XXXHIGH
236 };
237
238 int density = DisplayMetrics.DENSITY_XXXHIGH;
239 for (int i = densityBuckets.length - 1; i >= 0; i--) {
240 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
241 / DisplayMetrics.DENSITY_DEFAULT;
242 if (expectedSize >= requiredSize) {
243 density = densityBuckets[i];
244 }
245 }
246
247 return density;
248 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700249
Adam Cohen2e6da152015-05-06 11:42:25 -0700250 /**
251 * Apply any Partner customization grid overrides.
252 *
253 * Currently we support: all apps row / column count.
254 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700255 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
256 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700257 if (p != null) {
258 p.applyInvariantDeviceProfileOverrides(this, dm);
259 }
260 }
261
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700262 @Thunk float dist(float x0, float y0, float x1, float y1) {
263 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700264 }
265
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700266 /**
267 * Returns the closest device profiles ordered by closeness to the specified width and height
268 */
269 // Package private visibility for testing.
270 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
271 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700272
273 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700274 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
275 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
276 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700277 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
278 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700279 }
280 });
281
282 return pointsByNearness;
283 }
284
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700285 // Package private visibility for testing.
286 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
287 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700288 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700289
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700290 InvariantDeviceProfile p = points.get(0);
291 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
292 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700293 }
294
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700295 InvariantDeviceProfile out = new InvariantDeviceProfile();
296 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
297 p = new InvariantDeviceProfile(points.get(i));
298 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
299 weights += w;
300 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700301 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700302 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700303 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700304
305 private void add(InvariantDeviceProfile p) {
306 iconSize += p.iconSize;
307 iconTextSize += p.iconTextSize;
308 hotseatIconSize += p.hotseatIconSize;
309 }
310
311 private InvariantDeviceProfile multiply(float w) {
312 iconSize *= w;
313 iconTextSize *= w;
314 hotseatIconSize *= w;
315 return this;
316 }
317
Sunny Goyalbb011da2016-06-15 15:42:29 -0700318 public int getAllAppsButtonRank() {
319 if (ProviderConfig.IS_DOGFOOD_BUILD && FeatureFlags.NO_ALL_APPS_ICON) {
320 throw new IllegalAccessError("Accessing all apps rank when all-apps is disabled");
321 }
322 return numHotseatIcons / 2;
323 }
324
325 public boolean isAllAppsButtonRank(int rank) {
326 return rank == getAllAppsButtonRank();
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}