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