blob: 8be887b6d593123d0289188d300cb27814b699da [file] [log] [blame]
Felipe Lemeb63e0dd2018-12-18 11:56:42 -08001/*
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 */
16package android.view.contentcapture;
17
Felipe Leme87a9dc92018-12-18 14:28:07 -080018import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_FINISHED;
19import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_STARTED;
Felipe Lemeb63e0dd2018-12-18 11:56:42 -080020import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_APPEARED;
21import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_DISAPPEARED;
22import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_TEXT_CHANGED;
23import static android.view.contentcapture.ContentCaptureManager.DEBUG;
24import static android.view.contentcapture.ContentCaptureManager.VERBOSE;
25
26import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
27
28import android.annotation.NonNull;
29import android.annotation.Nullable;
30import android.content.ComponentName;
31import android.content.Context;
32import android.content.pm.ParceledListSlice;
33import android.os.Bundle;
34import android.os.Handler;
35import android.os.IBinder;
36import android.os.IBinder.DeathRecipient;
37import android.os.RemoteException;
38import android.os.SystemClock;
39import android.util.Log;
40import android.util.TimeUtils;
41import android.view.autofill.AutofillId;
42import android.view.contentcapture.ViewNode.ViewStructureImpl;
43
44import com.android.internal.os.IResultReceiver;
45
46import java.io.PrintWriter;
47import java.util.ArrayList;
48import java.util.Collections;
49import java.util.List;
50import 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 Leme87a9dc92018-12-18 14:28:07 -080064public final class MainContentCaptureSession extends ContentCaptureSession {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -080065
Felipe Lemeaf1a0e72019-01-03 11:07:25 -080066 private static final String TAG = MainContentCaptureSession.class.getSimpleName();
67
Felipe Lemeb63e0dd2018-12-18 11:56:42 -080068 /**
69 * Handler message used to flush the buffer.
70 */
71 private static final int MSG_FLUSH = 1;
72
73 /**
74 * Maximum number of events that are buffered before sent to the app.
75 */
76 // TODO(b/121044064): use settings
77 private static final int MAX_BUFFER_SIZE = 100;
78
79 /**
80 * Frequency the buffer is flushed if stale.
81 */
82 // TODO(b/121044064): use settings
83 private static final int FLUSHING_FREQUENCY_MS = 5_000;
84
85 /**
86 * Name of the {@link IResultReceiver} extra used to pass the binder interface to the service.
87 * @hide
88 */
89 public static final String EXTRA_BINDER = "binder";
90
91 @NonNull
92 private final AtomicBoolean mDisabled;
93
94 @NonNull
95 private final Context mContext;
96
97 @NonNull
98 private final Handler mHandler;
99
100 /**
101 * Interface to the system_server binder object - it's only used to start the session (and
102 * notify when the session is finished).
103 */
104 @Nullable
105 private final IContentCaptureManager mSystemServerInterface;
106
107 /**
108 * Direct interface to the service binder object - it's used to send the events, including the
109 * last ones (when the session is finished)
110 */
111 @Nullable
112 private IContentCaptureDirectManager mDirectServiceInterface;
113 @Nullable
114 private DeathRecipient mDirectServiceVulture;
115
116 private int mState = STATE_UNKNOWN;
117
118 @Nullable
119 private IBinder mApplicationToken;
120
121 @Nullable
122 private ComponentName mComponentName;
123
124 /**
125 * List of events held to be sent as a batch.
126 */
127 @Nullable
128 private ArrayList<ContentCaptureEvent> mEvents;
129
130 // Used just for debugging purposes (on dump)
131 private long mNextFlush;
132
Felipe Leme87a9dc92018-12-18 14:28:07 -0800133 /** @hide */
134 protected MainContentCaptureSession(@NonNull Context context, @NonNull Handler handler,
135 @Nullable IContentCaptureManager systemServerInterface,
136 @NonNull AtomicBoolean disabled) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800137 mContext = context;
138 mHandler = handler;
139 mSystemServerInterface = systemServerInterface;
140 mDisabled = disabled;
141 }
142
Felipe Leme87a9dc92018-12-18 14:28:07 -0800143 @Override
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800144 MainContentCaptureSession getMainCaptureSession() {
145 return this;
146 }
147
148 @Override
Felipe Leme87a9dc92018-12-18 14:28:07 -0800149 ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) {
150 final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext);
151 notifyChildSessionStarted(mId, child.mId, clientContext);
152 return child;
153 }
154
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800155 /**
156 * Starts this session.
157 *
158 * @hide
159 */
160 void start(@NonNull IBinder applicationToken, @NonNull ComponentName activityComponent) {
161 if (!isContentCaptureEnabled()) return;
162
163 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800164 Log.v(TAG, "start(): token=" + applicationToken + ", comp="
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800165 + ComponentName.flattenToShortString(activityComponent));
166 }
167
Felipe Leme87a9dc92018-12-18 14:28:07 -0800168 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleStartSession, this,
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800169 applicationToken, activityComponent));
170 }
171
172 @Override
173 void flush() {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800174 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleForceFlush, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800175 }
176
177 @Override
178 void onDestroy() {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800179 mHandler.removeMessages(MSG_FLUSH);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800180 mHandler.sendMessage(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800181 obtainMessage(MainContentCaptureSession::handleDestroySession, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800182 }
183
184 private void handleStartSession(@NonNull IBinder token, @NonNull ComponentName componentName) {
185 if (mState != STATE_UNKNOWN) {
186 // TODO(b/111276913): revisit this scenario
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800187 Log.w(TAG, "ignoring handleStartSession(" + token + ") while on state "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800188 + getStateAsString(mState));
189 return;
190 }
191 mState = STATE_WAITING_FOR_SERVER;
192 mApplicationToken = token;
193 mComponentName = componentName;
194
195 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800196 Log.v(TAG, "handleStartSession(): token=" + token + ", act="
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800197 + getActivityDebugName() + ", id=" + mId);
198 }
199 final int flags = 0; // TODO(b/111276913): get proper flags
200
201 try {
Adam He6884fed2019-01-07 13:18:32 -0800202 if (mSystemServerInterface == null) return;
203
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800204 mSystemServerInterface.startSession(mContext.getUserId(), mApplicationToken,
Felipe Leme87a9dc92018-12-18 14:28:07 -0800205 componentName, mId, flags, new IResultReceiver.Stub() {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800206 @Override
207 public void send(int resultCode, Bundle resultData) {
208 IBinder binder = null;
209 if (resultData != null) {
210 binder = resultData.getBinder(EXTRA_BINDER);
211 if (binder == null) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800212 Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800213 handleResetState();
214 return;
215 }
216 }
217 handleSessionStarted(resultCode, binder);
218 }
219 });
220 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800221 Log.w(TAG, "Error starting session for " + componentName.flattenToShortString() + ": "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800222 + e);
223 }
224 }
225
226 /**
227 * Callback from {@code system_server} after call to
228 * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String,
Felipe Lemebef744c2019-01-03 11:07:25 -0800229 * int, IResultReceiver)}.
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800230 *
231 * @param resultCode session state
232 * @param binder handle to {@code IContentCaptureDirectManager}
233 */
234 private void handleSessionStarted(int resultCode, @Nullable IBinder binder) {
235 mState = resultCode;
236 if (binder != null) {
237 mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder);
238 mDirectServiceVulture = () -> {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800239 Log.w(TAG, "Destroying session " + mId + " because service died");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800240 destroy();
241 };
242 try {
243 binder.linkToDeath(mDirectServiceVulture, 0);
244 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800245 Log.w(TAG, "Failed to link to death on " + binder + ": " + e);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800246 }
247 }
Felipe Lemee127c9a2019-01-04 14:56:38 -0800248 if (resultCode == STATE_DISABLED_NO_SERVICE || resultCode == STATE_DISABLED_DUPLICATED_ID) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800249 mDisabled.set(true);
250 handleResetSession(/* resetState= */ false);
251 } else {
252 mDisabled.set(false);
253 }
254 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800255 Log.v(TAG, "handleSessionStarted() result: code=" + resultCode + ", id=" + mId
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800256 + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get()
Felipe Lemee127c9a2019-01-04 14:56:38 -0800257 + ", binder=" + binder + ", events=" + (mEvents == null ? 0 : mEvents.size()));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800258 }
259 }
260
261 private void handleSendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) {
262 if (mEvents == null) {
263 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800264 Log.v(TAG, "Creating buffer for " + MAX_BUFFER_SIZE + " events");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800265 }
266 mEvents = new ArrayList<>(MAX_BUFFER_SIZE);
267 }
Adam Heac132652019-01-02 14:40:15 -0800268
269 if (!mEvents.isEmpty() && event.getType() == TYPE_VIEW_TEXT_CHANGED) {
270 final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1);
271
272 // TODO(b/121045053): check if flags match
273 if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED
274 && lastEvent.getId().equals(event.getId())) {
275 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800276 Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text = "
Adam Heac132652019-01-02 14:40:15 -0800277 + event.getText());
278 }
279 lastEvent.setText(event.getText());
280 } else {
281 mEvents.add(event);
282 }
283 } else {
284 mEvents.add(event);
285 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800286
287 final int numberEvents = mEvents.size();
288
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800289 final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE;
290
291 if (bufferEvent && !forceFlush) {
292 handleScheduleFlush(/* checkExisting= */ true);
293 return;
294 }
295
Felipe Lemee127c9a2019-01-04 14:56:38 -0800296 if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800297 // Callback from startSession hasn't been called yet - typically happens on system
298 // apps that are started before the system service
299 // TODO(b/111276913): try to ignore session while system is not ready / boot
300 // not complete instead. Similarly, the manager service should return right away
301 // when the user does not have a service set
Felipe Lemee127c9a2019-01-04 14:56:38 -0800302 if (DEBUG) {
303 Log.d(TAG, "Closing session for " + getActivityDebugName()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800304 + " after " + numberEvents + " delayed events and state "
305 + getStateAsString(mState));
306 }
307 handleResetState();
308 // TODO(b/111276913): blacklist activity / use special flag to indicate that
309 // when it's launched again
310 return;
311 }
312
313 handleForceFlush();
314 }
315
316 private void handleScheduleFlush(boolean checkExisting) {
317 if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) {
318 // "Renew" the flush message by removing the previous one
319 mHandler.removeMessages(MSG_FLUSH);
320 }
321 mNextFlush = SystemClock.elapsedRealtime() + FLUSHING_FREQUENCY_MS;
322 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800323 Log.v(TAG, "Scheduled to flush in " + FLUSHING_FREQUENCY_MS + "ms: " + mNextFlush);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800324 }
325 mHandler.sendMessageDelayed(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800326 obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800327 .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS);
328 }
329
330 private void handleFlushIfNeeded() {
331 if (mEvents.isEmpty()) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800332 if (VERBOSE) Log.v(TAG, "Nothing to flush");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800333 return;
334 }
335 handleForceFlush();
336 }
337
338 private void handleForceFlush() {
339 if (mEvents == null) return;
340
341 if (mDirectServiceInterface == null) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800342 if (VERBOSE) {
343 Log.v(TAG, "handleForceFlush(): hold your horses, client not ready: " + mEvents);
344 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800345 if (!mHandler.hasMessages(MSG_FLUSH)) {
346 handleScheduleFlush(/* checkExisting= */ false);
347 }
348 return;
349 }
350
351 final int numberEvents = mEvents.size();
352 try {
353 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800354 Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getActivityDebugName());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800355 }
356 mHandler.removeMessages(MSG_FLUSH);
357
358 final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents();
Felipe Lemefc24bea2018-12-18 13:19:01 -0800359 mDirectServiceInterface.sendEvents(events);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800360 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800361 Log.w(TAG, "Error sending " + numberEvents + " for " + getActivityDebugName()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800362 + ": " + e);
363 }
364 }
365
366 /**
367 * Resets the buffer and return a {@link ParceledListSlice} with the previous events.
368 */
369 @NonNull
370 private ParceledListSlice<ContentCaptureEvent> handleClearEvents() {
371 // NOTE: we must save a reference to the current mEvents and then set it to to null,
372 // otherwise clearing it would clear it in the receiving side if the service is also local.
373 final List<ContentCaptureEvent> events = mEvents == null
374 ? Collections.emptyList()
375 : mEvents;
376 mEvents = null;
377 return new ParceledListSlice<>(events);
378 }
379
380 private void handleDestroySession() {
381 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800382 Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800383 + (mEvents == null ? 0 : mEvents.size()) + " event(s) for "
384 + getActivityDebugName());
385 }
386
387 try {
Adam He6884fed2019-01-07 13:18:32 -0800388 if (mSystemServerInterface == null) return;
389
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800390 mSystemServerInterface.finishSession(mContext.getUserId(), mId);
391 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800392 Log.e(TAG, "Error destroying system-service session " + mId + " for "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800393 + getActivityDebugName() + ": " + e);
394 }
395 }
396
397 private void handleResetState() {
398 handleResetSession(/* resetState= */ true);
399 }
400
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800401 // TODO(b/122454205): once we support multiple sessions, we might need to move some of these
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800402 // clearings out.
403 private void handleResetSession(boolean resetState) {
404 if (resetState) {
405 mState = STATE_UNKNOWN;
406 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800407
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800408 // TODO(b/122454205): must reset children (which currently is owned by superclass)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800409 mApplicationToken = null;
410 mComponentName = null;
411 mEvents = null;
412 if (mDirectServiceInterface != null) {
413 mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0);
414 }
415 mDirectServiceInterface = null;
416 mHandler.removeMessages(MSG_FLUSH);
417 }
418
419 @Override
420 void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800421 notifyViewAppeared(mId, node);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800422 }
423
424 @Override
425 void internalNotifyViewDisappeared(@NonNull AutofillId id) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800426 notifyViewDisappeared(mId, id);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800427 }
428
429 @Override
430 void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text,
431 int flags) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800432 notifyViewTextChanged(mId, id, text, flags);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800433 }
434
435 @Override
436 boolean isContentCaptureEnabled() {
Felipe Lemebef744c2019-01-03 11:07:25 -0800437 return super.isContentCaptureEnabled() && mSystemServerInterface != null
438 && !mDisabled.get();
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800439 }
440
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800441 // TODO(b/122454205): refactor "notifyXXXX" methods below to a common "Buffer" object that is
Felipe Leme87a9dc92018-12-18 14:28:07 -0800442 // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such
443 // change should also get get rid of the "internalNotifyXXXX" methods above
444 void notifyChildSessionStarted(@NonNull String parentSessionId,
445 @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) {
446 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
447 new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED)
448 .setParentSessionId(parentSessionId)
449 .setClientContext(clientContext),
Felipe Lemee127c9a2019-01-04 14:56:38 -0800450 /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800451 }
452
453 void notifyChildSessionFinished(@NonNull String parentSessionId,
454 @NonNull String childSessionId) {
455 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
456 new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED)
Felipe Lemee127c9a2019-01-04 14:56:38 -0800457 .setParentSessionId(parentSessionId), /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800458 }
459
460 void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) {
461 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
462 new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED)
463 .setViewNode(node.mNode), /* forceFlush= */ false));
464 }
465
466 void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) {
467 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
468 new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id),
469 /* forceFlush= */ false));
470 }
471
472 void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id,
473 @Nullable CharSequence text, int flags) {
474 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
475 new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id)
476 .setText(text), /* forceFlush= */ false));
477 }
478
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800479 @Override
480 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
481 pw.print(prefix); pw.print("id: "); pw.println(mId);
482 pw.print(prefix); pw.print("mContext: "); pw.println(mContext);
483 pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId());
484 if (mSystemServerInterface != null) {
485 pw.print(prefix); pw.print("mSystemServerInterface: ");
486 pw.println(mSystemServerInterface);
487 }
488 if (mDirectServiceInterface != null) {
489 pw.print(prefix); pw.print("mDirectServiceInterface: ");
490 pw.println(mDirectServiceInterface);
491 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800492 pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get());
493 pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800494 pw.print(prefix); pw.print("state: "); pw.print(mState); pw.print(" (");
495 pw.print(getStateAsString(mState)); pw.println(")");
496 if (mApplicationToken != null) {
497 pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken);
498 }
499 if (mComponentName != null) {
500 pw.print(prefix); pw.print("component name: ");
501 pw.println(mComponentName.flattenToShortString());
502 }
503 if (mEvents != null && !mEvents.isEmpty()) {
504 final int numberEvents = mEvents.size();
505 pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents);
506 pw.print('/'); pw.println(MAX_BUFFER_SIZE);
507 if (VERBOSE && numberEvents > 0) {
508 final String prefix3 = prefix + " ";
509 for (int i = 0; i < numberEvents; i++) {
510 final ContentCaptureEvent event = mEvents.get(i);
511 pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw);
512 pw.println();
513 }
514 }
515 pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS);
516 pw.print(prefix); pw.print("next flush: ");
517 TimeUtils.formatDuration(mNextFlush - SystemClock.elapsedRealtime(), pw); pw.println();
518 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800519 super.dump(prefix, pw);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800520 }
521
522 /**
523 * Gets a string that can be used to identify the activity on logging statements.
524 */
525 private String getActivityDebugName() {
526 return mComponentName == null ? mContext.getPackageName()
527 : mComponentName.flattenToShortString();
528 }
529}