Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -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 | package android.view.contentcapture; |
| 17 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 18 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_FINISHED; |
| 19 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_STARTED; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 20 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_APPEARED; |
| 21 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_DISAPPEARED; |
| 22 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_TEXT_CHANGED; |
| 23 | import static android.view.contentcapture.ContentCaptureManager.DEBUG; |
| 24 | import static android.view.contentcapture.ContentCaptureManager.VERBOSE; |
| 25 | |
| 26 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 27 | |
| 28 | import android.annotation.NonNull; |
| 29 | import android.annotation.Nullable; |
| 30 | import android.content.ComponentName; |
| 31 | import android.content.Context; |
| 32 | import android.content.pm.ParceledListSlice; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.Handler; |
| 35 | import android.os.IBinder; |
| 36 | import android.os.IBinder.DeathRecipient; |
| 37 | import android.os.RemoteException; |
| 38 | import android.os.SystemClock; |
| 39 | import android.util.Log; |
| 40 | import android.util.TimeUtils; |
| 41 | import android.view.autofill.AutofillId; |
| 42 | import android.view.contentcapture.ViewNode.ViewStructureImpl; |
| 43 | |
| 44 | import com.android.internal.os.IResultReceiver; |
| 45 | |
| 46 | import java.io.PrintWriter; |
| 47 | import java.util.ArrayList; |
| 48 | import java.util.Collections; |
| 49 | import java.util.List; |
| 50 | import java.util.concurrent.atomic.AtomicBoolean; |
| 51 | |
| 52 | /** |
| 53 | * Main session associated with a context. |
| 54 | * |
| 55 | * <p>This session is created when the activity starts and finished when it stops; clients can use |
| 56 | * it to create children activities. |
| 57 | * |
| 58 | * <p><b>NOTE: all methods in this class should return right away, or do the real work in a handler |
| 59 | * thread. Hence, the only field that must be thread-safe is {@code mEnabled}, which is called at |
| 60 | * the beginning of every method. |
| 61 | * |
| 62 | * @hide |
| 63 | */ |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 64 | public final class MainContentCaptureSession extends ContentCaptureSession { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Handler message used to flush the buffer. |
| 68 | */ |
| 69 | private static final int MSG_FLUSH = 1; |
| 70 | |
| 71 | /** |
| 72 | * Maximum number of events that are buffered before sent to the app. |
| 73 | */ |
| 74 | // TODO(b/121044064): use settings |
| 75 | private static final int MAX_BUFFER_SIZE = 100; |
| 76 | |
| 77 | /** |
| 78 | * Frequency the buffer is flushed if stale. |
| 79 | */ |
| 80 | // TODO(b/121044064): use settings |
| 81 | private static final int FLUSHING_FREQUENCY_MS = 5_000; |
| 82 | |
| 83 | /** |
| 84 | * Name of the {@link IResultReceiver} extra used to pass the binder interface to the service. |
| 85 | * @hide |
| 86 | */ |
| 87 | public static final String EXTRA_BINDER = "binder"; |
| 88 | |
| 89 | @NonNull |
| 90 | private final AtomicBoolean mDisabled; |
| 91 | |
| 92 | @NonNull |
| 93 | private final Context mContext; |
| 94 | |
| 95 | @NonNull |
| 96 | private final Handler mHandler; |
| 97 | |
| 98 | /** |
| 99 | * Interface to the system_server binder object - it's only used to start the session (and |
| 100 | * notify when the session is finished). |
| 101 | */ |
| 102 | @Nullable |
| 103 | private final IContentCaptureManager mSystemServerInterface; |
| 104 | |
| 105 | /** |
| 106 | * Direct interface to the service binder object - it's used to send the events, including the |
| 107 | * last ones (when the session is finished) |
| 108 | */ |
| 109 | @Nullable |
| 110 | private IContentCaptureDirectManager mDirectServiceInterface; |
| 111 | @Nullable |
| 112 | private DeathRecipient mDirectServiceVulture; |
| 113 | |
| 114 | private int mState = STATE_UNKNOWN; |
| 115 | |
| 116 | @Nullable |
| 117 | private IBinder mApplicationToken; |
| 118 | |
| 119 | @Nullable |
| 120 | private ComponentName mComponentName; |
| 121 | |
| 122 | /** |
| 123 | * List of events held to be sent as a batch. |
| 124 | */ |
| 125 | @Nullable |
| 126 | private ArrayList<ContentCaptureEvent> mEvents; |
| 127 | |
| 128 | // Used just for debugging purposes (on dump) |
| 129 | private long mNextFlush; |
| 130 | |
| 131 | // Lazily created on demand. |
| 132 | private ContentCaptureSessionId mContentCaptureSessionId; |
| 133 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 134 | /** @hide */ |
| 135 | protected MainContentCaptureSession(@NonNull Context context, @NonNull Handler handler, |
| 136 | @Nullable IContentCaptureManager systemServerInterface, |
| 137 | @NonNull AtomicBoolean disabled) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 138 | mContext = context; |
| 139 | mHandler = handler; |
| 140 | mSystemServerInterface = systemServerInterface; |
| 141 | mDisabled = disabled; |
| 142 | } |
| 143 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 144 | @Override |
| 145 | ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) { |
| 146 | final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext); |
| 147 | notifyChildSessionStarted(mId, child.mId, clientContext); |
| 148 | return child; |
| 149 | } |
| 150 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 151 | /** |
| 152 | * Starts this session. |
| 153 | * |
| 154 | * @hide |
| 155 | */ |
| 156 | void start(@NonNull IBinder applicationToken, @NonNull ComponentName activityComponent) { |
| 157 | if (!isContentCaptureEnabled()) return; |
| 158 | |
| 159 | if (VERBOSE) { |
| 160 | Log.v(mTag, "start(): token=" + applicationToken + ", comp=" |
| 161 | + ComponentName.flattenToShortString(activityComponent)); |
| 162 | } |
| 163 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 164 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleStartSession, this, |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 165 | applicationToken, activityComponent)); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | void flush() { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 170 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleForceFlush, this)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
| 174 | void onDestroy() { |
| 175 | mHandler.sendMessage( |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 176 | obtainMessage(MainContentCaptureSession::handleDestroySession, this)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private void handleStartSession(@NonNull IBinder token, @NonNull ComponentName componentName) { |
| 180 | if (mState != STATE_UNKNOWN) { |
| 181 | // TODO(b/111276913): revisit this scenario |
| 182 | Log.w(mTag, "ignoring handleStartSession(" + token + ") while on state " |
| 183 | + getStateAsString(mState)); |
| 184 | return; |
| 185 | } |
| 186 | mState = STATE_WAITING_FOR_SERVER; |
| 187 | mApplicationToken = token; |
| 188 | mComponentName = componentName; |
| 189 | |
| 190 | if (VERBOSE) { |
| 191 | Log.v(mTag, "handleStartSession(): token=" + token + ", act=" |
| 192 | + getActivityDebugName() + ", id=" + mId); |
| 193 | } |
| 194 | final int flags = 0; // TODO(b/111276913): get proper flags |
| 195 | |
| 196 | try { |
| 197 | mSystemServerInterface.startSession(mContext.getUserId(), mApplicationToken, |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 198 | componentName, mId, flags, new IResultReceiver.Stub() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 199 | @Override |
| 200 | public void send(int resultCode, Bundle resultData) { |
| 201 | IBinder binder = null; |
| 202 | if (resultData != null) { |
| 203 | binder = resultData.getBinder(EXTRA_BINDER); |
| 204 | if (binder == null) { |
| 205 | Log.wtf(mTag, "No " + EXTRA_BINDER + " extra result"); |
| 206 | handleResetState(); |
| 207 | return; |
| 208 | } |
| 209 | } |
| 210 | handleSessionStarted(resultCode, binder); |
| 211 | } |
| 212 | }); |
| 213 | } catch (RemoteException e) { |
| 214 | Log.w(mTag, "Error starting session for " + componentName.flattenToShortString() + ": " |
| 215 | + e); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Callback from {@code system_server} after call to |
| 221 | * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String, |
| 222 | * ContentCaptureContext, int, IResultReceiver)}. |
| 223 | * |
| 224 | * @param resultCode session state |
| 225 | * @param binder handle to {@code IContentCaptureDirectManager} |
| 226 | */ |
| 227 | private void handleSessionStarted(int resultCode, @Nullable IBinder binder) { |
| 228 | mState = resultCode; |
| 229 | if (binder != null) { |
| 230 | mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder); |
| 231 | mDirectServiceVulture = () -> { |
| 232 | Log.w(mTag, "Destroying session " + mId + " because service died"); |
| 233 | destroy(); |
| 234 | }; |
| 235 | try { |
| 236 | binder.linkToDeath(mDirectServiceVulture, 0); |
| 237 | } catch (RemoteException e) { |
| 238 | Log.w(mTag, "Failed to link to death on " + binder + ": " + e); |
| 239 | } |
| 240 | } |
| 241 | if (resultCode == STATE_DISABLED || resultCode == STATE_DISABLED_DUPLICATED_ID) { |
| 242 | mDisabled.set(true); |
| 243 | handleResetSession(/* resetState= */ false); |
| 244 | } else { |
| 245 | mDisabled.set(false); |
| 246 | } |
| 247 | if (VERBOSE) { |
| 248 | Log.v(mTag, "handleSessionStarted() result: code=" + resultCode + ", id=" + mId |
| 249 | + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get() |
| 250 | + ", binder=" + binder); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | private void handleSendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) { |
| 255 | if (mEvents == null) { |
| 256 | if (VERBOSE) { |
| 257 | Log.v(mTag, "Creating buffer for " + MAX_BUFFER_SIZE + " events"); |
| 258 | } |
| 259 | mEvents = new ArrayList<>(MAX_BUFFER_SIZE); |
| 260 | } |
| 261 | mEvents.add(event); |
| 262 | |
| 263 | final int numberEvents = mEvents.size(); |
| 264 | |
| 265 | // TODO(b/120784831): need to optimize it so we buffer changes until a number of X are |
| 266 | // buffered (either total or per autofillid). For |
| 267 | // example, if the user typed "a", "b", "c" and the threshold is 3, we should buffer |
| 268 | // "a" and "b" then send "abc". |
| 269 | final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE; |
| 270 | |
| 271 | if (bufferEvent && !forceFlush) { |
| 272 | handleScheduleFlush(/* checkExisting= */ true); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | if (mState != STATE_ACTIVE) { |
| 277 | // Callback from startSession hasn't been called yet - typically happens on system |
| 278 | // apps that are started before the system service |
| 279 | // TODO(b/111276913): try to ignore session while system is not ready / boot |
| 280 | // not complete instead. Similarly, the manager service should return right away |
| 281 | // when the user does not have a service set |
| 282 | if (VERBOSE) { |
| 283 | Log.v(mTag, "Closing session for " + getActivityDebugName() |
| 284 | + " after " + numberEvents + " delayed events and state " |
| 285 | + getStateAsString(mState)); |
| 286 | } |
| 287 | handleResetState(); |
| 288 | // TODO(b/111276913): blacklist activity / use special flag to indicate that |
| 289 | // when it's launched again |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | handleForceFlush(); |
| 294 | } |
| 295 | |
| 296 | private void handleScheduleFlush(boolean checkExisting) { |
| 297 | if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) { |
| 298 | // "Renew" the flush message by removing the previous one |
| 299 | mHandler.removeMessages(MSG_FLUSH); |
| 300 | } |
| 301 | mNextFlush = SystemClock.elapsedRealtime() + FLUSHING_FREQUENCY_MS; |
| 302 | if (VERBOSE) { |
| 303 | Log.v(mTag, "Scheduled to flush in " + FLUSHING_FREQUENCY_MS + "ms: " + mNextFlush); |
| 304 | } |
| 305 | mHandler.sendMessageDelayed( |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 306 | obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 307 | .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS); |
| 308 | } |
| 309 | |
| 310 | private void handleFlushIfNeeded() { |
| 311 | if (mEvents.isEmpty()) { |
| 312 | if (VERBOSE) Log.v(mTag, "Nothing to flush"); |
| 313 | return; |
| 314 | } |
| 315 | handleForceFlush(); |
| 316 | } |
| 317 | |
| 318 | private void handleForceFlush() { |
| 319 | if (mEvents == null) return; |
| 320 | |
| 321 | if (mDirectServiceInterface == null) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 322 | if (DEBUG) Log.d(mTag, "handleForceFlush(): hold your horses, client not ready yet!"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 323 | if (!mHandler.hasMessages(MSG_FLUSH)) { |
| 324 | handleScheduleFlush(/* checkExisting= */ false); |
| 325 | } |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | final int numberEvents = mEvents.size(); |
| 330 | try { |
| 331 | if (DEBUG) { |
| 332 | Log.d(mTag, "Flushing " + numberEvents + " event(s) for " + getActivityDebugName()); |
| 333 | } |
| 334 | mHandler.removeMessages(MSG_FLUSH); |
| 335 | |
| 336 | final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents(); |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 337 | mDirectServiceInterface.sendEvents(events); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 338 | } catch (RemoteException e) { |
| 339 | Log.w(mTag, "Error sending " + numberEvents + " for " + getActivityDebugName() |
| 340 | + ": " + e); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Resets the buffer and return a {@link ParceledListSlice} with the previous events. |
| 346 | */ |
| 347 | @NonNull |
| 348 | private ParceledListSlice<ContentCaptureEvent> handleClearEvents() { |
| 349 | // NOTE: we must save a reference to the current mEvents and then set it to to null, |
| 350 | // otherwise clearing it would clear it in the receiving side if the service is also local. |
| 351 | final List<ContentCaptureEvent> events = mEvents == null |
| 352 | ? Collections.emptyList() |
| 353 | : mEvents; |
| 354 | mEvents = null; |
| 355 | return new ParceledListSlice<>(events); |
| 356 | } |
| 357 | |
| 358 | private void handleDestroySession() { |
| 359 | if (DEBUG) { |
| 360 | Log.d(mTag, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with " |
| 361 | + (mEvents == null ? 0 : mEvents.size()) + " event(s) for " |
| 362 | + getActivityDebugName()); |
| 363 | } |
| 364 | |
| 365 | try { |
| 366 | mSystemServerInterface.finishSession(mContext.getUserId(), mId); |
| 367 | } catch (RemoteException e) { |
| 368 | Log.e(mTag, "Error destroying system-service session " + mId + " for " |
| 369 | + getActivityDebugName() + ": " + e); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | private void handleResetState() { |
| 374 | handleResetSession(/* resetState= */ true); |
| 375 | } |
| 376 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 377 | // TODO(b/121033016): once we support multiple sessions, we might need to move some of these |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 378 | // clearings out. |
| 379 | private void handleResetSession(boolean resetState) { |
| 380 | if (resetState) { |
| 381 | mState = STATE_UNKNOWN; |
| 382 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 383 | |
| 384 | // TODO(b/121033016): must reset children (which currently is owned by superclass) |
| 385 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 386 | mContentCaptureSessionId = null; |
| 387 | mApplicationToken = null; |
| 388 | mComponentName = null; |
| 389 | mEvents = null; |
| 390 | if (mDirectServiceInterface != null) { |
| 391 | mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0); |
| 392 | } |
| 393 | mDirectServiceInterface = null; |
| 394 | mHandler.removeMessages(MSG_FLUSH); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 399 | notifyViewAppeared(mId, node); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | @Override |
| 403 | void internalNotifyViewDisappeared(@NonNull AutofillId id) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 404 | notifyViewDisappeared(mId, id); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text, |
| 409 | int flags) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 410 | notifyViewTextChanged(mId, id, text, flags); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | @Override |
| 414 | boolean isContentCaptureEnabled() { |
| 415 | return mSystemServerInterface != null && !mDisabled.get(); |
| 416 | } |
| 417 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 418 | // TODO(b/121033016): refactor "notifyXXXX" methods below to a common "Buffer" object that is |
| 419 | // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such |
| 420 | // change should also get get rid of the "internalNotifyXXXX" methods above |
| 421 | void notifyChildSessionStarted(@NonNull String parentSessionId, |
| 422 | @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) { |
| 423 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 424 | new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED) |
| 425 | .setParentSessionId(parentSessionId) |
| 426 | .setClientContext(clientContext), |
| 427 | /* forceFlush= */ false)); |
| 428 | } |
| 429 | |
| 430 | void notifyChildSessionFinished(@NonNull String parentSessionId, |
| 431 | @NonNull String childSessionId) { |
| 432 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 433 | new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED) |
| 434 | .setParentSessionId(parentSessionId), /* forceFlush= */ false)); |
| 435 | } |
| 436 | |
| 437 | void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) { |
| 438 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 439 | new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED) |
| 440 | .setViewNode(node.mNode), /* forceFlush= */ false)); |
| 441 | } |
| 442 | |
| 443 | void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) { |
| 444 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 445 | new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id), |
| 446 | /* forceFlush= */ false)); |
| 447 | } |
| 448 | |
| 449 | void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id, |
| 450 | @Nullable CharSequence text, int flags) { |
| 451 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 452 | new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id) |
| 453 | .setText(text), /* forceFlush= */ false)); |
| 454 | } |
| 455 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 456 | @Override |
| 457 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
| 458 | pw.print(prefix); pw.print("id: "); pw.println(mId); |
| 459 | pw.print(prefix); pw.print("mContext: "); pw.println(mContext); |
| 460 | pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId()); |
| 461 | if (mSystemServerInterface != null) { |
| 462 | pw.print(prefix); pw.print("mSystemServerInterface: "); |
| 463 | pw.println(mSystemServerInterface); |
| 464 | } |
| 465 | if (mDirectServiceInterface != null) { |
| 466 | pw.print(prefix); pw.print("mDirectServiceInterface: "); |
| 467 | pw.println(mDirectServiceInterface); |
| 468 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 469 | pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get()); |
| 470 | pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled()); |
| 471 | if (mContentCaptureSessionId != null) { |
| 472 | pw.print(prefix); pw.print("public id: "); pw.println(mContentCaptureSessionId); |
| 473 | } |
| 474 | pw.print(prefix); pw.print("state: "); pw.print(mState); pw.print(" ("); |
| 475 | pw.print(getStateAsString(mState)); pw.println(")"); |
| 476 | if (mApplicationToken != null) { |
| 477 | pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken); |
| 478 | } |
| 479 | if (mComponentName != null) { |
| 480 | pw.print(prefix); pw.print("component name: "); |
| 481 | pw.println(mComponentName.flattenToShortString()); |
| 482 | } |
| 483 | if (mEvents != null && !mEvents.isEmpty()) { |
| 484 | final int numberEvents = mEvents.size(); |
| 485 | pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents); |
| 486 | pw.print('/'); pw.println(MAX_BUFFER_SIZE); |
| 487 | if (VERBOSE && numberEvents > 0) { |
| 488 | final String prefix3 = prefix + " "; |
| 489 | for (int i = 0; i < numberEvents; i++) { |
| 490 | final ContentCaptureEvent event = mEvents.get(i); |
| 491 | pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw); |
| 492 | pw.println(); |
| 493 | } |
| 494 | } |
| 495 | pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS); |
| 496 | pw.print(prefix); pw.print("next flush: "); |
| 497 | TimeUtils.formatDuration(mNextFlush - SystemClock.elapsedRealtime(), pw); pw.println(); |
| 498 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 499 | super.dump(prefix, pw); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Gets a string that can be used to identify the activity on logging statements. |
| 504 | */ |
| 505 | private String getActivityDebugName() { |
| 506 | return mComponentName == null ? mContext.getPackageName() |
| 507 | : mComponentName.flattenToShortString(); |
| 508 | } |
| 509 | } |