Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c943ffba authored by Adam He's avatar Adam He Committed by Android (Google) Code Review
Browse files

Merge "Push buffer with TEXT_CHANGE_TIMEOUT from...

Merge "Push buffer with TEXT_CHANGE_TIMEOUT from device_config_text_change_flush_frequency if text_change event received."
parents a49171ea ba9f64d4
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -153,6 +153,8 @@ public abstract class ContentCaptureSession implements AutoCloseable {
    public static final int FLUSH_REASON_SESSION_FINISHED = 4;
    /** @hide */
    public static final int FLUSH_REASON_IDLE_TIMEOUT = 5;
    /** @hide */
    public static final int FLUSH_REASON_TEXT_CHANGE_TIMEOUT = 6;

    /** @hide */
    @IntDef(prefix = { "FLUSH_REASON_" }, value = {
@@ -160,7 +162,8 @@ public abstract class ContentCaptureSession implements AutoCloseable {
            FLUSH_REASON_VIEW_ROOT_ENTERED,
            FLUSH_REASON_SESSION_STARTED,
            FLUSH_REASON_SESSION_FINISHED,
            FLUSH_REASON_IDLE_TIMEOUT
            FLUSH_REASON_IDLE_TIMEOUT,
            FLUSH_REASON_TEXT_CHANGE_TIMEOUT
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface FlushReason{}
@@ -525,6 +528,8 @@ public abstract class ContentCaptureSession implements AutoCloseable {
                return "FINISHED";
            case FLUSH_REASON_IDLE_TIMEOUT:
                return "IDLE";
            case FLUSH_REASON_TEXT_CHANGE_TIMEOUT:
                return "TEXT_CHANGE";
            default:
                return "UNKOWN-" + reason;
        }
+46 −6
Original line number Diff line number Diff line
@@ -125,6 +125,11 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
    // Used just for debugging purposes (on dump)
    private long mNextFlush;

    /**
     * Whether the next buffer flush is queued by a text changed event.
     */
    private boolean mNextFlushForTextChanged = false;

    @Nullable
    private final LocalLog mFlushHistory;

@@ -331,7 +336,21 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
        final boolean bufferEvent = numberEvents < maxBufferSize;

        if (bufferEvent && !forceFlush) {
            scheduleFlush(FLUSH_REASON_IDLE_TIMEOUT, /* checkExisting= */ true);
            final int flushReason;
            if (eventType == TYPE_VIEW_TEXT_CHANGED) {
                mNextFlushForTextChanged = true;
                flushReason = FLUSH_REASON_TEXT_CHANGE_TIMEOUT;
            } else {
                if (mNextFlushForTextChanged) {
                    if (sVerbose) {
                        Log.i(TAG, "Not scheduling flush because next flush is for text changed");
                    }
                    return;
                }

                flushReason = FLUSH_REASON_IDLE_TIMEOUT;
            }
            scheduleFlush(flushReason, /* checkExisting= */ true);
            return;
        }

@@ -392,14 +411,25 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
            // "Renew" the flush message by removing the previous one
            mHandler.removeMessages(MSG_FLUSH);
        }
        final int idleFlushingFrequencyMs = mManager.mOptions.idleFlushingFrequencyMs;
        mNextFlush = System.currentTimeMillis() + idleFlushingFrequencyMs;

        final int flushFrequencyMs;
        if (reason == FLUSH_REASON_IDLE_TIMEOUT) {
            flushFrequencyMs = mManager.mOptions.idleFlushingFrequencyMs;
        } else if (reason == FLUSH_REASON_TEXT_CHANGE_TIMEOUT) {
            flushFrequencyMs = mManager.mOptions.textChangeFlushingFrequencyMs;
        } else {
            Log.e(TAG, "handleScheduleFlush(" + getDebugState(reason) + "): not called with a "
                    + "timeout reason.");
            return;
        }

        mNextFlush = System.currentTimeMillis() + flushFrequencyMs;
        if (sVerbose) {
            Log.v(TAG, "handleScheduleFlush(): scheduled to flush in "
                    + idleFlushingFrequencyMs + "ms: " + TimeUtils.logTimeOfDay(mNextFlush));
                    + flushFrequencyMs + "ms: " + TimeUtils.logTimeOfDay(mNextFlush));
        }
        // Post using a Runnable directly to trim a few μs from PooledLambda.obtainMessage()
        mHandler.postDelayed(() -> flushIfNeeded(reason), MSG_FLUSH, idleFlushingFrequencyMs);
        mHandler.postDelayed(() -> flushIfNeeded(reason), MSG_FLUSH, flushFrequencyMs);
    }

    @UiThread
@@ -448,6 +478,10 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
        try {
            mHandler.removeMessages(MSG_FLUSH);

            if (reason == FLUSH_REASON_TEXT_CHANGE_TIMEOUT) {
                mNextFlushForTextChanged = false;
            }

            final ParceledListSlice<ContentCaptureEvent> events = clearEvents();
            mDirectServiceInterface.sendEvents(events);
        } catch (RemoteException e) {
@@ -625,8 +659,14 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
                    pw.println();
                }
            }
            pw.print(prefix); pw.print("mNextFlushForTextChanged: ");
            pw.println(mNextFlushForTextChanged);
            pw.print(prefix); pw.print("flush frequency: ");
            if (mNextFlushForTextChanged) {
                pw.println(mManager.mOptions.textChangeFlushingFrequencyMs);
            } else {
                pw.println(mManager.mOptions.idleFlushingFrequencyMs);
            }
            pw.print(prefix); pw.print("next flush: ");
            TimeUtils.formatDuration(mNextFlush - System.currentTimeMillis(), pw);
            pw.print(" ("); pw.print(TimeUtils.logTimeOfDay(mNextFlush)); pw.println(")");