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; |
| 26 | import android.os.Binder; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 27 | import android.os.IBinder; |
| 28 | import android.os.RemoteException; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 29 | import android.os.UserHandle; |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 30 | import android.service.textclassifier.ITextClassifierCallback; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 31 | import android.service.textclassifier.ITextClassifierService; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 32 | import android.service.textclassifier.TextClassifierService; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 33 | import android.util.Slog; |
| 34 | import android.util.SparseArray; |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 35 | import android.view.textclassifier.ConversationActions; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 36 | import android.view.textclassifier.SelectionEvent; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 37 | import android.view.textclassifier.TextClassification; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 38 | import android.view.textclassifier.TextClassificationContext; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 39 | import android.view.textclassifier.TextClassificationManager; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 40 | import android.view.textclassifier.TextClassificationSessionId; |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 41 | import android.view.textclassifier.TextClassifierEvent; |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 42 | import android.view.textclassifier.TextLanguage; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 43 | import android.view.textclassifier.TextLinks; |
| 44 | import android.view.textclassifier.TextSelection; |
| 45 | |
| 46 | import com.android.internal.annotations.GuardedBy; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 47 | import com.android.internal.util.DumpUtils; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 48 | import com.android.internal.util.FunctionalUtils; |
| 49 | import com.android.internal.util.FunctionalUtils.ThrowingRunnable; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 50 | import com.android.internal.util.IndentingPrintWriter; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 51 | import com.android.internal.util.Preconditions; |
| 52 | import com.android.server.SystemService; |
| 53 | |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 54 | import java.io.FileDescriptor; |
| 55 | import java.io.PrintWriter; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 56 | import java.util.ArrayDeque; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 57 | import java.util.Queue; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * A manager for TextClassifier services. |
| 61 | * Apps bind to the TextClassificationManagerService for text classification. This service |
| 62 | * reroutes calls to it to a {@link TextClassifierService} that it manages. |
| 63 | */ |
| 64 | public final class TextClassificationManagerService extends ITextClassifierService.Stub { |
| 65 | |
| 66 | private static final String LOG_TAG = "TextClassificationManagerService"; |
| 67 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 68 | public static final class Lifecycle extends SystemService { |
| 69 | |
| 70 | private final TextClassificationManagerService mManagerService; |
| 71 | |
| 72 | public Lifecycle(Context context) { |
| 73 | super(context); |
| 74 | mManagerService = new TextClassificationManagerService(context); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onStart() { |
| 79 | try { |
| 80 | publishBinderService(Context.TEXT_CLASSIFICATION_SERVICE, mManagerService); |
| 81 | } catch (Throwable t) { |
| 82 | // Starting this service is not critical to the running of this device and should |
| 83 | // therefore not crash the device. If it fails, log the error and continue. |
| 84 | Slog.e(LOG_TAG, "Could not start the TextClassificationManagerService.", t); |
| 85 | } |
| 86 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 87 | |
| 88 | @Override |
| 89 | public void onStartUser(int userId) { |
| 90 | processAnyPendingWork(userId); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onUnlockUser(int userId) { |
| 95 | // Rebind if we failed earlier due to locked encrypted user |
| 96 | processAnyPendingWork(userId); |
| 97 | } |
| 98 | |
| 99 | private void processAnyPendingWork(int userId) { |
| 100 | synchronized (mManagerService.mLock) { |
| 101 | mManagerService.getUserStateLocked(userId).bindIfHasPendingRequestsLocked(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onStopUser(int userId) { |
| 107 | synchronized (mManagerService.mLock) { |
| 108 | UserState userState = mManagerService.peekUserStateLocked(userId); |
| 109 | if (userState != null) { |
| 110 | userState.mConnection.cleanupService(); |
| 111 | mManagerService.mUserStates.remove(userId); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private final Context mContext; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 119 | private final Object mLock; |
| 120 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 121 | final SparseArray<UserState> mUserStates = new SparseArray<>(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 122 | |
| 123 | private TextClassificationManagerService(Context context) { |
| 124 | mContext = Preconditions.checkNotNull(context); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 125 | mLock = new Object(); |
| 126 | } |
| 127 | |
| 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 | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 132 | throws RemoteException { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 133 | Preconditions.checkNotNull(request); |
| 134 | Preconditions.checkNotNull(callback); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 135 | validateInput(mContext, request.getCallingPackageName()); |
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 | synchronized (mLock) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 138 | UserState userState = getCallingUserStateLocked(); |
| 139 | if (!userState.bindLocked()) { |
| 140 | callback.onFailure(); |
| 141 | } else if (userState.isBoundLocked()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 142 | userState.mService.onSuggestSelection(sessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 143 | } else { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 144 | userState.mPendingRequests.add(new PendingRequest( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 145 | () -> onSuggestSelection(sessionId, request, callback), |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 146 | callback::onFailure, callback.asBinder(), this, userState)); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onClassifyText( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 153 | TextClassificationSessionId sessionId, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 154 | TextClassification.Request request, ITextClassifierCallback callback) |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 155 | throws RemoteException { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 156 | Preconditions.checkNotNull(request); |
| 157 | Preconditions.checkNotNull(callback); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 158 | validateInput(mContext, request.getCallingPackageName()); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 159 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 160 | synchronized (mLock) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 161 | UserState userState = getCallingUserStateLocked(); |
| 162 | if (!userState.bindLocked()) { |
| 163 | callback.onFailure(); |
| 164 | } else if (userState.isBoundLocked()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 165 | userState.mService.onClassifyText(sessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 166 | } else { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 167 | userState.mPendingRequests.add(new PendingRequest( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 168 | () -> onClassifyText(sessionId, request, callback), |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 169 | callback::onFailure, callback.asBinder(), this, userState)); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void onGenerateLinks( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 176 | TextClassificationSessionId sessionId, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 177 | TextLinks.Request request, ITextClassifierCallback callback) |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 178 | throws RemoteException { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 179 | Preconditions.checkNotNull(request); |
| 180 | Preconditions.checkNotNull(callback); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 181 | validateInput(mContext, request.getCallingPackageName()); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 182 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 183 | synchronized (mLock) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 184 | UserState userState = getCallingUserStateLocked(); |
| 185 | if (!userState.bindLocked()) { |
| 186 | callback.onFailure(); |
| 187 | } else if (userState.isBoundLocked()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 188 | userState.mService.onGenerateLinks(sessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 189 | } else { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 190 | userState.mPendingRequests.add(new PendingRequest( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 191 | () -> onGenerateLinks(sessionId, request, callback), |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 192 | callback::onFailure, callback.asBinder(), this, userState)); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 197 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 198 | public void onSelectionEvent( |
| 199 | TextClassificationSessionId sessionId, SelectionEvent event) throws RemoteException { |
| 200 | Preconditions.checkNotNull(event); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 201 | validateInput(mContext, event.getPackageName()); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 202 | |
| 203 | synchronized (mLock) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 204 | UserState userState = getCallingUserStateLocked(); |
| 205 | if (userState.isBoundLocked()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 206 | userState.mService.onSelectionEvent(sessionId, event); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 207 | } else { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 208 | userState.mPendingRequests.add(new PendingRequest( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 209 | () -> onSelectionEvent(sessionId, event), |
| 210 | null /* onServiceFailure */, null /* binder */, this, userState)); |
| 211 | } |
| 212 | } |
| 213 | } |
Abodunrinwa Toki | 37ccedc | 2018-12-11 00:35:11 +0000 | [diff] [blame] | 214 | @Override |
| 215 | public void onTextClassifierEvent( |
| 216 | TextClassificationSessionId sessionId, |
| 217 | TextClassifierEvent event) throws RemoteException { |
| 218 | Preconditions.checkNotNull(event); |
| 219 | final String packageName = event.getEventContext() == null |
| 220 | ? null |
| 221 | : event.getEventContext().getPackageName(); |
| 222 | validateInput(mContext, packageName); |
| 223 | |
| 224 | synchronized (mLock) { |
| 225 | UserState userState = getCallingUserStateLocked(); |
| 226 | if (userState.isBoundLocked()) { |
| 227 | userState.mService.onTextClassifierEvent(sessionId, event); |
| 228 | } else { |
| 229 | userState.mPendingRequests.add(new PendingRequest( |
| 230 | () -> onTextClassifierEvent(sessionId, event), |
| 231 | null /* onServiceFailure */, null /* binder */, this, userState)); |
| 232 | } |
| 233 | } |
| 234 | } |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 235 | |
| 236 | @Override |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 237 | public void onDetectLanguage( |
| 238 | TextClassificationSessionId sessionId, |
| 239 | TextLanguage.Request request, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 240 | ITextClassifierCallback callback) throws RemoteException { |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 241 | Preconditions.checkNotNull(request); |
| 242 | Preconditions.checkNotNull(callback); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 243 | validateInput(mContext, request.getCallingPackageName()); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 244 | |
| 245 | synchronized (mLock) { |
| 246 | UserState userState = getCallingUserStateLocked(); |
| 247 | if (!userState.bindLocked()) { |
| 248 | callback.onFailure(); |
| 249 | } else if (userState.isBoundLocked()) { |
| 250 | userState.mService.onDetectLanguage(sessionId, request, callback); |
| 251 | } else { |
| 252 | userState.mPendingRequests.add(new PendingRequest( |
| 253 | () -> onDetectLanguage(sessionId, request, callback), |
| 254 | callback::onFailure, callback.asBinder(), this, userState)); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void onSuggestConversationActions( |
| 261 | TextClassificationSessionId sessionId, |
| 262 | ConversationActions.Request request, |
Tony Mak | 9920dbb | 2019-01-23 19:49:30 +0000 | [diff] [blame] | 263 | ITextClassifierCallback callback) throws RemoteException { |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 264 | Preconditions.checkNotNull(request); |
| 265 | Preconditions.checkNotNull(callback); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 266 | validateInput(mContext, request.getCallingPackageName()); |
Tony Mak | 0be540b | 2018-11-09 16:58:35 +0000 | [diff] [blame] | 267 | |
| 268 | synchronized (mLock) { |
| 269 | UserState userState = getCallingUserStateLocked(); |
| 270 | if (!userState.bindLocked()) { |
| 271 | callback.onFailure(); |
| 272 | } else if (userState.isBoundLocked()) { |
| 273 | userState.mService.onSuggestConversationActions(sessionId, request, callback); |
| 274 | } else { |
| 275 | userState.mPendingRequests.add(new PendingRequest( |
| 276 | () -> onSuggestConversationActions(sessionId, request, callback), |
| 277 | callback::onFailure, callback.asBinder(), this, userState)); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 283 | public void onCreateTextClassificationSession( |
| 284 | TextClassificationContext classificationContext, TextClassificationSessionId sessionId) |
| 285 | throws RemoteException { |
| 286 | Preconditions.checkNotNull(sessionId); |
| 287 | Preconditions.checkNotNull(classificationContext); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 288 | validateInput(mContext, classificationContext.getPackageName()); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 289 | |
| 290 | synchronized (mLock) { |
| 291 | UserState userState = getCallingUserStateLocked(); |
| 292 | if (userState.isBoundLocked()) { |
| 293 | userState.mService.onCreateTextClassificationSession( |
| 294 | classificationContext, sessionId); |
| 295 | } else { |
| 296 | userState.mPendingRequests.add(new PendingRequest( |
| 297 | () -> onCreateTextClassificationSession(classificationContext, sessionId), |
| 298 | null /* onServiceFailure */, null /* binder */, this, userState)); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void onDestroyTextClassificationSession(TextClassificationSessionId sessionId) |
| 305 | throws RemoteException { |
| 306 | Preconditions.checkNotNull(sessionId); |
| 307 | |
| 308 | synchronized (mLock) { |
| 309 | UserState userState = getCallingUserStateLocked(); |
| 310 | if (userState.isBoundLocked()) { |
| 311 | userState.mService.onDestroyTextClassificationSession(sessionId); |
| 312 | } else { |
| 313 | userState.mPendingRequests.add(new PendingRequest( |
| 314 | () -> onDestroyTextClassificationSession(sessionId), |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 315 | null /* onServiceFailure */, null /* binder */, this, userState)); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 320 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 321 | private UserState getCallingUserStateLocked() { |
| 322 | return getUserStateLocked(UserHandle.getCallingUserId()); |
| 323 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 324 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 325 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 326 | private UserState getUserStateLocked(int userId) { |
| 327 | UserState result = mUserStates.get(userId); |
| 328 | if (result == null) { |
| 329 | result = new UserState(userId, mContext, mLock); |
| 330 | mUserStates.put(userId, result); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 331 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 332 | return result; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 335 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 336 | UserState peekUserStateLocked(int userId) { |
| 337 | return mUserStates.get(userId); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 340 | @Override |
| 341 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
| 342 | if (!DumpUtils.checkDumpPermission(mContext, LOG_TAG, fout)) return; |
| 343 | IndentingPrintWriter pw = new IndentingPrintWriter(fout, " "); |
| 344 | TextClassificationManager tcm = mContext.getSystemService(TextClassificationManager.class); |
| 345 | tcm.dump(pw); |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 346 | |
| 347 | pw.printPair("context", mContext); pw.println(); |
| 348 | synchronized (mLock) { |
| 349 | int size = mUserStates.size(); |
| 350 | pw.print("Number user states: "); pw.println(size); |
| 351 | if (size > 0) { |
| 352 | for (int i = 0; i < size; i++) { |
| 353 | pw.increaseIndent(); |
| 354 | UserState userState = mUserStates.valueAt(i); |
| 355 | pw.print(i); pw.print(":"); userState.dump(pw); pw.println(); |
| 356 | pw.decreaseIndent(); |
| 357 | } |
| 358 | } |
| 359 | } |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 362 | private static final class PendingRequest implements IBinder.DeathRecipient { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 363 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 364 | @Nullable private final IBinder mBinder; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 365 | @NonNull private final Runnable mRequest; |
| 366 | @Nullable private final Runnable mOnServiceFailure; |
| 367 | @GuardedBy("mLock") |
| 368 | @NonNull private final UserState mOwningUser; |
| 369 | @NonNull private final TextClassificationManagerService mService; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * Initializes a new pending request. |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 373 | * @param request action to perform when the service is bound |
| 374 | * @param onServiceFailure action to perform when the service dies or disconnects |
| 375 | * @param binder binder to the process that made this pending request |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 376 | * @param service |
| 377 | * @param owningUser |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 378 | */ |
| 379 | PendingRequest( |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 380 | @NonNull ThrowingRunnable request, @Nullable ThrowingRunnable onServiceFailure, |
| 381 | @Nullable IBinder binder, |
| 382 | TextClassificationManagerService service, |
| 383 | UserState owningUser) { |
| 384 | mRequest = |
| 385 | logOnFailure(Preconditions.checkNotNull(request), "handling pending request"); |
| 386 | mOnServiceFailure = |
| 387 | logOnFailure(onServiceFailure, "notifying callback of service failure"); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 388 | mBinder = binder; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 389 | mService = service; |
| 390 | mOwningUser = owningUser; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 391 | if (mBinder != null) { |
| 392 | try { |
| 393 | mBinder.linkToDeath(this, 0); |
| 394 | } catch (RemoteException e) { |
| 395 | e.printStackTrace(); |
| 396 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 400 | @Override |
| 401 | public void binderDied() { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 402 | synchronized (mService.mLock) { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 403 | // No need to handle this pending request anymore. Remove. |
| 404 | removeLocked(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | @GuardedBy("mLock") |
| 409 | private void removeLocked() { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 410 | mOwningUser.mPendingRequests.remove(this); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 411 | if (mBinder != null) { |
| 412 | mBinder.unlinkToDeath(this, 0); |
| 413 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 417 | private static Runnable logOnFailure(@Nullable ThrowingRunnable r, String opDesc) { |
| 418 | if (r == null) return null; |
| 419 | return FunctionalUtils.handleExceptions(r, |
| 420 | e -> Slog.d(LOG_TAG, "Error " + opDesc + ": " + e.getMessage())); |
| 421 | } |
| 422 | |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 423 | private static void validateInput(Context context, @Nullable String packageName) |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 424 | throws RemoteException { |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 425 | if (packageName == null) return; |
| 426 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 427 | try { |
| 428 | final int uid = context.getPackageManager() |
Eugene Susla | 7d3ea93 | 2018-11-26 13:59:09 -0800 | [diff] [blame] | 429 | .getPackageUidAsUser(packageName, UserHandle.getCallingUserId()); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 430 | Preconditions.checkArgument(Binder.getCallingUid() == uid); |
Abodunrinwa Toki | 0f13896 | 2018-12-03 22:35:10 +0000 | [diff] [blame] | 431 | } catch (Exception e) { |
| 432 | throw new RemoteException( |
| 433 | String.format("Invalid package: name=%s, error=%s", packageName, e)); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 436 | |
| 437 | private static final class UserState { |
| 438 | @UserIdInt final int mUserId; |
| 439 | final TextClassifierServiceConnection mConnection = new TextClassifierServiceConnection(); |
| 440 | @GuardedBy("mLock") |
| 441 | final Queue<PendingRequest> mPendingRequests = new ArrayDeque<>(); |
| 442 | @GuardedBy("mLock") |
| 443 | ITextClassifierService mService; |
| 444 | @GuardedBy("mLock") |
| 445 | boolean mBinding; |
| 446 | |
| 447 | private final Context mContext; |
| 448 | private final Object mLock; |
| 449 | |
| 450 | private UserState(int userId, Context context, Object lock) { |
| 451 | mUserId = userId; |
| 452 | mContext = Preconditions.checkNotNull(context); |
| 453 | mLock = Preconditions.checkNotNull(lock); |
| 454 | } |
| 455 | |
| 456 | @GuardedBy("mLock") |
| 457 | boolean isBoundLocked() { |
| 458 | return mService != null; |
| 459 | } |
| 460 | |
| 461 | @GuardedBy("mLock") |
| 462 | private void handlePendingRequestsLocked() { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 463 | PendingRequest request; |
| 464 | while ((request = mPendingRequests.poll()) != null) { |
| 465 | if (isBoundLocked()) { |
| 466 | request.mRequest.run(); |
| 467 | } else { |
| 468 | if (request.mOnServiceFailure != null) { |
| 469 | request.mOnServiceFailure.run(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (request.mBinder != null) { |
| 474 | request.mBinder.unlinkToDeath(request, 0); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 479 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 480 | private boolean bindIfHasPendingRequestsLocked() { |
| 481 | return !mPendingRequests.isEmpty() && bindLocked(); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * @return true if the service is bound or in the process of being bound. |
| 486 | * Returns false otherwise. |
| 487 | */ |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 488 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 489 | private boolean bindLocked() { |
| 490 | if (isBoundLocked() || mBinding) { |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | // TODO: Handle bind timeout. |
| 495 | final boolean willBind; |
| 496 | final long identity = Binder.clearCallingIdentity(); |
| 497 | try { |
| 498 | ComponentName componentName = |
| 499 | TextClassifierService.getServiceComponentName(mContext); |
| 500 | if (componentName == null) { |
| 501 | // Might happen if the storage is encrypted and the user is not unlocked |
| 502 | return false; |
| 503 | } |
| 504 | Intent serviceIntent = new Intent(TextClassifierService.SERVICE_INTERFACE) |
| 505 | .setComponent(componentName); |
| 506 | Slog.d(LOG_TAG, "Binding to " + serviceIntent.getComponent()); |
| 507 | willBind = mContext.bindServiceAsUser( |
| 508 | serviceIntent, mConnection, |
Dianne Hackborn | c390aa8 | 2019-01-09 16:38:22 -0800 | [diff] [blame] | 509 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE |
| 510 | | Context.BIND_RESTRICT_ASSOCIATIONS, |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 511 | UserHandle.of(mUserId)); |
| 512 | mBinding = willBind; |
| 513 | } finally { |
| 514 | Binder.restoreCallingIdentity(identity); |
| 515 | } |
| 516 | return willBind; |
| 517 | } |
| 518 | |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 519 | private void dump(IndentingPrintWriter pw) { |
| 520 | pw.printPair("context", mContext); |
| 521 | pw.printPair("userId", mUserId); |
| 522 | synchronized (mLock) { |
| 523 | pw.printPair("binding", mBinding); |
| 524 | pw.printPair("numberRequests", mPendingRequests.size()); |
| 525 | } |
| 526 | } |
| 527 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 528 | private final class TextClassifierServiceConnection implements ServiceConnection { |
| 529 | @Override |
| 530 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 531 | init(ITextClassifierService.Stub.asInterface(service)); |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public void onServiceDisconnected(ComponentName name) { |
| 536 | cleanupService(); |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public void onBindingDied(ComponentName name) { |
| 541 | cleanupService(); |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public void onNullBinding(ComponentName name) { |
| 546 | cleanupService(); |
| 547 | } |
| 548 | |
| 549 | void cleanupService() { |
| 550 | init(null); |
| 551 | } |
| 552 | |
| 553 | private void init(@Nullable ITextClassifierService service) { |
| 554 | synchronized (mLock) { |
| 555 | mService = service; |
| 556 | mBinding = false; |
| 557 | handlePendingRequestsLocked(); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 562 | } |