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