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