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