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