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