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

Commit c6a2e671 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
parent 2b210e10
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;
@@ -69,6 +71,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;
@@ -169,6 +172,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.
     */
@@ -238,6 +248,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;
@@ -261,6 +279,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;
@@ -270,6 +289,7 @@ public class DeviceStateMonitor extends Handler {
                + ", mIsCharging=" + mIsCharging
                + ", mIsPowerSaveOn=" + mIsPowerSaveOn
                + ", mIsLowDataExpected=" + mIsLowDataExpected
                + ", mIsCarModeOn=" + mIsCarModeOn
                + ", mIsWifiConnected=" + mIsWifiConnected
                + ", mIsAlwaysSignalStrengthReportingEnabled="
                + mIsAlwaysSignalStrengthReportingEnabled, false);
@@ -279,6 +299,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);
@@ -381,7 +403,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;
    }

    /**
@@ -434,14 +457,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);
        }
@@ -482,6 +504,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;
        }
@@ -652,7 +678,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;
    }

    /**
@@ -664,12 +692,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
@@ -696,6 +726,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
@@ -744,6 +786,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="