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