blob: 20a77b5af6884eed1402616342e9e1abe799c9cb [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;
Sunny Goyalc6205602015-05-21 20:46:33 -070023import android.os.Build;
Adam Cohen2e6da152015-05-06 11:42:25 -070024import android.util.DisplayMetrics;
Adam Cohen2e6da152015-05-06 11:42:25 -070025import android.view.Display;
26import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070027
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;
70 float iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -070071 int iconBitmapSize;
72 int fillResIconDpi;
Adam Cohen2e6da152015-05-06 11:42:25 -070073 float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070074
75 /**
76 * Number of icons inside the hotseat area.
77 */
Adam Cohen2e6da152015-05-06 11:42:25 -070078 float numHotseatIcons;
79 float hotseatIconSize;
80 int defaultLayoutId;
81
82 // Derived invariant properties
83 int hotseatAllAppsRank;
84
Sunny Goyalc6205602015-05-21 20:46:33 -070085 DeviceProfile landscapeProfile;
86 DeviceProfile portraitProfile;
87
Sunny Goyal0abb36f2015-06-23 10:53:59 -070088 // On Marshmallow the status bar is no longer opaque, when drawn on the right.
89 public boolean isRightInsetOpaque;
90
Adam Cohen2e6da152015-05-06 11:42:25 -070091 InvariantDeviceProfile() {
92 }
93
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070094 public InvariantDeviceProfile(InvariantDeviceProfile p) {
95 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
Winson Chung2c6e5cc2015-06-01 14:38:24 -070096 p.numFolderRows, p.numFolderColumns, p.minAllAppsPredictionColumns,
97 p.iconSize, p.iconTextSize, p.numHotseatIcons, p.hotseatIconSize,
98 p.defaultLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099 }
100
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700101 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc,
Adam Cohen2e6da152015-05-06 11:42:25 -0700102 float is, float its, float hs, float his, int dlId) {
103 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
104 if (hs % 2 == 0) {
105 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
106 }
107
108 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;
121 }
122
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700123 @TargetApi(Build.VERSION_CODES.M)
Adam Cohen2e6da152015-05-06 11:42:25 -0700124 InvariantDeviceProfile(Context context) {
125 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
126 Display display = wm.getDefaultDisplay();
127 DisplayMetrics dm = new DisplayMetrics();
128 display.getMetrics(dm);
129
130 Point smallestSize = new Point();
131 Point largestSize = new Point();
132 display.getCurrentSizeRange(smallestSize, largestSize);
133
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700134 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700135 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
136 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
137
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700138 ArrayList<InvariantDeviceProfile> closestProfiles =
139 findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles());
140 InvariantDeviceProfile interpolatedDeviceProfileOut =
141 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700142
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700143 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700144 numRows = closestProfile.numRows;
145 numColumns = closestProfile.numColumns;
146 numHotseatIcons = closestProfile.numHotseatIcons;
147 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
148 defaultLayoutId = closestProfile.defaultLayoutId;
149 numFolderRows = closestProfile.numFolderRows;
150 numFolderColumns = closestProfile.numFolderColumns;
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700151 minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns;
Adam Cohen2e6da152015-05-06 11:42:25 -0700152
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700153 iconSize = interpolatedDeviceProfileOut.iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700154 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700155 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
156 hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700157 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700158
159 // If the partner customization apk contains any grid overrides, apply them
160 // Supported overrides: numRows, numColumns, iconSize
161 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700162
163 Point realSize = new Point();
164 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700165 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700166 // same in any orientation.
167 int smallSide = Math.min(realSize.x, realSize.y);
168 int largeSide = Math.max(realSize.x, realSize.y);
169
170 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
171 largeSide, smallSide, true /* isLandscape */);
172 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
173 smallSide, largeSide, false /* isLandscape */);
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700174
175 isRightInsetOpaque = !Utilities.ATLEAST_MARSHMALLOW ||
176 context.getSystemService(ActivityManager.class).isLowRamDevice();
Adam Cohen2e6da152015-05-06 11:42:25 -0700177 }
178
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700179 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles() {
180 ArrayList<InvariantDeviceProfile> predefinedDeviceProfiles = new ArrayList<>();
181 // width, height, #rows, #columns, #folder rows, #folder columns,
182 // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId.
183 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700184 255, 300, 2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700185 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700186 255, 400, 3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700187 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700188 275, 420, 3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700189 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700190 255, 450, 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("Nexus S",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700192 296, 491.33f, 4, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700193 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700194 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 -0700195 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
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));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700197 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700198 406, 694, 5, 5, 4, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700199 // The tablet profile is odd in that the landscape orientation
200 // also includes the nav bar on the side
201 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700202 575, 904, 5, 6, 4, 5, 4, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700203 // Larger tablet profiles always have system bars on the top & bottom
204 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700205 727, 1207, 5, 6, 4, 5, 4, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700206 predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
Winson Chung2c6e5cc2015-06-01 14:38:24 -0700207 1527, 2527, 7, 7, 6, 6, 4, 100, 20, 7, 72, R.xml.default_workspace_4x4));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700208 return predefinedDeviceProfiles;
209 }
210
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700211 private int getLauncherIconDensity(int requiredSize) {
212 // Densities typically defined by an app.
213 int[] densityBuckets = new int[] {
214 DisplayMetrics.DENSITY_LOW,
215 DisplayMetrics.DENSITY_MEDIUM,
216 DisplayMetrics.DENSITY_TV,
217 DisplayMetrics.DENSITY_HIGH,
218 DisplayMetrics.DENSITY_XHIGH,
219 DisplayMetrics.DENSITY_XXHIGH,
220 DisplayMetrics.DENSITY_XXXHIGH
221 };
222
223 int density = DisplayMetrics.DENSITY_XXXHIGH;
224 for (int i = densityBuckets.length - 1; i >= 0; i--) {
225 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
226 / DisplayMetrics.DENSITY_DEFAULT;
227 if (expectedSize >= requiredSize) {
228 density = densityBuckets[i];
229 }
230 }
231
232 return density;
233 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700234
Adam Cohen2e6da152015-05-06 11:42:25 -0700235 /**
236 * Apply any Partner customization grid overrides.
237 *
238 * Currently we support: all apps row / column count.
239 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700240 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
241 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700242 if (p != null) {
243 p.applyInvariantDeviceProfileOverrides(this, dm);
244 }
245 }
246
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700247 @Thunk float dist(float x0, float y0, float x1, float y1) {
248 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700249 }
250
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700251 /**
252 * Returns the closest device profiles ordered by closeness to the specified width and height
253 */
254 // Package private visibility for testing.
255 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
256 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700257
258 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700259 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
260 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
261 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
262 return (int) (dist(width, height, a.minWidthDps, a.minHeightDps)
263 - dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700264 }
265 });
266
267 return pointsByNearness;
268 }
269
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700270 // Package private visibility for testing.
271 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
272 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700273 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700274
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700275 InvariantDeviceProfile p = points.get(0);
276 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
277 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700278 }
279
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700280 InvariantDeviceProfile out = new InvariantDeviceProfile();
281 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
282 p = new InvariantDeviceProfile(points.get(i));
283 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
284 weights += w;
285 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700286 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700287 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700288 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700289
290 private void add(InvariantDeviceProfile p) {
291 iconSize += p.iconSize;
292 iconTextSize += p.iconTextSize;
293 hotseatIconSize += p.hotseatIconSize;
294 }
295
296 private InvariantDeviceProfile multiply(float w) {
297 iconSize *= w;
298 iconTextSize *= w;
299 hotseatIconSize *= w;
300 return this;
301 }
302
303 private float weight(float x0, float y0, float x1, float y1, float pow) {
304 float d = dist(x0, y0, x1, y1);
305 if (Float.compare(d, 0f) == 0) {
306 return Float.POSITIVE_INFINITY;
307 }
308 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
309 }
310}