blob: ec3b44a268e562cd57d1c67fa2d45bab5e70fd16 [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 Leme87a9dc92018-12-18 14:28:07 -0800123 private static final int INITIAL_CHILDREN_CAPACITY = 5;
124
Felipe Leme1af85ea2019-01-16 13:23:40 -0800125 /** @hide */
126 public static final int FLUSH_REASON_FULL = 1;
127 /** @hide */
128 public static final int FLUSH_REASON_ACTIVITY_PAUSED = 2;
129 /** @hide */
130 public static final int FLUSH_REASON_ACTIVITY_RESUMED = 3;
131 /** @hide */
132 public static final int FLUSH_REASON_SESSION_STARTED = 4;
133 /** @hide */
134 public static final int FLUSH_REASON_SESSION_FINISHED = 5;
135 /** @hide */
136 public static final int FLUSH_REASON_IDLE_TIMEOUT = 6;
137
138 /** @hide */
139 @IntDef(prefix = { "FLUSH_REASON_" }, value = {
140 FLUSH_REASON_FULL,
141 FLUSH_REASON_ACTIVITY_PAUSED,
142 FLUSH_REASON_ACTIVITY_RESUMED,
143 FLUSH_REASON_SESSION_STARTED,
144 FLUSH_REASON_SESSION_FINISHED,
145 FLUSH_REASON_IDLE_TIMEOUT
146 })
147 @Retention(RetentionPolicy.SOURCE)
148 @interface FlushReason{}
149
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800150 private final Object mLock = new Object();
151
Felipe Lemebef744c2019-01-03 11:07:25 -0800152 /**
153 * Guard use to ignore events after it's destroyed.
154 */
155 @NonNull
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800156 @GuardedBy("mLock")
157 private boolean mDestroyed;
Felipe Lemebef744c2019-01-03 11:07:25 -0800158
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800159 /** @hide */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800160 @Nullable
Felipe Leme739b98f2019-01-11 12:31:20 -0800161 protected final String mId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800162
Felipe Leme35ea7632019-01-29 16:13:30 -0800163 private int mState = UNKNOWN_STATE;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800164
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800165 // Lazily created on demand.
166 private ContentCaptureSessionId mContentCaptureSessionId;
167
168 /**
Felipe Leme4eecbe62019-02-11 17:50:17 -0800169 * {@link ContentCaptureContext} set by client, or {@code null} when it's the
170 * {@link ContentCaptureManager#getMainContentCaptureSession() default session} for the
171 * context.
172 */
173 @Nullable
174 private ContentCaptureContext mClientContext;
175
176 /**
Felipe Leme87a9dc92018-12-18 14:28:07 -0800177 * List of children session.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800178 */
179 @Nullable
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800180 @GuardedBy("mLock")
Felipe Leme87a9dc92018-12-18 14:28:07 -0800181 private ArrayList<ContentCaptureSession> mChildren;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800182
183 /** @hide */
Felipe Leme87a9dc92018-12-18 14:28:07 -0800184 protected ContentCaptureSession() {
Felipe Leme739b98f2019-01-11 12:31:20 -0800185 this(UUID.randomUUID().toString());
186 }
187
188 /** @hide */
189 @VisibleForTesting
190 public ContentCaptureSession(@NonNull String id) {
191 mId = Preconditions.checkNotNull(id);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800192 }
193
Felipe Leme4eecbe62019-02-11 17:50:17 -0800194 // Used by ChildCOntentCaptureSession
195 ContentCaptureSession(@NonNull ContentCaptureContext initialContext) {
196 this();
197 mClientContext = Preconditions.checkNotNull(initialContext);
198 }
199
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800200 /** @hide */
201 @NonNull
202 abstract MainContentCaptureSession getMainCaptureSession();
203
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800204 /**
205 * Gets the id used to identify this session.
206 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800207 public final ContentCaptureSessionId getContentCaptureSessionId() {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800208 if (mContentCaptureSessionId == null) {
209 mContentCaptureSessionId = new ContentCaptureSessionId(mId);
210 }
211 return mContentCaptureSessionId;
212 }
213
Felipe Leme739b98f2019-01-11 12:31:20 -0800214 /** @hide */
215 @VisibleForTesting
216 public int getIdAsInt() {
217 // TODO(b/121197119): use sessionId instead of hashcode once it's changed to int
218 return mId.hashCode();
219 }
220
Felipe Leme01297692019-01-29 18:16:23 -0800221 /** @hide */
222 @NonNull
223 public String getId() {
224 return mId;
225 }
226
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800227 /**
Felipe Leme87a9dc92018-12-18 14:28:07 -0800228 * Creates a new {@link ContentCaptureSession}.
229 *
230 * <p>See {@link View#setContentCaptureSession(ContentCaptureSession)} for more info.
231 */
232 @NonNull
233 public final ContentCaptureSession createContentCaptureSession(
234 @NonNull ContentCaptureContext context) {
235 final ContentCaptureSession child = newChild(context);
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800236 if (sDebug) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800237 Log.d(TAG, "createContentCaptureSession(" + context + ": parent=" + mId + ", child="
Felipe Leme87a9dc92018-12-18 14:28:07 -0800238 + child.mId);
239 }
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800240 synchronized (mLock) {
241 if (mChildren == null) {
242 mChildren = new ArrayList<>(INITIAL_CHILDREN_CAPACITY);
243 }
244 mChildren.add(child);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800245 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800246 return child;
247 }
248
249 abstract ContentCaptureSession newChild(@NonNull ContentCaptureContext context);
250
251 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800252 * Flushes the buffered events to the service.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800253 */
Felipe Leme1af85ea2019-01-16 13:23:40 -0800254 abstract void flush(@FlushReason int reason);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800255
256 /**
Felipe Leme4eecbe62019-02-11 17:50:17 -0800257 * Sets the {@link ContentCaptureContext} associated with the session.
258 *
259 * <p>Typically used to change the context associated with the default session from an activity.
260 */
261 public final void setContentCaptureContext(@Nullable ContentCaptureContext context) {
262 mClientContext = context;
263 updateContentCaptureContext(context);
264 }
265
266 abstract void updateContentCaptureContext(@Nullable ContentCaptureContext context);
267
268 /**
269 * Gets the {@link ContentCaptureContext} associated with the session.
270 *
271 * @return context set on constructor or by
272 * {@link #setContentCaptureContext(ContentCaptureContext)}, or {@code null} if never
273 * explicitly set.
274 */
275 @Nullable
276 public final ContentCaptureContext getContentCaptureContext() {
277 return mClientContext;
278 }
279
280 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800281 * Destroys this session, flushing out all pending notifications to the service.
282 *
283 * <p>Once destroyed, any new notification will be dropped.
284 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800285 public final void destroy() {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800286 synchronized (mLock) {
287 if (mDestroyed) {
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800288 if (sDebug) Log.d(TAG, "destroy(" + mId + "): already destroyed");
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800289 return;
290 }
291 mDestroyed = true;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800292
Felipe Leme2057c382019-01-09 10:05:56 -0800293 // TODO(b/111276913): check state (for example, how to handle if it's waiting for remote
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800294 // id) and send it to the cache of batched commands
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800295 if (sVerbose) {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800296 Log.v(TAG, "destroy(): state=" + getStateAsString(mState) + ", mId=" + mId);
297 }
298 // Finish children first
299 if (mChildren != null) {
300 final int numberChildren = mChildren.size();
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800301 if (sVerbose) Log.v(TAG, "Destroying " + numberChildren + " children first");
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800302 for (int i = 0; i < numberChildren; i++) {
303 final ContentCaptureSession child = mChildren.get(i);
304 try {
305 child.destroy();
306 } catch (Exception e) {
307 Log.w(TAG, "exception destroying child session #" + i + ": " + e);
308 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800309 }
310 }
311 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800312
Felipe Leme87a9dc92018-12-18 14:28:07 -0800313 try {
Felipe Leme1af85ea2019-01-16 13:23:40 -0800314 flush(FLUSH_REASON_SESSION_FINISHED);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800315 } finally {
316 onDestroy();
317 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800318 }
319
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800320 abstract void onDestroy();
321
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800322 /** @hide */
323 @Override
324 public void close() {
325 destroy();
326 }
327
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800328 /**
329 * Notifies the Content Capture Service that a node has been added to the view structure.
330 *
331 * <p>Typically called "manually" by views that handle their own virtual view hierarchy, or
332 * automatically by the Android System for views that return {@code true} on
333 * {@link View#onProvideContentCaptureStructure(ViewStructure, int)}.
334 *
335 * @param node node that has been added.
336 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800337 public final void notifyViewAppeared(@NonNull ViewStructure node) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800338 Preconditions.checkNotNull(node);
339 if (!isContentCaptureEnabled()) return;
340
341 if (!(node instanceof ViewNode.ViewStructureImpl)) {
342 throw new IllegalArgumentException("Invalid node class: " + node.getClass());
343 }
344
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800345 internalNotifyViewAppeared((ViewStructureImpl) node);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800346 }
347
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800348 abstract void internalNotifyViewAppeared(@NonNull ViewNode.ViewStructureImpl node);
349
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800350 /**
351 * Notifies the Content Capture Service that a node has been removed from the view structure.
352 *
353 * <p>Typically called "manually" by views that handle their own virtual view hierarchy, or
354 * automatically by the Android System for standard views.
355 *
356 * @param id id of the node that has been removed.
357 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800358 public final void notifyViewDisappeared(@NonNull AutofillId id) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800359 Preconditions.checkNotNull(id);
360 if (!isContentCaptureEnabled()) return;
361
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800362 internalNotifyViewDisappeared(id);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800363 }
364
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800365 abstract void internalNotifyViewDisappeared(@NonNull AutofillId id);
366
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800367 /**
Felipe Lemeb53afba2019-01-16 15:47:18 -0800368 * Notifies the Content Capture Service that many nodes has been removed from a virtual view
369 * structure.
370 *
371 * <p>Should only be called by views that handle their own virtual view hierarchy.
372 *
373 * @param hostId id of the view hosting the virtual hierarchy.
374 * @param virtualIds ids of the virtual children.
375 *
376 * @throws IllegalArgumentException if the {@code hostId} is an autofill id for a virtual view.
377 * @throws IllegalArgumentException if {@code virtualIds} is empty
378 */
379 public final void notifyViewsDisappeared(@NonNull AutofillId hostId,
Felipe Lemea75333c2019-01-22 15:45:32 -0800380 @NonNull long[] virtualIds) {
381 Preconditions.checkArgument(hostId.isNonVirtual(), "parent cannot be virtual");
Felipe Lemeb53afba2019-01-16 15:47:18 -0800382 Preconditions.checkArgument(!ArrayUtils.isEmpty(virtualIds), "virtual ids cannot be empty");
383 if (!isContentCaptureEnabled()) return;
384
Felipe Leme48f363c2019-01-17 10:53:46 -0800385 // TODO(b/123036895): use a internalNotifyViewsDisappeared that optimizes how the event is
386 // parcelized
Felipe Lemea75333c2019-01-22 15:45:32 -0800387 for (long id : virtualIds) {
Felipe Lemeb53afba2019-01-16 15:47:18 -0800388 internalNotifyViewDisappeared(new AutofillId(hostId, id, getIdAsInt()));
389 }
390 }
391
392 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800393 * Notifies the Intelligence Service that the value of a text node has been changed.
394 *
395 * @param id of the node.
396 * @param text new text.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800397 */
Felipe Leme35ea7632019-01-29 16:13:30 -0800398 public final void notifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800399 Preconditions.checkNotNull(id);
400
401 if (!isContentCaptureEnabled()) return;
402
Felipe Leme21e8dcb2019-01-18 09:09:45 -0800403 internalNotifyViewTextChanged(id, text);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800404 }
405
Felipe Leme21e8dcb2019-01-18 09:09:45 -0800406 abstract void internalNotifyViewTextChanged(@NonNull AutofillId id,
407 @Nullable CharSequence text);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800408
Felipe Leme01297692019-01-29 18:16:23 -0800409 /** @hide */
410 public abstract void internalNotifyViewHierarchyEvent(boolean started);
411
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800412 /**
413 * Creates a {@link ViewStructure} for a "standard" view.
414 *
415 * @hide
416 */
417 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800418 public final ViewStructure newViewStructure(@NonNull View view) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800419 return new ViewNode.ViewStructureImpl(view);
420 }
421
422 /**
Felipe Leme2057c382019-01-09 10:05:56 -0800423 * Creates a new {@link AutofillId} for a virtual child, so it can be used to uniquely identify
424 * the children in the session.
425 *
426 * @param parentId id of the virtual view parent (it can be obtained by calling
427 * {@link ViewStructure#getAutofillId()} on the parent).
428 * @param virtualChildId id of the virtual child, relative to the parent.
429 *
430 * @return if for the virtual child
431 *
432 * @throws IllegalArgumentException if the {@code parentId} is a virtual child id.
433 */
Felipe Lemea75333c2019-01-22 15:45:32 -0800434 public @NonNull AutofillId newAutofillId(@NonNull AutofillId parentId, long virtualChildId) {
Felipe Leme2057c382019-01-09 10:05:56 -0800435 Preconditions.checkNotNull(parentId);
Felipe Lemea75333c2019-01-22 15:45:32 -0800436 Preconditions.checkArgument(parentId.isNonVirtual(), "virtual ids cannot have children");
Felipe Leme739b98f2019-01-11 12:31:20 -0800437 return new AutofillId(parentId, virtualChildId, getIdAsInt());
Felipe Leme2057c382019-01-09 10:05:56 -0800438 }
439
440 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800441 * Creates a {@link ViewStructure} for a "virtual" view, so it can be passed to
442 * {@link #notifyViewAppeared(ViewStructure)} by the view managing the virtual view hierarchy.
443 *
444 * @param parentId id of the virtual view parent (it can be obtained by calling
445 * {@link ViewStructure#getAutofillId()} on the parent).
446 * @param virtualId id of the virtual child, relative to the parent.
447 *
448 * @return a new {@link ViewStructure} that can be used for Content Capture purposes.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800449 */
450 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800451 public final ViewStructure newVirtualViewStructure(@NonNull AutofillId parentId,
Felipe Lemea75333c2019-01-22 15:45:32 -0800452 long virtualId) {
Felipe Leme739b98f2019-01-11 12:31:20 -0800453 return new ViewNode.ViewStructureImpl(parentId, virtualId, getIdAsInt());
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800454 }
455
Felipe Lemebef744c2019-01-03 11:07:25 -0800456 boolean isContentCaptureEnabled() {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800457 synchronized (mLock) {
458 return !mDestroyed;
459 }
Felipe Lemebef744c2019-01-03 11:07:25 -0800460 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800461
Felipe Leme87a9dc92018-12-18 14:28:07 -0800462 @CallSuper
463 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800464 pw.print(prefix); pw.print("id: "); pw.println(mId);
Felipe Leme4eecbe62019-02-11 17:50:17 -0800465 if (mClientContext != null) {
466 pw.print(prefix); mClientContext.dump(pw); pw.println();
467 }
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800468 synchronized (mLock) {
469 pw.print(prefix); pw.print("destroyed: "); pw.println(mDestroyed);
470 if (mChildren != null && !mChildren.isEmpty()) {
471 final String prefix2 = prefix + " ";
472 final int numberChildren = mChildren.size();
473 pw.print(prefix); pw.print("number children: "); pw.println(numberChildren);
474 for (int i = 0; i < numberChildren; i++) {
475 final ContentCaptureSession child = mChildren.get(i);
476 pw.print(prefix); pw.print(i); pw.println(": "); child.dump(prefix2, pw);
477 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800478 }
479 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800480 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800481
482 @Override
483 public String toString() {
484 return mId;
485 }
486
Felipe Leme1af85ea2019-01-16 13:23:40 -0800487 /** @hide */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800488 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800489 protected static String getStateAsString(int state) {
Felipe Leme35ea7632019-01-29 16:13:30 -0800490 return state + " (" + (state == UNKNOWN_STATE ? "UNKNOWN"
Felipe Leme01b87492019-01-15 13:26:52 -0800491 : DebugUtils.flagsToString(ContentCaptureSession.class, "STATE_", state)) + ")";
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800492 }
Felipe Leme1af85ea2019-01-16 13:23:40 -0800493
494 /** @hide */
495 @NonNull
496 static String getflushReasonAsString(@FlushReason int reason) {
497 switch (reason) {
498 case FLUSH_REASON_FULL:
499 return "FULL";
500 case FLUSH_REASON_ACTIVITY_PAUSED:
501 return "PAUSED";
502 case FLUSH_REASON_ACTIVITY_RESUMED:
503 return "RESUMED";
504 case FLUSH_REASON_SESSION_STARTED:
505 return "STARTED";
506 case FLUSH_REASON_SESSION_FINISHED:
507 return "FINISHED";
508 case FLUSH_REASON_IDLE_TIMEOUT:
509 return "IDLE";
510 default:
511 return "UNKOWN-" + reason;
512 }
513 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800514}