blob: 5914f447586644c97194f39fdc56542482d38bbc [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;
Sunny Goyal0abb36f2015-06-23 10:53:59 -070020import android.app.ActivityManager;
Adam Cohen2e6da152015-05-06 11:42:25 -070021import android.content.Context;
22import android.graphics.Point;
Adam Cohen2e6da152015-05-06 11:42:25 -070023import android.util.DisplayMetrics;
Adam Cohen2e6da152015-05-06 11:42:25 -070024import android.view.Display;
25import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070026
Adam Cohen2e6da152015-05-06 11:42:25 -070027import com.android.launcher3.util.Thunk;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070028
Adam Cohen2e6da152015-05-06 11:42:25 -070029import java.util.ArrayList;
30import java.util.Collections;
31import java.util.Comparator;
32
33public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070034
35 // 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 -070036 private static float DEFAULT_ICON_SIZE_DP = 60;
Adam Cohen2e6da152015-05-06 11:42:25 -070037
Sunny Goyal53d7ee42015-05-22 12:25:45 -070038 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
39
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070040 // Constants that affects the interpolation curve between statically defined device profile
41 // buckets.
42 private static float KNEARESTNEIGHBOR = 3;
43 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070044
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070045 // used to offset float not being able to express extremely small weights in extreme cases.
46 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070047
48 // Profile-defining invariant properties
49 String name;
50 float minWidthDps;
51 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070052
53 /**
54 * Number of icons per row and column in the workspace.
55 */
Adam Cohen2e6da152015-05-06 11:42:25 -070056 public int numRows;
57 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070058
59 /**
Winson Chung2c6e5cc2015-06-01 14:38:24 -070060 * The minimum number of predicted apps in all apps.
61 */
62 int minAllAppsPredictionColumns;
63
64 /**
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070065 * Number of icons per row and column in the folder.
66 */
Adam Cohen2e6da152015-05-06 11:42:25 -070067 public int numFolderRows;
68 public int numFolderColumns;
69 float iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -070070 int iconBitmapSize;
71 int fillResIconDpi;
Adam Cohen2e6da152015-05-06 11:42:25 -070072 float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070073
74 /**
75 * Number of icons inside the hotseat area.
76 */
Adam Cohen2e6da152015-05-06 11:42:25 -070077 float numHotseatIcons;
78 float hotseatIconSize;
79 int defaultLayoutId;
80
81 // Derived invariant properties
82 int hotseatAllAppsRank;
83
Sunny Goyalc6205602015-05-21 20:46:33 -070084 DeviceProfile landscapeProfile;
85 DeviceProfile portraitProfile;
86
Sunny Goyal0abb36f2015-06-23 10:53:59 -070087 // On Marshmallow the status bar is no longer opaque, when drawn on the right.
88 public boolean isRightInsetOpaque;
89
Adam Cohen2e6da152015-05-06 11:42:25 -070090 InvariantDeviceProfile() {
91 }
92
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070093 public InvariantDeviceProfile(InvariantDeviceProfile p) {
94 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Winson Chung2c6e5cc2015-06-01 14:38:24 -070095 p.numFolderRows, p.numFolderColumns, p.minAllAppsPredictionColumns,
96 p.iconSize, p.iconTextSize, p.numHotseatIcons, p.hotseatIconSize,
97 p.defaultLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070098 }
99
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700100 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc,
Adam Cohen2e6da152015-05-06 11:42:25 -0700101 float is, float its, float hs, float his, int dlId) {
102 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
103 if (hs % 2 == 0) {
104 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
105 }
106
107 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;
116 iconTextSize = its;
117 numHotseatIcons = hs;
118 hotseatIconSize = his;
119 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
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700137 ArrayList<InvariantDeviceProfile> closestProfiles =
138 findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles());
139 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;
146 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
147 defaultLayoutId = closestProfile.defaultLayoutId;
148 numFolderRows = closestProfile.numFolderRows;
149 numFolderColumns = closestProfile.numFolderColumns;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700150 minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns;
Adam Cohen2e6da152015-05-06 11:42:25 -0700151
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700152 iconSize = interpolatedDeviceProfileOut.iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700153 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700154 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
155 hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700156 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700157
158 // If the partner customization apk contains any grid overrides, apply them
159 // Supported overrides: numRows, numColumns, iconSize
160 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700161
162 Point realSize = new Point();
163 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700164 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700165 // same in any orientation.
166 int smallSide = Math.min(realSize.x, realSize.y);
167 int largeSide = Math.max(realSize.x, realSize.y);
168
169 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
170 largeSide, smallSide, true /* isLandscape */);
171 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
172 smallSide, largeSide, false /* isLandscape */);
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700173
174 isRightInsetOpaque = !Utilities.ATLEAST_MARSHMALLOW ||
175 context.getSystemService(ActivityManager.class).isLowRamDevice();
Adam Cohen2e6da152015-05-06 11:42:25 -0700176 }
177
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700178 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles() {
179 ArrayList<InvariantDeviceProfile> predefinedDeviceProfiles = new ArrayList<>();
180 // width, height, #rows, #columns, #folder rows, #folder columns,
181 // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId.
182 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700183 255, 300, 2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700184 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700185 255, 400, 3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700186 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700187 275, 420, 3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700188 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700189 255, 450, 3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700190 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus S",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700191 296, 491.33f, 4, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700192 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700193 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 -0700194 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
195 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 -0700196 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700197 406, 694, 5, 5, 4, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700198 // The tablet profile is odd in that the landscape orientation
199 // also includes the nav bar on the side
200 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700201 575, 904, 5, 6, 4, 5, 4, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700202 // Larger tablet profiles always have system bars on the top & bottom
203 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700204 727, 1207, 5, 6, 4, 5, 4, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700205 predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700206 1527, 2527, 7, 7, 6, 6, 4, 100, 20, 7, 72, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700207 return predefinedDeviceProfiles;
208 }
209
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700210 private int getLauncherIconDensity(int requiredSize) {
211 // Densities typically defined by an app.
212 int[] densityBuckets = new int[] {
213 DisplayMetrics.DENSITY_LOW,
214 DisplayMetrics.DENSITY_MEDIUM,
215 DisplayMetrics.DENSITY_TV,
216 DisplayMetrics.DENSITY_HIGH,
217 DisplayMetrics.DENSITY_XHIGH,
218 DisplayMetrics.DENSITY_XXHIGH,
219 DisplayMetrics.DENSITY_XXXHIGH
220 };
221
222 int density = DisplayMetrics.DENSITY_XXXHIGH;
223 for (int i = densityBuckets.length - 1; i >= 0; i--) {
224 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
225 / DisplayMetrics.DENSITY_DEFAULT;
226 if (expectedSize >= requiredSize) {
227 density = densityBuckets[i];
228 }
229 }
230
231 return density;
232 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700233
Adam Cohen2e6da152015-05-06 11:42:25 -0700234 /**
235 * Apply any Partner customization grid overrides.
236 *
237 * Currently we support: all apps row / column count.
238 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700239 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
240 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700241 if (p != null) {
242 p.applyInvariantDeviceProfileOverrides(this, dm);
243 }
244 }
245
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700246 @Thunk float dist(float x0, float y0, float x1, float y1) {
247 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700248 }
249
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700250 /**
251 * Returns the closest device profiles ordered by closeness to the specified width and height
252 */
253 // Package private visibility for testing.
254 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
255 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700256
257 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700258 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
259 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
260 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700261 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
262 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700263 }
264 });
265
266 return pointsByNearness;
267 }
268
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700269 // Package private visibility for testing.
270 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
271 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700272 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700273
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700274 InvariantDeviceProfile p = points.get(0);
275 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
276 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700277 }
278
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700279 InvariantDeviceProfile out = new InvariantDeviceProfile();
280 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
281 p = new InvariantDeviceProfile(points.get(i));
282 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
283 weights += w;
284 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700285 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700286 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700287 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700288
289 private void add(InvariantDeviceProfile p) {
290 iconSize += p.iconSize;
291 iconTextSize += p.iconTextSize;
292 hotseatIconSize += p.hotseatIconSize;
293 }
294
295 private InvariantDeviceProfile multiply(float w) {
296 iconSize *= w;
297 iconTextSize *= w;
298 hotseatIconSize *= w;
299 return this;
300 }
301
302 private float weight(float x0, float y0, float x1, float y1, float pow) {
303 float d = dist(x0, y0, x1, y1);
304 if (Float.compare(d, 0f) == 0) {
305 return Float.POSITIVE_INFINITY;
306 }
307 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
308 }
309}