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.service.textclassifier; |
| 18 | |
| 19 | import android.Manifest; |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 21 | import android.annotation.MainThread; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
| 24 | import android.annotation.SystemApi; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 26 | import android.app.Service; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
Makoto Onuki | 700feef | 2018-02-15 10:59:41 -0800 | [diff] [blame] | 30 | import android.content.pm.ResolveInfo; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 31 | import android.content.pm.ServiceInfo; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 32 | import android.os.Bundle; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 33 | import android.os.CancellationSignal; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 34 | import android.os.Handler; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 35 | import android.os.IBinder; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 36 | import android.os.Looper; |
| 37 | import android.os.Parcelable; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 38 | import android.os.RemoteException; |
| 39 | import android.text.TextUtils; |
| 40 | import android.util.Slog; |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 41 | import android.view.textclassifier.ConversationActions; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 42 | import android.view.textclassifier.SelectionEvent; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 43 | import android.view.textclassifier.TextClassification; |
Tony Mak | df5d4bf | 2020-02-05 11:39:51 +0000 | [diff] [blame] | 44 | import android.view.textclassifier.TextClassificationConstants; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 45 | import android.view.textclassifier.TextClassificationContext; |
Abodunrinwa Toki | e6d974a | 2018-03-06 18:18:30 +0000 | [diff] [blame] | 46 | import android.view.textclassifier.TextClassificationManager; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 47 | import android.view.textclassifier.TextClassificationSessionId; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 48 | import android.view.textclassifier.TextClassifier; |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 49 | import android.view.textclassifier.TextClassifierEvent; |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 50 | import android.view.textclassifier.TextLanguage; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 51 | import android.view.textclassifier.TextLinks; |
| 52 | import android.view.textclassifier.TextSelection; |
| 53 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 54 | import com.android.internal.util.Preconditions; |
| 55 | |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 56 | import java.lang.annotation.Retention; |
| 57 | import java.lang.annotation.RetentionPolicy; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.Executors; |
| 60 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Abstract base class for the TextClassifier service. |
| 63 | * |
| 64 | * <p>A TextClassifier service provides text classification related features for the system. |
Nikita Dubrovsky | 942db10 | 2019-09-24 10:49:19 -0700 | [diff] [blame] | 65 | * The system's default TextClassifierService provider is configured in |
| 66 | * {@code config_defaultTextClassifierPackage}. If this config has no value, a |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 67 | * {@link android.view.textclassifier.TextClassifierImpl} is loaded in the calling app's process. |
| 68 | * |
| 69 | * <p>See: {@link TextClassifier}. |
Abodunrinwa Toki | e6d974a | 2018-03-06 18:18:30 +0000 | [diff] [blame] | 70 | * See: {@link TextClassificationManager}. |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 71 | * |
| 72 | * <p>Include the following in the manifest: |
| 73 | * |
| 74 | * <pre> |
| 75 | * {@literal |
| 76 | * <service android:name=".YourTextClassifierService" |
| 77 | * android:permission="android.permission.BIND_TEXTCLASSIFIER_SERVICE"> |
| 78 | * <intent-filter> |
| 79 | * <action android:name="android.service.textclassifier.TextClassifierService" /> |
| 80 | * </intent-filter> |
| 81 | * </service>}</pre> |
| 82 | * |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 83 | * <p>From {@link android.os.Build.VERSION_CODES#Q} onward, all callbacks are called on the main |
| 84 | * thread. Prior to Q, there is no guarantee on what thread the callback will happen. You should |
| 85 | * make sure the callbacks are executed in your desired thread by using a executor, a handler or |
| 86 | * something else along the line. |
| 87 | * |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 88 | * @see TextClassifier |
| 89 | * @hide |
| 90 | */ |
| 91 | @SystemApi |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 92 | @TestApi |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 93 | public abstract class TextClassifierService extends Service { |
| 94 | |
| 95 | private static final String LOG_TAG = "TextClassifierService"; |
| 96 | |
| 97 | /** |
| 98 | * The {@link Intent} that must be declared as handled by the service. |
| 99 | * To be supported, the service must also require the |
| 100 | * {@link android.Manifest.permission#BIND_TEXTCLASSIFIER_SERVICE} permission so |
| 101 | * that other applications can not abuse it. |
| 102 | */ |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 103 | public static final String SERVICE_INTERFACE = |
| 104 | "android.service.textclassifier.TextClassifierService"; |
| 105 | |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 106 | /** @hide **/ |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 107 | public static final int CONNECTED = 0; |
| 108 | /** @hide **/ |
| 109 | public static final int DISCONNECTED = 1; |
| 110 | /** @hide */ |
| 111 | @IntDef(value = { |
| 112 | CONNECTED, |
| 113 | DISCONNECTED |
| 114 | }) |
| 115 | @Retention(RetentionPolicy.SOURCE) |
| 116 | public @interface ConnectionState{} |
| 117 | |
| 118 | /** @hide **/ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 119 | private static final String KEY_RESULT = "key_result"; |
| 120 | |
| 121 | private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper(), null, true); |
| 122 | private final ExecutorService mSingleThreadExecutor = Executors.newSingleThreadExecutor(); |
| 123 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 124 | private final ITextClassifierService.Stub mBinder = new ITextClassifierService.Stub() { |
| 125 | |
| 126 | // TODO(b/72533911): Implement cancellation signal |
| 127 | @NonNull private final CancellationSignal mCancellationSignal = new CancellationSignal(); |
| 128 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 129 | @Override |
| 130 | public void onSuggestSelection( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 131 | TextClassificationSessionId sessionId, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 132 | TextSelection.Request request, ITextClassifierCallback callback) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 133 | Preconditions.checkNotNull(request); |
| 134 | Preconditions.checkNotNull(callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 135 | mMainThreadHandler.post(() -> TextClassifierService.this.onSuggestSelection( |
| 136 | sessionId, request, mCancellationSignal, new ProxyCallback<>(callback))); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 137 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 140 | @Override |
| 141 | public void onClassifyText( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 142 | TextClassificationSessionId sessionId, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 143 | TextClassification.Request request, ITextClassifierCallback callback) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 144 | Preconditions.checkNotNull(request); |
| 145 | Preconditions.checkNotNull(callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 146 | mMainThreadHandler.post(() -> TextClassifierService.this.onClassifyText( |
| 147 | sessionId, request, mCancellationSignal, new ProxyCallback<>(callback))); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 150 | @Override |
| 151 | public void onGenerateLinks( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 152 | TextClassificationSessionId sessionId, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 153 | TextLinks.Request request, ITextClassifierCallback callback) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 154 | Preconditions.checkNotNull(request); |
| 155 | Preconditions.checkNotNull(callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 156 | mMainThreadHandler.post(() -> TextClassifierService.this.onGenerateLinks( |
| 157 | sessionId, request, mCancellationSignal, new ProxyCallback<>(callback))); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 158 | } |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 159 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 160 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 161 | public void onSelectionEvent( |
| 162 | TextClassificationSessionId sessionId, |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 163 | SelectionEvent event) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 164 | Preconditions.checkNotNull(event); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 165 | mMainThreadHandler.post( |
| 166 | () -> TextClassifierService.this.onSelectionEvent(sessionId, event)); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 169 | @Override |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 170 | public void onTextClassifierEvent( |
| 171 | TextClassificationSessionId sessionId, |
| 172 | TextClassifierEvent event) { |
| 173 | Preconditions.checkNotNull(event); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 174 | mMainThreadHandler.post( |
| 175 | () -> TextClassifierService.this.onTextClassifierEvent(sessionId, event)); |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 178 | @Override |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 179 | public void onDetectLanguage( |
| 180 | TextClassificationSessionId sessionId, |
| 181 | TextLanguage.Request request, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 182 | ITextClassifierCallback callback) { |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 183 | Preconditions.checkNotNull(request); |
| 184 | Preconditions.checkNotNull(callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 185 | mMainThreadHandler.post(() -> TextClassifierService.this.onDetectLanguage( |
| 186 | sessionId, request, mCancellationSignal, new ProxyCallback<>(callback))); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 189 | @Override |
| 190 | public void onSuggestConversationActions( |
| 191 | TextClassificationSessionId sessionId, |
| 192 | ConversationActions.Request request, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 193 | ITextClassifierCallback callback) { |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 194 | Preconditions.checkNotNull(request); |
| 195 | Preconditions.checkNotNull(callback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 196 | mMainThreadHandler.post(() -> TextClassifierService.this.onSuggestConversationActions( |
| 197 | sessionId, request, mCancellationSignal, new ProxyCallback<>(callback))); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 200 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 201 | public void onCreateTextClassificationSession( |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 202 | TextClassificationContext context, TextClassificationSessionId sessionId) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 203 | Preconditions.checkNotNull(context); |
| 204 | Preconditions.checkNotNull(sessionId); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 205 | mMainThreadHandler.post( |
| 206 | () -> TextClassifierService.this.onCreateTextClassificationSession( |
| 207 | context, sessionId)); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 210 | @Override |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 211 | public void onDestroyTextClassificationSession(TextClassificationSessionId sessionId) { |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 212 | mMainThreadHandler.post( |
| 213 | () -> TextClassifierService.this.onDestroyTextClassificationSession(sessionId)); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 214 | } |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 215 | |
| 216 | @Override |
| 217 | public void onConnectedStateChanged(@ConnectionState int connected) { |
| 218 | mMainThreadHandler.post(connected == CONNECTED ? TextClassifierService.this::onConnected |
| 219 | : TextClassifierService.this::onDisconnected); |
| 220 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | @Nullable |
| 224 | @Override |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 225 | public final IBinder onBind(@NonNull Intent intent) { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 226 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
| 227 | return mBinder; |
| 228 | } |
| 229 | return null; |
| 230 | } |
| 231 | |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 232 | @Override |
| 233 | public boolean onUnbind(@NonNull Intent intent) { |
| 234 | onDisconnected(); |
| 235 | return super.onUnbind(intent); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Called when the Android system connects to service. |
| 240 | */ |
| 241 | public void onConnected() { |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Called when the Android system disconnects from the service. |
| 246 | * |
| 247 | * <p> At this point this service may no longer be an active {@link TextClassifierService}. |
| 248 | */ |
| 249 | public void onDisconnected() { |
| 250 | } |
| 251 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 252 | /** |
| 253 | * Returns suggested text selection start and end indices, recognized entity types, and their |
| 254 | * associated confidence scores. The entity types are ordered from highest to lowest scoring. |
| 255 | * |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 256 | * @param sessionId the session id |
| 257 | * @param request the text selection request |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 258 | * @param cancellationSignal object to watch for canceling the current operation |
| 259 | * @param callback the callback to return the result to |
| 260 | */ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 261 | @MainThread |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 262 | public abstract void onSuggestSelection( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 263 | @Nullable TextClassificationSessionId sessionId, |
| 264 | @NonNull TextSelection.Request request, |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 265 | @NonNull CancellationSignal cancellationSignal, |
| 266 | @NonNull Callback<TextSelection> callback); |
| 267 | |
| 268 | /** |
| 269 | * Classifies the specified text and returns a {@link TextClassification} object that can be |
| 270 | * used to generate a widget for handling the classified text. |
| 271 | * |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 272 | * @param sessionId the session id |
| 273 | * @param request the text classification request |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 274 | * @param cancellationSignal object to watch for canceling the current operation |
| 275 | * @param callback the callback to return the result to |
| 276 | */ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 277 | @MainThread |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 278 | public abstract void onClassifyText( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 279 | @Nullable TextClassificationSessionId sessionId, |
| 280 | @NonNull TextClassification.Request request, |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 281 | @NonNull CancellationSignal cancellationSignal, |
| 282 | @NonNull Callback<TextClassification> callback); |
| 283 | |
| 284 | /** |
| 285 | * Generates and returns a {@link TextLinks} that may be applied to the text to annotate it with |
| 286 | * links information. |
| 287 | * |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 288 | * @param sessionId the session id |
| 289 | * @param request the text classification request |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 290 | * @param cancellationSignal object to watch for canceling the current operation |
| 291 | * @param callback the callback to return the result to |
| 292 | */ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 293 | @MainThread |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 294 | public abstract void onGenerateLinks( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 295 | @Nullable TextClassificationSessionId sessionId, |
| 296 | @NonNull TextLinks.Request request, |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 297 | @NonNull CancellationSignal cancellationSignal, |
| 298 | @NonNull Callback<TextLinks> callback); |
| 299 | |
| 300 | /** |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 301 | * Detects and returns the language of the give text. |
| 302 | * |
| 303 | * @param sessionId the session id |
| 304 | * @param request the language detection request |
| 305 | * @param cancellationSignal object to watch for canceling the current operation |
| 306 | * @param callback the callback to return the result to |
| 307 | */ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 308 | @MainThread |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 309 | public void onDetectLanguage( |
| 310 | @Nullable TextClassificationSessionId sessionId, |
| 311 | @NonNull TextLanguage.Request request, |
| 312 | @NonNull CancellationSignal cancellationSignal, |
| 313 | @NonNull Callback<TextLanguage> callback) { |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 314 | mSingleThreadExecutor.submit(() -> |
| 315 | callback.onSuccess(getLocalTextClassifier().detectLanguage(request))); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Suggests and returns a list of actions according to the given conversation. |
| 320 | * |
| 321 | * @param sessionId the session id |
| 322 | * @param request the conversation actions request |
| 323 | * @param cancellationSignal object to watch for canceling the current operation |
| 324 | * @param callback the callback to return the result to |
| 325 | */ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 326 | @MainThread |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 327 | public void onSuggestConversationActions( |
| 328 | @Nullable TextClassificationSessionId sessionId, |
| 329 | @NonNull ConversationActions.Request request, |
| 330 | @NonNull CancellationSignal cancellationSignal, |
| 331 | @NonNull Callback<ConversationActions> callback) { |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 332 | mSingleThreadExecutor.submit(() -> |
| 333 | callback.onSuccess(getLocalTextClassifier().suggestConversationActions(request))); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /** |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 337 | * Writes the selection event. |
| 338 | * This is called when a selection event occurs. e.g. user changed selection; or smart selection |
| 339 | * happened. |
| 340 | * |
| 341 | * <p>The default implementation ignores the event. |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 342 | * |
| 343 | * @param sessionId the session id |
| 344 | * @param event the selection event |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 345 | * @deprecated |
| 346 | * Use {@link #onTextClassifierEvent(TextClassificationSessionId, TextClassifierEvent)} |
| 347 | * instead |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 348 | */ |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 349 | @Deprecated |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 350 | @MainThread |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 351 | public void onSelectionEvent( |
| 352 | @Nullable TextClassificationSessionId sessionId, @NonNull SelectionEvent event) {} |
| 353 | |
| 354 | /** |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 355 | * Writes the TextClassifier event. |
| 356 | * This is called when a TextClassifier event occurs. e.g. user changed selection, |
| 357 | * smart selection happened, or a link was clicked. |
| 358 | * |
| 359 | * <p>The default implementation ignores the event. |
| 360 | * |
| 361 | * @param sessionId the session id |
| 362 | * @param event the TextClassifier event |
| 363 | */ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 364 | @MainThread |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 365 | public void onTextClassifierEvent( |
| 366 | @Nullable TextClassificationSessionId sessionId, @NonNull TextClassifierEvent event) {} |
| 367 | |
| 368 | /** |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 369 | * Creates a new text classification session for the specified context. |
| 370 | * |
| 371 | * @param context the text classification context |
| 372 | * @param sessionId the session's Id |
| 373 | */ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 374 | @MainThread |
Jan Althaus | 39ccc7e | 2018-04-04 13:56:40 +0200 | [diff] [blame] | 375 | public void onCreateTextClassificationSession( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 376 | @NonNull TextClassificationContext context, |
Jan Althaus | 39ccc7e | 2018-04-04 13:56:40 +0200 | [diff] [blame] | 377 | @NonNull TextClassificationSessionId sessionId) {} |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * Destroys the text classification session identified by the specified sessionId. |
| 381 | * |
| 382 | * @param sessionId the id of the session to destroy |
| 383 | */ |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 384 | @MainThread |
Jan Althaus | 39ccc7e | 2018-04-04 13:56:40 +0200 | [diff] [blame] | 385 | public void onDestroyTextClassificationSession( |
| 386 | @NonNull TextClassificationSessionId sessionId) {} |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 387 | |
| 388 | /** |
Abodunrinwa Toki | e6d974a | 2018-03-06 18:18:30 +0000 | [diff] [blame] | 389 | * Returns a TextClassifier that runs in this service's process. |
| 390 | * If the local TextClassifier is disabled, this returns {@link TextClassifier#NO_OP}. |
Abodunrinwa Toki | 324572e | 2019-02-12 19:01:01 +0000 | [diff] [blame] | 391 | * |
| 392 | * @deprecated Use {@link #getDefaultTextClassifierImplementation(Context)} instead. |
Abodunrinwa Toki | e6d974a | 2018-03-06 18:18:30 +0000 | [diff] [blame] | 393 | */ |
Abodunrinwa Toki | 324572e | 2019-02-12 19:01:01 +0000 | [diff] [blame] | 394 | @Deprecated |
Abodunrinwa Toki | e6d974a | 2018-03-06 18:18:30 +0000 | [diff] [blame] | 395 | public final TextClassifier getLocalTextClassifier() { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 396 | return TextClassifier.NO_OP; |
Abodunrinwa Toki | 324572e | 2019-02-12 19:01:01 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Returns the platform's default TextClassifier implementation. |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 401 | * |
| 402 | * @throws RuntimeException if the TextClassifier from |
| 403 | * PackageManager#getDefaultTextClassifierPackageName() calls |
| 404 | * this method. |
Abodunrinwa Toki | 324572e | 2019-02-12 19:01:01 +0000 | [diff] [blame] | 405 | */ |
Abodunrinwa Toki | 9aed55e | 2019-04-10 20:30:31 +0100 | [diff] [blame] | 406 | @NonNull |
Abodunrinwa Toki | 324572e | 2019-02-12 19:01:01 +0000 | [diff] [blame] | 407 | public static TextClassifier getDefaultTextClassifierImplementation(@NonNull Context context) { |
| 408 | final TextClassificationManager tcm = |
| 409 | context.getSystemService(TextClassificationManager.class); |
Tony Mak | df5d4bf | 2020-02-05 11:39:51 +0000 | [diff] [blame] | 410 | if (tcm == null) { |
| 411 | return TextClassifier.NO_OP; |
Abodunrinwa Toki | e6d974a | 2018-03-06 18:18:30 +0000 | [diff] [blame] | 412 | } |
Tony Mak | df5d4bf | 2020-02-05 11:39:51 +0000 | [diff] [blame] | 413 | TextClassificationConstants settings = new TextClassificationConstants(); |
| 414 | if (settings.getUseDefaultTextClassifierAsDefaultImplementation()) { |
| 415 | final String defaultTextClassifierPackageName = |
| 416 | context.getPackageManager().getDefaultTextClassifierPackageName(); |
| 417 | if (TextUtils.isEmpty(defaultTextClassifierPackageName)) { |
| 418 | return TextClassifier.NO_OP; |
| 419 | } |
| 420 | if (defaultTextClassifierPackageName.equals(context.getPackageName())) { |
| 421 | throw new RuntimeException( |
| 422 | "The default text classifier itself should not call the" |
| 423 | + "getDefaultTextClassifierImplementation() method."); |
| 424 | } |
| 425 | return tcm.getTextClassifier(TextClassifier.DEFAULT_SERVICE); |
| 426 | } else { |
| 427 | return tcm.getTextClassifier(TextClassifier.LOCAL); |
| 428 | } |
Abodunrinwa Toki | e6d974a | 2018-03-06 18:18:30 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 431 | /** @hide **/ |
| 432 | public static <T extends Parcelable> T getResponse(Bundle bundle) { |
| 433 | return bundle.getParcelable(KEY_RESULT); |
| 434 | } |
| 435 | |
Abodunrinwa Toki | e6d974a | 2018-03-06 18:18:30 +0000 | [diff] [blame] | 436 | /** |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 437 | * Callbacks for TextClassifierService results. |
| 438 | * |
| 439 | * @param <T> the type of the result |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 440 | */ |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 441 | public interface Callback<T> { |
| 442 | /** |
| 443 | * Returns the result. |
| 444 | */ |
| 445 | void onSuccess(T result); |
| 446 | |
| 447 | /** |
| 448 | * Signals a failure. |
| 449 | */ |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 450 | void onFailure(@NonNull CharSequence error); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 454 | * Returns the component name of the textclassifier service from the given package. |
| 455 | * Otherwise, returns null. |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 456 | * |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 457 | * @param context |
| 458 | * @param packageName the package to look for. |
| 459 | * @param resolveFlags the flags that are used by PackageManager to resolve the component name. |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 460 | * @hide |
| 461 | */ |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 462 | @Nullable |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 463 | public static ComponentName getServiceComponentName( |
| 464 | Context context, String packageName, int resolveFlags) { |
Makoto Onuki | 700feef | 2018-02-15 10:59:41 -0800 | [diff] [blame] | 465 | final Intent intent = new Intent(SERVICE_INTERFACE).setPackage(packageName); |
| 466 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 467 | final ResolveInfo ri = context.getPackageManager().resolveService(intent, resolveFlags); |
Makoto Onuki | 700feef | 2018-02-15 10:59:41 -0800 | [diff] [blame] | 468 | |
| 469 | if ((ri == null) || (ri.serviceInfo == null)) { |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 470 | Slog.w(LOG_TAG, String.format("Package or service not found in package %s for user %d", |
| 471 | packageName, context.getUserId())); |
Makoto Onuki | 700feef | 2018-02-15 10:59:41 -0800 | [diff] [blame] | 472 | return null; |
| 473 | } |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 474 | |
Makoto Onuki | 700feef | 2018-02-15 10:59:41 -0800 | [diff] [blame] | 475 | final ServiceInfo si = ri.serviceInfo; |
| 476 | |
| 477 | final String permission = si.permission; |
| 478 | if (Manifest.permission.BIND_TEXTCLASSIFIER_SERVICE.equals(permission)) { |
| 479 | return si.getComponentName(); |
| 480 | } |
| 481 | Slog.w(LOG_TAG, String.format( |
| 482 | "Service %s should require %s permission. Found %s permission", |
| 483 | si.getComponentName(), |
| 484 | Manifest.permission.BIND_TEXTCLASSIFIER_SERVICE, |
| 485 | si.permission)); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 486 | return null; |
| 487 | } |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * Forwards the callback result to a wrapped binder callback. |
| 491 | */ |
| 492 | private static final class ProxyCallback<T extends Parcelable> implements Callback<T> { |
Tony Mak | cdc81e8 | 2019-08-22 15:14:28 +0100 | [diff] [blame] | 493 | private ITextClassifierCallback mTextClassifierCallback; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 494 | |
| 495 | private ProxyCallback(ITextClassifierCallback textClassifierCallback) { |
Tony Mak | cdc81e8 | 2019-08-22 15:14:28 +0100 | [diff] [blame] | 496 | mTextClassifierCallback = Preconditions.checkNotNull(textClassifierCallback); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public void onSuccess(T result) { |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 501 | try { |
| 502 | Bundle bundle = new Bundle(1); |
| 503 | bundle.putParcelable(KEY_RESULT, result); |
Tony Mak | cdc81e8 | 2019-08-22 15:14:28 +0100 | [diff] [blame] | 504 | mTextClassifierCallback.onSuccess(bundle); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 505 | } catch (RemoteException e) { |
| 506 | Slog.d(LOG_TAG, "Error calling callback"); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public void onFailure(CharSequence error) { |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 512 | try { |
Joanne Chung | 0b7c2c4 | 2019-08-16 16:55:11 +0800 | [diff] [blame] | 513 | Slog.w(LOG_TAG, "Request fail: " + error); |
Tony Mak | cdc81e8 | 2019-08-22 15:14:28 +0100 | [diff] [blame] | 514 | mTextClassifierCallback.onFailure(); |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 515 | } catch (RemoteException e) { |
| 516 | Slog.d(LOG_TAG, "Error calling callback"); |
| 517 | } |
| 518 | } |
| 519 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 520 | } |