blob: fcaf834a22b4ed0e3d7355a30a865a2ce06c5586 [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;
Sunny Goyalc6205602015-05-21 20:46:33 -070022import android.os.Build;
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 /**
60 * Number of icons per row and column in the folder.
61 */
Adam Cohen2e6da152015-05-06 11:42:25 -070062 public int numFolderRows;
63 public int numFolderColumns;
64 float iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -070065 int iconBitmapSize;
66 int fillResIconDpi;
Adam Cohen2e6da152015-05-06 11:42:25 -070067 float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070068
69 /**
70 * Number of icons inside the hotseat area.
71 */
Adam Cohen2e6da152015-05-06 11:42:25 -070072 float numHotseatIcons;
73 float hotseatIconSize;
74 int defaultLayoutId;
75
76 // Derived invariant properties
77 int hotseatAllAppsRank;
78
Sunny Goyalc6205602015-05-21 20:46:33 -070079 DeviceProfile landscapeProfile;
80 DeviceProfile portraitProfile;
81
Adam Cohen2e6da152015-05-06 11:42:25 -070082 InvariantDeviceProfile() {
83 }
84
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070085 public InvariantDeviceProfile(InvariantDeviceProfile p) {
86 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
87 p.numFolderRows, p.numFolderColumns, p.iconSize, p.iconTextSize, p.numHotseatIcons,
88 p.hotseatIconSize, p.defaultLayoutId);
89 }
90
Adam Cohen2e6da152015-05-06 11:42:25 -070091 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc,
92 float is, float its, float hs, float his, int dlId) {
93 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
94 if (hs % 2 == 0) {
95 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
96 }
97
98 name = n;
99 minWidthDps = w;
100 minHeightDps = h;
101 numRows = r;
102 numColumns = c;
103 numFolderRows = fr;
104 numFolderColumns = fc;
105 iconSize = is;
106 iconTextSize = its;
107 numHotseatIcons = hs;
108 hotseatIconSize = his;
109 defaultLayoutId = dlId;
110 }
111
Sunny Goyalc6205602015-05-21 20:46:33 -0700112 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2e6da152015-05-06 11:42:25 -0700113 InvariantDeviceProfile(Context context) {
114 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
115 Display display = wm.getDefaultDisplay();
116 DisplayMetrics dm = new DisplayMetrics();
117 display.getMetrics(dm);
118
119 Point smallestSize = new Point();
120 Point largestSize = new Point();
121 display.getCurrentSizeRange(smallestSize, largestSize);
122
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700123 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700124 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
125 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
126
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700127 ArrayList<InvariantDeviceProfile> closestProfiles =
128 findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles());
129 InvariantDeviceProfile interpolatedDeviceProfileOut =
130 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700131
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700132 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700133 numRows = closestProfile.numRows;
134 numColumns = closestProfile.numColumns;
135 numHotseatIcons = closestProfile.numHotseatIcons;
136 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
137 defaultLayoutId = closestProfile.defaultLayoutId;
138 numFolderRows = closestProfile.numFolderRows;
139 numFolderColumns = closestProfile.numFolderColumns;
140
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700141 iconSize = interpolatedDeviceProfileOut.iconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700142 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700143 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
144 hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700145 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700146
147 // If the partner customization apk contains any grid overrides, apply them
148 // Supported overrides: numRows, numColumns, iconSize
149 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700150
151 Point realSize = new Point();
152 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700153 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700154 // same in any orientation.
155 int smallSide = Math.min(realSize.x, realSize.y);
156 int largeSide = Math.max(realSize.x, realSize.y);
157
158 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
159 largeSide, smallSide, true /* isLandscape */);
160 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
161 smallSide, largeSide, false /* isLandscape */);
Adam Cohen2e6da152015-05-06 11:42:25 -0700162 }
163
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700164 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles() {
165 ArrayList<InvariantDeviceProfile> predefinedDeviceProfiles = new ArrayList<>();
166 // width, height, #rows, #columns, #folder rows, #folder columns,
167 // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId.
168 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
169 255, 300, 2, 3, 2, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
170 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
171 255, 400, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
172 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
173 275, 420, 3, 4, 3, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
174 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
175 255, 450, 3, 4, 3, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
176 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus S",
177 296, 491.33f, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
178 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
179 335, 567, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
180 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
181 359, 567, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
182 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
183 406, 694, 5, 5, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
184 // The tablet profile is odd in that the landscape orientation
185 // also includes the nav bar on the side
186 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7",
187 575, 904, 5, 6, 4, 5, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
188 // Larger tablet profiles always have system bars on the top & bottom
189 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
190 727, 1207, 5, 6, 4, 5, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6));
191 predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
192 1527, 2527, 7, 7, 6, 6, 100, 20, 7, 72, R.xml.default_workspace_4x4));
193 return predefinedDeviceProfiles;
194 }
195
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700196 private int getLauncherIconDensity(int requiredSize) {
197 // Densities typically defined by an app.
198 int[] densityBuckets = new int[] {
199 DisplayMetrics.DENSITY_LOW,
200 DisplayMetrics.DENSITY_MEDIUM,
201 DisplayMetrics.DENSITY_TV,
202 DisplayMetrics.DENSITY_HIGH,
203 DisplayMetrics.DENSITY_XHIGH,
204 DisplayMetrics.DENSITY_XXHIGH,
205 DisplayMetrics.DENSITY_XXXHIGH
206 };
207
208 int density = DisplayMetrics.DENSITY_XXXHIGH;
209 for (int i = densityBuckets.length - 1; i >= 0; i--) {
210 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
211 / DisplayMetrics.DENSITY_DEFAULT;
212 if (expectedSize >= requiredSize) {
213 density = densityBuckets[i];
214 }
215 }
216
217 return density;
218 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700219
Adam Cohen2e6da152015-05-06 11:42:25 -0700220 /**
221 * Apply any Partner customization grid overrides.
222 *
223 * Currently we support: all apps row / column count.
224 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700225 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
226 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700227 if (p != null) {
228 p.applyInvariantDeviceProfileOverrides(this, dm);
229 }
230 }
231
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700232 @Thunk float dist(float x0, float y0, float x1, float y1) {
233 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700234 }
235
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700236 /**
237 * Returns the closest device profiles ordered by closeness to the specified width and height
238 */
239 // Package private visibility for testing.
240 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
241 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700242
243 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700244 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
245 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
246 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
247 return (int) (dist(width, height, a.minWidthDps, a.minHeightDps)
248 - dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700249 }
250 });
251
252 return pointsByNearness;
253 }
254
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700255 // Package private visibility for testing.
256 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
257 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700258 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700259
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700260 InvariantDeviceProfile p = points.get(0);
261 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
262 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700263 }
264
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700265 InvariantDeviceProfile out = new InvariantDeviceProfile();
266 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
267 p = new InvariantDeviceProfile(points.get(i));
268 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
269 weights += w;
270 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700271 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700272 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700273 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700274
275 private void add(InvariantDeviceProfile p) {
276 iconSize += p.iconSize;
277 iconTextSize += p.iconTextSize;
278 hotseatIconSize += p.hotseatIconSize;
279 }
280
281 private InvariantDeviceProfile multiply(float w) {
282 iconSize *= w;
283 iconTextSize *= w;
284 hotseatIconSize *= w;
285 return this;
286 }
287
288 private float weight(float x0, float y0, float x1, float y1, float pow) {
289 float d = dist(x0, y0, x1, y1);
290 if (Float.compare(d, 0f) == 0) {
291 return Float.POSITIVE_INFINITY;
292 }
293 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
294 }
295}