Add isCommunalMode() to CommunalManager SystemApi.

Design: go/communal-manager-api.

Test: atest CtsAppTestCases:CommunalManagerTest
Test: atest FrameworksMockingServicesTests:CommunalManagerServiceTest

Bug: 206054365
Change-Id: Ib1b964b17a7742b4e337862efa2b4e0562322fd0
This commit is contained in:
Xiaowen Lei
2021-11-12 00:59:57 +00:00
parent b1db354749
commit ef06f6dbaa
6 changed files with 60 additions and 2 deletions

View File

@@ -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 {

View File

@@ -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();
}
}
}

View File

@@ -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();
}

View File

@@ -5535,6 +5535,12 @@
<permission android:name="android.permission.WRITE_COMMUNAL_STATE"
android:protectionLevel="signature" />
<!-- Allows an application to view information from the currently active
{@link com.android.server.communal.CommunalManagerService}.
@hide -->
<permission android:name="android.permission.READ_COMMUNAL_STATE"
android:protectionLevel="signature|privileged"/>
<!-- Allows the holder to manage whether the system can bind to services
provided by instant apps. This permission is intended to protect
test/development fucntionality and should be used only in such cases.

View File

@@ -254,6 +254,18 @@ public final class CommunalManagerService extends SystemService {
+ "permission required to modify communal state.");
mCommunalViewIsShowing.set(isShowing);
}
/**
* Checks whether or not we are in communal mode.
*/
@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.");
return mCommunalViewIsShowing.get();
}
}
/**

View File

@@ -123,6 +123,8 @@ public class CommunalManagerServiceTest {
doNothing().when(mContextSpy).enforceCallingPermission(
eq(Manifest.permission.WRITE_COMMUNAL_STATE), anyString());
doNothing().when(mContextSpy).enforceCallingPermission(
eq(Manifest.permission.READ_COMMUNAL_STATE), anyString());
mService = new CommunalManagerService(mContextSpy);
mService.onBootPhase(SystemService.PHASE_THIRD_PARTY_APPS_CAN_START);
@@ -202,6 +204,18 @@ public class CommunalManagerServiceTest {
return new Intent(action, Uri.parse("package:" + packageName));
}
@Test
public void testIsCommunalMode_isTrue() throws RemoteException {
mBinder.setCommunalViewShowing(true);
assertThat(mBinder.isCommunalMode()).isTrue();
}
@Test
public void testIsCommunalMode_isFalse() throws RemoteException {
mBinder.setCommunalViewShowing(false);
assertThat(mBinder.isCommunalMode()).isFalse();
}
@Test
public void testIntercept_unlocked_communalOff_appNotEnabled_showWhenLockedOff() {
when(mKeyguardManager.isKeyguardLocked()).thenReturn(false);