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

Commit 23d41623 authored by Rambo Wang's avatar Rambo Wang
Browse files

DeviceStateMonitor: turn double negative into forward positive logic

On determining when we should turn on/off indication filters,
we have a double negative logic. eg. shouldTurnOff return false
to indicate we should turn on the filter. This is a bit confusing
and prone to introducing bugs.

This CL turns this into forward logic, eg. shouldTurnOn return true
to indicate we should turn on, making the logic a bit easier to
understand.

Bug: 151116827
Test: atest DeviceStateMonitorTest
Change-Id: I9ace326defc9fbf9f14275e0f226abbaae4daa42
parent 302183b6
Loading
Loading
Loading
Loading
+36 −41
Original line number Original line Diff line number Diff line
@@ -308,68 +308,64 @@ public class DeviceStateMonitor extends Handler {
    }
    }


    /**
    /**
     * @return True if signal strength update should be turned off.
     * @return True if signal strength update should be turned on.
     */
     */
    private boolean shouldTurnOffSignalStrength() {
    private boolean shouldTurnOnSignalStrength() {
        // We should not turn off signal strength update if one of the following condition is true.
        // We should turn on signal strength update if one of the following condition is true.
        // 1. The device is charging.
        // 1. The device is charging.
        // 2. When the screen is on.
        // 2. When the screen is on.
        // 3. Any of system services is registrating to always listen to signal strength changes
        // 3. Any of system services is registrating to always listen to signal strength changes
        if (mIsAlwaysSignalStrengthReportingEnabled || mIsCharging || mIsScreenOn) {
        return mIsAlwaysSignalStrengthReportingEnabled || mIsCharging || mIsScreenOn;
            return false;
        }

        // In all other cases, we turn off signal strength update.
        return true;
    }
    }


    /**
    /**
     * @return True if full network update should be turned off. Only significant changes will
     * @return True if full network update should be turned on. When off, only significant changes
     * trigger the network update unsolicited response.
     * will trigger the network update unsolicited response.
     */
     */
    private boolean shouldTurnOffFullNetworkUpdate() {
    private boolean shouldTurnOnFullNetworkUpdate() {
        return shouldTurnOffHighPowerConsumptionIndications();
        return shouldTurnOnHighPowerConsumptionIndications();
    }
    }


    /**
    /**
     * @return True if data dormancy status update should be turned off.
     * @return True if data dormancy status update should be turned on.
     */
     */
    private boolean shouldTurnOffDormancyUpdate() {
    private boolean shouldTurnOnDormancyUpdate() {
        return shouldTurnOffHighPowerConsumptionIndications();
        return shouldTurnOnHighPowerConsumptionIndications();
    }
    }


    /**
    /**
     * @return True if link capacity estimate update should be turned off.
     * @return True if link capacity estimate update should be turned on.
     */
     */
    private boolean shouldTurnOffLinkCapacityEstimate() {
    private boolean shouldTurnOnLinkCapacityEstimate() {
        return shouldTurnOffHighPowerConsumptionIndications();
        return shouldTurnOnHighPowerConsumptionIndications();
    }
    }


    /**
    /**
     * @return True if physical channel config update should be turned off.
     * @return True if physical channel config update should be turned on.
     */
     */
    private boolean shouldTurnOffPhysicalChannelConfig() {
    private boolean shouldTurnOnPhysicalChannelConfig() {
        return shouldTurnOffHighPowerConsumptionIndications();
        return shouldTurnOnHighPowerConsumptionIndications();
    }
    }


    /**
    /**
     * @return True if BarryingInfo update should be turned off.
     * @return True if BarryingInfo update should be turned on.
     */
     */
    private boolean shouldTurnOffBarringInfo() {
    private boolean shouldTurnOnBarringInfo() {
        return shouldTurnOffHighPowerConsumptionIndications();
        return shouldTurnOnHighPowerConsumptionIndications();
    }
    }


    /**
    /**
     * A common policy to determine if we should turn off unnecessary indications for power saving.
     * A common policy to determine if we should turn on necessary indications,
     * for power consumption's sake.
     *
     *
     * @return True if the response filter update should be turned off.
     * @return True if the response filter update should be turned on.
     */
     */
    private boolean shouldTurnOffHighPowerConsumptionIndications() {
    private boolean shouldTurnOnHighPowerConsumptionIndications() {
        // We should not turn off update if one of the following condition is true.
        // We should turn on update if one of the following condition is true.
        // 1. The device is charging.
        // 1. The device is charging.
        // 2. When the screen is on.
        // 2. When the screen is on.
        // 3. When the tethering is on.
        // 3. When the tethering is on.
        return !(mIsCharging || mIsScreenOn || mIsTetheringOn);
        return mIsCharging || mIsScreenOn || mIsTetheringOn;
    }
    }


    /**
    /**
@@ -418,22 +414,19 @@ public class DeviceStateMonitor extends Handler {
     * @param state True if enabled/on, otherwise disabled/off.
     * @param state True if enabled/on, otherwise disabled/off.
     */
     */
    private void onUpdateDeviceState(int eventType, boolean state) {
    private void onUpdateDeviceState(int eventType, boolean state) {
        boolean shouldPollBarringInfo = false;
        final boolean shouldTurnOnBarringInfoOld = shouldTurnOnBarringInfo();
        switch (eventType) {
        switch (eventType) {
            case EVENT_SCREEN_STATE_CHANGED:
            case EVENT_SCREEN_STATE_CHANGED:
                if (mIsScreenOn == state) return;
                if (mIsScreenOn == state) return;
                shouldPollBarringInfo = shouldTurnOffBarringInfo();
                mIsScreenOn = state;
                mIsScreenOn = state;
                break;
                break;
            case EVENT_CHARGING_STATE_CHANGED:
            case EVENT_CHARGING_STATE_CHANGED:
                if (mIsCharging == state) return;
                if (mIsCharging == state) return;
                shouldPollBarringInfo = shouldTurnOffBarringInfo();
                mIsCharging = state;
                mIsCharging = state;
                sendDeviceState(CHARGING_STATE, mIsCharging);
                sendDeviceState(CHARGING_STATE, mIsCharging);
                break;
                break;
            case EVENT_TETHERING_STATE_CHANGED:
            case EVENT_TETHERING_STATE_CHANGED:
                if (mIsTetheringOn == state) return;
                if (mIsTetheringOn == state) return;
                shouldPollBarringInfo = shouldTurnOffBarringInfo();
                mIsTetheringOn = state;
                mIsTetheringOn = state;
                break;
                break;
            case EVENT_POWER_SAVE_MODE_CHANGED:
            case EVENT_POWER_SAVE_MODE_CHANGED:
@@ -466,33 +459,35 @@ public class DeviceStateMonitor extends Handler {
        }
        }


        int newFilter = 0;
        int newFilter = 0;
        if (!shouldTurnOffSignalStrength()) {
        if (shouldTurnOnSignalStrength()) {
            newFilter |= IndicationFilter.SIGNAL_STRENGTH;
            newFilter |= IndicationFilter.SIGNAL_STRENGTH;
        }
        }


        if (!shouldTurnOffFullNetworkUpdate()) {
        if (shouldTurnOnFullNetworkUpdate()) {
            newFilter |= IndicationFilter.FULL_NETWORK_STATE;
            newFilter |= IndicationFilter.FULL_NETWORK_STATE;
        }
        }


        if (!shouldTurnOffDormancyUpdate()) {
        if (shouldTurnOnDormancyUpdate()) {
            newFilter |= IndicationFilter.DATA_CALL_DORMANCY_CHANGED;
            newFilter |= IndicationFilter.DATA_CALL_DORMANCY_CHANGED;
        }
        }


        if (!shouldTurnOffLinkCapacityEstimate()) {
        if (shouldTurnOnLinkCapacityEstimate()) {
            newFilter |= IndicationFilter.LINK_CAPACITY_ESTIMATE;
            newFilter |= IndicationFilter.LINK_CAPACITY_ESTIMATE;
        }
        }


        if (!shouldTurnOffPhysicalChannelConfig()) {
        if (shouldTurnOnPhysicalChannelConfig()) {
            newFilter |= IndicationFilter.PHYSICAL_CHANNEL_CONFIG;
            newFilter |= IndicationFilter.PHYSICAL_CHANNEL_CONFIG;
        }
        }


        if (!shouldTurnOffBarringInfo()) {
        final boolean shouldTurnOnBarringInfo = shouldTurnOnBarringInfo();
        if (shouldTurnOnBarringInfo) {
            newFilter |= IndicationFilter.BARRING_INFO;
            newFilter |= IndicationFilter.BARRING_INFO;
        }
        }


        setUnsolResponseFilter(newFilter, false);
        setUnsolResponseFilter(newFilter, false);


        if (shouldPollBarringInfo) {
        // Pull barring info AFTER setting filter, the order matters
        if (shouldTurnOnBarringInfo && !shouldTurnOnBarringInfoOld) {
            if (DBG) log("Manually pull barring info...", true);
            if (DBG) log("Manually pull barring info...", true);
            // use a null message since we don't care of receiving response
            // use a null message since we don't care of receiving response
            mPhone.mCi.getBarringInfo(null);
            mPhone.mCi.getBarringInfo(null);