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