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 | 159f028 | 2019-03-01 14:03:25 +0000 | [diff] [blame^] | 85 | String timeZone = message.getReferenceTime() == null |
| 86 | ? null |
| 87 | : message.getReferenceTime().getZone().getId(); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 88 | nativeMessages.push(new ActionsSuggestionsModel.ConversationMessage( |
| 89 | personEncoder.encode(message.getAuthor()), |
Tony Mak | 159f028 | 2019-03-01 14:03:25 +0000 | [diff] [blame^] | 90 | message.getText().toString(), referenceTime, timeZone, |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 91 | languageDetector.apply(message.getText()))); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 92 | } |
| 93 | return nativeMessages.toArray( |
| 94 | new ActionsSuggestionsModel.ConversationMessage[nativeMessages.size()]); |
| 95 | } |
| 96 | |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 97 | /** |
| 98 | * Returns the result id for logging. |
| 99 | */ |
| 100 | public static String createResultId( |
| 101 | Context context, |
| 102 | List<ConversationActions.Message> messages, |
| 103 | int modelVersion, |
| 104 | List<Locale> modelLocales) { |
| 105 | final StringJoiner localesJoiner = new StringJoiner(","); |
| 106 | for (Locale locale : modelLocales) { |
| 107 | localesJoiner.add(locale.toLanguageTag()); |
| 108 | } |
| 109 | final String modelName = String.format( |
| 110 | Locale.US, "%s_v%d", localesJoiner.toString(), modelVersion); |
| 111 | final int hash = Objects.hash( |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 112 | messages.stream().mapToInt(ActionsSuggestionsHelper::hashMessage), |
| 113 | context.getPackageName(), |
| 114 | System.currentTimeMillis()); |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame] | 115 | return SelectionSessionLogger.SignatureParser.createSignature( |
| 116 | SelectionSessionLogger.CLASSIFIER_ID, modelName, hash); |
| 117 | } |
| 118 | |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 119 | /** |
| 120 | * Returns a {@link android.view.textclassifier.LabeledIntent.TitleChooser} for |
| 121 | * conversation actions use case. |
| 122 | */ |
| 123 | @Nullable |
| 124 | public static LabeledIntent.TitleChooser createTitleChooser(String actionType) { |
| 125 | if (ConversationAction.TYPE_OPEN_URL.equals(actionType)) { |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 126 | return (labeledIntent, resolveInfo) -> { |
| 127 | if (resolveInfo.handleAllWebDataURI) { |
| 128 | return labeledIntent.titleWithEntity; |
| 129 | } |
| 130 | if ("android".equals(resolveInfo.activityInfo.packageName)) { |
| 131 | return labeledIntent.titleWithEntity; |
| 132 | } |
| 133 | return labeledIntent.titleWithoutEntity; |
| 134 | }; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 135 | } |
| 136 | return null; |
| 137 | } |
| 138 | |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Returns a list of {@link ConversationAction}s that have 0 duplicates. Two actions are |
| 141 | * duplicates if they may look the same to users. This function assumes every |
| 142 | * ConversationActions with a non-null RemoteAction also have a non-null intent in the extras. |
| 143 | */ |
| 144 | public static List<ConversationAction> removeActionsWithDuplicates( |
| 145 | List<ConversationAction> conversationActions) { |
| 146 | // Ideally, we should compare title and icon here, but comparing icon is expensive and thus |
| 147 | // we use the component name of the target handler as the heuristic. |
| 148 | Map<Pair<String, String>, Integer> counter = new ArrayMap<>(); |
| 149 | for (ConversationAction conversationAction : conversationActions) { |
| 150 | Pair<String, String> representation = getRepresentation(conversationAction); |
| 151 | if (representation == null) { |
| 152 | continue; |
| 153 | } |
| 154 | Integer existingCount = counter.getOrDefault(representation, 0); |
| 155 | counter.put(representation, existingCount + 1); |
| 156 | } |
| 157 | List<ConversationAction> result = new ArrayList<>(); |
| 158 | for (ConversationAction conversationAction : conversationActions) { |
| 159 | Pair<String, String> representation = getRepresentation(conversationAction); |
| 160 | if (representation == null || counter.getOrDefault(representation, 0) == 1) { |
| 161 | result.add(conversationAction); |
| 162 | } |
| 163 | } |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | @Nullable |
| 168 | private static Pair<String, String> getRepresentation( |
| 169 | ConversationAction conversationAction) { |
| 170 | RemoteAction remoteAction = conversationAction.getAction(); |
| 171 | if (remoteAction == null) { |
| 172 | return null; |
| 173 | } |
| 174 | return new Pair<>( |
| 175 | conversationAction.getAction().getTitle().toString(), |
| 176 | ExtrasUtils.getActionIntent( |
| 177 | conversationAction.getExtras()).getComponent().getPackageName()); |
| 178 | } |
| 179 | |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 180 | private static final class PersonEncoder { |
| 181 | private final Map<Person, Integer> mMapping = new ArrayMap<>(); |
| 182 | private int mNextUserId = FIRST_NON_LOCAL_USER; |
| 183 | |
| 184 | private int encode(Person person) { |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 185 | if (ConversationActions.Message.PERSON_USER_SELF.equals(person)) { |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 186 | return USER_LOCAL; |
| 187 | } |
| 188 | Integer result = mMapping.get(person); |
| 189 | if (result == null) { |
| 190 | mMapping.put(person, mNextUserId); |
| 191 | result = mNextUserId; |
| 192 | mNextUserId++; |
| 193 | } |
| 194 | return result; |
| 195 | } |
| 196 | } |
Tony Mak | 03a1d03 | 2019-01-24 15:12:00 +0000 | [diff] [blame] | 197 | |
| 198 | private static int hashMessage(ConversationActions.Message message) { |
| 199 | return Objects.hash(message.getAuthor(), message.getText(), message.getReferenceTime()); |
| 200 | } |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 201 | } |