blob: c23c85a97e9a6237a8ccd2e76022b9976140dc5f [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
144 ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) {
145 final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext);
146 notifyChildSessionStarted(mId, child.mId, clientContext);
147 return child;
148 }
149
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800150 /**
151 * Starts this session.
152 *
153 * @hide
154 */
155 void start(@NonNull IBinder applicationToken, @NonNull ComponentName activityComponent) {
156 if (!isContentCaptureEnabled()) return;
157
158 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800159 Log.v(TAG, "start(): token=" + applicationToken + ", comp="
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800160 + ComponentName.flattenToShortString(activityComponent));
161 }
162
Felipe Leme87a9dc92018-12-18 14:28:07 -0800163 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleStartSession, this,
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800164 applicationToken, activityComponent));
165 }
166
167 @Override
168 void flush() {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800169 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleForceFlush, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800170 }
171
172 @Override
173 void onDestroy() {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800174 mHandler.removeMessages(MSG_FLUSH);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800175 mHandler.sendMessage(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800176 obtainMessage(MainContentCaptureSession::handleDestroySession, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800177 }
178
179 private void handleStartSession(@NonNull IBinder token, @NonNull ComponentName componentName) {
180 if (mState != STATE_UNKNOWN) {
181 // TODO(b/111276913): revisit this scenario
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800182 Log.w(TAG, "ignoring handleStartSession(" + token + ") while on state "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800183 + getStateAsString(mState));
184 return;
185 }
186 mState = STATE_WAITING_FOR_SERVER;
187 mApplicationToken = token;
188 mComponentName = componentName;
189
190 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800191 Log.v(TAG, "handleStartSession(): token=" + token + ", act="
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800192 + 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 Leme87a9dc92018-12-18 14:28:07 -0800198 componentName, mId, flags, new IResultReceiver.Stub() {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800199 @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) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800205 Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800206 handleResetState();
207 return;
208 }
209 }
210 handleSessionStarted(resultCode, binder);
211 }
212 });
213 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800214 Log.w(TAG, "Error starting session for " + componentName.flattenToShortString() + ": "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800215 + e);
216 }
217 }
218
219 /**
220 * Callback from {@code system_server} after call to
221 * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String,
Felipe Lemebef744c2019-01-03 11:07:25 -0800222 * int, IResultReceiver)}.
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800223 *
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 = () -> {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800232 Log.w(TAG, "Destroying session " + mId + " because service died");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800233 destroy();
234 };
235 try {
236 binder.linkToDeath(mDirectServiceVulture, 0);
237 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800238 Log.w(TAG, "Failed to link to death on " + binder + ": " + e);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800239 }
240 }
Felipe Lemee127c9a2019-01-04 14:56:38 -0800241 if (resultCode == STATE_DISABLED_NO_SERVICE || resultCode == STATE_DISABLED_DUPLICATED_ID) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800242 mDisabled.set(true);
243 handleResetSession(/* resetState= */ false);
244 } else {
245 mDisabled.set(false);
246 }
247 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800248 Log.v(TAG, "handleSessionStarted() result: code=" + resultCode + ", id=" + mId
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800249 + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get()
Felipe Lemee127c9a2019-01-04 14:56:38 -0800250 + ", binder=" + binder + ", events=" + (mEvents == null ? 0 : mEvents.size()));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800251 }
252 }
253
254 private void handleSendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) {
255 if (mEvents == null) {
256 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800257 Log.v(TAG, "Creating buffer for " + MAX_BUFFER_SIZE + " events");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800258 }
259 mEvents = new ArrayList<>(MAX_BUFFER_SIZE);
260 }
Adam Heac132652019-01-02 14:40:15 -0800261
262 if (!mEvents.isEmpty() && event.getType() == TYPE_VIEW_TEXT_CHANGED) {
263 final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1);
264
265 // TODO(b/121045053): check if flags match
266 if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED
267 && lastEvent.getId().equals(event.getId())) {
268 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800269 Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text = "
Adam Heac132652019-01-02 14:40:15 -0800270 + event.getText());
271 }
272 lastEvent.setText(event.getText());
273 } else {
274 mEvents.add(event);
275 }
276 } else {
277 mEvents.add(event);
278 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800279
280 final int numberEvents = mEvents.size();
281
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800282 final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE;
283
284 if (bufferEvent && !forceFlush) {
285 handleScheduleFlush(/* checkExisting= */ true);
286 return;
287 }
288
Felipe Lemee127c9a2019-01-04 14:56:38 -0800289 if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800290 // Callback from startSession hasn't been called yet - typically happens on system
291 // apps that are started before the system service
292 // TODO(b/111276913): try to ignore session while system is not ready / boot
293 // not complete instead. Similarly, the manager service should return right away
294 // when the user does not have a service set
Felipe Lemee127c9a2019-01-04 14:56:38 -0800295 if (DEBUG) {
296 Log.d(TAG, "Closing session for " + getActivityDebugName()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800297 + " after " + numberEvents + " delayed events and state "
298 + getStateAsString(mState));
299 }
300 handleResetState();
301 // TODO(b/111276913): blacklist activity / use special flag to indicate that
302 // when it's launched again
303 return;
304 }
305
306 handleForceFlush();
307 }
308
309 private void handleScheduleFlush(boolean checkExisting) {
310 if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) {
311 // "Renew" the flush message by removing the previous one
312 mHandler.removeMessages(MSG_FLUSH);
313 }
314 mNextFlush = SystemClock.elapsedRealtime() + FLUSHING_FREQUENCY_MS;
315 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800316 Log.v(TAG, "Scheduled to flush in " + FLUSHING_FREQUENCY_MS + "ms: " + mNextFlush);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800317 }
318 mHandler.sendMessageDelayed(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800319 obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800320 .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS);
321 }
322
323 private void handleFlushIfNeeded() {
324 if (mEvents.isEmpty()) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800325 if (VERBOSE) Log.v(TAG, "Nothing to flush");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800326 return;
327 }
328 handleForceFlush();
329 }
330
331 private void handleForceFlush() {
332 if (mEvents == null) return;
333
334 if (mDirectServiceInterface == null) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800335 if (VERBOSE) {
336 Log.v(TAG, "handleForceFlush(): hold your horses, client not ready: " + mEvents);
337 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800338 if (!mHandler.hasMessages(MSG_FLUSH)) {
339 handleScheduleFlush(/* checkExisting= */ false);
340 }
341 return;
342 }
343
344 final int numberEvents = mEvents.size();
345 try {
346 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800347 Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getActivityDebugName());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800348 }
349 mHandler.removeMessages(MSG_FLUSH);
350
351 final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents();
Felipe Lemefc24bea2018-12-18 13:19:01 -0800352 mDirectServiceInterface.sendEvents(events);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800353 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800354 Log.w(TAG, "Error sending " + numberEvents + " for " + getActivityDebugName()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800355 + ": " + e);
356 }
357 }
358
359 /**
360 * Resets the buffer and return a {@link ParceledListSlice} with the previous events.
361 */
362 @NonNull
363 private ParceledListSlice<ContentCaptureEvent> handleClearEvents() {
364 // NOTE: we must save a reference to the current mEvents and then set it to to null,
365 // otherwise clearing it would clear it in the receiving side if the service is also local.
366 final List<ContentCaptureEvent> events = mEvents == null
367 ? Collections.emptyList()
368 : mEvents;
369 mEvents = null;
370 return new ParceledListSlice<>(events);
371 }
372
373 private void handleDestroySession() {
374 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800375 Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800376 + (mEvents == null ? 0 : mEvents.size()) + " event(s) for "
377 + getActivityDebugName());
378 }
379
380 try {
381 mSystemServerInterface.finishSession(mContext.getUserId(), mId);
382 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800383 Log.e(TAG, "Error destroying system-service session " + mId + " for "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800384 + getActivityDebugName() + ": " + e);
385 }
386 }
387
388 private void handleResetState() {
389 handleResetSession(/* resetState= */ true);
390 }
391
Felipe Leme87a9dc92018-12-18 14:28:07 -0800392 // TODO(b/121033016): once we support multiple sessions, we might need to move some of these
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800393 // clearings out.
394 private void handleResetSession(boolean resetState) {
395 if (resetState) {
396 mState = STATE_UNKNOWN;
397 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800398
399 // TODO(b/121033016): must reset children (which currently is owned by superclass)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800400 mApplicationToken = null;
401 mComponentName = null;
402 mEvents = null;
403 if (mDirectServiceInterface != null) {
404 mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0);
405 }
406 mDirectServiceInterface = null;
407 mHandler.removeMessages(MSG_FLUSH);
408 }
409
410 @Override
411 void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800412 notifyViewAppeared(mId, node);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800413 }
414
415 @Override
416 void internalNotifyViewDisappeared(@NonNull AutofillId id) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800417 notifyViewDisappeared(mId, id);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800418 }
419
420 @Override
421 void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text,
422 int flags) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800423 notifyViewTextChanged(mId, id, text, flags);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800424 }
425
426 @Override
427 boolean isContentCaptureEnabled() {
Felipe Lemebef744c2019-01-03 11:07:25 -0800428 return super.isContentCaptureEnabled() && mSystemServerInterface != null
429 && !mDisabled.get();
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800430 }
431
Felipe Leme87a9dc92018-12-18 14:28:07 -0800432 // TODO(b/121033016): refactor "notifyXXXX" methods below to a common "Buffer" object that is
433 // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such
434 // change should also get get rid of the "internalNotifyXXXX" methods above
435 void notifyChildSessionStarted(@NonNull String parentSessionId,
436 @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) {
437 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
438 new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED)
439 .setParentSessionId(parentSessionId)
440 .setClientContext(clientContext),
Felipe Lemee127c9a2019-01-04 14:56:38 -0800441 /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800442 }
443
444 void notifyChildSessionFinished(@NonNull String parentSessionId,
445 @NonNull String childSessionId) {
446 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
447 new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED)
Felipe Lemee127c9a2019-01-04 14:56:38 -0800448 .setParentSessionId(parentSessionId), /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800449 }
450
451 void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) {
452 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
453 new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED)
454 .setViewNode(node.mNode), /* forceFlush= */ false));
455 }
456
457 void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) {
458 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
459 new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id),
460 /* forceFlush= */ false));
461 }
462
463 void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id,
464 @Nullable CharSequence text, int flags) {
465 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
466 new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id)
467 .setText(text), /* forceFlush= */ false));
468 }
469
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800470 @Override
471 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
472 pw.print(prefix); pw.print("id: "); pw.println(mId);
473 pw.print(prefix); pw.print("mContext: "); pw.println(mContext);
474 pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId());
475 if (mSystemServerInterface != null) {
476 pw.print(prefix); pw.print("mSystemServerInterface: ");
477 pw.println(mSystemServerInterface);
478 }
479 if (mDirectServiceInterface != null) {
480 pw.print(prefix); pw.print("mDirectServiceInterface: ");
481 pw.println(mDirectServiceInterface);
482 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800483 pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get());
484 pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800485 pw.print(prefix); pw.print("state: "); pw.print(mState); pw.print(" (");
486 pw.print(getStateAsString(mState)); pw.println(")");
487 if (mApplicationToken != null) {
488 pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken);
489 }
490 if (mComponentName != null) {
491 pw.print(prefix); pw.print("component name: ");
492 pw.println(mComponentName.flattenToShortString());
493 }
494 if (mEvents != null && !mEvents.isEmpty()) {
495 final int numberEvents = mEvents.size();
496 pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents);
497 pw.print('/'); pw.println(MAX_BUFFER_SIZE);
498 if (VERBOSE && numberEvents > 0) {
499 final String prefix3 = prefix + " ";
500 for (int i = 0; i < numberEvents; i++) {
501 final ContentCaptureEvent event = mEvents.get(i);
502 pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw);
503 pw.println();
504 }
505 }
506 pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS);
507 pw.print(prefix); pw.print("next flush: ");
508 TimeUtils.formatDuration(mNextFlush - SystemClock.elapsedRealtime(), pw); pw.println();
509 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800510 super.dump(prefix, pw);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800511 }
512
513 /**
514 * Gets a string that can be used to identify the activity on logging statements.
515 */
516 private String getActivityDebugName() {
517 return mComponentName == null ? mContext.getPackageName()
518 : mComponentName.flattenToShortString();
519 }
520}