blob: da95d66f94106db6a6294a48f91fa63f55ee8a19 [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;
21import android.graphics.Point;
Adam Cohen2e6da152015-05-06 11:42:25 -070022import android.util.DisplayMetrics;
Adam Cohen2e6da152015-05-06 11:42:25 -070023import android.view.Display;
24import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070025
Sunny Goyalbb011da2016-06-15 15:42:29 -070026import com.android.launcher3.config.FeatureFlags;
27import com.android.launcher3.config.ProviderConfig;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import com.android.launcher3.util.Thunk;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070029
Adam Cohen2e6da152015-05-06 11:42:25 -070030import java.util.ArrayList;
31import java.util.Collections;
32import java.util.Comparator;
33
34public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070035
36 // 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 -070037 private static float DEFAULT_ICON_SIZE_DP = 60;
Adam Cohen2e6da152015-05-06 11:42:25 -070038
Sunny Goyal53d7ee42015-05-22 12:25:45 -070039 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
40
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070041 // Constants that affects the interpolation curve between statically defined device profile
42 // buckets.
43 private static float KNEARESTNEIGHBOR = 3;
44 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070045
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070046 // used to offset float not being able to express extremely small weights in extreme cases.
47 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070048
49 // Profile-defining invariant properties
50 String name;
51 float minWidthDps;
52 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070053
54 /**
55 * Number of icons per row and column in the workspace.
56 */
Adam Cohen2e6da152015-05-06 11:42:25 -070057 public int numRows;
58 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070059
60 /**
Winson Chung2c6e5cc2015-06-01 14:38:24 -070061 * The minimum number of predicted apps in all apps.
62 */
63 int minAllAppsPredictionColumns;
64
65 /**
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070066 * Number of icons per row and column in the folder.
67 */
Adam Cohen2e6da152015-05-06 11:42:25 -070068 public int numFolderRows;
69 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070070 public float iconSize;
71 public int iconBitmapSize;
72 public int fillResIconDpi;
73 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070074
75 /**
76 * Number of icons inside the hotseat area.
77 */
Sunny Goyalf862a262015-12-14 14:27:38 -080078 public int numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -070079 float hotseatIconSize;
80 int defaultLayoutId;
81
Sunny Goyalc6205602015-05-21 20:46:33 -070082 DeviceProfile landscapeProfile;
83 DeviceProfile portraitProfile;
84
Sunny Goyal6f866092016-03-17 17:04:15 -070085 public Point defaultWallpaperSize;
86
Sunny Goyalf076eae2016-01-11 12:25:10 -080087 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -070088 }
89
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070090 public InvariantDeviceProfile(InvariantDeviceProfile p) {
91 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Winson Chung2c6e5cc2015-06-01 14:38:24 -070092 p.numFolderRows, p.numFolderColumns, p.minAllAppsPredictionColumns,
93 p.iconSize, p.iconTextSize, p.numHotseatIcons, p.hotseatIconSize,
94 p.defaultLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070095 }
96
Winson Chung2c6e5cc2015-06-01 14:38:24 -070097 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc,
Tony Wickhamd6b40372015-09-23 18:37:57 -070098 float is, float its, int hs, float his, int dlId) {
Adam Cohen2e6da152015-05-06 11:42:25 -070099 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
100 if (hs % 2 == 0) {
101 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
102 }
103
104 name = n;
105 minWidthDps = w;
106 minHeightDps = h;
107 numRows = r;
108 numColumns = c;
109 numFolderRows = fr;
110 numFolderColumns = fc;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700111 minAllAppsPredictionColumns = maapc;
Adam Cohen2e6da152015-05-06 11:42:25 -0700112 iconSize = is;
113 iconTextSize = its;
114 numHotseatIcons = hs;
115 hotseatIconSize = his;
116 defaultLayoutId = dlId;
117 }
118
Sunny Goyalbbf01842015-10-08 07:41:15 -0700119 @TargetApi(23)
Adam Cohen2e6da152015-05-06 11:42:25 -0700120 InvariantDeviceProfile(Context context) {
121 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
122 Display display = wm.getDefaultDisplay();
123 DisplayMetrics dm = new DisplayMetrics();
124 display.getMetrics(dm);
125
126 Point smallestSize = new Point();
127 Point largestSize = new Point();
128 display.getCurrentSizeRange(smallestSize, largestSize);
129
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700130 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700131 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
132 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
133
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700134 ArrayList<InvariantDeviceProfile> closestProfiles =
135 findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles());
136 InvariantDeviceProfile interpolatedDeviceProfileOut =
137 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700138
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700139 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700140 numRows = closestProfile.numRows;
141 numColumns = closestProfile.numColumns;
142 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700143 defaultLayoutId = closestProfile.defaultLayoutId;
144 numFolderRows = closestProfile.numFolderRows;
145 numFolderColumns = closestProfile.numFolderColumns;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700146 minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns;
Adam Cohen2e6da152015-05-06 11:42:25 -0700147
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700148 iconSize = interpolatedDeviceProfileOut.iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700149 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700150 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
151 hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700152 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700153
154 // If the partner customization apk contains any grid overrides, apply them
155 // Supported overrides: numRows, numColumns, iconSize
156 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700157
158 Point realSize = new Point();
159 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700160 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700161 // same in any orientation.
162 int smallSide = Math.min(realSize.x, realSize.y);
163 int largeSide = Math.max(realSize.x, realSize.y);
164
165 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
166 largeSide, smallSide, true /* isLandscape */);
167 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
168 smallSide, largeSide, false /* isLandscape */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700169
170 // We need to ensure that there is enough extra space in the wallpaper
171 // for the intended parallax effects
172 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
173 defaultWallpaperSize = new Point(
174 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
175 largeSide);
176 } else {
177 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
178 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700179 }
180
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700181 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles() {
182 ArrayList<InvariantDeviceProfile> predefinedDeviceProfiles = new ArrayList<>();
183 // width, height, #rows, #columns, #folder rows, #folder columns,
184 // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId.
185 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
Sunny Goyal96a09632015-12-16 11:32:54 -0800186 255, 300, 2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_3x3));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700187 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
Sunny Goyal96a09632015-12-16 11:32:54 -0800188 255, 400, 3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_3x3));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700189 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700190 275, 420, 3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700191 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700192 255, 450, 3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700193 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus S",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700194 296, 491.33f, 4, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700195 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700196 359, 567, 4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
Tony Wickham58ecbe42015-09-29 15:15:53 -0700197 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
198 335, 567, 4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700199 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700200 406, 694, 5, 5, 4, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700201 // The tablet profile is odd in that the landscape orientation
202 // also includes the nav bar on the side
203 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700204 575, 904, 5, 6, 4, 5, 4, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700205 // Larger tablet profiles always have system bars on the top & bottom
206 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
Tony Wickhamd6b40372015-09-23 18:37:57 -0700207 727, 1207, 5, 6, 4, 5, 4, 76, 14.4f, 7, 76, R.xml.default_workspace_5x6));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700208 predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
Sunny Goyal96a09632015-12-16 11:32:54 -0800209 1527, 2527, 7, 7, 6, 6, 4, 100, 20, 7, 72, R.xml.default_workspace_5x6));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700210 return predefinedDeviceProfiles;
211 }
212
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700213 private int getLauncherIconDensity(int requiredSize) {
214 // Densities typically defined by an app.
215 int[] densityBuckets = new int[] {
216 DisplayMetrics.DENSITY_LOW,
217 DisplayMetrics.DENSITY_MEDIUM,
218 DisplayMetrics.DENSITY_TV,
219 DisplayMetrics.DENSITY_HIGH,
220 DisplayMetrics.DENSITY_XHIGH,
221 DisplayMetrics.DENSITY_XXHIGH,
222 DisplayMetrics.DENSITY_XXXHIGH
223 };
224
225 int density = DisplayMetrics.DENSITY_XXXHIGH;
226 for (int i = densityBuckets.length - 1; i >= 0; i--) {
227 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
228 / DisplayMetrics.DENSITY_DEFAULT;
229 if (expectedSize >= requiredSize) {
230 density = densityBuckets[i];
231 }
232 }
233
234 return density;
235 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700236
Adam Cohen2e6da152015-05-06 11:42:25 -0700237 /**
238 * Apply any Partner customization grid overrides.
239 *
240 * Currently we support: all apps row / column count.
241 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700242 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
243 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700244 if (p != null) {
245 p.applyInvariantDeviceProfileOverrides(this, dm);
246 }
247 }
248
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700249 @Thunk float dist(float x0, float y0, float x1, float y1) {
250 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700251 }
252
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700253 /**
254 * Returns the closest device profiles ordered by closeness to the specified width and height
255 */
256 // Package private visibility for testing.
257 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
258 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700259
260 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700261 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
262 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
263 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700264 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
265 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700266 }
267 });
268
269 return pointsByNearness;
270 }
271
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700272 // Package private visibility for testing.
273 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
274 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700275 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700276
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700277 InvariantDeviceProfile p = points.get(0);
278 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
279 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700280 }
281
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700282 InvariantDeviceProfile out = new InvariantDeviceProfile();
283 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
284 p = new InvariantDeviceProfile(points.get(i));
285 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
286 weights += w;
287 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700288 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700289 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700290 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700291
292 private void add(InvariantDeviceProfile p) {
293 iconSize += p.iconSize;
294 iconTextSize += p.iconTextSize;
295 hotseatIconSize += p.hotseatIconSize;
296 }
297
298 private InvariantDeviceProfile multiply(float w) {
299 iconSize *= w;
300 iconTextSize *= w;
301 hotseatIconSize *= w;
302 return this;
303 }
304
Sunny Goyalbb011da2016-06-15 15:42:29 -0700305 public int getAllAppsButtonRank() {
306 if (ProviderConfig.IS_DOGFOOD_BUILD && FeatureFlags.NO_ALL_APPS_ICON) {
307 throw new IllegalAccessError("Accessing all apps rank when all-apps is disabled");
308 }
309 return numHotseatIcons / 2;
310 }
311
312 public boolean isAllAppsButtonRank(int rank) {
313 return rank == getAllAppsButtonRank();
314 }
315
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700316 private float weight(float x0, float y0, float x1, float y1, float pow) {
317 float d = dist(x0, y0, x1, y1);
318 if (Float.compare(d, 0f) == 0) {
319 return Float.POSITIVE_INFINITY;
320 }
321 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
322 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700323
324 /**
325 * As a ratio of screen height, the total distance we want the parallax effect to span
326 * horizontally
327 */
328 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
329 float aspectRatio = width / (float) height;
330
331 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
332 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
333 // We will use these two data points to extrapolate how much the wallpaper parallax effect
334 // to span (ie travel) at any aspect ratio:
335
336 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
337 final float ASPECT_RATIO_PORTRAIT = 10/16f;
338 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
339 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
340
341 // To find out the desired width at different aspect ratios, we use the following two
342 // formulas, where the coefficient on x is the aspect ratio (width/height):
343 // (16/10)x + y = 1.5
344 // (10/16)x + y = 1.2
345 // We solve for x and y and end up with a final formula:
346 final float x =
347 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
348 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
349 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
350 return x * aspectRatio + y;
351 }
352
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700353}