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

Commit 34bb8bf7 authored by MingWei's avatar MingWei
Browse files

Make sure MainContentCaptureSession's control behaviour is not changed

Before moving to the background thread, most of the function in
MainContentCaptureSession is either run blokcing on the current thread
or posting to the main thread. Although we've done some optimization to
prevent unnecessary posting. The behaviour should only be presetned in
the treatment arm to ensure the experiment is not bias.

Test: CtsContentCaptureServiceTestCases, MainContentCaptureSessionTest.
BUG: 309411951
Change-Id: I8df106be6d9bcbeeaf5d236f0ed4c5fffd05b619
parent 08e5de96
Loading
Loading
Loading
Loading
+39 −11
Original line number Original line Diff line number Diff line
@@ -31,6 +31,7 @@ import static android.view.contentcapture.ContentCaptureHelper.getSanitizedStrin
import static android.view.contentcapture.ContentCaptureHelper.sDebug;
import static android.view.contentcapture.ContentCaptureHelper.sDebug;
import static android.view.contentcapture.ContentCaptureHelper.sVerbose;
import static android.view.contentcapture.ContentCaptureHelper.sVerbose;
import static android.view.contentcapture.ContentCaptureManager.RESULT_CODE_FALSE;
import static android.view.contentcapture.ContentCaptureManager.RESULT_CODE_FALSE;
import static android.view.contentcapture.flags.Flags.runOnBackgroundThreadEnabled;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
@@ -209,14 +210,14 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
                binder = resultData.getBinder(EXTRA_BINDER);
                binder = resultData.getBinder(EXTRA_BINDER);
                if (binder == null) {
                if (binder == null) {
                    Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
                    Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
                    mainSession.mHandler.post(() -> mainSession.resetSession(
                    mainSession.runOnContentCaptureThread(() -> mainSession.resetSession(
                            STATE_DISABLED | STATE_INTERNAL_ERROR));
                            STATE_DISABLED | STATE_INTERNAL_ERROR));
                    return;
                    return;
                }
                }
            } else {
            } else {
                binder = null;
                binder = null;
            }
            }
            mainSession.mHandler.post(() ->
            mainSession.runOnContentCaptureThread(() ->
                    mainSession.onSessionStarted(resultCode, binder));
                    mainSession.onSessionStarted(resultCode, binder));
        }
        }
    }
    }
@@ -256,7 +257,13 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
     */
     */
    void start(@NonNull IBinder token, @NonNull IBinder shareableActivityToken,
    void start(@NonNull IBinder token, @NonNull IBinder shareableActivityToken,
            @NonNull ComponentName component, int flags) {
            @NonNull ComponentName component, int flags) {
        runOnContentCaptureThread(() -> startImpl(token, shareableActivityToken, component, flags));
        if (runOnBackgroundThreadEnabled()) {
            runOnContentCaptureThread(
                    () -> startImpl(token, shareableActivityToken, component, flags));
        } else {
            // Preserve the control arm behaviour.
            startImpl(token, shareableActivityToken, component, flags);
        }
    }
    }


    private void startImpl(@NonNull IBinder token, @NonNull IBinder shareableActivityToken,
    private void startImpl(@NonNull IBinder token, @NonNull IBinder shareableActivityToken,
@@ -613,7 +620,12 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    @Override
    @Override
    public void flush(@FlushReason int reason) {
    public void flush(@FlushReason int reason) {
        if (runOnBackgroundThreadEnabled()) {
            runOnContentCaptureThread(() -> flushImpl(reason));
            runOnContentCaptureThread(() -> flushImpl(reason));
        } else {
            // Preserve the control arm behaviour.
            flushImpl(reason);
        }
    }
    }


    private void flushImpl(@FlushReason int reason) {
    private void flushImpl(@FlushReason int reason) {
@@ -904,7 +916,12 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
    /** public because is also used by ViewRootImpl */
    /** public because is also used by ViewRootImpl */
    public void notifyContentCaptureEvents(
    public void notifyContentCaptureEvents(
            @NonNull SparseArray<ArrayList<Object>> contentCaptureEvents) {
            @NonNull SparseArray<ArrayList<Object>> contentCaptureEvents) {
        if (runOnBackgroundThreadEnabled()) {
            runOnContentCaptureThread(() -> notifyContentCaptureEventsImpl(contentCaptureEvents));
            runOnContentCaptureThread(() -> notifyContentCaptureEventsImpl(contentCaptureEvents));
        } else {
            // Preserve the control arm behaviour.
            notifyContentCaptureEventsImpl(contentCaptureEvents);
        }
    }
    }


    private void notifyContentCaptureEventsImpl(
    private void notifyContentCaptureEventsImpl(
@@ -1076,19 +1093,30 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
     * </p>
     * </p>
     */
     */
    private void runOnContentCaptureThread(@NonNull Runnable r) {
    private void runOnContentCaptureThread(@NonNull Runnable r) {
        if (runOnBackgroundThreadEnabled()) {
            if (!mHandler.getLooper().isCurrentThread()) {
            if (!mHandler.getLooper().isCurrentThread()) {
                mHandler.post(r);
                mHandler.post(r);
            } else {
            } else {
                r.run();
                r.run();
            }
            }
        } else {
            // Preserve the control arm behaviour to always post to the handler.
            mHandler.post(r);
        }
    }
    }


    private void clearAndRunOnContentCaptureThread(@NonNull Runnable r, int what) {
    private void clearAndRunOnContentCaptureThread(@NonNull Runnable r, int what) {
        if (runOnBackgroundThreadEnabled()) {
            if (!mHandler.getLooper().isCurrentThread()) {
            if (!mHandler.getLooper().isCurrentThread()) {
                mHandler.removeMessages(what);
                mHandler.removeMessages(what);
                mHandler.post(r);
                mHandler.post(r);
            } else {
            } else {
                r.run();
                r.run();
            }
            }
        } else {
            // Preserve the control arm behaviour to always post to the handler.
            mHandler.removeMessages(what);
            mHandler.post(r);
        }
    }
    }
}
}