From 8167b48fce476ca6a71106bb775dca7b2d5ba378 Mon Sep 17 00:00:00 2001 From: Bryce Lee Date: Wed, 15 Feb 2023 15:26:26 -0800 Subject: [PATCH] Check system user is running process for dream logic. This changelist ensures system user is running the process before entering any dream related logic. This moves away from checking that the current user is running the process, as this will not always be true, such as when SystemUI restarts when a secondary user is running. Fixes: 269611342 Test: atest SystemProcessConditionTest Change-Id: I6471edaaea8dff93b0c86cb5bfee4a4cdabfd686 --- .../systemui/dreams/dagger/DreamModule.java | 4 +-- .../systemui/process/ProcessWrapper.java | 7 +++-- ...ition.java => SystemProcessCondition.java} | 15 ++++------ ...t.java => SystemProcessConditionTest.java} | 29 +++++++------------ 4 files changed, 24 insertions(+), 31 deletions(-) rename packages/SystemUI/src/com/android/systemui/process/condition/{UserProcessCondition.java => SystemProcessCondition.java} (67%) rename packages/SystemUI/tests/src/com/android/systemui/process/condition/{UserProcessConditionTest.java => SystemProcessConditionTest.java} (70%) diff --git a/packages/SystemUI/src/com/android/systemui/dreams/dagger/DreamModule.java b/packages/SystemUI/src/com/android/systemui/dreams/dagger/DreamModule.java index 88c02b8aa790c..13563dfd03f03 100644 --- a/packages/SystemUI/src/com/android/systemui/dreams/dagger/DreamModule.java +++ b/packages/SystemUI/src/com/android/systemui/dreams/dagger/DreamModule.java @@ -29,7 +29,7 @@ import com.android.systemui.dreams.DreamOverlayNotificationCountProvider; import com.android.systemui.dreams.DreamOverlayService; import com.android.systemui.dreams.complication.dagger.RegisteredComplicationsModule; import com.android.systemui.dreams.touch.scrim.dagger.ScrimModule; -import com.android.systemui.process.condition.UserProcessCondition; +import com.android.systemui.process.condition.SystemProcessCondition; import com.android.systemui.shared.condition.Condition; import com.android.systemui.shared.condition.Monitor; @@ -126,7 +126,7 @@ public interface DreamModule { @Binds @IntoSet @Named(DREAM_PRETEXT_CONDITIONS) - Condition bindsUserProcessCondition(UserProcessCondition condition); + Condition bindSystemProcessCondition(SystemProcessCondition condition); /** */ @Provides diff --git a/packages/SystemUI/src/com/android/systemui/process/ProcessWrapper.java b/packages/SystemUI/src/com/android/systemui/process/ProcessWrapper.java index 245cf89a83379..27510720ae2f0 100644 --- a/packages/SystemUI/src/com/android/systemui/process/ProcessWrapper.java +++ b/packages/SystemUI/src/com/android/systemui/process/ProcessWrapper.java @@ -26,7 +26,10 @@ public class ProcessWrapper { @Inject public ProcessWrapper() {} - public int getUserHandleIdentifier() { - return android.os.Process.myUserHandle().getIdentifier(); + /** + * Returns {@code true} if System User is running the current process. + */ + public boolean isSystemUser() { + return android.os.Process.myUserHandle().isSystem(); } } diff --git a/packages/SystemUI/src/com/android/systemui/process/condition/UserProcessCondition.java b/packages/SystemUI/src/com/android/systemui/process/condition/SystemProcessCondition.java similarity index 67% rename from packages/SystemUI/src/com/android/systemui/process/condition/UserProcessCondition.java rename to packages/SystemUI/src/com/android/systemui/process/condition/SystemProcessCondition.java index 5a21ea075ea3b..80fbf91150652 100644 --- a/packages/SystemUI/src/com/android/systemui/process/condition/UserProcessCondition.java +++ b/packages/SystemUI/src/com/android/systemui/process/condition/SystemProcessCondition.java @@ -17,29 +17,26 @@ package com.android.systemui.process.condition; import com.android.systemui.process.ProcessWrapper; -import com.android.systemui.settings.UserTracker; import com.android.systemui.shared.condition.Condition; import javax.inject.Inject; /** - * {@link UserProcessCondition} provides a signal when the process handle belongs to the current - * user. + * {@link SystemProcessCondition} checks to make sure the current process is being ran by the + * System User. */ -public class UserProcessCondition extends Condition { +public class SystemProcessCondition extends Condition { private final ProcessWrapper mProcessWrapper; - private final UserTracker mUserTracker; @Inject - public UserProcessCondition(ProcessWrapper processWrapper, UserTracker userTracker) { + public SystemProcessCondition(ProcessWrapper processWrapper) { + super(); mProcessWrapper = processWrapper; - mUserTracker = userTracker; } @Override protected void start() { - updateCondition(mUserTracker.getUserId() - == mProcessWrapper.getUserHandleIdentifier()); + updateCondition(mProcessWrapper.isSystemUser()); } @Override diff --git a/packages/SystemUI/tests/src/com/android/systemui/process/condition/UserProcessConditionTest.java b/packages/SystemUI/tests/src/com/android/systemui/process/condition/SystemProcessConditionTest.java similarity index 70% rename from packages/SystemUI/tests/src/com/android/systemui/process/condition/UserProcessConditionTest.java rename to packages/SystemUI/tests/src/com/android/systemui/process/condition/SystemProcessConditionTest.java index 2293fc5770293..fb7197706ddca 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/process/condition/UserProcessConditionTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/process/condition/SystemProcessConditionTest.java @@ -26,7 +26,6 @@ import androidx.test.filters.SmallTest; import com.android.systemui.SysuiTestCase; import com.android.systemui.process.ProcessWrapper; -import com.android.systemui.settings.UserTracker; import com.android.systemui.shared.condition.Condition; import com.android.systemui.shared.condition.Monitor; import com.android.systemui.util.concurrency.FakeExecutor; @@ -41,10 +40,7 @@ import org.mockito.MockitoAnnotations; @RunWith(AndroidTestingRunner.class) @TestableLooper.RunWithLooper @SmallTest -public class UserProcessConditionTest extends SysuiTestCase { - @Mock - UserTracker mUserTracker; - +public class SystemProcessConditionTest extends SysuiTestCase { @Mock ProcessWrapper mProcessWrapper; @@ -59,15 +55,14 @@ public class UserProcessConditionTest extends SysuiTestCase { } /** - * Verifies condition reports false when tracker reports a different user id than the - * identifier from the process handle. + * Verifies condition reports false when tracker reports the process is being ran by the + * system user. */ @Test - public void testConditionFailsWithDifferentIds() { + public void testConditionFailsWithNonSystemProcess() { - final Condition condition = new UserProcessCondition(mProcessWrapper, mUserTracker); - when(mProcessWrapper.getUserHandleIdentifier()).thenReturn(0); - when(mUserTracker.getUserId()).thenReturn(1); + final Condition condition = new SystemProcessCondition(mProcessWrapper); + when(mProcessWrapper.isSystemUser()).thenReturn(false); final Monitor monitor = new Monitor(mExecutor); @@ -81,15 +76,14 @@ public class UserProcessConditionTest extends SysuiTestCase { } /** - * Verifies condition reports false when tracker reports a different user id than the - * identifier from the process handle. + * Verifies condition reports true when tracker reports the process is being ran by the + * system user. */ @Test - public void testConditionSucceedsWithSameIds() { + public void testConditionSucceedsWithSystemProcess() { - final Condition condition = new UserProcessCondition(mProcessWrapper, mUserTracker); - when(mProcessWrapper.getUserHandleIdentifier()).thenReturn(0); - when(mUserTracker.getUserId()).thenReturn(0); + final Condition condition = new SystemProcessCondition(mProcessWrapper); + when(mProcessWrapper.isSystemUser()).thenReturn(true); final Monitor monitor = new Monitor(mExecutor); @@ -101,5 +95,4 @@ public class UserProcessConditionTest extends SysuiTestCase { verify(mCallback).onConditionsChanged(true); } - }