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 com.android.server.textclassifier; |
| 18 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 21 | import android.annotation.UserIdInt; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.ServiceConnection; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 27 | import android.os.Binder; |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 28 | import android.os.Bundle; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 29 | import android.os.IBinder; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 30 | import android.os.Process; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 33 | import android.provider.DeviceConfig; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 34 | import android.service.textclassifier.ITextClassifierCallback; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 35 | import android.service.textclassifier.ITextClassifierService; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 36 | import android.service.textclassifier.TextClassifierService; |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 37 | import android.service.textclassifier.TextClassifierService.ConnectionState; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 38 | import android.text.TextUtils; |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 39 | import android.util.ArrayMap; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 40 | import android.util.Slog; |
| 41 | import android.util.SparseArray; |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 42 | import android.view.textclassifier.ConversationActions; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 43 | import android.view.textclassifier.SelectionEvent; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 44 | import android.view.textclassifier.TextClassification; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 45 | import android.view.textclassifier.TextClassificationConstants; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 46 | import android.view.textclassifier.TextClassificationContext; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 47 | import android.view.textclassifier.TextClassificationManager; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 48 | import android.view.textclassifier.TextClassificationSessionId; |
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 | |
| 54 | import com.android.internal.annotations.GuardedBy; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 55 | import com.android.internal.util.DumpUtils; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 56 | import com.android.internal.util.FunctionalUtils; |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 57 | import com.android.internal.util.FunctionalUtils.ThrowingConsumer; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 58 | import com.android.internal.util.FunctionalUtils.ThrowingRunnable; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 59 | import com.android.internal.util.IndentingPrintWriter; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 60 | import com.android.internal.util.Preconditions; |
| 61 | import com.android.server.SystemService; |
| 62 | |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 63 | import java.io.FileDescriptor; |
| 64 | import java.io.PrintWriter; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 65 | import java.util.ArrayDeque; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 66 | import java.util.ArrayList; |
| 67 | import java.util.List; |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 68 | import java.util.Map; |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 69 | import java.util.Objects; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 70 | import java.util.Queue; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * A manager for TextClassifier services. |
| 74 | * Apps bind to the TextClassificationManagerService for text classification. This service |
| 75 | * reroutes calls to it to a {@link TextClassifierService} that it manages. |
| 76 | */ |
| 77 | public final class TextClassificationManagerService extends ITextClassifierService.Stub { |
| 78 | |
| 79 | private static final String LOG_TAG = "TextClassificationManagerService"; |
| 80 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 81 | private static final ITextClassifierCallback NO_OP_CALLBACK = new ITextClassifierCallback() { |
| 82 | @Override |
| 83 | public void onSuccess(Bundle result) {} |
| 84 | |
| 85 | @Override |
| 86 | public void onFailure() {} |
| 87 | |
| 88 | @Override |
| 89 | public IBinder asBinder() { |
| 90 | return null; |
| 91 | } |
| 92 | }; |
| 93 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 94 | public static final class Lifecycle extends SystemService { |
| 95 | |
| 96 | private final TextClassificationManagerService mManagerService; |
| 97 | |
| 98 | public Lifecycle(Context context) { |
| 99 | super(context); |
| 100 | mManagerService = new TextClassificationManagerService(context); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onStart() { |
| 105 | try { |
| 106 | publishBinderService(Context.TEXT_CLASSIFICATION_SERVICE, mManagerService); |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 107 | mManagerService.startListenSettings(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 108 | } catch (Throwable t) { |
| 109 | // Starting this service is not critical to the running of this device and should |
| 110 | // therefore not crash the device. If it fails, log the error and continue. |
| 111 | Slog.e(LOG_TAG, "Could not start the TextClassificationManagerService.", t); |
| 112 | } |
| 113 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 114 | |
| 115 | @Override |
| 116 | public void onStartUser(int userId) { |
| 117 | processAnyPendingWork(userId); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onUnlockUser(int userId) { |
| 122 | // Rebind if we failed earlier due to locked encrypted user |
| 123 | processAnyPendingWork(userId); |
| 124 | } |
| 125 | |
| 126 | private void processAnyPendingWork(int userId) { |
| 127 | synchronized (mManagerService.mLock) { |
| 128 | mManagerService.getUserStateLocked(userId).bindIfHasPendingRequestsLocked(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onStopUser(int userId) { |
| 134 | synchronized (mManagerService.mLock) { |
| 135 | UserState userState = mManagerService.peekUserStateLocked(userId); |
| 136 | if (userState != null) { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 137 | userState.cleanupServiceLocked(); |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 138 | mManagerService.mUserStates.remove(userId); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 145 | private final TextClassifierSettingsListener mSettingsListener; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 146 | private final Context mContext; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 147 | private final Object mLock; |
| 148 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 149 | final SparseArray<UserState> mUserStates = new SparseArray<>(); |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 150 | private final SessionCache mSessionCache; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 151 | private final TextClassificationConstants mSettings; |
| 152 | @Nullable |
| 153 | private final String mDefaultTextClassifierPackage; |
| 154 | @Nullable |
| 155 | private final String mSystemTextClassifierPackage; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 156 | |
| 157 | private TextClassificationManagerService(Context context) { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 158 | mContext = Objects.requireNonNull(context); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 159 | mLock = new Object(); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 160 | mSettings = new TextClassificationConstants(); |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 161 | mSettingsListener = new TextClassifierSettingsListener(mContext); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 162 | PackageManager packageManager = mContext.getPackageManager(); |
| 163 | mDefaultTextClassifierPackage = packageManager.getDefaultTextClassifierPackageName(); |
| 164 | mSystemTextClassifierPackage = packageManager.getSystemTextClassifierPackageName(); |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 165 | mSessionCache = new SessionCache(mLock); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 168 | private void startListenSettings() { |
| 169 | mSettingsListener.registerObserver(); |
| 170 | } |
| 171 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 172 | @Override |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 173 | public void onConnectedStateChanged(@ConnectionState int connected) { |
| 174 | } |
| 175 | |
| 176 | @Override |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 177 | public void onSuggestSelection( |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 178 | @Nullable TextClassificationSessionId sessionId, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 179 | TextSelection.Request request, ITextClassifierCallback callback) |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 180 | throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 181 | Objects.requireNonNull(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 182 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 183 | handleRequest( |
| 184 | request.getUserId(), |
| 185 | request.getCallingPackageName(), |
| 186 | /* attemptToBind= */ true, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 187 | request.getUseDefaultTextClassifier(), |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 188 | service -> service.onSuggestSelection(sessionId, request, callback), |
| 189 | "onSuggestSelection", |
| 190 | callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void onClassifyText( |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 195 | @Nullable TextClassificationSessionId sessionId, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 196 | TextClassification.Request request, ITextClassifierCallback callback) |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 197 | throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 198 | Objects.requireNonNull(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 199 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 200 | handleRequest( |
| 201 | request.getUserId(), |
| 202 | request.getCallingPackageName(), |
| 203 | /* attemptToBind= */ true, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 204 | request.getUseDefaultTextClassifier(), |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 205 | service -> service.onClassifyText(sessionId, request, callback), |
| 206 | "onClassifyText", |
| 207 | callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void onGenerateLinks( |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 212 | @Nullable TextClassificationSessionId sessionId, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 213 | TextLinks.Request request, ITextClassifierCallback callback) |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 214 | throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 215 | Objects.requireNonNull(request); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 216 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 217 | handleRequest( |
| 218 | request.getUserId(), |
| 219 | request.getCallingPackageName(), |
| 220 | /* attemptToBind= */ true, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 221 | request.getUseDefaultTextClassifier(), |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 222 | service -> service.onGenerateLinks(sessionId, request, callback), |
| 223 | "onGenerateLinks", |
| 224 | callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 227 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 228 | public void onSelectionEvent( |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 229 | @Nullable TextClassificationSessionId sessionId, SelectionEvent event) |
| 230 | throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 231 | Objects.requireNonNull(event); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 232 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 233 | handleRequest( |
| 234 | event.getUserId(), |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 235 | /* callingPackageName= */ null, |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 236 | /* attemptToBind= */ false, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 237 | event.getUseDefaultTextClassifier(), |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 238 | service -> service.onSelectionEvent(sessionId, event), |
| 239 | "onSelectionEvent", |
| 240 | NO_OP_CALLBACK); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 241 | } |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 242 | |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 243 | @Override |
| 244 | public void onTextClassifierEvent( |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 245 | @Nullable TextClassificationSessionId sessionId, |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 246 | TextClassifierEvent event) throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 247 | Objects.requireNonNull(event); |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 248 | |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 249 | final int userId = event.getEventContext() == null |
| 250 | ? UserHandle.getCallingUserId() |
| 251 | : event.getEventContext().getUserId(); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 252 | final boolean useDefaultTextClassifier = |
| 253 | event.getEventContext() != null |
| 254 | ? event.getEventContext().getUseDefaultTextClassifier() |
| 255 | : true; |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 256 | handleRequest( |
| 257 | userId, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 258 | /* callingPackageName= */ null, |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 259 | /* attemptToBind= */ false, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 260 | useDefaultTextClassifier, |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 261 | service -> service.onTextClassifierEvent(sessionId, event), |
| 262 | "onTextClassifierEvent", |
| 263 | NO_OP_CALLBACK); |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 264 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 265 | |
| 266 | @Override |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 267 | public void onDetectLanguage( |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 268 | @Nullable TextClassificationSessionId sessionId, |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 269 | TextLanguage.Request request, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 270 | ITextClassifierCallback callback) throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 271 | Objects.requireNonNull(request); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 272 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 273 | handleRequest( |
| 274 | request.getUserId(), |
| 275 | request.getCallingPackageName(), |
| 276 | /* attemptToBind= */ true, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 277 | request.getUseDefaultTextClassifier(), |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 278 | service -> service.onDetectLanguage(sessionId, request, callback), |
| 279 | "onDetectLanguage", |
| 280 | callback); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void onSuggestConversationActions( |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 285 | @Nullable TextClassificationSessionId sessionId, |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 286 | ConversationActions.Request request, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 287 | ITextClassifierCallback callback) throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 288 | Objects.requireNonNull(request); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 289 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 290 | handleRequest( |
| 291 | request.getUserId(), |
| 292 | request.getCallingPackageName(), |
| 293 | /* attemptToBind= */ true, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 294 | request.getUseDefaultTextClassifier(), |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 295 | service -> service.onSuggestConversationActions(sessionId, request, callback), |
| 296 | "onSuggestConversationActions", |
| 297 | callback); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 301 | public void onCreateTextClassificationSession( |
| 302 | TextClassificationContext classificationContext, TextClassificationSessionId sessionId) |
| 303 | throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 304 | Objects.requireNonNull(sessionId); |
| 305 | Objects.requireNonNull(classificationContext); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 306 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 307 | final int userId = classificationContext.getUserId(); |
| 308 | handleRequest( |
| 309 | userId, |
| 310 | classificationContext.getPackageName(), |
| 311 | /* attemptToBind= */ false, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 312 | classificationContext.getUseDefaultTextClassifier(), |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 313 | service -> { |
| 314 | service.onCreateTextClassificationSession(classificationContext, sessionId); |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 315 | mSessionCache.put(sessionId, classificationContext); |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 316 | }, |
| 317 | "onCreateTextClassificationSession", |
| 318 | NO_OP_CALLBACK); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onDestroyTextClassificationSession(TextClassificationSessionId sessionId) |
| 323 | throws RemoteException { |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 324 | Objects.requireNonNull(sessionId); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 325 | |
| 326 | synchronized (mLock) { |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 327 | final StrippedTextClassificationContext textClassificationContext = |
| 328 | mSessionCache.get(sessionId); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 329 | final int userId = textClassificationContext != null |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 330 | ? textClassificationContext.userId |
Abodunrinwa Toki | e849269 | 2019-07-01 19:41:44 +0100 | [diff] [blame] | 331 | : UserHandle.getCallingUserId(); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 332 | final boolean useDefaultTextClassifier = |
| 333 | textClassificationContext != null |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 334 | ? textClassificationContext.useDefaultTextClassifier |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 335 | : true; |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 336 | handleRequest( |
| 337 | userId, |
| 338 | /* callingPackageName= */ null, |
| 339 | /* attemptToBind= */ false, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 340 | useDefaultTextClassifier, |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 341 | service -> { |
| 342 | service.onDestroyTextClassificationSession(sessionId); |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 343 | mSessionCache.remove(sessionId); |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 344 | }, |
| 345 | "onDestroyTextClassificationSession", |
| 346 | NO_OP_CALLBACK); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 350 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 351 | private UserState getUserStateLocked(int userId) { |
| 352 | UserState result = mUserStates.get(userId); |
| 353 | if (result == null) { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 354 | result = new UserState(userId); |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 355 | mUserStates.put(userId, result); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 356 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 357 | return result; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 360 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 361 | UserState peekUserStateLocked(int userId) { |
| 362 | return mUserStates.get(userId); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 365 | private int resolvePackageToUid(@Nullable String packageName, @UserIdInt int userId) { |
| 366 | if (packageName == null) { |
| 367 | return Process.INVALID_UID; |
| 368 | } |
| 369 | final PackageManager pm = mContext.getPackageManager(); |
| 370 | try { |
| 371 | return pm.getPackageUidAsUser(packageName, userId); |
| 372 | } catch (PackageManager.NameNotFoundException e) { |
| 373 | Slog.e(LOG_TAG, "Could not get the UID for " + packageName); |
| 374 | } |
| 375 | return Process.INVALID_UID; |
| 376 | } |
| 377 | |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 378 | @Override |
| 379 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
| 380 | if (!DumpUtils.checkDumpPermission(mContext, LOG_TAG, fout)) return; |
| 381 | IndentingPrintWriter pw = new IndentingPrintWriter(fout, " "); |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 382 | |
Tony Mak | 384b8a69 | 2019-09-26 20:39:03 +0100 | [diff] [blame] | 383 | // Create a TCM instance with the system server identity. TCM creates a ContentObserver |
| 384 | // to listen for settings changes. It does not pass the checkContentProviderAccess check |
| 385 | // if we are using the shell identity, because AMS does not track of processes spawn from |
| 386 | // shell. |
| 387 | Binder.withCleanCallingIdentity( |
| 388 | () -> mContext.getSystemService(TextClassificationManager.class).dump(pw)); |
| 389 | |
| 390 | pw.printPair("context", mContext); |
| 391 | pw.println(); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 392 | pw.printPair("defaultTextClassifierPackage", mDefaultTextClassifierPackage); |
| 393 | pw.println(); |
| 394 | pw.printPair("systemTextClassifierPackage", mSystemTextClassifierPackage); |
| 395 | pw.println(); |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 396 | synchronized (mLock) { |
| 397 | int size = mUserStates.size(); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 398 | pw.print("Number user states: "); |
| 399 | pw.println(size); |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 400 | if (size > 0) { |
| 401 | for (int i = 0; i < size; i++) { |
| 402 | pw.increaseIndent(); |
| 403 | UserState userState = mUserStates.valueAt(i); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 404 | pw.printPair("User", mUserStates.keyAt(i)); |
| 405 | pw.println(); |
| 406 | userState.dump(pw); |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 407 | pw.decreaseIndent(); |
| 408 | } |
| 409 | } |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 410 | pw.println("Number of active sessions: " + mSessionCache.size()); |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 411 | } |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 414 | private void handleRequest( |
| 415 | @UserIdInt int userId, |
| 416 | @Nullable String callingPackageName, |
| 417 | boolean attemptToBind, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 418 | boolean useDefaultTextClassifier, |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 419 | @NonNull ThrowingConsumer<ITextClassifierService> textClassifierServiceConsumer, |
| 420 | @NonNull String methodName, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 421 | @NonNull ITextClassifierCallback callback) throws RemoteException { |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 422 | Objects.requireNonNull(textClassifierServiceConsumer); |
| 423 | Objects.requireNonNull(methodName); |
| 424 | Objects.requireNonNull(callback); |
| 425 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 426 | try { |
| 427 | validateCallingPackage(callingPackageName); |
| 428 | validateUser(userId); |
| 429 | } catch (Exception e) { |
| 430 | throw new RemoteException("Invalid request: " + e.getMessage(), e, |
| 431 | /* enableSuppression */ true, /* writableStackTrace */ true); |
| 432 | } |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 433 | synchronized (mLock) { |
| 434 | UserState userState = getUserStateLocked(userId); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 435 | ServiceState serviceState = |
| 436 | userState.getServiceStateLocked(useDefaultTextClassifier); |
| 437 | if (serviceState == null) { |
| 438 | Slog.d(LOG_TAG, "No configured system TextClassifierService"); |
| 439 | callback.onFailure(); |
| 440 | } else if (attemptToBind && !serviceState.bindLocked()) { |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 441 | Slog.d(LOG_TAG, "Unable to bind TextClassifierService at " + methodName); |
| 442 | callback.onFailure(); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 443 | } else if (serviceState.isBoundLocked()) { |
| 444 | if (!serviceState.checkRequestAcceptedLocked(Binder.getCallingUid(), methodName)) { |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 445 | return; |
| 446 | } |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 447 | textClassifierServiceConsumer.accept(serviceState.mService); |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 448 | } else { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 449 | serviceState.mPendingRequests.add( |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 450 | new PendingRequest( |
| 451 | methodName, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 452 | () -> textClassifierServiceConsumer.accept(serviceState.mService), |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 453 | callback::onFailure, callback.asBinder(), |
| 454 | this, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 455 | serviceState, |
Tony Mak | 6e1d5ad | 2020-01-16 14:18:22 +0000 | [diff] [blame] | 456 | Binder.getCallingUid())); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 461 | private void onTextClassifierServicePackageOverrideChanged(String overriddenPackage) { |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 462 | synchronized (mLock) { |
| 463 | final int size = mUserStates.size(); |
| 464 | for (int i = 0; i < size; i++) { |
| 465 | UserState userState = mUserStates.valueAt(i); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 466 | userState.onTextClassifierServicePackageOverrideChangedLocked(overriddenPackage); |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 471 | private static final class PendingRequest implements IBinder.DeathRecipient { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 472 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 473 | private final int mUid; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 474 | @Nullable |
| 475 | private final String mName; |
| 476 | @Nullable |
| 477 | private final IBinder mBinder; |
| 478 | @NonNull |
| 479 | private final Runnable mRequest; |
| 480 | @Nullable |
| 481 | private final Runnable mOnServiceFailure; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 482 | @GuardedBy("mLock") |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 483 | @NonNull |
| 484 | private final ServiceState mServiceState; |
| 485 | @NonNull |
| 486 | private final TextClassificationManagerService mService; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * Initializes a new pending request. |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 490 | * |
| 491 | * @param request action to perform when the service is bound |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 492 | * @param onServiceFailure action to perform when the service dies or disconnects |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 493 | * @param binder binder to the process that made this pending request |
| 494 | * @parm service the TCMS instance. |
| 495 | * @param serviceState the service state of the service that will execute the request. |
| 496 | * @param uid the calling uid of the request. |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 497 | */ |
Joanne Chung | 0b7c2c4 | 2019-08-16 16:55:11 +0800 | [diff] [blame] | 498 | PendingRequest(@Nullable String name, |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 499 | @NonNull ThrowingRunnable request, @Nullable ThrowingRunnable onServiceFailure, |
| 500 | @Nullable IBinder binder, |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 501 | @NonNull TextClassificationManagerService service, |
| 502 | @NonNull ServiceState serviceState, int uid) { |
Joanne Chung | 0b7c2c4 | 2019-08-16 16:55:11 +0800 | [diff] [blame] | 503 | mName = name; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 504 | mRequest = |
Daulet Zhanguzin | 0ccd2ef | 2020-01-02 17:21:51 +0000 | [diff] [blame] | 505 | logOnFailure(Objects.requireNonNull(request), "handling pending request"); |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 506 | mOnServiceFailure = |
| 507 | logOnFailure(onServiceFailure, "notifying callback of service failure"); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 508 | mBinder = binder; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 509 | mService = service; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 510 | mServiceState = Objects.requireNonNull(serviceState); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 511 | if (mBinder != null) { |
| 512 | try { |
| 513 | mBinder.linkToDeath(this, 0); |
| 514 | } catch (RemoteException e) { |
| 515 | e.printStackTrace(); |
| 516 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 517 | } |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 518 | mUid = uid; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 521 | @Override |
| 522 | public void binderDied() { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 523 | synchronized (mService.mLock) { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 524 | // No need to handle this pending request anymore. Remove. |
| 525 | removeLocked(); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | @GuardedBy("mLock") |
| 530 | private void removeLocked() { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 531 | mServiceState.mPendingRequests.remove(this); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 532 | if (mBinder != null) { |
| 533 | mBinder.unlinkToDeath(this, 0); |
| 534 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 538 | private static Runnable logOnFailure(@Nullable ThrowingRunnable r, String opDesc) { |
| 539 | if (r == null) return null; |
| 540 | return FunctionalUtils.handleExceptions(r, |
| 541 | e -> Slog.d(LOG_TAG, "Error " + opDesc + ": " + e.getMessage())); |
| 542 | } |
| 543 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 544 | private void validateCallingPackage(@Nullable String callingPackage) |
| 545 | throws PackageManager.NameNotFoundException { |
| 546 | if (callingPackage != null) { |
| 547 | final int packageUid = mContext.getPackageManager() |
| 548 | .getPackageUidAsUser(callingPackage, UserHandle.getCallingUserId()); |
| 549 | final int callingUid = Binder.getCallingUid(); |
| 550 | Preconditions.checkArgument( |
| 551 | callingUid == packageUid |
| 552 | // Trust the system process: |
| 553 | || callingUid == android.os.Process.SYSTEM_UID, |
| 554 | "Invalid package name. callingPackage=" + callingPackage |
| 555 | + ", callingUid=" + callingUid); |
| 556 | } |
| 557 | } |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 558 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 559 | private void validateUser(@UserIdInt int userId) { |
| 560 | Preconditions.checkArgument(userId != UserHandle.USER_NULL, "Null userId"); |
| 561 | final int callingUserId = UserHandle.getCallingUserId(); |
| 562 | if (callingUserId != userId) { |
| 563 | mContext.enforceCallingOrSelfPermission( |
| 564 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 565 | "Invalid userId. UserId=" + userId + ", CallingUserId=" + callingUserId); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 568 | |
Tony Mak | 53195e0 | 2020-02-06 14:52:57 +0000 | [diff] [blame] | 569 | /** |
| 570 | * Stores the stripped down version of {@link TextClassificationContext}s, i.e. {@link |
| 571 | * StrippedTextClassificationContext}, keyed by {@link TextClassificationSessionId}. Sessions |
| 572 | * are cleaned up automatically when the client process is dead. |
| 573 | */ |
| 574 | static final class SessionCache { |
| 575 | @NonNull |
| 576 | private final Object mLock; |
| 577 | @NonNull |
| 578 | @GuardedBy("mLock") |
| 579 | private final Map<TextClassificationSessionId, StrippedTextClassificationContext> mCache = |
| 580 | new ArrayMap<>(); |
| 581 | @NonNull |
| 582 | @GuardedBy("mLock") |
| 583 | private final Map<TextClassificationSessionId, DeathRecipient> mDeathRecipients = |
| 584 | new ArrayMap<>(); |
| 585 | |
| 586 | SessionCache(@NonNull Object lock) { |
| 587 | mLock = Objects.requireNonNull(lock); |
| 588 | } |
| 589 | |
| 590 | void put(@NonNull TextClassificationSessionId sessionId, |
| 591 | @NonNull TextClassificationContext textClassificationContext) { |
| 592 | synchronized (mLock) { |
| 593 | mCache.put(sessionId, |
| 594 | new StrippedTextClassificationContext(textClassificationContext)); |
| 595 | try { |
| 596 | DeathRecipient deathRecipient = () -> remove(sessionId); |
| 597 | sessionId.getToken().linkToDeath(deathRecipient, /* flags= */ 0); |
| 598 | mDeathRecipients.put(sessionId, deathRecipient); |
| 599 | } catch (RemoteException e) { |
| 600 | Slog.w(LOG_TAG, "SessionCache: Failed to link to death", e); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | @Nullable |
| 606 | StrippedTextClassificationContext get(@NonNull TextClassificationSessionId sessionId) { |
| 607 | Objects.requireNonNull(sessionId); |
| 608 | synchronized (mLock) { |
| 609 | return mCache.get(sessionId); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | void remove(@NonNull TextClassificationSessionId sessionId) { |
| 614 | Objects.requireNonNull(sessionId); |
| 615 | synchronized (mLock) { |
| 616 | DeathRecipient deathRecipient = mDeathRecipients.get(sessionId); |
| 617 | if (deathRecipient != null) { |
| 618 | sessionId.getToken().unlinkToDeath(deathRecipient, /* flags= */ 0); |
| 619 | } |
| 620 | mDeathRecipients.remove(sessionId); |
| 621 | mCache.remove(sessionId); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | int size() { |
| 626 | synchronized (mLock) { |
| 627 | return mCache.size(); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /** A stripped down version of {@link TextClassificationContext}. */ |
| 633 | static class StrippedTextClassificationContext { |
| 634 | @UserIdInt |
| 635 | public final int userId; |
| 636 | public final boolean useDefaultTextClassifier; |
| 637 | |
| 638 | StrippedTextClassificationContext(TextClassificationContext textClassificationContext) { |
| 639 | userId = textClassificationContext.getUserId(); |
| 640 | useDefaultTextClassifier = textClassificationContext.getUseDefaultTextClassifier(); |
| 641 | } |
| 642 | } |
| 643 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 644 | private final class UserState { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 645 | @UserIdInt |
| 646 | final int mUserId; |
| 647 | @Nullable |
| 648 | private final ServiceState mDefaultServiceState; |
| 649 | @Nullable |
| 650 | private final ServiceState mSystemServiceState; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 651 | @GuardedBy("mLock") |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 652 | @Nullable |
| 653 | private ServiceState mUntrustedServiceState; |
| 654 | |
| 655 | private UserState(int userId) { |
| 656 | mUserId = userId; |
| 657 | mDefaultServiceState = TextUtils.isEmpty(mDefaultTextClassifierPackage) |
| 658 | ? null |
| 659 | : new ServiceState(userId, mDefaultTextClassifierPackage, /* isTrusted= */true); |
| 660 | mSystemServiceState = TextUtils.isEmpty(mSystemTextClassifierPackage) |
| 661 | ? null |
| 662 | : new ServiceState(userId, mSystemTextClassifierPackage, /* isTrusted= */ true); |
| 663 | } |
| 664 | |
| 665 | @GuardedBy("mLock") |
| 666 | @Nullable |
| 667 | ServiceState getServiceStateLocked(boolean useDefaultTextClassifier) { |
| 668 | if (useDefaultTextClassifier) { |
| 669 | return mDefaultServiceState; |
| 670 | } |
| 671 | String textClassifierServicePackageOverride = |
Tony Mak | 504c98c | 2020-02-11 16:08:06 +0000 | [diff] [blame] | 672 | Binder.withCleanCallingIdentity( |
| 673 | mSettings::getTextClassifierServicePackageOverride); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 674 | if (!TextUtils.isEmpty(textClassifierServicePackageOverride)) { |
| 675 | if (textClassifierServicePackageOverride.equals(mDefaultTextClassifierPackage)) { |
| 676 | return mDefaultServiceState; |
| 677 | } |
| 678 | if (textClassifierServicePackageOverride.equals(mSystemTextClassifierPackage) |
| 679 | && mSystemServiceState != null) { |
| 680 | return mSystemServiceState; |
| 681 | } |
| 682 | if (mUntrustedServiceState == null) { |
| 683 | mUntrustedServiceState = |
| 684 | new ServiceState( |
| 685 | mUserId, |
| 686 | textClassifierServicePackageOverride, |
| 687 | /* isTrusted= */false); |
| 688 | } |
| 689 | return mUntrustedServiceState; |
| 690 | } |
| 691 | return mSystemServiceState != null ? mSystemServiceState : mDefaultServiceState; |
| 692 | } |
| 693 | |
| 694 | @GuardedBy("mLock") |
| 695 | void onTextClassifierServicePackageOverrideChangedLocked(String overriddenPackageName) { |
| 696 | // The override config is just used for testing, and the flag value is not expected |
| 697 | // to change often. So, let's keep it simple and just unbind all the services here. The |
| 698 | // right service will be bound when the next request comes. |
| 699 | for (ServiceState serviceState : getAllServiceStatesLocked()) { |
| 700 | serviceState.unbindIfBoundLocked(); |
| 701 | } |
| 702 | mUntrustedServiceState = null; |
| 703 | } |
| 704 | |
| 705 | @GuardedBy("mLock") |
| 706 | void bindIfHasPendingRequestsLocked() { |
| 707 | for (ServiceState serviceState : getAllServiceStatesLocked()) { |
| 708 | serviceState.bindIfHasPendingRequestsLocked(); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | @GuardedBy("mLock") |
| 713 | void cleanupServiceLocked() { |
| 714 | for (ServiceState serviceState : getAllServiceStatesLocked()) { |
| 715 | if (serviceState.mConnection != null) { |
| 716 | serviceState.mConnection.cleanupService(); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | @GuardedBy("mLock") |
| 722 | @NonNull |
| 723 | private List<ServiceState> getAllServiceStatesLocked() { |
| 724 | List<ServiceState> serviceStates = new ArrayList<>(); |
| 725 | if (mDefaultServiceState != null) { |
| 726 | serviceStates.add(mDefaultServiceState); |
| 727 | } |
| 728 | if (mSystemServiceState != null) { |
| 729 | serviceStates.add(mSystemServiceState); |
| 730 | } |
| 731 | if (mUntrustedServiceState != null) { |
| 732 | serviceStates.add(mUntrustedServiceState); |
| 733 | } |
| 734 | return serviceStates; |
| 735 | } |
| 736 | |
| 737 | void dump(IndentingPrintWriter pw) { |
| 738 | synchronized (mLock) { |
| 739 | pw.increaseIndent(); |
| 740 | dump(pw, mDefaultServiceState, "Default"); |
| 741 | dump(pw, mSystemServiceState, "System"); |
| 742 | dump(pw, mUntrustedServiceState, "Untrusted"); |
| 743 | pw.decreaseIndent(); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | private void dump( |
| 748 | IndentingPrintWriter pw, @Nullable ServiceState serviceState, String name) { |
| 749 | synchronized (mLock) { |
| 750 | if (serviceState != null) { |
| 751 | pw.print(name + ": "); |
| 752 | serviceState.dump(pw); |
| 753 | pw.println(); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | private final class ServiceState { |
| 760 | @UserIdInt |
| 761 | final int mUserId; |
| 762 | @NonNull |
| 763 | final String mPackageName; |
| 764 | @NonNull |
| 765 | final TextClassifierServiceConnection mConnection; |
| 766 | final boolean mIsTrusted; |
| 767 | @NonNull |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 768 | @GuardedBy("mLock") |
| 769 | final Queue<PendingRequest> mPendingRequests = new ArrayDeque<>(); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 770 | @Nullable |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 771 | @GuardedBy("mLock") |
| 772 | ITextClassifierService mService; |
| 773 | @GuardedBy("mLock") |
| 774 | boolean mBinding; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 775 | @Nullable |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 776 | @GuardedBy("mLock") |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 777 | ComponentName mBoundComponentName = null; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 778 | @GuardedBy("mLock") |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 779 | int mBoundServiceUid = Process.INVALID_UID; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 780 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 781 | private ServiceState(@UserIdInt int userId, String packageName, boolean isTrusted) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 782 | mUserId = userId; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 783 | mPackageName = packageName; |
| 784 | mConnection = new TextClassifierServiceConnection(mUserId); |
| 785 | mIsTrusted = isTrusted; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | @GuardedBy("mLock") |
| 789 | boolean isBoundLocked() { |
| 790 | return mService != null; |
| 791 | } |
| 792 | |
| 793 | @GuardedBy("mLock") |
| 794 | private void handlePendingRequestsLocked() { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 795 | PendingRequest request; |
| 796 | while ((request = mPendingRequests.poll()) != null) { |
| 797 | if (isBoundLocked()) { |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 798 | if (!checkRequestAcceptedLocked(request.mUid, request.mName)) { |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 799 | return; |
| 800 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 801 | request.mRequest.run(); |
| 802 | } else { |
| 803 | if (request.mOnServiceFailure != null) { |
Joanne Chung | 0b7c2c4 | 2019-08-16 16:55:11 +0800 | [diff] [blame] | 804 | Slog.d(LOG_TAG, "Unable to bind TextClassifierService for PendingRequest " |
| 805 | + request.mName); |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 806 | request.mOnServiceFailure.run(); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | if (request.mBinder != null) { |
| 811 | request.mBinder.unlinkToDeath(request, 0); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 816 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 817 | private boolean bindIfHasPendingRequestsLocked() { |
| 818 | return !mPendingRequests.isEmpty() && bindLocked(); |
| 819 | } |
| 820 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 821 | @GuardedBy("mLock") |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 822 | void unbindIfBoundLocked() { |
| 823 | if (isBoundLocked()) { |
| 824 | Slog.v(LOG_TAG, "Unbinding " + mBoundComponentName + " for " + mUserId); |
| 825 | mContext.unbindService(mConnection); |
| 826 | mConnection.cleanupService(); |
| 827 | } |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 828 | } |
| 829 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 830 | /** |
| 831 | * @return true if the service is bound or in the process of being bound. |
| 832 | * Returns false otherwise. |
| 833 | */ |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 834 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 835 | private boolean bindLocked() { |
| 836 | if (isBoundLocked() || mBinding) { |
| 837 | return true; |
| 838 | } |
| 839 | |
| 840 | // TODO: Handle bind timeout. |
| 841 | final boolean willBind; |
| 842 | final long identity = Binder.clearCallingIdentity(); |
| 843 | try { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 844 | final ComponentName componentName = getTextClassifierServiceComponent(); |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 845 | if (componentName == null) { |
| 846 | // Might happen if the storage is encrypted and the user is not unlocked |
| 847 | return false; |
| 848 | } |
| 849 | Intent serviceIntent = new Intent(TextClassifierService.SERVICE_INTERFACE) |
| 850 | .setComponent(componentName); |
| 851 | Slog.d(LOG_TAG, "Binding to " + serviceIntent.getComponent()); |
| 852 | willBind = mContext.bindServiceAsUser( |
| 853 | serviceIntent, mConnection, |
Dianne Hackborn | c390aa8 | 2019-01-09 16:38:22 -0800 | [diff] [blame] | 854 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE |
| 855 | | Context.BIND_RESTRICT_ASSOCIATIONS, |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 856 | UserHandle.of(mUserId)); |
| 857 | mBinding = willBind; |
| 858 | } finally { |
| 859 | Binder.restoreCallingIdentity(identity); |
| 860 | } |
| 861 | return willBind; |
| 862 | } |
| 863 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 864 | @Nullable |
| 865 | private ComponentName getTextClassifierServiceComponent() { |
| 866 | return TextClassifierService.getServiceComponentName( |
| 867 | mContext, |
| 868 | mPackageName, |
| 869 | mIsTrusted ? PackageManager.MATCH_SYSTEM_ONLY : 0); |
| 870 | } |
| 871 | |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 872 | private void dump(IndentingPrintWriter pw) { |
| 873 | pw.printPair("context", mContext); |
| 874 | pw.printPair("userId", mUserId); |
| 875 | synchronized (mLock) { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 876 | pw.printPair("packageName", mPackageName); |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 877 | pw.printPair("boundComponentName", mBoundComponentName); |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 878 | pw.printPair("isTrusted", mIsTrusted); |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 879 | pw.printPair("boundServiceUid", mBoundServiceUid); |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 880 | pw.printPair("binding", mBinding); |
| 881 | pw.printPair("numberRequests", mPendingRequests.size()); |
| 882 | } |
| 883 | } |
| 884 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 885 | @GuardedBy("mLock") |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 886 | private boolean checkRequestAcceptedLocked(int requestUid, @NonNull String methodName) { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 887 | if (mIsTrusted || (requestUid == mBoundServiceUid)) { |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 888 | return true; |
| 889 | } |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 890 | Slog.w(LOG_TAG, String.format( |
| 891 | "[%s] Non-default TextClassifierServices may only see text from the same uid.", |
| 892 | methodName)); |
| 893 | return false; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 894 | } |
| 895 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 896 | @GuardedBy("mLock") |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 897 | private void updateServiceInfoLocked(int userId, @Nullable ComponentName componentName) { |
| 898 | mBoundComponentName = componentName; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 899 | mBoundServiceUid = |
| 900 | mBoundComponentName == null |
| 901 | ? Process.INVALID_UID |
| 902 | : resolvePackageToUid(mBoundComponentName.getPackageName(), userId); |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 903 | } |
| 904 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 905 | private final class TextClassifierServiceConnection implements ServiceConnection { |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 906 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 907 | @UserIdInt |
| 908 | private final int mUserId; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 909 | |
| 910 | TextClassifierServiceConnection(int userId) { |
| 911 | mUserId = userId; |
| 912 | } |
| 913 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 914 | @Override |
| 915 | public void onServiceConnected(ComponentName name, IBinder service) { |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 916 | final ITextClassifierService tcService = ITextClassifierService.Stub.asInterface( |
| 917 | service); |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 918 | try { |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 919 | tcService.onConnectedStateChanged(TextClassifierService.CONNECTED); |
Joanne Chung | 0b7c0d4 | 2019-10-18 21:02:26 +0800 | [diff] [blame] | 920 | } catch (RemoteException e) { |
| 921 | Slog.e(LOG_TAG, "error in onConnectedStateChanged"); |
| 922 | } |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 923 | init(tcService, name); |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | @Override |
| 927 | public void onServiceDisconnected(ComponentName name) { |
| 928 | cleanupService(); |
| 929 | } |
| 930 | |
| 931 | @Override |
| 932 | public void onBindingDied(ComponentName name) { |
| 933 | cleanupService(); |
| 934 | } |
| 935 | |
| 936 | @Override |
| 937 | public void onNullBinding(ComponentName name) { |
| 938 | cleanupService(); |
| 939 | } |
| 940 | |
| 941 | void cleanupService() { |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 942 | init(/* service */ null, /* name */ null); |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 945 | private void init(@Nullable ITextClassifierService service, |
| 946 | @Nullable ComponentName name) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 947 | synchronized (mLock) { |
| 948 | mService = service; |
| 949 | mBinding = false; |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 950 | updateServiceInfoLocked(mUserId, name); |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 951 | handlePendingRequestsLocked(); |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | } |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 956 | |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 957 | private final class TextClassifierSettingsListener implements |
| 958 | DeviceConfig.OnPropertiesChangedListener { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 959 | @NonNull |
| 960 | private final Context mContext; |
| 961 | @Nullable |
| 962 | private String mServicePackageOverride; |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 963 | |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 964 | |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 965 | TextClassifierSettingsListener(Context context) { |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 966 | mContext = context; |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 967 | mServicePackageOverride = mSettings.getTextClassifierServicePackageOverride(); |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 968 | } |
| 969 | |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 970 | void registerObserver() { |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 971 | DeviceConfig.addOnPropertiesChangedListener( |
| 972 | DeviceConfig.NAMESPACE_TEXTCLASSIFIER, |
| 973 | mContext.getMainExecutor(), |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 974 | this); |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | @Override |
| 978 | public void onPropertiesChanged(DeviceConfig.Properties properties) { |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 979 | final String currentServicePackageOverride = |
| 980 | mSettings.getTextClassifierServicePackageOverride(); |
| 981 | if (TextUtils.equals(currentServicePackageOverride, mServicePackageOverride)) { |
Joanne Chung | c44f529 | 2019-12-02 21:18:43 +0800 | [diff] [blame] | 982 | return; |
| 983 | } |
Tony Mak | c5a7432 | 2020-02-04 17:18:15 +0000 | [diff] [blame] | 984 | mServicePackageOverride = currentServicePackageOverride; |
| 985 | onTextClassifierServicePackageOverrideChanged(currentServicePackageOverride); |
Joanne Chung | b50ab4b | 2019-10-14 16:40:57 +0800 | [diff] [blame] | 986 | } |
| 987 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 988 | } |