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