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