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