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