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