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