| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.view.contentcapture; |
| 17 |
|
| 18 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_CONTEXT_UPDATED; |
| 19 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_FINISHED; |
| 20 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_PAUSED; |
| 21 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_RESUMED; |
| 22 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_STARTED; |
| 23 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_APPEARED; |
| 24 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_DISAPPEARED; |
| 25 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_INSETS_CHANGED; |
| 26 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_TEXT_CHANGED; |
| 27 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_TREE_APPEARED; |
| 28 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_TREE_APPEARING; |
| 29 |
import static android.view.contentcapture.ContentCaptureEvent.TYPE_WINDOW_BOUNDS_CHANGED; |
| 30 |
import static android.view.contentcapture.ContentCaptureHelper.getSanitizedString; |
| 31 |
import static android.view.contentcapture.ContentCaptureHelper.sDebug; |
| 32 |
import static android.view.contentcapture.ContentCaptureHelper.sVerbose; |
| 33 |
import static android.view.contentcapture.ContentCaptureManager.RESULT_CODE_FALSE; |
| 34 |
|
| 35 |
import android.annotation.NonNull; |
| 36 |
import android.annotation.Nullable; |
| 37 |
import android.annotation.UiThread; |
| 38 |
import android.content.ComponentName; |
| 39 |
import android.content.pm.ParceledListSlice; |
| 40 |
import android.graphics.Insets; |
| 41 |
import android.graphics.Rect; |
| 42 |
import android.os.Bundle; |
| 43 |
import android.os.Handler; |
| 44 |
import android.os.IBinder; |
| 45 |
import android.os.IBinder.DeathRecipient; |
| 46 |
import android.os.RemoteException; |
| 47 |
import android.text.Selection; |
| 48 |
import android.text.Spannable; |
| 49 |
import android.text.SpannableString; |
| 50 |
import android.text.Spanned; |
| 51 |
import android.text.TextUtils; |
| 52 |
import android.util.LocalLog; |
| 53 |
import android.util.Log; |
| 54 |
import android.util.TimeUtils; |
| 55 |
import android.view.autofill.AutofillId; |
| 56 |
import android.view.contentcapture.ViewNode.ViewStructureImpl; |
| 57 |
import android.view.inputmethod.BaseInputConnection; |
| 58 |
|
| 59 |
import com.android.internal.os.IResultReceiver; |
| 60 |
|
| 61 |
import java.io.PrintWriter; |
| 62 |
import java.lang.ref.WeakReference; |
| 63 |
import java.util.ArrayList; |
| 64 |
import java.util.Collections; |
| 65 |
import java.util.List; |
| 66 |
import java.util.NoSuchElementException; |
| 67 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public final class MainContentCaptureSession extends ContentCaptureSession { |
| 78 |
|
| 79 |
private static final String TAG = MainContentCaptureSession.class.getSimpleName(); |
| 80 |
|
| 81 |
|
| 82 |
private static final boolean FORCE_FLUSH = true; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private static final int MSG_FLUSH = 1; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static final String EXTRA_BINDER = "binder"; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final String EXTRA_ENABLED_STATE = "enabled"; |
| 100 |
|
| 101 |
@NonNull |
| 102 |
private final AtomicBoolean mDisabled = new AtomicBoolean(false); |
| 103 |
|
| 104 |
@NonNull |
| 105 |
private final ContentCaptureManager.StrippedContext mContext; |
| 106 |
|
| 107 |
@NonNull |
| 108 |
private final ContentCaptureManager mManager; |
| 109 |
|
| 110 |
@NonNull |
| 111 |
private final Handler mHandler; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@NonNull |
| 118 |
private final IContentCaptureManager mSystemServerInterface; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@NonNull |
| 125 |
private IContentCaptureDirectManager mDirectServiceInterface; |
| 126 |
@Nullable |
| 127 |
private DeathRecipient mDirectServiceVulture; |
| 128 |
|
| 129 |
private int mState = UNKNOWN_STATE; |
| 130 |
|
| 131 |
@Nullable |
| 132 |
private IBinder mApplicationToken; |
| 133 |
@Nullable |
| 134 |
private IBinder mShareableActivityToken; |
| 135 |
|
| 136 |
@Nullable |
| 137 |
private ComponentName mComponentName; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@Nullable |
| 143 |
private ArrayList<ContentCaptureEvent> mEvents; |
| 144 |
|
| 145 |
|
| 146 |
private long mNextFlush; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private boolean mNextFlushForTextChanged = false; |
| 152 |
|
| 153 |
@Nullable |
| 154 |
private final LocalLog mFlushHistory; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
@NonNull |
| 160 |
private final SessionStateReceiver mSessionStateReceiver; |
| 161 |
|
| 162 |
private static class SessionStateReceiver extends IResultReceiver.Stub { |
| 163 |
private final WeakReference<MainContentCaptureSession> mMainSession; |
| 164 |
|
| 165 |
SessionStateReceiver(MainContentCaptureSession session) { |
| 166 |
mMainSession = new WeakReference<>(session); |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public void send(int resultCode, Bundle resultData) { |
| 171 |
final MainContentCaptureSession mainSession = mMainSession.get(); |
| 172 |
if (mainSession == null) { |
| 173 |
Log.w(TAG, "received result after mina session released"); |
| 174 |
return; |
| 175 |
} |
| 176 |
final IBinder binder; |
| 177 |
if (resultData != null) { |
| 178 |
|
| 179 |
final boolean hasEnabled = resultData.getBoolean(EXTRA_ENABLED_STATE); |
| 180 |
if (hasEnabled) { |
| 181 |
final boolean disabled = (resultCode == RESULT_CODE_FALSE); |
| 182 |
mainSession.mDisabled.set(disabled); |
| 183 |
return; |
| 184 |
} |
| 185 |
binder = resultData.getBinder(EXTRA_BINDER); |
| 186 |
if (binder == null) { |
| 187 |
Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result"); |
| 188 |
mainSession.mHandler.post(() -> mainSession.resetSession( |
| 189 |
STATE_DISABLED | STATE_INTERNAL_ERROR)); |
| 190 |
return; |
| 191 |
} |
| 192 |
} else { |
| 193 |
binder = null; |
| 194 |
} |
| 195 |
mainSession.mHandler.post(() -> mainSession.onSessionStarted(resultCode, binder)); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
protected MainContentCaptureSession(@NonNull ContentCaptureManager.StrippedContext context, |
| 200 |
@NonNull ContentCaptureManager manager, @NonNull Handler handler, |
| 201 |
@NonNull IContentCaptureManager systemServerInterface) { |
| 202 |
mContext = context; |
| 203 |
mManager = manager; |
| 204 |
mHandler = handler; |
| 205 |
mSystemServerInterface = systemServerInterface; |
| 206 |
|
| 207 |
final int logHistorySize = mManager.mOptions.logHistorySize; |
| 208 |
mFlushHistory = logHistorySize > 0 ? new LocalLog(logHistorySize) : null; |
| 209 |
|
| 210 |
mSessionStateReceiver = new SessionStateReceiver(this); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
MainContentCaptureSession getMainCaptureSession() { |
| 215 |
return this; |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) { |
| 220 |
final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext); |
| 221 |
notifyChildSessionStarted(mId, child.mId, clientContext); |
| 222 |
return child; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
@UiThread |
| 229 |
void start(@NonNull IBinder token, @NonNull IBinder shareableActivityToken, |
| 230 |
@NonNull ComponentName component, int flags) { |
| 231 |
if (!isContentCaptureEnabled()) return; |
| 232 |
|
| 233 |
if (sVerbose) { |
| 234 |
Log.v(TAG, "start(): token=" + token + ", comp=" |
| 235 |
+ ComponentName.flattenToShortString(component)); |
| 236 |
} |
| 237 |
|
| 238 |
if (hasStarted()) { |
| 239 |
|
| 240 |
if (sDebug) { |
| 241 |
Log.d(TAG, "ignoring handleStartSession(" + token + "/" |
| 242 |
+ ComponentName.flattenToShortString(component) + " while on state " |
| 243 |
+ getStateAsString(mState)); |
| 244 |
} |
| 245 |
return; |
| 246 |
} |
| 247 |
mState = STATE_WAITING_FOR_SERVER; |
| 248 |
mApplicationToken = token; |
| 249 |
mShareableActivityToken = shareableActivityToken; |
| 250 |
mComponentName = component; |
| 251 |
|
| 252 |
if (sVerbose) { |
| 253 |
Log.v(TAG, "handleStartSession(): token=" + token + ", act=" |
| 254 |
+ getDebugState() + ", id=" + mId); |
| 255 |
} |
| 256 |
|
| 257 |
try { |
| 258 |
mSystemServerInterface.startSession(mApplicationToken, mShareableActivityToken, |
| 259 |
component, mId, flags, mSessionStateReceiver); |
| 260 |
} catch (RemoteException e) { |
| 261 |
Log.w(TAG, "Error starting session for " + component.flattenToShortString() + ": " + e); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
void onDestroy() { |
| 267 |
mHandler.removeMessages(MSG_FLUSH); |
| 268 |
mHandler.post(() -> { |
| 269 |
try { |
| 270 |
flush(FLUSH_REASON_SESSION_FINISHED); |
| 271 |
} finally { |
| 272 |
destroySession(); |
| 273 |
} |
| 274 |
}); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
@UiThread |
| 286 |
private void onSessionStarted(int resultCode, @Nullable IBinder binder) { |
| 287 |
if (binder != null) { |
| 288 |
mDirectServiceInterface = IContentCaptureDirectManager.Stub.asInterface(binder); |
| 289 |
mDirectServiceVulture = () -> { |
| 290 |
Log.w(TAG, "Keeping session " + mId + " when service died"); |
| 291 |
mState = STATE_SERVICE_DIED; |
| 292 |
mDisabled.set(true); |
| 293 |
}; |
| 294 |
try { |
| 295 |
binder.linkToDeath(mDirectServiceVulture, 0); |
| 296 |
} catch (RemoteException e) { |
| 297 |
Log.w(TAG, "Failed to link to death on " + binder + ": " + e); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if ((resultCode & STATE_DISABLED) != 0) { |
| 302 |
resetSession(resultCode); |
| 303 |
} else { |
| 304 |
mState = resultCode; |
| 305 |
mDisabled.set(false); |
| 306 |
|
| 307 |
flushIfNeeded(FLUSH_REASON_SESSION_CONNECTED); |
| 308 |
} |
| 309 |
if (sVerbose) { |
| 310 |
Log.v(TAG, "handleSessionStarted() result: id=" + mId + " resultCode=" + resultCode |
| 311 |
+ ", state=" + getStateAsString(mState) + ", disabled=" + mDisabled.get() |
| 312 |
+ ", binder=" + binder + ", events=" + (mEvents == null ? 0 : mEvents.size())); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
@UiThread |
| 317 |
private void sendEvent(@NonNull ContentCaptureEvent event) { |
| 318 |
sendEvent(event, false); |
| 319 |
} |
| 320 |
|
| 321 |
@UiThread |
| 322 |
private void sendEvent(@NonNull ContentCaptureEvent event, boolean forceFlush) { |
| 323 |
final int eventType = event.getType(); |
| 324 |
if (sVerbose) Log.v(TAG, "handleSendEvent(" + getDebugState() + "): " + event); |
| 325 |
if (!hasStarted() && eventType != ContentCaptureEvent.TYPE_SESSION_STARTED |
| 326 |
&& eventType != ContentCaptureEvent.TYPE_CONTEXT_UPDATED) { |
| 327 |
|
| 328 |
if (sVerbose) { |
| 329 |
Log.v(TAG, "handleSendEvent(" + getDebugState() + ", " |
| 330 |
+ ContentCaptureEvent.getTypeAsString(eventType) |
| 331 |
+ "): dropping because session not started yet"); |
| 332 |
} |
| 333 |
return; |
| 334 |
} |
| 335 |
if (mDisabled.get()) { |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
if (sVerbose) Log.v(TAG, "handleSendEvent(): ignoring when disabled"); |
| 340 |
return; |
| 341 |
} |
| 342 |
final int maxBufferSize = mManager.mOptions.maxBufferSize; |
| 343 |
if (mEvents == null) { |
| 344 |
if (sVerbose) { |
| 345 |
Log.v(TAG, "handleSendEvent(): creating buffer for " + maxBufferSize + " events"); |
| 346 |
} |
| 347 |
mEvents = new ArrayList<>(maxBufferSize); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
boolean addEvent = true; |
| 352 |
|
| 353 |
if (eventType == TYPE_VIEW_TEXT_CHANGED) { |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
final CharSequence text = event.getText(); |
| 361 |
final boolean hasComposingSpan = event.hasComposingSpan(); |
| 362 |
if (hasComposingSpan) { |
| 363 |
ContentCaptureEvent lastEvent = null; |
| 364 |
for (int index = mEvents.size() - 1; index >= 0; index--) { |
| 365 |
final ContentCaptureEvent tmpEvent = mEvents.get(index); |
| 366 |
if (event.getId().equals(tmpEvent.getId())) { |
| 367 |
lastEvent = tmpEvent; |
| 368 |
break; |
| 369 |
} |
| 370 |
} |
| 371 |
if (lastEvent != null && lastEvent.hasComposingSpan()) { |
| 372 |
final CharSequence lastText = lastEvent.getText(); |
| 373 |
final boolean bothNonEmpty = !TextUtils.isEmpty(lastText) |
| 374 |
&& !TextUtils.isEmpty(text); |
| 375 |
boolean equalContent = |
| 376 |
TextUtils.equals(lastText, text) |
| 377 |
&& lastEvent.hasSameComposingSpan(event) |
| 378 |
&& lastEvent.hasSameSelectionSpan(event); |
| 379 |
if (equalContent) { |
| 380 |
addEvent = false; |
| 381 |
} else if (bothNonEmpty) { |
| 382 |
lastEvent.mergeEvent(event); |
| 383 |
addEvent = false; |
| 384 |
} |
| 385 |
if (!addEvent && sVerbose) { |
| 386 |
Log.v(TAG, "Buffering VIEW_TEXT_CHANGED event, updated text=" |
| 387 |
+ getSanitizedString(text)); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
if (!mEvents.isEmpty() && eventType == TYPE_VIEW_DISAPPEARED) { |
| 394 |
final ContentCaptureEvent lastEvent = mEvents.get(mEvents.size() - 1); |
| 395 |
if (lastEvent.getType() == TYPE_VIEW_DISAPPEARED |
| 396 |
&& event.getSessionId() == lastEvent.getSessionId()) { |
| 397 |
if (sVerbose) { |
| 398 |
Log.v(TAG, "Buffering TYPE_VIEW_DISAPPEARED events for session " |
| 399 |
+ lastEvent.getSessionId()); |
| 400 |
} |
| 401 |
lastEvent.mergeEvent(event); |
| 402 |
addEvent = false; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
if (addEvent) { |
| 407 |
mEvents.add(event); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
final int numberEvents = mEvents.size(); |
| 416 |
|
| 417 |
final boolean bufferEvent = numberEvents < maxBufferSize; |
| 418 |
|
| 419 |
if (bufferEvent && !forceFlush) { |
| 420 |
final int flushReason; |
| 421 |
if (eventType == TYPE_VIEW_TEXT_CHANGED) { |
| 422 |
mNextFlushForTextChanged = true; |
| 423 |
flushReason = FLUSH_REASON_TEXT_CHANGE_TIMEOUT; |
| 424 |
} else { |
| 425 |
if (mNextFlushForTextChanged) { |
| 426 |
if (sVerbose) { |
| 427 |
Log.i(TAG, "Not scheduling flush because next flush is for text changed"); |
| 428 |
} |
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
flushReason = FLUSH_REASON_IDLE_TIMEOUT; |
| 433 |
} |
| 434 |
scheduleFlush(flushReason, true); |
| 435 |
return; |
| 436 |
} |
| 437 |
|
| 438 |
if (mState != STATE_ACTIVE && numberEvents >= maxBufferSize) { |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
if (sDebug) { |
| 445 |
Log.d(TAG, "Closing session for " + getDebugState() |
| 446 |
+ " after " + numberEvents + " delayed events"); |
| 447 |
} |
| 448 |
resetSession(STATE_DISABLED | STATE_NO_RESPONSE); |
| 449 |
|
| 450 |
|
| 451 |
return; |
| 452 |
} |
| 453 |
final int flushReason; |
| 454 |
switch (eventType) { |
| 455 |
case ContentCaptureEvent.TYPE_SESSION_STARTED: |
| 456 |
flushReason = FLUSH_REASON_SESSION_STARTED; |
| 457 |
break; |
| 458 |
case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
| 459 |
flushReason = FLUSH_REASON_SESSION_FINISHED; |
| 460 |
break; |
| 461 |
default: |
| 462 |
flushReason = FLUSH_REASON_FULL; |
| 463 |
} |
| 464 |
|
| 465 |
flush(flushReason); |
| 466 |
} |
| 467 |
|
| 468 |
@UiThread |
| 469 |
private boolean hasStarted() { |
| 470 |
return mState != UNKNOWN_STATE; |
| 471 |
} |
| 472 |
|
| 473 |
@UiThread |
| 474 |
private void scheduleFlush(@FlushReason int reason, boolean checkExisting) { |
| 475 |
if (sVerbose) { |
| 476 |
Log.v(TAG, "handleScheduleFlush(" + getDebugState(reason) |
| 477 |
+ ", checkExisting=" + checkExisting); |
| 478 |
} |
| 479 |
if (!hasStarted()) { |
| 480 |
if (sVerbose) Log.v(TAG, "handleScheduleFlush(): session not started yet"); |
| 481 |
return; |
| 482 |
} |
| 483 |
|
| 484 |
if (mDisabled.get()) { |
| 485 |
|
| 486 |
|
| 487 |
Log.e(TAG, "handleScheduleFlush(" + getDebugState(reason) + "): should not be called " |
| 488 |
+ "when disabled. events=" + (mEvents == null ? null : mEvents.size())); |
| 489 |
return; |
| 490 |
} |
| 491 |
if (checkExisting && mHandler.hasMessages(MSG_FLUSH)) { |
| 492 |
|
| 493 |
mHandler.removeMessages(MSG_FLUSH); |
| 494 |
} |
| 495 |
|
| 496 |
final int flushFrequencyMs; |
| 497 |
if (reason == FLUSH_REASON_TEXT_CHANGE_TIMEOUT) { |
| 498 |
flushFrequencyMs = mManager.mOptions.textChangeFlushingFrequencyMs; |
| 499 |
} else { |
| 500 |
if (reason != FLUSH_REASON_IDLE_TIMEOUT) { |
| 501 |
if (sDebug) { |
| 502 |
Log.d(TAG, "handleScheduleFlush(" + getDebugState(reason) + "): not a timeout " |
| 503 |
+ "reason because mDirectServiceInterface is not ready yet"); |
| 504 |
} |
| 505 |
} |
| 506 |
flushFrequencyMs = mManager.mOptions.idleFlushingFrequencyMs; |
| 507 |
} |
| 508 |
|
| 509 |
mNextFlush = System.currentTimeMillis() + flushFrequencyMs; |
| 510 |
if (sVerbose) { |
| 511 |
Log.v(TAG, "handleScheduleFlush(): scheduled to flush in " |
| 512 |
+ flushFrequencyMs + "ms: " + TimeUtils.logTimeOfDay(mNextFlush)); |
| 513 |
} |
| 514 |
) |
| 515 |
mHandler.postDelayed(() -> flushIfNeeded(reason), MSG_FLUSH, flushFrequencyMs); |
| 516 |
} |
| 517 |
|
| 518 |
@UiThread |
| 519 |
private void flushIfNeeded(@FlushReason int reason) { |
| 520 |
if (mEvents == null || mEvents.isEmpty()) { |
| 521 |
if (sVerbose) Log.v(TAG, "Nothing to flush"); |
| 522 |
return; |
| 523 |
} |
| 524 |
flush(reason); |
| 525 |
} |
| 526 |
|
| 527 |
@Override |
| 528 |
@UiThread |
| 529 |
void flush(@FlushReason int reason) { |
| 530 |
if (mEvents == null) return; |
| 531 |
|
| 532 |
if (mDisabled.get()) { |
| 533 |
Log.e(TAG, "handleForceFlush(" + getDebugState(reason) + "): should not be when " |
| 534 |
+ "disabled"); |
| 535 |
return; |
| 536 |
} |
| 537 |
|
| 538 |
if (mDirectServiceInterface == null) { |
| 539 |
if (sVerbose) { |
| 540 |
Log.v(TAG, "handleForceFlush(" + getDebugState(reason) + "): hold your horses, " |
| 541 |
+ "client not ready: " + mEvents); |
| 542 |
} |
| 543 |
if (!mHandler.hasMessages(MSG_FLUSH)) { |
| 544 |
scheduleFlush(reason, false); |
| 545 |
} |
| 546 |
return; |
| 547 |
} |
| 548 |
|
| 549 |
mNextFlushForTextChanged = false; |
| 550 |
|
| 551 |
final int numberEvents = mEvents.size(); |
| 552 |
final String reasonString = getFlushReasonAsString(reason); |
| 553 |
if (sDebug) { |
| 554 |
Log.d(TAG, "Flushing " + numberEvents + " event(s) for " + getDebugState(reason)); |
| 555 |
} |
| 556 |
if (mFlushHistory != null) { |
| 557 |
|
| 558 |
final String logRecord = "r=" + reasonString + " s=" + numberEvents |
| 559 |
+ " m=" + mManager.mOptions.maxBufferSize |
| 560 |
+ " i=" + mManager.mOptions.idleFlushingFrequencyMs; |
| 561 |
mFlushHistory.log(logRecord); |
| 562 |
} |
| 563 |
try { |
| 564 |
mHandler.removeMessages(MSG_FLUSH); |
| 565 |
|
| 566 |
final ParceledListSlice<ContentCaptureEvent> events = clearEvents(); |
| 567 |
mDirectServiceInterface.sendEvents(events, reason, mManager.mOptions); |
| 568 |
} catch (RemoteException e) { |
| 569 |
Log.w(TAG, "Error sending " + numberEvents + " for " + getDebugState() |
| 570 |
+ ": " + e); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
@Override |
| 575 |
public void updateContentCaptureContext(@Nullable ContentCaptureContext context) { |
| 576 |
notifyContextUpdated(mId, context); |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
@NonNull |
| 583 |
@UiThread |
| 584 |
private ParceledListSlice<ContentCaptureEvent> clearEvents() { |
| 585 |
|
| 586 |
|
| 587 |
if (mEvents == null) { |
| 588 |
return new ParceledListSlice<>(Collections.EMPTY_LIST); |
| 589 |
} |
| 590 |
|
| 591 |
final List<ContentCaptureEvent> events = new ArrayList<>(mEvents); |
| 592 |
mEvents.clear(); |
| 593 |
return new ParceledListSlice<>(events); |
| 594 |
} |
| 595 |
|
| 596 |
@UiThread |
| 597 |
private void destroySession() { |
| 598 |
if (sDebug) { |
| 599 |
Log.d(TAG, "Destroying session (ctx=" + mContext + ", id=" + mId + ") with " |
| 600 |
+ (mEvents == null ? 0 : mEvents.size()) + " event(s) for " |
| 601 |
+ getDebugState()); |
| 602 |
} |
| 603 |
|
| 604 |
try { |
| 605 |
mSystemServerInterface.finishSession(mId); |
| 606 |
} catch (RemoteException e) { |
| 607 |
Log.e(TAG, "Error destroying system-service session " + mId + " for " |
| 608 |
+ getDebugState() + ": " + e); |
| 609 |
} |
| 610 |
|
| 611 |
if (mDirectServiceInterface != null) { |
| 612 |
mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0); |
| 613 |
} |
| 614 |
mDirectServiceInterface = null; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
@UiThread |
| 620 |
private void resetSession(int newState) { |
| 621 |
if (sVerbose) { |
| 622 |
Log.v(TAG, "handleResetSession(" + getActivityName() + "): from " |
| 623 |
+ getStateAsString(mState) + " to " + getStateAsString(newState)); |
| 624 |
} |
| 625 |
mState = newState; |
| 626 |
mDisabled.set((newState & STATE_DISABLED) != 0); |
| 627 |
|
| 628 |
mApplicationToken = null; |
| 629 |
mShareableActivityToken = null; |
| 630 |
mComponentName = null; |
| 631 |
mEvents = null; |
| 632 |
if (mDirectServiceInterface != null) { |
| 633 |
try { |
| 634 |
mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0); |
| 635 |
} catch (NoSuchElementException e) { |
| 636 |
Log.w(TAG, "IContentCaptureDirectManager does not exist"); |
| 637 |
} |
| 638 |
} |
| 639 |
mDirectServiceInterface = null; |
| 640 |
mHandler.removeMessages(MSG_FLUSH); |
| 641 |
} |
| 642 |
|
| 643 |
@Override |
| 644 |
void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) { |
| 645 |
notifyViewAppeared(mId, node); |
| 646 |
} |
| 647 |
|
| 648 |
@Override |
| 649 |
void internalNotifyViewDisappeared(@NonNull AutofillId id) { |
| 650 |
notifyViewDisappeared(mId, id); |
| 651 |
} |
| 652 |
|
| 653 |
@Override |
| 654 |
void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) { |
| 655 |
notifyViewTextChanged(mId, id, text); |
| 656 |
} |
| 657 |
|
| 658 |
@Override |
| 659 |
void internalNotifyViewInsetsChanged(@NonNull Insets viewInsets) { |
| 660 |
notifyViewInsetsChanged(mId, viewInsets); |
| 661 |
} |
| 662 |
|
| 663 |
@Override |
| 664 |
public void internalNotifyViewTreeEvent(boolean started) { |
| 665 |
notifyViewTreeEvent(mId, started); |
| 666 |
} |
| 667 |
|
| 668 |
@Override |
| 669 |
public void internalNotifySessionResumed() { |
| 670 |
notifySessionResumed(mId); |
| 671 |
} |
| 672 |
|
| 673 |
@Override |
| 674 |
public void internalNotifySessionPaused() { |
| 675 |
notifySessionPaused(mId); |
| 676 |
} |
| 677 |
|
| 678 |
@Override |
| 679 |
boolean isContentCaptureEnabled() { |
| 680 |
return super.isContentCaptureEnabled() && mManager.isContentCaptureEnabled(); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
boolean isDisabled() { |
| 685 |
return mDisabled.get(); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
boolean setDisabled(boolean disabled) { |
| 694 |
return mDisabled.compareAndSet(!disabled, disabled); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
void notifyChildSessionStarted(int parentSessionId, int childSessionId, |
| 701 |
@NonNull ContentCaptureContext clientContext) { |
| 702 |
mHandler.post(() -> sendEvent(new ContentCaptureEvent(childSessionId, TYPE_SESSION_STARTED) |
| 703 |
.setParentSessionId(parentSessionId).setClientContext(clientContext), |
| 704 |
FORCE_FLUSH)); |
| 705 |
} |
| 706 |
|
| 707 |
void notifyChildSessionFinished(int parentSessionId, int childSessionId) { |
| 708 |
mHandler.post(() -> sendEvent(new ContentCaptureEvent(childSessionId, TYPE_SESSION_FINISHED) |
| 709 |
.setParentSessionId(parentSessionId), FORCE_FLUSH)); |
| 710 |
} |
| 711 |
|
| 712 |
void notifyViewAppeared(int sessionId, @NonNull ViewStructureImpl node) { |
| 713 |
mHandler.post(() -> sendEvent(new ContentCaptureEvent(sessionId, TYPE_VIEW_APPEARED) |
| 714 |
.setViewNode(node.mNode))); |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
public void notifyViewDisappeared(int sessionId, @NonNull AutofillId id) { |
| 719 |
mHandler.post(() -> sendEvent( |
| 720 |
new ContentCaptureEvent(sessionId, TYPE_VIEW_DISAPPEARED).setAutofillId(id))); |
| 721 |
} |
| 722 |
|
| 723 |
void notifyViewTextChanged(int sessionId, @NonNull AutofillId id, @Nullable CharSequence text) { |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
final CharSequence eventText = stringOrSpannedStringWithoutNoCopySpans(text); |
| 728 |
|
| 729 |
final int composingStart; |
| 730 |
final int composingEnd; |
| 731 |
if (text instanceof Spannable) { |
| 732 |
composingStart = BaseInputConnection.getComposingSpanStart((Spannable) text); |
| 733 |
composingEnd = BaseInputConnection.getComposingSpanEnd((Spannable) text); |
| 734 |
} else { |
| 735 |
composingStart = ContentCaptureEvent.MAX_INVALID_VALUE; |
| 736 |
composingEnd = ContentCaptureEvent.MAX_INVALID_VALUE; |
| 737 |
} |
| 738 |
|
| 739 |
final int startIndex = Selection.getSelectionStart(text); |
| 740 |
final int endIndex = Selection.getSelectionEnd(text); |
| 741 |
mHandler.post(() -> sendEvent( |
| 742 |
new ContentCaptureEvent(sessionId, TYPE_VIEW_TEXT_CHANGED) |
| 743 |
.setAutofillId(id).setText(eventText) |
| 744 |
.setComposingIndex(composingStart, composingEnd) |
| 745 |
.setSelectionIndex(startIndex, endIndex))); |
| 746 |
} |
| 747 |
|
| 748 |
private CharSequence stringOrSpannedStringWithoutNoCopySpans(CharSequence source) { |
| 749 |
if (source == null) { |
| 750 |
return null; |
| 751 |
} else if (source instanceof Spanned) { |
| 752 |
return new SpannableString(source, true); |
| 753 |
} else { |
| 754 |
return source.toString(); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
public void notifyViewInsetsChanged(int sessionId, @NonNull Insets viewInsets) { |
| 760 |
mHandler.post(() -> sendEvent(new ContentCaptureEvent(sessionId, TYPE_VIEW_INSETS_CHANGED) |
| 761 |
.setInsets(viewInsets))); |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
public void notifyViewTreeEvent(int sessionId, boolean started) { |
| 766 |
final int type = started ? TYPE_VIEW_TREE_APPEARING : TYPE_VIEW_TREE_APPEARED; |
| 767 |
mHandler.post(() -> sendEvent(new ContentCaptureEvent(sessionId, type), FORCE_FLUSH)); |
| 768 |
} |
| 769 |
|
| 770 |
void notifySessionResumed(int sessionId) { |
| 771 |
mHandler.post(() -> sendEvent( |
| 772 |
new ContentCaptureEvent(sessionId, TYPE_SESSION_RESUMED), FORCE_FLUSH)); |
| 773 |
} |
| 774 |
|
| 775 |
void notifySessionPaused(int sessionId) { |
| 776 |
mHandler.post(() -> sendEvent( |
| 777 |
new ContentCaptureEvent(sessionId, TYPE_SESSION_PAUSED), FORCE_FLUSH)); |
| 778 |
} |
| 779 |
|
| 780 |
void notifyContextUpdated(int sessionId, @Nullable ContentCaptureContext context) { |
| 781 |
mHandler.post(() -> sendEvent(new ContentCaptureEvent(sessionId, TYPE_CONTEXT_UPDATED) |
| 782 |
.setClientContext(context), FORCE_FLUSH)); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
public void notifyWindowBoundsChanged(int sessionId, @NonNull Rect bounds) { |
| 787 |
mHandler.post(() -> sendEvent( |
| 788 |
new ContentCaptureEvent(sessionId, TYPE_WINDOW_BOUNDS_CHANGED) |
| 789 |
.setBounds(bounds) |
| 790 |
)); |
| 791 |
} |
| 792 |
|
| 793 |
@Override |
| 794 |
void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
| 795 |
super.dump(prefix, pw); |
| 796 |
|
| 797 |
pw.print(prefix); pw.print("mContext: "); pw.println(mContext); |
| 798 |
pw.print(prefix); pw.print("user: "); pw.println(mContext.getUserId()); |
| 799 |
if (mDirectServiceInterface != null) { |
| 800 |
pw.print(prefix); pw.print("mDirectServiceInterface: "); |
| 801 |
pw.println(mDirectServiceInterface); |
| 802 |
} |
| 803 |
pw.print(prefix); pw.print("mDisabled: "); pw.println(mDisabled.get()); |
| 804 |
pw.print(prefix); pw.print("isEnabled(): "); pw.println(isContentCaptureEnabled()); |
| 805 |
pw.print(prefix); pw.print("state: "); pw.println(getStateAsString(mState)); |
| 806 |
if (mApplicationToken != null) { |
| 807 |
pw.print(prefix); pw.print("app token: "); pw.println(mApplicationToken); |
| 808 |
} |
| 809 |
if (mShareableActivityToken != null) { |
| 810 |
pw.print(prefix); pw.print("sharable activity token: "); |
| 811 |
pw.println(mShareableActivityToken); |
| 812 |
} |
| 813 |
if (mComponentName != null) { |
| 814 |
pw.print(prefix); pw.print("component name: "); |
| 815 |
pw.println(mComponentName.flattenToShortString()); |
| 816 |
} |
| 817 |
if (mEvents != null && !mEvents.isEmpty()) { |
| 818 |
final int numberEvents = mEvents.size(); |
| 819 |
pw.print(prefix); pw.print("buffered events: "); pw.print(numberEvents); |
| 820 |
pw.print('/'); pw.println(mManager.mOptions.maxBufferSize); |
| 821 |
if (sVerbose && numberEvents > 0) { |
| 822 |
final String prefix3 = prefix + " "; |
| 823 |
for (int i = 0; i < numberEvents; i++) { |
| 824 |
final ContentCaptureEvent event = mEvents.get(i); |
| 825 |
pw.print(prefix3); pw.print(i); pw.print(": "); event.dump(pw); |
| 826 |
pw.println(); |
| 827 |
} |
| 828 |
} |
| 829 |
pw.print(prefix); pw.print("mNextFlushForTextChanged: "); |
| 830 |
pw.println(mNextFlushForTextChanged); |
| 831 |
pw.print(prefix); pw.print("flush frequency: "); |
| 832 |
if (mNextFlushForTextChanged) { |
| 833 |
pw.println(mManager.mOptions.textChangeFlushingFrequencyMs); |
| 834 |
} else { |
| 835 |
pw.println(mManager.mOptions.idleFlushingFrequencyMs); |
| 836 |
} |
| 837 |
pw.print(prefix); pw.print("next flush: "); |
| 838 |
TimeUtils.formatDuration(mNextFlush - System.currentTimeMillis(), pw); |
| 839 |
pw.print(" ("); pw.print(TimeUtils.logTimeOfDay(mNextFlush)); pw.println(")"); |
| 840 |
} |
| 841 |
if (mFlushHistory != null) { |
| 842 |
pw.print(prefix); pw.println("flush history:"); |
| 843 |
mFlushHistory.reverseDump( null, pw, null); pw.println(); |
| 844 |
} else { |
| 845 |
pw.print(prefix); pw.println("not logging flush history"); |
| 846 |
} |
| 847 |
|
| 848 |
super.dump(prefix, pw); |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
private String getActivityName() { |
| 855 |
return mComponentName == null |
| 856 |
? "pkg:" + mContext.getPackageName() |
| 857 |
: "act:" + mComponentName.flattenToShortString(); |
| 858 |
} |
| 859 |
|
| 860 |
@NonNull |
| 861 |
private String getDebugState() { |
| 862 |
return getActivityName() + " [state=" + getStateAsString(mState) + ", disabled=" |
| 863 |
+ mDisabled.get() + "]"; |
| 864 |
} |
| 865 |
|
| 866 |
@NonNull |
| 867 |
private String getDebugState(@FlushReason int reason) { |
| 868 |
return getDebugState() + ", reason=" + getFlushReasonAsString(reason); |
| 869 |
} |
| 870 |
} |