blob: f08130314806e2f290ab2c26df4666673f9970f1 [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
Sunny Goyal87dc48b2018-10-12 11:42:33 -070019import static com.android.launcher3.config.FeatureFlags.APPLY_CONFIG_AT_RUNTIME;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080020import static com.android.launcher3.Utilities.getDevicePrefs;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070021
Sunny Goyalc6205602015-05-21 20:46:33 -070022import android.annotation.TargetApi;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070023import android.content.BroadcastReceiver;
Adam Cohen2e6da152015-05-06 11:42:25 -070024import android.content.Context;
Hyunyoung Songe11eb472019-03-19 15:05:21 -070025import android.content.Intent;
26import android.content.IntentFilter;
Sunny Goyal27835952017-01-13 12:15:53 -080027import android.content.res.Configuration;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080028import android.content.res.Resources;
Sunny Goyal819e1932016-07-07 16:43:58 -070029import android.content.res.TypedArray;
30import android.content.res.XmlResourceParser;
Adam Cohen2e6da152015-05-06 11:42:25 -070031import android.graphics.Point;
Sunny Goyal415f1732018-11-29 10:33:47 -080032import android.text.TextUtils;
33import android.util.AttributeSet;
Adam Cohen2e6da152015-05-06 11:42:25 -070034import android.util.DisplayMetrics;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070035import android.util.Log;
Sunny Goyal5bc18462019-01-07 15:13:39 -080036import android.util.SparseArray;
37import android.util.TypedValue;
Sunny Goyal819e1932016-07-07 16:43:58 -070038import android.util.Xml;
Adam Cohen2e6da152015-05-06 11:42:25 -070039import android.view.Display;
40import android.view.WindowManager;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070041
Sunny Goyal90e3fbc2019-01-23 16:42:43 -080042import com.android.launcher3.folder.FolderShape;
Sunny Goyald0e360a2018-06-29 14:40:18 -070043import com.android.launcher3.util.ConfigMonitor;
Sunny Goyal5bc18462019-01-07 15:13:39 -080044import com.android.launcher3.util.IntArray;
Sunny Goyald0e360a2018-06-29 14:40:18 -070045import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyal5bc18462019-01-07 15:13:39 -080046import com.android.launcher3.util.Themes;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070047
Sunny Goyal819e1932016-07-07 16:43:58 -070048import org.xmlpull.v1.XmlPullParser;
49import org.xmlpull.v1.XmlPullParserException;
50
51import java.io.IOException;
Adam Cohen2e6da152015-05-06 11:42:25 -070052import java.util.ArrayList;
Sunny Goyal6d55f662019-01-02 12:13:43 -080053import java.util.Collections;
Adam Cohen2e6da152015-05-06 11:42:25 -070054
Sunny Goyal5bc18462019-01-07 15:13:39 -080055import androidx.annotation.Nullable;
Sunny Goyald2303072018-08-14 15:21:45 -070056import androidx.annotation.VisibleForTesting;
57
Adam Cohen2e6da152015-05-06 11:42:25 -070058public class InvariantDeviceProfile {
Adam Cohen2e6da152015-05-06 11:42:25 -070059
Hyunyoung Songc55a3502018-12-04 15:43:16 -080060 public static final String TAG = "IDP";
Sunny Goyald0e360a2018-06-29 14:40:18 -070061 // We do not need any synchronization for this variable as its only written on UI thread.
62 public static final MainThreadInitializedObject<InvariantDeviceProfile> INSTANCE =
Sunny Goyal87dc48b2018-10-12 11:42:33 -070063 new MainThreadInitializedObject<>(InvariantDeviceProfile::new);
Adam Cohen2e6da152015-05-06 11:42:25 -070064
Hyunyoung Songc55a3502018-12-04 15:43:16 -080065 private static final String KEY_IDP_GRID_NAME = "idp_grid_name";
Sunny Goyal415f1732018-11-29 10:33:47 -080066
Sunny Goyal53d7ee42015-05-22 12:25:45 -070067 private static final float ICON_SIZE_DEFINED_IN_APP_DP = 48;
68
Sunny Goyal87dc48b2018-10-12 11:42:33 -070069 public static final int CHANGE_FLAG_GRID = 1 << 0;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080070 public static final int CHANGE_FLAG_ICON_PARAMS = 1 << 1;
71
72 public static final String KEY_ICON_PATH_REF = "pref_icon_shape_path";
Sunny Goyal87dc48b2018-10-12 11:42:33 -070073
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070074 // Constants that affects the interpolation curve between statically defined device profile
75 // buckets.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080076 private static final float KNEARESTNEIGHBOR = 3;
77 private static final float WEIGHT_POWER = 5;
Adam Cohen2e6da152015-05-06 11:42:25 -070078
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070079 // used to offset float not being able to express extremely small weights in extreme cases.
Hyunyoung Songc55a3502018-12-04 15:43:16 -080080 private static final float WEIGHT_EFFICIENT = 100000f;
81
82 private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier(
83 "config_icon_mask", "string", "android");
Adam Cohen2e6da152015-05-06 11:42:25 -070084
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070085 /**
86 * Number of icons per row and column in the workspace.
87 */
Adam Cohen2e6da152015-05-06 11:42:25 -070088 public int numRows;
89 public int numColumns;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -070090
91 /**
92 * Number of icons per row and column in the folder.
93 */
Adam Cohen2e6da152015-05-06 11:42:25 -070094 public int numFolderRows;
95 public int numFolderColumns;
Sunny Goyalfc218302015-09-17 14:59:10 -070096 public float iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080097 public String iconShapePath;
Jon Mirandab28c4fc2017-06-20 10:58:36 -070098 public float landscapeIconSize;
Sunny Goyalfc218302015-09-17 14:59:10 -070099 public int iconBitmapSize;
100 public int fillResIconDpi;
101 public float iconTextSize;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700102
Sunny Goyal5bc18462019-01-07 15:13:39 -0800103 private SparseArray<TypedValue> mExtraAttrs;
104
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700105 /**
106 * Number of icons inside the hotseat area.
107 */
Sunny Goyalf862a262015-12-14 14:27:38 -0800108 public int numHotseatIcons;
Adam Cohen27824492017-09-22 17:10:55 -0700109
Sunny Goyal415f1732018-11-29 10:33:47 -0800110 public int defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700111 int demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700112
cuijiaxingabda8d72017-03-20 09:51:36 -0700113 public DeviceProfile landscapeProfile;
114 public DeviceProfile portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700115
Sunny Goyal6f866092016-03-17 17:04:15 -0700116 public Point defaultWallpaperSize;
117
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700118 private final ArrayList<OnIDPChangeListener> mChangeListeners = new ArrayList<>();
119 private ConfigMonitor mConfigMonitor;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700120 private OverlayMonitor mOverlayMonitor;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700121
Sunny Goyalf633ef52018-03-13 09:57:05 -0700122 @VisibleForTesting
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700123 public InvariantDeviceProfile() {}
Adam Cohen2e6da152015-05-06 11:42:25 -0700124
Sunny Goyalf633ef52018-03-13 09:57:05 -0700125 private InvariantDeviceProfile(InvariantDeviceProfile p) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800126 numRows = p.numRows;
127 numColumns = p.numColumns;
128 numFolderRows = p.numFolderRows;
129 numFolderColumns = p.numFolderColumns;
130 iconSize = p.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800131 iconShapePath = p.iconShapePath;
Sunny Goyal415f1732018-11-29 10:33:47 -0800132 landscapeIconSize = p.landscapeIconSize;
133 iconTextSize = p.iconTextSize;
134 numHotseatIcons = p.numHotseatIcons;
135 defaultLayoutId = p.defaultLayoutId;
136 demoModeLayoutId = p.demoModeLayoutId;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800137 mExtraAttrs = p.mExtraAttrs;
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700138 mOverlayMonitor = p.mOverlayMonitor;
Adam Cohen2e6da152015-05-06 11:42:25 -0700139 }
140
Sunny Goyalbbf01842015-10-08 07:41:15 -0700141 @TargetApi(23)
Sunny Goyald0e360a2018-06-29 14:40:18 -0700142 private InvariantDeviceProfile(Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800143 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700144 mConfigMonitor = new ConfigMonitor(context,
145 APPLY_CONFIG_AT_RUNTIME.get() ? this::onConfigChanged : this::killProcess);
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700146 mOverlayMonitor = new OverlayMonitor(context);
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700147 }
148
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700149 /**
150 * This constructor should NOT have any monitors by design.
151 */
Sunny Goyaleff44f32019-01-09 17:29:49 -0800152 public InvariantDeviceProfile(Context context, String gridName) {
153 String newName = initGrid(context, gridName);
154 if (newName == null || !newName.equals(gridName)) {
155 throw new IllegalArgumentException("Unknown grid name");
156 }
157 }
158
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800159 /**
160 * Retrieve system defined or RRO overriden icon shape.
161 */
162 private static String getIconShapePath(Context context) {
163 if (CONFIG_ICON_MASK_RES_ID == 0) {
164 Log.e(TAG, "Icon mask res identifier failed to retrieve.");
165 return "";
166 }
167 return context.getResources().getString(CONFIG_ICON_MASK_RES_ID);
168 }
169
Sunny Goyaleff44f32019-01-09 17:29:49 -0800170 private String initGrid(Context context, String gridName) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700171 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
172 Display display = wm.getDefaultDisplay();
173 DisplayMetrics dm = new DisplayMetrics();
174 display.getMetrics(dm);
175
176 Point smallestSize = new Point();
177 Point largestSize = new Point();
178 display.getCurrentSizeRange(smallestSize, largestSize);
179
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
Sunny Goyal415f1732018-11-29 10:33:47 -0800182 float minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
183 float minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
184 // Sort the profiles based on the closeness to the device size
Sunny Goyal6d55f662019-01-02 12:13:43 -0800185 Collections.sort(allOptions, (a, b) ->
Sunny Goyal415f1732018-11-29 10:33:47 -0800186 Float.compare(dist(minWidthDps, minHeightDps, a.minWidthDps, a.minHeightDps),
187 dist(minWidthDps, minHeightDps, b.minWidthDps, b.minHeightDps)));
188 DisplayOption interpolatedDisplayOption =
189 invDistWeightedInterpolate(minWidthDps, minHeightDps, allOptions);
Adam Cohen2e6da152015-05-06 11:42:25 -0700190
Sunny Goyal415f1732018-11-29 10:33:47 -0800191 GridOption closestProfile = allOptions.get(0).grid;
Adam Cohen2e6da152015-05-06 11:42:25 -0700192 numRows = closestProfile.numRows;
193 numColumns = closestProfile.numColumns;
194 numHotseatIcons = closestProfile.numHotseatIcons;
Adam Cohen2e6da152015-05-06 11:42:25 -0700195 defaultLayoutId = closestProfile.defaultLayoutId;
Adam Cohen27824492017-09-22 17:10:55 -0700196 demoModeLayoutId = closestProfile.demoModeLayoutId;
Adam Cohen2e6da152015-05-06 11:42:25 -0700197 numFolderRows = closestProfile.numFolderRows;
198 numFolderColumns = closestProfile.numFolderColumns;
Sunny Goyal5bc18462019-01-07 15:13:39 -0800199 mExtraAttrs = closestProfile.extraAttrs;
200
Sunny Goyal415f1732018-11-29 10:33:47 -0800201 if (!closestProfile.name.equals(gridName)) {
202 Utilities.getPrefs(context).edit()
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800203 .putString(KEY_IDP_GRID_NAME, closestProfile.name).apply();
Sunny Goyal415f1732018-11-29 10:33:47 -0800204 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700205
Sunny Goyal415f1732018-11-29 10:33:47 -0800206 iconSize = interpolatedDisplayOption.iconSize;
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800207 iconShapePath = getIconShapePath(context);
Sunny Goyal415f1732018-11-29 10:33:47 -0800208 landscapeIconSize = interpolatedDisplayOption.landscapeIconSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700209 iconBitmapSize = Utilities.pxFromDp(iconSize, dm);
Sunny Goyal415f1732018-11-29 10:33:47 -0800210 iconTextSize = interpolatedDisplayOption.iconTextSize;
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700211 fillResIconDpi = getLauncherIconDensity(iconBitmapSize);
Adam Cohen2e6da152015-05-06 11:42:25 -0700212
213 // If the partner customization apk contains any grid overrides, apply them
214 // Supported overrides: numRows, numColumns, iconSize
215 applyPartnerDeviceProfileOverrides(context, dm);
Sunny Goyalc6205602015-05-21 20:46:33 -0700216
217 Point realSize = new Point();
218 display.getRealSize(realSize);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700219 // The real size never changes. smallSide and largeSide will remain the
Sunny Goyalc6205602015-05-21 20:46:33 -0700220 // same in any orientation.
221 int smallSide = Math.min(realSize.x, realSize.y);
222 int largeSide = Math.max(realSize.x, realSize.y);
223
224 landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800225 largeSide, smallSide, true /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyalc6205602015-05-21 20:46:33 -0700226 portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,
Sunny Goyald70e75a2018-02-22 10:07:32 -0800227 smallSide, largeSide, false /* isLandscape */, false /* isMultiWindowMode */);
Sunny Goyal6f866092016-03-17 17:04:15 -0700228
229 // We need to ensure that there is enough extra space in the wallpaper
230 // for the intended parallax effects
231 if (context.getResources().getConfiguration().smallestScreenWidthDp >= 720) {
232 defaultWallpaperSize = new Point(
233 (int) (largeSide * wallpaperTravelToScreenWidthRatio(largeSide, smallSide)),
234 largeSide);
235 } else {
236 defaultWallpaperSize = new Point(Math.max(smallSide * 2, largeSide), largeSide);
237 }
Sunny Goyaleff44f32019-01-09 17:29:49 -0800238 return closestProfile.name;
Adam Cohen2e6da152015-05-06 11:42:25 -0700239 }
240
Sunny Goyal5bc18462019-01-07 15:13:39 -0800241 @Nullable
242 public TypedValue getAttrValue(int attr) {
243 return mExtraAttrs == null ? null : mExtraAttrs.get(attr);
244 }
245
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700246 public void addOnChangeListener(OnIDPChangeListener listener) {
247 mChangeListeners.add(listener);
248 }
249
Hyunyoung Songb4d1ca42019-01-08 17:15:16 -0800250 public void removeOnChangeListener(OnIDPChangeListener listener) {
251 mChangeListeners.remove(listener);
252 }
253
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700254 private void killProcess(Context context) {
255 Log.e("ConfigMonitor", "restarting launcher");
256 android.os.Process.killProcess(android.os.Process.myPid());
257 }
258
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800259 public void verifyConfigChangedInBackground(final Context context) {
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800260 String savedIconMaskPath = getDevicePrefs(context).getString(KEY_ICON_PATH_REF, "");
261 // Good place to check if grid size changed in themepicker when launcher was dead.
262 if (savedIconMaskPath.isEmpty()) {
263 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
264 .apply();
265 } else if (!savedIconMaskPath.equals(getIconShapePath(context))) {
266 getDevicePrefs(context).edit().putString(KEY_ICON_PATH_REF, getIconShapePath(context))
267 .apply();
268 apply(context, CHANGE_FLAG_ICON_PARAMS);
269 }
270 }
271
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800272 public void setCurrentGrid(Context context, String gridName) {
273 Context appContext = context.getApplicationContext();
274 Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
275 new MainThreadExecutor().execute(() -> onConfigChanged(appContext));
276 }
277
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700278 private void onConfigChanged(Context context) {
279 // Config changes, what shall we do?
280 InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this);
281
282 // Re-init grid
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800283 initGrid(context, Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null));
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700284
285 int changeFlags = 0;
286 if (numRows != oldProfile.numRows ||
287 numColumns != oldProfile.numColumns ||
288 numFolderColumns != oldProfile.numFolderColumns ||
289 numFolderRows != oldProfile.numFolderRows ||
290 numHotseatIcons != oldProfile.numHotseatIcons) {
291 changeFlags |= CHANGE_FLAG_GRID;
292 }
293
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800294 if (iconSize != oldProfile.iconSize || iconBitmapSize != oldProfile.iconBitmapSize ||
295 !iconShapePath.equals(oldProfile.iconShapePath)) {
296 changeFlags |= CHANGE_FLAG_ICON_PARAMS;
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700297 }
Sunny Goyal90e3fbc2019-01-23 16:42:43 -0800298 if (!iconShapePath.equals(oldProfile.iconShapePath)) {
299 FolderShape.init(context);
300 }
301
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800302 apply(context, changeFlags);
303 }
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700304
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800305 private void apply(Context context, int changeFlags) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700306 // Create a new config monitor
307 mConfigMonitor.unregister();
308 mConfigMonitor = new ConfigMonitor(context, this::onConfigChanged);
309
310 for (OnIDPChangeListener listener : mChangeListeners) {
311 listener.onIdpChanged(changeFlags, this);
312 }
313 }
314
Sunny Goyal415f1732018-11-29 10:33:47 -0800315 static ArrayList<DisplayOption> getPredefinedDeviceProfiles(Context context, String gridName) {
316 ArrayList<DisplayOption> profiles = new ArrayList<>();
Sunny Goyal819e1932016-07-07 16:43:58 -0700317 try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
318 final int depth = parser.getDepth();
319 int type;
Sunny Goyal819e1932016-07-07 16:43:58 -0700320 while (((type = parser.next()) != XmlPullParser.END_TAG ||
321 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800322 if ((type == XmlPullParser.START_TAG)
323 && GridOption.TAG_NAME.equals(parser.getName())) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800324
325 GridOption gridOption = new GridOption(context, Xml.asAttributeSet(parser));
326 final int displayDepth = parser.getDepth();
327 while (((type = parser.next()) != XmlPullParser.END_TAG ||
328 parser.getDepth() > displayDepth)
329 && type != XmlPullParser.END_DOCUMENT) {
330 if ((type == XmlPullParser.START_TAG) && "display-option".equals(
331 parser.getName())) {
332 profiles.add(new DisplayOption(
333 gridOption, context, Xml.asAttributeSet(parser)));
334 }
335 }
Sunny Goyal819e1932016-07-07 16:43:58 -0700336 }
337 }
338 } catch (IOException|XmlPullParserException e) {
339 throw new RuntimeException(e);
340 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800341
342 ArrayList<DisplayOption> filteredProfiles = new ArrayList<>();
343 if (!TextUtils.isEmpty(gridName)) {
344 for (DisplayOption option : profiles) {
345 if (gridName.equals(option.grid.name)) {
346 filteredProfiles.add(option);
347 }
348 }
349 }
350 if (filteredProfiles.isEmpty()) {
351 // No grid found, use the default options
352 for (DisplayOption option : profiles) {
353 if (option.canBeDefault) {
354 filteredProfiles.add(option);
355 }
356 }
357 }
358 if (filteredProfiles.isEmpty()) {
359 throw new RuntimeException("No display option with canBeDefault=true");
360 }
361 return filteredProfiles;
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700362 }
363
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700364 private int getLauncherIconDensity(int requiredSize) {
365 // Densities typically defined by an app.
366 int[] densityBuckets = new int[] {
367 DisplayMetrics.DENSITY_LOW,
368 DisplayMetrics.DENSITY_MEDIUM,
369 DisplayMetrics.DENSITY_TV,
370 DisplayMetrics.DENSITY_HIGH,
371 DisplayMetrics.DENSITY_XHIGH,
372 DisplayMetrics.DENSITY_XXHIGH,
373 DisplayMetrics.DENSITY_XXXHIGH
374 };
375
376 int density = DisplayMetrics.DENSITY_XXXHIGH;
377 for (int i = densityBuckets.length - 1; i >= 0; i--) {
378 float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
379 / DisplayMetrics.DENSITY_DEFAULT;
380 if (expectedSize >= requiredSize) {
381 density = densityBuckets[i];
382 }
383 }
384
385 return density;
386 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700387
Adam Cohen2e6da152015-05-06 11:42:25 -0700388 /**
389 * Apply any Partner customization grid overrides.
390 *
391 * Currently we support: all apps row / column count.
392 */
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700393 private void applyPartnerDeviceProfileOverrides(Context context, DisplayMetrics dm) {
394 Partner p = Partner.get(context.getPackageManager());
Adam Cohen2e6da152015-05-06 11:42:25 -0700395 if (p != null) {
396 p.applyInvariantDeviceProfileOverrides(this, dm);
397 }
398 }
399
Sunny Goyal415f1732018-11-29 10:33:47 -0800400 private static float dist(float x0, float y0, float x1, float y1) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700401 return (float) Math.hypot(x1 - x0, y1 - y0);
Adam Cohen2e6da152015-05-06 11:42:25 -0700402 }
403
Sunny Goyal415f1732018-11-29 10:33:47 -0800404 @VisibleForTesting
405 static DisplayOption invDistWeightedInterpolate(float width, float height,
406 ArrayList<DisplayOption> points) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700407 float weights = 0;
Adam Cohen2e6da152015-05-06 11:42:25 -0700408
Sunny Goyal415f1732018-11-29 10:33:47 -0800409 DisplayOption p = points.get(0);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700410 if (dist(width, height, p.minWidthDps, p.minHeightDps) == 0) {
411 return p;
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 }
413
Sunny Goyal415f1732018-11-29 10:33:47 -0800414 DisplayOption out = new DisplayOption();
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700415 for (int i = 0; i < points.size() && i < KNEARESTNEIGHBOR; ++i) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800416 p = points.get(i);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700417 float w = weight(width, height, p.minWidthDps, p.minHeightDps, WEIGHT_POWER);
418 weights += w;
Sunny Goyal415f1732018-11-29 10:33:47 -0800419 out.add(new DisplayOption().add(p).multiply(w));
Adam Cohen2e6da152015-05-06 11:42:25 -0700420 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800421 return out.multiply(1.0f / weights);
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700422 }
423
Sunny Goyal27835952017-01-13 12:15:53 -0800424 public DeviceProfile getDeviceProfile(Context context) {
425 return context.getResources().getConfiguration().orientation
426 == Configuration.ORIENTATION_LANDSCAPE ? landscapeProfile : portraitProfile;
427 }
428
Sunny Goyal415f1732018-11-29 10:33:47 -0800429 private static float weight(float x0, float y0, float x1, float y1, float pow) {
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700430 float d = dist(x0, y0, x1, y1);
431 if (Float.compare(d, 0f) == 0) {
432 return Float.POSITIVE_INFINITY;
433 }
434 return (float) (WEIGHT_EFFICIENT / Math.pow(d, pow));
435 }
Sunny Goyal6f866092016-03-17 17:04:15 -0700436
437 /**
438 * As a ratio of screen height, the total distance we want the parallax effect to span
439 * horizontally
440 */
441 private static float wallpaperTravelToScreenWidthRatio(int width, int height) {
442 float aspectRatio = width / (float) height;
443
444 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
445 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
446 // We will use these two data points to extrapolate how much the wallpaper parallax effect
447 // to span (ie travel) at any aspect ratio:
448
449 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
450 final float ASPECT_RATIO_PORTRAIT = 10/16f;
451 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
452 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
453
454 // To find out the desired width at different aspect ratios, we use the following two
455 // formulas, where the coefficient on x is the aspect ratio (width/height):
456 // (16/10)x + y = 1.5
457 // (10/16)x + y = 1.2
458 // We solve for x and y and end up with a final formula:
459 final float x =
460 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
461 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
462 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
463 return x * aspectRatio + y;
464 }
465
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700466 public interface OnIDPChangeListener {
467
468 void onIdpChanged(int changeFlags, InvariantDeviceProfile profile);
469 }
Sunny Goyal415f1732018-11-29 10:33:47 -0800470
471
Sunny Goyaleff44f32019-01-09 17:29:49 -0800472 public static final class GridOption {
Sunny Goyal415f1732018-11-29 10:33:47 -0800473
Sunny Goyal7d892ff2019-01-11 15:08:44 -0800474 public static final String TAG_NAME = "grid-option";
475
Sunny Goyaleff44f32019-01-09 17:29:49 -0800476 public final String name;
477 public final int numRows;
478 public final int numColumns;
Sunny Goyal415f1732018-11-29 10:33:47 -0800479
480 private final int numFolderRows;
481 private final int numFolderColumns;
482
483 private final int numHotseatIcons;
484
485 private final int defaultLayoutId;
486 private final int demoModeLayoutId;
487
Sunny Goyal5bc18462019-01-07 15:13:39 -0800488 private final SparseArray<TypedValue> extraAttrs;
489
Sunny Goyaleff44f32019-01-09 17:29:49 -0800490 public GridOption(Context context, AttributeSet attrs) {
Sunny Goyal415f1732018-11-29 10:33:47 -0800491 TypedArray a = context.obtainStyledAttributes(
492 attrs, R.styleable.GridDisplayOption);
493 name = a.getString(R.styleable.GridDisplayOption_name);
494 numRows = a.getInt(R.styleable.GridDisplayOption_numRows, 0);
495 numColumns = a.getInt(R.styleable.GridDisplayOption_numColumns, 0);
496
497 defaultLayoutId = a.getResourceId(
498 R.styleable.GridDisplayOption_defaultLayoutId, 0);
499 demoModeLayoutId = a.getResourceId(
500 R.styleable.GridDisplayOption_demoModeLayoutId, defaultLayoutId);
501 numHotseatIcons = a.getInt(
502 R.styleable.GridDisplayOption_numHotseatIcons, numColumns);
503 numFolderRows = a.getInt(
504 R.styleable.GridDisplayOption_numFolderRows, numRows);
505 numFolderColumns = a.getInt(
506 R.styleable.GridDisplayOption_numFolderColumns, numColumns);
507 a.recycle();
Sunny Goyal5bc18462019-01-07 15:13:39 -0800508
509 extraAttrs = Themes.createValueMap(context, attrs,
510 IntArray.wrap(R.styleable.GridDisplayOption));
Sunny Goyal415f1732018-11-29 10:33:47 -0800511 }
512 }
513
514 private static final class DisplayOption {
515 private final GridOption grid;
516
517 private final String name;
518 private final float minWidthDps;
519 private final float minHeightDps;
520 private final boolean canBeDefault;
521
522 private float iconSize;
523 private float landscapeIconSize;
524 private float iconTextSize;
525
526 DisplayOption(GridOption grid, Context context, AttributeSet attrs) {
527 this.grid = grid;
528
529 TypedArray a = context.obtainStyledAttributes(
530 attrs, R.styleable.ProfileDisplayOption);
531
532 name = a.getString(R.styleable.ProfileDisplayOption_name);
533 minWidthDps = a.getFloat(R.styleable.ProfileDisplayOption_minWidthDps, 0);
534 minHeightDps = a.getFloat(R.styleable.ProfileDisplayOption_minHeightDps, 0);
535 canBeDefault = a.getBoolean(
536 R.styleable.ProfileDisplayOption_canBeDefault, false);
537
538 iconSize = a.getFloat(R.styleable.ProfileDisplayOption_iconSize, 0);
539 landscapeIconSize = a.getFloat(R.styleable.ProfileDisplayOption_landscapeIconSize,
540 iconSize);
541 iconTextSize = a.getFloat(R.styleable.ProfileDisplayOption_iconTextSize, 0);
542 a.recycle();
543 }
544
545 DisplayOption() {
546 grid = null;
547 name = null;
548 minWidthDps = 0;
549 minHeightDps = 0;
550 canBeDefault = false;
551 }
552
553 private DisplayOption multiply(float w) {
554 iconSize *= w;
555 landscapeIconSize *= w;
556 iconTextSize *= w;
557 return this;
558 }
559
560 private DisplayOption add(DisplayOption p) {
561 iconSize += p.iconSize;
562 landscapeIconSize += p.landscapeIconSize;
563 iconTextSize += p.iconTextSize;
564 return this;
565 }
566 }
Hyunyoung Songe11eb472019-03-19 15:05:21 -0700567
568 private class OverlayMonitor extends BroadcastReceiver {
569
570 private final String ACTION_OVERLAY_CHANGED = "android.intent.action.OVERLAY_CHANGED";
571
572 OverlayMonitor(Context context) {
573 IntentFilter filter = new IntentFilter(ACTION_OVERLAY_CHANGED);
574 filter.addDataScheme("package");
575 context.registerReceiver(this, filter);
576 }
577
578 @Override
579 public void onReceive(Context context, Intent intent) {
580 onConfigChanged(context);
581 }
582 }
Hyunyoung Song35c3c7f2015-05-28 15:33:40 -0700583}