Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view.textclassifier; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.app.Person; |
| 21 | import android.text.TextUtils; |
| 22 | import android.util.ArrayMap; |
| 23 | |
| 24 | import com.android.internal.annotations.VisibleForTesting; |
| 25 | |
| 26 | import com.google.android.textclassifier.ActionsSuggestionsModel; |
| 27 | |
| 28 | import java.util.ArrayDeque; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.Deque; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | /** |
| 36 | * Helper class for action suggestions. |
| 37 | * |
| 38 | * @hide |
| 39 | */ |
| 40 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 41 | public final class ActionsSuggestionsHelper { |
| 42 | private static final int USER_LOCAL = 0; |
| 43 | private static final int FIRST_NON_LOCAL_USER = 1; |
| 44 | |
| 45 | private ActionsSuggestionsHelper() {} |
| 46 | |
| 47 | /** |
| 48 | * Converts the messages to a list of native messages object that the model can understand. |
| 49 | * <p> |
| 50 | * User id encoding - local user is represented as 0, Other users are numbered according to |
| 51 | * how far before they spoke last time in the conversation. For example, considering this |
| 52 | * conversation: |
| 53 | * <ul> |
| 54 | * <li> User A: xxx |
| 55 | * <li> Local user: yyy |
| 56 | * <li> User B: zzz |
| 57 | * </ul> |
| 58 | * User A will be encoded as 2, user B will be encoded as 1 and local user will be encoded as 0. |
| 59 | */ |
| 60 | @NonNull |
| 61 | public static ActionsSuggestionsModel.ConversationMessage[] toNativeMessages( |
| 62 | @NonNull List<ConversationActions.Message> messages) { |
| 63 | List<ConversationActions.Message> messagesWithText = |
| 64 | messages.stream() |
| 65 | .filter(message -> !TextUtils.isEmpty(message.getText())) |
| 66 | .collect(Collectors.toCollection(ArrayList::new)); |
| 67 | if (messagesWithText.isEmpty()) { |
| 68 | return new ActionsSuggestionsModel.ConversationMessage[0]; |
| 69 | } |
| 70 | int size = messagesWithText.size(); |
| 71 | // If the last message (the most important one) does not have the Person object, we will |
| 72 | // just use the last message and consider this message is sent from a remote user. |
| 73 | ConversationActions.Message lastMessage = messages.get(size - 1); |
| 74 | boolean useLastMessageOnly = lastMessage.getAuthor() == null; |
| 75 | if (useLastMessageOnly) { |
| 76 | return new ActionsSuggestionsModel.ConversationMessage[]{ |
| 77 | new ActionsSuggestionsModel.ConversationMessage( |
| 78 | FIRST_NON_LOCAL_USER, |
| 79 | lastMessage.getText().toString())}; |
| 80 | } |
| 81 | |
| 82 | // Encode the messages in the reverse order, stop whenever the Person object is missing. |
| 83 | Deque<ActionsSuggestionsModel.ConversationMessage> nativeMessages = new ArrayDeque<>(); |
| 84 | PersonEncoder personEncoder = new PersonEncoder(); |
| 85 | for (int i = size - 1; i >= 0; i--) { |
| 86 | ConversationActions.Message message = messagesWithText.get(i); |
| 87 | if (message.getAuthor() == null) { |
| 88 | break; |
| 89 | } |
| 90 | nativeMessages.push(new ActionsSuggestionsModel.ConversationMessage( |
| 91 | personEncoder.encode(message.getAuthor()), |
| 92 | message.getText().toString())); |
| 93 | } |
| 94 | return nativeMessages.toArray( |
| 95 | new ActionsSuggestionsModel.ConversationMessage[nativeMessages.size()]); |
| 96 | } |
| 97 | |
| 98 | private static final class PersonEncoder { |
| 99 | private final Map<Person, Integer> mMapping = new ArrayMap<>(); |
| 100 | private int mNextUserId = FIRST_NON_LOCAL_USER; |
| 101 | |
| 102 | private int encode(Person person) { |
| 103 | if (ConversationActions.Message.PERSON_USER_LOCAL.equals(person)) { |
| 104 | return USER_LOCAL; |
| 105 | } |
| 106 | Integer result = mMapping.get(person); |
| 107 | if (result == null) { |
| 108 | mMapping.put(person, mNextUserId); |
| 109 | result = mNextUserId; |
| 110 | mNextUserId++; |
| 111 | } |
| 112 | return result; |
| 113 | } |
| 114 | } |
| 115 | } |