From ef06f6dbaa137eaa5fb6248c0c609fcb85302413 Mon Sep 17 00:00:00 2001 From: Xiaowen Lei Date: Fri, 12 Nov 2021 00:59:57 +0000 Subject: [PATCH 1/2] Add isCommunalMode() to CommunalManager SystemApi. Design: go/communal-manager-api. Test: atest CtsAppTestCases:CommunalManagerTest Test: atest FrameworksMockingServicesTests:CommunalManagerServiceTest Bug: 206054365 Change-Id: Ib1b964b17a7742b4e337862efa2b4e0562322fd0 --- core/api/system-current.txt | 8 ++++++++ .../android/app/communal/CommunalManager.java | 17 +++++++++++++++++ .../android/app/communal/ICommunalManager.aidl | 5 +++-- core/res/AndroidManifest.xml | 6 ++++++ .../server/communal/CommunalManagerService.java | 12 ++++++++++++ .../communal/CommunalManagerServiceTest.java | 14 ++++++++++++++ 6 files changed, 60 insertions(+), 2 deletions(-) diff --git a/core/api/system-current.txt b/core/api/system-current.txt index dd951b4e71d36..d8cb961c1b378 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -1309,6 +1309,14 @@ package android.app.backup { } +package android.app.communal { + + public final class CommunalManager { + method @RequiresPermission("android.permission.READ_COMMUNAL_STATE") public boolean isCommunalMode(); + } + +} + package android.app.compat { public final class CompatChanges { diff --git a/core/java/android/app/communal/CommunalManager.java b/core/java/android/app/communal/CommunalManager.java index 60730adb43075..c2d2f271a8fbe 100644 --- a/core/java/android/app/communal/CommunalManager.java +++ b/core/java/android/app/communal/CommunalManager.java @@ -19,6 +19,7 @@ package android.app.communal; import android.Manifest; import android.annotation.RequiresFeature; import android.annotation.RequiresPermission; +import android.annotation.SystemApi; import android.annotation.SystemService; import android.compat.annotation.ChangeId; import android.compat.annotation.Disabled; @@ -33,6 +34,7 @@ import android.os.RemoteException; * * @hide */ +@SystemApi(client = SystemApi.Client.PRIVILEGED_APPS) @SystemService(Context.COMMUNAL_MANAGER_SERVICE) @RequiresFeature(PackageManager.FEATURE_COMMUNAL_MODE) public final class CommunalManager { @@ -59,6 +61,7 @@ public final class CommunalManager { @Disabled public static final long ALLOW_COMMUNAL_MODE_WITH_USER_CONSENT = 200324021L; + /** @hide */ public CommunalManager(ICommunalManager service) { mService = service; } @@ -67,6 +70,8 @@ public final class CommunalManager { * Updates whether or not the communal view is currently showing over the lockscreen. * * @param isShowing Whether communal view is showing. + * + * @hide */ @RequiresPermission(Manifest.permission.WRITE_COMMUNAL_STATE) public void setCommunalViewShowing(boolean isShowing) { @@ -76,4 +81,16 @@ public final class CommunalManager { throw e.rethrowFromSystemServer(); } } + + /** + * Check whether or not the communal view is currently showing over the lockscreen. + */ + @RequiresPermission(Manifest.permission.READ_COMMUNAL_STATE) + public boolean isCommunalMode() { + try { + return mService.isCommunalMode(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } } diff --git a/core/java/android/app/communal/ICommunalManager.aidl b/core/java/android/app/communal/ICommunalManager.aidl index 02e8a65707102..123b23c4b5536 100644 --- a/core/java/android/app/communal/ICommunalManager.aidl +++ b/core/java/android/app/communal/ICommunalManager.aidl @@ -22,6 +22,7 @@ package android.app.communal; * * @hide */ -oneway interface ICommunalManager { - void setCommunalViewShowing(boolean isShowing); +interface ICommunalManager { + oneway void setCommunalViewShowing(boolean isShowing); + boolean isCommunalMode(); } \ No newline at end of file diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index 6e2c807105168..07cff73a841a1 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -5535,6 +5535,12 @@ + + + + @hide + @TestApi --> + {@link com.android.server.communal.CommunalManagerService}. + @hide + @SystemApi --> diff --git a/data/etc/privapp-permissions-platform.xml b/data/etc/privapp-permissions-platform.xml index 60cb9d321f023..81db63eaa039d 100644 --- a/data/etc/privapp-permissions-platform.xml +++ b/data/etc/privapp-permissions-platform.xml @@ -519,6 +519,9 @@ applications that come with the platform + + + diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml index 262cf536eff55..e5b5285ceacaf 100644 --- a/packages/Shell/AndroidManifest.xml +++ b/packages/Shell/AndroidManifest.xml @@ -606,6 +606,10 @@ + + + + mListeners = + new RemoteCallbackList<>(); private final ActivityInterceptorCallback mActivityInterceptorCallback = new ActivityInterceptorCallback() { @@ -129,7 +134,7 @@ public final class CommunalManagerService extends SystemService { @Override public void onStart() { - publishBinderService(Context.COMMUNAL_MANAGER_SERVICE, mBinderService); + publishBinderService(Context.COMMUNAL_SERVICE, mBinderService); } @Override @@ -242,6 +247,27 @@ public final class CommunalManagerService extends SystemService { return !isAppAllowed(appInfo); } + private void dispatchCommunalMode(boolean isShowing) { + synchronized (mListeners) { + int i = mListeners.beginBroadcast(); + while (i > 0) { + i--; + try { + mListeners.getBroadcastItem(i).onCommunalModeChanged(isShowing); + } catch (RemoteException e) { + // Handled by the RemoteCallbackList. + } + } + mListeners.finishBroadcast(); + } + } + + private void enforceReadPermission() { + mContext.enforceCallingPermission(Manifest.permission.READ_COMMUNAL_STATE, + Manifest.permission.READ_COMMUNAL_STATE + + "permission required to read communal state."); + } + private final class BinderService extends ICommunalManager.Stub { /** * Sets whether or not we are in communal mode. @@ -252,7 +278,11 @@ public final class CommunalManagerService extends SystemService { mContext.enforceCallingPermission(Manifest.permission.WRITE_COMMUNAL_STATE, Manifest.permission.WRITE_COMMUNAL_STATE + "permission required to modify communal state."); + if (mCommunalViewIsShowing.get() == isShowing) { + return; + } mCommunalViewIsShowing.set(isShowing); + dispatchCommunalMode(isShowing); } /** @@ -261,11 +291,31 @@ public final class CommunalManagerService extends SystemService { @RequiresPermission(Manifest.permission.READ_COMMUNAL_STATE) @Override public boolean isCommunalMode() { - mContext.enforceCallingPermission(Manifest.permission.READ_COMMUNAL_STATE, - Manifest.permission.READ_COMMUNAL_STATE - + "permission required to read communal state."); + enforceReadPermission(); return mCommunalViewIsShowing.get(); } + + /** + * Adds a callback to execute when communal state changes. + */ + @RequiresPermission(Manifest.permission.READ_COMMUNAL_STATE) + public void addCommunalModeListener(ICommunalModeListener listener) { + enforceReadPermission(); + synchronized (mListeners) { + mListeners.register(listener); + } + } + + /** + * Removes an added callback that execute when communal state changes. + */ + @RequiresPermission(Manifest.permission.READ_COMMUNAL_STATE) + public void removeCommunalModeListener(ICommunalModeListener listener) { + enforceReadPermission(); + synchronized (mListeners) { + mListeners.unregister(listener); + } + } } /**