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

Commit f6a376bc authored by Paul Colta's avatar Paul Colta
Browse files

HDMI: Improve logic in PowerStatusMonitorActionFromPlayback

PowerStatusMonitorActionFromPlayback queries the power state of the TV once per minute. This CL improves the logic of the action to avoid overflow the CEC bus with unwanted query messages.

Test: atest com.android.server.hdmi & make
Bug: 388905874
Flag: EXEMPT bugfix
Change-Id: I8067868603f408657028d673aaa417c9d5ce089e
parent a19ea443
Loading
Loading
Loading
Loading
+16 −27
Original line number Original line Diff line number Diff line
@@ -28,21 +28,17 @@ import com.android.internal.annotations.VisibleForTesting;
 */
 */
public class PowerStatusMonitorActionFromPlayback extends HdmiCecFeatureAction {
public class PowerStatusMonitorActionFromPlayback extends HdmiCecFeatureAction {
    private static final String TAG = "PowerStatusMonitorActionFromPlayback";
    private static final String TAG = "PowerStatusMonitorActionFromPlayback";
    // State that waits for next monitoring.
    private static final int STATE_WAIT_FOR_NEXT_MONITORING = 1;


    // State that waits for <Report Power Status> once sending <Give Device Power Status>
    // State that waits for <Report Power Status> once sending <Give Device Power Status>
    // to all external devices.
    // to the TV.
    private static final int STATE_WAIT_FOR_REPORT_POWER_STATUS = 1;
    private static final int STATE_WAIT_FOR_REPORT_POWER_STATUS = 2;
    // State that waits for next monitoring.

    private static final int STATE_WAIT_FOR_NEXT_MONITORING = 2;
    // Monitoring interval (60s)
    // Monitoring interval (60s)
    @VisibleForTesting
    @VisibleForTesting
    protected static final int MONITORING_INTERVAL_MS = 60000;
    protected static final int MONITORING_INTERVAL_MS = 60000;
    // Timeout once sending <Give Device Power Status>
    // Timeout once sending <Give Device Power Status>
    private static final int REPORT_POWER_STATUS_TIMEOUT_MS = 5000;
    // Maximum number of retries in case the <Give Device Power Status> failed being sent or times
    // out.
    private static final int GIVE_POWER_STATUS_FOR_SOURCE_RETRIES = 5;
    private int mPowerStatusRetries = 0;


    PowerStatusMonitorActionFromPlayback(HdmiCecLocalDevice source) {
    PowerStatusMonitorActionFromPlayback(HdmiCecLocalDevice source) {
        super(source);
        super(source);
@@ -68,11 +64,10 @@ public class PowerStatusMonitorActionFromPlayback extends HdmiCecFeatureAction {


    private boolean handleReportPowerStatusFromTv(HdmiCecMessage cmd) {
    private boolean handleReportPowerStatusFromTv(HdmiCecMessage cmd) {
        int powerStatus = cmd.getParams()[0] & 0xFF;
        int powerStatus = cmd.getParams()[0] & 0xFF;
        mState = STATE_WAIT_FOR_NEXT_MONITORING;
        addTimer(mState, MONITORING_INTERVAL_MS);
        if (powerStatus == POWER_STATUS_STANDBY) {
        if (powerStatus == POWER_STATUS_STANDBY) {
            Slog.d(TAG, "TV reported it turned off, going to sleep.");
            Slog.d(TAG, "TV reported it turned off, going to sleep.");
            source().getService().standby();
            source().getService().standby();
            finish();
            return true;
            return true;
        }
        }
        return false;
        return false;
@@ -80,13 +75,19 @@ public class PowerStatusMonitorActionFromPlayback extends HdmiCecFeatureAction {


    @Override
    @Override
    void handleTimerEvent(int state) {
    void handleTimerEvent(int state) {
        if (mState != state) {
            return;
        }

        switch (mState) {
        switch (mState) {
            case STATE_WAIT_FOR_NEXT_MONITORING:
            case STATE_WAIT_FOR_NEXT_MONITORING:
                mPowerStatusRetries = 0;
                queryPowerStatus();
                queryPowerStatus();
                break;
                break;
            case STATE_WAIT_FOR_REPORT_POWER_STATUS:
            case STATE_WAIT_FOR_REPORT_POWER_STATUS:
                handleTimeout();
                mState = STATE_WAIT_FOR_NEXT_MONITORING;
                addTimer(mState, MONITORING_INTERVAL_MS);
                break;
            default:
                break;
                break;
        }
        }
    }
    }
@@ -96,18 +97,6 @@ public class PowerStatusMonitorActionFromPlayback extends HdmiCecFeatureAction {
                Constants.ADDR_TV));
                Constants.ADDR_TV));


        mState = STATE_WAIT_FOR_REPORT_POWER_STATUS;
        mState = STATE_WAIT_FOR_REPORT_POWER_STATUS;
        addTimer(mState, REPORT_POWER_STATUS_TIMEOUT_MS);
        addTimer(mState, HdmiConfig.TIMEOUT_MS);
    }

    private void handleTimeout() {
        if (mState == STATE_WAIT_FOR_REPORT_POWER_STATUS) {
            if (mPowerStatusRetries++ < GIVE_POWER_STATUS_FOR_SOURCE_RETRIES) {
                queryPowerStatus();
            } else {
                mPowerStatusRetries = 0;
                mState = STATE_WAIT_FOR_NEXT_MONITORING;
                addTimer(mState, MONITORING_INTERVAL_MS);
            }
        }
    }
    }
}
}
+3 −0
Original line number Original line Diff line number Diff line
@@ -2868,6 +2868,9 @@ public class HdmiCecLocalDevicePlaybackTest {


        assertThat(mPowerManager.isInteractive()).isTrue();
        assertThat(mPowerManager.isInteractive()).isTrue();
        mNativeWrapper.clearResultMessages();
        mNativeWrapper.clearResultMessages();
        mTestLooper.moveTimeForward(TIMEOUT_MS);
        mTestLooper.dispatchAll();

        mTestLooper.moveTimeForward(MONITORING_INTERVAL_MS);
        mTestLooper.moveTimeForward(MONITORING_INTERVAL_MS);
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();