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