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; |
| 38 | import android.os.SystemClock; |
| 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 | |
| 91 | @NonNull |
| 92 | private final AtomicBoolean mDisabled; |
| 93 | |
| 94 | @NonNull |
| 95 | private final Context mContext; |
| 96 | |
| 97 | @NonNull |
| 98 | private final Handler mHandler; |
| 99 | |
| 100 | /** |
| 101 | * Interface to the system_server binder object - it's only used to start the session (and |
| 102 | * notify when the session is finished). |
| 103 | */ |
| 104 | @Nullable |
| 105 | private final IContentCaptureManager mSystemServerInterface; |
| 106 | |
| 107 | /** |
| 108 | * Direct interface to the service binder object - it's used to send the events, including the |
| 109 | * last ones (when the session is finished) |
| 110 | */ |
| 111 | @Nullable |
| 112 | private IContentCaptureDirectManager mDirectServiceInterface; |
| 113 | @Nullable |
| 114 | private DeathRecipient mDirectServiceVulture; |
| 115 | |
| 116 | private int mState = STATE_UNKNOWN; |
| 117 | |
| 118 | @Nullable |
| 119 | private IBinder mApplicationToken; |
| 120 | |
| 121 | @Nullable |
| 122 | private ComponentName mComponentName; |
| 123 | |
| 124 | /** |
| 125 | * List of events held to be sent as a batch. |
| 126 | */ |
| 127 | @Nullable |
| 128 | private ArrayList<ContentCaptureEvent> mEvents; |
| 129 | |
| 130 | // Used just for debugging purposes (on dump) |
| 131 | private long mNextFlush; |
| 132 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 133 | /** @hide */ |
| 134 | protected MainContentCaptureSession(@NonNull Context context, @NonNull Handler handler, |
| 135 | @Nullable IContentCaptureManager systemServerInterface, |
| 136 | @NonNull AtomicBoolean disabled) { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 137 | mContext = context; |
| 138 | mHandler = handler; |
| 139 | mSystemServerInterface = systemServerInterface; |
| 140 | mDisabled = disabled; |
| 141 | } |
| 142 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 143 | @Override |
| 144 | ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) { |
| 145 | final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext); |
| 146 | notifyChildSessionStarted(mId, child.mId, clientContext); |
| 147 | return child; |
| 148 | } |
| 149 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 150 | /** |
| 151 | * Starts this session. |
| 152 | * |
| 153 | * @hide |
| 154 | */ |
| 155 | void start(@NonNull IBinder applicationToken, @NonNull ComponentName activityComponent) { |
| 156 | if (!isContentCaptureEnabled()) return; |
| 157 | |
| 158 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 159 | Log.v(TAG, "start(): token=" + applicationToken + ", comp=" |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 160 | + ComponentName.flattenToShortString(activityComponent)); |
| 161 | } |
| 162 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 163 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleStartSession, this, |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 164 | applicationToken, activityComponent)); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | void flush() { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 169 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleForceFlush, this)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | void onDestroy() { |
| 174 | mHandler.sendMessage( |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 175 | obtainMessage(MainContentCaptureSession::handleDestroySession, this)); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | private void handleStartSession(@NonNull IBinder token, @NonNull ComponentName componentName) { |
| 179 | if (mState != STATE_UNKNOWN) { |
| 180 | // TODO(b/111276913): revisit this scenario |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 181 | Log.w(TAG, "ignoring handleStartSession(" + token + ") while on state " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 182 | + getStateAsString(mState)); |
| 183 | return; |
| 184 | } |
| 185 | mState = STATE_WAITING_FOR_SERVER; |
| 186 | mApplicationToken = token; |
| 187 | mComponentName = componentName; |
| 188 | |
| 189 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 190 | Log.v(TAG, "handleStartSession(): token=" + token + ", act=" |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 191 | + getActivityDebugName() + ", id=" + mId); |
| 192 | } |
| 193 | final int flags = 0; // TODO(b/111276913): get proper flags |
| 194 | |
| 195 | try { |
| 196 | mSystemServerInterface.startSession(mContext.getUserId(), mApplicationToken, |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 197 | componentName, mId, flags, new IResultReceiver.Stub() { |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 198 | @Override |
| 199 | public void send(int resultCode, Bundle resultData) { |
| 200 | IBinder binder = null; |
| 201 | if (resultData != null) { |
| 202 | binder = resultData.getBinder(EXTRA_BINDER); |
| 203 | if (binder == null) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 204 | Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 205 | handleResetState(); |
| 206 | return; |
| 207 | } |
| 208 | } |
| 209 | handleSessionStarted(resultCode, binder); |
| 210 | } |
| 211 | }); |
| 212 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 213 | Log.w(TAG, "Error starting session for " + componentName.flattenToShortString() + ": " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 214 | + e); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Callback from {@code system_server} after call to |
| 220 | * {@link IContentCaptureManager#startSession(int, IBinder, ComponentName, String, |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 221 | * int, IResultReceiver)}. |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 222 | * |
| 223 | * @param resultCode session state |
| 224 | * @param binder handle to {@code IContentCaptureDirectManager} |
| 225 | */ |
| 226 | private void handleSessionStarted(int resultCode, @Nullable IBinder binder) { |
| 227 | mState = resultCode; |
| 228 | if (binder != null) { |
| 229 | mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder); |
| 230 | mDirectServiceVulture = () -> { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 231 | Log.w(TAG, "Destroying session " + mId + " because service died"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 232 | destroy(); |
| 233 | }; |
| 234 | try { |
| 235 | binder.linkToDeath(mDirectServiceVulture, 0); |
| 236 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 237 | Log.w(TAG, "Failed to link to death on " + binder + ": " + e); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | if (resultCode == STATE_DISABLED || resultCode == STATE_DISABLED_DUPLICATED_ID) { |
| 241 | mDisabled.set(true); |
| 242 | handleResetSession(/* resetState= */ false); |
| 243 | } else { |
| 244 | mDisabled.set(false); |
| 245 | } |
| 246 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 247 | Log.v(TAG, "handleSessionStarted() result: code=" + resultCode + ", id=" + mId |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 248 | + ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get() |
| 249 | + ", binder=" + binder); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | private void handleSendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) { |
| 254 | if (mEvents == null) { |
| 255 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 256 | Log.v(TAG, "Creating buffer for " + MAX_BUFFER_SIZE + " events"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 257 | } |
| 258 | mEvents = new ArrayList<>(MAX_BUFFER_SIZE); |
| 259 | } |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 260 | |
| 261 | if (!mEvents.isEmpty() && event.getType() == TYPE_VIEW_TEXT_CHANGED) { |
| 262 | final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 263 | |
| 264 | // TODO(b/121045053): check if flags match |
| 265 | if (lastEvent.getType() == TYPE_VIEW_TEXT_CHANGED |
| 266 | && lastEvent.getId().equals(event.getId())) { |
| 267 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 268 | Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text = " |
Adam He | ac13265 | 2019-01-02 14:40:15 -0800 | [diff] [blame] | 269 | + event.getText()); |
| 270 | } |
| 271 | lastEvent.setText(event.getText()); |
| 272 | } else { |
| 273 | mEvents.add(event); |
| 274 | } |
| 275 | } else { |
| 276 | mEvents.add(event); |
| 277 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 278 | |
| 279 | final int numberEvents = mEvents.size(); |
| 280 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 281 | final boolean bufferEvent = numberEvents < MAX_BUFFER_SIZE; |
| 282 | |
| 283 | if (bufferEvent && !forceFlush) { |
| 284 | handleScheduleFlush(/* checkExisting= */ true); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | if (mState != STATE_ACTIVE) { |
| 289 | // Callback from startSession hasn't been called yet - typically happens on system |
| 290 | // apps that are started before the system service |
| 291 | // TODO(b/111276913): try to ignore session while system is not ready / boot |
| 292 | // not complete instead. Similarly, the manager service should return right away |
| 293 | // when the user does not have a service set |
| 294 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 295 | Log.v(TAG, "Closing session for " + getActivityDebugName() |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 296 | + " after " + numberEvents + " delayed events and state " |
| 297 | + getStateAsString(mState)); |
| 298 | } |
| 299 | handleResetState(); |
| 300 | // TODO(b/111276913): blacklist activity / use special flag to indicate that |
| 301 | // when it's launched again |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | handleForceFlush(); |
| 306 | } |
| 307 | |
| 308 | private void handleScheduleFlush(boolean checkExisting) { |
| 309 | if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) { |
| 310 | // "Renew" the flush message by removing the previous one |
| 311 | mHandler.removeMessages(MSG_FLUSH); |
| 312 | } |
| 313 | mNextFlush = SystemClock.elapsedRealtime() + FLUSHING_FREQUENCY_MS; |
| 314 | if (VERBOSE) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 315 | Log.v(TAG, "Scheduled to flush in " + FLUSHING_FREQUENCY_MS + "ms: " + mNextFlush); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 316 | } |
| 317 | mHandler.sendMessageDelayed( |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 318 | obtainMessage(MainContentCaptureSession::handleFlushIfNeeded, this) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 319 | .setWhat(MSG_FLUSH), FLUSHING_FREQUENCY_MS); |
| 320 | } |
| 321 | |
| 322 | private void handleFlushIfNeeded() { |
| 323 | if (mEvents.isEmpty()) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 324 | if (VERBOSE) Log.v(TAG, "Nothing to flush"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | handleForceFlush(); |
| 328 | } |
| 329 | |
| 330 | private void handleForceFlush() { |
| 331 | if (mEvents == null) return; |
| 332 | |
| 333 | if (mDirectServiceInterface == null) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 334 | if (DEBUG) Log.d(TAG, "handleForceFlush(): hold your horses, client not ready yet!"); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 335 | if (!mHandler.hasMessages(MSG_FLUSH)) { |
| 336 | handleScheduleFlush(/* checkExisting= */ false); |
| 337 | } |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | final int numberEvents = mEvents.size(); |
| 342 | try { |
| 343 | if (DEBUG) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 344 | Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getActivityDebugName()); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 345 | } |
| 346 | mHandler.removeMessages(MSG_FLUSH); |
| 347 | |
| 348 | final ParceledListSlice<ContentCaptureEvent> events = handleClearEvents(); |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 349 | mDirectServiceInterface.sendEvents(events); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 350 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 351 | Log.w(TAG, "Error sending " + numberEvents + " for " + getActivityDebugName() |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 352 | + ": " + e); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Resets the buffer and return a {@link ParceledListSlice} with the previous events. |
| 358 | */ |
| 359 | @NonNull |
| 360 | private ParceledListSlice<ContentCaptureEvent> handleClearEvents() { |
| 361 | // NOTE: we must save a reference to the current mEvents and then set it to to null, |
| 362 | // otherwise clearing it would clear it in the receiving side if the service is also local. |
| 363 | final List<ContentCaptureEvent> events = mEvents == null |
| 364 | ? Collections.emptyList() |
| 365 | : mEvents; |
| 366 | mEvents = null; |
| 367 | return new ParceledListSlice<>(events); |
| 368 | } |
| 369 | |
| 370 | private void handleDestroySession() { |
| 371 | if (DEBUG) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 372 | Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 373 | + (mEvents == null ? 0 : mEvents.size()) + " event(s) for " |
| 374 | + getActivityDebugName()); |
| 375 | } |
| 376 | |
| 377 | try { |
| 378 | mSystemServerInterface.finishSession(mContext.getUserId(), mId); |
| 379 | } catch (RemoteException e) { |
Felipe Leme | af1a0e7 | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 380 | Log.e(TAG, "Error destroying system-service session " + mId + " for " |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 381 | + getActivityDebugName() + ": " + e); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | private void handleResetState() { |
| 386 | handleResetSession(/* resetState= */ true); |
| 387 | } |
| 388 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 389 | // TODO(b/121033016): 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] | 390 | // clearings out. |
| 391 | private void handleResetSession(boolean resetState) { |
| 392 | if (resetState) { |
| 393 | mState = STATE_UNKNOWN; |
| 394 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 395 | |
| 396 | // TODO(b/121033016): must reset children (which currently is owned by superclass) |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 397 | mApplicationToken = null; |
| 398 | mComponentName = null; |
| 399 | mEvents = null; |
| 400 | if (mDirectServiceInterface != null) { |
| 401 | mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0); |
| 402 | } |
| 403 | mDirectServiceInterface = null; |
| 404 | mHandler.removeMessages(MSG_FLUSH); |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 409 | notifyViewAppeared(mId, node); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
| 413 | void internalNotifyViewDisappeared(@NonNull AutofillId id) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 414 | notifyViewDisappeared(mId, id); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
| 418 | void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text, |
| 419 | int flags) { |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 420 | notifyViewTextChanged(mId, id, text, flags); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | @Override |
| 424 | boolean isContentCaptureEnabled() { |
Felipe Leme | bef744c | 2019-01-03 11:07:25 -0800 | [diff] [blame] | 425 | return super.isContentCaptureEnabled() && mSystemServerInterface != null |
| 426 | && !mDisabled.get(); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 429 | // TODO(b/121033016): refactor "notifyXXXX" methods below to a common "Buffer" object that is |
| 430 | // shared between ActivityContentCaptureSession and ChildContentCaptureSession objects. Such |
| 431 | // change should also get get rid of the "internalNotifyXXXX" methods above |
| 432 | void notifyChildSessionStarted(@NonNull String parentSessionId, |
| 433 | @NonNull String childSessionId, @NonNull ContentCaptureContext clientContext) { |
| 434 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 435 | new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED) |
| 436 | .setParentSessionId(parentSessionId) |
| 437 | .setClientContext(clientContext), |
| 438 | /* forceFlush= */ false)); |
| 439 | } |
| 440 | |
| 441 | void notifyChildSessionFinished(@NonNull String parentSessionId, |
| 442 | @NonNull String childSessionId) { |
| 443 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 444 | new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED) |
| 445 | .setParentSessionId(parentSessionId), /* forceFlush= */ false)); |
| 446 | } |
| 447 | |
| 448 | void notifyViewAppeared(@NonNull String sessionId, @NonNull ViewStructureImpl node) { |
| 449 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 450 | new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED) |
| 451 | .setViewNode(node.mNode), /* forceFlush= */ false)); |
| 452 | } |
| 453 | |
| 454 | void notifyViewDisappeared(@NonNull String sessionId, @NonNull AutofillId id) { |
| 455 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 456 | new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id), |
| 457 | /* forceFlush= */ false)); |
| 458 | } |
| 459 | |
| 460 | void notifyViewTextChanged(@NonNull String sessionId, @NonNull AutofillId id, |
| 461 | @Nullable CharSequence text, int flags) { |
| 462 | mHandler.sendMessage(obtainMessage(MainContentCaptureSession::handleSendEvent, this, |
| 463 | new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED, flags).setAutofillId(id) |
| 464 | .setText(text), /* forceFlush= */ false)); |
| 465 | } |
| 466 | |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 467 | @Override |
| 468 | void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
| 469 | pw.print(prefix); pw.print("id: "); pw.println(mId); |
| 470 | pw.print(prefix); pw.print("mContext: "); pw.println(mContext); |
| 471 | pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId()); |
| 472 | if (mSystemServerInterface != null) { |
| 473 | pw.print(prefix); pw.print("mSystemServerInterface: "); |
| 474 | pw.println(mSystemServerInterface); |
| 475 | } |
| 476 | if (mDirectServiceInterface != null) { |
| 477 | pw.print(prefix); pw.print("mDirectServiceInterface: "); |
| 478 | pw.println(mDirectServiceInterface); |
| 479 | } |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 480 | pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get()); |
| 481 | pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled()); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 482 | pw.print(prefix); pw.print("state: "); pw.print(mState); pw.print(" ("); |
| 483 | pw.print(getStateAsString(mState)); pw.println(")"); |
| 484 | if (mApplicationToken != null) { |
| 485 | pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken); |
| 486 | } |
| 487 | if (mComponentName != null) { |
| 488 | pw.print(prefix); pw.print("component name: "); |
| 489 | pw.println(mComponentName.flattenToShortString()); |
| 490 | } |
| 491 | if (mEvents != null && !mEvents.isEmpty()) { |
| 492 | final int numberEvents = mEvents.size(); |
| 493 | pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents); |
| 494 | pw.print('/'); pw.println(MAX_BUFFER_SIZE); |
| 495 | if (VERBOSE && numberEvents > 0) { |
| 496 | final String prefix3 = prefix + " "; |
| 497 | for (int i = 0; i < numberEvents; i++) { |
| 498 | final ContentCaptureEvent event = mEvents.get(i); |
| 499 | pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw); |
| 500 | pw.println(); |
| 501 | } |
| 502 | } |
| 503 | pw.print(prefix); pw.print("flush frequency: "); pw.println(FLUSHING_FREQUENCY_MS); |
| 504 | pw.print(prefix); pw.print("next flush: "); |
| 505 | TimeUtils.formatDuration(mNextFlush - SystemClock.elapsedRealtime(), pw); pw.println(); |
| 506 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 507 | super.dump(prefix, pw); |
Felipe Leme | b63e0dd | 2018-12-18 11:56:42 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Gets a string that can be used to identify the activity on logging statements. |
| 512 | */ |
| 513 | private String getActivityDebugName() { |
| 514 | return mComponentName == null ? mContext.getPackageName() |
| 515 | : mComponentName.flattenToShortString(); |
| 516 | } |
| 517 | } |