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