blob: bde87cb5056d84d2d24302c1817eecd70e10f044 [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;
21import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070022
Sunny Goyalc6205602015-05-21 20:46:33 -070023import android.annotation.TargetApi;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070024import android.appwidget.AppWidgetHostView;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070025import android.content.BroadcastReceiver;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070026import android.content.ComponentName;
Adam Cohen2e6da152015-05-06 11:42:25 -070027import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070028import android.content.Intent;
Sunny Goyal27835952017-01-13 12:15:53 -080029import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080030import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070031import android.content.res.TypedArray;
32import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070033import android.graphics.Point;
Sunny Goyal58fa4b62019-03-22 16:23:25 -070034import android.graphics.Rect;
Sunny Goyal415f1732018-11-29 10:33:47 -080035import android.text.TextUtils;
36import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070037import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070038import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080039import android.util.SparseArray;
40import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070041import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070042import android.view.Display;
43import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070044
Sunny Goyal905262c2019-05-03 16:50:43 -070045import com.android.launcher3.graphics.IconShape;
Sunny Goyald0e360a2018-06-29 14:40:18 -070046import com.android.launcher3.util.ConfigMonitor;
Sunny Goyal5bc18462019-01-07 15:13:39 -080047import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070048import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080049import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070050
Sunny Goyal819e1932016-07-07 16:43:58 -070051import org.xmlpull.v1.XmlPullParser;
52import org.xmlpull.v1.XmlPullParserException;
53
54import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070055import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080056import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070057
Sunny Goyal5bc18462019-01-07 15:13:39 -080058import androidx.annotation.Nullable;
Sunny Goyald2303072018-08-14 15:21:45 -070059import androidx.annotation.VisibleForTesting;
60
Adam Cohen2e6da152015-05-06 11:42:25 -070061public 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) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700175 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
176 Display display = wm.getDefaultDisplay();
177 DisplayMetrics dm = new DisplayMetrics();
178 display.getMetrics(dm);
179
180 Point smallestSize = new Point();
181 Point largestSize = new Point();
182 display.getCurrentSizeRange(smallestSize, largestSize);
183
Sunny Goyal415f1732018-11-29 10:33:47 -0800184 ArrayList<DisplayOption> allOptions = getPredefinedDeviceProfiles(context, gridName);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700185 // This guarantees that width < height
Sunny Goyal415f1732018-11-29 10:33:47 -0800186 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
187 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
188 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800189 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800190 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
191 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
192 DisplayOption interpolatedDisplayOption =
193 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700194
Sunny Goyal415f1732018-11-29 10:33:47 -0800195 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700196 numRows = closestProfile.numRows;
197 numColumns = closestProfile.numColumns;
198 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700199 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700200 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700201 numFolderRows = closestProfile.numFolderRows;
202 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800203 mExtraAttrs = closestProfile.extraAttrs;
204
Sunny Goyal415f1732018-11-29 10:33:47 -0800205 if (!closestProfile.name.equals(gridName)) {
206 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800207 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800208 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700209
Sunny Goyal415f1732018-11-29 10:33:47 -0800210 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800211 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800212 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
vadimt1b383af2019-05-08 15:29:37 -0700213 iconBitmapSize = ResourceUtils.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800214 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700215 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700216
217 // If the partner customization apk contains any grid overrides, apply them
218 // Supported overrides: numRows, numColumns, iconSize
219 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700220
221 Point realSize = new Point();
222 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700223 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700224 // same in any orientation.
225 int smallSide = Math.min(realSize.x, realSize.y);
226 int largeSide = Math.max(realSize.x, realSize.y);
227
228 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800229 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700230 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800231 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700232
233 // We need to ensure that there is enough extra space in the wallpaper
234 // for the intended parallax effects
235 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
236 defaultWallpaperSize = new Point(
237 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
238 largeSide);
239 } else {
240 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
241 }
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700242
243 ComponentName cn = new ComponentName(context.getPackageName(), getClass().getName());
244 defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
245
Sunny Goyaleff44f32019-01-09 17:29:49 -0800246 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700247 }
248
Sunny Goyal5bc18462019-01-07 15:13:39 -0800249 @Nullable
250 public TypedValue getAttrValue(int attr) {
251 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
252 }
253
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700254 public void addOnChangeListener(OnIDPChangeListener listener) {
255 mChangeListeners.add(listener);
256 }
257
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800258 public void removeOnChangeListener(OnIDPChangeListener listener) {
259 mChangeListeners.remove(listener);
260 }
261
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700262 private void killProcess(Context context) {
263 Log.e("ConfigMonitor", "restarting launcher");
264 android.os.Process.killProcess(android.os.Process.myPid());
265 }
266
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800267 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800268 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
269 // Good place to check if grid size changed in themepicker when launcher was dead.
270 if (savedIconMaskPath.isEmpty()) {
271 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
272 .apply();
273 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
274 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
275 .apply();
276 apply(context, CHANGE_FLAG_ICON_PARAMS);
277 }
278 }
279
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800280 public void setCurrentGrid(Context context, String gridName) {
281 Context appContext = context.getApplicationContext();
282 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
283 new MainThreadExecutor().execute(() -> onConfigChanged(appContext));
284 }
285
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700286 private void onConfigChanged(Context context) {
287 // Config changes, what shall we do?
288 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
289
290 // Re-init grid
Jon Mirandac0f1f4f2019-05-28 15:49:09 -0700291 // TODO(b/131867841): We pass in null here so that we can calculate the closest profile
292 // without the bias of the grid name.
293 initGrid(context, null);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700294
295 int changeFlags = 0;
296 if (numRows != oldProfile.numRows ||
297 numColumns != oldProfile.numColumns ||
298 numFolderColumns != oldProfile.numFolderColumns ||
299 numFolderRows != oldProfile.numFolderRows ||
300 numHotseatIcons != oldProfile.numHotseatIcons) {
301 changeFlags |= CHANGE_FLAG_GRID;
302 }
303
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800304 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
305 !iconShapePath.equals(oldProfile.iconShapePath)) {
306 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700307 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800308 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
Sunny Goyal905262c2019-05-03 16:50:43 -0700309 IconShape.init(context);
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800310 }
311
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800312 apply(context, changeFlags);
313 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700314
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800315 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700316 // Create a new config monitor
317 mConfigMonitor.unregister();
318 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
319
320 for (OnIDPChangeListener listener : mChangeListeners) {
321 listener.onIdpChanged(changeFlags, this);
322 }
323 }
324
Sunny Goyal415f1732018-11-29 10:33:47 -0800325 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
326 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700327 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
328 final int depth = parser.getDepth();
329 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700330 while (((type = parser.next()) != XmlPullParser.END_TAG ||
331 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800332 if ((type == XmlPullParser.START_TAG)
333 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800334
335 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
336 final int displayDepth = parser.getDepth();
337 while (((type = parser.next()) != XmlPullParser.END_TAG ||
338 parser.getDepth() > displayDepth)
339 && type != XmlPullParser.END_DOCUMENT) {
340 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
341 parser.getName())) {
342 profiles.add(new DisplayOption(
343 gridOption, context, Xml.asAttributeSet(parser)));
344 }
345 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700346 }
347 }
348 } catch (IOException|XmlPullParserException e) {
349 throw new RuntimeException(e);
350 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800351
352 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
353 if (!TextUtils.isEmpty(gridName)) {
354 for (DisplayOption option : profiles) {
355 if (gridName.equals(option.grid.name)) {
356 filteredProfiles.add(option);
357 }
358 }
359 }
360 if (filteredProfiles.isEmpty()) {
361 // No grid found, use the default options
362 for (DisplayOption option : profiles) {
363 if (option.canBeDefault) {
364 filteredProfiles.add(option);
365 }
366 }
367 }
368 if (filteredProfiles.isEmpty()) {
369 throw new RuntimeException("No display option with canBeDefault=true");
370 }
371 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700372 }
373
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700374 private int getLauncherIconDensity(int requiredSize) {
375 // Densities typically defined by an app.
376 int[] densityBuckets = new int[] {
377 DisplayMetrics.DENSITY_LOW,
378 DisplayMetrics.DENSITY_MEDIUM,
379 DisplayMetrics.DENSITY_TV,
380 DisplayMetrics.DENSITY_HIGH,
381 DisplayMetrics.DENSITY_XHIGH,
382 DisplayMetrics.DENSITY_XXHIGH,
383 DisplayMetrics.DENSITY_XXXHIGH
384 };
385
386 int density = DisplayMetrics.DENSITY_XXXHIGH;
387 for (int i = densityBuckets.length - 1; i >= 0; i--) {
388 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
389 / DisplayMetrics.DENSITY_DEFAULT;
390 if (expectedSize >= requiredSize) {
391 density = densityBuckets[i];
392 }
393 }
394
395 return density;
396 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700397
Adam Cohen2e6da152015-05-06 11:42:25 -0700398 /**
399 * Apply any Partner customization grid overrides.
400 *
401 * Currently we support: all apps row / column count.
402 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700403 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
404 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700405 if (p != null) {
406 p.applyInvariantDeviceProfileOverrides(this, dm);
407 }
408 }
409
Sunny Goyal415f1732018-11-29 10:33:47 -0800410 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700411 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 }
413
Sunny Goyal415f1732018-11-29 10:33:47 -0800414 @VisibleForTesting
415 static DisplayOption invDistWeightedInterpolate(float width, float height,
416 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700417 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700418
Sunny Goyal415f1732018-11-29 10:33:47 -0800419 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700420 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
421 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700422 }
423
Sunny Goyal415f1732018-11-29 10:33:47 -0800424 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700425 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800426 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700427 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
428 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800429 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700430 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800431 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700432 }
433
Sunny Goyal27835952017-01-13 12:15:53 -0800434 public DeviceProfile getDeviceProfile(Context context) {
435 return context.getResources().getConfiguration().orientation
436 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
437 }
438
Sunny Goyal415f1732018-11-29 10:33:47 -0800439 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700440 float d = dist(x0, y0, x1, y1);
441 if (Float.compare(d, 0f) == 0) {
442 return Float.POSITIVE_INFINITY;
443 }
444 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
445 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700446
447 /**
448 * As a ratio of screen height, the total distance we want the parallax effect to span
449 * horizontally
450 */
451 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
452 float aspectRatio = width / (float) height;
453
454 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
455 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
456 // We will use these two data points to extrapolate how much the wallpaper parallax effect
457 // to span (ie travel) at any aspect ratio:
458
459 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
460 final float ASPECT_RATIO_PORTRAIT = 10/16f;
461 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
462 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
463
464 // To find out the desired width at different aspect ratios, we use the following two
465 // formulas, where the coefficient on x is the aspect ratio (width/height):
466 // (16/10)x + y = 1.5
467 // (10/16)x + y = 1.2
468 // We solve for x and y and end up with a final formula:
469 final float x =
470 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
471 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
472 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
473 return x * aspectRatio + y;
474 }
475
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700476 public interface OnIDPChangeListener {
477
478 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
479 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800480
481
Sunny Goyaleff44f32019-01-09 17:29:49 -0800482 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800483
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800484 public static final String TAG_NAME = "grid-option";
485
Sunny Goyaleff44f32019-01-09 17:29:49 -0800486 public final String name;
487 public final int numRows;
488 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800489
490 private final int numFolderRows;
491 private final int numFolderColumns;
492
493 private final int numHotseatIcons;
494
495 private final int defaultLayoutId;
496 private final int demoModeLayoutId;
497
Sunny Goyal5bc18462019-01-07 15:13:39 -0800498 private final SparseArray<TypedValue> extraAttrs;
499
Sunny Goyaleff44f32019-01-09 17:29:49 -0800500 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800501 TypedArray a = context.obtainStyledAttributes(
502 attrs, R.styleable.GridDisplayOption);
503 name = a.getString(R.styleable.GridDisplayOption_name);
504 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
505 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
506
507 defaultLayoutId = a.getResourceId(
508 R.styleable.GridDisplayOption_defaultLayoutId, 0);
509 demoModeLayoutId = a.getResourceId(
510 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
511 numHotseatIcons = a.getInt(
512 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
513 numFolderRows = a.getInt(
514 R.styleable.GridDisplayOption_numFolderRows, numRows);
515 numFolderColumns = a.getInt(
516 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
517 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800518
519 extraAttrs = Themes.createValueMap(context, attrs,
520 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800521 }
522 }
523
524 private static final class DisplayOption {
525 private final GridOption grid;
526
527 private final String name;
528 private final float minWidthDps;
529 private final float minHeightDps;
530 private final boolean canBeDefault;
531
532 private float iconSize;
533 private float landscapeIconSize;
534 private float iconTextSize;
535
536 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
537 this.grid = grid;
538
539 TypedArray a = context.obtainStyledAttributes(
540 attrs, R.styleable.ProfileDisplayOption);
541
542 name = a.getString(R.styleable.ProfileDisplayOption_name);
543 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
544 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
545 canBeDefault = a.getBoolean(
546 R.styleable.ProfileDisplayOption_canBeDefault, false);
547
Ryan Mitchell01b8b682019-03-28 17:01:07 -0700548 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconImageSize, 0);
Sunny Goyal415f1732018-11-29 10:33:47 -0800549 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
550 iconSize);
551 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
552 a.recycle();
553 }
554
555 DisplayOption() {
556 grid = null;
557 name = null;
558 minWidthDps = 0;
559 minHeightDps = 0;
560 canBeDefault = false;
561 }
562
563 private DisplayOption multiply(float w) {
564 iconSize *= w;
565 landscapeIconSize *= w;
566 iconTextSize *= w;
567 return this;
568 }
569
570 private DisplayOption add(DisplayOption p) {
571 iconSize += p.iconSize;
572 landscapeIconSize += p.landscapeIconSize;
573 iconTextSize += p.iconTextSize;
574 return this;
575 }
576 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700577
578 private class OverlayMonitor extends BroadcastReceiver {
579
580 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
581
582 OverlayMonitor(Context context) {
Sunny Goyal8b0cb412019-04-22 09:01:26 -0700583 context.registerReceiver(this, getPackageFilter("android", ACTION_OVERLAY_CHANGED));
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700584 }
585
586 @Override
587 public void onReceive(Context context, Intent intent) {
588 onConfigChanged(context);
589 }
590 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700591}