Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -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 | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 18 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_CONTEXT_UPDATED; |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 19 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_INITIAL_VIEW_TREE_APPEARED; |
| 20 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_INITIAL_VIEW_TREE_APPEARING; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 21 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_FINISHED; |
| 22 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_STARTED; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 23 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_APPEARED; |
| 24 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_DISAPPEARED; |
| 25 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_TEXT_CHANGED; |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 26 | import static android.view.contentcapture.ContentCaptureHelper.DEBUG; |
| 27 | import static android.view.contentcapture.ContentCaptureHelper.VERBOSE; |
| 28 | import static android.view.contentcapture.ContentCaptureHelper.getSanitizedString; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 29 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 30 | import android.annotation.NonNull; |
| 31 | import android.annotation.Nullable; |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 32 | import android.annotation.UiThread; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 33 | import android.content.ComponentName; |
| 34 | import android.content.Context; |
| 35 | import android.content.pm.ParceledListSlice; |
| 36 | import android.os.Bundle; |
| 37 | import android.os.Handler; |
| 38 | import android.os.IBinder; |
| 39 | import android.os.IBinder.DeathRecipient; |
| 40 | import android.os.RemoteException; |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 41 | import android.util.LocalLog; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 42 | import android.util.Log; |
| 43 | import android.util.TimeUtils; |
| 44 | import android.view.autofill.AutofillId; |
| 45 | import android.view.contentcapture.ViewNode.ViewStructureImpl; |
| 46 | |
| 47 | import com.android.internal.os.IResultReceiver; |
| 48 | |
| 49 | import java.io.PrintWriter; |
| 50 | import java.util.ArrayList; |
| 51 | import java.util.Collections; |
| 52 | import java.util.List; |
| 53 | import java.util.concurrent.atomic.AtomicBoolean; |
| 54 | |
| 55 | /** |
| 56 | * Main session associated with a context. |
| 57 | * |
| 58 | * <p>This session is created when the activity starts and finished when it stops; clients can use |
| 59 | * it to create children activities. |
| 60 | * |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 61 | * @hide |
| 62 | */ |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 63 | public final class MainContentCaptureSession extends ContentCaptureSession { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 64 | |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 65 | private static final String TAG = MainContentCaptureSession.class.getSimpleName(); |
| 66 | |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 67 | private static final boolean FORCE_FLUSH = true; |
| 68 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Handler message used to flush the buffer. |
| 71 | */ |
| 72 | private static final int MSG_FLUSH = 1; |
| 73 | |
| 74 | /** |
| 75 | * Maximum number of events that are buffered before sent to the app. |
| 76 | */ |
| 77 | // TODO(b/121044064): use settings |
| 78 | private static final int MAX_BUFFER_SIZE = 100; |
| 79 | |
| 80 | /** |
| 81 | * Frequency the buffer is flushed if stale. |
| 82 | */ |
| 83 | // TODO(b/121044064): use settings |
| 84 | private static final int FLUSHING_FREQUENCY_MS = 5_000; |
| 85 | |
| 86 | /** |
| 87 | * Name of the {@link IResultReceiver} extra used to pass the binder interface to the service. |
| 88 | * @hide |
| 89 | */ |
| 90 | public static final String EXTRA_BINDER = "binder"; |
| 91 | |
| 92 | @NonNull |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 93 | private final AtomicBoolean mDisabled = new AtomicBoolean(false); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 94 | |
| 95 | @NonNull |
| 96 | private final Context mContext; |
| 97 | |
| 98 | @NonNull |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 99 | private final ContentCaptureManager mManager; |
| 100 | |
| 101 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 102 | private final Handler mHandler; |
| 103 | |
| 104 | /** |
| 105 | * Interface to the system_server binder object - it's only used to start the session (and |
| 106 | * notify when the session is finished). |
| 107 | */ |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 108 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 109 | private final IContentCaptureManager mSystemServerInterface; |
| 110 | |
| 111 | /** |
| 112 | * Direct interface to the service binder object - it's used to send the events, including the |
| 113 | * last ones (when the session is finished) |
| 114 | */ |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 115 | @NonNull |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 116 | private IContentCaptureDirectManager mDirectServiceInterface; |
| 117 | @Nullable |
| 118 | private DeathRecipient mDirectServiceVulture; |
| 119 | |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 120 | private int mState = UNKNOWN_STATE; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 121 | |
| 122 | @Nullable |
| 123 | private IBinder mApplicationToken; |
| 124 | |
| 125 | @Nullable |
| 126 | private ComponentName mComponentName; |
| 127 | |
| 128 | /** |
| 129 | * List of events held to be sent as a batch. |
| 130 | */ |
| 131 | @Nullable |
| 132 | private ArrayList<ContentCaptureEvent> mEvents; |
| 133 | |
| 134 | // Used just for debugging purposes (on dump) |
| 135 | private long mNextFlush; |
| 136 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 137 | // TODO(b/121044064): use settings to set size |
| 138 | private final LocalLog mFlushHistory = new LocalLog(10); |
| 139 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 140 | /** @hide */ |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 141 | protected MainContentCaptureSession(@NonNull Context context, |
| 142 | @NonNull ContentCaptureManager manager, @NonNull Handler handler, |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 143 | @NonNull IContentCaptureManager systemServerInterface) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 144 | mContext = context; |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 145 | mManager = manager; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 146 | mHandler = handler; |
| 147 | mSystemServerInterface = systemServerInterface; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 150 | @Override |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 151 | MainContentCaptureSession getMainCaptureSession() { |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | @Override |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 156 | ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) { |
| 157 | final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext); |
| 158 | notifyChildSessionStarted(mId, child.mId, clientContext); |
| 159 | return child; |
| 160 | } |
| 161 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Starts this session. |
| 164 | * |
| 165 | * @hide |
| 166 | */ |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 167 | @UiThread |
| 168 | void start(@NonNull IBinder token, @NonNull ComponentName component, |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 169 | int flags) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 170 | if (!isContentCaptureEnabled()) return; |
| 171 | |
| 172 | if (VERBOSE) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 173 | Log.v(TAG, "start(): token=" + token + ", comp=" |
| 174 | + ComponentName.flattenToShortString(component)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 177 | if (hasStarted()) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 178 | // TODO(b/122959591): make sure this is expected (and when), or use Log.w |
| 179 | if (DEBUG) { |
| 180 | Log.d(TAG, "ignoring handleStartSession(" + token + "/" |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 181 | + ComponentName.flattenToShortString(component) + " while on state " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 182 | + getStateAsString(mState)); |
| 183 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | mState = STATE_WAITING_FOR_SERVER; |
| 187 | mApplicationToken = token; |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 188 | mComponentName = component; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 189 | |
| 190 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 191 | Log.v(TAG, "handleStartSession(): token=" + token + ", act=" |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 192 | + getDebugState() + ", id=" + mId); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 193 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 194 | |
| 195 | try { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 196 | mSystemServerInterface.startSession(mApplicationToken, component, mId, flags, |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame] | 197 | new IResultReceiver.Stub() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 198 | @Override |
| 199 | public void send(int resultCode, Bundle resultData) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 200 | final IBinder binder; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 201 | if (resultData != null) { |
| 202 | binder = resultData.getBinder(EXTRA_BINDER); |
| 203 | if (binder == null) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 204 | Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result"); |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 205 | mHandler.post(() -> resetSession( |
| 206 | STATE_DISABLED | STATE_INTERNAL_ERROR)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 207 | return; |
| 208 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 209 | } else { |
| 210 | binder = null; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 211 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 212 | mHandler.post(() -> onSessionStarted(resultCode, binder)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 213 | } |
| 214 | }); |
| 215 | } catch (RemoteException e) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 216 | Log.w(TAG, "Error starting session for " + component.flattenToShortString() + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 220 | @Override |
| 221 | void onDestroy() { |
| 222 | mHandler.removeMessages(MSG_FLUSH); |
| 223 | mHandler.post(() -> destroySession()); |
| 224 | } |
| 225 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 226 | /** |
| 227 | * Callback from {@code system_server} after call to |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 228 | * {@link IContentCaptureManager#startSession(IBinder, ComponentName, String, int, |
| 229 | * IResultReceiver)} |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 230 | * |
| 231 | * @param resultCode session state |
| 232 | * @param binder handle to {@code IContentCaptureDirectManager} |
| 233 | */ |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 234 | @UiThread |
| 235 | private void onSessionStarted(int resultCode, @Nullable IBinder binder) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 236 | if (binder != null) { |
| 237 | mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder); |
| 238 | mDirectServiceVulture = () -> { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 239 | Log.w(TAG, "Destroying session " + mId + " because service died"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 240 | destroy(); |
| 241 | }; |
| 242 | try { |
| 243 | binder.linkToDeath(mDirectServiceVulture, 0); |
| 244 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 245 | Log.w(TAG, "Failed to link to death on " + binder + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 248 | |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 249 | if ((resultCode & STATE_DISABLED) != 0) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 250 | resetSession(resultCode); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 251 | } else { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 252 | mState = resultCode; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 253 | mDisabled.set(false); |
| 254 | } |
| 255 | if (VERBOSE) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 256 | Log.v(TAG, "handleSessionStarted() result: id=" + mId + " resultCode=" + resultCode |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 257 | + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get() |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 258 | + ", binder=" + binder + ", events=" + (mEvents == null ? 0 : mEvents.size())); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 262 | @UiThread |
| 263 | private void sendEvent(@NonNull ContentCaptureEvent event) { |
| 264 | sendEvent(event, /* forceFlush= */ false); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 267 | @UiThread |
| 268 | private void sendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 269 | final int eventType = event.getType(); |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 270 | if (VERBOSE) Log.v(TAG, "handleSendEvent(" + getDebugState() + "): " + event); |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 271 | if (!hasStarted() && eventType != ContentCaptureEvent.TYPE_SESSION_STARTED |
| 272 | && eventType != ContentCaptureEvent.TYPE_CONTEXT_UPDATED) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 273 | // TODO(b/120494182): comment when this could happen (dialogs?) |
| 274 | Log.v(TAG, "handleSendEvent(" + getDebugState() + ", " |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 275 | + ContentCaptureEvent.getTypeAsString(eventType) |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 276 | + "): dropping because session not started yet"); |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 277 | return; |
| 278 | } |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 279 | if (mDisabled.get()) { |
| 280 | // This happens when the event was queued in the handler before the sesison was ready, |
| 281 | // then handleSessionStarted() returned and set it as disabled - we need to drop it, |
| 282 | // otherwise it will keep triggering handleScheduleFlush() |
| 283 | if (VERBOSE) Log.v(TAG, "handleSendEvent(): ignoring when disabled"); |
| 284 | return; |
| 285 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 286 | if (mEvents == null) { |
| 287 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 288 | Log.v(TAG, "handleSendEvent(): creating buffer for " + MAX_BUFFER_SIZE + " events"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 289 | } |
| 290 | mEvents = new ArrayList<>(MAX_BUFFER_SIZE); |
| 291 | } |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 292 | |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 293 | // Some type of events can be merged together |
| 294 | boolean addEvent = true; |
| 295 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 296 | if (!mEvents.isEmpty() && eventType == TYPE_VIEW_TEXT_CHANGED) { |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 297 | final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 298 | |
| 299 | // TODO(b/121045053): check if flags match |
| 300 | if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED |
| 301 | && lastEvent.getId().equals(event.getId())) { |
| 302 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 303 | Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text=" |
| 304 | + getSanitizedString(event.getText())); |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 305 | } |
Felipe Leme | c8875e7 | 2019-02-08 09:45:34 -0800 | [diff] [blame] | 306 | // TODO(b/124107816): should call lastEvent.merge(event) instead |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 307 | lastEvent.setText(event.getText()); |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 308 | addEvent = false; |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 309 | } |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (!mEvents.isEmpty() && eventType == TYPE_VIEW_DISAPPEARED) { |
| 313 | final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 314 | if (lastEvent.getType() == TYPE_VIEW_DISAPPEARED |
| 315 | && event.getSessionId().equals(lastEvent.getSessionId())) { |
| 316 | if (VERBOSE) { |
| 317 | Log.v(TAG, "Buffering TYPE_VIEW_DISAPPEARED events for session " |
| 318 | + lastEvent.getSessionId()); |
| 319 | } |
Felipe Leme | c8875e7 | 2019-02-08 09:45:34 -0800 | [diff] [blame] | 320 | mergeViewsDisappearedEvent(lastEvent, event); |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 321 | addEvent = false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (addEvent) { |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 326 | mEvents.add(event); |
| 327 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 328 | |
| 329 | final int numberEvents = mEvents.size(); |
| 330 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 331 | final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE; |
| 332 | |
| 333 | if (bufferEvent && !forceFlush) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 334 | scheduleFlush(FLUSH_REASON_IDLE_TIMEOUT, /* checkExisting= */ true); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 338 | if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 339 | // Callback from startSession hasn't been called yet - typically happens on system |
| 340 | // apps that are started before the system service |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 341 | // TODO(b/122959591): try to ignore session while system is not ready / boot |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 342 | // not complete instead. Similarly, the manager service should return right away |
| 343 | // when the user does not have a service set |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 344 | if (DEBUG) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 345 | Log.d(TAG, "Closing session for " + getDebugState() |
| 346 | + " after " + numberEvents + " delayed events"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 347 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 348 | resetSession(STATE_DISABLED | STATE_NO_RESPONSE); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 349 | // TODO(b/111276913): blacklist activity / use special flag to indicate that |
| 350 | // when it's launched again |
| 351 | return; |
| 352 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 353 | final int flushReason; |
| 354 | switch (eventType) { |
| 355 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
| 356 | flushReason = FLUSH_REASON_SESSION_STARTED; |
| 357 | break; |
| 358 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
| 359 | flushReason = FLUSH_REASON_SESSION_FINISHED; |
| 360 | break; |
| 361 | default: |
| 362 | flushReason = FLUSH_REASON_FULL; |
| 363 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 364 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 365 | flush(flushReason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Felipe Leme | c8875e7 | 2019-02-08 09:45:34 -0800 | [diff] [blame] | 368 | // TODO(b/124107816): should be ContentCaptureEvent Event.merge(event) instead (which would |
| 369 | // replace the addAutofillId() method - we would also need unit tests on ContentCaptureEventTest |
| 370 | // to check these scenarios) |
| 371 | private void mergeViewsDisappearedEvent(@NonNull ContentCaptureEvent lastEvent, |
| 372 | @NonNull ContentCaptureEvent event) { |
| 373 | final List<AutofillId> ids = event.getIds(); |
| 374 | final AutofillId id = event.getId(); |
| 375 | if (ids != null) { |
| 376 | if (id != null) { |
| 377 | Log.w(TAG, "got TYPE_VIEW_DISAPPEARED event with both id and ids: " + event); |
| 378 | } |
| 379 | for (int i = 0; i < ids.size(); i++) { |
| 380 | lastEvent.addAutofillId(ids.get(i)); |
| 381 | } |
| 382 | return; |
| 383 | } |
| 384 | if (id != null) { |
| 385 | lastEvent.addAutofillId(id); |
| 386 | return; |
| 387 | } |
| 388 | throw new IllegalArgumentException( |
| 389 | "got TYPE_VIEW_DISAPPEARED event with neither id or ids: " + event); |
| 390 | } |
| 391 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 392 | @UiThread |
| 393 | private boolean hasStarted() { |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 394 | return mState != UNKNOWN_STATE; |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 397 | @UiThread |
| 398 | private void scheduleFlush(@FlushReason int reason, boolean checkExisting) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 399 | if (VERBOSE) { |
| 400 | Log.v(TAG, "handleScheduleFlush(" + getDebugState(reason) |
| 401 | + ", checkExisting=" + checkExisting); |
| 402 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 403 | if (!hasStarted()) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 404 | if (VERBOSE) Log.v(TAG, "handleScheduleFlush(): session not started yet"); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | if (mDisabled.get()) { |
| 409 | // Should not be called on this state, as handleSendEvent checks. |
| 410 | // But we rather add one if check and log than re-schedule and keep the session alive... |
| 411 | Log.e(TAG, "handleScheduleFlush(" + getDebugState(reason) + "): should not be called " |
| 412 | + "when disabled. events=" + (mEvents == null ? null : mEvents.size())); |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 413 | return; |
| 414 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 415 | if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) { |
| 416 | // "Renew" the flush message by removing the previous one |
| 417 | mHandler.removeMessages(MSG_FLUSH); |
| 418 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 419 | mNextFlush = System.currentTimeMillis() + FLUSHING_FREQUENCY_MS; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 420 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 421 | Log.v(TAG, "handleScheduleFlush(): scheduled to flush in " |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 422 | + FLUSHING_FREQUENCY_MS + "ms: " + TimeUtils.logTimeOfDay(mNextFlush)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 423 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 424 | // Post using a Runnable directly to trim a few μs from PooledLambda.obtainMessage() |
| 425 | mHandler.postDelayed(() -> flushIfNeeded(reason), MSG_FLUSH, FLUSHING_FREQUENCY_MS); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 428 | @UiThread |
| 429 | private void flushIfNeeded(@FlushReason int reason) { |
Felipe Leme | 3d570a4 | 2019-01-28 13:04:34 -0800 | [diff] [blame] | 430 | if (mEvents == null || mEvents.isEmpty()) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 431 | if (VERBOSE) Log.v(TAG, "Nothing to flush"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 432 | return; |
| 433 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 434 | flush(reason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 437 | @Override |
| 438 | @UiThread |
| 439 | void flush(@FlushReason int reason) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 440 | if (mEvents == null) return; |
| 441 | |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 442 | if (mDisabled.get()) { |
| 443 | Log.e(TAG, "handleForceFlush(" + getDebugState(reason) + "): should not be when " |
| 444 | + "disabled"); |
| 445 | return; |
| 446 | } |
| 447 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 448 | if (mDirectServiceInterface == null) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 449 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 450 | Log.v(TAG, "handleForceFlush(" + getDebugState(reason) + "): hold your horses, " |
| 451 | + "client not ready: " + mEvents); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 452 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 453 | if (!mHandler.hasMessages(MSG_FLUSH)) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 454 | scheduleFlush(reason, /* checkExisting= */ false); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 455 | } |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | final int numberEvents = mEvents.size(); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 460 | final String reasonString = getflushReasonAsString(reason); |
| 461 | if (DEBUG) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 462 | Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getDebugState(reason)); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 463 | } |
| 464 | // Logs reason, size, max size, idle timeout |
| 465 | final String logRecord = "r=" + reasonString + " s=" + numberEvents |
| 466 | + " m=" + MAX_BUFFER_SIZE + " i=" + FLUSHING_FREQUENCY_MS; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 467 | try { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 468 | mFlushHistory.log(logRecord); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 469 | mHandler.removeMessages(MSG_FLUSH); |
| 470 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 471 | final ParceledListSlice<ContentCaptureEvent> events = clearEvents(); |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 472 | mDirectServiceInterface.sendEvents(events); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 473 | } catch (RemoteException e) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 474 | Log.w(TAG, "Error sending " + numberEvents + " for " + getDebugState() |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 475 | + ": " + e); |
| 476 | } |
| 477 | } |
| 478 | |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 479 | @Override |
| 480 | public void updateContentCaptureContext(@Nullable ContentCaptureContext context) { |
| 481 | notifyContextUpdated(mId, context); |
| 482 | } |
| 483 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 484 | /** |
| 485 | * Resets the buffer and return a {@link ParceledListSlice} with the previous events. |
| 486 | */ |
| 487 | @NonNull |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 488 | @UiThread |
| 489 | private ParceledListSlice<ContentCaptureEvent> clearEvents() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 490 | // NOTE: we must save a reference to the current mEvents and then set it to to null, |
| 491 | // otherwise clearing it would clear it in the receiving side if the service is also local. |
| 492 | final List<ContentCaptureEvent> events = mEvents == null |
| 493 | ? Collections.emptyList() |
| 494 | : mEvents; |
| 495 | mEvents = null; |
| 496 | return new ParceledListSlice<>(events); |
| 497 | } |
| 498 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 499 | @UiThread |
| 500 | private void destroySession() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 501 | if (DEBUG) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 502 | Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 503 | + (mEvents == null ? 0 : mEvents.size()) + " event(s) for " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 504 | + getDebugState()); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | try { |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame] | 508 | mSystemServerInterface.finishSession(mId); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 509 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 510 | Log.e(TAG, "Error destroying system-service session " + mId + " for " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 511 | + getDebugState() + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 515 | // TODO(b/122454205): once we support multiple sessions, we might need to move some of these |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 516 | // clearings out. |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 517 | @UiThread |
| 518 | private void resetSession(int newState) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 519 | if (VERBOSE) { |
| 520 | Log.v(TAG, "handleResetSession(" + getActivityName() + "): from " |
| 521 | + getStateAsString(mState) + " to " + getStateAsString(newState)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 522 | } |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 523 | mState = newState; |
| 524 | mDisabled.set((newState & STATE_DISABLED) != 0); |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 525 | // TODO(b/122454205): must reset children (which currently is owned by superclass) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 526 | mApplicationToken = null; |
| 527 | mComponentName = null; |
| 528 | mEvents = null; |
| 529 | if (mDirectServiceInterface != null) { |
| 530 | mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0); |
| 531 | } |
| 532 | mDirectServiceInterface = null; |
| 533 | mHandler.removeMessages(MSG_FLUSH); |
| 534 | } |
| 535 | |
| 536 | @Override |
| 537 | void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 538 | notifyViewAppeared(mId, node); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | @Override |
| 542 | void internalNotifyViewDisappeared(@NonNull AutofillId id) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 543 | notifyViewDisappeared(mId, id); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | @Override |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 547 | void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) { |
| 548 | notifyViewTextChanged(mId, id, text); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | @Override |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 552 | public void internalNotifyViewHierarchyEvent(boolean started) { |
| 553 | notifyInitialViewHierarchyEvent(mId, started); |
| 554 | } |
| 555 | |
| 556 | @Override |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 557 | boolean isContentCaptureEnabled() { |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 558 | return super.isContentCaptureEnabled() && mManager.isContentCaptureEnabled(); |
| 559 | } |
| 560 | |
| 561 | // Called by ContentCaptureManager.isContentCaptureEnabled |
| 562 | boolean isDisabled() { |
| 563 | return mDisabled.get(); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 566 | // TODO(b/122454205): refactor "notifyXXXX" methods below to a common "Buffer" object that is |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 567 | // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such |
| 568 | // change should also get get rid of the "internalNotifyXXXX" methods above |
| 569 | void notifyChildSessionStarted(@NonNull String parentSessionId, |
| 570 | @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 571 | sendEvent(new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED) |
| 572 | .setParentSessionId(parentSessionId).setClientContext(clientContext), |
| 573 | FORCE_FLUSH); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void notifyChildSessionFinished(@NonNull String parentSessionId, |
| 577 | @NonNull String childSessionId) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 578 | sendEvent(new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED) |
| 579 | .setParentSessionId(parentSessionId), FORCE_FLUSH); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 583 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED) |
| 584 | .setViewNode(node.mNode)); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 588 | sendEvent( |
| 589 | new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id)); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /** @hide */ |
| 593 | public void notifyViewsDisappeared(@NonNull String sessionId, |
| 594 | @NonNull ArrayList<AutofillId> ids) { |
| 595 | final ContentCaptureEvent event = new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED); |
| 596 | if (ids.size() == 1) { |
| 597 | event.setAutofillId(ids.get(0)); |
| 598 | } else { |
| 599 | event.setAutofillIds(ids); |
| 600 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 601 | sendEvent(event); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id, |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 605 | @Nullable CharSequence text) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 606 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED).setAutofillId(id) |
| 607 | .setText(text)); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void notifyInitialViewHierarchyEvent(@NonNull String sessionId, boolean started) { |
| 611 | if (started) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 612 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_INITIAL_VIEW_TREE_APPEARING)); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 613 | } else { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 614 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_INITIAL_VIEW_TREE_APPEARED), |
| 615 | FORCE_FLUSH); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 616 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 619 | void notifyContextUpdated(@NonNull String sessionId, |
| 620 | @Nullable ContentCaptureContext context) { |
| 621 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_CONTEXT_UPDATED) |
| 622 | .setClientContext(context)); |
| 623 | } |
| 624 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 625 | @Override |
| 626 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 627 | super.dump(prefix, pw); |
| 628 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 629 | pw.print(prefix); pw.print("mContext: "); pw.println(mContext); |
| 630 | pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId()); |
Felipe Leme | d49d52c | 2019-02-15 09:48:20 -0800 | [diff] [blame] | 631 | pw.print(prefix); pw.print("mSystemServerInterface: "); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 632 | if (mDirectServiceInterface != null) { |
| 633 | pw.print(prefix); pw.print("mDirectServiceInterface: "); |
| 634 | pw.println(mDirectServiceInterface); |
| 635 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 636 | pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get()); |
| 637 | pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled()); |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 638 | pw.print(prefix); pw.print("state: "); pw.println(getStateAsString(mState)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 639 | if (mApplicationToken != null) { |
| 640 | pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken); |
| 641 | } |
| 642 | if (mComponentName != null) { |
| 643 | pw.print(prefix); pw.print("component name: "); |
| 644 | pw.println(mComponentName.flattenToShortString()); |
| 645 | } |
| 646 | if (mEvents != null && !mEvents.isEmpty()) { |
| 647 | final int numberEvents = mEvents.size(); |
| 648 | pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents); |
| 649 | pw.print('/'); pw.println(MAX_BUFFER_SIZE); |
| 650 | if (VERBOSE && numberEvents > 0) { |
| 651 | final String prefix3 = prefix + " "; |
| 652 | for (int i = 0; i < numberEvents; i++) { |
| 653 | final ContentCaptureEvent event = mEvents.get(i); |
| 654 | pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw); |
| 655 | pw.println(); |
| 656 | } |
| 657 | } |
| 658 | pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS); |
| 659 | pw.print(prefix); pw.print("next flush: "); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 660 | TimeUtils.formatDuration(mNextFlush - System.currentTimeMillis(), pw); |
| 661 | pw.print(" ("); pw.print(TimeUtils.logTimeOfDay(mNextFlush)); pw.println(")"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 662 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 663 | pw.print(prefix); pw.println("flush history:"); |
| 664 | mFlushHistory.reverseDump(/* fd= */ null, pw, /* args= */ null); pw.println(); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Gets a string that can be used to identify the activity on logging statements. |
| 669 | */ |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 670 | private String getActivityName() { |
| 671 | return mComponentName == null |
| 672 | ? "pkg:" + mContext.getPackageName() |
| 673 | : "act:" + mComponentName.flattenToShortString(); |
| 674 | } |
| 675 | |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 676 | @NonNull |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 677 | private String getDebugState() { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 678 | return getActivityName() + " [state=" + getStateAsString(mState) + ", disabled=" |
| 679 | + mDisabled.get() + "]"; |
| 680 | } |
| 681 | |
| 682 | @NonNull |
| 683 | private String getDebugState(@FlushReason int reason) { |
| 684 | return getDebugState() + ", reason=" + getflushReasonAsString(reason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 685 | } |
| 686 | } |