Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 19 | import android.annotation.TargetApi; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.graphics.Point; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 22 | import android.util.DisplayMetrics; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 23 | import android.view.Display; |
| 24 | import android.view.WindowManager; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 25 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 26 | import com.android.launcher3.util.Thunk; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 27 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.Collections; |
| 30 | import java.util.Comparator; |
| 31 | |
| 32 | public class InvariantDeviceProfile { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 33 | |
| 34 | // This is a static that we use for the default icon size on a 4/5-inch phone |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 35 | private static float DEFAULT_ICON_SIZE_DP = 60; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 36 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 37 | private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48; |
| 38 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 39 | // Constants that affects the interpolation curve between statically defined device profile |
| 40 | // buckets. |
| 41 | private static float KNEARESTNEIGHBOR = 3; |
| 42 | private static float WEIGHT_POWER = 5; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 43 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 44 | // used to offset float not being able to express extremely small weights in extreme cases. |
| 45 | private static float WEIGHT_EFFICIENT = 100000f; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 46 | |
| 47 | // Profile-defining invariant properties |
| 48 | String name; |
| 49 | float minWidthDps; |
| 50 | float minHeightDps; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Number of icons per row and column in the workspace. |
| 54 | */ |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 55 | public int numRows; |
| 56 | public int numColumns; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 59 | * The minimum number of predicted apps in all apps. |
| 60 | */ |
| 61 | int minAllAppsPredictionColumns; |
| 62 | |
| 63 | /** |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 64 | * Number of icons per row and column in the folder. |
| 65 | */ |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 66 | public int numFolderRows; |
| 67 | public int numFolderColumns; |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 68 | public float iconSize; |
| 69 | public int iconBitmapSize; |
| 70 | public int fillResIconDpi; |
| 71 | public float iconTextSize; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Number of icons inside the hotseat area. |
| 75 | */ |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 76 | public int numHotseatIcons; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 77 | float hotseatIconSize; |
| 78 | int defaultLayoutId; |
| 79 | |
| 80 | // Derived invariant properties |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 81 | public int hotseatAllAppsRank; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 82 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 83 | DeviceProfile landscapeProfile; |
| 84 | DeviceProfile portraitProfile; |
| 85 | |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame^] | 86 | public Point defaultWallpaperSize; |
| 87 | |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 88 | public InvariantDeviceProfile() { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 91 | public InvariantDeviceProfile(InvariantDeviceProfile p) { |
| 92 | this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns, |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 93 | p.numFolderRows, p.numFolderColumns, p.minAllAppsPredictionColumns, |
| 94 | p.iconSize, p.iconTextSize, p.numHotseatIcons, p.hotseatIconSize, |
| 95 | p.defaultLayoutId); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 98 | InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, int maapc, |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 99 | float is, float its, int hs, float his, int dlId) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 100 | // Ensure that we have an odd number of hotseat items (since we need to place all apps) |
| 101 | if (hs % 2 == 0) { |
| 102 | throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces"); |
| 103 | } |
| 104 | |
| 105 | name = n; |
| 106 | minWidthDps = w; |
| 107 | minHeightDps = h; |
| 108 | numRows = r; |
| 109 | numColumns = c; |
| 110 | numFolderRows = fr; |
| 111 | numFolderColumns = fc; |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 112 | minAllAppsPredictionColumns = maapc; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 113 | iconSize = is; |
| 114 | iconTextSize = its; |
| 115 | numHotseatIcons = hs; |
| 116 | hotseatIconSize = his; |
| 117 | defaultLayoutId = dlId; |
| 118 | } |
| 119 | |
Sunny Goyal | bbf0184 | 2015-10-08 07:41:15 -0700 | [diff] [blame] | 120 | @TargetApi(23) |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 121 | InvariantDeviceProfile(Context context) { |
| 122 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 123 | Display display = wm.getDefaultDisplay(); |
| 124 | DisplayMetrics dm = new DisplayMetrics(); |
| 125 | display.getMetrics(dm); |
| 126 | |
| 127 | Point smallestSize = new Point(); |
| 128 | Point largestSize = new Point(); |
| 129 | display.getCurrentSizeRange(smallestSize, largestSize); |
| 130 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 131 | // This guarantees that width < height |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 132 | minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm); |
| 133 | minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm); |
| 134 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 135 | ArrayList<InvariantDeviceProfile> closestProfiles = |
| 136 | findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles()); |
| 137 | InvariantDeviceProfile interpolatedDeviceProfileOut = |
| 138 | invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 139 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 140 | InvariantDeviceProfile closestProfile = closestProfiles.get(0); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 141 | numRows = closestProfile.numRows; |
| 142 | numColumns = closestProfile.numColumns; |
| 143 | numHotseatIcons = closestProfile.numHotseatIcons; |
| 144 | hotseatAllAppsRank = (int) (numHotseatIcons / 2); |
| 145 | defaultLayoutId = closestProfile.defaultLayoutId; |
| 146 | numFolderRows = closestProfile.numFolderRows; |
| 147 | numFolderColumns = closestProfile.numFolderColumns; |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 148 | minAllAppsPredictionColumns = closestProfile.minAllAppsPredictionColumns; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 149 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 150 | iconSize = interpolatedDeviceProfileOut.iconSize; |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 151 | iconBitmapSize = Utilities.pxFromDp(iconSize, dm); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 152 | iconTextSize = interpolatedDeviceProfileOut.iconTextSize; |
| 153 | hotseatIconSize = interpolatedDeviceProfileOut.hotseatIconSize; |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 154 | fillResIconDpi = getLauncherIconDensity(iconBitmapSize); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 155 | |
| 156 | // If the partner customization apk contains any grid overrides, apply them |
| 157 | // Supported overrides: numRows, numColumns, iconSize |
| 158 | applyPartnerDeviceProfileOverrides(context, dm); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 159 | |
| 160 | Point realSize = new Point(); |
| 161 | display.getRealSize(realSize); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 162 | // The real size never changes. smallSide and largeSide will remain the |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 163 | // same in any orientation. |
| 164 | int smallSide = Math.min(realSize.x, realSize.y); |
| 165 | int largeSide = Math.max(realSize.x, realSize.y); |
| 166 | |
| 167 | landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize, |
| 168 | largeSide, smallSide, true /* isLandscape */); |
| 169 | portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize, |
| 170 | smallSide, largeSide, false /* isLandscape */); |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame^] | 171 | |
| 172 | // We need to ensure that there is enough extra space in the wallpaper |
| 173 | // for the intended parallax effects |
| 174 | if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) { |
| 175 | defaultWallpaperSize = new Point( |
| 176 | (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)), |
| 177 | largeSide); |
| 178 | } else { |
| 179 | defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide); |
| 180 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 183 | ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles() { |
| 184 | ArrayList<InvariantDeviceProfile> predefinedDeviceProfiles = new ArrayList<>(); |
| 185 | // width, height, #rows, #columns, #folder rows, #folder columns, |
| 186 | // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId. |
| 187 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby", |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 188 | 255, 300, 2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_3x3)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 189 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby", |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 190 | 255, 400, 3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_3x3)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 191 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby", |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 192 | 275, 420, 3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 193 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby", |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 194 | 255, 450, 3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 195 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus S", |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 196 | 296, 491.33f, 4, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 197 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4", |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 198 | 359, 567, 4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4)); |
Tony Wickham | 58ecbe4 | 2015-09-29 15:15:53 -0700 | [diff] [blame] | 199 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5", |
| 200 | 335, 567, 4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 201 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone", |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 202 | 406, 694, 5, 5, 4, 4, 4, 64, 14.4f, 5, 56, R.xml.default_workspace_5x5)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 203 | // The tablet profile is odd in that the landscape orientation |
| 204 | // also includes the nav bar on the side |
| 205 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7", |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 206 | 575, 904, 5, 6, 4, 5, 4, 72, 14.4f, 7, 60, R.xml.default_workspace_5x6)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 207 | // Larger tablet profiles always have system bars on the top & bottom |
| 208 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10", |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 209 | 727, 1207, 5, 6, 4, 5, 4, 76, 14.4f, 7, 76, R.xml.default_workspace_5x6)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 210 | predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet", |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 211 | 1527, 2527, 7, 7, 6, 6, 4, 100, 20, 7, 72, R.xml.default_workspace_5x6)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 212 | return predefinedDeviceProfiles; |
| 213 | } |
| 214 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 215 | private int getLauncherIconDensity(int requiredSize) { |
| 216 | // Densities typically defined by an app. |
| 217 | int[] densityBuckets = new int[] { |
| 218 | DisplayMetrics.DENSITY_LOW, |
| 219 | DisplayMetrics.DENSITY_MEDIUM, |
| 220 | DisplayMetrics.DENSITY_TV, |
| 221 | DisplayMetrics.DENSITY_HIGH, |
| 222 | DisplayMetrics.DENSITY_XHIGH, |
| 223 | DisplayMetrics.DENSITY_XXHIGH, |
| 224 | DisplayMetrics.DENSITY_XXXHIGH |
| 225 | }; |
| 226 | |
| 227 | int density = DisplayMetrics.DENSITY_XXXHIGH; |
| 228 | for (int i = densityBuckets.length - 1; i >= 0; i--) { |
| 229 | float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i] |
| 230 | / DisplayMetrics.DENSITY_DEFAULT; |
| 231 | if (expectedSize >= requiredSize) { |
| 232 | density = densityBuckets[i]; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return density; |
| 237 | } |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 238 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Apply any Partner customization grid overrides. |
| 241 | * |
| 242 | * Currently we support: all apps row / column count. |
| 243 | */ |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 244 | private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) { |
| 245 | Partner p = Partner.get(context.getPackageManager()); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 246 | if (p != null) { |
| 247 | p.applyInvariantDeviceProfileOverrides(this, dm); |
| 248 | } |
| 249 | } |
| 250 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 251 | @Thunk float dist(float x0, float y0, float x1, float y1) { |
| 252 | return (float) Math.hypot(x1 - x0, y1 - y0); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Returns the closest device profiles ordered by closeness to the specified width and height |
| 257 | */ |
| 258 | // Package private visibility for testing. |
| 259 | ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles( |
| 260 | final float width, final float height, ArrayList<InvariantDeviceProfile> points) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 261 | |
| 262 | // Sort the profiles by their closeness to the dimensions |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 263 | ArrayList<InvariantDeviceProfile> pointsByNearness = points; |
| 264 | Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() { |
| 265 | public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) { |
Winson | 46a06da | 2015-09-29 16:58:02 -0700 | [diff] [blame] | 266 | return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps), |
| 267 | dist(width, height, b.minWidthDps, b.minHeightDps)); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 268 | } |
| 269 | }); |
| 270 | |
| 271 | return pointsByNearness; |
| 272 | } |
| 273 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 274 | // Package private visibility for testing. |
| 275 | InvariantDeviceProfile invDistWeightedInterpolate(float width, float height, |
| 276 | ArrayList<InvariantDeviceProfile> points) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 277 | float weights = 0; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 278 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 279 | InvariantDeviceProfile p = points.get(0); |
| 280 | if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) { |
| 281 | return p; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 284 | InvariantDeviceProfile out = new InvariantDeviceProfile(); |
| 285 | for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) { |
| 286 | p = new InvariantDeviceProfile(points.get(i)); |
| 287 | float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER); |
| 288 | weights += w; |
| 289 | out.add(p.multiply(w)); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 290 | } |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 291 | return out.multiply(1.0f/weights); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 292 | } |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 293 | |
| 294 | private void add(InvariantDeviceProfile p) { |
| 295 | iconSize += p.iconSize; |
| 296 | iconTextSize += p.iconTextSize; |
| 297 | hotseatIconSize += p.hotseatIconSize; |
| 298 | } |
| 299 | |
| 300 | private InvariantDeviceProfile multiply(float w) { |
| 301 | iconSize *= w; |
| 302 | iconTextSize *= w; |
| 303 | hotseatIconSize *= w; |
| 304 | return this; |
| 305 | } |
| 306 | |
| 307 | private float weight(float x0, float y0, float x1, float y1, float pow) { |
| 308 | float d = dist(x0, y0, x1, y1); |
| 309 | if (Float.compare(d, 0f) == 0) { |
| 310 | return Float.POSITIVE_INFINITY; |
| 311 | } |
| 312 | return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow)); |
| 313 | } |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame^] | 314 | |
| 315 | /** |
| 316 | * As a ratio of screen height, the total distance we want the parallax effect to span |
| 317 | * horizontally |
| 318 | */ |
| 319 | private static float wallpaperTravelToScreenWidthRatio(int width, int height) { |
| 320 | float aspectRatio = width / (float) height; |
| 321 | |
| 322 | // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width |
| 323 | // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width |
| 324 | // We will use these two data points to extrapolate how much the wallpaper parallax effect |
| 325 | // to span (ie travel) at any aspect ratio: |
| 326 | |
| 327 | final float ASPECT_RATIO_LANDSCAPE = 16/10f; |
| 328 | final float ASPECT_RATIO_PORTRAIT = 10/16f; |
| 329 | final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f; |
| 330 | final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f; |
| 331 | |
| 332 | // To find out the desired width at different aspect ratios, we use the following two |
| 333 | // formulas, where the coefficient on x is the aspect ratio (width/height): |
| 334 | // (16/10)x + y = 1.5 |
| 335 | // (10/16)x + y = 1.2 |
| 336 | // We solve for x and y and end up with a final formula: |
| 337 | final float x = |
| 338 | (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) / |
| 339 | (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT); |
| 340 | final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT; |
| 341 | return x * aspectRatio + y; |
| 342 | } |
| 343 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 344 | } |