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