blob: 208a822eba7451d5a6a90c17921f1ecc318ab10d [file] [log] [blame]
Adam Cohen2e6da152015-05-06 11:42:25 -07001/*
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
17package com.android.launcher3;
18
Hyunyoung Songc55a3502018-12-04 15:43:16 -080019import static com.android.launcher3.Utilities.getDevicePrefs;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070020import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070021import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal8b0cb412019-04-22 09:01:26 -070022import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070023
Sunny Goyalc6205602015-05-21 20:46:33 -070024import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070025import android.appwidget.AppWidgetHostView;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070026import android.content.BroadcastReceiver;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070027import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070028import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070029import android.content.Intent;
Sunny Goyal27835952017-01-13 12:15:53 -080030import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080031import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070032import android.content.res.TypedArray;
33import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import android.graphics.Point;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070035import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080036import android.text.TextUtils;
37import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070038import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070039import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080040import android.util.SparseArray;
41import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070042import android.util.Xml;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070043
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070044import androidx.annotation.Nullable;
45import androidx.annotation.VisibleForTesting;
46
Sunny Goyal905262c2019-05-03 16:50:43 -070047import com.android.launcher3.graphics.IconShape;
Sunny Goyald0e360a2018-06-29 14:40:18 -070048import com.android.launcher3.util.ConfigMonitor;
Winson Chung13c1c2c2019-09-06 11:46:19 -070049import com.android.launcher3.util.DefaultDisplay;
Sunny Goyal5bc18462019-01-07 15:13:39 -080050import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070051import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080052import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070053
Sunny Goyal819e1932016-07-07 16:43:58 -070054import org.xmlpull.v1.XmlPullParser;
55import org.xmlpull.v1.XmlPullParserException;
56
57import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070058import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080059import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070060
61public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070062
Hyunyoung Songc55a3502018-12-04 15:43:16 -080063 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070064 // We do not need any synchronization for this variable as its only written on UI thread.
65 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070066 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070067
Hyunyoung Songc55a3502018-12-04 15:43:16 -080068 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080069
Sunny Goyal53d7ee42015-05-22 12:25:45 -070070 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
71
Sunny Goyal87dc48b2018-10-12 11:42:33 -070072 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080073 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
74
75 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070076
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070077 // Constants that affects the interpolation curve between statically defined device profile
78 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080079 private static final float KNEARESTNEIGHBOR = 3;
80 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070081
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070082 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080083 private static final float WEIGHT_EFFICIENT = 100000f;
84
85 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
86 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070087
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070088 /**
89 * Number of icons per row and column in the workspace.
90 */
Adam Cohen2e6da152015-05-06 11:42:25 -070091 public int numRows;
92 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070093
94 /**
95 * Number of icons per row and column in the folder.
96 */
Adam Cohen2e6da152015-05-06 11:42:25 -070097 public int numFolderRows;
98 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070099 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800100 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -0700101 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -0700102 public int iconBitmapSize;
103 public int fillResIconDpi;
104 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700105
Sunny Goyal5bc18462019-01-07 15:13:39 -0800106 private SparseArray<TypedValue> mExtraAttrs;
107
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700108 /**
109 * Number of icons inside the hotseat area.
110 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800111 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700112
Sunny Goyal415f1732018-11-29 10:33:47 -0800113 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700114 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700115
cuijiaxingabda8d72017-03-20 09:51:36 -0700116 public DeviceProfile landscapeProfile;
117 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700118
Sunny Goyal6f866092016-03-17 17:04:15 -0700119 public Point defaultWallpaperSize;
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700120 public Rect defaultWidgetPadding;
Sunny Goyal6f866092016-03-17 17:04:15 -0700121
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700122 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
123 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700124 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700125
Sunny Goyalf633ef52018-03-13 09:57:05 -0700126 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700127 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700128
Sunny Goyalf633ef52018-03-13 09:57:05 -0700129 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800130 numRows = p.numRows;
131 numColumns = p.numColumns;
132 numFolderRows = p.numFolderRows;
133 numFolderColumns = p.numFolderColumns;
134 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800135 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800136 landscapeIconSize = p.landscapeIconSize;
137 iconTextSize = p.iconTextSize;
138 numHotseatIcons = p.numHotseatIcons;
139 defaultLayoutId = p.defaultLayoutId;
140 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800141 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700142 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700143 }
144
Sunny Goyalbbf01842015-10-08 07:41:15 -0700145 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700146 private InvariantDeviceProfile(Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800147 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700148 mConfigMonitor = new ConfigMonitor(context,
149 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700150 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700151 }
152
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700153 /**
154 * This constructor should NOT have any monitors by design.
155 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800156 public InvariantDeviceProfile(Context context, String gridName) {
157 String newName = initGrid(context, gridName);
158 if (newName == null || !newName.equals(gridName)) {
159 throw new IllegalArgumentException("Unknown grid name");
160 }
161 }
162
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800163 /**
164 * Retrieve system defined or RRO overriden icon shape.
165 */
166 private static String getIconShapePath(Context context) {
167 if (CONFIG_ICON_MASK_RES_ID == 0) {
168 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
169 return "";
170 }
171 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
172 }
173
Sunny Goyaleff44f32019-01-09 17:29:49 -0800174 private String initGrid(Context context, String gridName) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700175 DefaultDisplay.Info displayInfo = DefaultDisplay.INSTANCE.get(context).getInfo();
Adam Cohen2e6da152015-05-06 11:42:25 -0700176
Winson Chung13c1c2c2019-09-06 11:46:19 -0700177 Point smallestSize = new Point(displayInfo.smallestSize);
178 Point largestSize = new Point(displayInfo.largestSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700179
Sunny Goyal415f1732018-11-29 10:33:47 -0800180 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700181 // This guarantees that width < height
Winson Chung13c1c2c2019-09-06 11:46:19 -0700182 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y),
183 displayInfo.metrics);
184 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y),
185 displayInfo.metrics);
Sunny Goyal415f1732018-11-29 10:33:47 -0800186 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800187 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800188 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
189 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
190 DisplayOption interpolatedDisplayOption =
191 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700192
Sunny Goyal415f1732018-11-29 10:33:47 -0800193 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700194 numRows = closestProfile.numRows;
195 numColumns = closestProfile.numColumns;
196 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700197 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700198 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700199 numFolderRows = closestProfile.numFolderRows;
200 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800201 mExtraAttrs = closestProfile.extraAttrs;
202
Sunny Goyal415f1732018-11-29 10:33:47 -0800203 if (!closestProfile.name.equals(gridName)) {
204 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800205 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800206 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700207
Sunny Goyal415f1732018-11-29 10:33:47 -0800208 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800209 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800210 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Winson Chung13c1c2c2019-09-06 11:46:19 -0700211 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, displayInfo.metrics);
Sunny Goyal415f1732018-11-29 10:33:47 -0800212 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700213 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700214
215 // If the partner customization apk contains any grid overrides, apply them
216 // Supported overrides: numRows, numColumns, iconSize
Winson Chung13c1c2c2019-09-06 11:46:19 -0700217 applyPartnerDeviceProfileOverrides(context, displayInfo.metrics);
Sunny Goyalc6205602015-05-21 20:46:33 -0700218
Winson Chung13c1c2c2019-09-06 11:46:19 -0700219 Point realSize = new Point(displayInfo.realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700220 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700221 // same in any orientation.
222 int smallSide = Math.min(realSize.x, realSize.y);
223 int largeSide = Math.max(realSize.x, realSize.y);
224
225 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800226 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700227 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800228 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700229
230 // We need to ensure that there is enough extra space in the wallpaper
231 // for the intended parallax effects
232 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
233 defaultWallpaperSize = new Point(
234 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
235 largeSide);
236 } else {
237 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
238 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700239
240 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
241 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
242
Sunny Goyaleff44f32019-01-09 17:29:49 -0800243 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700244 }
245
Sunny Goyal5bc18462019-01-07 15:13:39 -0800246 @Nullable
247 public TypedValue getAttrValue(int attr) {
248 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
249 }
250
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700251 public void addOnChangeListener(OnIDPChangeListener listener) {
252 mChangeListeners.add(listener);
253 }
254
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800255 public void removeOnChangeListener(OnIDPChangeListener listener) {
256 mChangeListeners.remove(listener);
257 }
258
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700259 private void killProcess(Context context) {
260 Log.e("ConfigMonitor", "restarting launcher");
261 android.os.Process.killProcess(android.os.Process.myPid());
262 }
263
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800264 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800265 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
266 // Good place to check if grid size changed in themepicker when launcher was dead.
267 if (savedIconMaskPath.isEmpty()) {
268 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
269 .apply();
270 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
271 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
272 .apply();
273 apply(context, CHANGE_FLAG_ICON_PARAMS);
274 }
275 }
276
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800277 public void setCurrentGrid(Context context, String gridName) {
278 Context appContext = context.getApplicationContext();
279 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700280 MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800281 }
282
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700283 private void onConfigChanged(Context context) {
284 // Config changes, what shall we do?
285 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
286
287 // Re-init grid
Jon Mirandac0f1f4f2019-05-28 15:49:09 -0700288 // TODO(b/131867841): We pass in null here so that we can calculate the closest profile
289 // without the bias of the grid name.
290 initGrid(context, null);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700291
292 int changeFlags = 0;
293 if (numRows != oldProfile.numRows ||
294 numColumns != oldProfile.numColumns ||
295 numFolderColumns != oldProfile.numFolderColumns ||
296 numFolderRows != oldProfile.numFolderRows ||
297 numHotseatIcons != oldProfile.numHotseatIcons) {
298 changeFlags |= CHANGE_FLAG_GRID;
299 }
300
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800301 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
302 !iconShapePath.equals(oldProfile.iconShapePath)) {
303 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700304 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800305 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700306 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800307 }
308
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800309 apply(context, changeFlags);
310 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700311
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800312 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700313 // Create a new config monitor
314 mConfigMonitor.unregister();
315 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
316
317 for (OnIDPChangeListener listener : mChangeListeners) {
318 listener.onIdpChanged(changeFlags, this);
319 }
320 }
321
Sunny Goyal415f1732018-11-29 10:33:47 -0800322 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
323 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700324 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
325 final int depth = parser.getDepth();
326 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700327 while (((type = parser.next()) != XmlPullParser.END_TAG ||
328 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800329 if ((type == XmlPullParser.START_TAG)
330 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800331
332 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
333 final int displayDepth = parser.getDepth();
334 while (((type = parser.next()) != XmlPullParser.END_TAG ||
335 parser.getDepth() > displayDepth)
336 && type != XmlPullParser.END_DOCUMENT) {
337 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
338 parser.getName())) {
339 profiles.add(new DisplayOption(
340 gridOption, context, Xml.asAttributeSet(parser)));
341 }
342 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700343 }
344 }
345 } catch (IOException|XmlPullParserException e) {
346 throw new RuntimeException(e);
347 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800348
349 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
350 if (!TextUtils.isEmpty(gridName)) {
351 for (DisplayOption option : profiles) {
352 if (gridName.equals(option.grid.name)) {
353 filteredProfiles.add(option);
354 }
355 }
356 }
357 if (filteredProfiles.isEmpty()) {
358 // No grid found, use the default options
359 for (DisplayOption option : profiles) {
360 if (option.canBeDefault) {
361 filteredProfiles.add(option);
362 }
363 }
364 }
365 if (filteredProfiles.isEmpty()) {
366 throw new RuntimeException("No display option with canBeDefault=true");
367 }
368 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700369 }
370
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700371 private int getLauncherIconDensity(int requiredSize) {
372 // Densities typically defined by an app.
373 int[] densityBuckets = new int[] {
374 DisplayMetrics.DENSITY_LOW,
375 DisplayMetrics.DENSITY_MEDIUM,
376 DisplayMetrics.DENSITY_TV,
377 DisplayMetrics.DENSITY_HIGH,
378 DisplayMetrics.DENSITY_XHIGH,
379 DisplayMetrics.DENSITY_XXHIGH,
380 DisplayMetrics.DENSITY_XXXHIGH
381 };
382
383 int density = DisplayMetrics.DENSITY_XXXHIGH;
384 for (int i = densityBuckets.length - 1; i >= 0; i--) {
385 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
386 / DisplayMetrics.DENSITY_DEFAULT;
387 if (expectedSize >= requiredSize) {
388 density = densityBuckets[i];
389 }
390 }
391
392 return density;
393 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700394
Adam Cohen2e6da152015-05-06 11:42:25 -0700395 /**
396 * Apply any Partner customization grid overrides.
397 *
398 * Currently we support: all apps row / column count.
399 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700400 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
401 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700402 if (p != null) {
403 p.applyInvariantDeviceProfileOverrides(this, dm);
404 }
405 }
406
Sunny Goyal415f1732018-11-29 10:33:47 -0800407 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700408 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700409 }
410
Sunny Goyal415f1732018-11-29 10:33:47 -0800411 @VisibleForTesting
412 static DisplayOption invDistWeightedInterpolate(float width, float height,
413 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700414 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700415
Sunny Goyal415f1732018-11-29 10:33:47 -0800416 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700417 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
418 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700419 }
420
Sunny Goyal415f1732018-11-29 10:33:47 -0800421 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700422 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800423 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700424 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
425 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800426 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700427 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800428 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700429 }
430
Sunny Goyal27835952017-01-13 12:15:53 -0800431 public DeviceProfile getDeviceProfile(Context context) {
432 return context.getResources().getConfiguration().orientation
433 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
434 }
435
Sunny Goyal415f1732018-11-29 10:33:47 -0800436 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700437 float d = dist(x0, y0, x1, y1);
438 if (Float.compare(d, 0f) == 0) {
439 return Float.POSITIVE_INFINITY;
440 }
441 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
442 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700443
444 /**
445 * As a ratio of screen height, the total distance we want the parallax effect to span
446 * horizontally
447 */
448 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
449 float aspectRatio = width / (float) height;
450
451 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
452 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
453 // We will use these two data points to extrapolate how much the wallpaper parallax effect
454 // to span (ie travel) at any aspect ratio:
455
456 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
457 final float ASPECT_RATIO_PORTRAIT = 10/16f;
458 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
459 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
460
461 // To find out the desired width at different aspect ratios, we use the following two
462 // formulas, where the coefficient on x is the aspect ratio (width/height):
463 // (16/10)x + y = 1.5
464 // (10/16)x + y = 1.2
465 // We solve for x and y and end up with a final formula:
466 final float x =
467 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
468 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
469 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
470 return x * aspectRatio + y;
471 }
472
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700473 public interface OnIDPChangeListener {
474
475 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
476 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800477
478
Sunny Goyaleff44f32019-01-09 17:29:49 -0800479 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800480
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800481 public static final String TAG_NAME = "grid-option";
482
Sunny Goyaleff44f32019-01-09 17:29:49 -0800483 public final String name;
484 public final int numRows;
485 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800486
487 private final int numFolderRows;
488 private final int numFolderColumns;
489
490 private final int numHotseatIcons;
491
492 private final int defaultLayoutId;
493 private final int demoModeLayoutId;
494
Sunny Goyal5bc18462019-01-07 15:13:39 -0800495 private final SparseArray<TypedValue> extraAttrs;
496
Sunny Goyaleff44f32019-01-09 17:29:49 -0800497 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800498 TypedArray a = context.obtainStyledAttributes(
499 attrs, R.styleable.GridDisplayOption);
500 name = a.getString(R.styleable.GridDisplayOption_name);
501 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
502 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
503
504 defaultLayoutId = a.getResourceId(
505 R.styleable.GridDisplayOption_defaultLayoutId, 0);
506 demoModeLayoutId = a.getResourceId(
507 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
508 numHotseatIcons = a.getInt(
509 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
510 numFolderRows = a.getInt(
511 R.styleable.GridDisplayOption_numFolderRows, numRows);
512 numFolderColumns = a.getInt(
513 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
514 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800515
516 extraAttrs = Themes.createValueMap(context, attrs,
517 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800518 }
519 }
520
521 private static final class DisplayOption {
522 private final GridOption grid;
523
524 private final String name;
525 private final float minWidthDps;
526 private final float minHeightDps;
527 private final boolean canBeDefault;
528
529 private float iconSize;
530 private float landscapeIconSize;
531 private float iconTextSize;
532
533 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
534 this.grid = grid;
535
536 TypedArray a = context.obtainStyledAttributes(
537 attrs, R.styleable.ProfileDisplayOption);
538
539 name = a.getString(R.styleable.ProfileDisplayOption_name);
540 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
541 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
542 canBeDefault = a.getBoolean(
543 R.styleable.ProfileDisplayOption_canBeDefault, false);
544
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700545 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800546 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
547 iconSize);
548 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
549 a.recycle();
550 }
551
552 DisplayOption() {
553 grid = null;
554 name = null;
555 minWidthDps = 0;
556 minHeightDps = 0;
557 canBeDefault = false;
558 }
559
560 private DisplayOption multiply(float w) {
561 iconSize *= w;
562 landscapeIconSize *= w;
563 iconTextSize *= w;
564 return this;
565 }
566
567 private DisplayOption add(DisplayOption p) {
568 iconSize += p.iconSize;
569 landscapeIconSize += p.landscapeIconSize;
570 iconTextSize += p.iconTextSize;
571 return this;
572 }
573 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700574
575 private class OverlayMonitor extends BroadcastReceiver {
576
577 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
578
579 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700580 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700581 }
582
583 @Override
584 public void onReceive(Context context, Intent intent) {
585 onConfigChanged(context);
586 }
587 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700588}