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

Commit 774d5fe0 authored by Mady Mellor's avatar Mady Mellor
Browse files

Ensure that the shortcuts added to conversations are longlived

This was never being checked previously.

Bug: 149925743
Test: atest NotificationManagerServiceTest
Change-Id: I54151d9018a768b8bbc57771ba315bf5d4be2496
parent 6dc855e1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -185,7 +185,7 @@ public class BubbleExtractor implements NotificationSignalExtractor {

            String shortcutId = metadata.getShortcutId();
            boolean shortcutValid = shortcutId != null
                    && mShortcutHelper.hasValidShortcutInfo(shortcutId, pkg, r.getUser());
                    && mShortcutHelper.getValidShortcutInfo(shortcutId, pkg, r.getUser()) != null;
            if (metadata.getIntent() == null && !shortcutValid) {
                // Should have a shortcut if intent is null
                logBubbleError(r.getKey(),
+4 −3
Original line number Diff line number Diff line
@@ -3453,7 +3453,7 @@ public class NotificationManagerService extends SystemService {
            ArrayList<ConversationChannelWrapper> conversations =
                    mPreferencesHelper.getConversations(onlyImportant);
            for (ConversationChannelWrapper conversation : conversations) {
                conversation.setShortcutInfo(mShortcutHelper.getShortcutInfo(
                conversation.setShortcutInfo(mShortcutHelper.getValidShortcutInfo(
                        conversation.getNotificationChannel().getConversationId(),
                        conversation.getPkg(),
                        UserHandle.of(UserHandle.getUserId(conversation.getUid()))));
@@ -3476,7 +3476,7 @@ public class NotificationManagerService extends SystemService {
            ArrayList<ConversationChannelWrapper> conversations =
                    mPreferencesHelper.getConversations(pkg, uid);
            for (ConversationChannelWrapper conversation : conversations) {
                conversation.setShortcutInfo(mShortcutHelper.getShortcutInfo(
                conversation.setShortcutInfo(mShortcutHelper.getValidShortcutInfo(
                        conversation.getNotificationChannel().getConversationId(),
                        pkg,
                        UserHandle.of(UserHandle.getUserId(uid))));
@@ -5647,7 +5647,8 @@ public class NotificationManagerService extends SystemService {
            }
        }

        r.setShortcutInfo(mShortcutHelper.getShortcutInfo(notification.getShortcutId(), pkg, user));
        r.setShortcutInfo(mShortcutHelper.getValidShortcutInfo(
                notification.getShortcutId(), pkg, user));

        if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r,
                r.getSbn().getOverrideGroupKey() != null)) {
+6 −8
Original line number Diff line number Diff line
@@ -121,7 +121,10 @@ class ShortcutHelper {
        mLauncherAppsService = launcherApps;
    }

    ShortcutInfo getShortcutInfo(String shortcutId, String packageName, UserHandle user) {
    /**
     * Only returns shortcut info if it's found and if it's {@link ShortcutInfo#isLongLived()}.
     */
    ShortcutInfo getValidShortcutInfo(String shortcutId, String packageName, UserHandle user) {
        if (mLauncherAppsService == null) {
            return null;
        }
@@ -135,20 +138,15 @@ class ShortcutHelper {
            query.setShortcutIds(Arrays.asList(shortcutId));
            query.setQueryFlags(FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_CACHED);
            List<ShortcutInfo> shortcuts = mLauncherAppsService.getShortcuts(query, user);
            return shortcuts != null && shortcuts.size() > 0
            ShortcutInfo info = shortcuts != null && shortcuts.size() > 0
                    ? shortcuts.get(0)
                    : null;
            return info != null && info.isLongLived() ? info : null;
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    boolean hasValidShortcutInfo(String shortcutId, String packageName,
            UserHandle user) {
        ShortcutInfo shortcutInfo = getShortcutInfo(shortcutId, packageName, user);
        return shortcutInfo != null && shortcutInfo.isLongLived();
    }

    /**
     * Shortcut based bubbles require some extra work to listen for shortcut changes.
     *
+6 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.app.ActivityManager;
@@ -33,6 +34,7 @@ import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ShortcutInfo;
import android.os.UserHandle;
import android.service.notification.StatusBarNotification;
import android.test.suitebuilder.annotation.SmallTest;
@@ -110,8 +112,10 @@ public class BubbleCheckerTest extends UiServiceTestCase {

    void setUpShortcutBubble(boolean isValid) {
        when(mBubbleMetadata.getShortcutId()).thenReturn(SHORTCUT_ID);
        when(mShortcutHelper.hasValidShortcutInfo(SHORTCUT_ID, PKG, mUserHandle))
                .thenReturn(isValid);
        ShortcutInfo info = mock(ShortcutInfo.class);
        when(info.getId()).thenReturn(SHORTCUT_ID);
        when(mShortcutHelper.getValidShortcutInfo(SHORTCUT_ID, PKG, mUserHandle))
                .thenReturn(isValid ? info : null);
        when(mBubbleMetadata.getIntent()).thenReturn(null);
    }

+28 −0
Original line number Diff line number Diff line
@@ -6372,13 +6372,41 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {

        ShortcutInfo si = mock(ShortcutInfo.class);
        when(si.getShortLabel()).thenReturn("Hello");
        when(si.isLongLived()).thenReturn(true);
        when(mLauncherApps.getShortcuts(any(), any())).thenReturn(Arrays.asList(si));

        List<ConversationChannelWrapper> conversations =
                mBinderService.getConversationsForPackage(PKG_P, mUid).getList();
        assertEquals(si, conversations.get(0).getShortcutInfo());
        assertEquals(si, conversations.get(1).getShortcutInfo());
    }

    @Test
    public void testGetConversationsForPackage_shortcut_notLongLived() throws Exception {
        mService.setPreferencesHelper(mPreferencesHelper);
        ArrayList<ConversationChannelWrapper> convos = new ArrayList<>();
        ConversationChannelWrapper convo1 = new ConversationChannelWrapper();
        NotificationChannel channel1 = new NotificationChannel("a", "a", 1);
        channel1.setConversationId("parent1", "convo 1");
        convo1.setNotificationChannel(channel1);
        convos.add(convo1);

        ConversationChannelWrapper convo2 = new ConversationChannelWrapper();
        NotificationChannel channel2 = new NotificationChannel("b", "b", 1);
        channel2.setConversationId("parent1", "convo 2");
        convo2.setNotificationChannel(channel2);
        convos.add(convo2);
        when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos);

        ShortcutInfo si = mock(ShortcutInfo.class);
        when(si.getShortLabel()).thenReturn("Hello");
        when(si.isLongLived()).thenReturn(false);
        when(mLauncherApps.getShortcuts(any(), any())).thenReturn(Arrays.asList(si));

        List<ConversationChannelWrapper> conversations =
                mBinderService.getConversationsForPackage(PKG_P, mUid).getList();
        assertNull(conversations.get(0).getShortcutInfo());
        assertNull(conversations.get(1).getShortcutInfo());
    }

    @Test