blob: 1e051a43a42fed1793e87865ab7f95ac438640f1 [file] [log] [blame]
Felipe Lemeaa5088e2018-12-10 14:53:58 -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 Lemed32d8f6f2019-02-15 10:25:33 -080018import static android.view.contentcapture.ContentCaptureHelper.sDebug;
19import static android.view.contentcapture.ContentCaptureHelper.sVerbose;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080020
Felipe Leme87a9dc92018-12-18 14:28:07 -080021import android.annotation.CallSuper;
Felipe Leme1af85ea2019-01-16 13:23:40 -080022import android.annotation.IntDef;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080023import android.annotation.NonNull;
24import android.annotation.Nullable;
Felipe Leme01b87492019-01-15 13:26:52 -080025import android.util.DebugUtils;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080026import android.util.Log;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080027import android.view.View;
28import android.view.ViewStructure;
29import android.view.autofill.AutofillId;
Felipe Lemeb63e0dd2018-12-18 11:56:42 -080030import android.view.contentcapture.ViewNode.ViewStructureImpl;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080031
Felipe Leme4bc0f6b2019-01-03 19:01:07 -080032import com.android.internal.annotations.GuardedBy;
Felipe Leme739b98f2019-01-11 12:31:20 -080033import com.android.internal.annotations.VisibleForTesting;
Felipe Lemeb53afba2019-01-16 15:47:18 -080034import com.android.internal.util.ArrayUtils;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080035import com.android.internal.util.Preconditions;
36
Felipe Lemeaa5088e2018-12-10 14:53:58 -080037import java.io.PrintWriter;
Felipe Leme1af85ea2019-01-16 13:23:40 -080038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
Felipe Leme87a9dc92018-12-18 14:28:07 -080040import java.util.ArrayList;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080041import java.util.UUID;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080042
43/**
44 * Session used to notify a system-provided Content Capture service about events associated with
45 * views.
46 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -080047public abstract class ContentCaptureSession implements AutoCloseable {
Felipe Lemeaa5088e2018-12-10 14:53:58 -080048
Felipe Lemeaf1a0e72019-01-03 11:07:25 -080049 private static final String TAG = ContentCaptureSession.class.getSimpleName();
50
Felipe Lemeaa5088e2018-12-10 14:53:58 -080051 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -080052 * Initial state, when there is no session.
53 *
54 * @hide
55 */
Felipe Leme01b87492019-01-15 13:26:52 -080056 // NOTE: not prefixed by STATE_ so it's not printed on getStateAsString()
Felipe Leme35ea7632019-01-29 16:13:30 -080057 public static final int UNKNOWN_STATE = 0x0;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080058
59 /**
60 * Service's startSession() was called, but server didn't confirm it was created yet.
61 *
62 * @hide
63 */
Felipe Leme01b87492019-01-15 13:26:52 -080064 public static final int STATE_WAITING_FOR_SERVER = 0x1;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080065
66 /**
67 * Session is active.
68 *
69 * @hide
70 */
Felipe Leme01b87492019-01-15 13:26:52 -080071 public static final int STATE_ACTIVE = 0x2;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080072
73 /**
Felipe Lemee127c9a2019-01-04 14:56:38 -080074 * Session is disabled because there is no service for this user.
Felipe Lemeaa5088e2018-12-10 14:53:58 -080075 *
76 * @hide
77 */
Felipe Leme01b87492019-01-15 13:26:52 -080078 public static final int STATE_DISABLED = 0x4;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080079
80 /**
Felipe Lemeb96878492018-12-17 12:22:29 -080081 * Session is disabled because its id already existed on server.
82 *
83 * @hide
84 */
Felipe Leme01b87492019-01-15 13:26:52 -080085 public static final int STATE_DUPLICATED_ID = 0x8;
86
87 /**
88 * Session is disabled because service is not set for user.
89 *
90 * @hide
91 */
92 public static final int STATE_NO_SERVICE = 0x10;
Felipe Lemeb96878492018-12-17 12:22:29 -080093
Adam He328c0e32019-01-03 15:19:22 -080094 /**
95 * Session is disabled by FLAG_SECURE
96 *
97 * @hide
98 */
Felipe Leme01b87492019-01-15 13:26:52 -080099 public static final int STATE_FLAG_SECURE = 0x20;
Adam He328c0e32019-01-03 15:19:22 -0800100
Adam He6079d152019-01-10 11:37:17 -0800101 /**
Felipe Leme91ddeca2019-01-24 18:01:58 -0800102 * Session is disabled manually by the specific app
103 * (through {@link ContentCaptureManager#setContentCaptureEnabled(boolean)}).
Adam He6079d152019-01-10 11:37:17 -0800104 *
105 * @hide
106 */
Felipe Leme01b87492019-01-15 13:26:52 -0800107 public static final int STATE_BY_APP = 0x40;
108
Felipe Lemed65692c2019-01-16 12:10:50 -0800109 /**
110 * Session is disabled because session start was never replied.
111 *
112 * @hide
113 */
114 public static final int STATE_NO_RESPONSE = 0x80;
115
116 /**
117 * Session is disabled because an internal error.
118 *
119 * @hide
120 */
121 public static final int STATE_INTERNAL_ERROR = 0x100;
Adam He6079d152019-01-10 11:37:17 -0800122
Felipe Lemee8eae9d42019-02-13 15:41:55 -0800123 /**
124 * Session is disabled because service didn't whitelist package.
125 *
126 * @hide
127 */
128 public static final int STATE_PACKAGE_NOT_WHITELISTED = 0x200;
129
Felipe Leme87a9dc92018-12-18 14:28:07 -0800130 private static final int INITIAL_CHILDREN_CAPACITY = 5;
131
Felipe Leme1af85ea2019-01-16 13:23:40 -0800132 /** @hide */
133 public static final int FLUSH_REASON_FULL = 1;
134 /** @hide */
135 public static final int FLUSH_REASON_ACTIVITY_PAUSED = 2;
136 /** @hide */
137 public static final int FLUSH_REASON_ACTIVITY_RESUMED = 3;
138 /** @hide */
139 public static final int FLUSH_REASON_SESSION_STARTED = 4;
140 /** @hide */
141 public static final int FLUSH_REASON_SESSION_FINISHED = 5;
142 /** @hide */
143 public static final int FLUSH_REASON_IDLE_TIMEOUT = 6;
144
145 /** @hide */
146 @IntDef(prefix = { "FLUSH_REASON_" }, value = {
147 FLUSH_REASON_FULL,
148 FLUSH_REASON_ACTIVITY_PAUSED,
149 FLUSH_REASON_ACTIVITY_RESUMED,
150 FLUSH_REASON_SESSION_STARTED,
151 FLUSH_REASON_SESSION_FINISHED,
152 FLUSH_REASON_IDLE_TIMEOUT
153 })
154 @Retention(RetentionPolicy.SOURCE)
155 @interface FlushReason{}
156
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800157 private final Object mLock = new Object();
158
Felipe Lemebef744c2019-01-03 11:07:25 -0800159 /**
160 * Guard use to ignore events after it's destroyed.
161 */
162 @NonNull
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800163 @GuardedBy("mLock")
164 private boolean mDestroyed;
Felipe Lemebef744c2019-01-03 11:07:25 -0800165
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800166 /** @hide */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800167 @Nullable
Felipe Leme739b98f2019-01-11 12:31:20 -0800168 protected final String mId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800169
Felipe Leme35ea7632019-01-29 16:13:30 -0800170 private int mState = UNKNOWN_STATE;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800171
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800172 // Lazily created on demand.
173 private ContentCaptureSessionId mContentCaptureSessionId;
174
175 /**
Felipe Leme4eecbe62019-02-11 17:50:17 -0800176 * {@link ContentCaptureContext} set by client, or {@code null} when it's the
177 * {@link ContentCaptureManager#getMainContentCaptureSession() default session} for the
178 * context.
179 */
180 @Nullable
181 private ContentCaptureContext mClientContext;
182
183 /**
Felipe Leme87a9dc92018-12-18 14:28:07 -0800184 * List of children session.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800185 */
186 @Nullable
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800187 @GuardedBy("mLock")
Felipe Leme87a9dc92018-12-18 14:28:07 -0800188 private ArrayList<ContentCaptureSession> mChildren;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800189
190 /** @hide */
Felipe Leme87a9dc92018-12-18 14:28:07 -0800191 protected ContentCaptureSession() {
Felipe Leme739b98f2019-01-11 12:31:20 -0800192 this(UUID.randomUUID().toString());
193 }
194
195 /** @hide */
196 @VisibleForTesting
197 public ContentCaptureSession(@NonNull String id) {
198 mId = Preconditions.checkNotNull(id);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800199 }
200
Felipe Leme4eecbe62019-02-11 17:50:17 -0800201 // Used by ChildCOntentCaptureSession
202 ContentCaptureSession(@NonNull ContentCaptureContext initialContext) {
203 this();
204 mClientContext = Preconditions.checkNotNull(initialContext);
205 }
206
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800207 /** @hide */
208 @NonNull
209 abstract MainContentCaptureSession getMainCaptureSession();
210
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800211 /**
212 * Gets the id used to identify this session.
213 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800214 public final ContentCaptureSessionId getContentCaptureSessionId() {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800215 if (mContentCaptureSessionId == null) {
216 mContentCaptureSessionId = new ContentCaptureSessionId(mId);
217 }
218 return mContentCaptureSessionId;
219 }
220
Felipe Leme739b98f2019-01-11 12:31:20 -0800221 /** @hide */
222 @VisibleForTesting
223 public int getIdAsInt() {
224 // TODO(b/121197119): use sessionId instead of hashcode once it's changed to int
225 return mId.hashCode();
226 }
227
Felipe Leme01297692019-01-29 18:16:23 -0800228 /** @hide */
229 @NonNull
230 public String getId() {
231 return mId;
232 }
233
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800234 /**
Felipe Leme87a9dc92018-12-18 14:28:07 -0800235 * Creates a new {@link ContentCaptureSession}.
236 *
237 * <p>See {@link View#setContentCaptureSession(ContentCaptureSession)} for more info.
238 */
239 @NonNull
240 public final ContentCaptureSession createContentCaptureSession(
241 @NonNull ContentCaptureContext context) {
242 final ContentCaptureSession child = newChild(context);
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800243 if (sDebug) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800244 Log.d(TAG, "createContentCaptureSession(" + context + ": parent=" + mId + ", child="
Felipe Leme87a9dc92018-12-18 14:28:07 -0800245 + child.mId);
246 }
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800247 synchronized (mLock) {
248 if (mChildren == null) {
249 mChildren = new ArrayList<>(INITIAL_CHILDREN_CAPACITY);
250 }
251 mChildren.add(child);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800252 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800253 return child;
254 }
255
256 abstract ContentCaptureSession newChild(@NonNull ContentCaptureContext context);
257
258 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800259 * Flushes the buffered events to the service.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800260 */
Felipe Leme1af85ea2019-01-16 13:23:40 -0800261 abstract void flush(@FlushReason int reason);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800262
263 /**
Felipe Leme4eecbe62019-02-11 17:50:17 -0800264 * Sets the {@link ContentCaptureContext} associated with the session.
265 *
266 * <p>Typically used to change the context associated with the default session from an activity.
267 */
268 public final void setContentCaptureContext(@Nullable ContentCaptureContext context) {
269 mClientContext = context;
270 updateContentCaptureContext(context);
271 }
272
273 abstract void updateContentCaptureContext(@Nullable ContentCaptureContext context);
274
275 /**
276 * Gets the {@link ContentCaptureContext} associated with the session.
277 *
278 * @return context set on constructor or by
279 * {@link #setContentCaptureContext(ContentCaptureContext)}, or {@code null} if never
280 * explicitly set.
281 */
282 @Nullable
283 public final ContentCaptureContext getContentCaptureContext() {
284 return mClientContext;
285 }
286
287 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800288 * Destroys this session, flushing out all pending notifications to the service.
289 *
290 * <p>Once destroyed, any new notification will be dropped.
291 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800292 public final void destroy() {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800293 synchronized (mLock) {
294 if (mDestroyed) {
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800295 if (sDebug) Log.d(TAG, "destroy(" + mId + "): already destroyed");
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800296 return;
297 }
298 mDestroyed = true;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800299
Felipe Leme2057c382019-01-09 10:05:56 -0800300 // TODO(b/111276913): check state (for example, how to handle if it's waiting for remote
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800301 // id) and send it to the cache of batched commands
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800302 if (sVerbose) {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800303 Log.v(TAG, "destroy(): state=" + getStateAsString(mState) + ", mId=" + mId);
304 }
305 // Finish children first
306 if (mChildren != null) {
307 final int numberChildren = mChildren.size();
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800308 if (sVerbose) Log.v(TAG, "Destroying " + numberChildren + " children first");
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800309 for (int i = 0; i < numberChildren; i++) {
310 final ContentCaptureSession child = mChildren.get(i);
311 try {
312 child.destroy();
313 } catch (Exception e) {
314 Log.w(TAG, "exception destroying child session #" + i + ": " + e);
315 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800316 }
317 }
318 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800319
Felipe Leme87a9dc92018-12-18 14:28:07 -0800320 try {
Felipe Leme1af85ea2019-01-16 13:23:40 -0800321 flush(FLUSH_REASON_SESSION_FINISHED);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800322 } finally {
323 onDestroy();
324 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800325 }
326
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800327 abstract void onDestroy();
328
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800329 /** @hide */
330 @Override
331 public void close() {
332 destroy();
333 }
334
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800335 /**
336 * Notifies the Content Capture Service that a node has been added to the view structure.
337 *
338 * <p>Typically called "manually" by views that handle their own virtual view hierarchy, or
339 * automatically by the Android System for views that return {@code true} on
340 * {@link View#onProvideContentCaptureStructure(ViewStructure, int)}.
341 *
342 * @param node node that has been added.
343 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800344 public final void notifyViewAppeared(@NonNull ViewStructure node) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800345 Preconditions.checkNotNull(node);
346 if (!isContentCaptureEnabled()) return;
347
348 if (!(node instanceof ViewNode.ViewStructureImpl)) {
349 throw new IllegalArgumentException("Invalid node class: " + node.getClass());
350 }
351
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800352 internalNotifyViewAppeared((ViewStructureImpl) node);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800353 }
354
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800355 abstract void internalNotifyViewAppeared(@NonNull ViewNode.ViewStructureImpl node);
356
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800357 /**
358 * Notifies the Content Capture Service that a node has been removed from the view structure.
359 *
360 * <p>Typically called "manually" by views that handle their own virtual view hierarchy, or
361 * automatically by the Android System for standard views.
362 *
363 * @param id id of the node that has been removed.
364 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800365 public final void notifyViewDisappeared(@NonNull AutofillId id) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800366 Preconditions.checkNotNull(id);
367 if (!isContentCaptureEnabled()) return;
368
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800369 internalNotifyViewDisappeared(id);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800370 }
371
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800372 abstract void internalNotifyViewDisappeared(@NonNull AutofillId id);
373
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800374 /**
Felipe Lemeb53afba2019-01-16 15:47:18 -0800375 * Notifies the Content Capture Service that many nodes has been removed from a virtual view
376 * structure.
377 *
378 * <p>Should only be called by views that handle their own virtual view hierarchy.
379 *
380 * @param hostId id of the view hosting the virtual hierarchy.
381 * @param virtualIds ids of the virtual children.
382 *
383 * @throws IllegalArgumentException if the {@code hostId} is an autofill id for a virtual view.
384 * @throws IllegalArgumentException if {@code virtualIds} is empty
385 */
386 public final void notifyViewsDisappeared(@NonNull AutofillId hostId,
Felipe Lemea75333c2019-01-22 15:45:32 -0800387 @NonNull long[] virtualIds) {
388 Preconditions.checkArgument(hostId.isNonVirtual(), "parent cannot be virtual");
Felipe Lemeb53afba2019-01-16 15:47:18 -0800389 Preconditions.checkArgument(!ArrayUtils.isEmpty(virtualIds), "virtual ids cannot be empty");
390 if (!isContentCaptureEnabled()) return;
391
Felipe Leme48f363c2019-01-17 10:53:46 -0800392 // TODO(b/123036895): use a internalNotifyViewsDisappeared that optimizes how the event is
393 // parcelized
Felipe Lemea75333c2019-01-22 15:45:32 -0800394 for (long id : virtualIds) {
Felipe Lemeb53afba2019-01-16 15:47:18 -0800395 internalNotifyViewDisappeared(new AutofillId(hostId, id, getIdAsInt()));
396 }
397 }
398
399 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800400 * Notifies the Intelligence Service that the value of a text node has been changed.
401 *
402 * @param id of the node.
403 * @param text new text.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800404 */
Felipe Leme35ea7632019-01-29 16:13:30 -0800405 public final void notifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800406 Preconditions.checkNotNull(id);
407
408 if (!isContentCaptureEnabled()) return;
409
Felipe Leme21e8dcb2019-01-18 09:09:45 -0800410 internalNotifyViewTextChanged(id, text);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800411 }
412
Felipe Leme21e8dcb2019-01-18 09:09:45 -0800413 abstract void internalNotifyViewTextChanged(@NonNull AutofillId id,
414 @Nullable CharSequence text);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800415
Felipe Leme01297692019-01-29 18:16:23 -0800416 /** @hide */
417 public abstract void internalNotifyViewHierarchyEvent(boolean started);
418
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800419 /**
420 * Creates a {@link ViewStructure} for a "standard" view.
421 *
422 * @hide
423 */
424 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800425 public final ViewStructure newViewStructure(@NonNull View view) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800426 return new ViewNode.ViewStructureImpl(view);
427 }
428
429 /**
Felipe Leme2057c382019-01-09 10:05:56 -0800430 * Creates a new {@link AutofillId} for a virtual child, so it can be used to uniquely identify
431 * the children in the session.
432 *
433 * @param parentId id of the virtual view parent (it can be obtained by calling
434 * {@link ViewStructure#getAutofillId()} on the parent).
435 * @param virtualChildId id of the virtual child, relative to the parent.
436 *
437 * @return if for the virtual child
438 *
439 * @throws IllegalArgumentException if the {@code parentId} is a virtual child id.
440 */
Felipe Lemea75333c2019-01-22 15:45:32 -0800441 public @NonNull AutofillId newAutofillId(@NonNull AutofillId parentId, long virtualChildId) {
Felipe Leme2057c382019-01-09 10:05:56 -0800442 Preconditions.checkNotNull(parentId);
Felipe Lemea75333c2019-01-22 15:45:32 -0800443 Preconditions.checkArgument(parentId.isNonVirtual(), "virtual ids cannot have children");
Felipe Leme739b98f2019-01-11 12:31:20 -0800444 return new AutofillId(parentId, virtualChildId, getIdAsInt());
Felipe Leme2057c382019-01-09 10:05:56 -0800445 }
446
447 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800448 * Creates a {@link ViewStructure} for a "virtual" view, so it can be passed to
449 * {@link #notifyViewAppeared(ViewStructure)} by the view managing the virtual view hierarchy.
450 *
451 * @param parentId id of the virtual view parent (it can be obtained by calling
452 * {@link ViewStructure#getAutofillId()} on the parent).
453 * @param virtualId id of the virtual child, relative to the parent.
454 *
455 * @return a new {@link ViewStructure} that can be used for Content Capture purposes.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800456 */
457 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800458 public final ViewStructure newVirtualViewStructure(@NonNull AutofillId parentId,
Felipe Lemea75333c2019-01-22 15:45:32 -0800459 long virtualId) {
Felipe Leme739b98f2019-01-11 12:31:20 -0800460 return new ViewNode.ViewStructureImpl(parentId, virtualId, getIdAsInt());
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800461 }
462
Felipe Lemebef744c2019-01-03 11:07:25 -0800463 boolean isContentCaptureEnabled() {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800464 synchronized (mLock) {
465 return !mDestroyed;
466 }
Felipe Lemebef744c2019-01-03 11:07:25 -0800467 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800468
Felipe Leme87a9dc92018-12-18 14:28:07 -0800469 @CallSuper
470 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800471 pw.print(prefix); pw.print("id: "); pw.println(mId);
Felipe Leme4eecbe62019-02-11 17:50:17 -0800472 if (mClientContext != null) {
473 pw.print(prefix); mClientContext.dump(pw); pw.println();
474 }
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800475 synchronized (mLock) {
476 pw.print(prefix); pw.print("destroyed: "); pw.println(mDestroyed);
477 if (mChildren != null && !mChildren.isEmpty()) {
478 final String prefix2 = prefix + " ";
479 final int numberChildren = mChildren.size();
480 pw.print(prefix); pw.print("number children: "); pw.println(numberChildren);
481 for (int i = 0; i < numberChildren; i++) {
482 final ContentCaptureSession child = mChildren.get(i);
483 pw.print(prefix); pw.print(i); pw.println(": "); child.dump(prefix2, pw);
484 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800485 }
486 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800487 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800488
489 @Override
490 public String toString() {
491 return mId;
492 }
493
Felipe Leme1af85ea2019-01-16 13:23:40 -0800494 /** @hide */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800495 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800496 protected static String getStateAsString(int state) {
Felipe Leme35ea7632019-01-29 16:13:30 -0800497 return state + " (" + (state == UNKNOWN_STATE ? "UNKNOWN"
Felipe Leme01b87492019-01-15 13:26:52 -0800498 : DebugUtils.flagsToString(ContentCaptureSession.class, "STATE_", state)) + ")";
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800499 }
Felipe Leme1af85ea2019-01-16 13:23:40 -0800500
501 /** @hide */
502 @NonNull
503 static String getflushReasonAsString(@FlushReason int reason) {
504 switch (reason) {
505 case FLUSH_REASON_FULL:
506 return "FULL";
507 case FLUSH_REASON_ACTIVITY_PAUSED:
508 return "PAUSED";
509 case FLUSH_REASON_ACTIVITY_RESUMED:
510 return "RESUMED";
511 case FLUSH_REASON_SESSION_STARTED:
512 return "STARTED";
513 case FLUSH_REASON_SESSION_FINISHED:
514 return "FINISHED";
515 case FLUSH_REASON_IDLE_TIMEOUT:
516 return "IDLE";
517 default:
518 return "UNKOWN-" + reason;
519 }
520 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800521}