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