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