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

Commit 7f077899 authored by Garvit Narang's avatar Garvit Narang Committed by Android (Google) Code Review
Browse files

Merge changes from topic "device-effects-applier-integration" into main

* changes:
  Fix setting device effects applier
  Annotate NotificationManagerInternal with @Keep
parents 511699c8 11624e75
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -22,8 +22,11 @@ import android.app.NotificationChannelGroup;
import android.app.backup.BackupRestoreEventLogger;
import android.service.notification.DeviceEffectsApplier;

import com.android.internal.annotations.Keep;

import java.util.Set;

@Keep
public interface NotificationManagerInternal {
    NotificationChannel getNotificationChannel(String pkg, int uid, String channelId);
    NotificationChannelGroup getNotificationChannelGroup(String pkg, int uid, String channelId);
+1 −1
Original line number Diff line number Diff line
@@ -3089,7 +3089,7 @@ public class NotificationManagerService extends SystemService {
            migrateDefaultNAS();
            maybeShowInitialReviewPermissionsNotification();
            if (android.app.Flags.modesApi()) {
            if (android.app.Flags.modesApi() && !mZenModeHelper.hasDeviceEffectsApplier()) {
                // Cannot be done earlier, as some services aren't ready until this point.
                mZenModeHelper.setDeviceEffectsApplier(
                        new DefaultDeviceEffectsApplier(getContext()));
+11 −2
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_ENABLED;
import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_REMOVED;
import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_UNKNOWN;
import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_ANY;
import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_CONFIG;
import static android.app.backup.NotificationLoggingConstants.ERROR_XML_PARSING;
import static android.service.notification.Condition.SOURCE_UNKNOWN;
import static android.service.notification.Condition.SOURCE_USER_ACTION;
import static android.service.notification.Condition.STATE_FALSE;
@@ -44,8 +46,6 @@ import static android.service.notification.ZenModeConfig.isImplicitRuleId;

import static com.android.internal.util.FrameworkStatsLog.DND_MODE_RULE;
import static com.android.internal.util.Preconditions.checkArgument;
import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_CONFIG;
import static android.app.backup.NotificationLoggingConstants.ERROR_XML_PARSING;

import static java.util.Objects.requireNonNull;

@@ -302,6 +302,15 @@ public class ZenModeHelper {
        mIsSystemServicesReady = true;
    }

    /**
     * @return whether a {@link DeviceEffectsApplier} has already been set or not
     */
    boolean hasDeviceEffectsApplier() {
        synchronized (mConfigLock) {
            return mDeviceEffectsApplier != null;
        }
    }

    /**
     * Set the {@link DeviceEffectsApplier} used to apply the consolidated effects.
     *
+43 −13
Original line number Diff line number Diff line
@@ -361,6 +361,9 @@ import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
@@ -375,9 +378,6 @@ import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;
@SmallTest
@RunWith(ParameterizedAndroidJunit4.class)
@RunWithLooper
@@ -490,7 +490,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
    private final NotificationChannel mParentChannel =
            new NotificationChannel(PARENT_CHANNEL_ID, "parentName", IMPORTANCE_DEFAULT);
    private final NotificationChannel mConversationChannel =
            new NotificationChannel(CONVERSATION_CHANNEL_ID, "conversationName", IMPORTANCE_DEFAULT);
            new NotificationChannel(
                    CONVERSATION_CHANNEL_ID, "conversationName", IMPORTANCE_DEFAULT);
    private static final String PARENT_CHANNEL_ID = "parentChannelId";
    private static final String CONVERSATION_CHANNEL_ID = "conversationChannelId";
@@ -4297,8 +4298,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                    new NotificationChannel("foo", "foo", IMPORTANCE_HIGH));
        Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
        mBinderService.enqueueNotificationWithTag(mPkg, mPkg, "testTvExtenderChannelOverride_onTv", 0,
                generateNotificationRecord(null, tv).getNotification(), mUserId);
        mBinderService.enqueueNotificationWithTag(
                mPkg,
                mPkg,
                "testTvExtenderChannelOverride_onTv",
                0,
                generateNotificationRecord(null, tv).getNotification(),
                mUserId);
        verify(mPreferencesHelper, times(1)).getConversationNotificationChannel(
                anyString(), anyInt(), eq("foo"), eq(null), anyBoolean(), anyBoolean());
    }
@@ -4312,8 +4318,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                mTestNotificationChannel);
        Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
        mBinderService.enqueueNotificationWithTag(mPkg, mPkg, "testTvExtenderChannelOverride_notOnTv",
                0, generateNotificationRecord(null, tv).getNotification(), mUserId);
        mBinderService.enqueueNotificationWithTag(
                mPkg,
                mPkg,
                "testTvExtenderChannelOverride_notOnTv",
                0,
                generateNotificationRecord(null, tv).getNotification(),
                mUserId);
        verify(mPreferencesHelper, times(1)).getConversationNotificationChannel(
                anyString(), anyInt(), eq(mTestNotificationChannel.getId()), eq(null),
                anyBoolean(), anyBoolean());
@@ -7746,8 +7757,20 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                .setContentTitle("foo")
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setStyle(new Notification.MessagingStyle("").addMessage(message2));
        NotificationRecord recordB = new NotificationRecord(mContext, new StatusBarNotification(mPkg,
                mPkg, 0, "tag", mUid, 0, nbB.build(), UserHandle.getUserHandleForUid(mUid), null, 0),
        NotificationRecord recordB =
                new NotificationRecord(
                        mContext,
                        new StatusBarNotification(
                                mPkg,
                                mPkg,
                                0,
                                "tag",
                                mUid,
                                0,
                                nbB.build(),
                                UserHandle.getUserHandleForUid(mUid),
                                null,
                                0),
                        c);
        // Update means we drop access to first
@@ -15400,8 +15423,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS; i++) {
            StatusBarNotification sbn = generateNotificationRecord(mTestNotificationChannel,
                    i, null, false).getSbn();
            mBinderService.enqueueNotificationWithTag(mPkg, mPkg, "testCannotPostNonUijWhenOverLimit",
                    sbn.getId(), sbn.getNotification(), sbn.getUserId());
            mBinderService.enqueueNotificationWithTag(
                    mPkg,
                    mPkg,
                    "testCannotPostNonUijWhenOverLimit",
                    sbn.getId(),
                    sbn.getNotification(),
                    sbn.getUserId());
            waitForIdle();
        }
@@ -16131,6 +16159,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        initNMS(SystemService.PHASE_SYSTEM_SERVICES_READY);
        mInternalService.setDeviceEffectsApplier(mock(DeviceEffectsApplier.class));
        mService.onBootPhase(SystemService.PHASE_THIRD_PARTY_APPS_CAN_START, mMainLooper);
        // No exception!
    }
+22 −5
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@ import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS;
import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
import static android.app.NotificationManager.Policy.STATE_PRIORITY_CHANNELS_BLOCKED;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_CONFIG;
import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_RULES;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.Process.SYSTEM_UID;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
@@ -84,8 +86,6 @@ import static com.android.os.dnd.DNDProtoEnums.PEOPLE_STARRED;
import static com.android.os.dnd.DNDProtoEnums.ROOT_CONFIG;
import static com.android.os.dnd.DNDProtoEnums.STATE_ALLOW;
import static com.android.os.dnd.DNDProtoEnums.STATE_DISALLOW;
import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_CONFIG;
import static android.app.backup.NotificationLoggingConstants.DATA_TYPE_ZEN_RULES;
import static com.android.server.notification.ZenModeEventLogger.ACTIVE_RULE_TYPE_MANUAL;
import static com.android.server.notification.ZenModeHelper.RULE_LIMIT_PER_PACKAGE;

@@ -102,6 +102,7 @@ import static junit.framework.TestCase.fail;

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
@@ -200,6 +201,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.xmlpull.v1.XmlPullParserException;

import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
@@ -219,9 +223,6 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;

@SmallTest
@SuppressLint("GuardedBy") // It's ok for this test to access guarded methods from the service.
@RunWith(ParameterizedAndroidJunit4.class)
@@ -5348,6 +5349,22 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        mTestableLooper.processAllMessages();

        verify(mDeviceEffectsApplier).apply(eq(effects), eq(ORIGIN_APP));
        assertTrue(mZenModeHelper.hasDeviceEffectsApplier());
    }

    @Test
    public void testHasDeviceEffectsApplier_returnsFalseIfNotSet() {
        assertFalse(mZenModeHelper.hasDeviceEffectsApplier());
    }

    @Test
    @EnableFlags(FLAG_MODES_API)
    public void testSettingDeviceEffects_throwsExceptionIfAlreadySet() {
        mZenModeHelper.setDeviceEffectsApplier(mDeviceEffectsApplier);

        assertThrows(
                IllegalStateException.class,
                () -> mZenModeHelper.setDeviceEffectsApplier(mDeviceEffectsApplier));
    }

    @Test