blob: ab8f346bdd2e668a82d97bfe846c5567e9d24b00 [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 */
Felipe Lemed58c1ea2019-02-21 11:22:45 -0800135 public static final int FLUSH_REASON_VIEW_ROOT_ENTERED = 2;
Felipe Leme1af85ea2019-01-16 13:23:40 -0800136 /** @hide */
Felipe Lemeb0da18f2019-02-22 15:10:02 -0800137 public static final int FLUSH_REASON_SESSION_STARTED = 3;
Felipe Leme1af85ea2019-01-16 13:23:40 -0800138 /** @hide */
Felipe Lemeb0da18f2019-02-22 15:10:02 -0800139 public static final int FLUSH_REASON_SESSION_FINISHED = 4;
Felipe Leme1af85ea2019-01-16 13:23:40 -0800140 /** @hide */
Felipe Lemeb0da18f2019-02-22 15:10:02 -0800141 public static final int FLUSH_REASON_IDLE_TIMEOUT = 5;
Felipe Leme1af85ea2019-01-16 13:23:40 -0800142
143 /** @hide */
144 @IntDef(prefix = { "FLUSH_REASON_" }, value = {
145 FLUSH_REASON_FULL,
Felipe Lemeb0da18f2019-02-22 15:10:02 -0800146 FLUSH_REASON_VIEW_ROOT_ENTERED,
Felipe Leme1af85ea2019-01-16 13:23:40 -0800147 FLUSH_REASON_SESSION_STARTED,
148 FLUSH_REASON_SESSION_FINISHED,
Felipe Lemeb0da18f2019-02-22 15:10:02 -0800149 FLUSH_REASON_IDLE_TIMEOUT
Felipe Leme1af85ea2019-01-16 13:23:40 -0800150 })
151 @Retention(RetentionPolicy.SOURCE)
Felipe Lemed58c1ea2019-02-21 11:22:45 -0800152 public @interface FlushReason{}
Felipe Leme1af85ea2019-01-16 13:23:40 -0800153
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800154 private final Object mLock = new Object();
155
Felipe Lemebef744c2019-01-03 11:07:25 -0800156 /**
157 * Guard use to ignore events after it's destroyed.
158 */
159 @NonNull
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800160 @GuardedBy("mLock")
161 private boolean mDestroyed;
Felipe Lemebef744c2019-01-03 11:07:25 -0800162
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800163 /** @hide */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800164 @Nullable
Felipe Leme739b98f2019-01-11 12:31:20 -0800165 protected final String mId;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800166
Felipe Leme35ea7632019-01-29 16:13:30 -0800167 private int mState = UNKNOWN_STATE;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800168
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800169 // Lazily created on demand.
170 private ContentCaptureSessionId mContentCaptureSessionId;
171
172 /**
Felipe Leme4eecbe62019-02-11 17:50:17 -0800173 * {@link ContentCaptureContext} set by client, or {@code null} when it's the
174 * {@link ContentCaptureManager#getMainContentCaptureSession() default session} for the
175 * context.
176 */
177 @Nullable
178 private ContentCaptureContext mClientContext;
179
180 /**
Felipe Leme87a9dc92018-12-18 14:28:07 -0800181 * List of children session.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800182 */
183 @Nullable
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800184 @GuardedBy("mLock")
Felipe Leme87a9dc92018-12-18 14:28:07 -0800185 private ArrayList<ContentCaptureSession> mChildren;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800186
187 /** @hide */
Felipe Leme87a9dc92018-12-18 14:28:07 -0800188 protected ContentCaptureSession() {
Felipe Leme739b98f2019-01-11 12:31:20 -0800189 this(UUID.randomUUID().toString());
190 }
191
192 /** @hide */
193 @VisibleForTesting
194 public ContentCaptureSession(@NonNull String id) {
195 mId = Preconditions.checkNotNull(id);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800196 }
197
Felipe Leme4eecbe62019-02-11 17:50:17 -0800198 // Used by ChildCOntentCaptureSession
199 ContentCaptureSession(@NonNull ContentCaptureContext initialContext) {
200 this();
201 mClientContext = Preconditions.checkNotNull(initialContext);
202 }
203
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800204 /** @hide */
205 @NonNull
206 abstract MainContentCaptureSession getMainCaptureSession();
207
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800208 /**
209 * Gets the id used to identify this session.
210 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800211 public final ContentCaptureSessionId getContentCaptureSessionId() {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800212 if (mContentCaptureSessionId == null) {
213 mContentCaptureSessionId = new ContentCaptureSessionId(mId);
214 }
215 return mContentCaptureSessionId;
216 }
217
Felipe Leme739b98f2019-01-11 12:31:20 -0800218 /** @hide */
219 @VisibleForTesting
220 public int getIdAsInt() {
221 // TODO(b/121197119): use sessionId instead of hashcode once it's changed to int
222 return mId.hashCode();
223 }
224
Felipe Leme01297692019-01-29 18:16:23 -0800225 /** @hide */
226 @NonNull
227 public String getId() {
228 return mId;
229 }
230
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800231 /**
Felipe Leme87a9dc92018-12-18 14:28:07 -0800232 * Creates a new {@link ContentCaptureSession}.
233 *
234 * <p>See {@link View#setContentCaptureSession(ContentCaptureSession)} for more info.
235 */
236 @NonNull
237 public final ContentCaptureSession createContentCaptureSession(
238 @NonNull ContentCaptureContext context) {
239 final ContentCaptureSession child = newChild(context);
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800240 if (sDebug) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800241 Log.d(TAG, "createContentCaptureSession(" + context + ": parent=" + mId + ", child="
Felipe Leme87a9dc92018-12-18 14:28:07 -0800242 + child.mId);
243 }
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800244 synchronized (mLock) {
245 if (mChildren == null) {
246 mChildren = new ArrayList<>(INITIAL_CHILDREN_CAPACITY);
247 }
248 mChildren.add(child);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800249 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800250 return child;
251 }
252
253 abstract ContentCaptureSession newChild(@NonNull ContentCaptureContext context);
254
255 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800256 * Flushes the buffered events to the service.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800257 */
Felipe Leme1af85ea2019-01-16 13:23:40 -0800258 abstract void flush(@FlushReason int reason);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800259
260 /**
Felipe Leme4eecbe62019-02-11 17:50:17 -0800261 * Sets the {@link ContentCaptureContext} associated with the session.
262 *
263 * <p>Typically used to change the context associated with the default session from an activity.
264 */
265 public final void setContentCaptureContext(@Nullable ContentCaptureContext context) {
266 mClientContext = context;
267 updateContentCaptureContext(context);
268 }
269
270 abstract void updateContentCaptureContext(@Nullable ContentCaptureContext context);
271
272 /**
273 * Gets the {@link ContentCaptureContext} associated with the session.
274 *
275 * @return context set on constructor or by
276 * {@link #setContentCaptureContext(ContentCaptureContext)}, or {@code null} if never
277 * explicitly set.
278 */
279 @Nullable
280 public final ContentCaptureContext getContentCaptureContext() {
281 return mClientContext;
282 }
283
284 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800285 * Destroys this session, flushing out all pending notifications to the service.
286 *
287 * <p>Once destroyed, any new notification will be dropped.
288 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800289 public final void destroy() {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800290 synchronized (mLock) {
291 if (mDestroyed) {
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800292 if (sDebug) Log.d(TAG, "destroy(" + mId + "): already destroyed");
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800293 return;
294 }
295 mDestroyed = true;
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800296
Felipe Leme2057c382019-01-09 10:05:56 -0800297 // TODO(b/111276913): check state (for example, how to handle if it's waiting for remote
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800298 // id) and send it to the cache of batched commands
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800299 if (sVerbose) {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800300 Log.v(TAG, "destroy(): state=" + getStateAsString(mState) + ", mId=" + mId);
301 }
302 // Finish children first
303 if (mChildren != null) {
304 final int numberChildren = mChildren.size();
Felipe Lemed32d8f6f2019-02-15 10:25:33 -0800305 if (sVerbose) Log.v(TAG, "Destroying " + numberChildren + " children first");
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800306 for (int i = 0; i < numberChildren; i++) {
307 final ContentCaptureSession child = mChildren.get(i);
308 try {
309 child.destroy();
310 } catch (Exception e) {
311 Log.w(TAG, "exception destroying child session #" + i + ": " + e);
312 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800313 }
314 }
315 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800316
Felipe Leme87a9dc92018-12-18 14:28:07 -0800317 try {
Felipe Leme1af85ea2019-01-16 13:23:40 -0800318 flush(FLUSH_REASON_SESSION_FINISHED);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800319 } finally {
320 onDestroy();
321 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800322 }
323
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800324 abstract void onDestroy();
325
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800326 /** @hide */
327 @Override
328 public void close() {
329 destroy();
330 }
331
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800332 /**
333 * Notifies the Content Capture Service that a node has been added to the view structure.
334 *
335 * <p>Typically called "manually" by views that handle their own virtual view hierarchy, or
336 * automatically by the Android System for views that return {@code true} on
337 * {@link View#onProvideContentCaptureStructure(ViewStructure, int)}.
338 *
339 * @param node node that has been added.
340 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800341 public final void notifyViewAppeared(@NonNull ViewStructure node) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800342 Preconditions.checkNotNull(node);
343 if (!isContentCaptureEnabled()) return;
344
345 if (!(node instanceof ViewNode.ViewStructureImpl)) {
346 throw new IllegalArgumentException("Invalid node class: " + node.getClass());
347 }
348
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800349 internalNotifyViewAppeared((ViewStructureImpl) node);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800350 }
351
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800352 abstract void internalNotifyViewAppeared(@NonNull ViewNode.ViewStructureImpl node);
353
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800354 /**
355 * Notifies the Content Capture Service that a node has been removed from the view structure.
356 *
357 * <p>Typically called "manually" by views that handle their own virtual view hierarchy, or
358 * automatically by the Android System for standard views.
359 *
360 * @param id id of the node that has been removed.
361 */
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800362 public final void notifyViewDisappeared(@NonNull AutofillId id) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800363 Preconditions.checkNotNull(id);
364 if (!isContentCaptureEnabled()) return;
365
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800366 internalNotifyViewDisappeared(id);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800367 }
368
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800369 abstract void internalNotifyViewDisappeared(@NonNull AutofillId id);
370
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800371 /**
Felipe Lemeb53afba2019-01-16 15:47:18 -0800372 * Notifies the Content Capture Service that many nodes has been removed from a virtual view
373 * structure.
374 *
375 * <p>Should only be called by views that handle their own virtual view hierarchy.
376 *
377 * @param hostId id of the view hosting the virtual hierarchy.
378 * @param virtualIds ids of the virtual children.
379 *
380 * @throws IllegalArgumentException if the {@code hostId} is an autofill id for a virtual view.
381 * @throws IllegalArgumentException if {@code virtualIds} is empty
382 */
383 public final void notifyViewsDisappeared(@NonNull AutofillId hostId,
Felipe Lemea75333c2019-01-22 15:45:32 -0800384 @NonNull long[] virtualIds) {
385 Preconditions.checkArgument(hostId.isNonVirtual(), "parent cannot be virtual");
Felipe Lemeb53afba2019-01-16 15:47:18 -0800386 Preconditions.checkArgument(!ArrayUtils.isEmpty(virtualIds), "virtual ids cannot be empty");
387 if (!isContentCaptureEnabled()) return;
388
Felipe Leme48f363c2019-01-17 10:53:46 -0800389 // TODO(b/123036895): use a internalNotifyViewsDisappeared that optimizes how the event is
390 // parcelized
Felipe Lemea75333c2019-01-22 15:45:32 -0800391 for (long id : virtualIds) {
Felipe Lemeb53afba2019-01-16 15:47:18 -0800392 internalNotifyViewDisappeared(new AutofillId(hostId, id, getIdAsInt()));
393 }
394 }
395
396 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800397 * Notifies the Intelligence Service that the value of a text node has been changed.
398 *
399 * @param id of the node.
400 * @param text new text.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800401 */
Felipe Leme35ea7632019-01-29 16:13:30 -0800402 public final void notifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800403 Preconditions.checkNotNull(id);
404
405 if (!isContentCaptureEnabled()) return;
406
Felipe Leme21e8dcb2019-01-18 09:09:45 -0800407 internalNotifyViewTextChanged(id, text);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800408 }
409
Felipe Leme21e8dcb2019-01-18 09:09:45 -0800410 abstract void internalNotifyViewTextChanged(@NonNull AutofillId id,
411 @Nullable CharSequence text);
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800412
Felipe Leme01297692019-01-29 18:16:23 -0800413 /** @hide */
Felipe Lemeb0da18f2019-02-22 15:10:02 -0800414 public abstract void internalNotifyViewTreeEvent(boolean started);
Felipe Leme01297692019-01-29 18:16:23 -0800415
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800416 /**
417 * Creates a {@link ViewStructure} for a "standard" view.
418 *
419 * @hide
420 */
421 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800422 public final ViewStructure newViewStructure(@NonNull View view) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800423 return new ViewNode.ViewStructureImpl(view);
424 }
425
426 /**
Felipe Leme2057c382019-01-09 10:05:56 -0800427 * Creates a new {@link AutofillId} for a virtual child, so it can be used to uniquely identify
428 * the children in the session.
429 *
430 * @param parentId id of the virtual view parent (it can be obtained by calling
431 * {@link ViewStructure#getAutofillId()} on the parent).
432 * @param virtualChildId id of the virtual child, relative to the parent.
433 *
434 * @return if for the virtual child
435 *
436 * @throws IllegalArgumentException if the {@code parentId} is a virtual child id.
437 */
Felipe Lemea75333c2019-01-22 15:45:32 -0800438 public @NonNull AutofillId newAutofillId(@NonNull AutofillId parentId, long virtualChildId) {
Felipe Leme2057c382019-01-09 10:05:56 -0800439 Preconditions.checkNotNull(parentId);
Felipe Lemea75333c2019-01-22 15:45:32 -0800440 Preconditions.checkArgument(parentId.isNonVirtual(), "virtual ids cannot have children");
Felipe Leme739b98f2019-01-11 12:31:20 -0800441 return new AutofillId(parentId, virtualChildId, getIdAsInt());
Felipe Leme2057c382019-01-09 10:05:56 -0800442 }
443
444 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800445 * Creates a {@link ViewStructure} for a "virtual" view, so it can be passed to
446 * {@link #notifyViewAppeared(ViewStructure)} by the view managing the virtual view hierarchy.
447 *
448 * @param parentId id of the virtual view parent (it can be obtained by calling
449 * {@link ViewStructure#getAutofillId()} on the parent).
450 * @param virtualId id of the virtual child, relative to the parent.
451 *
452 * @return a new {@link ViewStructure} that can be used for Content Capture purposes.
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800453 */
454 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800455 public final ViewStructure newVirtualViewStructure(@NonNull AutofillId parentId,
Felipe Lemea75333c2019-01-22 15:45:32 -0800456 long virtualId) {
Felipe Leme739b98f2019-01-11 12:31:20 -0800457 return new ViewNode.ViewStructureImpl(parentId, virtualId, getIdAsInt());
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800458 }
459
Felipe Lemebef744c2019-01-03 11:07:25 -0800460 boolean isContentCaptureEnabled() {
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800461 synchronized (mLock) {
462 return !mDestroyed;
463 }
Felipe Lemebef744c2019-01-03 11:07:25 -0800464 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800465
Felipe Leme87a9dc92018-12-18 14:28:07 -0800466 @CallSuper
467 void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
Felipe Lemeaf1a0e72019-01-03 11:07:25 -0800468 pw.print(prefix); pw.print("id: "); pw.println(mId);
Felipe Leme4eecbe62019-02-11 17:50:17 -0800469 if (mClientContext != null) {
470 pw.print(prefix); mClientContext.dump(pw); pw.println();
471 }
Felipe Leme4bc0f6b2019-01-03 19:01:07 -0800472 synchronized (mLock) {
473 pw.print(prefix); pw.print("destroyed: "); pw.println(mDestroyed);
474 if (mChildren != null && !mChildren.isEmpty()) {
475 final String prefix2 = prefix + " ";
476 final int numberChildren = mChildren.size();
477 pw.print(prefix); pw.print("number children: "); pw.println(numberChildren);
478 for (int i = 0; i < numberChildren; i++) {
479 final ContentCaptureSession child = mChildren.get(i);
480 pw.print(prefix); pw.print(i); pw.println(": "); child.dump(prefix2, pw);
481 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800482 }
483 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800484 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800485
486 @Override
487 public String toString() {
488 return mId;
489 }
490
Felipe Leme1af85ea2019-01-16 13:23:40 -0800491 /** @hide */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800492 @NonNull
Felipe Lemeb63e0dd2018-12-18 11:56:42 -0800493 protected static String getStateAsString(int state) {
Felipe Leme35ea7632019-01-29 16:13:30 -0800494 return state + " (" + (state == UNKNOWN_STATE ? "UNKNOWN"
Felipe Leme01b87492019-01-15 13:26:52 -0800495 : DebugUtils.flagsToString(ContentCaptureSession.class, "STATE_", state)) + ")";
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800496 }
Felipe Leme1af85ea2019-01-16 13:23:40 -0800497
498 /** @hide */
499 @NonNull
Felipe Lemed58c1ea2019-02-21 11:22:45 -0800500 public static String getFlushReasonAsString(@FlushReason int reason) {
Felipe Leme1af85ea2019-01-16 13:23:40 -0800501 switch (reason) {
502 case FLUSH_REASON_FULL:
503 return "FULL";
Felipe Lemeb0da18f2019-02-22 15:10:02 -0800504 case FLUSH_REASON_VIEW_ROOT_ENTERED:
505 return "VIEW_ROOT";
Felipe Leme1af85ea2019-01-16 13:23:40 -0800506 case FLUSH_REASON_SESSION_STARTED:
507 return "STARTED";
508 case FLUSH_REASON_SESSION_FINISHED:
509 return "FINISHED";
510 case FLUSH_REASON_IDLE_TIMEOUT:
511 return "IDLE";
512 default:
513 return "UNKOWN-" + reason;
514 }
515 }
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800516}