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

Commit 56923f6a authored by Lucas Silva's avatar Lucas Silva
Browse files

Allow the dream activity in SLS.

We should not have to explicitly allowlist dreams, as any developer
should be able to make a dream and have it run on the device.

Test: locally on device
Test: atest FrameworksMockingServicesTests:CommunalManagerServiceTest
Bug: 203673428
Bug: 203692863
Bug: 203684016
Change-Id: I932b6574dcfc29826554ee058dbcee161503e03c
parent 0acf65b2
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.compat.annotation.ChangeId;
import android.compat.annotation.Disabled;
import android.compat.annotation.Overridable;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.IIntentSender;
import android.content.Intent;
@@ -45,6 +46,7 @@ import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.dreams.DreamManagerInternal;
import android.text.TextUtils;
import android.util.Log;
import android.util.Slog;
@@ -76,6 +78,7 @@ public final class CommunalManagerService extends SystemService {
    private final BinderService mBinderService;
    private final PackageReceiver mPackageReceiver;
    private final PackageManager mPackageManager;
    private final DreamManagerInternal mDreamManagerInternal;

    /**
     * This change id is used to annotate packages which are allowed to run in communal mode.
@@ -134,6 +137,7 @@ public final class CommunalManagerService extends SystemService {
        mContext = context;
        mPackageManager = mContext.getPackageManager();
        mAtmInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
        mDreamManagerInternal = LocalServices.getService(DreamManagerInternal.class);
        mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
        mBinderService = new BinderService();
        mPackageReceiver = new PackageReceiver(mContext);
@@ -205,7 +209,7 @@ public final class CommunalManagerService extends SystemService {
    }

    private boolean isAppAllowed(ApplicationInfo appInfo) {
        if (isChangeEnabled(ALLOW_COMMUNAL_MODE_BY_DEFAULT, appInfo)) {
        if (isActiveDream(appInfo) || isChangeEnabled(ALLOW_COMMUNAL_MODE_BY_DEFAULT, appInfo)) {
            return true;
        }

@@ -213,6 +217,19 @@ public final class CommunalManagerService extends SystemService {
                && getUserEnabledApps().contains(appInfo.packageName);
    }

    private boolean isActiveDream(ApplicationInfo appInfo) {
        final ComponentName activeDream = mDreamManagerInternal.getActiveDreamComponent(
                /* doze= */ false);
        final ComponentName activeDoze = mDreamManagerInternal.getActiveDreamComponent(
                /* doze= */ true);
        return isFromPackage(activeDream, appInfo) || isFromPackage(activeDoze, appInfo);
    }

    private static boolean isFromPackage(ComponentName componentName, ApplicationInfo appInfo) {
        if (componentName == null) return false;
        return TextUtils.equals(appInfo.packageName, componentName.getPackageName());
    }

    private static boolean isChangeEnabled(long changeId, ApplicationInfo appInfo) {
        return CompatChanges.isChangeEnabled(changeId, appInfo.packageName, UserHandle.SYSTEM);
    }
+16 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.app.KeyguardManager;
import android.app.communal.ICommunalManager;
import android.app.compat.CompatChanges;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.ActivityInfo;
@@ -48,6 +49,7 @@ import android.os.RemoteException;
import android.os.UserHandle;
import android.platform.test.annotations.Presubmit;
import android.provider.Settings;
import android.service.dreams.DreamManagerInternal;
import android.test.mock.MockContentResolver;

import androidx.test.InstrumentationRegistry;
@@ -92,6 +94,8 @@ public class CommunalManagerServiceTest {
    private ActivityTaskManagerInternal mAtmInternal;
    @Mock
    private KeyguardManager mKeyguardManager;
    @Mock
    private DreamManagerInternal mDreamManagerInternal;

    private ActivityInterceptorCallback mActivityInterceptorCallback;
    private BroadcastReceiver mPackageReceiver;
@@ -115,6 +119,7 @@ public class CommunalManagerServiceTest {

        when(mContextSpy.getSystemService(KeyguardManager.class)).thenReturn(mKeyguardManager);
        addLocalServiceMock(ActivityTaskManagerInternal.class, mAtmInternal);
        addLocalServiceMock(DreamManagerInternal.class, mDreamManagerInternal);

        doNothing().when(mContextSpy).enforceCallingPermission(
                eq(Manifest.permission.WRITE_COMMUNAL_STATE), anyString());
@@ -286,6 +291,17 @@ public class CommunalManagerServiceTest {
        assertDoesIntercept();
    }

    @Test
    public void testIntercept_locked_communalOn_dream() throws RemoteException {
        mBinder.setCommunalViewShowing(true);
        when(mKeyguardManager.isKeyguardLocked()).thenReturn(true);
        when(mDreamManagerInternal.getActiveDreamComponent(false)).thenReturn(
                new ComponentName(TEST_PACKAGE_NAME, "SomeClass"));

        allowPackages(TEST_PACKAGE_NAME);
        assertDoesNotIntercept();
    }

    @Test
    public void testUpdateSettings_packageUninstalled() {
        allowPackages("package1,package2");