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 | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 20 | import android.app.Person; |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame^] | 21 | import android.app.RemoteAction; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 22 | import android.content.Context; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 23 | import android.text.TextUtils; |
| 24 | import android.util.ArrayMap; |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame^] | 25 | import android.util.Pair; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 26 | |
| 27 | import com.android.internal.annotations.VisibleForTesting; |
| 28 | |
| 29 | import com.google.android.textclassifier.ActionsSuggestionsModel; |
| 30 | |
| 31 | import java.util.ArrayDeque; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Deque; |
| 34 | import java.util.List; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 35 | import java.util.Locale; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 36 | import java.util.Map; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 37 | import java.util.Objects; |
| 38 | import java.util.StringJoiner; |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 39 | import java.util.function.Function; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | /** |
| 43 | * Helper class for action suggestions. |
| 44 | * |
| 45 | * @hide |
| 46 | */ |
| 47 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 48 | public final class ActionsSuggestionsHelper { |
| 49 | private static final int USER_LOCAL = 0; |
| 50 | private static final int FIRST_NON_LOCAL_USER = 1; |
| 51 | |
| 52 | private ActionsSuggestionsHelper() {} |
| 53 | |
| 54 | /** |
| 55 | * Converts the messages to a list of native messages object that the model can understand. |
| 56 | * <p> |
| 57 | * User id encoding - local user is represented as 0, Other users are numbered according to |
| 58 | * how far before they spoke last time in the conversation. For example, considering this |
| 59 | * conversation: |
| 60 | * <ul> |
| 61 | * <li> User A: xxx |
| 62 | * <li> Local user: yyy |
| 63 | * <li> User B: zzz |
| 64 | * </ul> |
| 65 | * User A will be encoded as 2, user B will be encoded as 1 and local user will be encoded as 0. |
| 66 | */ |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 67 | public static ActionsSuggestionsModel.ConversationMessage[] toNativeMessages( |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 68 | List<ConversationActions.Message> messages, |
| 69 | Function<CharSequence, String> languageDetector) { |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 70 | List<ConversationActions.Message> messagesWithText = |
| 71 | messages.stream() |
| 72 | .filter(message -> !TextUtils.isEmpty(message.getText())) |
| 73 | .collect(Collectors.toCollection(ArrayList::new)); |
| 74 | if (messagesWithText.isEmpty()) { |
| 75 | return new ActionsSuggestionsModel.ConversationMessage[0]; |
| 76 | } |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 77 | Deque<ActionsSuggestionsModel.ConversationMessage> nativeMessages = new ArrayDeque<>(); |
| 78 | PersonEncoder personEncoder = new PersonEncoder(); |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 79 | int size = messagesWithText.size(); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 80 | for (int i = size - 1; i >= 0; i--) { |
| 81 | ConversationActions.Message message = messagesWithText.get(i); |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 82 | long referenceTime = message.getReferenceTime() == null |
| 83 | ? 0 |
| 84 | : message.getReferenceTime().toInstant().toEpochMilli(); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 85 | nativeMessages.push(new ActionsSuggestionsModel.ConversationMessage( |
| 86 | personEncoder.encode(message.getAuthor()), |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 87 | message.getText().toString(), referenceTime, |
| 88 | languageDetector.apply(message.getText()))); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 89 | } |
| 90 | return nativeMessages.toArray( |
| 91 | new ActionsSuggestionsModel.ConversationMessage[nativeMessages.size()]); |
| 92 | } |
| 93 | |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 94 | /** |
| 95 | * Returns the result id for logging. |
| 96 | */ |
| 97 | public static String createResultId( |
| 98 | Context context, |
| 99 | List<ConversationActions.Message> messages, |
| 100 | int modelVersion, |
| 101 | List<Locale> modelLocales) { |
| 102 | final StringJoiner localesJoiner = new StringJoiner(","); |
| 103 | for (Locale locale : modelLocales) { |
| 104 | localesJoiner.add(locale.toLanguageTag()); |
| 105 | } |
| 106 | final String modelName = String.format( |
| 107 | Locale.US, "%s_v%d", localesJoiner.toString(), modelVersion); |
| 108 | final int hash = Objects.hash( |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 109 | messages.stream().mapToInt(ActionsSuggestionsHelper::hashMessage), |
| 110 | context.getPackageName(), |
| 111 | System.currentTimeMillis()); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 112 | return SelectionSessionLogger.SignatureParser.createSignature( |
| 113 | SelectionSessionLogger.CLASSIFIER_ID, modelName, hash); |
| 114 | } |
| 115 | |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 116 | /** |
| 117 | * Returns a {@link android.view.textclassifier.LabeledIntent.TitleChooser} for |
| 118 | * conversation actions use case. |
| 119 | */ |
| 120 | @Nullable |
| 121 | public static LabeledIntent.TitleChooser createTitleChooser(String actionType) { |
| 122 | if (ConversationAction.TYPE_OPEN_URL.equals(actionType)) { |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame^] | 123 | return (labeledIntent, resolveInfo) -> { |
| 124 | if (resolveInfo.handleAllWebDataURI) { |
| 125 | return labeledIntent.titleWithEntity; |
| 126 | } |
| 127 | if ("android".equals(resolveInfo.activityInfo.packageName)) { |
| 128 | return labeledIntent.titleWithEntity; |
| 129 | } |
| 130 | return labeledIntent.titleWithoutEntity; |
| 131 | }; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 132 | } |
| 133 | return null; |
| 134 | } |
| 135 | |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame^] | 136 | /** |
| 137 | * Returns a list of {@link ConversationAction}s that have 0 duplicates. Two actions are |
| 138 | * duplicates if they may look the same to users. This function assumes every |
| 139 | * ConversationActions with a non-null RemoteAction also have a non-null intent in the extras. |
| 140 | */ |
| 141 | public static List<ConversationAction> removeActionsWithDuplicates( |
| 142 | List<ConversationAction> conversationActions) { |
| 143 | // Ideally, we should compare title and icon here, but comparing icon is expensive and thus |
| 144 | // we use the component name of the target handler as the heuristic. |
| 145 | Map<Pair<String, String>, Integer> counter = new ArrayMap<>(); |
| 146 | for (ConversationAction conversationAction : conversationActions) { |
| 147 | Pair<String, String> representation = getRepresentation(conversationAction); |
| 148 | if (representation == null) { |
| 149 | continue; |
| 150 | } |
| 151 | Integer existingCount = counter.getOrDefault(representation, 0); |
| 152 | counter.put(representation, existingCount + 1); |
| 153 | } |
| 154 | List<ConversationAction> result = new ArrayList<>(); |
| 155 | for (ConversationAction conversationAction : conversationActions) { |
| 156 | Pair<String, String> representation = getRepresentation(conversationAction); |
| 157 | if (representation == null || counter.getOrDefault(representation, 0) == 1) { |
| 158 | result.add(conversationAction); |
| 159 | } |
| 160 | } |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | @Nullable |
| 165 | private static Pair<String, String> getRepresentation( |
| 166 | ConversationAction conversationAction) { |
| 167 | RemoteAction remoteAction = conversationAction.getAction(); |
| 168 | if (remoteAction == null) { |
| 169 | return null; |
| 170 | } |
| 171 | return new Pair<>( |
| 172 | conversationAction.getAction().getTitle().toString(), |
| 173 | ExtrasUtils.getActionIntent( |
| 174 | conversationAction.getExtras()).getComponent().getPackageName()); |
| 175 | } |
| 176 | |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 177 | private static final class PersonEncoder { |
| 178 | private final Map<Person, Integer> mMapping = new ArrayMap<>(); |
| 179 | private int mNextUserId = FIRST_NON_LOCAL_USER; |
| 180 | |
| 181 | private int encode(Person person) { |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 182 | if (ConversationActions.Message.PERSON_USER_SELF.equals(person)) { |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 183 | return USER_LOCAL; |
| 184 | } |
| 185 | Integer result = mMapping.get(person); |
| 186 | if (result == null) { |
| 187 | mMapping.put(person, mNextUserId); |
| 188 | result = mNextUserId; |
| 189 | mNextUserId++; |
| 190 | } |
| 191 | return result; |
| 192 | } |
| 193 | } |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 194 | |
| 195 | private static int hashMessage(ConversationActions.Message message) { |
| 196 | return Objects.hash(message.getAuthor(), message.getText(), message.getReferenceTime()); |
| 197 | } |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 198 | } |