Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.view.contentcapture; |
| 17 | |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 18 | import static android.view.contentcapture.ContentCaptureHelper.sDebug; |
| 19 | import static android.view.contentcapture.ContentCaptureHelper.sVerbose; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 20 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 21 | import android.annotation.CallSuper; |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 22 | import android.annotation.IntDef; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.Nullable; |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 25 | import android.util.DebugUtils; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 26 | import android.util.Log; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewStructure; |
| 29 | import android.view.autofill.AutofillId; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 30 | import android.view.contentcapture.ViewNode.ViewStructureImpl; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 31 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 32 | import com.android.internal.annotations.GuardedBy; |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 33 | import com.android.internal.annotations.VisibleForTesting; |
Felipe Leme | b53afba | 2019-01-16 15:47:18 -0800 | [diff] [blame] | 34 | import com.android.internal.util.ArrayUtils; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 35 | import com.android.internal.util.Preconditions; |
| 36 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 37 | import java.io.PrintWriter; |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 38 | import java.lang.annotation.Retention; |
| 39 | import java.lang.annotation.RetentionPolicy; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 40 | import java.util.ArrayList; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 41 | import java.util.UUID; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Session used to notify a system-provided Content Capture service about events associated with |
| 45 | * views. |
| 46 | */ |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 47 | public abstract class ContentCaptureSession implements AutoCloseable { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 48 | |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 49 | private static final String TAG = ContentCaptureSession.class.getSimpleName(); |
| 50 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 51 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 52 | * Initial state, when there is no session. |
| 53 | * |
| 54 | * @hide |
| 55 | */ |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 56 | // NOTE: not prefixed by STATE_ so it's not printed on getStateAsString() |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 57 | public static final int UNKNOWN_STATE = 0x0; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Service's startSession() was called, but server didn't confirm it was created yet. |
| 61 | * |
| 62 | * @hide |
| 63 | */ |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 64 | public static final int STATE_WAITING_FOR_SERVER = 0x1; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Session is active. |
| 68 | * |
| 69 | * @hide |
| 70 | */ |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 71 | public static final int STATE_ACTIVE = 0x2; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 74 | * Session is disabled because there is no service for this user. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 75 | * |
| 76 | * @hide |
| 77 | */ |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 78 | public static final int STATE_DISABLED = 0x4; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 81 | * Session is disabled because its id already existed on server. |
| 82 | * |
| 83 | * @hide |
| 84 | */ |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 85 | 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 Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 93 | |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Session is disabled by FLAG_SECURE |
| 96 | * |
| 97 | * @hide |
| 98 | */ |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 99 | public static final int STATE_FLAG_SECURE = 0x20; |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 100 | |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 101 | /** |
Felipe Leme | 91ddeca | 2019-01-24 18:01:58 -0800 | [diff] [blame] | 102 | * Session is disabled manually by the specific app |
| 103 | * (through {@link ContentCaptureManager#setContentCaptureEnabled(boolean)}). |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 104 | * |
| 105 | * @hide |
| 106 | */ |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 107 | public static final int STATE_BY_APP = 0x40; |
| 108 | |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 109 | /** |
| 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 He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 122 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 123 | private static final int INITIAL_CHILDREN_CAPACITY = 5; |
| 124 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 125 | /** @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 Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 150 | private final Object mLock = new Object(); |
| 151 | |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 152 | /** |
| 153 | * Guard use to ignore events after it's destroyed. |
| 154 | */ |
| 155 | @NonNull |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 156 | @GuardedBy("mLock") |
| 157 | private boolean mDestroyed; |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 158 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 159 | /** @hide */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 160 | @Nullable |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 161 | protected final String mId; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 162 | |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 163 | private int mState = UNKNOWN_STATE; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 164 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 165 | // Lazily created on demand. |
| 166 | private ContentCaptureSessionId mContentCaptureSessionId; |
| 167 | |
| 168 | /** |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 169 | * {@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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 177 | * List of children session. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 178 | */ |
| 179 | @Nullable |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 180 | @GuardedBy("mLock") |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 181 | private ArrayList<ContentCaptureSession> mChildren; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 182 | |
| 183 | /** @hide */ |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 184 | protected ContentCaptureSession() { |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 185 | this(UUID.randomUUID().toString()); |
| 186 | } |
| 187 | |
| 188 | /** @hide */ |
| 189 | @VisibleForTesting |
| 190 | public ContentCaptureSession(@NonNull String id) { |
| 191 | mId = Preconditions.checkNotNull(id); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 194 | // Used by ChildCOntentCaptureSession |
| 195 | ContentCaptureSession(@NonNull ContentCaptureContext initialContext) { |
| 196 | this(); |
| 197 | mClientContext = Preconditions.checkNotNull(initialContext); |
| 198 | } |
| 199 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 200 | /** @hide */ |
| 201 | @NonNull |
| 202 | abstract MainContentCaptureSession getMainCaptureSession(); |
| 203 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Gets the id used to identify this session. |
| 206 | */ |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 207 | public final ContentCaptureSessionId getContentCaptureSessionId() { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 208 | if (mContentCaptureSessionId == null) { |
| 209 | mContentCaptureSessionId = new ContentCaptureSessionId(mId); |
| 210 | } |
| 211 | return mContentCaptureSessionId; |
| 212 | } |
| 213 | |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 214 | /** @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 Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 221 | /** @hide */ |
| 222 | @NonNull |
| 223 | public String getId() { |
| 224 | return mId; |
| 225 | } |
| 226 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 227 | /** |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 228 | * 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 Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 236 | if (sDebug) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 237 | Log.d(TAG, "createContentCaptureSession(" + context + ": parent=" + mId + ", child=" |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 238 | + child.mId); |
| 239 | } |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 240 | synchronized (mLock) { |
| 241 | if (mChildren == null) { |
| 242 | mChildren = new ArrayList<>(INITIAL_CHILDREN_CAPACITY); |
| 243 | } |
| 244 | mChildren.add(child); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 245 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 246 | return child; |
| 247 | } |
| 248 | |
| 249 | abstract ContentCaptureSession newChild(@NonNull ContentCaptureContext context); |
| 250 | |
| 251 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 252 | * Flushes the buffered events to the service. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 253 | */ |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 254 | abstract void flush(@FlushReason int reason); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 255 | |
| 256 | /** |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 257 | * 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 281 | * Destroys this session, flushing out all pending notifications to the service. |
| 282 | * |
| 283 | * <p>Once destroyed, any new notification will be dropped. |
| 284 | */ |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 285 | public final void destroy() { |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 286 | synchronized (mLock) { |
| 287 | if (mDestroyed) { |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 288 | if (sDebug) Log.d(TAG, "destroy(" + mId + "): already destroyed"); |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 289 | return; |
| 290 | } |
| 291 | mDestroyed = true; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 292 | |
Felipe Leme | 2057c38 | 2019-01-09 10:05:56 -0800 | [diff] [blame] | 293 | // TODO(b/111276913): check state (for example, how to handle if it's waiting for remote |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 294 | // id) and send it to the cache of batched commands |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 295 | if (sVerbose) { |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 296 | 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 Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 301 | if (sVerbose) Log.v(TAG, "Destroying " + numberChildren + " children first"); |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 302 | 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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 312 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 313 | try { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 314 | flush(FLUSH_REASON_SESSION_FINISHED); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 315 | } finally { |
| 316 | onDestroy(); |
| 317 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 320 | abstract void onDestroy(); |
| 321 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 322 | /** @hide */ |
| 323 | @Override |
| 324 | public void close() { |
| 325 | destroy(); |
| 326 | } |
| 327 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 328 | /** |
| 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 Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 337 | public final void notifyViewAppeared(@NonNull ViewStructure node) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 338 | 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 Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 345 | internalNotifyViewAppeared((ViewStructureImpl) node); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 348 | abstract void internalNotifyViewAppeared(@NonNull ViewNode.ViewStructureImpl node); |
| 349 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 350 | /** |
| 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 Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 358 | public final void notifyViewDisappeared(@NonNull AutofillId id) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 359 | Preconditions.checkNotNull(id); |
| 360 | if (!isContentCaptureEnabled()) return; |
| 361 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 362 | internalNotifyViewDisappeared(id); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 365 | abstract void internalNotifyViewDisappeared(@NonNull AutofillId id); |
| 366 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 367 | /** |
Felipe Leme | b53afba | 2019-01-16 15:47:18 -0800 | [diff] [blame] | 368 | * 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 Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 380 | @NonNull long[] virtualIds) { |
| 381 | Preconditions.checkArgument(hostId.isNonVirtual(), "parent cannot be virtual"); |
Felipe Leme | b53afba | 2019-01-16 15:47:18 -0800 | [diff] [blame] | 382 | Preconditions.checkArgument(!ArrayUtils.isEmpty(virtualIds), "virtual ids cannot be empty"); |
| 383 | if (!isContentCaptureEnabled()) return; |
| 384 | |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 385 | // TODO(b/123036895): use a internalNotifyViewsDisappeared that optimizes how the event is |
| 386 | // parcelized |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 387 | for (long id : virtualIds) { |
Felipe Leme | b53afba | 2019-01-16 15:47:18 -0800 | [diff] [blame] | 388 | internalNotifyViewDisappeared(new AutofillId(hostId, id, getIdAsInt())); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 393 | * 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 397 | */ |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 398 | public final void notifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 399 | Preconditions.checkNotNull(id); |
| 400 | |
| 401 | if (!isContentCaptureEnabled()) return; |
| 402 | |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 403 | internalNotifyViewTextChanged(id, text); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 406 | abstract void internalNotifyViewTextChanged(@NonNull AutofillId id, |
| 407 | @Nullable CharSequence text); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 408 | |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 409 | /** @hide */ |
| 410 | public abstract void internalNotifyViewHierarchyEvent(boolean started); |
| 411 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 412 | /** |
| 413 | * Creates a {@link ViewStructure} for a "standard" view. |
| 414 | * |
| 415 | * @hide |
| 416 | */ |
| 417 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 418 | public final ViewStructure newViewStructure(@NonNull View view) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 419 | return new ViewNode.ViewStructureImpl(view); |
| 420 | } |
| 421 | |
| 422 | /** |
Felipe Leme | 2057c38 | 2019-01-09 10:05:56 -0800 | [diff] [blame] | 423 | * 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 Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 434 | public @NonNull AutofillId newAutofillId(@NonNull AutofillId parentId, long virtualChildId) { |
Felipe Leme | 2057c38 | 2019-01-09 10:05:56 -0800 | [diff] [blame] | 435 | Preconditions.checkNotNull(parentId); |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 436 | Preconditions.checkArgument(parentId.isNonVirtual(), "virtual ids cannot have children"); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 437 | return new AutofillId(parentId, virtualChildId, getIdAsInt()); |
Felipe Leme | 2057c38 | 2019-01-09 10:05:56 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 441 | * 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 449 | */ |
| 450 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 451 | public final ViewStructure newVirtualViewStructure(@NonNull AutofillId parentId, |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 452 | long virtualId) { |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 453 | return new ViewNode.ViewStructureImpl(parentId, virtualId, getIdAsInt()); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 456 | boolean isContentCaptureEnabled() { |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 457 | synchronized (mLock) { |
| 458 | return !mDestroyed; |
| 459 | } |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 460 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 461 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 462 | @CallSuper |
| 463 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 464 | pw.print(prefix); pw.print("id: "); pw.println(mId); |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 465 | if (mClientContext != null) { |
| 466 | pw.print(prefix); mClientContext.dump(pw); pw.println(); |
| 467 | } |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 468 | 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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 478 | } |
| 479 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 480 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 481 | |
| 482 | @Override |
| 483 | public String toString() { |
| 484 | return mId; |
| 485 | } |
| 486 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 487 | /** @hide */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 488 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 489 | protected static String getStateAsString(int state) { |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 490 | return state + " (" + (state == UNKNOWN_STATE ? "UNKNOWN" |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 491 | : DebugUtils.flagsToString(ContentCaptureSession.class, "STATE_", state)) + ")"; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 492 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 493 | |
| 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 514 | } |