blob: 246fa74b9c9bab7bc4c74c12acea7476f7c1e4db [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;
Sunny Goyal27835952017-01-13 12:15:53 -080021import android.content.res.Configuration;
Sunny Goyal819e1932016-07-07 16:43:58 -070022import android.content.res.TypedArray;
23import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070024import android.graphics.Point;
Adam Cohen2e6da152015-05-06 11:42:25 -070025import android.util.DisplayMetrics;
Sunny Goyal819e1932016-07-07 16:43:58 -070026import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070027import android.view.Display;
28import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070029
Sunny Goyalbb011da2016-06-15 15:42:29 -070030import com.android.launcher3.config.FeatureFlags;
Adam Cohen2e6da152015-05-06 11:42:25 -070031import com.android.launcher3.util.Thunk;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070032
Sunny Goyal819e1932016-07-07 16:43:58 -070033import org.xmlpull.v1.XmlPullParser;
34import org.xmlpull.v1.XmlPullParserException;
35
36import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070037import java.util.ArrayList;
38import java.util.Collections;
39import java.util.Comparator;
40
41public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070042
43 // 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 -070044 private static float DEFAULT_ICON_SIZE_DP = 60;
Adam Cohen2e6da152015-05-06 11:42:25 -070045
Sunny Goyal53d7ee42015-05-22 12:25:45 -070046 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
47
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070048 // Constants that affects the interpolation curve between statically defined device profile
49 // buckets.
50 private static float KNEARESTNEIGHBOR = 3;
51 private static float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070052
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070053 // used to offset float not being able to express extremely small weights in extreme cases.
54 private static float WEIGHT_EFFICIENT = 100000f;
Adam Cohen2e6da152015-05-06 11:42:25 -070055
56 // Profile-defining invariant properties
57 String name;
58 float minWidthDps;
59 float minHeightDps;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070060
61 /**
62 * Number of icons per row and column in the workspace.
63 */
Adam Cohen2e6da152015-05-06 11:42:25 -070064 public int numRows;
65 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070066
67 /**
68 * Number of icons per row and column in the folder.
69 */
Adam Cohen2e6da152015-05-06 11:42:25 -070070 public int numFolderRows;
71 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070072 public float iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070073 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070074 public int iconBitmapSize;
75 public int fillResIconDpi;
76 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070077
78 /**
79 * Number of icons inside the hotseat area.
80 */
Sunny Goyalf862a262015-12-14 14:27:38 -080081 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -070082
Adam Cohen2e6da152015-05-06 11:42:25 -070083 int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -070084 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -070085
cuijiaxingabda8d72017-03-20 09:51:36 -070086 public DeviceProfile landscapeProfile;
87 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -070088
Sunny Goyal6f866092016-03-17 17:04:15 -070089 public Point defaultWallpaperSize;
90
Sunny Goyalf076eae2016-01-11 12:25:10 -080091 public InvariantDeviceProfile() {
Adam Cohen2e6da152015-05-06 11:42:25 -070092 }
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,
Sunny Goyalb1d222e2018-01-30 20:52:27 -080096 p.numFolderRows, p.numFolderColumns,
Jon Mirandab28c4fc2017-06-20 10:58:36 -070097 p.iconSize, p.landscapeIconSize, p.iconTextSize, p.numHotseatIcons,
Adam Cohen27824492017-09-22 17:10:55 -070098 p.defaultLayoutId, p.demoModeLayoutId);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070099 }
100
Sunny Goyalb1d222e2018-01-30 20:52:27 -0800101 InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc,
Adam Cohen27824492017-09-22 17:10:55 -0700102 float is, float lis, float its, int hs, int dlId, int dmlId) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700103 name = n;
104 minWidthDps = w;
105 minHeightDps = h;
106 numRows = r;
107 numColumns = c;
108 numFolderRows = fr;
109 numFolderColumns = fc;
110 iconSize = is;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700111 landscapeIconSize = lis;
Adam Cohen2e6da152015-05-06 11:42:25 -0700112 iconTextSize = its;
113 numHotseatIcons = hs;
Adam Cohen2e6da152015-05-06 11:42:25 -0700114 defaultLayoutId = dlId;
Adam Cohen27824492017-09-22 17:10:55 -0700115 demoModeLayoutId = dmlId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 }
117
Sunny Goyalbbf01842015-10-08 07:41:15 -0700118 @TargetApi(23)
Adam Cohen2e6da152015-05-06 11:42:25 -0700119 InvariantDeviceProfile(Context context) {
120 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
121 Display display = wm.getDefaultDisplay();
122 DisplayMetrics dm = new DisplayMetrics();
123 display.getMetrics(dm);
124
125 Point smallestSize = new Point();
126 Point largestSize = new Point();
127 display.getCurrentSizeRange(smallestSize, largestSize);
128
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700129 // This guarantees that width < height
Adam Cohen2e6da152015-05-06 11:42:25 -0700130 minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
131 minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
132
Sunny Goyal819e1932016-07-07 16:43:58 -0700133 ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
134 minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700135 InvariantDeviceProfile interpolatedDeviceProfileOut =
136 invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles);
Adam Cohen2e6da152015-05-06 11:42:25 -0700137
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700138 InvariantDeviceProfile closestProfile = closestProfiles.get(0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700139 numRows = closestProfile.numRows;
140 numColumns = closestProfile.numColumns;
141 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700142 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700143 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700144 numFolderRows = closestProfile.numFolderRows;
145 numFolderColumns = closestProfile.numFolderColumns;
146
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700147 iconSize = interpolatedDeviceProfileOut.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700148 landscapeIconSize = interpolatedDeviceProfileOut.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700149 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700150 iconTextSize = interpolatedDeviceProfileOut.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700151 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700152
153 // If the partner customization apk contains any grid overrides, apply them
154 // Supported overrides: numRows, numColumns, iconSize
155 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700156
157 Point realSize = new Point();
158 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700159 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700160 // same in any orientation.
161 int smallSide = Math.min(realSize.x, realSize.y);
162 int largeSide = Math.max(realSize.x, realSize.y);
163
164 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
165 largeSide, smallSide, true /* isLandscape */);
166 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
167 smallSide, largeSide, false /* isLandscape */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700168
169 // We need to ensure that there is enough extra space in the wallpaper
170 // for the intended parallax effects
171 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
172 defaultWallpaperSize = new Point(
173 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
174 largeSide);
175 } else {
176 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
177 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700178 }
179
Sunny Goyal819e1932016-07-07 16:43:58 -0700180 ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
181 ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
182 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
183 final int depth = parser.getDepth();
184 int type;
185
186 while (((type = parser.next()) != XmlPullParser.END_TAG ||
187 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
188 if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
189 TypedArray a = context.obtainStyledAttributes(
190 Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
191 int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
192 int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
193 float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
194 profiles.add(new InvariantDeviceProfile(
195 a.getString(R.styleable.InvariantDeviceProfile_name),
196 a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
197 a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
198 numRows,
199 numColumns,
200 a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
201 a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
Sunny Goyal819e1932016-07-07 16:43:58 -0700202 iconSize,
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700203 a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize),
Sunny Goyal819e1932016-07-07 16:43:58 -0700204 a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
205 a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
Adam Cohen27824492017-09-22 17:10:55 -0700206 a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0),
207 a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0)));
Sunny Goyal819e1932016-07-07 16:43:58 -0700208 a.recycle();
209 }
210 }
211 } catch (IOException|XmlPullParserException e) {
212 throw new RuntimeException(e);
213 }
214 return profiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700215 }
216
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700217 private int getLauncherIconDensity(int requiredSize) {
218 // Densities typically defined by an app.
219 int[] densityBuckets = new int[] {
220 DisplayMetrics.DENSITY_LOW,
221 DisplayMetrics.DENSITY_MEDIUM,
222 DisplayMetrics.DENSITY_TV,
223 DisplayMetrics.DENSITY_HIGH,
224 DisplayMetrics.DENSITY_XHIGH,
225 DisplayMetrics.DENSITY_XXHIGH,
226 DisplayMetrics.DENSITY_XXXHIGH
227 };
228
229 int density = DisplayMetrics.DENSITY_XXXHIGH;
230 for (int i = densityBuckets.length - 1; i >= 0; i--) {
231 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
232 / DisplayMetrics.DENSITY_DEFAULT;
233 if (expectedSize >= requiredSize) {
234 density = densityBuckets[i];
235 }
236 }
237
238 return density;
239 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700240
Adam Cohen2e6da152015-05-06 11:42:25 -0700241 /**
242 * Apply any Partner customization grid overrides.
243 *
244 * Currently we support: all apps row / column count.
245 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700246 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
247 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700248 if (p != null) {
249 p.applyInvariantDeviceProfileOverrides(this, dm);
250 }
251 }
252
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700253 @Thunk float dist(float x0, float y0, float x1, float y1) {
254 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700255 }
256
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700257 /**
258 * Returns the closest device profiles ordered by closeness to the specified width and height
259 */
260 // Package private visibility for testing.
261 ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles(
262 final float width, final float height, ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700263
264 // Sort the profiles by their closeness to the dimensions
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700265 ArrayList<InvariantDeviceProfile> pointsByNearness = points;
266 Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() {
267 public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) {
Winson46a06da2015-09-29 16:58:02 -0700268 return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps),
269 dist(width, height, b.minWidthDps, b.minHeightDps));
Adam Cohen2e6da152015-05-06 11:42:25 -0700270 }
271 });
272
273 return pointsByNearness;
274 }
275
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700276 // Package private visibility for testing.
277 InvariantDeviceProfile invDistWeightedInterpolate(float width, float height,
278 ArrayList<InvariantDeviceProfile> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700279 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700280
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700281 InvariantDeviceProfile p = points.get(0);
282 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
283 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700284 }
285
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700286 InvariantDeviceProfile out = new InvariantDeviceProfile();
287 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
288 p = new InvariantDeviceProfile(points.get(i));
289 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
290 weights += w;
291 out.add(p.multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700292 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700293 return out.multiply(1.0f/weights);
Adam Cohen2e6da152015-05-06 11:42:25 -0700294 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700295
296 private void add(InvariantDeviceProfile p) {
297 iconSize += p.iconSize;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700298 landscapeIconSize += p.landscapeIconSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700299 iconTextSize += p.iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700300 }
301
302 private InvariantDeviceProfile multiply(float w) {
303 iconSize *= w;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700304 landscapeIconSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700305 iconTextSize *= w;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700306 return this;
307 }
308
Sunny Goyalbb011da2016-06-15 15:42:29 -0700309 public int getAllAppsButtonRank() {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800310 if (FeatureFlags.IS_DOGFOOD_BUILD && FeatureFlags.NO_ALL_APPS_ICON) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700311 throw new IllegalAccessError("Accessing all apps rank when all-apps is disabled");
312 }
313 return numHotseatIcons / 2;
314 }
315
316 public boolean isAllAppsButtonRank(int rank) {
317 return rank == getAllAppsButtonRank();
318 }
319
Sunny Goyal27835952017-01-13 12:15:53 -0800320 public DeviceProfile getDeviceProfile(Context context) {
321 return context.getResources().getConfiguration().orientation
322 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
323 }
324
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700325 private float weight(float x0, float y0, float x1, float y1, float pow) {
326 float d = dist(x0, y0, x1, y1);
327 if (Float.compare(d, 0f) == 0) {
328 return Float.POSITIVE_INFINITY;
329 }
330 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
331 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700332
333 /**
334 * As a ratio of screen height, the total distance we want the parallax effect to span
335 * horizontally
336 */
337 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
338 float aspectRatio = width / (float) height;
339
340 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
341 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
342 // We will use these two data points to extrapolate how much the wallpaper parallax effect
343 // to span (ie travel) at any aspect ratio:
344
345 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
346 final float ASPECT_RATIO_PORTRAIT = 10/16f;
347 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
348 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
349
350 // To find out the desired width at different aspect ratios, we use the following two
351 // formulas, where the coefficient on x is the aspect ratio (width/height):
352 // (16/10)x + y = 1.5
353 // (10/16)x + y = 1.2
354 // We solve for x and y and end up with a final formula:
355 final float x =
356 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
357 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
358 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
359 return x * aspectRatio + y;
360 }
361
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700362}