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