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