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