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