blob: ea6f2fe16ef652411561b065809fa281ff9c427c [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
66 /**
67 * Handler message used to flush the buffer.
68 */
69 private static final int MSG_FLUSH = 1;
70
71 /**
72 * Maximum number of events that are buffered before sent to the app.
73 */
74 // TODO(b/121044064): use settings
75 private static final int MAX_BUFFER_SIZE = 100;
76
77 /**
78 * Frequency the buffer is flushed if stale.
79 */
80 // TODO(b/121044064): use settings
81 private static final int FLUSHING_FREQUENCY_MS = 5_000;
82
83 /**
84 * Name of the {@link IResultReceiver} extra used to pass the binder interface to the service.
85 * @hide
86 */
87 public static final String EXTRA_BINDER = "binder";
88
89 @NonNull
90 private final AtomicBoolean mDisabled;
91
92 @NonNull
93 private final Context mContext;
94
95 @NonNull
96 private final Handler mHandler;
97
98 /**
99 * Interface to the system_server binder object - it's only used to start the session (and
100 * notify when the session is finished).
101 */
102 @Nullable
103 private final IContentCaptureManager mSystemServerInterface;
104
105 /**
106 * Direct interface to the service binder object - it's used to send the events, including the
107 * last ones (when the session is finished)
108 */
109 @Nullable
110 private IContentCaptureDirectManager mDirectServiceInterface;
111 @Nullable
112 private DeathRecipient mDirectServiceVulture;
113
114 private int mState = STATE_UNKNOWN;
115
116 @Nullable
117 private IBinder mApplicationToken;
118
119 @Nullable
120 private ComponentName mComponentName;
121
122 /**
123 * List of events held to be sent as a batch.
124 */
125 @Nullable
126 private ArrayList<ContentCaptureEvent> mEvents;
127
128 // Used just for debugging purposes (on dump)
129 private long mNextFlush;
130
131 // Lazily created on demand.
132 private ContentCaptureSessionId mContentCaptureSessionId;
133
Felipe Leme87a9dc92018-12-18 14:28:07 -0800134 /** @hide */
135 protected MainContentCaptureSession(@NonNull Context context, @NonNull Handler handler,
136 @Nullable IContentCaptureManager systemServerInterface,
137 @NonNull AtomicBoolean disabled) {
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800138 mContext = context;
139 mHandler = handler;
140 mSystemServerInterface = systemServerInterface;
141 mDisabled = disabled;
142 }
143
Felipe Leme87a9dc92018-12-18 14:28:07 -0800144 @Override
145 ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) {
146 final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext);
147 notifyChildSessionStarted(mId, child.mId, clientContext);
148 return child;
149 }
150
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800151 /**
152 * Starts this session.
153 *
154 * @hide
155 */
156 void start(@NonNull IBinder applicationToken, @NonNull ComponentName activityComponent) {
157 if (!isContentCaptureEnabled()) return;
158
159 if (VERBOSE) {
160 Log.v(mTag, "start(): token=" + applicationToken + ", comp="
161 + ComponentName.flattenToShortString(activityComponent));
162 }
163
Felipe Leme87a9dc92018-12-18 14:28:07 -0800164 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleStartSession, this,
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800165 applicationToken, activityComponent));
166 }
167
168 @Override
169 void flush() {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800170 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleForceFlush, this));
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800171 }
172
173 @Override
174 void onDestroy() {
175 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
182 Log.w(mTag, "ignoring handleStartSession(" + token + ") while on state "
183 + getStateAsString(mState));
184 return;
185 }
186 mState = STATE_WAITING_FOR_SERVER;
187 mApplicationToken = token;
188 mComponentName = componentName;
189
190 if (VERBOSE) {
191 Log.v(mTag, "handleStartSession(): token=" + token + ", act="
192 + 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) {
205 Log.wtf(mTag, "No " + EXTRA_BINDER + " extra result");
206 handleResetState();
207 return;
208 }
209 }
210 handleSessionStarted(resultCode, binder);
211 }
212 });
213 } catch (RemoteException e) {
214 Log.w(mTag, "Error starting session for " + componentName.flattenToShortString() + ": "
215 + e);
216 }
217 }
218
219 /**
220 * Callback from {@code system_server} after call to
221 * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String,
222 * ContentCaptureContext, int, IResultReceiver)}.
223 *
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 = () -> {
232 Log.w(mTag, "Destroying session " + mId + " because service died");
233 destroy();
234 };
235 try {
236 binder.linkToDeath(mDirectServiceVulture, 0);
237 } catch (RemoteException e) {
238 Log.w(mTag, "Failed to link to death on " + binder + ": " + e);
239 }
240 }
241 if (resultCode == STATE_DISABLED || resultCode == STATE_DISABLED_DUPLICATED_ID) {
242 mDisabled.set(true);
243 handleResetSession(/* resetState= */ false);
244 } else {
245 mDisabled.set(false);
246 }
247 if (VERBOSE) {
248 Log.v(mTag, "handleSessionStarted() result: code=" + resultCode + ", id=" + mId
249 + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get()
250 + ", binder=" + binder);
251 }
252 }
253
254 private void handleSendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) {
255 if (mEvents == null) {
256 if (VERBOSE) {
257 Log.v(mTag, "Creating buffer for " + MAX_BUFFER_SIZE + " events");
258 }
259 mEvents = new ArrayList<>(MAX_BUFFER_SIZE);
260 }
261 mEvents.add(event);
262
263 final int numberEvents = mEvents.size();
264
265 // TODO(b/120784831): need to optimize it so we buffer changes until a number of X are
266 // buffered (either total or per autofillid). For
267 // example, if the user typed "a", "b", "c" and the threshold is 3, we should buffer
268 // "a" and "b" then send "abc".
269 final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE;
270
271 if (bufferEvent && !forceFlush) {
272 handleScheduleFlush(/* checkExisting= */ true);
273 return;
274 }
275
276 if (mState != STATE_ACTIVE) {
277 // Callback from startSession hasn't been called yet - typically happens on system
278 // apps that are started before the system service
279 // TODO(b/111276913): try to ignore session while system is not ready / boot
280 // not complete instead. Similarly, the manager service should return right away
281 // when the user does not have a service set
282 if (VERBOSE) {
283 Log.v(mTag, "Closing session for " + getActivityDebugName()
284 + " after " + numberEvents + " delayed events and state "
285 + getStateAsString(mState));
286 }
287 handleResetState();
288 // TODO(b/111276913): blacklist activity / use special flag to indicate that
289 // when it's launched again
290 return;
291 }
292
293 handleForceFlush();
294 }
295
296 private void handleScheduleFlush(boolean checkExisting) {
297 if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) {
298 // "Renew" the flush message by removing the previous one
299 mHandler.removeMessages(MSG_FLUSH);
300 }
301 mNextFlush = SystemClock.elapsedRealtime() + FLUSHING_FREQUENCY_MS;
302 if (VERBOSE) {
303 Log.v(mTag, "Scheduled to flush in " + FLUSHING_FREQUENCY_MS + "ms: " + mNextFlush);
304 }
305 mHandler.sendMessageDelayed(
Felipe Leme87a9dc92018-12-18 14:28:07 -0800306 obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this)
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800307 .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS);
308 }
309
310 private void handleFlushIfNeeded() {
311 if (mEvents.isEmpty()) {
312 if (VERBOSE) Log.v(mTag, "Nothing to flush");
313 return;
314 }
315 handleForceFlush();
316 }
317
318 private void handleForceFlush() {
319 if (mEvents == null) return;
320
321 if (mDirectServiceInterface == null) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800322 if (DEBUG) Log.d(mTag, "handleForceFlush(): hold your horses, client not ready yet!");
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800323 if (!mHandler.hasMessages(MSG_FLUSH)) {
324 handleScheduleFlush(/* checkExisting= */ false);
325 }
326 return;
327 }
328
329 final int numberEvents = mEvents.size();
330 try {
331 if (DEBUG) {
332 Log.d(mTag, "Flushing " + numberEvents + " event(s) for " + getActivityDebugName());
333 }
334 mHandler.removeMessages(MSG_FLUSH);
335
336 final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents();
Felipe Lemefc24bea2018-12-18 13:19:01 -0800337 mDirectServiceInterface.sendEvents(events);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800338 } catch (RemoteException e) {
339 Log.w(mTag, "Error sending " + numberEvents + " for " + getActivityDebugName()
340 + ": " + e);
341 }
342 }
343
344 /**
345 * Resets the buffer and return a {@link ParceledListSlice} with the previous events.
346 */
347 @NonNull
348 private ParceledListSlice<ContentCaptureEvent> handleClearEvents() {
349 // NOTE: we must save a reference to the current mEvents and then set it to to null,
350 // otherwise clearing it would clear it in the receiving side if the service is also local.
351 final List<ContentCaptureEvent> events = mEvents == null
352 ? Collections.emptyList()
353 : mEvents;
354 mEvents = null;
355 return new ParceledListSlice<>(events);
356 }
357
358 private void handleDestroySession() {
359 if (DEBUG) {
360 Log.d(mTag, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with "
361 + (mEvents == null ? 0 : mEvents.size()) + " event(s) for "
362 + getActivityDebugName());
363 }
364
365 try {
366 mSystemServerInterface.finishSession(mContext.getUserId(), mId);
367 } catch (RemoteException e) {
368 Log.e(mTag, "Error destroying system-service session " + mId + " for "
369 + getActivityDebugName() + ": " + e);
370 }
371 }
372
373 private void handleResetState() {
374 handleResetSession(/* resetState= */ true);
375 }
376
Felipe Leme87a9dc92018-12-18 14:28:07 -0800377 // TODO(b/121033016): once we support multiple sessions, we might need to move some of these
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800378 // clearings out.
379 private void handleResetSession(boolean resetState) {
380 if (resetState) {
381 mState = STATE_UNKNOWN;
382 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800383
384 // TODO(b/121033016): must reset children (which currently is owned by superclass)
385
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800386 mContentCaptureSessionId = null;
387 mApplicationToken = null;
388 mComponentName = null;
389 mEvents = null;
390 if (mDirectServiceInterface != null) {
391 mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0);
392 }
393 mDirectServiceInterface = null;
394 mHandler.removeMessages(MSG_FLUSH);
395 }
396
397 @Override
398 void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800399 notifyViewAppeared(mId, node);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800400 }
401
402 @Override
403 void internalNotifyViewDisappeared(@NonNull AutofillId id) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800404 notifyViewDisappeared(mId, id);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800405 }
406
407 @Override
408 void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text,
409 int flags) {
Felipe Leme87a9dc92018-12-18 14:28:07 -0800410 notifyViewTextChanged(mId, id, text, flags);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800411 }
412
413 @Override
414 boolean isContentCaptureEnabled() {
415 return mSystemServerInterface != null && !mDisabled.get();
416 }
417
Felipe Leme87a9dc92018-12-18 14:28:07 -0800418 // TODO(b/121033016): refactor "notifyXXXX" methods below to a common "Buffer" object that is
419 // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such
420 // change should also get get rid of the "internalNotifyXXXX" methods above
421 void notifyChildSessionStarted(@NonNull String parentSessionId,
422 @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) {
423 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
424 new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED)
425 .setParentSessionId(parentSessionId)
426 .setClientContext(clientContext),
427 /* forceFlush= */ false));
428 }
429
430 void notifyChildSessionFinished(@NonNull String parentSessionId,
431 @NonNull String childSessionId) {
432 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
433 new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED)
434 .setParentSessionId(parentSessionId), /* forceFlush= */ false));
435 }
436
437 void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) {
438 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
439 new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED)
440 .setViewNode(node.mNode), /* forceFlush= */ false));
441 }
442
443 void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) {
444 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
445 new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id),
446 /* forceFlush= */ false));
447 }
448
449 void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id,
450 @Nullable CharSequence text, int flags) {
451 mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this,
452 new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id)
453 .setText(text), /* forceFlush= */ false));
454 }
455
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800456 @Override
457 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
458 pw.print(prefix); pw.print("id: "); pw.println(mId);
459 pw.print(prefix); pw.print("mContext: "); pw.println(mContext);
460 pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId());
461 if (mSystemServerInterface != null) {
462 pw.print(prefix); pw.print("mSystemServerInterface: ");
463 pw.println(mSystemServerInterface);
464 }
465 if (mDirectServiceInterface != null) {
466 pw.print(prefix); pw.print("mDirectServiceInterface: ");
467 pw.println(mDirectServiceInterface);
468 }
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800469 pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get());
470 pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled());
471 if (mContentCaptureSessionId != null) {
472 pw.print(prefix); pw.print("public id: "); pw.println(mContentCaptureSessionId);
473 }
474 pw.print(prefix); pw.print("state: "); pw.print(mState); pw.print(" (");
475 pw.print(getStateAsString(mState)); pw.println(")");
476 if (mApplicationToken != null) {
477 pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken);
478 }
479 if (mComponentName != null) {
480 pw.print(prefix); pw.print("component name: ");
481 pw.println(mComponentName.flattenToShortString());
482 }
483 if (mEvents != null && !mEvents.isEmpty()) {
484 final int numberEvents = mEvents.size();
485 pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents);
486 pw.print('/'); pw.println(MAX_BUFFER_SIZE);
487 if (VERBOSE && numberEvents > 0) {
488 final String prefix3 = prefix + " ";
489 for (int i = 0; i < numberEvents; i++) {
490 final ContentCaptureEvent event = mEvents.get(i);
491 pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw);
492 pw.println();
493 }
494 }
495 pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS);
496 pw.print(prefix); pw.print("next flush: ");
497 TimeUtils.formatDuration(mNextFlush - SystemClock.elapsedRealtime(), pw); pw.println();
498 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800499 super.dump(prefix, pw);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800500 }
501
502 /**
503 * Gets a string that can be used to identify the activity on logging statements.
504 */
505 private String getActivityDebugName() {
506 return mComponentName == null ? mContext.getPackageName()
507 : mComponentName.flattenToShortString();
508 }
509}