blob: 18ae454cc5f5c87c726614cd8db41cff8d2590f9 [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 */
Adam He328c0e32019-01-03 15:19:22 -0800160 void start(@NonNull IBinder applicationToken, @NonNull ComponentName activityComponent,
161 int flags) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800162 if (!isContentCaptureEnabled()) return;
163
164 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800165 Log.v(TAG, "start(): token=" + applicationToken + ", comp="
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800166 + ComponentName.flattenToShortString(activityComponent));
167 }
168
Felipe Leme87a9dc92018-12-18 14:28:07 -0800169 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleStartSession, this,
Adam He328c0e32019-01-03 15:19:22 -0800170 applicationToken, activityComponent, flags));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800171 }
172
173 @Override
174 void flush() {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800175 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleForceFlush, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800176 }
177
178 @Override
179 void onDestroy() {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800180 mHandler.removeMessages(MSG_FLUSH);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800181 mHandler.sendMessage(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800182 obtainMessage(MainContentCaptureSession::handleDestroySession, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800183 }
184
Adam He328c0e32019-01-03 15:19:22 -0800185 private void handleStartSession(@NonNull IBinder token, @NonNull ComponentName componentName,
186 int flags) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800187 if (mState != STATE_UNKNOWN) {
188 // TODO(b/111276913): revisit this scenario
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800189 Log.w(TAG, "ignoring handleStartSession(" + token + ") while on state "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800190 + getStateAsString(mState));
191 return;
192 }
193 mState = STATE_WAITING_FOR_SERVER;
194 mApplicationToken = token;
195 mComponentName = componentName;
196
197 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800198 Log.v(TAG, "handleStartSession(): token=" + token + ", act="
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800199 + getActivityDebugName() + ", id=" + mId);
200 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800201
202 try {
Adam He6884fed2019-01-07 13:18:32 -0800203 if (mSystemServerInterface == null) return;
204
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800205 mSystemServerInterface.startSession(mContext.getUserId(), mApplicationToken,
Felipe Leme87a9dc92018-12-18 14:28:07 -0800206 componentName, mId, flags, new IResultReceiver.Stub() {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800207 @Override
208 public void send(int resultCode, Bundle resultData) {
209 IBinder binder = null;
210 if (resultData != null) {
211 binder = resultData.getBinder(EXTRA_BINDER);
212 if (binder == null) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800213 Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800214 handleResetState();
215 return;
216 }
217 }
218 handleSessionStarted(resultCode, binder);
219 }
220 });
221 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800222 Log.w(TAG, "Error starting session for " + componentName.flattenToShortString() + ": "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800223 + e);
224 }
225 }
226
227 /**
228 * Callback from {@code system_server} after call to
229 * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String,
Felipe Lemebef744c2019-01-03 11:07:25 -0800230 * int, IResultReceiver)}.
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800231 *
232 * @param resultCode session state
233 * @param binder handle to {@code IContentCaptureDirectManager}
234 */
235 private void handleSessionStarted(int resultCode, @Nullable IBinder binder) {
236 mState = resultCode;
237 if (binder != null) {
238 mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder);
239 mDirectServiceVulture = () -> {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800240 Log.w(TAG, "Destroying session " + mId + " because service died");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800241 destroy();
242 };
243 try {
244 binder.linkToDeath(mDirectServiceVulture, 0);
245 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800246 Log.w(TAG, "Failed to link to death on " + binder + ": " + e);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800247 }
248 }
Adam He328c0e32019-01-03 15:19:22 -0800249
250 // TODO(b/111276913): change the resultCode to use flags so there's just one flag for
251 // disabled stuff
252 if (resultCode == STATE_DISABLED_NO_SERVICE || resultCode == STATE_DISABLED_DUPLICATED_ID
Adam He6079d152019-01-10 11:37:17 -0800253 || resultCode == STATE_DISABLED_BY_FLAG_SECURE
254 || resultCode == STATE_DISABLED_BY_APP) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800255 mDisabled.set(true);
256 handleResetSession(/* resetState= */ false);
257 } else {
258 mDisabled.set(false);
259 }
260 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800261 Log.v(TAG, "handleSessionStarted() result: code=" + resultCode + ", id=" + mId
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800262 + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get()
Felipe Lemee127c9a2019-01-04 14:56:38 -0800263 + ", binder=" + binder + ", events=" + (mEvents == null ? 0 : mEvents.size()));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800264 }
265 }
266
267 private void handleSendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) {
268 if (mEvents == null) {
269 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800270 Log.v(TAG, "Creating buffer for " + MAX_BUFFER_SIZE + " events");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800271 }
272 mEvents = new ArrayList<>(MAX_BUFFER_SIZE);
273 }
Adam Heac132652019-01-02 14:40:15 -0800274
275 if (!mEvents.isEmpty() && event.getType() == TYPE_VIEW_TEXT_CHANGED) {
276 final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1);
277
278 // TODO(b/121045053): check if flags match
279 if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED
280 && lastEvent.getId().equals(event.getId())) {
281 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800282 Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text = "
Adam Heac132652019-01-02 14:40:15 -0800283 + event.getText());
284 }
285 lastEvent.setText(event.getText());
286 } else {
287 mEvents.add(event);
288 }
289 } else {
290 mEvents.add(event);
291 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800292
293 final int numberEvents = mEvents.size();
294
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800295 final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE;
296
297 if (bufferEvent && !forceFlush) {
298 handleScheduleFlush(/* checkExisting= */ true);
299 return;
300 }
301
Felipe Lemee127c9a2019-01-04 14:56:38 -0800302 if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800303 // Callback from startSession hasn't been called yet - typically happens on system
304 // apps that are started before the system service
305 // TODO(b/111276913): try to ignore session while system is not ready / boot
306 // not complete instead. Similarly, the manager service should return right away
307 // when the user does not have a service set
Felipe Lemee127c9a2019-01-04 14:56:38 -0800308 if (DEBUG) {
309 Log.d(TAG, "Closing session for " + getActivityDebugName()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800310 + " after " + numberEvents + " delayed events and state "
311 + getStateAsString(mState));
312 }
313 handleResetState();
314 // TODO(b/111276913): blacklist activity / use special flag to indicate that
315 // when it's launched again
316 return;
317 }
318
319 handleForceFlush();
320 }
321
322 private void handleScheduleFlush(boolean checkExisting) {
323 if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) {
324 // "Renew" the flush message by removing the previous one
325 mHandler.removeMessages(MSG_FLUSH);
326 }
327 mNextFlush = SystemClock.elapsedRealtime() + FLUSHING_FREQUENCY_MS;
328 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800329 Log.v(TAG, "Scheduled to flush in " + FLUSHING_FREQUENCY_MS + "ms: " + mNextFlush);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800330 }
331 mHandler.sendMessageDelayed(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800332 obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800333 .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS);
334 }
335
336 private void handleFlushIfNeeded() {
337 if (mEvents.isEmpty()) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800338 if (VERBOSE) Log.v(TAG, "Nothing to flush");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800339 return;
340 }
341 handleForceFlush();
342 }
343
344 private void handleForceFlush() {
345 if (mEvents == null) return;
346
347 if (mDirectServiceInterface == null) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800348 if (VERBOSE) {
349 Log.v(TAG, "handleForceFlush(): hold your horses, client not ready: " + mEvents);
350 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800351 if (!mHandler.hasMessages(MSG_FLUSH)) {
352 handleScheduleFlush(/* checkExisting= */ false);
353 }
354 return;
355 }
356
357 final int numberEvents = mEvents.size();
358 try {
359 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800360 Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getActivityDebugName());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800361 }
362 mHandler.removeMessages(MSG_FLUSH);
363
364 final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents();
Felipe Lemefc24bea2018-12-18 13:19:01 -0800365 mDirectServiceInterface.sendEvents(events);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800366 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800367 Log.w(TAG, "Error sending " + numberEvents + " for " + getActivityDebugName()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800368 + ": " + e);
369 }
370 }
371
372 /**
373 * Resets the buffer and return a {@link ParceledListSlice} with the previous events.
374 */
375 @NonNull
376 private ParceledListSlice<ContentCaptureEvent> handleClearEvents() {
377 // NOTE: we must save a reference to the current mEvents and then set it to to null,
378 // otherwise clearing it would clear it in the receiving side if the service is also local.
379 final List<ContentCaptureEvent> events = mEvents == null
380 ? Collections.emptyList()
381 : mEvents;
382 mEvents = null;
383 return new ParceledListSlice<>(events);
384 }
385
386 private void handleDestroySession() {
387 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800388 Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800389 + (mEvents == null ? 0 : mEvents.size()) + " event(s) for "
390 + getActivityDebugName());
391 }
392
393 try {
Adam He6884fed2019-01-07 13:18:32 -0800394 if (mSystemServerInterface == null) return;
395
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800396 mSystemServerInterface.finishSession(mContext.getUserId(), mId);
397 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800398 Log.e(TAG, "Error destroying system-service session " + mId + " for "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800399 + getActivityDebugName() + ": " + e);
400 }
401 }
402
403 private void handleResetState() {
404 handleResetSession(/* resetState= */ true);
405 }
406
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800407 // TODO(b/122454205): once we support multiple sessions, we might need to move some of these
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800408 // clearings out.
409 private void handleResetSession(boolean resetState) {
410 if (resetState) {
411 mState = STATE_UNKNOWN;
412 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800413
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800414 // TODO(b/122454205): must reset children (which currently is owned by superclass)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800415 mApplicationToken = null;
416 mComponentName = null;
417 mEvents = null;
418 if (mDirectServiceInterface != null) {
419 mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0);
420 }
421 mDirectServiceInterface = null;
422 mHandler.removeMessages(MSG_FLUSH);
423 }
424
425 @Override
426 void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800427 notifyViewAppeared(mId, node);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800428 }
429
430 @Override
431 void internalNotifyViewDisappeared(@NonNull AutofillId id) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800432 notifyViewDisappeared(mId, id);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800433 }
434
435 @Override
436 void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text,
437 int flags) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800438 notifyViewTextChanged(mId, id, text, flags);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800439 }
440
441 @Override
442 boolean isContentCaptureEnabled() {
Felipe Lemebef744c2019-01-03 11:07:25 -0800443 return super.isContentCaptureEnabled() && mSystemServerInterface != null
444 && !mDisabled.get();
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800445 }
446
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800447 // TODO(b/122454205): refactor "notifyXXXX" methods below to a common "Buffer" object that is
Felipe Leme87a9dc92018-12-18 14:28:07 -0800448 // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such
449 // change should also get get rid of the "internalNotifyXXXX" methods above
450 void notifyChildSessionStarted(@NonNull String parentSessionId,
451 @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) {
452 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
453 new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED)
454 .setParentSessionId(parentSessionId)
455 .setClientContext(clientContext),
Felipe Lemee127c9a2019-01-04 14:56:38 -0800456 /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800457 }
458
459 void notifyChildSessionFinished(@NonNull String parentSessionId,
460 @NonNull String childSessionId) {
461 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
462 new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED)
Felipe Lemee127c9a2019-01-04 14:56:38 -0800463 .setParentSessionId(parentSessionId), /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800464 }
465
466 void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) {
467 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
468 new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED)
469 .setViewNode(node.mNode), /* forceFlush= */ false));
470 }
471
472 void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) {
473 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
474 new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id),
475 /* forceFlush= */ false));
476 }
477
478 void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id,
479 @Nullable CharSequence text, int flags) {
480 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
481 new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id)
482 .setText(text), /* forceFlush= */ false));
483 }
484
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800485 @Override
486 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
487 pw.print(prefix); pw.print("id: "); pw.println(mId);
488 pw.print(prefix); pw.print("mContext: "); pw.println(mContext);
489 pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId());
490 if (mSystemServerInterface != null) {
491 pw.print(prefix); pw.print("mSystemServerInterface: ");
492 pw.println(mSystemServerInterface);
493 }
494 if (mDirectServiceInterface != null) {
495 pw.print(prefix); pw.print("mDirectServiceInterface: ");
496 pw.println(mDirectServiceInterface);
497 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800498 pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get());
499 pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800500 pw.print(prefix); pw.print("state: "); pw.print(mState); pw.print(" (");
501 pw.print(getStateAsString(mState)); pw.println(")");
502 if (mApplicationToken != null) {
503 pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken);
504 }
505 if (mComponentName != null) {
506 pw.print(prefix); pw.print("component name: ");
507 pw.println(mComponentName.flattenToShortString());
508 }
509 if (mEvents != null && !mEvents.isEmpty()) {
510 final int numberEvents = mEvents.size();
511 pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents);
512 pw.print('/'); pw.println(MAX_BUFFER_SIZE);
513 if (VERBOSE && numberEvents > 0) {
514 final String prefix3 = prefix + " ";
515 for (int i = 0; i < numberEvents; i++) {
516 final ContentCaptureEvent event = mEvents.get(i);
517 pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw);
518 pw.println();
519 }
520 }
521 pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS);
522 pw.print(prefix); pw.print("next flush: ");
523 TimeUtils.formatDuration(mNextFlush - SystemClock.elapsedRealtime(), pw); pw.println();
524 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800525 super.dump(prefix, pw);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800526 }
527
528 /**
529 * Gets a string that can be used to identify the activity on logging statements.
530 */
531 private String getActivityDebugName() {
532 return mComponentName == null ? mContext.getPackageName()
533 : mComponentName.flattenToShortString();
534 }
535}