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 | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame^] | 123 | /** |
| 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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 130 | private static final int INITIAL_CHILDREN_CAPACITY = 5; |
| 131 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 132 | /** @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 Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 157 | private final Object mLock = new Object(); |
| 158 | |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 159 | /** |
| 160 | * Guard use to ignore events after it's destroyed. |
| 161 | */ |
| 162 | @NonNull |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 163 | @GuardedBy("mLock") |
| 164 | private boolean mDestroyed; |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 165 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 166 | /** @hide */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 167 | @Nullable |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 168 | protected final String mId; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 169 | |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 170 | private int mState = UNKNOWN_STATE; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 171 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 172 | // Lazily created on demand. |
| 173 | private ContentCaptureSessionId mContentCaptureSessionId; |
| 174 | |
| 175 | /** |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 176 | * {@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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 184 | * List of children session. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 185 | */ |
| 186 | @Nullable |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 187 | @GuardedBy("mLock") |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 188 | private ArrayList<ContentCaptureSession> mChildren; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 189 | |
| 190 | /** @hide */ |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 191 | protected ContentCaptureSession() { |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 192 | this(UUID.randomUUID().toString()); |
| 193 | } |
| 194 | |
| 195 | /** @hide */ |
| 196 | @VisibleForTesting |
| 197 | public ContentCaptureSession(@NonNull String id) { |
| 198 | mId = Preconditions.checkNotNull(id); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 201 | // Used by ChildCOntentCaptureSession |
| 202 | ContentCaptureSession(@NonNull ContentCaptureContext initialContext) { |
| 203 | this(); |
| 204 | mClientContext = Preconditions.checkNotNull(initialContext); |
| 205 | } |
| 206 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 207 | /** @hide */ |
| 208 | @NonNull |
| 209 | abstract MainContentCaptureSession getMainCaptureSession(); |
| 210 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 211 | /** |
| 212 | * Gets the id used to identify this session. |
| 213 | */ |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 214 | public final ContentCaptureSessionId getContentCaptureSessionId() { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 215 | if (mContentCaptureSessionId == null) { |
| 216 | mContentCaptureSessionId = new ContentCaptureSessionId(mId); |
| 217 | } |
| 218 | return mContentCaptureSessionId; |
| 219 | } |
| 220 | |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 221 | /** @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 Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 228 | /** @hide */ |
| 229 | @NonNull |
| 230 | public String getId() { |
| 231 | return mId; |
| 232 | } |
| 233 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 234 | /** |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 235 | * 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 Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 243 | if (sDebug) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 244 | Log.d(TAG, "createContentCaptureSession(" + context + ": parent=" + mId + ", child=" |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 245 | + child.mId); |
| 246 | } |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 247 | synchronized (mLock) { |
| 248 | if (mChildren == null) { |
| 249 | mChildren = new ArrayList<>(INITIAL_CHILDREN_CAPACITY); |
| 250 | } |
| 251 | mChildren.add(child); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 252 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 253 | return child; |
| 254 | } |
| 255 | |
| 256 | abstract ContentCaptureSession newChild(@NonNull ContentCaptureContext context); |
| 257 | |
| 258 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 259 | * Flushes the buffered events to the service. |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 260 | */ |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 261 | abstract void flush(@FlushReason int reason); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 262 | |
| 263 | /** |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 264 | * 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 288 | * Destroys this session, flushing out all pending notifications to the service. |
| 289 | * |
| 290 | * <p>Once destroyed, any new notification will be dropped. |
| 291 | */ |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 292 | public final void destroy() { |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 293 | synchronized (mLock) { |
| 294 | if (mDestroyed) { |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 295 | if (sDebug) Log.d(TAG, "destroy(" + mId + "): already destroyed"); |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | mDestroyed = true; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 299 | |
Felipe Leme | 2057c38 | 2019-01-09 10:05:56 -0800 | [diff] [blame] | 300 | // 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] | 301 | // id) and send it to the cache of batched commands |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 302 | if (sVerbose) { |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 303 | 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 Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 308 | if (sVerbose) Log.v(TAG, "Destroying " + numberChildren + " children first"); |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 309 | 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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 319 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 320 | try { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 321 | flush(FLUSH_REASON_SESSION_FINISHED); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 322 | } finally { |
| 323 | onDestroy(); |
| 324 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 327 | abstract void onDestroy(); |
| 328 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 329 | /** @hide */ |
| 330 | @Override |
| 331 | public void close() { |
| 332 | destroy(); |
| 333 | } |
| 334 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 335 | /** |
| 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 Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 344 | public final void notifyViewAppeared(@NonNull ViewStructure node) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 345 | 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 Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 352 | internalNotifyViewAppeared((ViewStructureImpl) node); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 355 | abstract void internalNotifyViewAppeared(@NonNull ViewNode.ViewStructureImpl node); |
| 356 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 357 | /** |
| 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 Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 365 | public final void notifyViewDisappeared(@NonNull AutofillId id) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 366 | Preconditions.checkNotNull(id); |
| 367 | if (!isContentCaptureEnabled()) return; |
| 368 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 369 | internalNotifyViewDisappeared(id); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 372 | abstract void internalNotifyViewDisappeared(@NonNull AutofillId id); |
| 373 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 374 | /** |
Felipe Leme | b53afba | 2019-01-16 15:47:18 -0800 | [diff] [blame] | 375 | * 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 Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 387 | @NonNull long[] virtualIds) { |
| 388 | Preconditions.checkArgument(hostId.isNonVirtual(), "parent cannot be virtual"); |
Felipe Leme | b53afba | 2019-01-16 15:47:18 -0800 | [diff] [blame] | 389 | Preconditions.checkArgument(!ArrayUtils.isEmpty(virtualIds), "virtual ids cannot be empty"); |
| 390 | if (!isContentCaptureEnabled()) return; |
| 391 | |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 392 | // TODO(b/123036895): use a internalNotifyViewsDisappeared that optimizes how the event is |
| 393 | // parcelized |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 394 | for (long id : virtualIds) { |
Felipe Leme | b53afba | 2019-01-16 15:47:18 -0800 | [diff] [blame] | 395 | internalNotifyViewDisappeared(new AutofillId(hostId, id, getIdAsInt())); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 400 | * 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 404 | */ |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 405 | public final void notifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 406 | Preconditions.checkNotNull(id); |
| 407 | |
| 408 | if (!isContentCaptureEnabled()) return; |
| 409 | |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 410 | internalNotifyViewTextChanged(id, text); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 413 | abstract void internalNotifyViewTextChanged(@NonNull AutofillId id, |
| 414 | @Nullable CharSequence text); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 415 | |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 416 | /** @hide */ |
| 417 | public abstract void internalNotifyViewHierarchyEvent(boolean started); |
| 418 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 419 | /** |
| 420 | * Creates a {@link ViewStructure} for a "standard" view. |
| 421 | * |
| 422 | * @hide |
| 423 | */ |
| 424 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 425 | public final ViewStructure newViewStructure(@NonNull View view) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 426 | return new ViewNode.ViewStructureImpl(view); |
| 427 | } |
| 428 | |
| 429 | /** |
Felipe Leme | 2057c38 | 2019-01-09 10:05:56 -0800 | [diff] [blame] | 430 | * 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 Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 441 | public @NonNull AutofillId newAutofillId(@NonNull AutofillId parentId, long virtualChildId) { |
Felipe Leme | 2057c38 | 2019-01-09 10:05:56 -0800 | [diff] [blame] | 442 | Preconditions.checkNotNull(parentId); |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 443 | Preconditions.checkArgument(parentId.isNonVirtual(), "virtual ids cannot have children"); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 444 | return new AutofillId(parentId, virtualChildId, getIdAsInt()); |
Felipe Leme | 2057c38 | 2019-01-09 10:05:56 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 448 | * 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 456 | */ |
| 457 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 458 | public final ViewStructure newVirtualViewStructure(@NonNull AutofillId parentId, |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 459 | long virtualId) { |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 460 | return new ViewNode.ViewStructureImpl(parentId, virtualId, getIdAsInt()); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 463 | boolean isContentCaptureEnabled() { |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 464 | synchronized (mLock) { |
| 465 | return !mDestroyed; |
| 466 | } |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 467 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 468 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 469 | @CallSuper |
| 470 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 471 | pw.print(prefix); pw.print("id: "); pw.println(mId); |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 472 | if (mClientContext != null) { |
| 473 | pw.print(prefix); mClientContext.dump(pw); pw.println(); |
| 474 | } |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 475 | 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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 487 | } |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 488 | |
| 489 | @Override |
| 490 | public String toString() { |
| 491 | return mId; |
| 492 | } |
| 493 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 494 | /** @hide */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 495 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 496 | protected static String getStateAsString(int state) { |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 497 | return state + " (" + (state == UNKNOWN_STATE ? "UNKNOWN" |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 498 | : DebugUtils.flagsToString(ContentCaptureSession.class, "STATE_", state)) + ")"; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 499 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 500 | |
| 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 521 | } |