From 4b1a1f130979c751f7690f5ecf75be7438121a83 Mon Sep 17 00:00:00 2001 From: Yogisha Dixit Date: Fri, 25 Sep 2020 13:15:00 +0100 Subject: RESTRICT AUTOMERGE Add a hidden API to check if ambient display is suppressed for a token. Added 1 method: boolean isAmbientDisplaySuppressedForTokenByApp(String token, int appUid) Context: The Settings team is working on adding a string to the AOD settings page when ambient display is suppressed by Bedtime mode: b/168790245. The current PowerManager#isAmbientDisplaySuppressedForToken(String token) method will only work if the calling app is also the app that suppressed the ambient display. That is, if Digital Wellbeing called suppressAmbientDisplay("winddown"), then isAmbientDisplaySuppressedForToken("winddown") will only return true for Digital Wellbeing. It will return false for Settings app This CL adds a hidden API PowerManager#isAmbientDisplaySuppressedForTokenByApp(String token, int appUid) that will return true if the given app is suppressing ambient display with the given token. Settings can then call isAmbientDisplaySuppressedForTokenByApp("winddown", digitalWellbeingUid) to get whether ambient display is suppressed by Bedtime mode. Test: atest FrameworksServicesTests:PowerManagerServiceTest Bug: 169241595 Change-Id: I41aecc4ff0ab159d67e62193af27362a96c3174c (cherry picked from commit 784b62a517d869d639f7da8e4977c68367b38f84) --- core/java/android/os/IPowerManager.aidl | 2 + core/java/android/os/PowerManager.java | 21 +++++++ core/res/AndroidManifest.xml | 4 ++ .../android/server/power/PowerManagerService.java | 16 +++++ services/tests/servicestests/AndroidManifest.xml | 1 + .../server/power/PowerManagerServiceTest.java | 70 ++++++++++++++++++++++ 6 files changed, 114 insertions(+) diff --git a/core/java/android/os/IPowerManager.aidl b/core/java/android/os/IPowerManager.aidl index 187fd590c080..f171441bb787 100644 --- a/core/java/android/os/IPowerManager.aidl +++ b/core/java/android/os/IPowerManager.aidl @@ -94,6 +94,8 @@ interface IPowerManager boolean isAmbientDisplaySuppressedForToken(String token); // returns whether ambient display is suppressed by any app with any token. boolean isAmbientDisplaySuppressed(); + // returns whether ambient display is suppressed by the given app with the given token. + boolean isAmbientDisplaySuppressedForTokenByApp(String token, int appUid); // Forces the system to suspend even if there are held wakelocks. boolean forceSuspend(); diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java index be2de0edda2d..e3a0d18a23cd 100644 --- a/core/java/android/os/PowerManager.java +++ b/core/java/android/os/PowerManager.java @@ -2126,6 +2126,27 @@ public final class PowerManager { } } + /** + * Returns true if ambient display is suppressed by the given {@code appUid} with the given + * {@code token}. + * + *

This method will return false if {@link #isAmbientDisplayAvailable()} is false. + * + * @param token The identifier of the ambient display suppression. + * @param appUid The uid of the app that suppressed ambient display. + * @hide + */ + @RequiresPermission(allOf = { + android.Manifest.permission.READ_DREAM_STATE, + android.Manifest.permission.READ_DREAM_SUPPRESSION }) + public boolean isAmbientDisplaySuppressedForTokenByApp(@NonNull String token, int appUid) { + try { + return mService.isAmbientDisplaySuppressedForTokenByApp(token, appUid); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + /** * Returns the reason the phone was last shutdown. Calling app must have the * {@link android.Manifest.permission#DEVICE_POWER} permission to request this information. diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index bb134f11319b..162fa70dd16a 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -4295,6 +4295,10 @@ + + + + diff --git a/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java index d244e687c8b8..d7897dbb6133 100644 --- a/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java @@ -77,6 +77,7 @@ import com.android.server.SystemService; import com.android.server.lights.LightsManager; import com.android.server.policy.WindowManagerPolicy; import com.android.server.power.PowerManagerService.BatteryReceiver; +import com.android.server.power.PowerManagerService.BinderService; import com.android.server.power.PowerManagerService.Injector; import com.android.server.power.PowerManagerService.NativeWrapper; import com.android.server.power.PowerManagerService.UserSwitchedReceiver; @@ -173,6 +174,7 @@ public class PowerManagerServiceTest { when(mInattentiveSleepWarningControllerMock.isShown()).thenReturn(false); when(mDisplayManagerInternalMock.requestPowerState(any(), anyBoolean())).thenReturn(true); when(mSystemPropertiesMock.get(eq(SYSTEM_PROPERTY_QUIESCENT), anyString())).thenReturn(""); + when(mAmbientDisplayConfigurationMock.ambientDisplayAvailable()).thenReturn(true); mDisplayPowerRequest = new DisplayPowerRequest(); addLocalServiceMock(LightsManager.class, mLightsManagerMock); @@ -967,4 +969,72 @@ public class PowerManagerServiceTest { assertThat(mService.getBinderServiceInstance().isAmbientDisplaySuppressedForToken("test2")) .isFalse(); } + + @Test + public void testIsAmbientDisplaySuppressedForTokenByApp_ambientDisplayUnavailable() + throws Exception { + createService(); + when(mAmbientDisplayConfigurationMock.ambientDisplayAvailable()).thenReturn(false); + + BinderService service = mService.getBinderServiceInstance(); + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test", Binder.getCallingUid())) + .isFalse(); + } + + @Test + public void testIsAmbientDisplaySuppressedForTokenByApp_default() + throws Exception { + createService(); + + BinderService service = mService.getBinderServiceInstance(); + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test", Binder.getCallingUid())) + .isFalse(); + } + + @Test + public void testIsAmbientDisplaySuppressedForTokenByApp_suppressedByCallingApp() + throws Exception { + createService(); + BinderService service = mService.getBinderServiceInstance(); + service.suppressAmbientDisplay("test", true); + + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test", Binder.getCallingUid())) + .isTrue(); + // Check that isAmbientDisplaySuppressedForTokenByApp doesn't return true for another app. + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test", /* appUid= */ 123)) + .isFalse(); + } + + @Test + public void testIsAmbientDisplaySuppressedForTokenByApp_notSuppressedByCallingApp() + throws Exception { + createService(); + BinderService service = mService.getBinderServiceInstance(); + service.suppressAmbientDisplay("test", false); + + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test", Binder.getCallingUid())) + .isFalse(); + // Check that isAmbientDisplaySuppressedForTokenByApp doesn't return true for another app. + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test", /* appUid= */ 123)) + .isFalse(); + } + + @Test + public void testIsAmbientDisplaySuppressedForTokenByApp_multipleTokensSuppressedByCallingApp() + throws Exception { + createService(); + BinderService service = mService.getBinderServiceInstance(); + service.suppressAmbientDisplay("test1", true); + service.suppressAmbientDisplay("test2", true); + + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test1", Binder.getCallingUid())) + .isTrue(); + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test2", Binder.getCallingUid())) + .isTrue(); + // Check that isAmbientDisplaySuppressedForTokenByApp doesn't return true for another app. + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test1", /* appUid= */ 123)) + .isFalse(); + assertThat(service.isAmbientDisplaySuppressedForTokenByApp("test2", /* appUid= */ 123)) + .isFalse(); + } } -- cgit v1.2.3-59-g8ed1b