blob: a3ff8c09aa0c1d4045ac5792a3b484afc957a53d [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
Felipe Leme01b87492019-01-15 13:26:52 -080091 // TODO(b/111276913): make sure disabled state is in sync with manager's disabled
Felipe Lemeb63e0dd2018-12-18 11:56:42 -080092 @NonNull
93 private final AtomicBoolean mDisabled;
94
95 @NonNull
96 private final Context mContext;
97
98 @NonNull
99 private final Handler mHandler;
100
101 /**
102 * Interface to the system_server binder object - it's only used to start the session (and
103 * notify when the session is finished).
104 */
105 @Nullable
106 private final IContentCaptureManager mSystemServerInterface;
107
108 /**
109 * Direct interface to the service binder object - it's used to send the events, including the
110 * last ones (when the session is finished)
111 */
112 @Nullable
113 private IContentCaptureDirectManager mDirectServiceInterface;
114 @Nullable
115 private DeathRecipient mDirectServiceVulture;
116
Felipe Leme01b87492019-01-15 13:26:52 -0800117 private int mState = UNKNWON_STATE;
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800118
119 @Nullable
120 private IBinder mApplicationToken;
121
122 @Nullable
123 private ComponentName mComponentName;
124
125 /**
126 * List of events held to be sent as a batch.
127 */
128 @Nullable
129 private ArrayList<ContentCaptureEvent> mEvents;
130
131 // Used just for debugging purposes (on dump)
132 private long mNextFlush;
133
Felipe Leme87a9dc92018-12-18 14:28:07 -0800134 /** @hide */
135 protected MainContentCaptureSession(@NonNull Context context, @NonNull Handler handler,
136 @Nullable IContentCaptureManager systemServerInterface,
Felipe Leme01b87492019-01-15 13:26:52 -0800137 @NonNull boolean disabled) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800138 mContext = context;
139 mHandler = handler;
140 mSystemServerInterface = systemServerInterface;
Felipe Leme01b87492019-01-15 13:26:52 -0800141 mDisabled = new AtomicBoolean(disabled);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800142 }
143
Felipe Leme87a9dc92018-12-18 14:28:07 -0800144 @Override
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800145 MainContentCaptureSession getMainCaptureSession() {
146 return this;
147 }
148
149 @Override
Felipe Leme87a9dc92018-12-18 14:28:07 -0800150 ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) {
151 final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext);
152 notifyChildSessionStarted(mId, child.mId, clientContext);
153 return child;
154 }
155
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800156 /**
157 * Starts this session.
158 *
159 * @hide
160 */
Adam He328c0e32019-01-03 15:19:22 -0800161 void start(@NonNull IBinder applicationToken, @NonNull ComponentName activityComponent,
162 int flags) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800163 if (!isContentCaptureEnabled()) return;
164
165 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800166 Log.v(TAG, "start(): token=" + applicationToken + ", comp="
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800167 + ComponentName.flattenToShortString(activityComponent));
168 }
169
Felipe Leme87a9dc92018-12-18 14:28:07 -0800170 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleStartSession, this,
Adam He328c0e32019-01-03 15:19:22 -0800171 applicationToken, activityComponent, flags));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800172 }
173
174 @Override
175 void flush() {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800176 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleForceFlush, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800177 }
178
179 @Override
180 void onDestroy() {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800181 mHandler.removeMessages(MSG_FLUSH);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800182 mHandler.sendMessage(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800183 obtainMessage(MainContentCaptureSession::handleDestroySession, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800184 }
185
Adam He328c0e32019-01-03 15:19:22 -0800186 private void handleStartSession(@NonNull IBinder token, @NonNull ComponentName componentName,
187 int flags) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800188 if (handleHasStarted()) {
189 // TODO(b/122959591): make sure this is expected (and when), or use Log.w
190 if (DEBUG) {
191 Log.d(TAG, "ignoring handleStartSession(" + token + "/"
192 + ComponentName.flattenToShortString(componentName) + " while on state "
193 + getStateAsString(mState));
194 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800195 return;
196 }
197 mState = STATE_WAITING_FOR_SERVER;
198 mApplicationToken = token;
199 mComponentName = componentName;
200
201 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800202 Log.v(TAG, "handleStartSession(): token=" + token + ", act="
Felipe Lemed65692c2019-01-16 12:10:50 -0800203 + getDebugState() + ", id=" + mId);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800204 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800205
206 try {
Adam He6884fed2019-01-07 13:18:32 -0800207 if (mSystemServerInterface == null) return;
208
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800209 mSystemServerInterface.startSession(mContext.getUserId(), mApplicationToken,
Felipe Leme87a9dc92018-12-18 14:28:07 -0800210 componentName, mId, flags, new IResultReceiver.Stub() {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800211 @Override
212 public void send(int resultCode, Bundle resultData) {
213 IBinder binder = null;
214 if (resultData != null) {
215 binder = resultData.getBinder(EXTRA_BINDER);
216 if (binder == null) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800217 Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
Felipe Lemed65692c2019-01-16 12:10:50 -0800218 handleResetSession(STATE_DISABLED | STATE_INTERNAL_ERROR);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800219 return;
220 }
221 }
222 handleSessionStarted(resultCode, binder);
223 }
224 });
225 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800226 Log.w(TAG, "Error starting session for " + componentName.flattenToShortString() + ": "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800227 + e);
228 }
229 }
230
231 /**
232 * Callback from {@code system_server} after call to
233 * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String,
Felipe Lemebef744c2019-01-03 11:07:25 -0800234 * int, IResultReceiver)}.
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800235 *
236 * @param resultCode session state
237 * @param binder handle to {@code IContentCaptureDirectManager}
238 */
239 private void handleSessionStarted(int resultCode, @Nullable IBinder binder) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800240 if (binder != null) {
241 mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder);
242 mDirectServiceVulture = () -> {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800243 Log.w(TAG, "Destroying session " + mId + " because service died");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800244 destroy();
245 };
246 try {
247 binder.linkToDeath(mDirectServiceVulture, 0);
248 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800249 Log.w(TAG, "Failed to link to death on " + binder + ": " + e);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800250 }
251 }
Adam He328c0e32019-01-03 15:19:22 -0800252
Felipe Lemed65692c2019-01-16 12:10:50 -0800253 if ((resultCode & STATE_DISABLED) != 0) {
254 handleResetSession(resultCode);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800255 } else {
Felipe Lemed65692c2019-01-16 12:10:50 -0800256 mState = resultCode;
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800257 mDisabled.set(false);
258 }
259 if (VERBOSE) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800260 Log.v(TAG, "handleSessionStarted() result: id=" + mId + " resultCode=" + resultCode
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) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800267 if (!handleHasStarted()
268 && event.getType() != ContentCaptureEvent.TYPE_SESSION_STARTED) {
269 // TODO(b/120494182): comment when this could happen (dialogs?)
270 Log.v(TAG, "handleSendEvent(" + getDebugState() + ", "
271 + ContentCaptureEvent.getTypeAsString(event.getType())
272 + "): session not started yet");
273 return;
274 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800275 if (mEvents == null) {
276 if (VERBOSE) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800277 Log.v(TAG, "handleSendEvent(" + getDebugState() + ", "
278 + ContentCaptureEvent.getTypeAsString(event.getType())
279 + "): cCreating buffer for " + MAX_BUFFER_SIZE + " events");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800280 }
281 mEvents = new ArrayList<>(MAX_BUFFER_SIZE);
282 }
Adam Heac132652019-01-02 14:40:15 -0800283
284 if (!mEvents.isEmpty() && event.getType() == TYPE_VIEW_TEXT_CHANGED) {
285 final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1);
286
287 // TODO(b/121045053): check if flags match
288 if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED
289 && lastEvent.getId().equals(event.getId())) {
290 if (VERBOSE) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800291 Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text = "
Adam Heac132652019-01-02 14:40:15 -0800292 + event.getText());
293 }
294 lastEvent.setText(event.getText());
295 } else {
296 mEvents.add(event);
297 }
298 } else {
299 mEvents.add(event);
300 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800301
302 final int numberEvents = mEvents.size();
303
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800304 final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE;
305
306 if (bufferEvent && !forceFlush) {
307 handleScheduleFlush(/* checkExisting= */ true);
308 return;
309 }
310
Felipe Lemee127c9a2019-01-04 14:56:38 -0800311 if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800312 // Callback from startSession hasn't been called yet - typically happens on system
313 // apps that are started before the system service
Felipe Lemed65692c2019-01-16 12:10:50 -0800314 // TODO(b/122959591): try to ignore session while system is not ready / boot
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800315 // not complete instead. Similarly, the manager service should return right away
316 // when the user does not have a service set
Felipe Lemee127c9a2019-01-04 14:56:38 -0800317 if (DEBUG) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800318 Log.d(TAG, "Closing session for " + getDebugState()
319 + " after " + numberEvents + " delayed events");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800320 }
Felipe Lemed65692c2019-01-16 12:10:50 -0800321 handleResetSession(STATE_DISABLED | STATE_NO_RESPONSE);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800322 // TODO(b/111276913): blacklist activity / use special flag to indicate that
323 // when it's launched again
324 return;
325 }
326
327 handleForceFlush();
328 }
329
Felipe Lemed65692c2019-01-16 12:10:50 -0800330 private boolean handleHasStarted() {
331 return mState != UNKNWON_STATE;
332 }
333
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800334 private void handleScheduleFlush(boolean checkExisting) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800335 if (!handleHasStarted()) {
336 Log.v(TAG, "handleScheduleFlush(" + getDebugState() + "): session not started yet");
337 return;
338 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800339 if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) {
340 // "Renew" the flush message by removing the previous one
341 mHandler.removeMessages(MSG_FLUSH);
342 }
343 mNextFlush = SystemClock.elapsedRealtime() + FLUSHING_FREQUENCY_MS;
344 if (VERBOSE) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800345 Log.v(TAG, "handleScheduleFlush(" + getDebugState() + "): scheduled to flush in "
346 + FLUSHING_FREQUENCY_MS + "ms: " + TimeUtils.formatUptime(mNextFlush));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800347 }
348 mHandler.sendMessageDelayed(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800349 obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800350 .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS);
351 }
352
353 private void handleFlushIfNeeded() {
354 if (mEvents.isEmpty()) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800355 if (VERBOSE) Log.v(TAG, "Nothing to flush");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800356 return;
357 }
358 handleForceFlush();
359 }
360
361 private void handleForceFlush() {
362 if (mEvents == null) return;
363
364 if (mDirectServiceInterface == null) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800365 if (VERBOSE) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800366 Log.v(TAG, "handleForceFlush(" + getDebugState()
367 + "): hold your horses, client not ready: " + mEvents);
Felipe Lemee127c9a2019-01-04 14:56:38 -0800368 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800369 if (!mHandler.hasMessages(MSG_FLUSH)) {
370 handleScheduleFlush(/* checkExisting= */ false);
371 }
372 return;
373 }
374
375 final int numberEvents = mEvents.size();
376 try {
377 if (DEBUG) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800378 Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getDebugState());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800379 }
380 mHandler.removeMessages(MSG_FLUSH);
381
382 final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents();
Felipe Lemefc24bea2018-12-18 13:19:01 -0800383 mDirectServiceInterface.sendEvents(events);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800384 } catch (RemoteException e) {
Felipe Lemed65692c2019-01-16 12:10:50 -0800385 Log.w(TAG, "Error sending " + numberEvents + " for " + getDebugState()
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800386 + ": " + e);
387 }
388 }
389
390 /**
391 * Resets the buffer and return a {@link ParceledListSlice} with the previous events.
392 */
393 @NonNull
394 private ParceledListSlice<ContentCaptureEvent> handleClearEvents() {
395 // NOTE: we must save a reference to the current mEvents and then set it to to null,
396 // otherwise clearing it would clear it in the receiving side if the service is also local.
397 final List<ContentCaptureEvent> events = mEvents == null
398 ? Collections.emptyList()
399 : mEvents;
400 mEvents = null;
401 return new ParceledListSlice<>(events);
402 }
403
404 private void handleDestroySession() {
405 if (DEBUG) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800406 Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with "
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800407 + (mEvents == null ? 0 : mEvents.size()) + " event(s) for "
Felipe Lemed65692c2019-01-16 12:10:50 -0800408 + getDebugState());
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800409 }
410
411 try {
Adam He6884fed2019-01-07 13:18:32 -0800412 if (mSystemServerInterface == null) return;
413
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800414 mSystemServerInterface.finishSession(mContext.getUserId(), mId);
415 } catch (RemoteException e) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800416 Log.e(TAG, "Error destroying system-service session " + mId + " for "
Felipe Lemed65692c2019-01-16 12:10:50 -0800417 + getDebugState() + ": " + e);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800418 }
419 }
420
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800421 // TODO(b/122454205): once we support multiple sessions, we might need to move some of these
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800422 // clearings out.
Felipe Lemed65692c2019-01-16 12:10:50 -0800423 private void handleResetSession(int newState) {
424 if (VERBOSE) {
425 Log.v(TAG, "handleResetSession(" + getActivityName() + "): from "
426 + getStateAsString(mState) + " to " + getStateAsString(newState));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800427 }
Felipe Lemed65692c2019-01-16 12:10:50 -0800428 mState = newState;
429 mDisabled.set((newState & STATE_DISABLED) != 0);
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800430 // TODO(b/122454205): must reset children (which currently is owned by superclass)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800431 mApplicationToken = null;
432 mComponentName = null;
433 mEvents = null;
434 if (mDirectServiceInterface != null) {
435 mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0);
436 }
437 mDirectServiceInterface = null;
438 mHandler.removeMessages(MSG_FLUSH);
439 }
440
441 @Override
442 void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800443 notifyViewAppeared(mId, node);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800444 }
445
446 @Override
447 void internalNotifyViewDisappeared(@NonNull AutofillId id) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800448 notifyViewDisappeared(mId, id);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800449 }
450
451 @Override
452 void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text,
453 int flags) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800454 notifyViewTextChanged(mId, id, text, flags);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800455 }
456
457 @Override
458 boolean isContentCaptureEnabled() {
Felipe Lemebef744c2019-01-03 11:07:25 -0800459 return super.isContentCaptureEnabled() && mSystemServerInterface != null
460 && !mDisabled.get();
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800461 }
462
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800463 // TODO(b/122454205): refactor "notifyXXXX" methods below to a common "Buffer" object that is
Felipe Leme87a9dc92018-12-18 14:28:07 -0800464 // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such
465 // change should also get get rid of the "internalNotifyXXXX" methods above
466 void notifyChildSessionStarted(@NonNull String parentSessionId,
467 @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) {
468 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
469 new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED)
470 .setParentSessionId(parentSessionId)
471 .setClientContext(clientContext),
Felipe Lemee127c9a2019-01-04 14:56:38 -0800472 /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800473 }
474
475 void notifyChildSessionFinished(@NonNull String parentSessionId,
476 @NonNull String childSessionId) {
477 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
478 new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED)
Felipe Lemee127c9a2019-01-04 14:56:38 -0800479 .setParentSessionId(parentSessionId), /* forceFlush= */ true));
Felipe Leme87a9dc92018-12-18 14:28:07 -0800480 }
481
482 void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) {
483 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
484 new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED)
485 .setViewNode(node.mNode), /* forceFlush= */ false));
486 }
487
488 void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) {
489 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
490 new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id),
491 /* forceFlush= */ false));
492 }
493
494 void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id,
495 @Nullable CharSequence text, int flags) {
496 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
497 new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id)
498 .setText(text), /* forceFlush= */ false));
499 }
500
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800501 @Override
502 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
503 pw.print(prefix); pw.print("id: "); pw.println(mId);
504 pw.print(prefix); pw.print("mContext: "); pw.println(mContext);
505 pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId());
506 if (mSystemServerInterface != null) {
507 pw.print(prefix); pw.print("mSystemServerInterface: ");
508 pw.println(mSystemServerInterface);
509 }
510 if (mDirectServiceInterface != null) {
511 pw.print(prefix); pw.print("mDirectServiceInterface: ");
512 pw.println(mDirectServiceInterface);
513 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800514 pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get());
515 pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled());
Felipe Leme01b87492019-01-15 13:26:52 -0800516 pw.print(prefix); pw.print("state: "); pw.println(getStateAsString(mState));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800517 if (mApplicationToken != null) {
518 pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken);
519 }
520 if (mComponentName != null) {
521 pw.print(prefix); pw.print("component name: ");
522 pw.println(mComponentName.flattenToShortString());
523 }
524 if (mEvents != null && !mEvents.isEmpty()) {
525 final int numberEvents = mEvents.size();
526 pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents);
527 pw.print('/'); pw.println(MAX_BUFFER_SIZE);
528 if (VERBOSE && numberEvents > 0) {
529 final String prefix3 = prefix + " ";
530 for (int i = 0; i < numberEvents; i++) {
531 final ContentCaptureEvent event = mEvents.get(i);
532 pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw);
533 pw.println();
534 }
535 }
536 pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS);
537 pw.print(prefix); pw.print("next flush: ");
538 TimeUtils.formatDuration(mNextFlush - SystemClock.elapsedRealtime(), pw); pw.println();
539 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800540 super.dump(prefix, pw);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800541 }
542
543 /**
544 * Gets a string that can be used to identify the activity on logging statements.
545 */
Felipe Lemed65692c2019-01-16 12:10:50 -0800546 private String getActivityName() {
547 return mComponentName == null
548 ? "pkg:" + mContext.getPackageName()
549 : "act:" + mComponentName.flattenToShortString();
550 }
551
552 private String getDebugState() {
553 return getActivityName() + " (state=" + getStateAsString(mState) + ")";
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800554 }
555}