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 | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 19 | import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME; |
| 20 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 21 | import android.annotation.TargetApi; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 23 | import android.content.res.Configuration; |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
| 25 | import android.content.res.XmlResourceParser; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 26 | import android.graphics.Point; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 27 | import android.util.DisplayMetrics; |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 28 | import android.util.Log; |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 29 | import android.util.Xml; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 30 | import android.view.Display; |
| 31 | import android.view.WindowManager; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 32 | |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.ConfigMonitor; |
| 34 | import com.android.launcher3.util.MainThreadInitializedObject; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 35 | import com.android.launcher3.util.Thunk; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 36 | |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 37 | import org.xmlpull.v1.XmlPullParser; |
| 38 | import org.xmlpull.v1.XmlPullParserException; |
| 39 | |
| 40 | import java.io.IOException; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.Collections; |
| 43 | import java.util.Comparator; |
| 44 | |
Sunny Goyal | d230307 | 2018-08-14 15:21:45 -0700 | [diff] [blame] | 45 | import androidx.annotation.VisibleForTesting; |
| 46 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 47 | public class InvariantDeviceProfile { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 48 | |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 49 | // We do not need any synchronization for this variable as its only written on UI thread. |
| 50 | public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE = |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 51 | new MainThreadInitializedObject<>(InvariantDeviceProfile::new); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 52 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 53 | private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48; |
| 54 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 55 | public static final int CHANGE_FLAG_GRID = 1 << 0; |
| 56 | public static final int CHANGE_FLAG_ICON_SIZE = 1 << 1; |
| 57 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 58 | // Constants that affects the interpolation curve between statically defined device profile |
| 59 | // buckets. |
| 60 | private static float KNEARESTNEIGHBOR = 3; |
| 61 | private static float WEIGHT_POWER = 5; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 62 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 63 | // used to offset float not being able to express extremely small weights in extreme cases. |
| 64 | private static float WEIGHT_EFFICIENT = 100000f; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 65 | |
| 66 | // Profile-defining invariant properties |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 67 | private String name; |
| 68 | private float minWidthDps; |
| 69 | private float minHeightDps; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Number of icons per row and column in the workspace. |
| 73 | */ |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 74 | public int numRows; |
| 75 | public int numColumns; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Number of icons per row and column in the folder. |
| 79 | */ |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 80 | public int numFolderRows; |
| 81 | public int numFolderColumns; |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 82 | public float iconSize; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 83 | public float landscapeIconSize; |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 84 | public int iconBitmapSize; |
| 85 | public int fillResIconDpi; |
| 86 | public float iconTextSize; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Number of icons inside the hotseat area. |
| 90 | */ |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 91 | public int numHotseatIcons; |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 92 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 93 | int defaultLayoutId; |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 94 | int demoModeLayoutId; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 95 | |
cuijiaxing | abda8d7 | 2017-03-20 09:51:36 -0700 | [diff] [blame] | 96 | public DeviceProfile landscapeProfile; |
| 97 | public DeviceProfile portraitProfile; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 98 | |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 99 | public Point defaultWallpaperSize; |
| 100 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 101 | private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>(); |
| 102 | private ConfigMonitor mConfigMonitor; |
| 103 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 104 | @VisibleForTesting |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 105 | public InvariantDeviceProfile() {} |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 106 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 107 | private InvariantDeviceProfile(InvariantDeviceProfile p) { |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 108 | this(p.name, p.minWidthDps, p.minHeightDps, p.numRows, p.numColumns, |
Sunny Goyal | b1d222e | 2018-01-30 20:52:27 -0800 | [diff] [blame] | 109 | p.numFolderRows, p.numFolderColumns, |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 110 | p.iconSize, p.landscapeIconSize, p.iconTextSize, p.numHotseatIcons, |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 111 | p.defaultLayoutId, p.demoModeLayoutId); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 114 | private InvariantDeviceProfile(String n, float w, float h, int r, int c, int fr, int fc, |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 115 | float is, float lis, float its, int hs, int dlId, int dmlId) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 116 | name = n; |
| 117 | minWidthDps = w; |
| 118 | minHeightDps = h; |
| 119 | numRows = r; |
| 120 | numColumns = c; |
| 121 | numFolderRows = fr; |
| 122 | numFolderColumns = fc; |
| 123 | iconSize = is; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 124 | landscapeIconSize = lis; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 125 | iconTextSize = its; |
| 126 | numHotseatIcons = hs; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 127 | defaultLayoutId = dlId; |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 128 | demoModeLayoutId = dmlId; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Sunny Goyal | bbf0184 | 2015-10-08 07:41:15 -0700 | [diff] [blame] | 131 | @TargetApi(23) |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 132 | private InvariantDeviceProfile(Context context) { |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 133 | initGrid(context); |
| 134 | mConfigMonitor = new ConfigMonitor(context, |
| 135 | APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess); |
| 136 | } |
| 137 | |
| 138 | private void initGrid(Context context) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 139 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 140 | Display display = wm.getDefaultDisplay(); |
| 141 | DisplayMetrics dm = new DisplayMetrics(); |
| 142 | display.getMetrics(dm); |
| 143 | |
| 144 | Point smallestSize = new Point(); |
| 145 | Point largestSize = new Point(); |
| 146 | display.getCurrentSizeRange(smallestSize, largestSize); |
| 147 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 148 | // This guarantees that width < height |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 149 | minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm); |
| 150 | minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm); |
| 151 | |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 152 | ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles( |
| 153 | minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context)); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 154 | InvariantDeviceProfile interpolatedDeviceProfileOut = |
| 155 | invDistWeightedInterpolate(minWidthDps, minHeightDps, closestProfiles); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 156 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 157 | InvariantDeviceProfile closestProfile = closestProfiles.get(0); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 158 | numRows = closestProfile.numRows; |
| 159 | numColumns = closestProfile.numColumns; |
| 160 | numHotseatIcons = closestProfile.numHotseatIcons; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 161 | defaultLayoutId = closestProfile.defaultLayoutId; |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 162 | demoModeLayoutId = closestProfile.demoModeLayoutId; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 163 | numFolderRows = closestProfile.numFolderRows; |
| 164 | numFolderColumns = closestProfile.numFolderColumns; |
| 165 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 166 | iconSize = interpolatedDeviceProfileOut.iconSize; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 167 | landscapeIconSize = interpolatedDeviceProfileOut.landscapeIconSize; |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 168 | iconBitmapSize = Utilities.pxFromDp(iconSize, dm); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 169 | iconTextSize = interpolatedDeviceProfileOut.iconTextSize; |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 170 | fillResIconDpi = getLauncherIconDensity(iconBitmapSize); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 171 | |
| 172 | // If the partner customization apk contains any grid overrides, apply them |
| 173 | // Supported overrides: numRows, numColumns, iconSize |
| 174 | applyPartnerDeviceProfileOverrides(context, dm); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 175 | |
| 176 | Point realSize = new Point(); |
| 177 | display.getRealSize(realSize); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 178 | // The real size never changes. smallSide and largeSide will remain the |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 179 | // same in any orientation. |
| 180 | int smallSide = Math.min(realSize.x, realSize.y); |
| 181 | int largeSide = Math.max(realSize.x, realSize.y); |
| 182 | |
| 183 | landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize, |
Sunny Goyal | d70e75a | 2018-02-22 10:07:32 -0800 | [diff] [blame] | 184 | largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 185 | portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize, |
Sunny Goyal | d70e75a | 2018-02-22 10:07:32 -0800 | [diff] [blame] | 186 | smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */); |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 187 | |
| 188 | // We need to ensure that there is enough extra space in the wallpaper |
| 189 | // for the intended parallax effects |
| 190 | if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) { |
| 191 | defaultWallpaperSize = new Point( |
| 192 | (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)), |
| 193 | largeSide); |
| 194 | } else { |
| 195 | defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide); |
| 196 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 199 | public void addOnChangeListener(OnIDPChangeListener listener) { |
| 200 | mChangeListeners.add(listener); |
| 201 | } |
| 202 | |
| 203 | private void killProcess(Context context) { |
| 204 | Log.e("ConfigMonitor", "restarting launcher"); |
| 205 | android.os.Process.killProcess(android.os.Process.myPid()); |
| 206 | } |
| 207 | |
| 208 | private void onConfigChanged(Context context) { |
| 209 | // Config changes, what shall we do? |
| 210 | InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this); |
| 211 | |
| 212 | // Re-init grid |
| 213 | initGrid(context); |
| 214 | |
| 215 | int changeFlags = 0; |
| 216 | if (numRows != oldProfile.numRows || |
| 217 | numColumns != oldProfile.numColumns || |
| 218 | numFolderColumns != oldProfile.numFolderColumns || |
| 219 | numFolderRows != oldProfile.numFolderRows || |
| 220 | numHotseatIcons != oldProfile.numHotseatIcons) { |
| 221 | changeFlags |= CHANGE_FLAG_GRID; |
| 222 | } |
| 223 | |
| 224 | if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize) { |
| 225 | changeFlags |= CHANGE_FLAG_ICON_SIZE; |
| 226 | } |
| 227 | |
| 228 | // Create a new config monitor |
| 229 | mConfigMonitor.unregister(); |
| 230 | mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged); |
| 231 | |
| 232 | for (OnIDPChangeListener listener : mChangeListeners) { |
| 233 | listener.onIdpChanged(changeFlags, this); |
| 234 | } |
| 235 | } |
| 236 | |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 237 | ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) { |
| 238 | ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>(); |
| 239 | try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) { |
| 240 | final int depth = parser.getDepth(); |
| 241 | int type; |
| 242 | |
| 243 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 244 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 245 | if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) { |
| 246 | TypedArray a = context.obtainStyledAttributes( |
| 247 | Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile); |
| 248 | int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0); |
| 249 | int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0); |
| 250 | float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0); |
| 251 | profiles.add(new InvariantDeviceProfile( |
| 252 | a.getString(R.styleable.InvariantDeviceProfile_name), |
| 253 | a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0), |
| 254 | a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0), |
| 255 | numRows, |
| 256 | numColumns, |
| 257 | a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows), |
| 258 | a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns), |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 259 | iconSize, |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 260 | a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize), |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 261 | a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0), |
| 262 | a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns), |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 263 | a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0), |
| 264 | a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0))); |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 265 | a.recycle(); |
| 266 | } |
| 267 | } |
| 268 | } catch (IOException|XmlPullParserException e) { |
| 269 | throw new RuntimeException(e); |
| 270 | } |
| 271 | return profiles; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 274 | private int getLauncherIconDensity(int requiredSize) { |
| 275 | // Densities typically defined by an app. |
| 276 | int[] densityBuckets = new int[] { |
| 277 | DisplayMetrics.DENSITY_LOW, |
| 278 | DisplayMetrics.DENSITY_MEDIUM, |
| 279 | DisplayMetrics.DENSITY_TV, |
| 280 | DisplayMetrics.DENSITY_HIGH, |
| 281 | DisplayMetrics.DENSITY_XHIGH, |
| 282 | DisplayMetrics.DENSITY_XXHIGH, |
| 283 | DisplayMetrics.DENSITY_XXXHIGH |
| 284 | }; |
| 285 | |
| 286 | int density = DisplayMetrics.DENSITY_XXXHIGH; |
| 287 | for (int i = densityBuckets.length - 1; i >= 0; i--) { |
| 288 | float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i] |
| 289 | / DisplayMetrics.DENSITY_DEFAULT; |
| 290 | if (expectedSize >= requiredSize) { |
| 291 | density = densityBuckets[i]; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return density; |
| 296 | } |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 297 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Apply any Partner customization grid overrides. |
| 300 | * |
| 301 | * Currently we support: all apps row / column count. |
| 302 | */ |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 303 | private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) { |
| 304 | Partner p = Partner.get(context.getPackageManager()); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 305 | if (p != null) { |
| 306 | p.applyInvariantDeviceProfileOverrides(this, dm); |
| 307 | } |
| 308 | } |
| 309 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 310 | @Thunk float dist(float x0, float y0, float x1, float y1) { |
| 311 | return (float) Math.hypot(x1 - x0, y1 - y0); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 314 | /** |
| 315 | * Returns the closest device profiles ordered by closeness to the specified width and height |
| 316 | */ |
| 317 | // Package private visibility for testing. |
| 318 | ArrayList<InvariantDeviceProfile> findClosestDeviceProfiles( |
| 319 | final float width, final float height, ArrayList<InvariantDeviceProfile> points) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 320 | |
| 321 | // Sort the profiles by their closeness to the dimensions |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 322 | ArrayList<InvariantDeviceProfile> pointsByNearness = points; |
| 323 | Collections.sort(pointsByNearness, new Comparator<InvariantDeviceProfile>() { |
| 324 | public int compare(InvariantDeviceProfile a, InvariantDeviceProfile b) { |
Winson | 46a06da | 2015-09-29 16:58:02 -0700 | [diff] [blame] | 325 | return Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps), |
| 326 | dist(width, height, b.minWidthDps, b.minHeightDps)); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 327 | } |
| 328 | }); |
| 329 | |
| 330 | return pointsByNearness; |
| 331 | } |
| 332 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 333 | // Package private visibility for testing. |
| 334 | InvariantDeviceProfile invDistWeightedInterpolate(float width, float height, |
| 335 | ArrayList<InvariantDeviceProfile> points) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 336 | float weights = 0; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 337 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 338 | InvariantDeviceProfile p = points.get(0); |
| 339 | if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) { |
| 340 | return p; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 343 | InvariantDeviceProfile out = new InvariantDeviceProfile(); |
| 344 | for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) { |
| 345 | p = new InvariantDeviceProfile(points.get(i)); |
| 346 | float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER); |
| 347 | weights += w; |
| 348 | out.add(p.multiply(w)); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 349 | } |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 350 | return out.multiply(1.0f/weights); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 351 | } |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 352 | |
| 353 | private void add(InvariantDeviceProfile p) { |
| 354 | iconSize += p.iconSize; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 355 | landscapeIconSize += p.landscapeIconSize; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 356 | iconTextSize += p.iconTextSize; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | private InvariantDeviceProfile multiply(float w) { |
| 360 | iconSize *= w; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 361 | landscapeIconSize *= w; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 362 | iconTextSize *= w; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 363 | return this; |
| 364 | } |
| 365 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 366 | public DeviceProfile getDeviceProfile(Context context) { |
| 367 | return context.getResources().getConfiguration().orientation |
| 368 | == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile; |
| 369 | } |
| 370 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 371 | private float weight(float x0, float y0, float x1, float y1, float pow) { |
| 372 | float d = dist(x0, y0, x1, y1); |
| 373 | if (Float.compare(d, 0f) == 0) { |
| 374 | return Float.POSITIVE_INFINITY; |
| 375 | } |
| 376 | return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow)); |
| 377 | } |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * As a ratio of screen height, the total distance we want the parallax effect to span |
| 381 | * horizontally |
| 382 | */ |
| 383 | private static float wallpaperTravelToScreenWidthRatio(int width, int height) { |
| 384 | float aspectRatio = width / (float) height; |
| 385 | |
| 386 | // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width |
| 387 | // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width |
| 388 | // We will use these two data points to extrapolate how much the wallpaper parallax effect |
| 389 | // to span (ie travel) at any aspect ratio: |
| 390 | |
| 391 | final float ASPECT_RATIO_LANDSCAPE = 16/10f; |
| 392 | final float ASPECT_RATIO_PORTRAIT = 10/16f; |
| 393 | final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f; |
| 394 | final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f; |
| 395 | |
| 396 | // To find out the desired width at different aspect ratios, we use the following two |
| 397 | // formulas, where the coefficient on x is the aspect ratio (width/height): |
| 398 | // (16/10)x + y = 1.5 |
| 399 | // (10/16)x + y = 1.2 |
| 400 | // We solve for x and y and end up with a final formula: |
| 401 | final float x = |
| 402 | (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) / |
| 403 | (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT); |
| 404 | final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT; |
| 405 | return x * aspectRatio + y; |
| 406 | } |
| 407 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame^] | 408 | public interface OnIDPChangeListener { |
| 409 | |
| 410 | void onIdpChanged(int changeFlags, InvariantDeviceProfile profile); |
| 411 | } |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 412 | } |