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