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

Commit 76894652 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Do not generate smart suggestions if the last message is from local user"

parents e83f72e1 7ab83089
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class SmartActionsHelper {
@@ -121,6 +122,12 @@ public class SmartActionsHelper {
        if (messages.isEmpty()) {
            return Collections.emptyList();
        }
        // Do not generate smart actions if the last message is from the local user.
        ConversationActions.Message lastMessage = messages.get(messages.size() - 1);
        if (arePersonsEqual(
                ConversationActions.Message.PERSON_USER_SELF, lastMessage.getAuthor())) {
            return Collections.emptyList();
        }

        TextClassifier.EntityConfig.Builder typeConfigBuilder =
                new TextClassifier.EntityConfig.Builder();
@@ -316,13 +323,12 @@ public class SmartActionsHelper {
            if (message == null) {
                continue;
            }
            // As per the javadoc of Notification.addMessage, null means local user.
            Person senderPerson = message.getSenderPerson();
            // Skip encoding once the sender is missing as it is important to distinguish
            // local user and remote user when generating replies.
            if (senderPerson == null) {
                break;
                senderPerson = localUser;
            }
            Person author = localUser != null && localUser.equals(senderPerson)
            Person author = localUser != null && arePersonsEqual(localUser, senderPerson)
                    ? ConversationActions.Message.PERSON_USER_SELF : senderPerson;
            extractMessages.push(new ConversationActions.Message.Builder(author)
                    .setText(message.getText())
@@ -337,6 +343,12 @@ public class SmartActionsHelper {
        return new ArrayList<>(extractMessages);
    }

    private static boolean arePersonsEqual(@NonNull Person left, @NonNull Person right) {
        return Objects.equals(left.getKey(), right.getKey())
                && Objects.equals(left.getName(), right.getName())
                && Objects.equals(left.getUri(), right.getUri());
    }

    static class SmartSuggestions {
        public final ArrayList<CharSequence> replies;
        public final ArrayList<Notification.Action> actions;
+33 −4
Original line number Diff line number Diff line
@@ -222,28 +222,57 @@ public class SmartActionsHelperTest {

        List<ConversationActions.Message> messages =
                runSuggestAndCaptureRequest().getConversation();
        assertThat(messages).hasSize(3);
        assertThat(messages).hasSize(4);

        ConversationActions.Message secondMessage = messages.get(0);
        ConversationActions.Message firstMessage = messages.get(0);
        MessageSubject.assertThat(firstMessage).hasText("firstMessage");
        MessageSubject.assertThat(firstMessage)
                .hasPerson(ConversationActions.Message.PERSON_USER_SELF);
        MessageSubject.assertThat(firstMessage)
                .hasReferenceTime(createZonedDateTimeFromMsUtc(1000));

        ConversationActions.Message secondMessage = messages.get(1);
        MessageSubject.assertThat(secondMessage).hasText("secondMessage");
        MessageSubject.assertThat(secondMessage)
                .hasPerson(ConversationActions.Message.PERSON_USER_SELF);
        MessageSubject.assertThat(secondMessage)
                .hasReferenceTime(createZonedDateTimeFromMsUtc(2000));

        ConversationActions.Message thirdMessage = messages.get(1);
        ConversationActions.Message thirdMessage = messages.get(2);
        MessageSubject.assertThat(thirdMessage).hasText("thirdMessage");
        MessageSubject.assertThat(thirdMessage).hasPerson(userA);
        MessageSubject.assertThat(thirdMessage)
                .hasReferenceTime(createZonedDateTimeFromMsUtc(3000));

        ConversationActions.Message fourthMessage = messages.get(2);
        ConversationActions.Message fourthMessage = messages.get(3);
        MessageSubject.assertThat(fourthMessage).hasText("fourthMessage");
        MessageSubject.assertThat(fourthMessage).hasPerson(userB);
        MessageSubject.assertThat(fourthMessage)
                .hasReferenceTime(createZonedDateTimeFromMsUtc(4000));
    }

    @Test
    public void testSuggest_lastMessageLocalUser() {
        Person me = new Person.Builder().setName("Me").build();
        Person userA = new Person.Builder().setName("A").build();
        Notification.MessagingStyle style =
                new Notification.MessagingStyle(me)
                        .addMessage("firstMessage", 1000, userA)
                        .addMessage("secondMessage", 2000, me);
        Notification notification =
                mNotificationBuilder
                        .setContentText("You have two new messages")
                        .setStyle(style)
                        .setActions(createReplyAction())
                        .build();
        when(mStatusBarNotification.getNotification()).thenReturn(notification);

        mSmartActionsHelper.suggest(createNotificationEntry());

        verify(mTextClassifier, never())
                .suggestConversationActions(any(ConversationActions.Request.class));
    }

    @Test
    public void testSuggest_messageStyle_noPerson() {
        Person me = new Person.Builder().setName("Me").build();