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 | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 108 | @Nullable // TODO(b/122959591): shoul never be null, we should make main session null instead |
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 | */ |
| 115 | @Nullable |
| 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, |
| 143 | @Nullable 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 { |
Adam He | 6884fed | 2019-01-07 13:18:32 -0800 | [diff] [blame] | 196 | if (mSystemServerInterface == null) return; |
| 197 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 198 | mSystemServerInterface.startSession(mApplicationToken, component, mId, flags, |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame] | 199 | new IResultReceiver.Stub() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 200 | @Override |
| 201 | public void send(int resultCode, Bundle resultData) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 202 | final IBinder binder; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 203 | if (resultData != null) { |
| 204 | binder = resultData.getBinder(EXTRA_BINDER); |
| 205 | if (binder == null) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 206 | Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result"); |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 207 | mHandler.post(() -> resetSession( |
| 208 | STATE_DISABLED | STATE_INTERNAL_ERROR)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 209 | return; |
| 210 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 211 | } else { |
| 212 | binder = null; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 213 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 214 | mHandler.post(() -> onSessionStarted(resultCode, binder)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 215 | } |
| 216 | }); |
| 217 | } catch (RemoteException e) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 218 | Log.w(TAG, "Error starting session for " + component.flattenToShortString() + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 222 | @Override |
| 223 | void onDestroy() { |
| 224 | mHandler.removeMessages(MSG_FLUSH); |
| 225 | mHandler.post(() -> destroySession()); |
| 226 | } |
| 227 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 228 | /** |
| 229 | * Callback from {@code system_server} after call to |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 230 | * {@link IContentCaptureManager#startSession(IBinder, ComponentName, String, int, |
| 231 | * IResultReceiver)} |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 232 | * |
| 233 | * @param resultCode session state |
| 234 | * @param binder handle to {@code IContentCaptureDirectManager} |
| 235 | */ |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 236 | @UiThread |
| 237 | private void onSessionStarted(int resultCode, @Nullable IBinder binder) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 238 | if (binder != null) { |
| 239 | mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder); |
| 240 | mDirectServiceVulture = () -> { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 241 | Log.w(TAG, "Destroying session " + mId + " because service died"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 242 | destroy(); |
| 243 | }; |
| 244 | try { |
| 245 | binder.linkToDeath(mDirectServiceVulture, 0); |
| 246 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 247 | Log.w(TAG, "Failed to link to death on " + binder + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 250 | |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 251 | if ((resultCode & STATE_DISABLED) != 0) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 252 | resetSession(resultCode); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 253 | } else { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 254 | mState = resultCode; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 255 | mDisabled.set(false); |
| 256 | } |
| 257 | if (VERBOSE) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 258 | Log.v(TAG, "handleSessionStarted() result: id=" + mId + " resultCode=" + resultCode |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 259 | + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get() |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 260 | + ", binder=" + binder + ", events=" + (mEvents == null ? 0 : mEvents.size())); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 264 | @UiThread |
| 265 | private void sendEvent(@NonNull ContentCaptureEvent event) { |
| 266 | sendEvent(event, /* forceFlush= */ false); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 269 | @UiThread |
| 270 | private void sendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 271 | final int eventType = event.getType(); |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 272 | if (VERBOSE) Log.v(TAG, "handleSendEvent(" + getDebugState() + "): " + event); |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame^] | 273 | if (!hasStarted() && eventType != ContentCaptureEvent.TYPE_SESSION_STARTED |
| 274 | && eventType != ContentCaptureEvent.TYPE_CONTEXT_UPDATED) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 275 | // TODO(b/120494182): comment when this could happen (dialogs?) |
| 276 | Log.v(TAG, "handleSendEvent(" + getDebugState() + ", " |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 277 | + ContentCaptureEvent.getTypeAsString(eventType) |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame^] | 278 | + "): dropping because session not started yet"); |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 279 | return; |
| 280 | } |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 281 | if (mDisabled.get()) { |
| 282 | // This happens when the event was queued in the handler before the sesison was ready, |
| 283 | // then handleSessionStarted() returned and set it as disabled - we need to drop it, |
| 284 | // otherwise it will keep triggering handleScheduleFlush() |
| 285 | if (VERBOSE) Log.v(TAG, "handleSendEvent(): ignoring when disabled"); |
| 286 | return; |
| 287 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 288 | if (mEvents == null) { |
| 289 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 290 | Log.v(TAG, "handleSendEvent(): creating buffer for " + MAX_BUFFER_SIZE + " events"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 291 | } |
| 292 | mEvents = new ArrayList<>(MAX_BUFFER_SIZE); |
| 293 | } |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 294 | |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 295 | // Some type of events can be merged together |
| 296 | boolean addEvent = true; |
| 297 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 298 | if (!mEvents.isEmpty() && eventType == TYPE_VIEW_TEXT_CHANGED) { |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 299 | final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 300 | |
| 301 | // TODO(b/121045053): check if flags match |
| 302 | if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED |
| 303 | && lastEvent.getId().equals(event.getId())) { |
| 304 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 305 | Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text=" |
| 306 | + getSanitizedString(event.getText())); |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 307 | } |
Felipe Leme | c8875e7 | 2019-02-08 09:45:34 -0800 | [diff] [blame] | 308 | // TODO(b/124107816): should call lastEvent.merge(event) instead |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 309 | lastEvent.setText(event.getText()); |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 310 | addEvent = false; |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 311 | } |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (!mEvents.isEmpty() && eventType == TYPE_VIEW_DISAPPEARED) { |
| 315 | final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 316 | if (lastEvent.getType() == TYPE_VIEW_DISAPPEARED |
| 317 | && event.getSessionId().equals(lastEvent.getSessionId())) { |
| 318 | if (VERBOSE) { |
| 319 | Log.v(TAG, "Buffering TYPE_VIEW_DISAPPEARED events for session " |
| 320 | + lastEvent.getSessionId()); |
| 321 | } |
Felipe Leme | c8875e7 | 2019-02-08 09:45:34 -0800 | [diff] [blame] | 322 | mergeViewsDisappearedEvent(lastEvent, event); |
Felipe Leme | 48f363c | 2019-01-17 10:53:46 -0800 | [diff] [blame] | 323 | addEvent = false; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (addEvent) { |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 328 | mEvents.add(event); |
| 329 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 330 | |
| 331 | final int numberEvents = mEvents.size(); |
| 332 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 333 | final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE; |
| 334 | |
| 335 | if (bufferEvent && !forceFlush) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 336 | scheduleFlush(FLUSH_REASON_IDLE_TIMEOUT, /* checkExisting= */ true); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 340 | if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 341 | // Callback from startSession hasn't been called yet - typically happens on system |
| 342 | // apps that are started before the system service |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 343 | // 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] | 344 | // not complete instead. Similarly, the manager service should return right away |
| 345 | // when the user does not have a service set |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 346 | if (DEBUG) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 347 | Log.d(TAG, "Closing session for " + getDebugState() |
| 348 | + " after " + numberEvents + " delayed events"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 349 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 350 | resetSession(STATE_DISABLED | STATE_NO_RESPONSE); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 351 | // TODO(b/111276913): blacklist activity / use special flag to indicate that |
| 352 | // when it's launched again |
| 353 | return; |
| 354 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 355 | final int flushReason; |
| 356 | switch (eventType) { |
| 357 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
| 358 | flushReason = FLUSH_REASON_SESSION_STARTED; |
| 359 | break; |
| 360 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
| 361 | flushReason = FLUSH_REASON_SESSION_FINISHED; |
| 362 | break; |
| 363 | default: |
| 364 | flushReason = FLUSH_REASON_FULL; |
| 365 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 366 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 367 | flush(flushReason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Felipe Leme | c8875e7 | 2019-02-08 09:45:34 -0800 | [diff] [blame] | 370 | // TODO(b/124107816): should be ContentCaptureEvent Event.merge(event) instead (which would |
| 371 | // replace the addAutofillId() method - we would also need unit tests on ContentCaptureEventTest |
| 372 | // to check these scenarios) |
| 373 | private void mergeViewsDisappearedEvent(@NonNull ContentCaptureEvent lastEvent, |
| 374 | @NonNull ContentCaptureEvent event) { |
| 375 | final List<AutofillId> ids = event.getIds(); |
| 376 | final AutofillId id = event.getId(); |
| 377 | if (ids != null) { |
| 378 | if (id != null) { |
| 379 | Log.w(TAG, "got TYPE_VIEW_DISAPPEARED event with both id and ids: " + event); |
| 380 | } |
| 381 | for (int i = 0; i < ids.size(); i++) { |
| 382 | lastEvent.addAutofillId(ids.get(i)); |
| 383 | } |
| 384 | return; |
| 385 | } |
| 386 | if (id != null) { |
| 387 | lastEvent.addAutofillId(id); |
| 388 | return; |
| 389 | } |
| 390 | throw new IllegalArgumentException( |
| 391 | "got TYPE_VIEW_DISAPPEARED event with neither id or ids: " + event); |
| 392 | } |
| 393 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 394 | @UiThread |
| 395 | private boolean hasStarted() { |
Felipe Leme | 35ea763 | 2019-01-29 16:13:30 -0800 | [diff] [blame] | 396 | return mState != UNKNOWN_STATE; |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 399 | @UiThread |
| 400 | private void scheduleFlush(@FlushReason int reason, boolean checkExisting) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 401 | if (VERBOSE) { |
| 402 | Log.v(TAG, "handleScheduleFlush(" + getDebugState(reason) |
| 403 | + ", checkExisting=" + checkExisting); |
| 404 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 405 | if (!hasStarted()) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 406 | if (VERBOSE) Log.v(TAG, "handleScheduleFlush(): session not started yet"); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | if (mDisabled.get()) { |
| 411 | // Should not be called on this state, as handleSendEvent checks. |
| 412 | // But we rather add one if check and log than re-schedule and keep the session alive... |
| 413 | Log.e(TAG, "handleScheduleFlush(" + getDebugState(reason) + "): should not be called " |
| 414 | + "when disabled. events=" + (mEvents == null ? null : mEvents.size())); |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 415 | return; |
| 416 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 417 | if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) { |
| 418 | // "Renew" the flush message by removing the previous one |
| 419 | mHandler.removeMessages(MSG_FLUSH); |
| 420 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 421 | mNextFlush = System.currentTimeMillis() + FLUSHING_FREQUENCY_MS; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 422 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 423 | Log.v(TAG, "handleScheduleFlush(): scheduled to flush in " |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 424 | + FLUSHING_FREQUENCY_MS + "ms: " + TimeUtils.logTimeOfDay(mNextFlush)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 425 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 426 | // Post using a Runnable directly to trim a few μs from PooledLambda.obtainMessage() |
| 427 | mHandler.postDelayed(() -> flushIfNeeded(reason), MSG_FLUSH, FLUSHING_FREQUENCY_MS); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 430 | @UiThread |
| 431 | private void flushIfNeeded(@FlushReason int reason) { |
Felipe Leme | 3d570a4 | 2019-01-28 13:04:34 -0800 | [diff] [blame] | 432 | if (mEvents == null || mEvents.isEmpty()) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 433 | if (VERBOSE) Log.v(TAG, "Nothing to flush"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 434 | return; |
| 435 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 436 | flush(reason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 439 | @Override |
| 440 | @UiThread |
| 441 | void flush(@FlushReason int reason) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 442 | if (mEvents == null) return; |
| 443 | |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 444 | if (mDisabled.get()) { |
| 445 | Log.e(TAG, "handleForceFlush(" + getDebugState(reason) + "): should not be when " |
| 446 | + "disabled"); |
| 447 | return; |
| 448 | } |
| 449 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 450 | if (mDirectServiceInterface == null) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 451 | if (VERBOSE) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 452 | Log.v(TAG, "handleForceFlush(" + getDebugState(reason) + "): hold your horses, " |
| 453 | + "client not ready: " + mEvents); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 454 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 455 | if (!mHandler.hasMessages(MSG_FLUSH)) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 456 | scheduleFlush(reason, /* checkExisting= */ false); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 457 | } |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | final int numberEvents = mEvents.size(); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 462 | final String reasonString = getflushReasonAsString(reason); |
| 463 | if (DEBUG) { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 464 | Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getDebugState(reason)); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 465 | } |
| 466 | // Logs reason, size, max size, idle timeout |
| 467 | final String logRecord = "r=" + reasonString + " s=" + numberEvents |
| 468 | + " m=" + MAX_BUFFER_SIZE + " i=" + FLUSHING_FREQUENCY_MS; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 469 | try { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 470 | mFlushHistory.log(logRecord); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 471 | mHandler.removeMessages(MSG_FLUSH); |
| 472 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 473 | final ParceledListSlice<ContentCaptureEvent> events = clearEvents(); |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 474 | mDirectServiceInterface.sendEvents(events); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 475 | } catch (RemoteException e) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 476 | Log.w(TAG, "Error sending " + numberEvents + " for " + getDebugState() |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 477 | + ": " + e); |
| 478 | } |
| 479 | } |
| 480 | |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame^] | 481 | @Override |
| 482 | public void updateContentCaptureContext(@Nullable ContentCaptureContext context) { |
| 483 | notifyContextUpdated(mId, context); |
| 484 | } |
| 485 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 486 | /** |
| 487 | * Resets the buffer and return a {@link ParceledListSlice} with the previous events. |
| 488 | */ |
| 489 | @NonNull |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 490 | @UiThread |
| 491 | private ParceledListSlice<ContentCaptureEvent> clearEvents() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 492 | // NOTE: we must save a reference to the current mEvents and then set it to to null, |
| 493 | // otherwise clearing it would clear it in the receiving side if the service is also local. |
| 494 | final List<ContentCaptureEvent> events = mEvents == null |
| 495 | ? Collections.emptyList() |
| 496 | : mEvents; |
| 497 | mEvents = null; |
| 498 | return new ParceledListSlice<>(events); |
| 499 | } |
| 500 | |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 501 | @UiThread |
| 502 | private void destroySession() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 503 | if (DEBUG) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 504 | Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 505 | + (mEvents == null ? 0 : mEvents.size()) + " event(s) for " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 506 | + getDebugState()); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | try { |
Adam He | 6884fed | 2019-01-07 13:18:32 -0800 | [diff] [blame] | 510 | if (mSystemServerInterface == null) return; |
| 511 | |
Felipe Leme | f2aa0d2 | 2019-01-28 10:38:46 -0800 | [diff] [blame] | 512 | mSystemServerInterface.finishSession(mId); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 513 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 514 | Log.e(TAG, "Error destroying system-service session " + mId + " for " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 515 | + getDebugState() + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 519 | // 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] | 520 | // clearings out. |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 521 | @UiThread |
| 522 | private void resetSession(int newState) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 523 | if (VERBOSE) { |
| 524 | Log.v(TAG, "handleResetSession(" + getActivityName() + "): from " |
| 525 | + getStateAsString(mState) + " to " + getStateAsString(newState)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 526 | } |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 527 | mState = newState; |
| 528 | mDisabled.set((newState & STATE_DISABLED) != 0); |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 529 | // TODO(b/122454205): must reset children (which currently is owned by superclass) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 530 | mApplicationToken = null; |
| 531 | mComponentName = null; |
| 532 | mEvents = null; |
| 533 | if (mDirectServiceInterface != null) { |
| 534 | mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0); |
| 535 | } |
| 536 | mDirectServiceInterface = null; |
| 537 | mHandler.removeMessages(MSG_FLUSH); |
| 538 | } |
| 539 | |
| 540 | @Override |
| 541 | void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 542 | notifyViewAppeared(mId, node); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Override |
| 546 | void internalNotifyViewDisappeared(@NonNull AutofillId id) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 547 | notifyViewDisappeared(mId, id); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | @Override |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 551 | void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) { |
| 552 | notifyViewTextChanged(mId, id, text); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | @Override |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 556 | public void internalNotifyViewHierarchyEvent(boolean started) { |
| 557 | notifyInitialViewHierarchyEvent(mId, started); |
| 558 | } |
| 559 | |
| 560 | @Override |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 561 | boolean isContentCaptureEnabled() { |
Felipe Leme | 609991d | 2019-01-30 16:27:24 -0800 | [diff] [blame] | 562 | return super.isContentCaptureEnabled() && mManager.isContentCaptureEnabled(); |
| 563 | } |
| 564 | |
| 565 | // Called by ContentCaptureManager.isContentCaptureEnabled |
| 566 | boolean isDisabled() { |
| 567 | return mDisabled.get(); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 570 | // 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] | 571 | // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such |
| 572 | // change should also get get rid of the "internalNotifyXXXX" methods above |
| 573 | void notifyChildSessionStarted(@NonNull String parentSessionId, |
| 574 | @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 575 | sendEvent(new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED) |
| 576 | .setParentSessionId(parentSessionId).setClientContext(clientContext), |
| 577 | FORCE_FLUSH); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | void notifyChildSessionFinished(@NonNull String parentSessionId, |
| 581 | @NonNull String childSessionId) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 582 | sendEvent(new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED) |
| 583 | .setParentSessionId(parentSessionId), FORCE_FLUSH); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 587 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED) |
| 588 | .setViewNode(node.mNode)); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 592 | sendEvent( |
| 593 | new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id)); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** @hide */ |
| 597 | public void notifyViewsDisappeared(@NonNull String sessionId, |
| 598 | @NonNull ArrayList<AutofillId> ids) { |
| 599 | final ContentCaptureEvent event = new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED); |
| 600 | if (ids.size() == 1) { |
| 601 | event.setAutofillId(ids.get(0)); |
| 602 | } else { |
| 603 | event.setAutofillIds(ids); |
| 604 | } |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 605 | sendEvent(event); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id, |
Felipe Leme | 21e8dcb | 2019-01-18 09:09:45 -0800 | [diff] [blame] | 609 | @Nullable CharSequence text) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 610 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED).setAutofillId(id) |
| 611 | .setText(text)); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void notifyInitialViewHierarchyEvent(@NonNull String sessionId, boolean started) { |
| 615 | if (started) { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 616 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_INITIAL_VIEW_TREE_APPEARING)); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 617 | } else { |
Felipe Leme | 3fe6e92 | 2019-02-04 17:52:27 -0800 | [diff] [blame] | 618 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_INITIAL_VIEW_TREE_APPEARED), |
| 619 | FORCE_FLUSH); |
Felipe Leme | 0129769 | 2019-01-29 18:16:23 -0800 | [diff] [blame] | 620 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame^] | 623 | void notifyContextUpdated(@NonNull String sessionId, |
| 624 | @Nullable ContentCaptureContext context) { |
| 625 | sendEvent(new ContentCaptureEvent(sessionId, TYPE_CONTEXT_UPDATED) |
| 626 | .setClientContext(context)); |
| 627 | } |
| 628 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 629 | @Override |
| 630 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 631 | pw.print(prefix); pw.print("mContext: "); pw.println(mContext); |
| 632 | pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId()); |
| 633 | if (mSystemServerInterface != null) { |
| 634 | pw.print(prefix); pw.print("mSystemServerInterface: "); |
| 635 | pw.println(mSystemServerInterface); |
| 636 | } |
| 637 | if (mDirectServiceInterface != null) { |
| 638 | pw.print(prefix); pw.print("mDirectServiceInterface: "); |
| 639 | pw.println(mDirectServiceInterface); |
| 640 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 641 | pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get()); |
| 642 | pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled()); |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 643 | pw.print(prefix); pw.print("state: "); pw.println(getStateAsString(mState)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 644 | if (mApplicationToken != null) { |
| 645 | pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken); |
| 646 | } |
| 647 | if (mComponentName != null) { |
| 648 | pw.print(prefix); pw.print("component name: "); |
| 649 | pw.println(mComponentName.flattenToShortString()); |
| 650 | } |
| 651 | if (mEvents != null && !mEvents.isEmpty()) { |
| 652 | final int numberEvents = mEvents.size(); |
| 653 | pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents); |
| 654 | pw.print('/'); pw.println(MAX_BUFFER_SIZE); |
| 655 | if (VERBOSE && numberEvents > 0) { |
| 656 | final String prefix3 = prefix + " "; |
| 657 | for (int i = 0; i < numberEvents; i++) { |
| 658 | final ContentCaptureEvent event = mEvents.get(i); |
| 659 | pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw); |
| 660 | pw.println(); |
| 661 | } |
| 662 | } |
| 663 | pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS); |
| 664 | pw.print(prefix); pw.print("next flush: "); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 665 | TimeUtils.formatDuration(mNextFlush - System.currentTimeMillis(), pw); |
| 666 | pw.print(" ("); pw.print(TimeUtils.logTimeOfDay(mNextFlush)); pw.println(")"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 667 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame] | 668 | pw.print(prefix); pw.println("flush history:"); |
| 669 | mFlushHistory.reverseDump(/* fd= */ null, pw, /* args= */ null); pw.println(); |
| 670 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 671 | super.dump(prefix, pw); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Gets a string that can be used to identify the activity on logging statements. |
| 676 | */ |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 677 | private String getActivityName() { |
| 678 | return mComponentName == null |
| 679 | ? "pkg:" + mContext.getPackageName() |
| 680 | : "act:" + mComponentName.flattenToShortString(); |
| 681 | } |
| 682 | |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 683 | @NonNull |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 684 | private String getDebugState() { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 685 | return getActivityName() + " [state=" + getStateAsString(mState) + ", disabled=" |
| 686 | + mDisabled.get() + "]"; |
| 687 | } |
| 688 | |
| 689 | @NonNull |
| 690 | private String getDebugState(@FlushReason int reason) { |
| 691 | return getDebugState() + ", reason=" + getflushReasonAsString(reason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 692 | } |
| 693 | } |