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

Commit 6954347b authored by Charles Chen's avatar Charles Chen
Browse files

Refactor checkAddPermission

Bug: 128338354
Test: Only simple refactor, build pass

Change-Id: I8f249fa80c885f7b35ef32c8fb3d3adcb95febd7
parent 848b6d44
Loading
Loading
Loading
Loading
+4 −8
Original line number Original line Diff line number Diff line
@@ -45,7 +45,6 @@ import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
import static android.view.WindowManager.LayoutParams.LAST_SYSTEM_WINDOW;
import static android.view.WindowManager.LayoutParams.LAST_SYSTEM_WINDOW;
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY;
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR;
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR;
import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
@@ -2058,11 +2057,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {


    /** {@inheritDoc} */
    /** {@inheritDoc} */
    @Override
    @Override
    public int checkAddPermission(WindowManager.LayoutParams attrs, int[] outAppOp) {
    public int checkAddPermission(int type, boolean isRoundedCornerOverlay, String packageName,
        final int type = attrs.type;
            int[] outAppOp) {
        final boolean isRoundedCornerOverlay =
                (attrs.privateFlags & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY) != 0;

        if (isRoundedCornerOverlay && mContext.checkCallingOrSelfPermission(INTERNAL_SYSTEM_WINDOW)
        if (isRoundedCornerOverlay && mContext.checkCallingOrSelfPermission(INTERNAL_SYSTEM_WINDOW)
                != PERMISSION_GRANTED) {
                != PERMISSION_GRANTED) {
            return ADD_PERMISSION_DENIED;
            return ADD_PERMISSION_DENIED;
@@ -2119,7 +2115,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        ApplicationInfo appInfo;
        ApplicationInfo appInfo;
        try {
        try {
            appInfo = mPackageManager.getApplicationInfoAsUser(
            appInfo = mPackageManager.getApplicationInfoAsUser(
                            attrs.packageName,
                            packageName,
                            0 /* flags */,
                            0 /* flags */,
                            UserHandle.getUserId(callingUid));
                            UserHandle.getUserId(callingUid));
        } catch (PackageManager.NameNotFoundException e) {
        } catch (PackageManager.NameNotFoundException e) {
@@ -2139,7 +2135,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {


        // check if user has enabled this operation. SecurityException will be thrown if this app
        // check if user has enabled this operation. SecurityException will be thrown if this app
        // has not been allowed by the user
        // has not been allowed by the user
        final int mode = mAppOpsManager.noteOpNoThrow(outAppOp[0], callingUid, attrs.packageName);
        final int mode = mAppOpsManager.noteOpNoThrow(outAppOp[0], callingUid, packageName);
        switch (mode) {
        switch (mode) {
            case AppOpsManager.MODE_ALLOWED:
            case AppOpsManager.MODE_ALLOWED:
            case AppOpsManager.MODE_IGNORED:
            case AppOpsManager.MODE_IGNORED:
+11 −3
Original line number Original line Diff line number Diff line
@@ -690,17 +690,25 @@ public interface WindowManagerPolicy extends WindowManagerPolicyConstants {
            WindowManagerFuncs windowManagerFuncs);
            WindowManagerFuncs windowManagerFuncs);


    /**
    /**
     * Check permissions when adding a window.
     * Check permissions when adding a window or a window token from
     * {@link android.app.WindowContext}.
     *
     *
     * @param attrs The window's LayoutParams.
     * @param type The window type
     * @param isRoundedCornerOverlay {@code true} to indicate the adding window is
     *                                           round corner overlay.
     * @param packageName package name
     * @param outAppOp First element will be filled with the app op corresponding to
     * @param outAppOp First element will be filled with the app op corresponding to
     *                 this window, or OP_NONE.
     *                 this window, or OP_NONE.
     *
     *
     * @return {@link WindowManagerGlobal#ADD_OKAY} if the add can proceed;
     * @return {@link WindowManagerGlobal#ADD_OKAY} if the add can proceed;
     *      else an error code, usually
     *      else an error code, usually
     *      {@link WindowManagerGlobal#ADD_PERMISSION_DENIED}, to abort the add.
     *      {@link WindowManagerGlobal#ADD_PERMISSION_DENIED}, to abort the add.
     *
     * @see IWindowManager#addWindowTokenWithOptions(IBinder, int, int, Bundle, String)
     * @see WindowManager.LayoutParams#PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY
     */
     */
    public int checkAddPermission(WindowManager.LayoutParams attrs, int[] outAppOp);
    int checkAddPermission(int type, boolean isRoundedCornerOverlay, String packageName,
            int[] outAppOp);


    /**
    /**
     * After the window manager has computed the current configuration based
     * After the window manager has computed the current configuration based
+6 −7
Original line number Original line Diff line number Diff line
@@ -1321,7 +1321,10 @@ public class WindowManagerService extends IWindowManager.Stub
            DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel,
            DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel,
            InsetsState outInsetsState) {
            InsetsState outInsetsState) {
        int[] appOp = new int[1];
        int[] appOp = new int[1];
        int res = mPolicy.checkAddPermission(attrs, appOp);
        final boolean isRoundedCornerOverlay = (attrs.privateFlags
                & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY) != 0;
        int res = mPolicy.checkAddPermission(attrs.type, isRoundedCornerOverlay, attrs.packageName,
                appOp);
        if (res != WindowManagerGlobal.ADD_OKAY) {
        if (res != WindowManagerGlobal.ADD_OKAY) {
            return res;
            return res;
        }
        }
@@ -1395,8 +1398,6 @@ public class WindowManagerService extends IWindowManager.Stub
                    return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
                    return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
                }
                }
                final IBinder binder = attrs.token != null ? attrs.token : client.asBinder();
                final IBinder binder = attrs.token != null ? attrs.token : client.asBinder();
                final boolean isRoundedCornerOverlay =
                        (attrs.privateFlags & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY) != 0;
                token = new WindowToken(this, binder, type, false, displayContent,
                token = new WindowToken(this, binder, type, false, displayContent,
                        session.mCanAddInternalSystemWindow, isRoundedCornerOverlay);
                        session.mCanAddInternalSystemWindow, isRoundedCornerOverlay);
            } else if (rootType >= FIRST_APPLICATION_WINDOW
            } else if (rootType >= FIRST_APPLICATION_WINDOW
@@ -2545,10 +2546,8 @@ public class WindowManagerService extends IWindowManager.Stub
        final boolean callerCanManageAppTokens =
        final boolean callerCanManageAppTokens =
                checkCallingPermission(MANAGE_APP_TOKENS, "addWindowToken()");
                checkCallingPermission(MANAGE_APP_TOKENS, "addWindowToken()");
        if (!callerCanManageAppTokens) {
        if (!callerCanManageAppTokens) {
            // TODO(window-context): refactor checkAddPermission to not take attrs.
            final int res = mPolicy.checkAddPermission(type, false /* isRoundedCornerOverlay */,
            LayoutParams attrs = new LayoutParams(type);
                    packageName, new int[1]);
            attrs.packageName = packageName;
            final int res = mPolicy.checkAddPermission(attrs, new int[1]);
            if (res != ADD_OKAY) {
            if (res != ADD_OKAY) {
                return res;
                return res;
            }
            }
+2 −1
Original line number Original line Diff line number Diff line
@@ -75,7 +75,8 @@ class TestWindowManagerPolicy implements WindowManagerPolicy {
    }
    }


    @Override
    @Override
    public int checkAddPermission(WindowManager.LayoutParams attrs, int[] outAppOp) {
    public int checkAddPermission(int type, boolean isRoundedCornerOverlay, String packageName,
            int[] outAppOp) {
        return 0;
        return 0;
    }
    }