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

Commit 77d29661 authored by Sarah Chin's avatar Sarah Chin
Browse files

Add car mode changed listener to DeviceStateMonitor

Since Android Auto is a private display, isScreenOn() can't accurately
determine whether the Android Auto display is on or not. Add a listener
for car mode changed to accurately consider the Android Auto display in
shouldEnableHighPowerConsumptionIndications().
Also add more logs for other states.

Test: atest DeviceStateMonitorTest
Bug: 157257888
Change-Id: Id56899b14f46bcf6ae2d800e53b44aad74a75923
Merged-In: Id56899b14f46bcf6ae2d800e53b44aad74a75923
parent 2f3350dd
Loading
Loading
Loading
Loading
+51 −8
Original line number Diff line number Diff line
@@ -20,10 +20,12 @@ import static android.hardware.radio.V1_0.DeviceStateType.CHARGING_STATE;
import static android.hardware.radio.V1_0.DeviceStateType.LOW_DATA_EXPECTED;
import static android.hardware.radio.V1_0.DeviceStateType.POWER_SAVE_MODE;

import android.app.UiModeManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.hardware.display.DisplayManager;
import android.hardware.radio.V1_5.IndicationFilter;
import android.net.ConnectivityManager;
@@ -67,6 +69,7 @@ public class DeviceStateMonitor extends Handler {
    protected static final String TAG = DeviceStateMonitor.class.getSimpleName();

    static final int EVENT_RIL_CONNECTED                = 0;
    static final int EVENT_CAR_MODE_CHANGED             = 1;
    @VisibleForTesting
    static final int EVENT_SCREEN_STATE_CHANGED         = 2;
    static final int EVENT_POWER_SAVE_MODE_CHANGED      = 3;
@@ -163,6 +166,13 @@ public class DeviceStateMonitor extends Handler {
     */
    private boolean mIsWifiConnected;

    /**
     * Car mode is on. True means the device is currently connected to Android Auto. This should be
     * handled by mIsScreenOn, but the Android Auto display is private and not accessible by
     * DeviceStateMonitor from DisplayMonitor.
     */
    private boolean mIsCarModeOn;

    /**
     * True indicates we should always enable the signal strength reporting from radio.
     */
@@ -232,6 +242,14 @@ public class DeviceStateMonitor extends Handler {
                    msg = obtainMessage(EVENT_TETHERING_STATE_CHANGED);
                    msg.arg1 = isTetheringOn ? 1 : 0;
                    break;
                case UiModeManager.ACTION_ENTER_CAR_MODE_PRIORITIZED:
                    msg = obtainMessage(EVENT_CAR_MODE_CHANGED);
                    msg.arg1 = 1; // car mode on
                    break;
                case UiModeManager.ACTION_EXIT_CAR_MODE_PRIORITIZED:
                    msg = obtainMessage(EVENT_CAR_MODE_CHANGED);
                    msg.arg1 = 0; // car mode off
                    break;
                default:
                    log("Unexpected broadcast intent: " + intent, false);
                    return;
@@ -255,6 +273,7 @@ public class DeviceStateMonitor extends Handler {
        mIsPowerSaveOn = isPowerSaveModeOn();
        mIsCharging = isDeviceCharging();
        mIsScreenOn = isScreenOn();
        mIsCarModeOn = isCarModeOn();
        // Assuming tethering is always off after boot up.
        mIsTetheringOn = false;
        mIsLowDataExpected = false;
@@ -264,6 +283,7 @@ public class DeviceStateMonitor extends Handler {
                + ", mIsCharging=" + mIsCharging
                + ", mIsPowerSaveOn=" + mIsPowerSaveOn
                + ", mIsLowDataExpected=" + mIsLowDataExpected
                + ", mIsCarModeOn=" + mIsCarModeOn
                + ", mIsWifiConnected=" + mIsWifiConnected
                + ", mIsAlwaysSignalStrengthReportingEnabled="
                + mIsAlwaysSignalStrengthReportingEnabled, false);
@@ -273,6 +293,8 @@ public class DeviceStateMonitor extends Handler {
        filter.addAction(BatteryManager.ACTION_CHARGING);
        filter.addAction(BatteryManager.ACTION_DISCHARGING);
        filter.addAction(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
        filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE_PRIORITIZED);
        filter.addAction(UiModeManager.ACTION_EXIT_CAR_MODE_PRIORITIZED);
        mPhone.getContext().registerReceiver(mBroadcastReceiver, filter, null, mPhone);

        mPhone.mCi.registerForRilConnected(this, EVENT_RIL_CONNECTED, null);
@@ -375,7 +397,8 @@ public class DeviceStateMonitor extends Handler {
        // 1. The device is charging.
        // 2. When the screen is on.
        // 3. When the tethering is on.
        return mIsCharging || mIsScreenOn || mIsTetheringOn;
        // 4. When car mode (Android Auto) is on.
        return mIsCharging || mIsScreenOn || mIsTetheringOn || mIsCarModeOn;
    }

    /**
@@ -404,14 +427,13 @@ public class DeviceStateMonitor extends Handler {
            case EVENT_POWER_SAVE_MODE_CHANGED:
            case EVENT_CHARGING_STATE_CHANGED:
            case EVENT_TETHERING_STATE_CHANGED:
            case EVENT_UPDATE_ALWAYS_REPORT_SIGNAL_STRENGTH:
            case EVENT_CAR_MODE_CHANGED:
                onUpdateDeviceState(msg.what, msg.arg1 != 0);
                break;
            case EVENT_WIFI_CONNECTION_CHANGED:
                onUpdateDeviceState(msg.what, msg.arg1 != WIFI_UNAVAILABLE);
                break;
            case EVENT_UPDATE_ALWAYS_REPORT_SIGNAL_STRENGTH:
                onUpdateDeviceState(msg.what, msg.arg1 != 0);
                break;
            default:
                throw new IllegalStateException("Unexpected message arrives. msg = " + msg.what);
        }
@@ -452,6 +474,10 @@ public class DeviceStateMonitor extends Handler {
                if (mIsAlwaysSignalStrengthReportingEnabled == state) return;
                mIsAlwaysSignalStrengthReportingEnabled = state;
                break;
            case EVENT_CAR_MODE_CHANGED:
                if (mIsCarModeOn == state) return;
                mIsCarModeOn = state;
                break;
            default:
                return;
        }
@@ -622,7 +648,9 @@ public class DeviceStateMonitor extends Handler {
    private boolean isPowerSaveModeOn() {
        final PowerManager pm = (PowerManager) mPhone.getContext().getSystemService(
                Context.POWER_SERVICE);
        return pm.isPowerSaveMode();
        boolean retval = pm.isPowerSaveMode();
        log("isPowerSaveModeOn=" + retval, true);
        return retval;
    }

    /**
@@ -634,12 +662,14 @@ public class DeviceStateMonitor extends Handler {
    private boolean isDeviceCharging() {
        final BatteryManager bm = (BatteryManager) mPhone.getContext().getSystemService(
                Context.BATTERY_SERVICE);
        return bm.isCharging();
        boolean retval = bm.isCharging();
        log("isDeviceCharging=" + retval, true);
        return retval;
    }

    /**
     * @return True if one the device's screen (e.g. main screen, wifi display, HDMI display, or
     *         Android auto, etc...) is on.
     * @return True if one the device's screen (e.g. main screen, wifi display, HDMI display etc...)
     * is on.
     */
    private boolean isScreenOn() {
        // Note that we don't listen to Intent.SCREEN_ON and Intent.SCREEN_OFF because they are no
@@ -666,6 +696,18 @@ public class DeviceStateMonitor extends Handler {
        return false;
    }

    /**
     * @return True if car mode (Android Auto) is on.
     */
    private boolean isCarModeOn() {
        final UiModeManager umm = (UiModeManager) mPhone.getContext().getSystemService(
                Context.UI_MODE_SERVICE);
        if (umm == null) return false;
        boolean retval = umm.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR;
        log("isCarModeOn=" + retval, true);
        return retval;
    }

    /**
     * Register for PhysicalChannelConfig notifications changed. On change, msg.obj will be an
     * AsyncResult with a boolean result. AsyncResult.result is true if notifications are enabled
@@ -714,6 +756,7 @@ public class DeviceStateMonitor extends Handler {
        ipw.println("mIsCharging=" + mIsCharging);
        ipw.println("mIsPowerSaveOn=" + mIsPowerSaveOn);
        ipw.println("mIsLowDataExpected=" + mIsLowDataExpected);
        ipw.println("mIsCarModeOn=" + mIsCarModeOn);
        ipw.println("mUnsolicitedResponseFilter=" + mUnsolicitedResponseFilter);
        ipw.println("mIsWifiConnected=" + mIsWifiConnected);
        ipw.println("mIsAlwaysSignalStrengthReportingEnabled="