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