blob: fcd6d60f66c2614b91ff004f2117327f00e341f4 [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
19import android.content.Context;
20import android.graphics.Point;
21import android.graphics.PointF;
22import android.util.DisplayMetrics;
23import android.util.TypedValue;
24import android.view.Display;
25import android.view.WindowManager;
26
27import com.android.launcher3.util.Thunk;
28
29import java.util.ArrayList;
30import java.util.Collections;
31import java.util.Comparator;
32
33public class InvariantDeviceProfile {
34 private static final String TAG = "InvariantDeviceProfile";
35
36 // This is a static that we use for the default icon size on a 4/5-inch phone
37 static float DEFAULT_ICON_SIZE_DP = 60;
38
39
40 static ArrayList<InvariantDeviceProfile> sDeviceProfiles =
41 new ArrayList<InvariantDeviceProfile>();
42 static {
43 sDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
44 255, 300, 2, 3, 2, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
45 sDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
46 255, 400, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
47 sDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
48 275, 420, 3, 4, 3, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
49 sDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
50 255, 450, 3, 4, 3, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
51 sDeviceProfiles.add(new InvariantDeviceProfile("Nexus S",
52 296, 491.33f, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
53 sDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
54 335, 567, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
55 sDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
56 359, 567, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
57 sDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
58 406, 694, 5, 5, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5));
59 // The tablet profile is odd in that the landscape orientation
60 // also includes the nav bar on the side
61 sDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7",
62 575, 904, 5, 6, 4, 5, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6));
63 // Larger tablet profiles always have system bars on the top & bottom
64 sDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
65 727, 1207, 5, 6, 4, 5, 76, 14.4f, 7, 64, R.xml.default_workspace_5x6));
66 sDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
67 1527, 2527, 7, 7, 6, 6, 100, 20, 7, 72, R.xml.default_workspace_4x4));
68 }
69
70 class DeviceProfileQuery {
71 InvariantDeviceProfile profile;
72 float widthDps;
73 float heightDps;
74 float value;
75 PointF dimens;
76
77 DeviceProfileQuery(InvariantDeviceProfile p, float v) {
78 widthDps = p.minWidthDps;
79 heightDps = p.minHeightDps;
80 value = v;
81 dimens = new PointF(widthDps, heightDps);
82 profile = p;
83 }
84 }
85
86 // Profile-defining invariant properties
87 String name;
88 float minWidthDps;
89 float minHeightDps;
90 public int numRows;
91 public int numColumns;
92 public int numFolderRows;
93 public int numFolderColumns;
94 float iconSize;
95 float iconTextSize;
96 float numHotseatIcons;
97 float hotseatIconSize;
98 int defaultLayoutId;
99
100 // Derived invariant properties
101 int hotseatAllAppsRank;
102
103 InvariantDeviceProfile() {
104 }
105
106 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc,
107 float is, float its, float hs, float his, int dlId) {
108 // Ensure that we have an odd number of hotseat items (since we need to place all apps)
109 if (hs % 2 == 0) {
110 throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
111 }
112
113 name = n;
114 minWidthDps = w;
115 minHeightDps = h;
116 numRows = r;
117 numColumns = c;
118 numFolderRows = fr;
119 numFolderColumns = fc;
120 iconSize = is;
121 iconTextSize = its;
122 numHotseatIcons = hs;
123 hotseatIconSize = his;
124 defaultLayoutId = dlId;
125 }
126
127 InvariantDeviceProfile(Context context) {
128 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
129 Display display = wm.getDefaultDisplay();
130 DisplayMetrics dm = new DisplayMetrics();
131 display.getMetrics(dm);
132
133 Point smallestSize = new Point();
134 Point largestSize = new Point();
135 display.getCurrentSizeRange(smallestSize, largestSize);
136
137 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
138 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
139
140 ArrayList<DeviceProfileQuery> points =
141 new ArrayList<DeviceProfileQuery>();
142
143 // Find the closes profile given the width/height
144 for (InvariantDeviceProfile p : sDeviceProfiles) {
145 points.add(new DeviceProfileQuery(p, 0f));
146 }
147
148 InvariantDeviceProfile closestProfile =
149 findClosestDeviceProfile(minWidthDps, minHeightDps, points);
150
151 // The following properties are inherited directly from the nearest archetypal profile
152 numRows = closestProfile.numRows;
153 numColumns = closestProfile.numColumns;
154 numHotseatIcons = closestProfile.numHotseatIcons;
155 hotseatAllAppsRank = (int) (numHotseatIcons / 2);
156 defaultLayoutId = closestProfile.defaultLayoutId;
157 numFolderRows = closestProfile.numFolderRows;
158 numFolderColumns = closestProfile.numFolderColumns;
159
160
161 // The following properties are interpolated based on proximity to nearby archetypal
162 // profiles
163 points.clear();
164 for (InvariantDeviceProfile p : sDeviceProfiles) {
165 points.add(new DeviceProfileQuery(p, p.iconSize));
166 }
167 iconSize = invDistWeightedInterpolate(minWidthDps, minHeightDps, points);
168 points.clear();
169 for (InvariantDeviceProfile p : sDeviceProfiles) {
170 points.add(new DeviceProfileQuery(p, p.iconTextSize));
171 }
172 iconTextSize = invDistWeightedInterpolate(minWidthDps, minHeightDps, points);
173 points.clear();
174 for (InvariantDeviceProfile p : sDeviceProfiles) {
175 points.add(new DeviceProfileQuery(p, p.hotseatIconSize));
176 }
177 hotseatIconSize = invDistWeightedInterpolate(minWidthDps, minHeightDps, points);
178
179 // If the partner customization apk contains any grid overrides, apply them
180 // Supported overrides: numRows, numColumns, iconSize
181 applyPartnerDeviceProfileOverrides(context, dm);
182 }
183
184 /**
185 * Apply any Partner customization grid overrides.
186 *
187 * Currently we support: all apps row / column count.
188 */
189 private void applyPartnerDeviceProfileOverrides(Context ctx, DisplayMetrics dm) {
190 Partner p = Partner.get(ctx.getPackageManager());
191 if (p != null) {
192 p.applyInvariantDeviceProfileOverrides(this, dm);
193 }
194 }
195
196 @Thunk float dist(PointF p0, PointF p1) {
197 return (float) Math.sqrt((p1.x - p0.x)*(p1.x-p0.x) +
198 (p1.y-p0.y)*(p1.y-p0.y));
199 }
200
201 private float weight(PointF a, PointF b,
202 float pow) {
203 float d = dist(a, b);
204 if (d == 0f) {
205 return Float.POSITIVE_INFINITY;
206 }
207 return (float) (1f / Math.pow(d, pow));
208 }
209
210 /** Returns the closest device profile given the width and height and a list of profiles */
211 private InvariantDeviceProfile findClosestDeviceProfile(float width, float height,
212 ArrayList<DeviceProfileQuery> points) {
213 return findClosestDeviceProfiles(width, height, points).get(0).profile;
214 }
215
216 /** Returns the closest device profiles ordered by closeness to the specified width and height */
217 private ArrayList<DeviceProfileQuery> findClosestDeviceProfiles(float width, float height,
218 ArrayList<DeviceProfileQuery> points) {
219 final PointF xy = new PointF(width, height);
220
221 // Sort the profiles by their closeness to the dimensions
222 ArrayList<DeviceProfileQuery> pointsByNearness = points;
223 Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
224 public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
225 return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
226 }
227 });
228
229 return pointsByNearness;
230 }
231
232 private float invDistWeightedInterpolate(float width, float height,
233 ArrayList<DeviceProfileQuery> points) {
234 float sum = 0;
235 float weights = 0;
236 float pow = 5;
237 float kNearestNeighbors = 3;
238 final PointF xy = new PointF(width, height);
239
240 ArrayList<DeviceProfileQuery> pointsByNearness = findClosestDeviceProfiles(width, height,
241 points);
242
243 for (int i = 0; i < pointsByNearness.size(); ++i) {
244 DeviceProfileQuery p = pointsByNearness.get(i);
245 if (i < kNearestNeighbors) {
246 float w = weight(xy, p.dimens, pow);
247 if (w == Float.POSITIVE_INFINITY) {
248 return p.value;
249 }
250 weights += w;
251 }
252 }
253
254 for (int i = 0; i < pointsByNearness.size(); ++i) {
255 DeviceProfileQuery p = pointsByNearness.get(i);
256 if (i < kNearestNeighbors) {
257 float w = weight(xy, p.dimens, pow);
258 sum += w * p.value / weights;
259 }
260 }
261
262 return sum;
263 }
264}