blob: 7f34593a9a0e3f766018a5c0e9802c680958d366 [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
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070038 // Constants that affects the interpolation curve between statically defined device profile
39 // buckets.
40 private static float KNEARESTNEIGHBOR = 3;
41 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070042
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070043 // used to offset float not being able to express extremely small weights in extreme cases.
44 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070045
46 // Profile-defining invariant properties
47 String name;
48 float minWidthDps;
49 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070050
51 /**
52 * Number of icons per row and column in the workspace.
53 */
Adam Cohen2e6da152015-05-06 11:42:25 -070054 public int numRows;
55 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070056
57 /**
58 * Number of icons per row and column in the folder.
59 */
Adam Cohen2e6da152015-05-06 11:42:25 -070060 public int numFolderRows;
61 public int numFolderColumns;
62 float iconSize;
63 float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070064
65 /**
66 * Number of icons inside the hotseat area.
67 */
Adam Cohen2e6da152015-05-06 11:42:25 -070068 float numHotseatIcons;
69 float hotseatIconSize;
70 int defaultLayoutId;
71
72 // Derived invariant properties
73 int hotseatAllAppsRank;
74
Sunny Goyalc6205602015-05-21 20:46:33 -070075 DeviceProfile landscapeProfile;
76 DeviceProfile portraitProfile;
77
Adam Cohen2e6da152015-05-06 11:42:25 -070078 InvariantDeviceProfile() {
79 }
80
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070081 public InvariantDeviceProfile(InvariantDeviceProfile p) {
82 this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns,
83 p.numFolderRows, p.numFolderColumns, p.iconSize, p.iconTextSize, p.numHotseatIcons,
84 p.hotseatIconSize, p.defaultLayoutId);
85 }
86
Adam Cohen2e6da152015-05-06 11:42:25 -070087 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc,
88 float is, float its, float hs, float his, int dlId) {
89 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
90 if (hs % 2 == 0) {
91 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
92 }
93
94 name = n;
95 minWidthDps = w;
96 minHeightDps = h;
97 numRows = r;
98 numColumns = c;
99 numFolderRows = fr;
100 numFolderColumns = fc;
101 iconSize = is;
102 iconTextSize = its;
103 numHotseatIcons = hs;
104 hotseatIconSize = his;
105 defaultLayoutId = dlId;
106 }
107
Sunny Goyalc6205602015-05-21 20:46:33 -0700108 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2e6da152015-05-06 11:42:25 -0700109 InvariantDeviceProfile(Context context) {
110 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
111 Display display = wm.getDefaultDisplay();
112 DisplayMetrics dm = new DisplayMetrics();
113 display.getMetrics(dm);
114
115 Point smallestSize = new Point();
116 Point largestSize = new Point();
117 display.getCurrentSizeRange(smallestSize, largestSize);
118
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700119 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700120 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
121 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
122
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700123 ArrayList<InvariantDeviceProfile> closestProfiles =
124 findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles());
125 InvariantDeviceProfile interpolatedDeviceProfileOut =
126 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700127
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700128 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700129 numRows = closestProfile.numRows;
130 numColumns = closestProfile.numColumns;
131 numHotseatIcons = closestProfile.numHotseatIcons;
132 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
133 defaultLayoutId = closestProfile.defaultLayoutId;
134 numFolderRows = closestProfile.numFolderRows;
135 numFolderColumns = closestProfile.numFolderColumns;
136
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700137 iconSize = interpolatedDeviceProfileOut.iconSize;
138 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
139 hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize;
Adam Cohen2e6da152015-05-06 11:42:25 -0700140
141 // If the partner customization apk contains any grid overrides, apply them
142 // Supported overrides: numRows, numColumns, iconSize
143 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700144
145 Point realSize = new Point();
146 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700147 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700148 // same in any orientation.
149 int smallSide = Math.min(realSize.x, realSize.y);
150 int largeSide = Math.max(realSize.x, realSize.y);
151
152 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
153 largeSide, smallSide, true /* isLandscape */);
154 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
155 smallSide, largeSide, false /* isLandscape */);
Adam Cohen2e6da152015-05-06 11:42:25 -0700156 }
157
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700158 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles() {
159 ArrayList<InvariantDeviceProfile> predefinedDeviceProfiles = new ArrayList<>();
160 // width, height, #rows, #columns, #folder rows, #folder columns,
161 // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId.
162 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
163 255, 300, 2, 3, 2, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
164 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
165 255, 400, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
166 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
167 275, 420, 3, 4, 3, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
168 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
169 255, 450, 3, 4, 3, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
170 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus S",
171 296, 491.33f, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
172 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
173 335, 567, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
174 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
175 359, 567, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
176 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
177 406, 694, 5, 5, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
178 // The tablet profile is odd in that the landscape orientation
179 // also includes the nav bar on the side
180 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7",
181 575, 904, 5, 6, 4, 5, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
182 // Larger tablet profiles always have system bars on the top & bottom
183 predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
184 727, 1207, 5, 6, 4, 5, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6));
185 predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
186 1527, 2527, 7, 7, 6, 6, 100, 20, 7, 72, R.xml.default_workspace_4x4));
187 return predefinedDeviceProfiles;
188 }
189
190
Adam Cohen2e6da152015-05-06 11:42:25 -0700191 /**
192 * Apply any Partner customization grid overrides.
193 *
194 * Currently we support: all apps row / column count.
195 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700196 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
197 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700198 if (p != null) {
199 p.applyInvariantDeviceProfileOverrides(this, dm);
200 }
201 }
202
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700203 @Thunk float dist(float x0, float y0, float x1, float y1) {
204 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700205 }
206
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700207 /**
208 * Returns the closest device profiles ordered by closeness to the specified width and height
209 */
210 // Package private visibility for testing.
211 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
212 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700213
214 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700215 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
216 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
217 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
218 return (int) (dist(width, height, a.minWidthDps, a.minHeightDps)
219 - dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700220 }
221 });
222
223 return pointsByNearness;
224 }
225
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700226 // Package private visibility for testing.
227 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
228 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700229 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700230
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700231 InvariantDeviceProfile p = points.get(0);
232 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
233 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700234 }
235
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700236 InvariantDeviceProfile out = new InvariantDeviceProfile();
237 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
238 p = new InvariantDeviceProfile(points.get(i));
239 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
240 weights += w;
241 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700242 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700243 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700244 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700245
246 private void add(InvariantDeviceProfile p) {
247 iconSize += p.iconSize;
248 iconTextSize += p.iconTextSize;
249 hotseatIconSize += p.hotseatIconSize;
250 }
251
252 private InvariantDeviceProfile multiply(float w) {
253 iconSize *= w;
254 iconTextSize *= w;
255 hotseatIconSize *= w;
256 return this;
257 }
258
259 private float weight(float x0, float y0, float x1, float y1, float pow) {
260 float d = dist(x0, y0, x1, y1);
261 if (Float.compare(d, 0f) == 0) {
262 return Float.POSITIVE_INFINITY;
263 }
264 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
265 }
266}