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