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 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.Utilities.getDevicePrefs; |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 20 | import static com.android.launcher3.Utilities.getPointString; |
Jon Miranda | 73dcc98 | 2020-12-11 14:14:46 -0800 | [diff] [blame] | 21 | import static com.android.launcher3.config.FeatureFlags.ENABLE_FOUR_COLUMNS; |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 22 | import static com.android.launcher3.config.FeatureFlags.ENABLE_TWO_PANEL_HOME; |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 23 | import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; |
Sunny Goyal | 8b0cb41 | 2019-04-22 09:01:26 -0700 | [diff] [blame] | 24 | import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter; |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 25 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 26 | import android.annotation.TargetApi; |
Sunny Goyal | 58fa4b6 | 2019-03-22 16:23:25 -0700 | [diff] [blame] | 27 | import android.appwidget.AppWidgetHostView; |
Hyunyoung Song | e11eb47 | 2019-03-19 15:05:21 -0700 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
Sunny Goyal | 58fa4b6 | 2019-03-22 16:23:25 -0700 | [diff] [blame] | 29 | import android.content.ComponentName; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Hyunyoung Song | e11eb47 | 2019-03-19 15:05:21 -0700 | [diff] [blame] | 31 | import android.content.Intent; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 32 | import android.content.res.Configuration; |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 33 | import android.content.res.Resources; |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 34 | import android.content.res.TypedArray; |
| 35 | import android.content.res.XmlResourceParser; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 36 | import android.graphics.Point; |
Sunny Goyal | 58fa4b6 | 2019-03-22 16:23:25 -0700 | [diff] [blame] | 37 | import android.graphics.Rect; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 38 | import android.text.TextUtils; |
| 39 | import android.util.AttributeSet; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 40 | import android.util.DisplayMetrics; |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 41 | import android.util.Log; |
Sunny Goyal | 5bc1846 | 2019-01-07 15:13:39 -0800 | [diff] [blame] | 42 | import android.util.SparseArray; |
| 43 | import android.util.TypedValue; |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 44 | import android.util.Xml; |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 45 | import android.view.Display; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 46 | |
Sunny Goyal | 6fe3eec | 2019-08-15 14:53:41 -0700 | [diff] [blame] | 47 | import androidx.annotation.Nullable; |
| 48 | import androidx.annotation.VisibleForTesting; |
| 49 | |
Sunny Goyal | 905262c | 2019-05-03 16:50:43 -0700 | [diff] [blame] | 50 | import com.android.launcher3.graphics.IconShape; |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 51 | import com.android.launcher3.util.ConfigMonitor; |
Sunny Goyal | fd58da6 | 2020-08-11 12:06:49 -0700 | [diff] [blame] | 52 | import com.android.launcher3.util.DisplayController; |
| 53 | import com.android.launcher3.util.DisplayController.Info; |
Sunny Goyal | 5bc1846 | 2019-01-07 15:13:39 -0800 | [diff] [blame] | 54 | import com.android.launcher3.util.IntArray; |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 55 | import com.android.launcher3.util.MainThreadInitializedObject; |
Sunny Goyal | 5bc1846 | 2019-01-07 15:13:39 -0800 | [diff] [blame] | 56 | import com.android.launcher3.util.Themes; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 57 | |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 58 | import org.xmlpull.v1.XmlPullParser; |
| 59 | import org.xmlpull.v1.XmlPullParserException; |
| 60 | |
| 61 | import java.io.IOException; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 62 | import java.util.ArrayList; |
Sunny Goyal | 6d55f66 | 2019-01-02 12:13:43 -0800 | [diff] [blame] | 63 | import java.util.Collections; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 64 | |
| 65 | public class InvariantDeviceProfile { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 66 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 67 | public static final String TAG = "IDP"; |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 68 | // We do not need any synchronization for this variable as its only written on UI thread. |
| 69 | public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE = |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 70 | new MainThreadInitializedObject<>(InvariantDeviceProfile::new); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 71 | |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 72 | public static final String KEY_MIGRATION_SRC_WORKSPACE_SIZE = "migration_src_workspace_size"; |
| 73 | public static final String KEY_MIGRATION_SRC_HOTSEAT_COUNT = "migration_src_hotseat_count"; |
| 74 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 75 | private static final String KEY_IDP_GRID_NAME = "idp_grid_name"; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 76 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 77 | private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48; |
| 78 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 79 | public static final int CHANGE_FLAG_GRID = 1 << 0; |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 80 | public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1; |
| 81 | |
| 82 | public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path"; |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 83 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 84 | // Constants that affects the interpolation curve between statically defined device profile |
| 85 | // buckets. |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 86 | private static final float KNEARESTNEIGHBOR = 3; |
| 87 | private static final float WEIGHT_POWER = 5; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 88 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 89 | // 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] | 90 | private static final float WEIGHT_EFFICIENT = 100000f; |
| 91 | |
| 92 | private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier( |
| 93 | "config_icon_mask", "string", "android"); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 94 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Number of icons per row and column in the workspace. |
| 97 | */ |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 98 | public int numRows; |
| 99 | public int numColumns; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Number of icons per row and column in the folder. |
| 103 | */ |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 104 | public int numFolderRows; |
| 105 | public int numFolderColumns; |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 106 | public float iconSize; |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 107 | public String iconShapePath; |
Jon Miranda | b28c4fc | 2017-06-20 10:58:36 -0700 | [diff] [blame] | 108 | public float landscapeIconSize; |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 109 | public int iconBitmapSize; |
| 110 | public int fillResIconDpi; |
| 111 | public float iconTextSize; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 112 | public float allAppsIconSize; |
| 113 | public float allAppsIconTextSize; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 114 | |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 115 | public float minCellHeight; |
| 116 | public float minCellWidth; |
| 117 | public float borderSpacing; |
| 118 | |
Sunny Goyal | 5bc1846 | 2019-01-07 15:13:39 -0800 | [diff] [blame] | 119 | private SparseArray<TypedValue> mExtraAttrs; |
| 120 | |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Number of icons inside the hotseat area. |
| 123 | */ |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 124 | public int numHotseatIcons; |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 125 | |
Jon Miranda | 6f7e970 | 2019-09-16 14:44:14 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Number of columns in the all apps list. |
| 128 | */ |
| 129 | public int numAllAppsColumns; |
| 130 | |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 131 | /** |
| 132 | * Do not query directly. see {@link DeviceProfile#isScalableGrid}. |
| 133 | */ |
| 134 | protected boolean isScalable; |
Jon Miranda | c9e69fa | 2021-03-22 17:13:34 -0400 | [diff] [blame] | 135 | public int devicePaddingId; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 136 | |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 137 | public String dbFile; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 138 | public int defaultLayoutId; |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 139 | int demoModeLayoutId; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 140 | |
cuijiaxing | abda8d7 | 2017-03-20 09:51:36 -0700 | [diff] [blame] | 141 | public DeviceProfile landscapeProfile; |
| 142 | public DeviceProfile portraitProfile; |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 143 | |
Jon Miranda | c9e69fa | 2021-03-22 17:13:34 -0400 | [diff] [blame] | 144 | @Nullable public DevicePaddings devicePaddings; |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 145 | |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 146 | public Point defaultWallpaperSize; |
Sunny Goyal | 58fa4b6 | 2019-03-22 16:23:25 -0700 | [diff] [blame] | 147 | public Rect defaultWidgetPadding; |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 148 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 149 | private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>(); |
| 150 | private ConfigMonitor mConfigMonitor; |
Hyunyoung Song | e11eb47 | 2019-03-19 15:05:21 -0700 | [diff] [blame] | 151 | private OverlayMonitor mOverlayMonitor; |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 152 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 153 | @VisibleForTesting |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 154 | public InvariantDeviceProfile() {} |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 155 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 156 | private InvariantDeviceProfile(InvariantDeviceProfile p) { |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 157 | numRows = p.numRows; |
| 158 | numColumns = p.numColumns; |
| 159 | numFolderRows = p.numFolderRows; |
| 160 | numFolderColumns = p.numFolderColumns; |
| 161 | iconSize = p.iconSize; |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 162 | iconShapePath = p.iconShapePath; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 163 | landscapeIconSize = p.landscapeIconSize; |
Tony Wickham | 9459f31 | 2020-06-15 13:30:20 -0500 | [diff] [blame] | 164 | iconBitmapSize = p.iconBitmapSize; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 165 | iconTextSize = p.iconTextSize; |
| 166 | numHotseatIcons = p.numHotseatIcons; |
Jon Miranda | 6f7e970 | 2019-09-16 14:44:14 -0700 | [diff] [blame] | 167 | numAllAppsColumns = p.numAllAppsColumns; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 168 | isScalable = p.isScalable; |
Jon Miranda | c9e69fa | 2021-03-22 17:13:34 -0400 | [diff] [blame] | 169 | devicePaddingId = p.devicePaddingId; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 170 | minCellHeight = p.minCellHeight; |
| 171 | minCellWidth = p.minCellWidth; |
| 172 | borderSpacing = p.borderSpacing; |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 173 | dbFile = p.dbFile; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 174 | allAppsIconSize = p.allAppsIconSize; |
| 175 | allAppsIconTextSize = p.allAppsIconTextSize; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 176 | defaultLayoutId = p.defaultLayoutId; |
| 177 | demoModeLayoutId = p.demoModeLayoutId; |
Sunny Goyal | 5bc1846 | 2019-01-07 15:13:39 -0800 | [diff] [blame] | 178 | mExtraAttrs = p.mExtraAttrs; |
Hyunyoung Song | e11eb47 | 2019-03-19 15:05:21 -0700 | [diff] [blame] | 179 | mOverlayMonitor = p.mOverlayMonitor; |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 180 | devicePaddings = p.devicePaddings; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Sunny Goyal | bbf0184 | 2015-10-08 07:41:15 -0700 | [diff] [blame] | 183 | @TargetApi(23) |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 184 | private InvariantDeviceProfile(Context context) { |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 185 | String gridName = getCurrentGridName(context); |
| 186 | String newGridName = initGrid(context, gridName); |
| 187 | if (!newGridName.equals(gridName)) { |
| 188 | Utilities.getPrefs(context).edit().putString(KEY_IDP_GRID_NAME, newGridName).apply(); |
| 189 | } |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 190 | Utilities.getPrefs(context).edit() |
| 191 | .putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, numHotseatIcons) |
| 192 | .putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, getPointString(numColumns, numRows)) |
| 193 | .apply(); |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 194 | |
Sunny Goyal | ead22a5 | 2021-02-02 23:21:55 -0800 | [diff] [blame] | 195 | mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged); |
Hyunyoung Song | e11eb47 | 2019-03-19 15:05:21 -0700 | [diff] [blame] | 196 | mOverlayMonitor = new OverlayMonitor(context); |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Hyunyoung Song | e11eb47 | 2019-03-19 15:05:21 -0700 | [diff] [blame] | 199 | /** |
| 200 | * This constructor should NOT have any monitors by design. |
| 201 | */ |
Sunny Goyal | eff44f3 | 2019-01-09 17:29:49 -0800 | [diff] [blame] | 202 | public InvariantDeviceProfile(Context context, String gridName) { |
| 203 | String newName = initGrid(context, gridName); |
| 204 | if (newName == null || !newName.equals(gridName)) { |
| 205 | throw new IllegalArgumentException("Unknown grid name"); |
| 206 | } |
| 207 | } |
| 208 | |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 209 | /** |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 210 | * This constructor should NOT have any monitors by design. |
| 211 | */ |
| 212 | public InvariantDeviceProfile(Context context, Display display) { |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 213 | // Ensure that the main device profile is initialized |
| 214 | InvariantDeviceProfile originalProfile = INSTANCE.get(context); |
| 215 | String gridName = getCurrentGridName(context); |
| 216 | |
| 217 | // Get the display info based on default display and interpolate it to existing display |
| 218 | DisplayOption defaultDisplayOption = invDistWeightedInterpolate( |
Sunny Goyal | fd58da6 | 2020-08-11 12:06:49 -0700 | [diff] [blame] | 219 | DisplayController.getDefaultDisplay(context).getInfo(), |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 220 | getPredefinedDeviceProfiles(context, gridName)); |
| 221 | |
Alex Chau | 417bd72 | 2021-01-26 15:22:18 +0000 | [diff] [blame] | 222 | Info myInfo = new Info(context, display); |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 223 | DisplayOption myDisplayOption = invDistWeightedInterpolate( |
| 224 | myInfo, getPredefinedDeviceProfiles(context, gridName)); |
| 225 | |
| 226 | DisplayOption result = new DisplayOption(defaultDisplayOption.grid) |
| 227 | .add(myDisplayOption); |
| 228 | result.iconSize = defaultDisplayOption.iconSize; |
| 229 | result.landscapeIconSize = defaultDisplayOption.landscapeIconSize; |
Alex Chau | 7c43972 | 2021-03-24 11:32:44 +0000 | [diff] [blame] | 230 | if (defaultDisplayOption.allAppsIconSize < myDisplayOption.allAppsIconSize) { |
| 231 | result.allAppsIconSize = defaultDisplayOption.allAppsIconSize; |
| 232 | result.numAllAppsColumns = defaultDisplayOption.numAllAppsColumns; |
| 233 | } else { |
| 234 | result.allAppsIconSize = myDisplayOption.allAppsIconSize; |
| 235 | result.numAllAppsColumns = myDisplayOption.numAllAppsColumns; |
| 236 | } |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 237 | result.minCellHeight = defaultDisplayOption.minCellHeight; |
| 238 | result.minCellWidth = defaultDisplayOption.minCellWidth; |
| 239 | result.borderSpacing = defaultDisplayOption.borderSpacing; |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 240 | |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 241 | initGrid(context, myInfo, result); |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Tracy Zhou | 42255d2 | 2020-03-13 00:38:11 -0700 | [diff] [blame] | 244 | public static String getCurrentGridName(Context context) { |
Andras Kloczl | 8e57cce | 2021-02-11 23:51:19 +0100 | [diff] [blame] | 245 | if (ENABLE_TWO_PANEL_HOME.get()) { |
| 246 | return ENABLE_TWO_PANEL_HOME.key; |
| 247 | } |
Jon Miranda | 73dcc98 | 2020-12-11 14:14:46 -0800 | [diff] [blame] | 248 | if (ENABLE_FOUR_COLUMNS.get()) { |
| 249 | return ENABLE_FOUR_COLUMNS.key; |
| 250 | } |
Tracy Zhou | c6060e6 | 2020-04-27 13:05:34 -0700 | [diff] [blame] | 251 | return Utilities.isGridOptionsEnabled(context) |
| 252 | ? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null; |
Tracy Zhou | 42255d2 | 2020-03-13 00:38:11 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 255 | /** |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 256 | * Retrieve system defined or RRO overriden icon shape. |
| 257 | */ |
| 258 | private static String getIconShapePath(Context context) { |
| 259 | if (CONFIG_ICON_MASK_RES_ID == 0) { |
| 260 | Log.e(TAG, "Icon mask res identifier failed to retrieve."); |
| 261 | return ""; |
| 262 | } |
| 263 | return context.getResources().getString(CONFIG_ICON_MASK_RES_ID); |
| 264 | } |
| 265 | |
Sunny Goyal | eff44f3 | 2019-01-09 17:29:49 -0800 | [diff] [blame] | 266 | private String initGrid(Context context, String gridName) { |
Sunny Goyal | fd58da6 | 2020-08-11 12:06:49 -0700 | [diff] [blame] | 267 | Info displayInfo = DisplayController.getDefaultDisplay(context).getInfo(); |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 268 | ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName); |
| 269 | |
| 270 | DisplayOption displayOption = invDistWeightedInterpolate(displayInfo, allOptions); |
| 271 | initGrid(context, displayInfo, displayOption); |
| 272 | return displayOption.grid.name; |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 273 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 274 | |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 275 | private void initGrid( |
Sunny Goyal | fd58da6 | 2020-08-11 12:06:49 -0700 | [diff] [blame] | 276 | Context context, Info displayInfo, DisplayOption displayOption) { |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 277 | GridOption closestProfile = displayOption.grid; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 278 | numRows = closestProfile.numRows; |
| 279 | numColumns = closestProfile.numColumns; |
| 280 | numHotseatIcons = closestProfile.numHotseatIcons; |
| 281 | dbFile = closestProfile.dbFile; |
| 282 | defaultLayoutId = closestProfile.defaultLayoutId; |
| 283 | demoModeLayoutId = closestProfile.demoModeLayoutId; |
| 284 | numFolderRows = closestProfile.numFolderRows; |
| 285 | numFolderColumns = closestProfile.numFolderColumns; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 286 | isScalable = closestProfile.isScalable; |
Jon Miranda | c9e69fa | 2021-03-22 17:13:34 -0400 | [diff] [blame] | 287 | devicePaddingId = closestProfile.devicePaddingId; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 288 | |
| 289 | mExtraAttrs = closestProfile.extraAttrs; |
| 290 | |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 291 | iconSize = displayOption.iconSize; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 292 | iconShapePath = getIconShapePath(context); |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 293 | landscapeIconSize = displayOption.landscapeIconSize; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 294 | iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics); |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 295 | iconTextSize = displayOption.iconTextSize; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 296 | fillResIconDpi = getLauncherIconDensity(iconBitmapSize); |
| 297 | |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 298 | minCellHeight = displayOption.minCellHeight; |
| 299 | minCellWidth = displayOption.minCellWidth; |
| 300 | borderSpacing = displayOption.borderSpacing; |
Alex Chau | 7c43972 | 2021-03-24 11:32:44 +0000 | [diff] [blame] | 301 | numAllAppsColumns = Math.round(displayOption.numAllAppsColumns); |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 302 | |
Tracy Zhou | c6060e6 | 2020-04-27 13:05:34 -0700 | [diff] [blame] | 303 | if (Utilities.isGridOptionsEnabled(context)) { |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 304 | allAppsIconSize = displayOption.allAppsIconSize; |
| 305 | allAppsIconTextSize = displayOption.allAppsIconTextSize; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 306 | } else { |
| 307 | allAppsIconSize = iconSize; |
| 308 | allAppsIconTextSize = iconTextSize; |
| 309 | } |
| 310 | |
Jon Miranda | c9e69fa | 2021-03-22 17:13:34 -0400 | [diff] [blame] | 311 | if (devicePaddingId != 0) { |
| 312 | devicePaddings = new DevicePaddings(context, devicePaddingId); |
| 313 | } |
| 314 | |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 315 | // If the partner customization apk contains any grid overrides, apply them |
| 316 | // Supported overrides: numRows, numColumns, iconSize |
| 317 | applyPartnerDeviceProfileOverrides(context, displayInfo.metrics); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 318 | |
Winson Chung | 13c1c2c | 2019-09-06 11:46:19 -0700 | [diff] [blame] | 319 | Point realSize = new Point(displayInfo.realSize); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 320 | // The real size never changes. smallSide and largeSide will remain the |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 321 | // same in any orientation. |
| 322 | int smallSide = Math.min(realSize.x, realSize.y); |
| 323 | int largeSide = Math.max(realSize.x, realSize.y); |
| 324 | |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 325 | DeviceProfile.Builder builder = new DeviceProfile.Builder(context, this, displayInfo) |
| 326 | .setSizeRange(new Point(displayInfo.smallestSize), |
| 327 | new Point(displayInfo.largestSize)); |
| 328 | |
| 329 | landscapeProfile = builder.setSize(largeSide, smallSide).build(); |
| 330 | portraitProfile = builder.setSize(smallSide, largeSide).build(); |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 331 | |
| 332 | // We need to ensure that there is enough extra space in the wallpaper |
| 333 | // for the intended parallax effects |
| 334 | if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) { |
| 335 | defaultWallpaperSize = new Point( |
| 336 | (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)), |
| 337 | largeSide); |
| 338 | } else { |
| 339 | defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide); |
| 340 | } |
Sunny Goyal | 58fa4b6 | 2019-03-22 16:23:25 -0700 | [diff] [blame] | 341 | |
| 342 | ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName()); |
| 343 | defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Sunny Goyal | 5bc1846 | 2019-01-07 15:13:39 -0800 | [diff] [blame] | 346 | @Nullable |
| 347 | public TypedValue getAttrValue(int attr) { |
| 348 | return mExtraAttrs == null ? null : mExtraAttrs.get(attr); |
| 349 | } |
| 350 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 351 | public void addOnChangeListener(OnIDPChangeListener listener) { |
| 352 | mChangeListeners.add(listener); |
| 353 | } |
| 354 | |
Hyunyoung Song | b4d1ca4 | 2019-01-08 17:15:16 -0800 | [diff] [blame] | 355 | public void removeOnChangeListener(OnIDPChangeListener listener) { |
| 356 | mChangeListeners.remove(listener); |
| 357 | } |
| 358 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 359 | public void verifyConfigChangedInBackground(final Context context) { |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 360 | String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, ""); |
| 361 | // Good place to check if grid size changed in themepicker when launcher was dead. |
| 362 | if (savedIconMaskPath.isEmpty()) { |
| 363 | getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context)) |
| 364 | .apply(); |
| 365 | } else if (!savedIconMaskPath.equals(getIconShapePath(context))) { |
| 366 | getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context)) |
| 367 | .apply(); |
| 368 | apply(context, CHANGE_FLAG_ICON_PARAMS); |
| 369 | } |
| 370 | } |
| 371 | |
Sunny Goyal | 7d892ff | 2019-01-11 15:08:44 -0800 | [diff] [blame] | 372 | public void setCurrentGrid(Context context, String gridName) { |
| 373 | Context appContext = context.getApplicationContext(); |
| 374 | Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply(); |
Sunny Goyal | 6fe3eec | 2019-08-15 14:53:41 -0700 | [diff] [blame] | 375 | MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext)); |
Sunny Goyal | 7d892ff | 2019-01-11 15:08:44 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 378 | private void onConfigChanged(Context context) { |
| 379 | // Config changes, what shall we do? |
| 380 | InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this); |
| 381 | |
| 382 | // Re-init grid |
Tracy Zhou | c6060e6 | 2020-04-27 13:05:34 -0700 | [diff] [blame] | 383 | String gridName = getCurrentGridName(context); |
Jon Miranda | 6f7e970 | 2019-09-16 14:44:14 -0700 | [diff] [blame] | 384 | initGrid(context, gridName); |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 385 | |
| 386 | int changeFlags = 0; |
| 387 | if (numRows != oldProfile.numRows || |
| 388 | numColumns != oldProfile.numColumns || |
| 389 | numFolderColumns != oldProfile.numFolderColumns || |
| 390 | numFolderRows != oldProfile.numFolderRows || |
| 391 | numHotseatIcons != oldProfile.numHotseatIcons) { |
| 392 | changeFlags |= CHANGE_FLAG_GRID; |
| 393 | } |
| 394 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 395 | if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize || |
| 396 | !iconShapePath.equals(oldProfile.iconShapePath)) { |
| 397 | changeFlags |= CHANGE_FLAG_ICON_PARAMS; |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 398 | } |
Sunny Goyal | 90e3fbc | 2019-01-23 16:42:43 -0800 | [diff] [blame] | 399 | if (!iconShapePath.equals(oldProfile.iconShapePath)) { |
Sunny Goyal | 905262c | 2019-05-03 16:50:43 -0700 | [diff] [blame] | 400 | IconShape.init(context); |
Sunny Goyal | 90e3fbc | 2019-01-23 16:42:43 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 403 | apply(context, changeFlags); |
| 404 | } |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 405 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 406 | private void apply(Context context, int changeFlags) { |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 407 | // Create a new config monitor |
| 408 | mConfigMonitor.unregister(); |
| 409 | mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged); |
| 410 | |
| 411 | for (OnIDPChangeListener listener : mChangeListeners) { |
| 412 | listener.onIdpChanged(changeFlags, this); |
| 413 | } |
| 414 | } |
| 415 | |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 416 | static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) { |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 417 | ArrayList<DisplayOption> profiles = new ArrayList<>(); |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 418 | try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) { |
| 419 | final int depth = parser.getDepth(); |
| 420 | int type; |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 421 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 422 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
Sunny Goyal | 7d892ff | 2019-01-11 15:08:44 -0800 | [diff] [blame] | 423 | if ((type == XmlPullParser.START_TAG) |
| 424 | && GridOption.TAG_NAME.equals(parser.getName())) { |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 425 | |
| 426 | GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser)); |
| 427 | final int displayDepth = parser.getDepth(); |
| 428 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 429 | parser.getDepth() > displayDepth) |
| 430 | && type != XmlPullParser.END_DOCUMENT) { |
| 431 | if ((type == XmlPullParser.START_TAG) && "display-option".equals( |
| 432 | parser.getName())) { |
| 433 | profiles.add(new DisplayOption( |
| 434 | gridOption, context, Xml.asAttributeSet(parser))); |
| 435 | } |
| 436 | } |
Sunny Goyal | 819e193 | 2016-07-07 16:43:58 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } catch (IOException|XmlPullParserException e) { |
| 440 | throw new RuntimeException(e); |
| 441 | } |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 442 | |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 443 | ArrayList<DisplayOption> filteredProfiles = new ArrayList<>(); |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 444 | if (!TextUtils.isEmpty(gridName)) { |
| 445 | for (DisplayOption option : profiles) { |
| 446 | if (gridName.equals(option.grid.name)) { |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 447 | filteredProfiles.add(option); |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | } |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 451 | if (filteredProfiles.isEmpty()) { |
| 452 | // No grid found, use the default options |
| 453 | for (DisplayOption option : profiles) { |
| 454 | if (option.canBeDefault) { |
| 455 | filteredProfiles.add(option); |
| 456 | } |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 457 | } |
| 458 | } |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 459 | if (filteredProfiles.isEmpty()) { |
| 460 | throw new RuntimeException("No display option with canBeDefault=true"); |
| 461 | } |
| 462 | return filteredProfiles; |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 465 | private int getLauncherIconDensity(int requiredSize) { |
| 466 | // Densities typically defined by an app. |
| 467 | int[] densityBuckets = new int[] { |
| 468 | DisplayMetrics.DENSITY_LOW, |
| 469 | DisplayMetrics.DENSITY_MEDIUM, |
| 470 | DisplayMetrics.DENSITY_TV, |
| 471 | DisplayMetrics.DENSITY_HIGH, |
| 472 | DisplayMetrics.DENSITY_XHIGH, |
| 473 | DisplayMetrics.DENSITY_XXHIGH, |
| 474 | DisplayMetrics.DENSITY_XXXHIGH |
| 475 | }; |
| 476 | |
| 477 | int density = DisplayMetrics.DENSITY_XXXHIGH; |
| 478 | for (int i = densityBuckets.length - 1; i >= 0; i--) { |
| 479 | float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i] |
| 480 | / DisplayMetrics.DENSITY_DEFAULT; |
| 481 | if (expectedSize >= requiredSize) { |
| 482 | density = densityBuckets[i]; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return density; |
| 487 | } |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 488 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 489 | /** |
| 490 | * Apply any Partner customization grid overrides. |
| 491 | * |
| 492 | * Currently we support: all apps row / column count. |
| 493 | */ |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 494 | private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) { |
| 495 | Partner p = Partner.get(context.getPackageManager()); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 496 | if (p != null) { |
| 497 | p.applyInvariantDeviceProfileOverrides(this, dm); |
| 498 | } |
| 499 | } |
| 500 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 501 | private static float dist(float x0, float y0, float x1, float y1) { |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 502 | return (float) Math.hypot(x1 - x0, y1 - y0); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 505 | @VisibleForTesting |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 506 | static DisplayOption invDistWeightedInterpolate( |
Sunny Goyal | fd58da6 | 2020-08-11 12:06:49 -0700 | [diff] [blame] | 507 | Info displayInfo, ArrayList<DisplayOption> points) { |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 508 | Point smallestSize = new Point(displayInfo.smallestSize); |
| 509 | Point largestSize = new Point(displayInfo.largestSize); |
| 510 | |
| 511 | // This guarantees that width < height |
| 512 | float width = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), |
| 513 | displayInfo.metrics); |
| 514 | float height = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), |
| 515 | displayInfo.metrics); |
| 516 | |
| 517 | // Sort the profiles based on the closeness to the device size |
| 518 | Collections.sort(points, (a, b) -> |
| 519 | Float.compare(dist(width, height, a.minWidthDps, a.minHeightDps), |
| 520 | dist(width, height, b.minWidthDps, b.minHeightDps))); |
| 521 | |
| 522 | GridOption closestOption = points.get(0).grid; |
| 523 | float weights = 0; |
| 524 | |
| 525 | DisplayOption p = points.get(0); |
| 526 | if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) { |
| 527 | return p; |
| 528 | } |
| 529 | |
| 530 | DisplayOption out = new DisplayOption(closestOption); |
| 531 | for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) { |
| 532 | p = points.get(i); |
| 533 | float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER); |
| 534 | weights += w; |
| 535 | out.add(new DisplayOption().add(p).multiply(w)); |
| 536 | } |
| 537 | return out.multiply(1.0f / weights); |
| 538 | } |
| 539 | |
| 540 | @VisibleForTesting |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 541 | static DisplayOption invDistWeightedInterpolate(float width, float height, |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 542 | ArrayList<DisplayOption> points) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 543 | float weights = 0; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 544 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 545 | DisplayOption p = points.get(0); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 546 | if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) { |
| 547 | return p; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 550 | DisplayOption out = new DisplayOption(); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 551 | for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) { |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 552 | p = points.get(i); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 553 | float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER); |
| 554 | weights += w; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 555 | out.add(new DisplayOption().add(p).multiply(w)); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 556 | } |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 557 | return out.multiply(1.0f / weights); |
Hyunyoung Song | 35c3c7f | 2015-05-28 15:33:40 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 560 | public DeviceProfile getDeviceProfile(Context context) { |
| 561 | return context.getResources().getConfiguration().orientation |
| 562 | == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile; |
| 563 | } |
| 564 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 565 | 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] | 566 | float d = dist(x0, y0, x1, y1); |
| 567 | if (Float.compare(d, 0f) == 0) { |
| 568 | return Float.POSITIVE_INFINITY; |
| 569 | } |
| 570 | return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow)); |
| 571 | } |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * As a ratio of screen height, the total distance we want the parallax effect to span |
| 575 | * horizontally |
| 576 | */ |
| 577 | private static float wallpaperTravelToScreenWidthRatio(int width, int height) { |
| 578 | float aspectRatio = width / (float) height; |
| 579 | |
| 580 | // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width |
| 581 | // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width |
| 582 | // We will use these two data points to extrapolate how much the wallpaper parallax effect |
| 583 | // to span (ie travel) at any aspect ratio: |
| 584 | |
| 585 | final float ASPECT_RATIO_LANDSCAPE = 16/10f; |
| 586 | final float ASPECT_RATIO_PORTRAIT = 10/16f; |
| 587 | final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f; |
| 588 | final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f; |
| 589 | |
| 590 | // To find out the desired width at different aspect ratios, we use the following two |
| 591 | // formulas, where the coefficient on x is the aspect ratio (width/height): |
| 592 | // (16/10)x + y = 1.5 |
| 593 | // (10/16)x + y = 1.2 |
| 594 | // We solve for x and y and end up with a final formula: |
| 595 | final float x = |
| 596 | (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) / |
| 597 | (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT); |
| 598 | final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT; |
| 599 | return x * aspectRatio + y; |
| 600 | } |
| 601 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 602 | public interface OnIDPChangeListener { |
| 603 | |
| 604 | void onIdpChanged(int changeFlags, InvariantDeviceProfile profile); |
| 605 | } |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 606 | |
| 607 | |
Sunny Goyal | eff44f3 | 2019-01-09 17:29:49 -0800 | [diff] [blame] | 608 | public static final class GridOption { |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 609 | |
Sunny Goyal | 7d892ff | 2019-01-11 15:08:44 -0800 | [diff] [blame] | 610 | public static final String TAG_NAME = "grid-option"; |
| 611 | |
Sunny Goyal | eff44f3 | 2019-01-09 17:29:49 -0800 | [diff] [blame] | 612 | public final String name; |
| 613 | public final int numRows; |
| 614 | public final int numColumns; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 615 | |
| 616 | private final int numFolderRows; |
| 617 | private final int numFolderColumns; |
| 618 | |
| 619 | private final int numHotseatIcons; |
| 620 | |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 621 | private final String dbFile; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 622 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 623 | private final int defaultLayoutId; |
| 624 | private final int demoModeLayoutId; |
| 625 | |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 626 | private final boolean isScalable; |
Jon Miranda | c9e69fa | 2021-03-22 17:13:34 -0400 | [diff] [blame] | 627 | private final int devicePaddingId; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 628 | |
Jon Miranda | ec1277e | 2021-03-25 10:41:54 -0400 | [diff] [blame^] | 629 | public final boolean visible; |
| 630 | |
Sunny Goyal | 5bc1846 | 2019-01-07 15:13:39 -0800 | [diff] [blame] | 631 | private final SparseArray<TypedValue> extraAttrs; |
| 632 | |
Sunny Goyal | eff44f3 | 2019-01-09 17:29:49 -0800 | [diff] [blame] | 633 | public GridOption(Context context, AttributeSet attrs) { |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 634 | TypedArray a = context.obtainStyledAttributes( |
| 635 | attrs, R.styleable.GridDisplayOption); |
| 636 | name = a.getString(R.styleable.GridDisplayOption_name); |
| 637 | numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0); |
| 638 | numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0); |
| 639 | |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 640 | dbFile = a.getString(R.styleable.GridDisplayOption_dbFile); |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 641 | defaultLayoutId = a.getResourceId( |
| 642 | R.styleable.GridDisplayOption_defaultLayoutId, 0); |
| 643 | demoModeLayoutId = a.getResourceId( |
| 644 | R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId); |
| 645 | numHotseatIcons = a.getInt( |
| 646 | R.styleable.GridDisplayOption_numHotseatIcons, numColumns); |
| 647 | numFolderRows = a.getInt( |
| 648 | R.styleable.GridDisplayOption_numFolderRows, numRows); |
| 649 | numFolderColumns = a.getInt( |
| 650 | R.styleable.GridDisplayOption_numFolderColumns, numColumns); |
Jon Miranda | 6f7e970 | 2019-09-16 14:44:14 -0700 | [diff] [blame] | 651 | |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 652 | isScalable = a.getBoolean( |
| 653 | R.styleable.GridDisplayOption_isScalable, false); |
Jon Miranda | c9e69fa | 2021-03-22 17:13:34 -0400 | [diff] [blame] | 654 | devicePaddingId = a.getResourceId( |
| 655 | R.styleable.GridDisplayOption_devicePaddingId, 0); |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 656 | |
Jon Miranda | ec1277e | 2021-03-25 10:41:54 -0400 | [diff] [blame^] | 657 | visible = a.getBoolean(R.styleable.GridDisplayOption_visible, true); |
| 658 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 659 | a.recycle(); |
Sunny Goyal | 5bc1846 | 2019-01-07 15:13:39 -0800 | [diff] [blame] | 660 | |
| 661 | extraAttrs = Themes.createValueMap(context, attrs, |
| 662 | IntArray.wrap(R.styleable.GridDisplayOption)); |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | private static final class DisplayOption { |
| 667 | private final GridOption grid; |
| 668 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 669 | private final float minWidthDps; |
| 670 | private final float minHeightDps; |
| 671 | private final boolean canBeDefault; |
| 672 | |
Alex Chau | 7c43972 | 2021-03-24 11:32:44 +0000 | [diff] [blame] | 673 | private float numAllAppsColumns; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 674 | private float minCellHeight; |
| 675 | private float minCellWidth; |
| 676 | private float borderSpacing; |
| 677 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 678 | private float iconSize; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 679 | private float iconTextSize; |
Jon Miranda | 6f7e970 | 2019-09-16 14:44:14 -0700 | [diff] [blame] | 680 | private float landscapeIconSize; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 681 | private float allAppsIconSize; |
| 682 | private float allAppsIconTextSize; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 683 | |
| 684 | DisplayOption(GridOption grid, Context context, AttributeSet attrs) { |
| 685 | this.grid = grid; |
| 686 | |
| 687 | TypedArray a = context.obtainStyledAttributes( |
| 688 | attrs, R.styleable.ProfileDisplayOption); |
| 689 | |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 690 | minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0); |
| 691 | minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0); |
| 692 | canBeDefault = a.getBoolean( |
| 693 | R.styleable.ProfileDisplayOption_canBeDefault, false); |
Alex Chau | 7c43972 | 2021-03-24 11:32:44 +0000 | [diff] [blame] | 694 | numAllAppsColumns = a.getInt(R.styleable.ProfileDisplayOption_numAllAppsColumns, |
| 695 | grid.numColumns); |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 696 | |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 697 | minCellHeight = a.getFloat(R.styleable.ProfileDisplayOption_minCellHeightDps, 0); |
| 698 | minCellWidth = a.getFloat(R.styleable.ProfileDisplayOption_minCellWidthDps, 0); |
| 699 | borderSpacing = a.getFloat(R.styleable.ProfileDisplayOption_borderSpacingDps, 0); |
| 700 | |
Ryan Mitchell | 01b8b68 | 2019-03-28 17:01:07 -0700 | [diff] [blame] | 701 | iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0); |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 702 | landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize, |
| 703 | iconSize); |
| 704 | iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0); |
Jon Miranda | 6f7e970 | 2019-09-16 14:44:14 -0700 | [diff] [blame] | 705 | |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 706 | allAppsIconSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconSize, |
| 707 | iconSize); |
| 708 | allAppsIconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_allAppsIconTextSize, |
| 709 | iconTextSize); |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 710 | a.recycle(); |
| 711 | } |
| 712 | |
| 713 | DisplayOption() { |
Sunny Goyal | 0e7a338 | 2020-04-13 17:15:05 -0700 | [diff] [blame] | 714 | this(null); |
| 715 | } |
| 716 | |
| 717 | DisplayOption(GridOption grid) { |
| 718 | this.grid = grid; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 719 | minWidthDps = 0; |
| 720 | minHeightDps = 0; |
| 721 | canBeDefault = false; |
Alex Chau | 7c43972 | 2021-03-24 11:32:44 +0000 | [diff] [blame] | 722 | numAllAppsColumns = 0; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 723 | minCellHeight = 0; |
| 724 | minCellWidth = 0; |
| 725 | borderSpacing = 0; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | private DisplayOption multiply(float w) { |
Alex Chau | 7c43972 | 2021-03-24 11:32:44 +0000 | [diff] [blame] | 729 | numAllAppsColumns *= w; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 730 | iconSize *= w; |
| 731 | landscapeIconSize *= w; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 732 | allAppsIconSize *= w; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 733 | iconTextSize *= w; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 734 | allAppsIconTextSize *= w; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 735 | minCellHeight *= w; |
| 736 | minCellWidth *= w; |
| 737 | borderSpacing *= w; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 738 | return this; |
| 739 | } |
| 740 | |
| 741 | private DisplayOption add(DisplayOption p) { |
Alex Chau | 7c43972 | 2021-03-24 11:32:44 +0000 | [diff] [blame] | 742 | numAllAppsColumns += p.numAllAppsColumns; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 743 | iconSize += p.iconSize; |
| 744 | landscapeIconSize += p.landscapeIconSize; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 745 | allAppsIconSize += p.allAppsIconSize; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 746 | iconTextSize += p.iconTextSize; |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 747 | allAppsIconTextSize += p.allAppsIconTextSize; |
Jon Miranda | e126d72 | 2021-02-25 10:45:20 -0500 | [diff] [blame] | 748 | minCellHeight += p.minCellHeight; |
| 749 | minCellWidth += p.minCellWidth; |
| 750 | borderSpacing += p.borderSpacing; |
Sunny Goyal | 415f173 | 2018-11-29 10:33:47 -0800 | [diff] [blame] | 751 | return this; |
| 752 | } |
| 753 | } |
Hyunyoung Song | e11eb47 | 2019-03-19 15:05:21 -0700 | [diff] [blame] | 754 | |
| 755 | private class OverlayMonitor extends BroadcastReceiver { |
| 756 | |
| 757 | private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED"; |
| 758 | |
| 759 | OverlayMonitor(Context context) { |
Sunny Goyal | 8b0cb41 | 2019-04-22 09:01:26 -0700 | [diff] [blame] | 760 | context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED)); |
Hyunyoung Song | e11eb47 | 2019-03-19 15:05:21 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | @Override |
| 764 | public void onReceive(Context context, Intent intent) { |
| 765 | onConfigChanged(context); |
| 766 | } |
| 767 | } |
Sunny Goyal | ae190ff | 2020-04-14 00:19:01 +0000 | [diff] [blame] | 768 | } |