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

Commit 4be1bf63 authored by BK Choi's avatar BK Choi Committed by Android (Google) Code Review
Browse files

Merge "Fix the enforcing method to pass current user for visible background users" into main

parents 16452102 ec8e8394
Loading
Loading
Loading
Loading
+20 −13
Original line number Original line Diff line number Diff line
@@ -443,6 +443,11 @@ final class UiModeManagerService extends SystemService {
        mDreamsDisabledByAmbientModeSuppression = disabledByAmbientModeSuppression;
        mDreamsDisabledByAmbientModeSuppression = disabledByAmbientModeSuppression;
    }
    }


    @VisibleForTesting
    void setCurrentUser(int currentUserId) {
        mCurrentUser = currentUserId;
    }

    @Override
    @Override
    public void onUserSwitching(@Nullable TargetUser from, @NonNull TargetUser to) {
    public void onUserSwitching(@Nullable TargetUser from, @NonNull TargetUser to) {
        mCurrentUser = to.getUserIdentifier();
        mCurrentUser = to.getUserIdentifier();
@@ -864,9 +869,9 @@ final class UiModeManagerService extends SystemService {
                    throw new IllegalArgumentException("Unknown mode: " + mode);
                    throw new IllegalArgumentException("Unknown mode: " + mode);
            }
            }


            final int user = UserHandle.getCallingUserId();
            enforceCurrentUserIfVisibleBackgroundEnabled(mCurrentUser);
            enforceCurrentUserIfVisibleBackgroundEnabled(user);


            final int user = UserHandle.getCallingUserId();
            final long ident = Binder.clearCallingIdentity();
            final long ident = Binder.clearCallingIdentity();
            try {
            try {
                synchronized (mLock) {
                synchronized (mLock) {
@@ -929,7 +934,7 @@ final class UiModeManagerService extends SystemService {
                @AttentionModeThemeOverlayType int attentionModeThemeOverlayType) {
                @AttentionModeThemeOverlayType int attentionModeThemeOverlayType) {
            setAttentionModeThemeOverlay_enforcePermission();
            setAttentionModeThemeOverlay_enforcePermission();


            enforceCurrentUserIfVisibleBackgroundEnabled(UserHandle.getCallingUserId());
            enforceCurrentUserIfVisibleBackgroundEnabled(mCurrentUser);


            synchronized (mLock) {
            synchronized (mLock) {
                if (mAttentionModeThemeOverlay != attentionModeThemeOverlayType) {
                if (mAttentionModeThemeOverlay != attentionModeThemeOverlayType) {
@@ -1020,16 +1025,16 @@ final class UiModeManagerService extends SystemService {
                return false;
                return false;
            }
            }
            final int user = Binder.getCallingUserHandle().getIdentifier();
            final int user = Binder.getCallingUserHandle().getIdentifier();
            enforceCurrentUserIfVisibleBackgroundEnabled(user);

            if (user != mCurrentUser && getContext().checkCallingOrSelfPermission(
            if (user != mCurrentUser && getContext().checkCallingOrSelfPermission(
                    android.Manifest.permission.INTERACT_ACROSS_USERS)
                    android.Manifest.permission.INTERACT_ACROSS_USERS)
                    != PackageManager.PERMISSION_GRANTED) {
                    != PackageManager.PERMISSION_GRANTED) {
                Slog.e(TAG, "Target user is not current user,"
                Slog.e(TAG, "Target user is not current user,"
                        + " INTERACT_ACROSS_USERS permission is required");
                        + " INTERACT_ACROSS_USERS permission is required");
                return false;
                return false;

            }
            }

            enforceCurrentUserIfVisibleBackgroundEnabled(mCurrentUser);

            // Store the last requested bedtime night mode state so that we don't need to notify
            // Store the last requested bedtime night mode state so that we don't need to notify
            // anyone if the user decides to switch to the night mode to bedtime.
            // anyone if the user decides to switch to the night mode to bedtime.
            if (modeCustomType == MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
            if (modeCustomType == MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
@@ -1078,9 +1083,10 @@ final class UiModeManagerService extends SystemService {
                Slog.e(TAG, "Set custom time start, requires MODIFY_DAY_NIGHT_MODE permission");
                Slog.e(TAG, "Set custom time start, requires MODIFY_DAY_NIGHT_MODE permission");
                return;
                return;
            }
            }
            final int user = UserHandle.getCallingUserId();
            enforceCurrentUserIfVisibleBackgroundEnabled(user);


            enforceCurrentUserIfVisibleBackgroundEnabled(mCurrentUser);

            final int user = UserHandle.getCallingUserId();
            final long ident = Binder.clearCallingIdentity();
            final long ident = Binder.clearCallingIdentity();
            try {
            try {
                LocalTime newTime = LocalTime.ofNanoOfDay(time * 1000);
                LocalTime newTime = LocalTime.ofNanoOfDay(time * 1000);
@@ -1108,9 +1114,10 @@ final class UiModeManagerService extends SystemService {
                Slog.e(TAG, "Set custom time end, requires MODIFY_DAY_NIGHT_MODE permission");
                Slog.e(TAG, "Set custom time end, requires MODIFY_DAY_NIGHT_MODE permission");
                return;
                return;
            }
            }
            final int user = UserHandle.getCallingUserId();
            enforceCurrentUserIfVisibleBackgroundEnabled(user);


            enforceCurrentUserIfVisibleBackgroundEnabled(mCurrentUser);

            final int user = UserHandle.getCallingUserId();
            final long ident = Binder.clearCallingIdentity();
            final long ident = Binder.clearCallingIdentity();
            try {
            try {
                LocalTime newTime = LocalTime.ofNanoOfDay(time * 1000);
                LocalTime newTime = LocalTime.ofNanoOfDay(time * 1000);
@@ -1131,7 +1138,7 @@ final class UiModeManagerService extends SystemService {
            assertLegit(callingPackage);
            assertLegit(callingPackage);
            assertSingleProjectionType(projectionType);
            assertSingleProjectionType(projectionType);
            enforceProjectionTypePermissions(projectionType);
            enforceProjectionTypePermissions(projectionType);
            enforceCurrentUserIfVisibleBackgroundEnabled(getCallingUserId());
            enforceCurrentUserIfVisibleBackgroundEnabled(mCurrentUser);


            synchronized (mLock) {
            synchronized (mLock) {
                if (mProjectionHolders == null) {
                if (mProjectionHolders == null) {
@@ -1177,7 +1184,7 @@ final class UiModeManagerService extends SystemService {
            assertLegit(callingPackage);
            assertLegit(callingPackage);
            assertSingleProjectionType(projectionType);
            assertSingleProjectionType(projectionType);
            enforceProjectionTypePermissions(projectionType);
            enforceProjectionTypePermissions(projectionType);
            enforceCurrentUserIfVisibleBackgroundEnabled(getCallingUserId());
            enforceCurrentUserIfVisibleBackgroundEnabled(mCurrentUser);


            return releaseProjectionUnchecked(projectionType, callingPackage);
            return releaseProjectionUnchecked(projectionType, callingPackage);
        }
        }
@@ -1219,7 +1226,7 @@ final class UiModeManagerService extends SystemService {
                return;
                return;
            }
            }


            enforceCurrentUserIfVisibleBackgroundEnabled(getCallingUserId());
            enforceCurrentUserIfVisibleBackgroundEnabled(mCurrentUser);


            synchronized (mLock) {
            synchronized (mLock) {
                if (mProjectionListeners == null) {
                if (mProjectionListeners == null) {
+3 −0
Original line number Original line Diff line number Diff line
@@ -68,6 +68,7 @@ import static org.testng.Assert.assertThrows;


import android.Manifest;
import android.Manifest;
import android.app.Activity;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.AlarmManager;
import android.app.Flags;
import android.app.Flags;
import android.app.IOnProjectionStateChangedListener;
import android.app.IOnProjectionStateChangedListener;
@@ -247,6 +248,8 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
        mInjector = spy(new TestInjector());
        mInjector = spy(new TestInjector());
        mUiManagerService = new UiModeManagerService(mContext, /* setupWizardComplete= */ true,
        mUiManagerService = new UiModeManagerService(mContext, /* setupWizardComplete= */ true,
                mTwilightManager, mInjector);
                mTwilightManager, mInjector);
        // Initialize the current user.
        mUiManagerService.setCurrentUser(ActivityManager.getCurrentUser());
        try {
        try {
            mUiManagerService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
            mUiManagerService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
        } catch (SecurityException e) {/* ignore for permission denial */}
        } catch (SecurityException e) {/* ignore for permission denial */}