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