Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 77e33e1a authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Limit UI context verification to UI related APIs"

parents 6ce4b37e 9ca5f902
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -1995,8 +1995,7 @@ class ContextImpl extends Context {
    }

    private static boolean isUiComponent(String name) {
        return WINDOW_SERVICE.equals(name) || LAYOUT_INFLATER_SERVICE.equals(name)
                || WALLPAPER_SERVICE.equals(name);
        return WINDOW_SERVICE.equals(name) || LAYOUT_INFLATER_SERVICE.equals(name);
    }

    @Override
+16 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ import android.os.IBinder;
import android.os.Looper;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.StrictMode;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Log;
@@ -607,6 +608,7 @@ public class WallpaperManager {
     */
    @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
    public Drawable getDrawable() {
        assertUiContext("getDrawable");
        final ColorManagementProxy cmProxy = getColorManagementProxy();
        Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, cmProxy);
        if (bm != null) {
@@ -674,6 +676,7 @@ public class WallpaperManager {
     */
    public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
            float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
        assertUiContext("getBuiltInDrawable");
        if (sGlobals.mService == null) {
            Log.w(TAG, "WallpaperService not running");
            throw new RuntimeException(new DeadSystemException());
@@ -839,6 +842,7 @@ public class WallpaperManager {
     * null pointer if these is none.
     */
    public Drawable peekDrawable() {
        assertUiContext("peekDrawable");
        final ColorManagementProxy cmProxy = getColorManagementProxy();
        Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy);
        if (bm != null) {
@@ -881,6 +885,7 @@ public class WallpaperManager {
     */
    @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
    public Drawable peekFastDrawable() {
        assertUiContext("peekFastDrawable");
        final ColorManagementProxy cmProxy = getColorManagementProxy();
        Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy);
        if (bm != null) {
@@ -1047,6 +1052,7 @@ public class WallpaperManager {
     */
    @UnsupportedAppUsage
    public @Nullable WallpaperColors getWallpaperColors(int which, int userId) {
        assertUiContext("getWallpaperColors");
        return sGlobals.getWallpaperColors(which, userId, mContext.getDisplayId());
    }

@@ -1262,6 +1268,7 @@ public class WallpaperManager {
    @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
    public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
            throws IOException {
        assertUiContext("setResource");
        if (sGlobals.mService == null) {
            Log.w(TAG, "WallpaperService not running");
            throw new RuntimeException(new DeadSystemException());
@@ -1582,6 +1589,7 @@ public class WallpaperManager {
     * @see #getDesiredMinimumHeight()
     */
    public int getDesiredMinimumWidth() {
        assertUiContext("getDesiredMinimumWidth");
        if (sGlobals.mService == null) {
            Log.w(TAG, "WallpaperService not running");
            throw new RuntimeException(new DeadSystemException());
@@ -1610,6 +1618,7 @@ public class WallpaperManager {
     * @see #getDesiredMinimumWidth()
     */
    public int getDesiredMinimumHeight() {
        assertUiContext("getDesiredMinimumHeight");
        if (sGlobals.mService == null) {
            Log.w(TAG, "WallpaperService not running");
            throw new RuntimeException(new DeadSystemException());
@@ -1640,6 +1649,7 @@ public class WallpaperManager {
     * @param minimumHeight Desired minimum height
     */
    public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
        assertUiContext("suggestDesiredDimensions");
        try {
            /**
             * The framework makes no attempt to limit the window size
@@ -1695,6 +1705,7 @@ public class WallpaperManager {
     */
    @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_HINTS)
    public void setDisplayPadding(Rect padding) {
        assertUiContext("setDisplayPadding");
        try {
            if (sGlobals.mService == null) {
                Log.w(TAG, "WallpaperService not running");
@@ -1947,6 +1958,7 @@ public class WallpaperManager {
     */
    @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
    public void clear() throws IOException {
        assertUiContext("clear");
        setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false);
    }

@@ -2095,6 +2107,10 @@ public class WallpaperManager {
        return mCmProxy;
    }

    private void assertUiContext(final String methodName) {
        StrictMode.assertUiContext(mContext, methodName);
    }

    /**
     * A hidden class to help {@link Globals#getCurrentWallpaperLocked} handle color management.
     * @hide
+27 −0
Original line number Diff line number Diff line
@@ -2194,6 +2194,33 @@ public final class StrictMode {
        onVmPolicyViolation(new IncorrectContextUseViolation(message, originStack));
    }

    /**
     * A helper method to verify if the {@code context} is a UI context and throw
     * {@link IncorrectContextUseViolation} if the {@code context} is not a UI context.
     *
     * @param context The context to verify if it is a UI context
     * @param methodName The asserted method name
     *
     * @see Context#isUiContext()
     * @see IncorrectContextUseViolation
     *
     * @hide
     */
    public static void assertUiContext(@NonNull Context context, @NonNull String methodName) {
        if (vmIncorrectContextUseEnabled() && !context.isUiContext()) {
            final String errorMessage = "Tried to access UI related API" + methodName
                    + " from a non-UI Context:" + context;
            final String message = "UI-related services, such as WindowManager, WallpaperService "
                    + "or LayoutInflater should be accessed from Activity or other UI "
                    + "Contexts. Use an Activity or a Context created with "
                    + "Context#createWindowContext(int, Bundle), which are adjusted to "
                    + "the configuration and visual bounds of an area on screen.";
            final Exception exception = new IllegalAccessException(errorMessage);
            StrictMode.onIncorrectContextUsed(message, exception);
            Log.e(TAG, errorMessage + " " + message, exception);
        }
    }

    /** Assume locked until we hear otherwise */
    private static volatile boolean sUserKeyUnlocked = false;