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