Merge "Relaxed WallpaperManager API assertion" into sc-dev

This commit is contained in:
Charles Chen
2021-03-10 08:17:22 +00:00
committed by Android (Google) Code Review
3 changed files with 13 additions and 19 deletions

View File

@@ -1995,7 +1995,7 @@ class ContextImpl extends Context {
final String errorMessage = "Tried to access visual service " final String errorMessage = "Tried to access visual service "
+ SystemServiceRegistry.getSystemServiceClassName(name) + SystemServiceRegistry.getSystemServiceClassName(name)
+ " from a non-visual Context:" + getOuterContext(); + " from a non-visual Context:" + getOuterContext();
final String message = "Visual services, such as WindowManager, WallpaperService " final String message = "Visual services, such as WindowManager"
+ "or LayoutInflater should be accessed from Activity or other visual " + "or LayoutInflater should be accessed from Activity or other visual "
+ "Context. Use an Activity or a Context created with " + "Context. Use an Activity or a Context created with "
+ "Context#createWindowContext(int, Bundle), which are adjusted to " + "Context#createWindowContext(int, Bundle), which are adjusted to "

View File

@@ -650,7 +650,6 @@ public class WallpaperManager {
*/ */
@RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
public Drawable getDrawable() { public Drawable getDrawable() {
assertUiContext("getDrawable");
final ColorManagementProxy cmProxy = getColorManagementProxy(); final ColorManagementProxy cmProxy = getColorManagementProxy();
Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, cmProxy); Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, cmProxy);
if (bm != null) { if (bm != null) {
@@ -718,7 +717,6 @@ public class WallpaperManager {
*/ */
public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) { float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
assertUiContext("getBuiltInDrawable");
if (sGlobals.mService == null) { if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running"); Log.w(TAG, "WallpaperService not running");
throw new RuntimeException(new DeadSystemException()); throw new RuntimeException(new DeadSystemException());
@@ -884,7 +882,6 @@ public class WallpaperManager {
* null pointer if these is none. * null pointer if these is none.
*/ */
public Drawable peekDrawable() { public Drawable peekDrawable() {
assertUiContext("peekDrawable");
final ColorManagementProxy cmProxy = getColorManagementProxy(); final ColorManagementProxy cmProxy = getColorManagementProxy();
Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy); Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy);
if (bm != null) { if (bm != null) {
@@ -927,7 +924,6 @@ public class WallpaperManager {
*/ */
@RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
public Drawable peekFastDrawable() { public Drawable peekFastDrawable() {
assertUiContext("peekFastDrawable");
final ColorManagementProxy cmProxy = getColorManagementProxy(); final ColorManagementProxy cmProxy = getColorManagementProxy();
Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy); Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy);
if (bm != null) { if (bm != null) {
@@ -1084,6 +1080,7 @@ public class WallpaperManager {
return getWallpaperColors(which, mContext.getUserId()); return getWallpaperColors(which, mContext.getUserId());
} }
// TODO(b/181083333): add multiple root display area support on this API.
/** /**
* Get the primary colors of the wallpaper configured in the given user. * Get the primary colors of the wallpaper configured in the given user.
* @param which wallpaper type. Must be either {@link #FLAG_SYSTEM} or * @param which wallpaper type. Must be either {@link #FLAG_SYSTEM} or
@@ -1094,7 +1091,7 @@ public class WallpaperManager {
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public @Nullable WallpaperColors getWallpaperColors(int which, int userId) { public @Nullable WallpaperColors getWallpaperColors(int which, int userId) {
assertUiContext("getWallpaperColors"); StrictMode.assertUiContext(mContext, "getWallpaperColors");
return sGlobals.getWallpaperColors(which, userId, mContext.getDisplayId()); return sGlobals.getWallpaperColors(which, userId, mContext.getDisplayId());
} }
@@ -1333,7 +1330,6 @@ public class WallpaperManager {
@RequiresPermission(android.Manifest.permission.SET_WALLPAPER) @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
public int setResource(@RawRes int resid, @SetWallpaperFlags int which) public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
throws IOException { throws IOException {
assertUiContext("setResource");
if (sGlobals.mService == null) { if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running"); Log.w(TAG, "WallpaperService not running");
throw new RuntimeException(new DeadSystemException()); throw new RuntimeException(new DeadSystemException());
@@ -1637,6 +1633,7 @@ public class WallpaperManager {
} }
} }
// TODO(b/181083333): add multiple root display area support on this API.
/** /**
* Returns the desired minimum width for the wallpaper. Callers of * Returns the desired minimum width for the wallpaper. Callers of
* {@link #setBitmap(android.graphics.Bitmap)} or * {@link #setBitmap(android.graphics.Bitmap)} or
@@ -1654,7 +1651,7 @@ public class WallpaperManager {
* @see #getDesiredMinimumHeight() * @see #getDesiredMinimumHeight()
*/ */
public int getDesiredMinimumWidth() { public int getDesiredMinimumWidth() {
assertUiContext("getDesiredMinimumWidth"); StrictMode.assertUiContext(mContext, "getDesiredMinimumWidth");
if (sGlobals.mService == null) { if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running"); Log.w(TAG, "WallpaperService not running");
throw new RuntimeException(new DeadSystemException()); throw new RuntimeException(new DeadSystemException());
@@ -1666,6 +1663,7 @@ public class WallpaperManager {
} }
} }
// TODO(b/181083333): add multiple root display area support on this API.
/** /**
* Returns the desired minimum height for the wallpaper. Callers of * Returns the desired minimum height for the wallpaper. Callers of
* {@link #setBitmap(android.graphics.Bitmap)} or * {@link #setBitmap(android.graphics.Bitmap)} or
@@ -1683,7 +1681,7 @@ public class WallpaperManager {
* @see #getDesiredMinimumWidth() * @see #getDesiredMinimumWidth()
*/ */
public int getDesiredMinimumHeight() { public int getDesiredMinimumHeight() {
assertUiContext("getDesiredMinimumHeight"); StrictMode.assertUiContext(mContext, "getDesiredMinimumHeight");
if (sGlobals.mService == null) { if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running"); Log.w(TAG, "WallpaperService not running");
throw new RuntimeException(new DeadSystemException()); throw new RuntimeException(new DeadSystemException());
@@ -1695,6 +1693,7 @@ public class WallpaperManager {
} }
} }
// TODO(b/181083333): add multiple root display area support on this API.
/** /**
* For use only by the current home application, to specify the size of * For use only by the current home application, to specify the size of
* wallpaper it would like to use. This allows such applications to have * wallpaper it would like to use. This allows such applications to have
@@ -1714,7 +1713,7 @@ public class WallpaperManager {
* @param minimumHeight Desired minimum height * @param minimumHeight Desired minimum height
*/ */
public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) { public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
assertUiContext("suggestDesiredDimensions"); StrictMode.assertUiContext(mContext, "suggestDesiredDimensions");
try { try {
/** /**
* The framework makes no attempt to limit the window size * The framework makes no attempt to limit the window size
@@ -1757,6 +1756,7 @@ public class WallpaperManager {
} }
} }
// TODO(b/181083333): add multiple root display area support on this API.
/** /**
* Specify extra padding that the wallpaper should have outside of the display. * Specify extra padding that the wallpaper should have outside of the display.
* That is, the given padding supplies additional pixels the wallpaper should extend * That is, the given padding supplies additional pixels the wallpaper should extend
@@ -1770,7 +1770,7 @@ public class WallpaperManager {
*/ */
@RequiresPermission(android.Manifest.permission.SET_WALLPAPER_HINTS) @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_HINTS)
public void setDisplayPadding(Rect padding) { public void setDisplayPadding(Rect padding) {
assertUiContext("setDisplayPadding"); StrictMode.assertUiContext(mContext, "setDisplayPadding");
try { try {
if (sGlobals.mService == null) { if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running"); Log.w(TAG, "WallpaperService not running");
@@ -2023,7 +2023,6 @@ public class WallpaperManager {
*/ */
@RequiresPermission(android.Manifest.permission.SET_WALLPAPER) @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
public void clear() throws IOException { public void clear() throws IOException {
assertUiContext("clear");
setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false); setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false);
} }
@@ -2176,10 +2175,6 @@ public class WallpaperManager {
return mCmProxy; return mCmProxy;
} }
private void assertUiContext(final String methodName) {
StrictMode.assertUiContext(mContext, methodName);
}
/** /**
* A hidden class to help {@link Globals#getCurrentWallpaperLocked} handle color management. * A hidden class to help {@link Globals#getCurrentWallpaperLocked} handle color management.
* @hide * @hide

View File

@@ -2274,10 +2274,9 @@ public final class StrictMode {
*/ */
public static void assertUiContext(@NonNull Context context, @NonNull String methodName) { public static void assertUiContext(@NonNull Context context, @NonNull String methodName) {
if (vmIncorrectContextUseEnabled() && !context.isUiContext()) { if (vmIncorrectContextUseEnabled() && !context.isUiContext()) {
final String errorMessage = "Tried to access UI related API" + methodName final String errorMessage = "Tried to access UI related API:" + methodName
+ " from a non-UI Context:" + context; + " from a non-UI Context:" + context;
final String message = "UI-related services, such as WindowManager, WallpaperService " final String message = methodName + " should be accessed from Activity or other UI "
+ "or LayoutInflater should be accessed from Activity or other UI "
+ "Contexts. Use an Activity or a Context created with " + "Contexts. Use an Activity or a Context created with "
+ "Context#createWindowContext(int, Bundle), which are adjusted to " + "Context#createWindowContext(int, Bundle), which are adjusted to "
+ "the configuration and visual bounds of an area on screen."; + "the configuration and visual bounds of an area on screen.";