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