Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [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 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.WorkerThread; |
| 22 | import android.content.Context; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 23 | import android.os.Bundle; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 24 | import android.os.Looper; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 25 | import android.os.Parcelable; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 26 | import android.os.RemoteException; |
| 27 | import android.os.ServiceManager; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 28 | import android.service.textclassifier.ITextClassifierCallback; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 29 | import android.service.textclassifier.ITextClassifierService; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 30 | import android.service.textclassifier.TextClassifierService; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 31 | |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 32 | import com.android.internal.annotations.VisibleForTesting; |
| 33 | import com.android.internal.annotations.VisibleForTesting.Visibility; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 34 | import com.android.internal.util.IndentingPrintWriter; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 35 | |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 36 | import java.util.Objects; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 37 | import java.util.concurrent.CountDownLatch; |
| 38 | import java.util.concurrent.TimeUnit; |
| 39 | |
| 40 | /** |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 41 | * proxy to the request to TextClassifierService via the TextClassificationManagerService. |
| 42 | * |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 43 | * @hide |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 44 | */ |
Abodunrinwa Toki | 6563833 | 2018-03-16 21:08:50 +0000 | [diff] [blame] | 45 | @VisibleForTesting(visibility = Visibility.PACKAGE) |
| 46 | public final class SystemTextClassifier implements TextClassifier { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 47 | |
| 48 | private static final String LOG_TAG = "SystemTextClassifier"; |
| 49 | |
| 50 | private final ITextClassifierService mManagerService; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 51 | private final TextClassificationConstants mSettings; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 52 | private final TextClassifier mFallback; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 53 | private TextClassificationSessionId mSessionId; |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 54 | // NOTE: Always set this before sending a request to the manager service otherwise the |
| 55 | // manager service will throw a remote exception. |
| 56 | @NonNull |
| 57 | private final SystemTextClassifierMetadata mSystemTcMetadata; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 58 | |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 59 | /** |
| 60 | * Constructor of {@link SystemTextClassifier} |
| 61 | * |
| 62 | * @param context the context of the request. |
| 63 | * @param settings TextClassifier specific settings. |
| 64 | * @param useDefault whether to use the default text classifier to handle this request |
| 65 | */ |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 66 | public SystemTextClassifier( |
| 67 | Context context, |
| 68 | TextClassificationConstants settings, |
| 69 | boolean useDefault) throws ServiceManager.ServiceNotFoundException { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 70 | mManagerService = ITextClassifierService.Stub.asInterface( |
| 71 | ServiceManager.getServiceOrThrow(Context.TEXT_CLASSIFICATION_SERVICE)); |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 72 | mSettings = Objects.requireNonNull(settings); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 73 | mFallback = TextClassifier.NO_OP; |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 74 | // NOTE: Always set this before sending a request to the manager service otherwise the |
| 75 | // manager service will throw a remote exception. |
| 76 | mSystemTcMetadata = new SystemTextClassifierMetadata( |
| 77 | Objects.requireNonNull(context.getOpPackageName()), context.getUserId(), |
| 78 | useDefault); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @inheritDoc |
| 83 | */ |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 84 | @Override |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 85 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 86 | public TextSelection suggestSelection(TextSelection.Request request) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 87 | Objects.requireNonNull(request); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 88 | Utils.checkMainThread(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 89 | try { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 90 | request.setSystemTextClassifierMetadata(mSystemTcMetadata); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 91 | final BlockingCallback<TextSelection> callback = |
| 92 | new BlockingCallback<>("textselection"); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 93 | mManagerService.onSuggestSelection(mSessionId, request, callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 94 | final TextSelection selection = callback.get(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 95 | if (selection != null) { |
| 96 | return selection; |
| 97 | } |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 98 | } catch (RemoteException e) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 99 | Log.e(LOG_TAG, "Error suggesting selection for text. Using fallback.", e); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 100 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 101 | return mFallback.suggestSelection(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @inheritDoc |
| 106 | */ |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 107 | @Override |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 108 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 109 | public TextClassification classifyText(TextClassification.Request request) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 110 | Objects.requireNonNull(request); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 111 | Utils.checkMainThread(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 112 | try { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 113 | request.setSystemTextClassifierMetadata(mSystemTcMetadata); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 114 | final BlockingCallback<TextClassification> callback = |
| 115 | new BlockingCallback<>("textclassification"); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 116 | mManagerService.onClassifyText(mSessionId, request, callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 117 | final TextClassification classification = callback.get(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 118 | if (classification != null) { |
| 119 | return classification; |
| 120 | } |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 121 | } catch (RemoteException e) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 122 | Log.e(LOG_TAG, "Error classifying text. Using fallback.", e); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 123 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 124 | return mFallback.classifyText(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @inheritDoc |
| 129 | */ |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 130 | @Override |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 131 | @WorkerThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 132 | public TextLinks generateLinks(@NonNull TextLinks.Request request) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 133 | Objects.requireNonNull(request); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 134 | Utils.checkMainThread(); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 135 | if (!Utils.checkTextLength(request.getText(), getMaxGenerateLinksTextLength())) { |
| 136 | return mFallback.generateLinks(request); |
| 137 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 138 | if (!mSettings.isSmartLinkifyEnabled() && request.isLegacyFallback()) { |
| 139 | return Utils.generateLegacyLinks(request); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 142 | try { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 143 | request.setSystemTextClassifierMetadata(mSystemTcMetadata); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 144 | final BlockingCallback<TextLinks> callback = |
| 145 | new BlockingCallback<>("textlinks"); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 146 | mManagerService.onGenerateLinks(mSessionId, request, callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 147 | final TextLinks links = callback.get(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 148 | if (links != null) { |
| 149 | return links; |
| 150 | } |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 151 | } catch (RemoteException e) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 152 | Log.e(LOG_TAG, "Error generating links. Using fallback.", e); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 153 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 154 | return mFallback.generateLinks(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Abodunrinwa Toki | dfd3c65 | 2018-05-08 18:23:10 +0100 | [diff] [blame] | 157 | @Override |
| 158 | public void onSelectionEvent(SelectionEvent event) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 159 | Objects.requireNonNull(event); |
Abodunrinwa Toki | dfd3c65 | 2018-05-08 18:23:10 +0100 | [diff] [blame] | 160 | Utils.checkMainThread(); |
| 161 | |
| 162 | try { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 163 | event.setSystemTextClassifierMetadata(mSystemTcMetadata); |
Abodunrinwa Toki | dfd3c65 | 2018-05-08 18:23:10 +0100 | [diff] [blame] | 164 | mManagerService.onSelectionEvent(mSessionId, event); |
| 165 | } catch (RemoteException e) { |
| 166 | Log.e(LOG_TAG, "Error reporting selection event.", e); |
| 167 | } |
| 168 | } |
| 169 | |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 170 | @Override |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 171 | public void onTextClassifierEvent(@NonNull TextClassifierEvent event) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 172 | Objects.requireNonNull(event); |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 173 | Utils.checkMainThread(); |
| 174 | |
| 175 | try { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 176 | final TextClassificationContext tcContext = |
| 177 | event.getEventContext() == null ? new TextClassificationContext.Builder( |
| 178 | mSystemTcMetadata.getCallingPackageName(), WIDGET_TYPE_UNKNOWN).build() |
| 179 | : event.getEventContext(); |
| 180 | tcContext.setSystemTextClassifierMetadata(mSystemTcMetadata); |
Abodunrinwa Toki | 1480ed1 | 2019-07-15 16:01:51 +0100 | [diff] [blame] | 181 | event.setEventContext(tcContext); |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 182 | mManagerService.onTextClassifierEvent(mSessionId, event); |
| 183 | } catch (RemoteException e) { |
| 184 | Log.e(LOG_TAG, "Error reporting textclassifier event.", e); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | @Override |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 189 | public TextLanguage detectLanguage(TextLanguage.Request request) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 190 | Objects.requireNonNull(request); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 191 | Utils.checkMainThread(); |
| 192 | |
| 193 | try { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 194 | request.setSystemTextClassifierMetadata(mSystemTcMetadata); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 195 | final BlockingCallback<TextLanguage> callback = |
| 196 | new BlockingCallback<>("textlanguage"); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 197 | mManagerService.onDetectLanguage(mSessionId, request, callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 198 | final TextLanguage textLanguage = callback.get(); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 199 | if (textLanguage != null) { |
| 200 | return textLanguage; |
| 201 | } |
| 202 | } catch (RemoteException e) { |
| 203 | Log.e(LOG_TAG, "Error detecting language.", e); |
| 204 | } |
| 205 | return mFallback.detectLanguage(request); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public ConversationActions suggestConversationActions(ConversationActions.Request request) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 210 | Objects.requireNonNull(request); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 211 | Utils.checkMainThread(); |
| 212 | |
| 213 | try { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 214 | request.setSystemTextClassifierMetadata(mSystemTcMetadata); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 215 | final BlockingCallback<ConversationActions> callback = |
| 216 | new BlockingCallback<>("conversation-actions"); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 217 | mManagerService.onSuggestConversationActions(mSessionId, request, callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 218 | final ConversationActions conversationActions = callback.get(); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 219 | if (conversationActions != null) { |
| 220 | return conversationActions; |
| 221 | } |
| 222 | } catch (RemoteException e) { |
| 223 | Log.e(LOG_TAG, "Error reporting selection event.", e); |
| 224 | } |
| 225 | return mFallback.suggestConversationActions(request); |
| 226 | } |
| 227 | |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 228 | /** |
| 229 | * @inheritDoc |
| 230 | */ |
| 231 | @Override |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 232 | @WorkerThread |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 233 | public int getMaxGenerateLinksTextLength() { |
| 234 | // TODO: retrieve this from the bound service. |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 235 | return mSettings.getGenerateLinksMaxTextLength(); |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 238 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 239 | public void destroy() { |
| 240 | try { |
| 241 | if (mSessionId != null) { |
| 242 | mManagerService.onDestroyTextClassificationSession(mSessionId); |
| 243 | } |
| 244 | } catch (RemoteException e) { |
| 245 | Log.e(LOG_TAG, "Error destroying classification session.", e); |
| 246 | } |
| 247 | } |
| 248 | |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 249 | @Override |
| 250 | public void dump(@NonNull IndentingPrintWriter printWriter) { |
| 251 | printWriter.println("SystemTextClassifier:"); |
| 252 | printWriter.increaseIndent(); |
| 253 | printWriter.printPair("mFallback", mFallback); |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 254 | printWriter.printPair("mSessionId", mSessionId); |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 255 | printWriter.printPair("mSystemTcMetadata", mSystemTcMetadata); |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 256 | printWriter.decreaseIndent(); |
| 257 | printWriter.println(); |
| 258 | } |
| 259 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 260 | /** |
| 261 | * Attempts to initialize a new classification session. |
| 262 | * |
| 263 | * @param classificationContext the classification context |
| 264 | * @param sessionId the session's id |
| 265 | */ |
| 266 | void initializeRemoteSession( |
| 267 | @NonNull TextClassificationContext classificationContext, |
| 268 | @NonNull TextClassificationSessionId sessionId) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 269 | mSessionId = Objects.requireNonNull(sessionId); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 270 | try { |
Joanne Chung | 97d3a45 | 2020-03-04 19:03:11 +0800 | [diff] [blame] | 271 | classificationContext.setSystemTextClassifierMetadata(mSystemTcMetadata); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 272 | mManagerService.onCreateTextClassificationSession(classificationContext, mSessionId); |
| 273 | } catch (RemoteException e) { |
| 274 | Log.e(LOG_TAG, "Error starting a new classification session.", e); |
| 275 | } |
| 276 | } |
| 277 | |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 278 | private static final class BlockingCallback<T extends Parcelable> |
| 279 | extends ITextClassifierCallback.Stub { |
| 280 | private final ResponseReceiver<T> mReceiver; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 281 | |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 282 | BlockingCallback(String name) { |
| 283 | mReceiver = new ResponseReceiver<>(name); |
| 284 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 285 | |
| 286 | @Override |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 287 | public void onSuccess(Bundle result) { |
| 288 | mReceiver.onSuccess(TextClassifierService.getResponse(result)); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void onFailure() { |
| 293 | mReceiver.onFailure(); |
| 294 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 295 | |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 296 | public T get() { |
| 297 | return mReceiver.get(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 302 | private static final class ResponseReceiver<T> { |
| 303 | |
| 304 | private final CountDownLatch mLatch = new CountDownLatch(1); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 305 | private final String mName; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 306 | private T mResponse; |
| 307 | |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 308 | private ResponseReceiver(String name) { |
| 309 | mName = name; |
| 310 | } |
| 311 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 312 | public void onSuccess(T response) { |
| 313 | mResponse = response; |
| 314 | mLatch.countDown(); |
| 315 | } |
| 316 | |
| 317 | public void onFailure() { |
Joanne Chung | 0b7c2c4 | 2019-08-16 16:55:11 +0800 | [diff] [blame] | 318 | Log.e(LOG_TAG, "Request failed at " + mName, null); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 319 | mLatch.countDown(); |
| 320 | } |
| 321 | |
| 322 | @Nullable |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 323 | public T get() { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 324 | // If this is running on the main thread, do not block for a response. |
| 325 | // The response will unfortunately be null and the TextClassifier should depend on its |
| 326 | // fallback. |
| 327 | // NOTE that TextClassifier calls should preferably always be called on a worker thread. |
| 328 | if (Looper.myLooper() != Looper.getMainLooper()) { |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 329 | try { |
| 330 | boolean success = mLatch.await(2, TimeUnit.SECONDS); |
| 331 | if (!success) { |
| 332 | Log.w(LOG_TAG, "Timeout in ResponseReceiver.get(): " + mName); |
| 333 | } |
| 334 | } catch (InterruptedException e) { |
| 335 | Thread.currentThread().interrupt(); |
| 336 | Log.e(LOG_TAG, "Interrupted during ResponseReceiver.get(): " + mName, e); |
| 337 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 338 | } |
| 339 | return mResponse; |
| 340 | } |
| 341 | } |
| 342 | } |