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 | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 289 | if (!mEvents.isEmpty() && eventType == TYPE_VIEW_TEXT_CHANGED) { |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 290 | final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 291 | |
| 292 | // TODO(b/121045053): check if flags match |
| 293 | if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED |
| 294 | && lastEvent.getId().equals(event.getId())) { |
| 295 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 296 | Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text = " |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 297 | + event.getText()); |
| 298 | } |
| 299 | lastEvent.setText(event.getText()); |
| 300 | } else { |
| 301 | mEvents.add(event); |
| 302 | } |
| 303 | } else { |
| 304 | mEvents.add(event); |
| 305 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 306 | |
| 307 | final int numberEvents = mEvents.size(); |
| 308 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 309 | final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE; |
| 310 | |
| 311 | if (bufferEvent && !forceFlush) { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 312 | handleScheduleFlush(FLUSH_REASON_IDLE_TIMEOUT, /* checkExisting= */ true); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 316 | if (mState != STATE_ACTIVE && numberEvents >= MAX_BUFFER_SIZE) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 317 | // Callback from startSession hasn't been called yet - typically happens on system |
| 318 | // apps that are started before the system service |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 319 | // 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] | 320 | // not complete instead. Similarly, the manager service should return right away |
| 321 | // when the user does not have a service set |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 322 | if (DEBUG) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 323 | Log.d(TAG, "Closing session for " + getDebugState() |
| 324 | + " after " + numberEvents + " delayed events"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 325 | } |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 326 | handleResetSession(STATE_DISABLED | STATE_NO_RESPONSE); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 327 | // TODO(b/111276913): blacklist activity / use special flag to indicate that |
| 328 | // when it's launched again |
| 329 | return; |
| 330 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 331 | final int flushReason; |
| 332 | switch (eventType) { |
| 333 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
| 334 | flushReason = FLUSH_REASON_SESSION_STARTED; |
| 335 | break; |
| 336 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
| 337 | flushReason = FLUSH_REASON_SESSION_FINISHED; |
| 338 | break; |
| 339 | default: |
| 340 | flushReason = FLUSH_REASON_FULL; |
| 341 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 342 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 343 | handleForceFlush(flushReason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 346 | private boolean handleHasStarted() { |
| 347 | return mState != UNKNWON_STATE; |
| 348 | } |
| 349 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 350 | private void handleScheduleFlush(@FlushReason int reason, boolean checkExisting) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 351 | if (!handleHasStarted()) { |
| 352 | Log.v(TAG, "handleScheduleFlush(" + getDebugState() + "): session not started yet"); |
| 353 | return; |
| 354 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 355 | if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) { |
| 356 | // "Renew" the flush message by removing the previous one |
| 357 | mHandler.removeMessages(MSG_FLUSH); |
| 358 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 359 | mNextFlush = System.currentTimeMillis() + FLUSHING_FREQUENCY_MS; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 360 | if (VERBOSE) { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 361 | Log.v(TAG, "handleScheduleFlush(" + getDebugState() |
| 362 | + ", reason=" + getflushReasonAsString(reason) + "): scheduled to flush in " |
| 363 | + FLUSHING_FREQUENCY_MS + "ms: " + TimeUtils.logTimeOfDay(mNextFlush)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 364 | } |
| 365 | mHandler.sendMessageDelayed( |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 366 | obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this, reason) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 367 | .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS); |
| 368 | } |
| 369 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 370 | private void handleFlushIfNeeded(@FlushReason int reason) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 371 | if (mEvents.isEmpty()) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 372 | if (VERBOSE) Log.v(TAG, "Nothing to flush"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 373 | return; |
| 374 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 375 | handleForceFlush(reason); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 378 | private void handleForceFlush(@FlushReason int reason) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 379 | if (mEvents == null) return; |
| 380 | |
| 381 | if (mDirectServiceInterface == null) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 382 | if (VERBOSE) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 383 | Log.v(TAG, "handleForceFlush(" + getDebugState() |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 384 | + ", reason=" + getflushReasonAsString(reason) |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 385 | + "): hold your horses, client not ready: " + mEvents); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 386 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 387 | if (!mHandler.hasMessages(MSG_FLUSH)) { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 388 | handleScheduleFlush(reason, /* checkExisting= */ false); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 389 | } |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | final int numberEvents = mEvents.size(); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 394 | final String reasonString = getflushReasonAsString(reason); |
| 395 | if (DEBUG) { |
| 396 | Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getDebugState() |
| 397 | + ". Reason: " + reasonString); |
| 398 | } |
| 399 | // Logs reason, size, max size, idle timeout |
| 400 | final String logRecord = "r=" + reasonString + " s=" + numberEvents |
| 401 | + " m=" + MAX_BUFFER_SIZE + " i=" + FLUSHING_FREQUENCY_MS; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 402 | try { |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 403 | mFlushHistory.log(logRecord); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 404 | mHandler.removeMessages(MSG_FLUSH); |
| 405 | |
| 406 | final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents(); |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 407 | mDirectServiceInterface.sendEvents(events); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 408 | } catch (RemoteException e) { |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 409 | Log.w(TAG, "Error sending " + numberEvents + " for " + getDebugState() |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 410 | + ": " + e); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Resets the buffer and return a {@link ParceledListSlice} with the previous events. |
| 416 | */ |
| 417 | @NonNull |
| 418 | private ParceledListSlice<ContentCaptureEvent> handleClearEvents() { |
| 419 | // NOTE: we must save a reference to the current mEvents and then set it to to null, |
| 420 | // otherwise clearing it would clear it in the receiving side if the service is also local. |
| 421 | final List<ContentCaptureEvent> events = mEvents == null |
| 422 | ? Collections.emptyList() |
| 423 | : mEvents; |
| 424 | mEvents = null; |
| 425 | return new ParceledListSlice<>(events); |
| 426 | } |
| 427 | |
| 428 | private void handleDestroySession() { |
| 429 | if (DEBUG) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 430 | Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 431 | + (mEvents == null ? 0 : mEvents.size()) + " event(s) for " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 432 | + getDebugState()); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | try { |
Adam He | 6884fed | 2019-01-07 13:18:32 -0800 | [diff] [blame] | 436 | if (mSystemServerInterface == null) return; |
| 437 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 438 | mSystemServerInterface.finishSession(mContext.getUserId(), mId); |
| 439 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 440 | Log.e(TAG, "Error destroying system-service session " + mId + " for " |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 441 | + getDebugState() + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 445 | // 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] | 446 | // clearings out. |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 447 | private void handleResetSession(int newState) { |
| 448 | if (VERBOSE) { |
| 449 | Log.v(TAG, "handleResetSession(" + getActivityName() + "): from " |
| 450 | + getStateAsString(mState) + " to " + getStateAsString(newState)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 451 | } |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 452 | mState = newState; |
| 453 | mDisabled.set((newState & STATE_DISABLED) != 0); |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 454 | // TODO(b/122454205): must reset children (which currently is owned by superclass) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 455 | mApplicationToken = null; |
| 456 | mComponentName = null; |
| 457 | mEvents = null; |
| 458 | if (mDirectServiceInterface != null) { |
| 459 | mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0); |
| 460 | } |
| 461 | mDirectServiceInterface = null; |
| 462 | mHandler.removeMessages(MSG_FLUSH); |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 467 | notifyViewAppeared(mId, node); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | @Override |
| 471 | void internalNotifyViewDisappeared(@NonNull AutofillId id) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 472 | notifyViewDisappeared(mId, id); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | @Override |
| 476 | void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text, |
| 477 | int flags) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 478 | notifyViewTextChanged(mId, id, text, flags); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Override |
| 482 | boolean isContentCaptureEnabled() { |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 483 | return super.isContentCaptureEnabled() && mSystemServerInterface != null |
| 484 | && !mDisabled.get(); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Felipe Leme | 4bc0f6b | 2019-01-03 19:01:07 -0800 | [diff] [blame] | 487 | // 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] | 488 | // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such |
| 489 | // change should also get get rid of the "internalNotifyXXXX" methods above |
| 490 | void notifyChildSessionStarted(@NonNull String parentSessionId, |
| 491 | @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) { |
| 492 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 493 | new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED) |
| 494 | .setParentSessionId(parentSessionId) |
| 495 | .setClientContext(clientContext), |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 496 | /* forceFlush= */ true)); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void notifyChildSessionFinished(@NonNull String parentSessionId, |
| 500 | @NonNull String childSessionId) { |
| 501 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 502 | new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED) |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 503 | .setParentSessionId(parentSessionId), /* forceFlush= */ true)); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) { |
| 507 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 508 | new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED) |
| 509 | .setViewNode(node.mNode), /* forceFlush= */ false)); |
| 510 | } |
| 511 | |
| 512 | void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) { |
| 513 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 514 | new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id), |
| 515 | /* forceFlush= */ false)); |
| 516 | } |
| 517 | |
| 518 | void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id, |
| 519 | @Nullable CharSequence text, int flags) { |
| 520 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 521 | new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id) |
| 522 | .setText(text), /* forceFlush= */ false)); |
| 523 | } |
| 524 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 525 | @Override |
| 526 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 527 | pw.print(prefix); pw.print("mContext: "); pw.println(mContext); |
| 528 | pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId()); |
| 529 | if (mSystemServerInterface != null) { |
| 530 | pw.print(prefix); pw.print("mSystemServerInterface: "); |
| 531 | pw.println(mSystemServerInterface); |
| 532 | } |
| 533 | if (mDirectServiceInterface != null) { |
| 534 | pw.print(prefix); pw.print("mDirectServiceInterface: "); |
| 535 | pw.println(mDirectServiceInterface); |
| 536 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 537 | pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get()); |
| 538 | pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled()); |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 539 | pw.print(prefix); pw.print("state: "); pw.println(getStateAsString(mState)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 540 | if (mApplicationToken != null) { |
| 541 | pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken); |
| 542 | } |
| 543 | if (mComponentName != null) { |
| 544 | pw.print(prefix); pw.print("component name: "); |
| 545 | pw.println(mComponentName.flattenToShortString()); |
| 546 | } |
| 547 | if (mEvents != null && !mEvents.isEmpty()) { |
| 548 | final int numberEvents = mEvents.size(); |
| 549 | pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents); |
| 550 | pw.print('/'); pw.println(MAX_BUFFER_SIZE); |
| 551 | if (VERBOSE && numberEvents > 0) { |
| 552 | final String prefix3 = prefix + " "; |
| 553 | for (int i = 0; i < numberEvents; i++) { |
| 554 | final ContentCaptureEvent event = mEvents.get(i); |
| 555 | pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw); |
| 556 | pw.println(); |
| 557 | } |
| 558 | } |
| 559 | pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS); |
| 560 | pw.print(prefix); pw.print("next flush: "); |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 561 | TimeUtils.formatDuration(mNextFlush - System.currentTimeMillis(), pw); |
| 562 | pw.print(" ("); pw.print(TimeUtils.logTimeOfDay(mNextFlush)); pw.println(")"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 563 | } |
Felipe Leme | 1af85ea | 2019-01-16 13:23:40 -0800 | [diff] [blame^] | 564 | pw.print(prefix); pw.println("flush history:"); |
| 565 | mFlushHistory.reverseDump(/* fd= */ null, pw, /* args= */ null); pw.println(); |
| 566 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 567 | super.dump(prefix, pw); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Gets a string that can be used to identify the activity on logging statements. |
| 572 | */ |
Felipe Leme | d65692c | 2019-01-16 12:10:50 -0800 | [diff] [blame] | 573 | private String getActivityName() { |
| 574 | return mComponentName == null |
| 575 | ? "pkg:" + mContext.getPackageName() |
| 576 | : "act:" + mComponentName.flattenToShortString(); |
| 577 | } |
| 578 | |
| 579 | private String getDebugState() { |
| 580 | return getActivityName() + " (state=" + getStateAsString(mState) + ")"; |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 581 | } |
| 582 | } |