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