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 | |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 19 | import android.app.Person; |
| 20 | import android.text.TextUtils; |
| 21 | import android.util.ArrayMap; |
| 22 | |
| 23 | import com.android.internal.annotations.VisibleForTesting; |
| 24 | |
| 25 | import com.google.android.textclassifier.ActionsSuggestionsModel; |
| 26 | |
| 27 | import java.util.ArrayDeque; |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Deque; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 32 | import java.util.function.Function; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 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 | */ |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 60 | public static ActionsSuggestionsModel.ConversationMessage[] toNativeMessages( |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 61 | List<ConversationActions.Message> messages, |
| 62 | Function<CharSequence, String> languageDetector) { |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 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 | } |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 70 | Deque<ActionsSuggestionsModel.ConversationMessage> nativeMessages = new ArrayDeque<>(); |
| 71 | PersonEncoder personEncoder = new PersonEncoder(); |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 72 | int size = messagesWithText.size(); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 73 | for (int i = size - 1; i >= 0; i--) { |
| 74 | ConversationActions.Message message = messagesWithText.get(i); |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 75 | long referenceTime = message.getReferenceTime() == null |
| 76 | ? 0 |
| 77 | : message.getReferenceTime().toInstant().toEpochMilli(); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 78 | nativeMessages.push(new ActionsSuggestionsModel.ConversationMessage( |
| 79 | personEncoder.encode(message.getAuthor()), |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 80 | message.getText().toString(), referenceTime, |
| 81 | languageDetector.apply(message.getText()))); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 82 | } |
| 83 | return nativeMessages.toArray( |
| 84 | new ActionsSuggestionsModel.ConversationMessage[nativeMessages.size()]); |
| 85 | } |
| 86 | |
| 87 | private static final class PersonEncoder { |
| 88 | private final Map<Person, Integer> mMapping = new ArrayMap<>(); |
| 89 | private int mNextUserId = FIRST_NON_LOCAL_USER; |
| 90 | |
| 91 | private int encode(Person person) { |
| 92 | if (ConversationActions.Message.PERSON_USER_LOCAL.equals(person)) { |
| 93 | return USER_LOCAL; |
| 94 | } |
| 95 | Integer result = mMapping.get(person); |
| 96 | if (result == null) { |
| 97 | mMapping.put(person, mNextUserId); |
| 98 | result = mNextUserId; |
| 99 | mNextUserId++; |
| 100 | } |
| 101 | return result; |
| 102 | } |
| 103 | } |
| 104 | } |