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