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

Commit 3bcab82f authored by Xi Zhang's avatar Xi Zhang
Browse files

Make RecordingController using the interface's callback

The ScreenRecordLegacyUxControllerImpl is a wrapper of RecordingController. It also forwards the callback to the wrapped RecordingController using a bridge, which has caused some issues.

In this CL, we remove RecordingController's callback and replace it with the parent interface's callback, since they are identical.

Test: manual tests
Flag: EXEMPT strict mechanical refactors
Bug: 412778881

Change-Id: Ib2b9c76afd995866e1be6c53532a44481ff43ddd
parent 4d4495e2
Loading
Loading
Loading
Loading
+8 −40
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ import java.util.concurrent.Executor;
 * Helper class to initiate a screen recording
 */
public class RecordingController
        implements CallbackController<RecordingController.RecordingStateChangeCallback> {
        implements CallbackController<ScreenRecordUxController.StateChangeCallback> {

    private final ScreenRecordUxController mScreenRecordUxController;
    private boolean mIsStarting;
@@ -74,7 +74,7 @@ public class RecordingController
    private final ScreenRecordPermissionContentManager.Factory
            mScreenRecordPermissionContentManagerFactory;

    private final CopyOnWriteArrayList<RecordingStateChangeCallback> mListeners =
    private final CopyOnWriteArrayList<ScreenRecordUxController.StateChangeCallback> mListeners =
            new CopyOnWriteArrayList<>();

    private final Lazy<ScreenCaptureDevicePolicyResolver> mDevicePolicyResolver;
@@ -207,7 +207,7 @@ public class RecordingController
        mCountDownTimer = new CountDownTimer(ms, interval) {
            @Override
            public void onTick(long millisUntilFinished) {
                for (RecordingStateChangeCallback cb : mListeners) {
                for (ScreenRecordUxController.StateChangeCallback cb : mListeners) {
                    cb.onCountdown(millisUntilFinished);
                }
            }
@@ -216,7 +216,7 @@ public class RecordingController
            public void onFinish() {
                mIsStarting = false;
                mIsRecording = true;
                for (RecordingStateChangeCallback cb : mListeners) {
                for (ScreenRecordUxController.StateChangeCallback cb : mListeners) {
                    cb.onCountdownEnd();
                }
                try {
@@ -248,7 +248,7 @@ public class RecordingController
        }
        mIsStarting = false;

        for (RecordingStateChangeCallback cb : mListeners) {
        for (ScreenRecordUxController.StateChangeCallback cb : mListeners) {
            cb.onCountdownEnd();
        }
    }
@@ -298,7 +298,7 @@ public class RecordingController
            mBroadcastDispatcher.unregisterReceiver(mStateChangeReceiver);
        }
        mIsRecording = isRecording;
        for (RecordingStateChangeCallback cb : mListeners) {
        for (ScreenRecordUxController.StateChangeCallback cb : mListeners) {
            if (isRecording) {
                cb.onRecordingStart();
            } else {
@@ -312,44 +312,12 @@ public class RecordingController
    }

    @Override
    public void addCallback(@NonNull RecordingStateChangeCallback listener) {
    public void addCallback(@NonNull ScreenRecordUxController.StateChangeCallback listener) {
        mListeners.add(listener);
    }

    @Override
    public void removeCallback(@NonNull RecordingStateChangeCallback listener) {
    public void removeCallback(@NonNull ScreenRecordUxController.StateChangeCallback listener) {
        mListeners.remove(listener);
    }

    /**
     * A callback for changes in the screen recording state
     */
    public interface RecordingStateChangeCallback {
        /**
         * Called when a countdown to recording has updated
         *
         * @param millisUntilFinished Time in ms remaining in the countdown
         */
        default void onCountdown(long millisUntilFinished) {
        }

        /**
         * Called when a countdown to recording has ended. This is a separate method so that if
         * needed, listeners can handle cases where recording fails to start
         */
        default void onCountdownEnd() {
        }

        /**
         * Called when a screen recording has started
         */
        default void onRecordingStart() {
        }

        /**
         * Called when a screen recording has ended
         */
        default void onRecordingEnd() {
        }
    }
}
+5 −42
Original line number Diff line number Diff line
@@ -31,8 +31,7 @@ import java.util.concurrent.Executor
/**
 * [ScreenRecordUxController] implementation of the existing screen recording workflow.
 *
 * This class forwards all calls to the underlying [RecordingController] instance. It manages its
 * own set of listeners and bridges them to the original controller's callback mechanism.
 * This class forwards all calls to the underlying [RecordingController] instance.
 */
class ScreenRecordLegacyUxControllerImpl(
    @Main private val mainExecutor: Executor,
@@ -61,28 +60,6 @@ class ScreenRecordLegacyUxControllerImpl(
            screenRecordPermissionContentManagerFactory,
        )

    private val listeners = mutableListOf<ScreenRecordUxController.StateChangeCallback>()

    // Internal bridge callback that forwards events from the Java controller to Kotlin listeners.
    private val internalCallbackBridge =
        object : RecordingController.RecordingStateChangeCallback {
            override fun onCountdown(millisUntilFinished: Long) {
                listeners.forEach { it.onCountdown(millisUntilFinished) }
            }

            override fun onCountdownEnd() {
                listeners.forEach { it.onCountdownEnd() }
            }

            override fun onRecordingStart() {
                listeners.forEach { it.onRecordingStart() }
            }

            override fun onRecordingEnd() {
                listeners.forEach { it.onRecordingEnd() }
            }
        }

    /** @see RecordingController.isScreenCaptureDisabled */
    override val isScreenCaptureDisabled: Boolean
        get() = recordingController.isScreenCaptureDisabled
@@ -151,35 +128,21 @@ class ScreenRecordLegacyUxControllerImpl(
    }

    /**
     * Adds a listener to receive screen recording state changes. Registers the internal bridge
     * callback with the underlying [RecordingController] if this is the first listener added.
     * Adds a listener to receive screen recording state changes.
     *
     * @param listener The [StateChangeCallback] to add.
     */
    override fun addCallback(listener: ScreenRecordUxController.StateChangeCallback) {
        // Add the listener before checking size and potentially registering the bridge
        if (listeners.add(listener)) {
            // If this was the first listener added, register the bridge callback
            if (listeners.size == 1) {
                recordingController.addCallback(internalCallbackBridge)
            }
        }
        recordingController.addCallback(listener)
    }

    /**
     * Removes a listener that was previously added. Unregisters the internal bridge callback from
     * the underlying [RecordingController] if this was the last listener removed.
     * Removes a listener that was previously added.
     *
     * @param listener The [StateChangeCallback] to remove.
     */
    override fun removeCallback(listener: ScreenRecordUxController.StateChangeCallback) {
        // Remove the listener before checking size and potentially unregistering the bridge
        if (listeners.remove(listener)) {
            // If this was the last listener, unregister the bridge callback
            if (listeners.isEmpty()) {
                recordingController.removeCallback(internalCallbackBridge)
            }
        }
        recordingController.removeCallback(listener)
    }

    companion object {
+1 −1
Original line number Diff line number Diff line
@@ -72,7 +72,7 @@ public class RecordingControllerTest extends SysuiTestCase {
    private FakeSystemClock mFakeSystemClock = new FakeSystemClock();
    private FakeExecutor mMainExecutor = new FakeExecutor(mFakeSystemClock);
    @Mock
    private RecordingController.RecordingStateChangeCallback mCallback;
    private ScreenRecordUxController.StateChangeCallback mCallback;
    @Mock
    private BroadcastDispatcher mBroadcastDispatcher;
    @Mock