blob: a29aaf013d4915b1fd2d0181a7be4610f1d22f7b [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
253 || resultCode == STATE_DISABLED_BY_FLAG_SECURE) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800254 mDisabled.set(true);
255 handleResetSession(/* resetState= */ false);
256 } else {
257 mDisabled.set(false);
258 }
259 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800260 Log.v(TAG, "handleSessionStarted() result: code=" + resultCode + ", id=" + mId
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800261 + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get()
Felipe Lemee127c9a2019-01-04 14:56:38 -0800262 + ", binder=" + binder + ", events=" + (mEvents == null ? 0 : mEvents.size()));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800263 }
264 }
265
266 private void handleSendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) {
267 if (mEvents == null) {
268 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800269 Log.v(TAG, "Creating buffer for " + MAX_BUFFER_SIZE + " events");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800270 }
271 mEvents = new ArrayList<>(MAX_BUFFER_SIZE);
272 }
Adam Heac132652019-01-02 14:40:15 -0800273
274 if (!mEvents.isEmpty() && event.getType() == TYPE_VIEW_TEXT_CHANGED) {
275 final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1);
276
277 // TODO(b/121045053): check if flags match
278 if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED
279 && lastEvent.getId().equals(event.getId())) {
280 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800281 Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text = "
Adam Heac132652019-01-02 14:40:15 -0800282 + event.getText());
283 }
284 lastEvent.setText(event.getText());
285 } else {
286 mEvents.add(event);
287 }
288 } else {
289 mEvents.add(event);
290 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800291
292 final int numberEvents = mEvents.size();
293
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800294 final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE;
295
296 if (bufferEvent && !forceFlush) {
297 handleScheduleFlush(/* checkExisting= */ true);
298 return;
299 }
300
Felipe Lemee127c9a2019-01-04 14:56:38 -0800301 if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800302 // Callback from startSession hasn't been called yet - typically happens on system
303 // apps that are started before the system service
304 // TODO(b/111276913): try to ignore session while system is not ready / boot
305 // not complete instead. Similarly, the manager service should return right away
306 // when the user does not have a service set
Felipe Lemee127c9a2019-01-04 14:56:38 -0800307 if (DEBUG) {
308 Log.d(TAG, "Closing session for " + getActivityDebugName()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800309 + " after " + numberEvents + " delayed events and state "
310 + getStateAsString(mState));
311 }
312 handleResetState();
313 // TODO(b/111276913): blacklist activity / use special flag to indicate that
314 // when it's launched again
315 return;
316 }
317
318 handleForceFlush();
319 }
320
321 private void handleScheduleFlush(boolean checkExisting) {
322 if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) {
323 // "Renew" the flush message by removing the previous one
324 mHandler.removeMessages(MSG_FLUSH);
325 }
326 mNextFlush = SystemClock.elapsedRealtime() + FLUSHING_FREQUENCY_MS;
327 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800328 Log.v(TAG, "Scheduled to flush in " + FLUSHING_FREQUENCY_MS + "ms: " + mNextFlush);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800329 }
330 mHandler.sendMessageDelayed(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800331 obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800332 .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS);
333 }
334
335 private void handleFlushIfNeeded() {
336 if (mEvents.isEmpty()) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800337 if (VERBOSE) Log.v(TAG, "Nothing to flush");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800338 return;
339 }
340 handleForceFlush();
341 }
342
343 private void handleForceFlush() {
344 if (mEvents == null) return;
345
346 if (mDirectServiceInterface == null) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800347 if (VERBOSE) {
348 Log.v(TAG, "handleForceFlush(): hold your horses, client not ready: " + mEvents);
349 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800350 if (!mHandler.hasMessages(MSG_FLUSH)) {
351 handleScheduleFlush(/* checkExisting= */ false);
352 }
353 return;
354 }
355
356 final int numberEvents = mEvents.size();
357 try {
358 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800359 Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getActivityDebugName());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800360 }
361 mHandler.removeMessages(MSG_FLUSH);
362
363 final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents();
Felipe Lemefc24bea2018-12-18 13:19:01 -0800364 mDirectServiceInterface.sendEvents(events);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800365 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800366 Log.w(TAG, "Error sending " + numberEvents + " for " + getActivityDebugName()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800367 + ": " + e);
368 }
369 }
370
371 /**
372 * Resets the buffer and return a {@link ParceledListSlice} with the previous events.
373 */
374 @NonNull
375 private ParceledListSlice<ContentCaptureEvent> handleClearEvents() {
376 // NOTE: we must save a reference to the current mEvents and then set it to to null,
377 // otherwise clearing it would clear it in the receiving side if the service is also local.
378 final List<ContentCaptureEvent> events = mEvents == null
379 ? Collections.emptyList()
380 : mEvents;
381 mEvents = null;
382 return new ParceledListSlice<>(events);
383 }
384
385 private void handleDestroySession() {
386 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800387 Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800388 + (mEvents == null ? 0 : mEvents.size()) + " event(s) for "
389 + getActivityDebugName());
390 }
391
392 try {
Adam He6884fed2019-01-07 13:18:32 -0800393 if (mSystemServerInterface == null) return;
394
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800395 mSystemServerInterface.finishSession(mContext.getUserId(), mId);
396 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800397 Log.e(TAG, "Error destroying system-service session " + mId + " for "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800398 + getActivityDebugName() + ": " + e);
399 }
400 }
401
402 private void handleResetState() {
403 handleResetSession(/* resetState= */ true);
404 }
405
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800406 // TODO(b/122454205): once we support multiple sessions, we might need to move some of these
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800407 // clearings out.
408 private void handleResetSession(boolean resetState) {
409 if (resetState) {
410 mState = STATE_UNKNOWN;
411 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800412
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800413 // TODO(b/122454205): must reset children (which currently is owned by superclass)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800414 mApplicationToken = null;
415 mComponentName = null;
416 mEvents = null;
417 if (mDirectServiceInterface != null) {
418 mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0);
419 }
420 mDirectServiceInterface = null;
421 mHandler.removeMessages(MSG_FLUSH);
422 }
423
424 @Override
425 void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800426 notifyViewAppeared(mId, node);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800427 }
428
429 @Override
430 void internalNotifyViewDisappeared(@NonNull AutofillId id) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800431 notifyViewDisappeared(mId, id);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800432 }
433
434 @Override
435 void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text,
436 int flags) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800437 notifyViewTextChanged(mId, id, text, flags);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800438 }
439
440 @Override
441 boolean isContentCaptureEnabled() {
Felipe Lemebef744c2019-01-03 11:07:25 -0800442 return super.isContentCaptureEnabled() && mSystemServerInterface != null
443 && !mDisabled.get();
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800444 }
445
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800446 // TODO(b/122454205): refactor "notifyXXXX" methods below to a common "Buffer" object that is
Felipe Leme87a9dc92018-12-18 14:28:07 -0800447 // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such
448 // change should also get get rid of the "internalNotifyXXXX" methods above
449 void notifyChildSessionStarted(@NonNull String parentSessionId,
450 @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) {
451 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
452 new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED)
453 .setParentSessionId(parentSessionId)
454 .setClientContext(clientContext),
Felipe Lemee127c9a2019-01-04 14:56:38 -0800455 /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800456 }
457
458 void notifyChildSessionFinished(@NonNull String parentSessionId,
459 @NonNull String childSessionId) {
460 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
461 new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED)
Felipe Lemee127c9a2019-01-04 14:56:38 -0800462 .setParentSessionId(parentSessionId), /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800463 }
464
465 void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) {
466 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
467 new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED)
468 .setViewNode(node.mNode), /* forceFlush= */ false));
469 }
470
471 void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) {
472 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
473 new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id),
474 /* forceFlush= */ false));
475 }
476
477 void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id,
478 @Nullable CharSequence text, int flags) {
479 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
480 new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id)
481 .setText(text), /* forceFlush= */ false));
482 }
483
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800484 @Override
485 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
486 pw.print(prefix); pw.print("id: "); pw.println(mId);
487 pw.print(prefix); pw.print("mContext: "); pw.println(mContext);
488 pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId());
489 if (mSystemServerInterface != null) {
490 pw.print(prefix); pw.print("mSystemServerInterface: ");
491 pw.println(mSystemServerInterface);
492 }
493 if (mDirectServiceInterface != null) {
494 pw.print(prefix); pw.print("mDirectServiceInterface: ");
495 pw.println(mDirectServiceInterface);
496 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800497 pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get());
498 pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800499 pw.print(prefix); pw.print("state: "); pw.print(mState); pw.print(" (");
500 pw.print(getStateAsString(mState)); pw.println(")");
501 if (mApplicationToken != null) {
502 pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken);
503 }
504 if (mComponentName != null) {
505 pw.print(prefix); pw.print("component name: ");
506 pw.println(mComponentName.flattenToShortString());
507 }
508 if (mEvents != null && !mEvents.isEmpty()) {
509 final int numberEvents = mEvents.size();
510 pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents);
511 pw.print('/'); pw.println(MAX_BUFFER_SIZE);
512 if (VERBOSE && numberEvents > 0) {
513 final String prefix3 = prefix + " ";
514 for (int i = 0; i < numberEvents; i++) {
515 final ContentCaptureEvent event = mEvents.get(i);
516 pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw);
517 pw.println();
518 }
519 }
520 pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS);
521 pw.print(prefix); pw.print("next flush: ");
522 TimeUtils.formatDuration(mNextFlush - SystemClock.elapsedRealtime(), pw); pw.println();
523 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800524 super.dump(prefix, pw);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800525 }
526
527 /**
528 * Gets a string that can be used to identify the activity on logging statements.
529 */
530 private String getActivityDebugName() {
531 return mComponentName == null ? mContext.getPackageName()
532 : mComponentName.flattenToShortString();
533 }
534}