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