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